-
Notifications
You must be signed in to change notification settings - Fork 10
/
sysfs-led-binary.c
294 lines (231 loc) · 8.32 KB
/
sysfs-led-binary.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
/** @file sysfs-led-binary.c
*
* mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
* <p>
* Copyright (C) 2013-2017 Jolla Ltd.
* <p>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* 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
*/
/* ========================================================================= *
* Binary led control: Jolla C backend
*
* One channel, which:
* - must have 'brightness' control file
*
* Assumptions built into code:
* - Using zero brightness disables led, any non-zero value enables it -> If
* mce requests "black" rgb value, use brightness of zero - otherwise 255.
* ========================================================================= */
#include "sysfs-led-binary.h"
#include "sysfs-led-util.h"
#include "sysfs-val.h"
#include "plugin-config.h"
#include <stdio.h>
#include <string.h>
#include <glib.h>
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
typedef struct
{
const char *brightness;
const char *max_brightness;
const char *on_string;
const char *off_string;
} led_paths_binary_t;
typedef struct
{
sysfsval_t *cached_max_brightness;
sysfsval_t *cached_brightness;
int on_value;
int off_value;
} led_channel_binary_t;
/* ------------------------------------------------------------------------- *
* ONE_CHANNEL
* ------------------------------------------------------------------------- */
static void led_channel_binary_init (led_channel_binary_t *self);
static void led_channel_binary_close (led_channel_binary_t *self);
static bool led_channel_binary_probe (led_channel_binary_t *self, const led_paths_binary_t *path);
static void led_channel_binary_set_value (led_channel_binary_t *self, int value);
/* ------------------------------------------------------------------------- *
* ALL_CHANNELS
* ------------------------------------------------------------------------- */
static void led_control_binary_map_color (int r, int g, int b, int *mono);
static void led_control_binary_value_cb (void *data, int r, int g, int b);
static void led_control_binary_close_cb (void *data);
bool led_control_binary_probe (led_control_t *self);
/* ========================================================================= *
* ONE_CHANNEL
* ========================================================================= */
static void
led_channel_binary_init(led_channel_binary_t *self)
{
self->cached_max_brightness = sysfsval_create();
self->cached_brightness = sysfsval_create();
}
static void
led_channel_binary_close(led_channel_binary_t *self)
{
sysfsval_delete(self->cached_max_brightness),
self->cached_max_brightness = 0;
sysfsval_delete(self->cached_brightness),
self->cached_brightness = 0;
}
static bool
led_channel_binary_probe(led_channel_binary_t *self,
const led_paths_binary_t *path)
{
bool res = false;
if( !sysfsval_open_rw(self->cached_brightness, path->brightness) )
goto cleanup;
if( sysfsval_open_ro(self->cached_max_brightness, path->max_brightness) )
sysfsval_refresh(self->cached_max_brightness);
if( sysfsval_get(self->cached_max_brightness) <= 0 )
sysfsval_assume(self->cached_max_brightness, 1);
res = true;
cleanup:
/* Always close the max_brightness file */
sysfsval_close(self->cached_max_brightness);
/* On failure close the other files too */
if( !res ) {
sysfsval_close(self->cached_brightness);
}
return res;
}
static void
led_channel_binary_set_value(led_channel_binary_t *self, int value)
{
value = value ? self->on_value : self->off_value;
sysfsval_set(self->cached_brightness, value);
}
/* ========================================================================= *
* ALL_CHANNELS
* ========================================================================= */
#define BINARY_CHANNELS 1
static void
led_control_binary_map_color(int r, int g, int b, int *mono)
{
/* Only binary on/off control is available, use
* 255 as logical level if nonzero rgb value has
* been requested.
*/
*mono = (r || g || b) ? 255 : 0;
}
static void
led_control_binary_value_cb(void *data, int r, int g, int b)
{
led_channel_binary_t *channel = data;
int mono = 0;
led_control_binary_map_color(r, g, b, &mono);
led_channel_binary_set_value(channel + 0, mono);
}
static void
led_control_binary_close_cb(void *data)
{
led_channel_binary_t *channel = data;
led_channel_binary_close(channel + 0);
}
static bool
led_control_binary_static_probe(led_channel_binary_t *channel)
{
/** Sysfs control paths for binary leds */
static const led_paths_binary_t paths[][BINARY_CHANNELS] =
{
// binary
{
{
.brightness = "/sys/class/leds/button-backlight/brightness",
},
},
};
bool ack = false;
for( size_t i = 0; i < G_N_ELEMENTS(paths); ++i ) {
if( !(ack = led_channel_binary_probe(&channel[0], &paths[i][0])) )
continue;
channel[i].off_value = 0;
channel[i].on_value = sysfsval_get(channel[0].cached_max_brightness);
break;
}
return ack;
}
static bool
led_control_binary_dynamic_probe(led_channel_binary_t *channel)
{
static const objconf_t binary_conf[] =
{
OBJCONF_FILE(led_paths_binary_t, brightness, Brightness),
OBJCONF_FILE(led_paths_binary_t, max_brightness, MaxBrightness),
OBJCONF_STRING(led_paths_binary_t, on_string, OnValue, 0),
OBJCONF_STRING(led_paths_binary_t, off_string, OffValue, 0),
OBJCONF_STOP
};
static const char * const pfix[BINARY_CHANNELS] =
{
"Led",
};
bool ack = false;
led_paths_binary_t paths[BINARY_CHANNELS];
memset(paths, 0, sizeof paths);
for( size_t i = 0; i < BINARY_CHANNELS; ++i )
objconf_init(binary_conf, &paths[i]);
for( size_t i = 0; i < BINARY_CHANNELS; ++i ) {
if( !objconf_parse(binary_conf, &paths[i], pfix[i]) )
goto cleanup;
if( !led_channel_binary_probe(channel+i, &paths[i]) )
goto cleanup;
if( paths[i].off_string )
channel[i].off_value = strtol(paths[i].off_string, 0, 0);
else
channel[i].off_value = 0;
if( paths[i].on_string )
channel[i].on_value = strtol(paths[i].on_string, 0, 0);
else
channel[i].on_value = sysfsval_get(channel[i].cached_max_brightness);
mce_log(LL_DEBUG,"[%s] on_value=%d off_value=%d",
pfix[i],
channel[i].on_value,
channel[i].off_value);
}
ack = true;
cleanup:
for( size_t i = 0; i < BINARY_CHANNELS; ++i )
objconf_quit(binary_conf, &paths[i]);
return ack;
}
bool
led_control_binary_probe(led_control_t *self)
{
static led_channel_binary_t channel[BINARY_CHANNELS];
bool res = false;
led_channel_binary_init(channel+0);
self->name = "binary";
self->data = channel;
self->enable = 0;
self->blink = 0;
self->value = led_control_binary_value_cb;
self->close = led_control_binary_close_cb;
/* We can use sw breathing logic to simulate hw blinking */
self->can_breathe = true;
self->breath_type = LED_RAMP_HARD_STEP;
if( self->use_config )
res = led_control_binary_dynamic_probe(channel);
if( !res )
res = led_control_binary_static_probe(channel);
if( !res )
led_control_close(self);
return res;
}