forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mce-dsme.c
693 lines (589 loc) · 16.1 KB
/
mce-dsme.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/**
* @file mce-dsme.c
* Interface code and logic between
* DSME (the Device State Management Entity)
* and MCE (the Mode Control Entity)
* <p>
* Copyright © 2004-2011 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <[email protected]>
* @author Ismo Laitinen <[email protected]>
*
* 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 <glib.h>
#include <time.h> /* time(), time_t */
#include <errno.h> /* errno */
#include <stdlib.h> /* exit(), free(), EXIT_FAILURE */
#include <unistd.h> /* getpid() */
#include <dsme/state.h> /* dsme_state_t */
#include <dsme/messages.h> /* DSM_MSGTYPE_* */
#include <dsme/protocol.h> /* dsmesock_send(),
* dsmesock_receive(),
* dsmesock_connect(),
* dsmesock_close(),
* dsmesock_connection_t
*/
#include <dsme/processwd.h> /* DSM_MSGTYPE_PROCESSWD_PING,
* DSM_MSGTYPE_PROCESSWD_PONG,
* DSM_MSGTYPE_PROCESSWD_CREATE,
* DSM_MSGTYPE_PROCESSWD_DELETE
*/
#include <mce/mode-names.h>
#include "mce.h" /* mce_add_submode_int32(),
* mce_rem_submode_int32(),
* mce_get_submode_int32(),
* MCE_INVALID_TRANSLATION,
* MCE_LED_PATTERN_DEVICE_ON,
* MCE_LED_PATTERN_DEVICE_SOFT_OFF,
* submode_t,
* system_state_t,
* MCE_SOFTOFF_SUBMODE,
* MCE_TRANSITION_SUBMODE,
* mainloop,
* charger_state_pipe,
* display_state_pipe,
* system_state_pipe,
* led_pattern_activate_pipe,
* led_pattern_deactivate_pipe
*/
#include "mce-dsme.h"
#include "mce-lib.h" /* mce_translate_string_to_int_with_default(),
* mce_translation_t
*/
#include "mce-log.h" /* mce_log(), LL_* */
#include "mce-dbus.h" /* mce_dbus_handler_add(),
* DBUS_MESSAGE_TYPE_SIGNAL
*/
#include "mce-conf.h" /* mce_conf_get_string() */
#include "datapipe.h" /* execute_datapipe(),
* execute_datapippe_output_triggers(),
* datapipe_get_gint(),
* append_output_trigger_to_datapipe(),
* remove_output_trigger_from_datapipe()
*/
/** Charger state */
static gboolean charger_connected = FALSE;
/** Pointer to the dsmesock connection */
static dsmesock_connection_t *dsme_conn = NULL;
/** TRUE if dsme is disabled (for debugging), FALSE if dsme is enabled */
static gboolean dsme_disabled = FALSE;
/** ID for state transition timer source */
static guint transition_timeout_cb_id = 0;
/** Soft poweroff charger connect policy */
static gint softoff_charger_connect_policy = DEFAULT_SOFTOFF_CHARGER_CONNECT;
/** Mapping of soft poweroff charger connect integer <-> policy string */
static const mce_translation_t soft_poweroff_charger_connect_translation[] = {
{
.number = SOFTOFF_CHARGER_CONNECT_WAKEUP,
.string = SOFTOFF_CHARGER_CONNECT_WAKEUP_STR
}, {
.number = SOFTOFF_CHARGER_CONNECT_IGNORE,
.string = SOFTOFF_CHARGER_CONNECT_IGNORE_STR
}, { /* MCE_INVALID_TRANSLATION marks the end of this array */
.number = MCE_INVALID_TRANSLATION,
.string = NULL
}
};
/** dsme I/O channel */
static GIOChannel *dsme_iochan = NULL;
/** dsme data channel GSource ID */
static guint dsme_data_source_id;
/** dsme error channel GSource ID */
static guint dsme_error_source_id;
static gboolean init_dsmesock(void);
/**
* Generic send function for dsmesock messages
* XXX: How should we handle sending failures?
*
* @param msg A pointer to the message to send
*/
static void mce_dsme_send(gpointer msg)
{
if (dsme_disabled == TRUE)
goto EXIT;
if (dsme_conn == NULL) {
mce_log(LL_CRIT,
"Attempt to use dsme_conn uninitialised; aborting!");
// FIXME: this is not how one should exit from mainloop
mce_quit_mainloop();
exit(EXIT_FAILURE);
}
if ((dsmesock_send(dsme_conn, msg)) == -1) {
mce_log(LL_CRIT,
"dsmesock_send error: %s",
g_strerror(errno));
#ifdef MCE_DSME_ERROR_POLICY
// FIXME: this is not how one should exit from mainloop
mce_quit_mainloop();
exit(EXIT_FAILURE);
#endif /* MCE_DSME_ERROR_POLICY */
}
EXIT:
return;
}
/**
* Send pong message to the DSME process watchdog
*/
static void dsme_send_pong(void)
{
/* Set up the message */
DSM_MSGTYPE_PROCESSWD_PONG msg =
DSME_MSG_INIT(DSM_MSGTYPE_PROCESSWD_PONG);
msg.pid = getpid();
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_PROCESSWD_PONG sent to DSME");
execute_datapipe(&heartbeat_pipe, GINT_TO_POINTER(0),
USE_INDATA, DONT_CACHE_INDATA);
}
/**
* Register to DSME process watchdog
*/
static void dsme_init_processwd(void)
{
/* Set up the message */
DSM_MSGTYPE_PROCESSWD_CREATE msg =
DSME_MSG_INIT(DSM_MSGTYPE_PROCESSWD_CREATE);
msg.pid = getpid();
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_PROCESSWD_CREATE sent to DSME");
}
/**
* Unregister from DSME process watchdog
*/
static void dsme_exit_processwd(void)
{
mce_log(LL_DEBUG,
"Disabling DSME process watchdog");
/* Set up the message */
DSM_MSGTYPE_PROCESSWD_DELETE msg =
DSME_MSG_INIT(DSM_MSGTYPE_PROCESSWD_DELETE);
msg.pid = getpid();
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_PROCESSWD_DELETE sent to DSME");
}
/**
* Send system state inquiry
*/
static void query_system_state(void)
{
/* Set up the message */
DSM_MSGTYPE_STATE_QUERY msg = DSME_MSG_INIT(DSM_MSGTYPE_STATE_QUERY);
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_STATE_QUERY sent to DSME");
}
/**
* Request powerup
*/
void request_powerup(void)
{
/* Set up the message */
DSM_MSGTYPE_POWERUP_REQ msg = DSME_MSG_INIT(DSM_MSGTYPE_POWERUP_REQ);
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_POWERUP_REQ sent to DSME");
}
/**
* Request reboot
*/
void request_reboot(void)
{
/* Set up the message */
DSM_MSGTYPE_REBOOT_REQ msg = DSME_MSG_INIT(DSM_MSGTYPE_REBOOT_REQ);
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_REBOOT_REQ sent to DSME");
}
/**
* Request soft poweron
*/
void request_soft_poweron(void)
{
/* Disable the soft poweroff LED pattern */
execute_datapipe_output_triggers(&led_pattern_deactivate_pipe,
MCE_LED_PATTERN_DEVICE_SOFT_OFF,
USE_INDATA);
mce_rem_submode_int32(MCE_SOFTOFF_SUBMODE);
execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_ON),
USE_INDATA, CACHE_INDATA);
}
/**
* Request soft poweroff
*/
void request_soft_poweroff(void)
{
mce_add_submode_int32(MCE_SOFTOFF_SUBMODE);
execute_datapipe(&display_state_pipe,
GINT_TO_POINTER(MCE_DISPLAY_LPM_OFF),
USE_INDATA, CACHE_INDATA);
/* Enable the soft poweroff LED pattern */
execute_datapipe_output_triggers(&led_pattern_activate_pipe,
MCE_LED_PATTERN_DEVICE_SOFT_OFF,
USE_INDATA);
}
/**
* Timeout callback for transition
*
* @param data Unused
* @return Always returns FALSE, to disable the timeout
*/
static gboolean transition_timeout_cb(gpointer data)
{
(void)data;
transition_timeout_cb_id = 0;
mce_rem_submode_int32(MCE_TRANSITION_SUBMODE);
return FALSE;
}
/**
* Cancel state transition timeout
*/
static void cancel_state_transition_timeout(void)
{
/* Remove the timeout source for state transitions */
if (transition_timeout_cb_id != 0) {
g_source_remove(transition_timeout_cb_id);
transition_timeout_cb_id = 0;
}
}
/**
* Setup state transition timeout
*/
static void setup_transition_timeout(void)
{
cancel_state_transition_timeout();
/* Setup new timeout */
transition_timeout_cb_id =
g_timeout_add(TRANSITION_DELAY, transition_timeout_cb, NULL);
}
/**
* Request normal shutdown
*/
void request_normal_shutdown(void)
{
/* Set up the message */
DSM_MSGTYPE_SHUTDOWN_REQ msg = DSME_MSG_INIT(DSM_MSGTYPE_SHUTDOWN_REQ);
/* Send the message */
mce_dsme_send(&msg);
mce_log(LL_DEBUG,
"DSM_MSGTYPE_SHUTDOWN_REQ (DSME_NORMAL_SHUTDOWN) "
"sent to DSME");
}
/**
* Convert DSME dsme state
* to a state enum that we can export to datapipes
*
* @param dsmestate The DSME dsme_state_t with the value to convert
* @return the converted value
*/
static system_state_t normalise_dsme_state(dsme_state_t dsmestate)
{
system_state_t state = MCE_STATE_UNDEF;
switch (dsmestate) {
case DSME_STATE_SHUTDOWN:
state = MCE_STATE_SHUTDOWN;
break;
case DSME_STATE_USER:
state = MCE_STATE_USER;
break;
case DSME_STATE_ACTDEAD:
state = MCE_STATE_ACTDEAD;
break;
case DSME_STATE_REBOOT:
state = MCE_STATE_REBOOT;
break;
case DSME_STATE_BOOT:
state = MCE_STATE_BOOT;
break;
case DSME_STATE_NOT_SET:
break;
case DSME_STATE_TEST:
mce_log(LL_WARN,
"Received DSME_STATE_TEST; treating as undefined");
break;
case DSME_STATE_MALF:
mce_log(LL_WARN,
"Received DSME_STATE_MALF; treating as undefined");
break;
case DSME_STATE_LOCAL:
mce_log(LL_WARN,
"Received DSME_STATE_LOCAL; treating as undefined");
break;
default:
mce_log(LL_ERR,
"Received an unknown state from DSME; "
"treating as undefined");
break;
}
return state;
}
/**
* Callback for pending I/O from dsmesock
*
* XXX: is the error policy reasonable?
*
* @param source Unused
* @param condition Unused
* @param data Unused
* @return TRUE on success, FALSE on failure
*/
static gboolean io_data_ready_cb(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
dsmemsg_generic_t *msg;
DSM_MSGTYPE_STATE_CHANGE_IND *msg2;
system_state_t oldstate = datapipe_get_gint(system_state_pipe);
system_state_t newstate = MCE_STATE_UNDEF;
(void)source;
(void)condition;
(void)data;
if (dsme_disabled == TRUE)
goto EXIT;
if ((msg = (dsmemsg_generic_t *)dsmesock_receive(dsme_conn)) == NULL)
goto EXIT;
if (DSMEMSG_CAST(DSM_MSGTYPE_CLOSE, msg)) {
/* DSME socket closed: try once to reopen;
* if that fails, exit
*/
mce_log(LL_ERR,
"DSME socket closed; trying to reopen");
if ((init_dsmesock()) == FALSE) {
// FIXME: this is not how one should exit from mainloop
mce_quit_mainloop();
exit(EXIT_FAILURE);
}
} else if (DSMEMSG_CAST(DSM_MSGTYPE_PROCESSWD_PING, msg)) {
dsme_send_pong();
} else if ((msg2 = DSMEMSG_CAST(DSM_MSGTYPE_STATE_CHANGE_IND, msg))) {
newstate = normalise_dsme_state(msg2->state);
mce_log(LL_DEBUG,
"DSME device state change: %d",
newstate);
/* If we're changing to a different state,
* add the transition flag, UNLESS the old state
* was MCE_STATE_UNDEF
*/
if ((oldstate != newstate) && (oldstate != MCE_STATE_UNDEF))
mce_add_submode_int32(MCE_TRANSITION_SUBMODE);
switch (newstate) {
case MCE_STATE_USER:
execute_datapipe_output_triggers(&led_pattern_activate_pipe, MCE_LED_PATTERN_DEVICE_ON, USE_INDATA);
break;
case MCE_STATE_ACTDEAD:
case MCE_STATE_BOOT:
case MCE_STATE_UNDEF:
break;
case MCE_STATE_SHUTDOWN:
case MCE_STATE_REBOOT:
execute_datapipe_output_triggers(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_DEVICE_ON, USE_INDATA);
break;
default:
break;
}
execute_datapipe(&system_state_pipe,
GINT_TO_POINTER(newstate),
USE_INDATA, CACHE_INDATA);
} else {
mce_log(LL_DEBUG,
"Unknown message type (%x) received from DSME!",
msg->type_); /* <- unholy access of a private member */
}
free(msg);
EXIT:
return TRUE;
}
/**
* Callback for I/O errors from dsmesock
*
* @param source Unused
* @param condition Unused
* @param data Unused
* @return Will never return; if there is an I/O-error we exit the mainloop
*/
static gboolean io_error_cb(GIOChannel *source,
GIOCondition condition,
gpointer data) G_GNUC_NORETURN;
static gboolean io_error_cb(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
/* Silence warnings */
(void)source;
(void)condition;
(void)data;
/* DSME socket closed/error */
mce_log(LL_CRIT,
"DSME socket closed/error, exiting...");
// FIXME: this is not how one should exit from mainloop
mce_quit_mainloop();
exit(EXIT_FAILURE);
}
/**
* D-Bus callback for the init done notification signal
*
* @param msg The D-Bus message
* @return TRUE on success, FALSE on failure
*/
static gboolean init_done_dbus_cb(DBusMessage *const msg)
{
gboolean status = FALSE;
(void)msg;
mce_log(LL_DEBUG,
"Received init done notification");
if ((mce_get_submode_int32() & MCE_TRANSITION_SUBMODE)) {
setup_transition_timeout();
}
status = TRUE;
//EXIT:
return status;
}
/**
* Datapipe trigger for the charger state
*
* @param data TRUE if the charger was connected,
* FALSE if the charger was disconnected
*/
static void charger_state_trigger(gconstpointer const data)
{
submode_t submode = mce_get_submode_int32();
charger_connected = GPOINTER_TO_INT(data);
if ((submode & MCE_SOFTOFF_SUBMODE) != 0) {
if (softoff_charger_connect_policy == SOFTOFF_CHARGER_CONNECT_WAKEUP) {
request_soft_poweron();
}
}
}
/**
* Initialise dsmesock connection
*
* @return TRUE on success, FALSE on failure
*/
static gboolean init_dsmesock(void)
{
gboolean status = FALSE;
if (dsme_conn == NULL) {
if ((dsme_conn = dsmesock_connect()) == NULL) {
mce_log(LL_CRIT,
"Failed to open DSME socket");
goto EXIT;
}
}
if ((dsme_iochan = g_io_channel_unix_new(dsme_conn->fd)) == NULL) {
mce_log(LL_CRIT,
"Failed to set up I/O channel for DSME socket");
goto EXIT;
}
dsme_data_source_id = g_io_add_watch(dsme_iochan,
G_IO_IN | G_IO_PRI,
io_data_ready_cb, NULL);
dsme_error_source_id = g_io_add_watch(dsme_iochan,
G_IO_ERR | G_IO_HUP,
io_error_cb, NULL);
/* Query the current system state; if the mainloop isn't running,
* this will trigger an update when the mainloop starts
*/
query_system_state();
status = TRUE;
EXIT:
return status;
}
/**
* Close dsmesock connection
*/
static void close_dsmesock(void)
{
mce_log(LL_DEBUG,
"Shutting down dsmesock I/O channel");
if (dsme_iochan != NULL) {
GError *error = NULL;
g_source_remove(dsme_data_source_id);
g_source_remove(dsme_error_source_id);
g_io_channel_shutdown(dsme_iochan, FALSE, &error);
g_io_channel_unref(dsme_iochan);
g_clear_error(&error);
}
mce_log(LL_DEBUG,
"Closing DSME sock");
dsmesock_close(dsme_conn);
}
/**
* Init function for the mce-dsme component
*
* @param debug_mode TRUE - do not exit if dsme fails
* @return TRUE on success, FALSE on failure
*/
gboolean mce_dsme_init(gboolean debug_mode)
{
gboolean status = FALSE;
gchar *tmp = NULL;
/* Append triggers/filters to datapipes */
append_output_trigger_to_datapipe(&charger_state_pipe,
charger_state_trigger);
mce_log(LL_DEBUG,
"Connecting to DSME sock");
if (init_dsmesock() == FALSE) {
if (debug_mode == TRUE) {
dsme_disabled = TRUE;
} else {
goto EXIT;
}
}
/* Register with DSME's process watchdog */
dsme_init_processwd();
/* init_done */
if (mce_dbus_handler_add("com.nokia.startup.signal",
"init_done",
NULL,
DBUS_MESSAGE_TYPE_SIGNAL,
init_done_dbus_cb) == NULL)
goto EXIT;
/* Get configuration options */
tmp = mce_conf_get_string(MCE_CONF_SOFTPOWEROFF_GROUP,
MCE_CONF_SOFTPOWEROFF_CHARGER_POLICY_CONNECT,
"");
softoff_charger_connect_policy = mce_translate_string_to_int_with_default(soft_poweroff_charger_connect_translation, tmp, DEFAULT_SOFTOFF_CHARGER_CONNECT);
g_free(tmp);
status = TRUE;
EXIT:
return status;
}
/**
* Exit function for the mce-dsme component
*
* @todo D-Bus unregistration
* @todo trigger unregistration
*/
void mce_dsme_exit(void)
{
if (dsme_conn != NULL) {
dsme_exit_processwd();
close_dsmesock();
}
/* Remove triggers/filters from datapipes */
remove_output_trigger_from_datapipe(&charger_state_pipe,
charger_state_trigger);
/* Remove all timer sources */
cancel_state_transition_timeout();
return;
}