/
battery-bme.c
537 lines (444 loc) · 12.3 KB
/
battery-bme.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
531
532
533
534
535
536
537
/**
* @file battery-bme.c
* Battery module -- this implements battery and charger logic for MCE
* <p>
* Copyright © 2008-2011 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (C) 2013-2019 Jolla Ltd.
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../mce.h"
#include "../mce-log.h"
#include "../mce-dbus.h"
#include "../bme-dbus-names.h"
#include <gmodule.h>
/** Module name */
#define MODULE_NAME "battery"
/** Functionality provided by this module */
static const gchar *const provides[] = { MODULE_NAME, NULL };
/** Module information */
G_MODULE_EXPORT module_info_struct module_info = {
/** Name of the module */
.name = MODULE_NAME,
/** Module provides */
.provides = provides,
/** Module priority */
.priority = 100
};
/** Cached value of the charger connected state */
static gint cached_charger_connected = -1;
/**
* D-Bus callback for the battery full signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean battery_full_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received battery full signal");
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_CHARGING);
datapipe_exec_full(&led_pattern_activate_pipe, MCE_LED_PATTERN_BATTERY_FULL);
datapipe_exec_full(&battery_status_pipe,
GINT_TO_POINTER(BATTERY_STATUS_FULL));
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the battery ok signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean battery_ok_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received battery ok signal");
// datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_LOW);
datapipe_exec_full(&battery_status_pipe,
GINT_TO_POINTER(BATTERY_STATUS_OK));
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the battery low signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean battery_low_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received battery low signal");
// datapipe_exec_full(&led_pattern_activate_pipe, MCE_LED_PATTERN_BATTERY_LOW);
datapipe_exec_full(&battery_status_pipe,
GINT_TO_POINTER(BATTERY_STATUS_LOW));
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the battery empty signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean battery_empty_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received battery empty signal");
datapipe_exec_full(&battery_status_pipe,
GINT_TO_POINTER(BATTERY_STATUS_EMPTY));
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the battery state changed signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean battery_state_changed_dbus_cb(DBusMessage *const msg)
{
dbus_uint32_t percentage;
DBusMessageIter iter;
dbus_uint32_t now;
dbus_uint32_t max;
gint argcount = 0;
gint argtype;
gboolean status = FALSE;
mce_log(LL_DEBUG,
"Received battery state changed signal");
if (dbus_message_iter_init(msg, &iter) == FALSE) {
// XXX: should we return an error instead?
mce_log(LL_ERR,
"Failed to initialise D-Bus message iterator; "
"message has no arguments");
goto EXIT;
}
while ((argtype = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID) {
if (argtype == DBUS_TYPE_UINT32) {
if (argcount == 0) {
dbus_message_iter_get_basic(&iter, &now);
} else if (argcount == 1) {
dbus_message_iter_get_basic(&iter, &max);
} else if (argcount == 2) {
dbus_message_iter_get_basic(&iter, &percentage);
}
} else if (argcount < 3) {
mce_log(LL_ERR,
"Argument %d passed to %s.%s has "
"incorrect type",
argcount,
BME_SIGNAL_IF,
BME_BATTERY_STATE_UPDATE);
goto EXIT;
}
argcount++;
dbus_message_iter_next (&iter);
}
if (argcount < 2) {
mce_log(LL_ERR,
"Too few arguments received from "
"%s.%s; "
"got %d, expected %d-%d",
BME_SIGNAL_IF, BME_BATTERY_STATE_UPDATE,
argcount, 2, 3);
goto EXIT;
}
if (argcount > 3) {
mce_log(LL_INFO,
"Too many arguments received from "
"%s.%s; "
"got %d, expected %d-%d -- ignoring extra arguments",
BME_SIGNAL_IF, BME_BATTERY_STATE_UPDATE,
argcount, 2, 3);
}
if (argcount == 2) {
percentage = (now * 10 / max) * 10;
}
mce_log(LL_DEBUG,
"Percentage: %d",
percentage);
datapipe_exec_full(&battery_level_pipe,
GINT_TO_POINTER(percentage));
status = TRUE;
EXIT:
return status;
}
/**
* D-Bus callback for the charger_charging_on signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean charger_charging_on_dbus_cb(DBusMessage *const msg)
{
charger_state_t old_charger_state = datapipe_get_gint(charger_state_pipe);
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received charger_charging_on signal");
/* Only update the charger state if needed */
if (old_charger_state != CHARGER_STATE_ON) {
datapipe_exec_full(&charger_state_pipe,
GINT_TO_POINTER(CHARGER_STATE_ON));
}
/* In case these are active; there's no harm to call them anyway */
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_FULL);
// datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_LOW);
datapipe_exec_full(&led_pattern_activate_pipe, MCE_LED_PATTERN_BATTERY_CHARGING);
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the charger_charging_off signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean charger_charging_off_dbus_cb(DBusMessage *const msg)
{
charger_state_t old_charger_state = datapipe_get_gint(charger_state_pipe);
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received charger_charging_off signal");
/* Only update the charger state if needed */
if (old_charger_state != CHARGER_STATE_OFF) {
datapipe_exec_full(&charger_state_pipe,
GINT_TO_POINTER(CHARGER_STATE_OFF));
}
/* In case these are active; there's no harm to call them anyway */
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_CHARGING);
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the charger_charging_failed signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean charger_charging_failed_dbus_cb(DBusMessage *const msg)
{
charger_state_t old_charger_state = datapipe_get_gint(charger_state_pipe);
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received charger_charging_failed signal");
/* Only update the charger state if needed */
if (old_charger_state != CHARGER_STATE_OFF) {
datapipe_exec_full(&charger_state_pipe,
GINT_TO_POINTER(CHARGER_STATE_OFF));
}
/* In case these are active; there's no harm to call them anyway */
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_FULL);
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_CHARGING);
/* Generate activity */
mce_datapipe_generate_activity();
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the charger_connected signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean charger_connected_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received charger_connected signal");
if (cached_charger_connected != 1) {
/* Generate activity */
mce_datapipe_generate_activity();
cached_charger_connected = 1;
}
status = TRUE;
//EXIT:
return status;
}
/**
* D-Bus callback for the charger_disconnected signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean charger_disconnected_dbus_cb(DBusMessage *const msg)
{
charger_state_t old_charger_state = datapipe_get_gint(charger_state_pipe);
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received charger_disconnected signal");
/* Only update the charger state if needed */
if (old_charger_state != CHARGER_STATE_OFF) {
datapipe_exec_full(&charger_state_pipe,
GINT_TO_POINTER(CHARGER_STATE_OFF));
}
/* In case these are active; there's no harm to call them anyway */
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_FULL);
datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_BATTERY_CHARGING);
if (cached_charger_connected != 0) {
/* Generate activity */
mce_datapipe_generate_activity();
cached_charger_connected = 0;
}
status = TRUE;
//EXIT:
return status;
}
/**
* Request update of charger status
*
* @return TRUE on success, FALSE on failure
*/
static gboolean request_charger_status(void)
{
return dbus_send(BME_SERVICE, BME_REQUEST_PATH, BME_REQUEST_IF,
BME_STATUS_INFO_REQ, NULL, DBUS_TYPE_INVALID);
}
/** Array of dbus message handlers */
static mce_dbus_handler_t battery_bme_dbus_handlers[] =
{
/* signals */
{
.interface = BME_SIGNAL_IF,
.name = BME_BATTERY_FULL,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = battery_full_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_BATTERY_OK,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = battery_ok_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_BATTERY_LOW,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = battery_low_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_BATTERY_EMPTY,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = battery_empty_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_BATTERY_STATE_UPDATE,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = battery_state_changed_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_CHARGER_CHARGING_ON,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = charger_charging_on_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_CHARGER_CHARGING_OFF,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = charger_charging_off_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_CHARGER_CHARGING_FAILED,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = charger_charging_failed_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_CHARGER_CONNECTED,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = charger_connected_dbus_cb,
},
{
.interface = BME_SIGNAL_IF,
.name = BME_CHARGER_DISCONNECTED,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.callback = charger_disconnected_dbus_cb,
},
/* sentinel */
{
.interface = 0
}
};
/** Add dbus handlers
*/
static void battery_bme_init_dbus(void)
{
mce_dbus_handler_register_array(battery_bme_dbus_handlers);
}
/** Remove dbus handlers
*/
static void battery_bme_quit_dbus(void)
{
mce_dbus_handler_unregister_array(battery_bme_dbus_handlers);
}
/**
* Init function for the battery and charger module
*
* @todo XXX status needs to be set on error!
*
* @param module Unused
* @return NULL on success, a string with an error message on failure
*/
G_MODULE_EXPORT const gchar *g_module_check_init(GModule *module);
const gchar *g_module_check_init(GModule *module)
{
(void)module;
/* Add dbus handlers */
battery_bme_init_dbus();
/* Update charger status */
request_charger_status();
return NULL;
}
/**
* Exit function for the battery and charger module
*
* @todo D-Bus unregistration
*
* @param module Unused
*/
G_MODULE_EXPORT void g_module_unload(GModule *module);
void g_module_unload(GModule *module)
{
(void)module;
/* Remove dbus handlers */
battery_bme_quit_dbus();
return;
}