Makistos
10/25/2013 - 12:25 PM

This code will print out all sysfs devices under a certain device class (which is supplied as a parameter). #linux #sysfs #c

This code will print out all sysfs devices under a certain device class (which is supplied as a parameter). #linux #sysfs #c

#include <sysfs/libsysfs.h>
#include <stdio.h>

/**
 * @brief Lists all the devices in the given class.
 *
 * @param className Name of the class (in /sys/class).
 */
void sysFsListDevices(char *className)
{
        struct sysfs_class *class;
        struct dlist *devices;
        struct sysfs_class_device *class_device;
        struct sysfs_device *device;

        class = sysfs_open_class(className);

        devices = sysfs_get_class_devices(class);

        dlist_for_each(devices) {
                class_device =
                    (struct sysfs_class_device *)devices->marker->data;
                printf("Class - Name: %s, classname: %s, path: %s\n",
                       class_device->name, class_device->classname,
                       class_device->path);

                device = sysfs_get_classdev_device(class_device);

                printf
                    ("Device - Name: %s, bus_id: %s, bus: %s, driver_name: %s, path: %s\n\n",
                     device->name, device->bus_id, device->bus,
                     device->driver_name, device->path);

        }

	return;
}