sysfs.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "impl/sysfs.h"
  2. #include "light.h"
  3. #include "helpers.h"
  4. #include <stdio.h> //snprintf
  5. #include <stdlib.h> // malloc, free
  6. #include <dirent.h> // opendir, readdir
  7. static bool _impl_sysfs_init_leds(light_device_enumerator_t *enumerator)
  8. {
  9. // Create a new backlight device
  10. light_device_t *leds_device = malloc(sizeof(light_device_t));
  11. snprintf(leds_device->name, sizeof(leds_device->name), "%s", "leds");
  12. // Add it to the enumerator
  13. light_add_enumerator_device(enumerator, leds_device);
  14. // Iterate through the led controllers and create a device_target for each controller
  15. DIR *leds_dir;
  16. struct dirent *curr_entry;
  17. if((leds_dir = opendir("/sys/class/leds")) == NULL)
  18. {
  19. LIGHT_ERR("failed to open leds controller directory for reading");
  20. return false;
  21. }
  22. while((curr_entry = readdir(leds_dir)) != NULL)
  23. {
  24. // Skip dot entries
  25. if(curr_entry->d_name[0] == '.')
  26. {
  27. continue;
  28. }
  29. // Create a new device target for the controller
  30. light_device_target_t *led_controller = malloc(sizeof(light_device_target_t));
  31. snprintf(led_controller->name, sizeof(led_controller->name), "%s", curr_entry->d_name);
  32. // Setup the function bindings
  33. led_controller->set_value = impl_sysfs_set;
  34. led_controller->get_value = impl_sysfs_get;
  35. led_controller->get_max_value = impl_sysfs_getmax;
  36. led_controller->custom_command = impl_sysfs_command;
  37. // Setup the target data
  38. impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
  39. led_controller->device_target_data = dev_data;
  40. snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/leds/%s/brightness", curr_entry->d_name);
  41. snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/leds/%s/max_brightness", curr_entry->d_name);
  42. // Add it to the device
  43. light_add_device_target(leds_device, led_controller);
  44. }
  45. closedir(leds_dir);
  46. return true;
  47. }
  48. static bool _impl_sysfs_init_backlight(light_device_enumerator_t *enumerator)
  49. {
  50. // Create a new backlight device
  51. light_device_t *backlight_device = malloc(sizeof(light_device_t));
  52. snprintf(backlight_device->name, sizeof(backlight_device->name), "%s", "backlight");
  53. // Add it to the enumerator
  54. light_add_enumerator_device(enumerator, backlight_device);
  55. // Iterate through the backlight controllers and create a device_target for each controller
  56. DIR *backlight_dir;
  57. struct dirent *curr_entry;
  58. // Keep track of the best controller, and create an autodevice from that
  59. char best_controller[NAME_MAX];
  60. uint64_t best_value = 0;
  61. if((backlight_dir = opendir("/sys/class/backlight")) == NULL)
  62. {
  63. LIGHT_ERR("failed to open backlight controller directory for reading");
  64. return false;
  65. }
  66. while((curr_entry = readdir(backlight_dir)) != NULL)
  67. {
  68. // Skip dot entries
  69. if(curr_entry->d_name[0] == '.')
  70. {
  71. continue;
  72. }
  73. // Create a new device target for the controller
  74. light_device_target_t *backlight_controller = malloc(sizeof(light_device_target_t));
  75. snprintf(backlight_controller->name, sizeof(backlight_controller->name), "%s", curr_entry->d_name);
  76. // Setup the function bindings
  77. backlight_controller->set_value = impl_sysfs_set;
  78. backlight_controller->get_value = impl_sysfs_get;
  79. backlight_controller->get_max_value = impl_sysfs_getmax;
  80. backlight_controller->custom_command = impl_sysfs_command;
  81. // Setup the target data
  82. impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
  83. backlight_controller->device_target_data = dev_data;
  84. snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", curr_entry->d_name);
  85. snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", curr_entry->d_name);
  86. // Read the max brightness to get the best one
  87. uint64_t curr_value = 0;
  88. if(light_file_read_uint64(dev_data->max_brightness, &curr_value))
  89. {
  90. if(curr_value > best_value)
  91. {
  92. best_value = curr_value;
  93. snprintf(best_controller, sizeof(best_controller), "%s", backlight_controller->name);
  94. }
  95. }
  96. // Add it to the device
  97. light_add_device_target(backlight_device, backlight_controller);
  98. }
  99. closedir(backlight_dir);
  100. // Create an auto controller
  101. light_device_target_t *auto_controller = malloc(sizeof(light_device_target_t));
  102. snprintf(auto_controller->name, sizeof(auto_controller->name), "%s", "auto");
  103. // Setup the function bindings
  104. auto_controller->set_value = impl_sysfs_set;
  105. auto_controller->get_value = impl_sysfs_get;
  106. auto_controller->get_max_value = impl_sysfs_getmax;
  107. auto_controller->custom_command = impl_sysfs_command;
  108. // Setup the target data
  109. impl_sysfs_data_t *dev_data = malloc(sizeof(impl_sysfs_data_t));
  110. auto_controller->device_target_data = dev_data;
  111. snprintf(dev_data->brightness, sizeof(dev_data->brightness), "/sys/class/backlight/%s/brightness", best_controller);
  112. snprintf(dev_data->max_brightness, sizeof(dev_data->max_brightness), "/sys/class/backlight/%s/max_brightness", best_controller);
  113. // Add it to the device
  114. light_add_device_target(backlight_device, auto_controller);
  115. return true;
  116. }
  117. bool impl_sysfs_init(light_device_enumerator_t *enumerator)
  118. {
  119. // Create a device for the backlight
  120. _impl_sysfs_init_backlight(enumerator);
  121. // Create devices for the leds
  122. _impl_sysfs_init_leds(enumerator);
  123. return true;
  124. }
  125. bool impl_sysfs_free(light_device_enumerator_t *enumerator)
  126. {
  127. // Iterate through the devices in the enumerator
  128. for(uint64_t d = 0; d < enumerator->num_devices; d++)
  129. {
  130. light_device_t *curr_device = enumerator->devices[d];
  131. // If the given device points to NULL, we can safely skip it
  132. if(curr_device == NULL)
  133. {
  134. continue;
  135. }
  136. for(uint64_t t = 0; t < curr_device->num_targets; t++)
  137. {
  138. light_device_target_t *curr_target = curr_device->targets[t];
  139. if(curr_target == NULL)
  140. {
  141. continue;
  142. }
  143. if(curr_target->device_target_data != NULL)
  144. {
  145. free(curr_target->device_target_data);
  146. }
  147. free(curr_target);
  148. }
  149. // If the given device has any device_data, free it
  150. if(curr_device->device_data != NULL)
  151. {
  152. free(curr_device->device_data);
  153. }
  154. light_dispose_device(curr_device);
  155. // Free the device
  156. free(curr_device);
  157. }
  158. return true;
  159. }
  160. bool impl_sysfs_set(light_device_target_t *target, uint64_t in_value)
  161. {
  162. impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
  163. if(!light_file_write_uint64(data->brightness, in_value))
  164. {
  165. LIGHT_ERR("failed to write to sysfs device");
  166. return false;
  167. }
  168. return true;
  169. }
  170. bool impl_sysfs_get(light_device_target_t *target, uint64_t *out_value)
  171. {
  172. impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
  173. if(!light_file_read_uint64(data->brightness, out_value))
  174. {
  175. LIGHT_ERR("failed to read from sysfs device");
  176. return false;
  177. }
  178. return true;
  179. }
  180. bool impl_sysfs_getmax(light_device_target_t *target, uint64_t *out_value)
  181. {
  182. impl_sysfs_data_t *data = (impl_sysfs_data_t*)target->device_target_data;
  183. if(!light_file_read_uint64(data->max_brightness, out_value))
  184. {
  185. LIGHT_ERR("failed to read from sysfs device");
  186. return false;
  187. }
  188. return true;
  189. }
  190. bool impl_sysfs_command(light_device_target_t *target, char const *command_string)
  191. {
  192. // No current need for custom commands in sysfs enumerator
  193. // To implement support, simply parse the command string to your liking, and return false on invalid input or results!
  194. return true;
  195. }