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