Reading sysfs devices in Linux


/ Published in: C
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. #include <sysfs/libsysfs.h>
  2. #include <stdio.h>
  3.  
  4. /**
  5.  * @brief Lists all the devices in the given class.
  6.  *
  7.  * @param className Name of the class (in /sys/class).
  8.  */
  9. void sysFsListDevices(char *className)
  10. {
  11. struct sysfs_class *class;
  12. struct dlist *devices;
  13. struct sysfs_class_device *class_device;
  14. struct sysfs_device *device;
  15.  
  16. class = sysfs_open_class(className);
  17.  
  18. devices = sysfs_get_class_devices(class);
  19.  
  20. dlist_for_each(devices) {
  21. class_device =
  22. (struct sysfs_class_device *)devices->marker->data;
  23. printf("Class - Name: %s, classname: %s, path: %s\n",
  24. class_device->name, class_device->classname,
  25. class_device->path);
  26.  
  27. device = sysfs_get_classdev_device(class_device);
  28.  
  29. ("Device - Name: %s, bus_id: %s, bus: %s, driver_name: %s, path: %s\n\n",
  30. device->name, device->bus_id, device->bus,
  31. device->driver_name, device->path);
  32.  
  33. }
  34.  
  35. return;
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.