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