forked from mer-hybris/mce-plugin-libhybris
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin-api.c
530 lines (451 loc) · 14 KB
/
plugin-api.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/** @file plugin-api.c
*
* mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
* <p>
* Copyright (C) 2013-2017 Jolla Ltd.
* <p>
* @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
*/
/* ========================================================================= *
* This module implements "mce-libhybris-plugin" for use from
* "mce-libhybris-module" within mce.
*
* The idea of "hybris-plugin" is shortly:
* - it uses no mce functions or data types
* - it can be compiled independently from mce
* - it exposes no libhybris/android datatypes / functions
*
* And the idea of "hybris-module" is:
* - it contains functions with the same names as "hybris-plugin"
* - if called, the functions will load & call "hybris-plugin" code
* - if "hybris-plugin" is not present "hybris-module" functions
* still work, but return failures for everything
*
* Put together:
* - mce code can assume that libhybris code is always available and
* callable during hw probing activity
* - if hybris plugin is not installed (or if some hw is not supported
* by the underlying android code), failures will be reported and mce
* can try other existing ways to proble hw controls
* ========================================================================= */
#include "plugin-api.h"
#include "plugin-logging.h"
#include "hybris-fb.h"
#include "hybris-lights.h"
#include "hybris-sensors.h"
#include "sysfs-led-main.h"
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* FRAME_BUFFER_POWER_STATE
* ------------------------------------------------------------------------- */
bool mce_hybris_framebuffer_init (void);
void mce_hybris_framebuffer_quit (void);
bool mce_hybris_framebuffer_set_power (bool state);
/* ------------------------------------------------------------------------- *
* DISPLAY_BACKLIGHT_BRIGHTNESS
* ------------------------------------------------------------------------- */
bool mce_hybris_backlight_init (void);
void mce_hybris_backlight_quit (void);
bool mce_hybris_backlight_set_brightness (int level);
/* ------------------------------------------------------------------------- *
* KEYPAD_BACKLIGHT_BRIGHTNESS
* ------------------------------------------------------------------------- */
bool mce_hybris_keypad_init (void);
void mce_hybris_keypad_quit (void);
bool mce_hybris_keypad_set_brightness (int level);
/* ------------------------------------------------------------------------- *
* INDICATOR_LED_PATTERN
* ------------------------------------------------------------------------- */
bool mce_hybris_indicator_init (void);
void mce_hybris_indicator_quit (void);
bool mce_hybris_indicator_set_pattern (int r, int g, int b, int ms_on, int ms_off);
bool mce_hybris_indicator_can_breathe (void);
void mce_hybris_indicator_enable_breathing(bool enable);
bool mce_hybris_indicator_set_brightness (int level);
/* ------------------------------------------------------------------------- *
* PROXIMITY_SENSOR
* ------------------------------------------------------------------------- */
bool mce_hybris_ps_init (void);
void mce_hybris_ps_quit (void);
bool mce_hybris_ps_set_active (bool state);
void mce_hybris_ps_set_hook (mce_hybris_ps_fn cb);
/* ------------------------------------------------------------------------- *
* AMBIENT_LIGHT_SENSOR
* ------------------------------------------------------------------------- */
bool mce_hybris_als_init (void);
void mce_hybris_als_quit (void);
bool mce_hybris_als_set_active (bool state);
void mce_hybris_als_set_hook (mce_hybris_als_fn cb);
/* ------------------------------------------------------------------------- *
* GENERIC
* ------------------------------------------------------------------------- */
void mce_hybris_quit (void);
#ifdef ENABLE_HYBRIS_SUPPORT
/* ========================================================================= *
* FRAME_BUFFER_POWER_STATE
* ========================================================================= */
/** Initialize libhybris frame buffer device object
*
* @return true on success, false on failure
*/
bool
mce_hybris_framebuffer_init(void)
{
return hybris_device_fb_init();
}
/** Release libhybris frame buffer device object
*/
void
mce_hybris_framebuffer_quit(void)
{
hybris_device_fb_quit();
}
/** Set frame buffer power state via libhybris
*
* @param state true to power on, false to power off
*
* @return true on success, false on failure
*/
bool
mce_hybris_framebuffer_set_power(bool state)
{
return hybris_device_fb_set_power(state);
}
/* ========================================================================= *
* DISPLAY_BACKLIGHT_BRIGHTNESS
* ========================================================================= */
/** Initialize libhybris display backlight device object
*
* @return true on success, false on failure
*/
bool
mce_hybris_backlight_init(void)
{
return hybris_device_backlight_init();
}
/** Release libhybris display backlight device object
*/
void
mce_hybris_backlight_quit(void)
{
hybris_device_backlight_quit();
}
/** Set display backlight brightness via libhybris
*
* @param level 0=off ... 255=maximum brightness
*
* @return true on success, false on failure
*/
bool
mce_hybris_backlight_set_brightness(int level)
{
return hybris_device_backlight_set_brightness(level);
}
/* ========================================================================= *
* KEYPAD_BACKLIGHT_BRIGHTNESS
* ========================================================================= */
/** Initialize libhybris keypad backlight device object
*
* @return true on success, false on failure
*/
bool
mce_hybris_keypad_init(void)
{
return hybris_device_keypad_init();
}
/** Release libhybris keypad backlight device object
*/
void
mce_hybris_keypad_quit(void)
{
hybris_device_keypad_quit();
}
/** Set display keypad brightness via libhybris
*
* @param level 0=off ... 255=maximum brightness
*
* @return true on success, false on failure
*/
bool
mce_hybris_keypad_set_brightness(int level)
{
return hybris_device_keypad_set_brightness(level);
}
#endif //ENABLE_HYBRIS_SUPPORT
/* ========================================================================= *
* INDICATOR_LED_PATTERN
* ========================================================================= */
/** Clamp integer values to given range
*
* @param lo minimum value allowed
* @param hi maximum value allowed
* @param val value to clamp
*
* @return val clamped to [lo, hi]
*/
static inline int
clamp_to_range(int lo, int hi, int val)
{
return val <= lo ? lo : val <= hi ? val : hi;
}
/** Flag for: controls for RGB leds exist in sysfs */
static bool mce_hybris_indicator_uses_sysfs = false;
/** Initialize libhybris indicator led device object
*
* @return true on success, false on failure
*/
bool
mce_hybris_indicator_init(void)
{
static bool done = false;
static bool ack = false;
if( done ) {
goto cleanup;
}
done = true;
if( sysfs_led_init() ) {
mce_hybris_indicator_uses_sysfs = true;
}
#ifdef ENABLE_HYBRIS_SUPPORT
else if( !hybris_device_indicator_init() ) {
goto cleanup;
}
#else
else {
goto cleanup;
}
#endif
ack = true;
cleanup:
mce_log(LL_DEBUG, "res = %s", ack ? "true" : "false");
return ack;
}
/** Release libhybris indicator led device object
*/
void
mce_hybris_indicator_quit(void)
{
if( mce_hybris_indicator_uses_sysfs ) {
/* Release sysfs controls */
sysfs_led_quit();
}
#ifdef ENABLE_HYBRIS_SUPPORT
else {
/* Release libhybris controls */
hybris_device_indicator_quit();
}
#endif
}
/** Set indicator led pattern via libhybris
*
* @param r red intensity 0 ... 255
* @param g green intensity 0 ... 255
* @param b blue intensity 0 ... 255
* @param ms_on milliseconds to keep the led on, or 0 for no flashing
* @param ms_on milliseconds to keep the led off, or 0 for no flashing
*
* @return true on success, false on failure
*/
bool
mce_hybris_indicator_set_pattern(int r, int g, int b, int ms_on, int ms_off)
{
bool ack = false;
/* Sanitize input values */
/* Clamp time periods to [0, 60] second range.
*
* While periods longer than few seconds might not count as "blinking",
* we need to leave some slack to allow beacon style patterns with
* relatively long off periods */
ms_on = clamp_to_range(0, 60000, ms_on);
ms_off = clamp_to_range(0, 60000, ms_off);
/* Both on and off periods need to be non-zero for the blinking
* to happen in the first place. And if the periods are too
* short it starts to look like led failure more than indication
* of something. */
if( ms_on < 50 || ms_off < 50 ) {
ms_on = ms_off = 0;
}
/* Clamp rgb values to [0, 255] range */
r = clamp_to_range(0, 255, r);
g = clamp_to_range(0, 255, g);
b = clamp_to_range(0, 255, b);
/* Use raw sysfs controls if possible */
if( mce_hybris_indicator_uses_sysfs ) {
ack = sysfs_led_set_pattern(r, g, b, ms_on, ms_off);
}
#ifdef ENABLE_HYBRIS_SUPPORT
else {
ack = hybris_device_indicator_set_pattern(r, g, b, ms_on, ms_off);
}
#endif
mce_log(LL_DEBUG, "pattern(%d,%d,%d,%d,%d) -> %s",
r,g,b, ms_on, ms_off , ack ? "success" : "failure");
return ack;
}
/** Query if currently active led backend can support breathing
*
* @return true if breathing can be requested, false otherwise
*/
bool
mce_hybris_indicator_can_breathe(void)
{
bool ack = false;
/* Note: We can't know how access via hybris behaves, so err
* on the safe side and assume that breathing is not ok
* unless we have direct sysfs controls.
*/
if( mce_hybris_indicator_uses_sysfs )
{
ack = sysfs_led_can_breathe();
}
/* The result does not change during runtime of mce, so
* log only once */
static bool logged = false;
if( !logged )
{
logged = true;
mce_log(LL_DEBUG, "res = %s", ack ? "true" : "false");
}
return ack;
}
/** Enable/disable sw breathing
*
* @param enable true to enable sw breathing, false to disable
*/
void
mce_hybris_indicator_enable_breathing(bool enable)
{
mce_log(LL_DEBUG, "enable = %s", enable ? "true" : "false");
if( mce_hybris_indicator_uses_sysfs ) {
sysfs_led_set_breathing(enable);
}
}
/** Set indicator led brightness
*
* @param level 1=minimum, 255=maximum
*
* @return true on success, or false on failure
*/
bool
mce_hybris_indicator_set_brightness(int level)
{
mce_log(LL_DEBUG, "level = %d", level);
if( mce_hybris_indicator_uses_sysfs ) {
/* Clamp brightness values to [1, 255] range */
level = clamp_to_range(1, 255, level);
sysfs_led_set_brightness(level);
}
/* Note: failure means this function is not available - which is
* handled at mce side stub. From this plugin we always return true */
return true;
}
#ifdef ENABLE_HYBRIS_SUPPORT
/* ========================================================================= *
* PROXIMITY_SENSOR
* ========================================================================= */
/** Start using proximity sensor via libhybris
*
* @return true on success, false on failure
*/
bool
mce_hybris_ps_init(void)
{
return hybris_sensor_ps_init();
}
/** Stop using proximity sensor via libhybris
*
* @return true on success, false on failure
*/
void
mce_hybris_ps_quit(void)
{
hybris_sensor_ps_quit();
}
/** Set proximity sensort input enabled state
*
* @param state true to enable input, or false to disable input
*/
bool
mce_hybris_ps_set_active(bool state)
{
return hybris_sensor_ps_set_active(state);
}
/** Set callback function for handling proximity sensor events
*
* Note: the callback function will be called from worker thread.
*/
void
mce_hybris_ps_set_hook(mce_hybris_ps_fn cb)
{
hybris_sensor_ps_set_hook(cb);
}
/* ========================================================================= *
* AMBIENT_LIGHT_SENSOR
* ========================================================================= */
/** Start using ambient light sensor via libhybris
*
* @return true on success, false on failure
*/
bool
mce_hybris_als_init(void)
{
return hybris_device_als_init();
}
/** Stop using ambient light sensor via libhybris
*
* @return true on success, false on failure
*/
void
mce_hybris_als_quit(void)
{
hybris_device_als_quit();
}
/** Set ambient light sensor input enabled state
*
* @param state true to enable input, or false to disable input
*/
bool
mce_hybris_als_set_active(bool state)
{
return hybris_device_als_set_active(state);
}
/** Set callback function for handling ambient light sensor events
*
* Note: the callback function will be called from worker thread.
*/
void
mce_hybris_als_set_hook(mce_hybris_als_fn cb)
{
hybris_device_als_set_hook(cb);
}
#endif //ENABLE_HYBRIS_SUPPORT
/* ========================================================================= *
* GENERIC
* ========================================================================= */
/** Release all resources allocated by this module
*/
void
mce_hybris_quit(void)
{
#ifdef ENABLE_HYBRIS_SUPPORT
hybris_plugin_fb_unload();
hybris_plugin_lights_unload();
hybris_plugin_sensors_unload();
#endif
}