-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsysfs-led-bacon.c
365 lines (298 loc) · 10.5 KB
/
sysfs-led-bacon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/** @file sysfs-led-bacon.c
*
* mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
* <p>
* Copyright (C) 2017 Jolla Ltd.
* <p>
* @Author Willem-Jan de Hoog <[email protected]>
* @author Simo Piiroinen <[email protected]>
*
* mce-plugin-libhybris is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* mce-plugin-libhybris is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mce-plugin-libhybris; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* ========================================================================= *
* RGB led control: bacon backend
*
* Three channels, all of which:
* - must have 'brightness' control file
* - must have 'grpfreq', 'grppwm' and 'blink' to blink delay control file
* - must have 'reset' control file
*
* Based on code from device/oneplus/bacon/liblight/lights.c
* ========================================================================= */
#include "sysfs-led-bacon.h"
#include "sysfs-led-util.h"
#include "plugin-logging.h"
#include "plugin-config.h"
#include <stdio.h>
#include <string.h>
#include <glib.h>
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
typedef struct
{
const char *brightness;
const char *grpfreq;
const char *grppwm;
const char *blink;
const char *ledreset;
} led_paths_bacon_t;
typedef struct
{
int fd_brightness;
int fd_grpfreq;
int fd_grppwm;
int fd_blink;
int fd_ledreset;
int brightness;
int freq;
int pwm;
int blink;
int maxval;
} led_channel_bacon_t;
/* ------------------------------------------------------------------------- *
* ONE_CHANNEL
* ------------------------------------------------------------------------- */
static void led_channel_bacon_init (led_channel_bacon_t *self);
static void led_channel_bacon_close (led_channel_bacon_t *self);
static bool led_channel_bacon_probe (led_channel_bacon_t *self, const led_paths_bacon_t *path);
/* ------------------------------------------------------------------------- *
* ALL_CHANNELS
* ------------------------------------------------------------------------- */
static void led_control_bacon_enable_cb (void *data, bool enable);
static void led_control_bacon_blink_cb (void *data, int on_ms, int off_ms);
static void led_control_bacon_value_cb (void *data, int r, int g, int b);
static void led_control_bacon_close_cb (void *data);
bool led_control_bacon_probe (led_control_t *self);
/* ========================================================================= *
* ONE_CHANNEL
* ========================================================================= */
static void
led_channel_bacon_init(led_channel_bacon_t *self)
{
self->fd_brightness = -1;
self->fd_grpfreq = -1;
self->fd_grppwm = -1;
self->fd_blink = -1;
self->fd_ledreset = -1;
self->blink = 0;
self->maxval = 255; // load from max_brightness?
}
static void
led_channel_bacon_close(led_channel_bacon_t *self)
{
led_util_close_file(&self->fd_brightness);
led_util_close_file(&self->fd_grpfreq);
led_util_close_file(&self->fd_grppwm);
led_util_close_file(&self->fd_blink);
led_util_close_file(&self->fd_ledreset);
}
static bool
led_channel_bacon_probe(led_channel_bacon_t *self,
const led_paths_bacon_t *path)
{
bool res = false;
led_channel_bacon_close(self);
if( !led_util_open_file(&self->fd_brightness, path->brightness) ||
!led_util_open_file(&self->fd_grpfreq, path->grpfreq) ||
!led_util_open_file(&self->fd_grppwm, path->grppwm) ||
!led_util_open_file(&self->fd_blink, path->blink) ||
!led_util_open_file(&self->fd_ledreset, path->ledreset))
{
goto cleanup;
}
res = true;
cleanup:
if( !res ) led_channel_bacon_close(self);
return res;
}
/* ========================================================================= *
* ALL_CHANNELS
* ========================================================================= */
#define BACON_CHANNELS 3
static void
led_control_bacon_enable_cb(void *data, bool enable)
{
const led_channel_bacon_t *channel = data;
mce_log(LL_INFO, "led_control_bacon_enable_cb(%d)", enable);
if(!enable)
dprintf(channel->fd_ledreset, "%d", 1);
}
static void
led_control_bacon_blink_cb(void *data, int on_ms, int off_ms)
{
led_channel_bacon_t *channel = data;
mce_log(LL_INFO, "led_control_bacon_blink_cb(%d,%d)", on_ms, off_ms);
if( on_ms > 0 && off_ms > 0 ) {
int totalMS = on_ms + off_ms;
// the LED appears to blink about once per second if freq is 20
// 1000ms / 20 = 50
channel->freq = totalMS / 50;
// pwm specifies the ratio of ON versus OFF
// pwm = 0 -> always off
// pwm = 255 => always on
channel->pwm = (on_ms * 255) / totalMS;
// the low 4 bits are ignored, so round up if necessary
if( channel->pwm > 0 && channel->pwm < 16 )
channel->pwm = 16;
channel->blink = 1;
} else {
channel->blink = 0;
channel->freq = 0;
channel->pwm = 0;
}
if( channel->blink ) {
dprintf(channel->fd_grpfreq, "%d", channel->freq);
dprintf(channel->fd_grppwm, "%d", channel->pwm);
}
dprintf(channel->fd_blink, "%d", channel->blink);
}
static void
led_control_bacon_value_cb(void *data, int r, int g, int b)
{
led_channel_bacon_t *channel = data;
mce_log(LL_INFO, "led_control_bacon_value_cb(%d,%d,%d), blink=%d", r, g, b, channel->blink);
if( channel->blink )
dprintf(channel->fd_ledreset, "%d", 0);
(channel+0)->brightness = led_util_scale_value(r, (channel+0)->maxval);
(channel+1)->brightness = led_util_scale_value(g, (channel+1)->maxval);
(channel+2)->brightness = led_util_scale_value(b, (channel+2)->maxval);
dprintf((channel+0)->fd_brightness, "%d", (channel+0)->brightness);
dprintf((channel+1)->fd_brightness, "%d", (channel+1)->brightness);
dprintf((channel+2)->fd_brightness, "%d", (channel+2)->brightness);
if( channel->blink ) {
// need to reset the blink when changing color (do we?)
dprintf(channel->fd_grpfreq, "%d", channel->freq); // 1s
dprintf(channel->fd_grppwm, "%d", channel->pwm); // 50%?
dprintf(channel->fd_blink, "%d", 1);
} else
dprintf(channel->fd_blink, "%d", 0);
}
static void
led_control_bacon_close_cb(void *data)
{
led_channel_bacon_t *channel = data;
led_channel_bacon_close(channel + 0);
led_channel_bacon_close(channel + 1);
led_channel_bacon_close(channel + 2);
}
static bool
led_control_bacon_static_probe(led_channel_bacon_t *channel)
{
/** Sysfs control paths for RGB leds */
static const led_paths_bacon_t paths[][BACON_CHANNELS] =
{
// bacon
{
{
.brightness = "/sys/class/leds/red/brightness",
.grpfreq = "/sys/class/leds/red/device/grpfreq",
.grppwm = "/sys/class/leds/red/device/grppwm",
.blink = "/sys/class/leds/red/device/blink",
.ledreset = "/sys/class/leds/red/device/ledreset",
},
{
.brightness = "/sys/class/leds/green/brightness",
.grpfreq = "/sys/class/leds/green/device/grpfreq",
.grppwm = "/sys/class/leds/green/device/grppwm",
.blink = "/sys/class/leds/green/device/blink",
.ledreset = "/sys/class/leds/green/device/ledreset",
},
{
.brightness = "/sys/class/leds/blue/brightness",
.grpfreq = "/sys/class/leds/blue/device/grpfreq",
.grppwm = "/sys/class/leds/blue/device/grppwm",
.blink = "/sys/class/leds/blue/device/blink",
.ledreset = "/sys/class/leds/blue/device/ledreset",
}
},
};
bool ack = false;
for( size_t i = 0; i < G_N_ELEMENTS(paths); ++i ) {
if( led_channel_bacon_probe(&channel[0], &paths[i][0]) &&
led_channel_bacon_probe(&channel[1], &paths[i][1]) &&
led_channel_bacon_probe(&channel[2], &paths[i][2]) ) {
ack = true;
break;
}
}
return ack;
}
static bool
led_control_bacon_dynamic_probe(led_channel_bacon_t *channel)
{
/* See inifiles/60-bacon.ini for example config file */
static const objconf_t bacon_conf[] =
{
OBJCONF_FILE(led_paths_bacon_t, brightness, Brightness),
#if 0 // load from max_brightness?
OBJCONF_FILE(led_paths_bacon_t, max_brightness, MaxBrightness),
#endif
OBJCONF_FILE_EX(led_paths_bacon_t, grpfreq, GrpFreq,
"device/grpfreq"),
OBJCONF_FILE_EX(led_paths_bacon_t, grppwm, GrpPwm,
"device/grppwm"),
OBJCONF_FILE_EX(led_paths_bacon_t, blink, Blink,
"device/blink"),
OBJCONF_FILE_EX(led_paths_bacon_t, ledreset, LedReset,
"device/ledreset"),
OBJCONF_STOP
};
static const char * const pfix[BACON_CHANNELS] =
{
"Red", "Green", "Blue"
};
bool ack = false;
led_paths_bacon_t paths[BACON_CHANNELS];
memset(paths, 0, sizeof paths);
for( size_t i = 0; i < BACON_CHANNELS; ++i )
objconf_init(bacon_conf, &paths[i]);
for( size_t i = 0; i < BACON_CHANNELS; ++i )
{
if( !objconf_parse(bacon_conf, &paths[i], pfix[i]) )
goto cleanup;
if( !led_channel_bacon_probe(channel+i, &paths[i]) )
goto cleanup;
}
ack = true;
cleanup:
for( size_t i = 0; i < BACON_CHANNELS; ++i )
objconf_quit(bacon_conf, &paths[i]);
return ack;
}
bool
led_control_bacon_probe(led_control_t *self)
{
static led_channel_bacon_t channel[BACON_CHANNELS];
bool res = false;
led_channel_bacon_init(channel+0);
led_channel_bacon_init(channel+1);
led_channel_bacon_init(channel+2);
self->name = "bacon";
self->data = channel;
self->enable = led_control_bacon_enable_cb;
self->blink = led_control_bacon_blink_cb;
self->value = led_control_bacon_value_cb;
self->close = led_control_bacon_close_cb;
// need to check
self->can_breathe = false;
if( self->use_config )
res = led_control_bacon_dynamic_probe(channel);
if( !res )
res = led_control_bacon_static_probe(channel);
if( !res )
led_control_close(self);
return res;
}