Skip to content

dimitry-ishenko-cpp/libudevpp

Repository files navigation

libudev++ – a Udev Library for C++

The libudev++ library is a C++ wrapper for libudev. It enables one to enumerate, monitor and introspect devices on the local system.

Installation

Prerequisites

Binary

Debian/Ubuntu/etc:

$ p=libudev++
$ v=1.0
$ wget https://github.com/dimitry-ishenko-cpp/libudevpp/releases/download/v${v}/${p}_${v}_amd64.deb
$ sudo apt install ./${p}_${v}_amd64.deb

Install the development package, if you are planning to develop applications with libudev++:

$ p=libudev++-dev
$ v=1.0
$ wget https://github.com/dimitry-ishenko-cpp/libudevpp/releases/download/v${v}/${p}_${v}_amd64.deb
$ sudo apt install ./${p}_${v}_amd64.deb

RaspberryPi:

$ p=libudev++
$ v=1.0
$ wget https://github.com/dimitry-ishenko-cpp/libudevpp/releases/download/v${v}/${p}_${v}_armhf.deb
$ sudo apt install ./${p}_${v}_armhf.deb

Install the development package, if you are planning to develop applications with libudev++:

$ p=libudev++-dev
$ v=1.0
$ wget https://github.com/dimitry-ishenko-cpp/libudevpp/releases/download/v${v}/${p}_${v}_armhf.deb
$ sudo apt install ./${p}_${v}_armhf.deb

From source

Stable version (requires CMake >= 3.1):

$ p=libudevpp
$ v=1.0
$ wget https://github.com/dimitry-ishenko-cpp/${p}/archive/v${v}.tar.gz
$ tar xzf v${v}.tar.gz
$ mkdir ${p}-${v}/build
$ cd ${p}-${v}/build
$ cmake ..
$ make
$ sudo make install

Latest master (requires git and CMake >= 3.1):

$ p=libudevpp
$ git clone --recursive https://github.com/dimitry-ishenko-cpp/${p}.git
$ mkdir ${p}/build
$ cd ${p}/build
$ cmake ..
$ make
$ sudo make install

Developing with libudev++

To develop with libudev++ simply add #include <udev++.hpp> to your program and -ludev++ to your linker. Following are a few examples demonstrating its capabilities:

Example #1:

auto ctx = udev::udev::instance();

udev::enumerate enumerate{ ctx };
enumerate.match_subsystem("block");

auto devices = enumerate.get();
for(auto const& device : devices)
{
    using namespace std;

    if(device.type() == "partition" && device.property("ID_BUS") == "ata")
    {
        cout << "Found hard disk" << endl
             << "Path: " << device.syspath() << endl
             << "Node: " << device.devnode() << endl
             << "  FS: " << device.property("ID_FS_TYPE") << endl
             << "Name: " << device.sysname() << endl
             << "   #: " << device.sysnum () << endl;
        cout << endl;
    }
}

Example #2:

auto ctx = udev::udev::instance();

udev::monitor monitor{ ctx };
monitor.match_device("block");

for(;;)
{
    using namespace std::chrono_literals;
    using namespace std;

    if(auto device = monitor.try_get_for(30s))
    {
        switch(device.action())
        {
        case udev::added:
            if(device.type() == "partition" && device.property("ID_BUS") == "usb")
            {
                cout << "USB drive plugged in" << endl
                     << "Path: " << device.syspath() << endl
                     << "Node: " << device.devnode() << endl
                     << "  FS: " << device.property("ID_FS_TYPE") << endl
                     << "Name: " << device.sysname() << endl
                     << "   #: " << device.sysnum () << endl;
                cout << endl;
            }
            break;

        case udev::removed:
            if(device.type() == "partition" && device.property("ID_BUS") == "usb")
            {
                cout << "USB drive " << device.devnode() << " unplugged" << endl;
                cout << endl;
            }
            break;
        }
    }
    else
    {
        cout << "Nothing seems to be happening" << endl;
        break;
    }
}

Authors

  • Dimitry Ishenko - dimitry (dot) ishenko (at) (gee) mail (dot) com

License

This project is distributed under the GNU GPL license. See the LICENSE.md file for details.

Acknowledgments