The libudev++ library is a C++ wrapper for libudev. It enables one to enumerate, monitor and introspect devices on the local system.
- libudev (provided by systemd)
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
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
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;
}
}
- Dimitry Ishenko - dimitry (dot) ishenko (at) (gee) mail (dot) com
This project is distributed under the GNU GPL license. See the LICENSE.md file for details.