-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmce-common.c
1272 lines (1055 loc) · 36.6 KB
/
mce-common.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file mce-common.c
* Common state logic for Mode Control Entity
* <p>
* Copyright (c) 2017 - 2021 Jolla Ltd.
* Copyright (c) 2019 - 2020 Open Mobile Platform LLC.
* <p>
* @author Simo Piiroinen <[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 "mce-common.h"
#include "mce.h"
#include "mce-dbus.h"
#include "mce-lib.h"
#include "mce-log.h"
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
/* ========================================================================= *
* TYPES
* ========================================================================= */
/** Bookkeeping data for on-condition callback function */
typedef struct
{
/** Source identification, for mass cancelation */
gchar *srce;
/** Function to call */
GDestroyNotify func;
/** Parameter to give to the function */
gpointer aptr;
} on_condition_t;
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* ON_CONDITION
* ------------------------------------------------------------------------- */
static bool on_condition_matches (on_condition_t *self, const char *srce, GDestroyNotify func, gpointer aptr);
static on_condition_t *on_condition_create (const char *srce, GDestroyNotify func, gpointer aptr);
static void on_condition_delete (on_condition_t *self);
static void on_condition_exec (on_condition_t *self);
static void on_condition_exec_and_delete_cb(gpointer self);
static void on_condition_delete_cb (gpointer self);
/* ------------------------------------------------------------------------- *
* COMMON_ON_PROXIMITY
* ------------------------------------------------------------------------- */
static gboolean common_on_proximity_exec_cb (gpointer aptr);
static void common_on_proximity_exec (void);
void common_on_proximity_schedule(const char *srce, GDestroyNotify func, gpointer aptr);
void common_on_proximity_cancel (const char *srce, GDestroyNotify func, gpointer aptr);
static void common_on_proximity_quit (void);
/* ------------------------------------------------------------------------- *
* COMMON_DBUS
* ------------------------------------------------------------------------- */
static void common_dbus_send_usb_cable_state (DBusMessage *const req);
static gboolean common_dbus_get_usb_cable_state_cb(DBusMessage *const req);
static void common_dbus_send_charger_type (DBusMessage *const req);
static gboolean common_dbus_get_charger_type_cb (DBusMessage *const req);
static void common_dbus_send_charger_state (DBusMessage *const req);
static gboolean common_dbus_get_charger_state_cb (DBusMessage *const req);
static void common_dbus_send_battery_status (DBusMessage *const req);
static gboolean common_dbus_get_battery_status_cb (DBusMessage *const req);
static void common_dbus_send_battery_state (DBusMessage *const req);
static gboolean common_dbus_get_battery_state_cb (DBusMessage *const req);
static void common_dbus_send_battery_level (DBusMessage *const req);
static gboolean common_dbus_get_battery_level_cb (DBusMessage *const req);
static void common_dbus_send_memnotify_level (void);
static gboolean common_dbus_get_memnotify_level_cb(DBusMessage *const req);
static gboolean common_dbus_initial_cb (gpointer aptr);
static void common_dbus_init (void);
static void common_dbus_quit (void);
/* ------------------------------------------------------------------------- *
* COMMON_DATAPIPE
* ------------------------------------------------------------------------- */
static void common_datapipe_usb_cable_state_cb (gconstpointer data);
static void common_datapipe_charger_type_cb (gconstpointer data);
static void common_datapipe_charger_state_cb (gconstpointer data);
static void common_datapipe_battery_status_cb (gconstpointer data);
static void common_datapipe_battery_state_cb (gconstpointer data);
static void common_datapipe_battery_level_cb (gconstpointer data);
static void common_datapipe_proximity_sensor_actual_cb(gconstpointer data);
static void common_datapipe_memnotify_level_cb (gconstpointer data);
static void common_datapipe_init (void);
static void common_datapipe_quit (void);
/* ------------------------------------------------------------------------- *
* MCE_COMMON
* ------------------------------------------------------------------------- */
bool mce_common_init(void);
void mce_common_quit(void);
/* ========================================================================= *
* STATE_DATA
* ========================================================================= */
/** USB cable status; assume undefined */
static usb_cable_state_t usb_cable_state = USB_CABLE_UNDEF;
/** Charger type; assume none */
static charger_type_t charger_type = CHARGER_TYPE_NONE;
/** Charger state; assume undefined */
static charger_state_t charger_state = CHARGER_STATE_UNDEF;
/** Battery status; assume undefined */
static battery_status_t battery_status = BATTERY_STATUS_UNDEF;
/** Battery state; assume unknown */
static battery_state_t battery_state = BATTERY_STATE_UNKNOWN;
/** Battery charge level: assume unknown */
static gint battery_level = MCE_BATTERY_LEVEL_UNKNOWN;
/** Cached (raw) proximity sensor state */
static cover_state_t proximity_sensor_actual = COVER_UNDEF;
/** Cached memory use level */
static memnotify_level_t memnotify_level = MEMNOTIFY_LEVEL_UNKNOWN;
/* ========================================================================= *
* ON_CONDITION
* ========================================================================= */
static bool
on_condition_matches(on_condition_t *self,
const char *srce, GDestroyNotify func, gpointer aptr)
{
bool matches = false;
if( !srce || !self || !self->srce )
goto EXIT;
if( strcmp(self->srce, srce) )
goto EXIT;
if( self->func == func && self->aptr == aptr )
matches = true;
else
matches = !func && !srce;
EXIT:
return matches;
}
static on_condition_t *
on_condition_create(const char *srce, GDestroyNotify func, gpointer aptr)
{
on_condition_t *self = g_slice_alloc0(sizeof *self);
self->srce = g_strdup(srce);
self->func = func;
self->aptr = aptr;
return self;
}
static void
on_condition_delete(on_condition_t *self)
{
if( self ) {
g_free(self->srce);
g_slice_free1(sizeof *self, self);
}
}
static void
on_condition_exec(on_condition_t *self)
{
if( self )
self->func(self->aptr);
}
static void
on_condition_exec_and_delete_cb(gpointer self)
{
on_condition_exec(self);
on_condition_delete(self);
}
static void
on_condition_delete_cb(gpointer self)
{
on_condition_delete(self);
}
/* ========================================================================= *
* COMMON_ON_PROXIMITY
* ========================================================================= */
#define COMMON_ON_DEMAND_TAG "common_on_proximity"
static GSList *common_on_proximity_actions = 0;
static guint common_on_proximity_exec_id = 0;
static gboolean
common_on_proximity_exec_cb(gpointer aptr)
{
(void)aptr;
gboolean result = G_SOURCE_REMOVE;
GSList *todo;
/* Execute queued actions */
if( (todo = common_on_proximity_actions) ) {
common_on_proximity_actions = 0;
todo = g_slist_reverse(todo);
g_slist_free_full(todo, on_condition_exec_and_delete_cb);
}
/* Check if executed actions queued more actions */
if( common_on_proximity_actions ) {
/* Repeat to handle freshly added actions */
result = G_SOURCE_CONTINUE;
}
else {
/* Queue exchausted - sensor no longer needed */
datapipe_exec_full(&proximity_sensor_required_pipe,
PROXIMITY_SENSOR_REQUIRED_REM
COMMON_ON_DEMAND_TAG);
}
if( result == G_SOURCE_REMOVE )
common_on_proximity_exec_id = 0;
return result;
}
static void
common_on_proximity_exec(void)
{
/* Execute via idle to make sure all proximity
* datapipe listeners have had a chance to
* register sensor state before callbacks
* get triggered. */
if( !common_on_proximity_exec_id ) {
common_on_proximity_exec_id =
mce_wakelocked_idle_add(common_on_proximity_exec_cb,
0);
}
}
/** Execute callback function when actual proximity sensor state is available
*
* @param srce Call site identification
* @param func Callback function pointer
* @param aptr Parameter for the callback function
*/
void
common_on_proximity_schedule(const char *srce, GDestroyNotify func, gpointer aptr)
{
/* In order to execute actions in the requested order,
* immediate execution can be allowed only when proximity
* sensor state is known and the already queued actions
* have been executed.
*/
if( proximity_sensor_actual == COVER_UNDEF ||
common_on_proximity_actions ||
common_on_proximity_exec_id ) {
// TODO: all failures to communicate sensor power up with
// sensorfwd should lead to mce-sensorfw module
// declaring proximity=not-covered, but having an
// explicit timeout here would not hurt ...
if( !common_on_proximity_actions )
datapipe_exec_full(&proximity_sensor_required_pipe,
PROXIMITY_SENSOR_REQUIRED_ADD
COMMON_ON_DEMAND_TAG);
common_on_proximity_actions =
g_slist_prepend(common_on_proximity_actions,
on_condition_create(srce, func, aptr));
}
else {
func(aptr);
}
}
/** Cancel pending on-proximity callback
*
* @param srce Call site identification
* @param func Callback function pointer, or NULL for all
* @param aptr Parameter for the callback function, or NULL for all
*/
void
common_on_proximity_cancel(const char *srce, GDestroyNotify func, gpointer aptr)
{
for( GSList *iter = common_on_proximity_actions; iter; iter = iter->next ) {
on_condition_t *item = iter->data;
if( !item )
continue;
if( !on_condition_matches(item, srce, func, aptr) )
continue;
/* Detach from list and delete.
*
* Note that the list itself is not garbage collected in order not
* to disturb possibly pending execute etc logic -> all iterators
* must be prepared to bump into empty links.
*/
iter->data = 0;
on_condition_delete(item);
}
}
static void
common_on_proximity_quit(void)
{
/* Cancel pending "on_condition" actions */
g_slist_free_full(common_on_proximity_actions,
on_condition_delete_cb),
common_on_proximity_actions = 0;
/* Do not leave active timers behind */
if( common_on_proximity_exec_id ) {
g_source_remove(common_on_proximity_exec_id),
common_on_proximity_exec_id = 0;
}
}
/* ========================================================================= *
* DBUS_FUNCTIONS
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* usb_cable_state
* ------------------------------------------------------------------------- */
/** Send usb_cable_state D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_usb_cable_state(DBusMessage *const req)
{
static const char *last = 0;
DBusMessage *msg = NULL;
const char *value = usb_cable_state_to_dbus(usb_cable_state);
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_USB_CABLE_STATE_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %s",
req ? "reply" : "broadcast",
"usb_cable_state", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling usb_cable_state D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_usb_cable_state_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "usb_cable_state query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_usb_cable_state(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* charger_type
* ------------------------------------------------------------------------- */
/** Send charger_type D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_charger_type(DBusMessage *const req)
{
static const char *last = 0;
DBusMessage *msg = NULL;
const char *value = charger_type_to_dbus(charger_type);
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_CHARGER_TYPE_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %s",
req ? "reply" : "broadcast",
"charger_type", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling charger_type D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_charger_type_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "charger_type query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_charger_type(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* charger_state
* ------------------------------------------------------------------------- */
/** Send charger_state D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_charger_state(DBusMessage *const req)
{
static const char *last = 0;
DBusMessage *msg = NULL;
const char *value = charger_state_to_dbus(charger_state);
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_CHARGER_STATE_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %s",
req ? "reply" : "broadcast",
"charger_state", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling charger_state D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_charger_state_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "charger_state query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_charger_state(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* battery_status
* ------------------------------------------------------------------------- */
/** Send battery_status D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_battery_status(DBusMessage *const req)
{
static const char *last = 0;
DBusMessage *msg = NULL;
const char *value = battery_status_to_dbus(battery_status);
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_BATTERY_STATUS_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %s",
req ? "reply" : "broadcast",
"battery_status", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling battery_status D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_battery_status_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "battery_status query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_battery_status(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* battery_state
* ------------------------------------------------------------------------- */
/** Send battery_state D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_battery_state(DBusMessage *const req)
{
static const char *last = 0;
DBusMessage *msg = NULL;
const char *value = battery_state_to_dbus(battery_state);
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_BATTERY_STATE_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %s",
req ? "reply" : "broadcast",
"battery_state", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling battery_state D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_battery_state_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "battery_state query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_battery_state(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* battery_level
* ------------------------------------------------------------------------- */
/** Send battery_level D-Bus signal / method call reply
*
* @param req method call message to reply, or NULL to send signal
*/
static void
common_dbus_send_battery_level(DBusMessage *const req)
{
static dbus_int32_t last = MCE_BATTERY_LEVEL_UNKNOWN - 1;
DBusMessage *msg = NULL;
dbus_int32_t value = battery_level;
/* Normalize to values allowed by MCE D-Bus api documentation */
if( value < 0 )
value = MCE_BATTERY_LEVEL_UNKNOWN;
else if( value > 100 )
value = 100;
if( req ) {
msg = dbus_new_method_reply(req);
}
else if( last == value ) {
goto EXIT;
}
else {
last = value;
msg = dbus_new_signal(MCE_SIGNAL_PATH,
MCE_SIGNAL_IF,
MCE_BATTERY_LEVEL_SIG);
}
if( !dbus_message_append_args(msg,
DBUS_TYPE_INT32, &value,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "%s: %s = %d",
req ? "reply" : "broadcast",
"battery_level", value);
dbus_send_message(msg), msg = 0;
EXIT:
if( msg )
dbus_message_unref(msg);
}
/** Callback for handling battery_level D-Bus queries
*
* @param req method call message to reply
*/
static gboolean
common_dbus_get_battery_level_cb(DBusMessage *const req)
{
mce_log(LL_DEBUG, "battery_level query from: %s",
mce_dbus_get_message_sender_ident(req));
if( !dbus_message_get_no_reply(req) )
common_dbus_send_battery_level(req);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* memnotify_level
* ------------------------------------------------------------------------- */
/** Send memory use level signal on system bus
*/
static void
common_dbus_send_memnotify_level(void)
{
/* Initialize last seen value to something that is never used */
memnotify_level_t last = MEMNOTIFY_LEVEL_COUNT;
if( last != memnotify_level ) {
last = memnotify_level;
const char *sig = MCE_MEMORY_LEVEL_SIG;
const char *arg = memnotify_level_repr(memnotify_level);
mce_log(LL_DEVEL, "sending dbus signal: %s %s", sig, arg);
dbus_send(0, MCE_SIGNAL_PATH, MCE_SIGNAL_IF, sig, 0,
DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
}
}
/** D-Bus callback for the get memory level method call
*
* @param msg The D-Bus message
*
* @return TRUE
*/
static gboolean
common_dbus_get_memnotify_level_cb(DBusMessage *const req)
{
mce_log(LL_DEVEL, "Received memory level get request from %s",
mce_dbus_get_message_sender_ident(req));
DBusMessage *rsp = dbus_new_method_reply(req);
const char *arg = memnotify_level_repr(memnotify_level);
if( !dbus_message_append_args(rsp,
DBUS_TYPE_STRING, &arg,
DBUS_TYPE_INVALID) )
goto EXIT;
mce_log(LL_DEBUG, "sending memory level reply: %s", arg);
dbus_send_message(rsp), rsp = 0;
EXIT:
if( rsp )
dbus_message_unref(rsp);
return TRUE;
}
/* ------------------------------------------------------------------------- *
* init/quit
* ------------------------------------------------------------------------- */
/** Array of dbus message handlers */
static mce_dbus_handler_t common_dbus_handlers[] =
{
/* signals - outbound (for Introspect purposes only) */
{
.interface = MCE_SIGNAL_IF,
.name = MCE_USB_CABLE_STATE_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"usb_cable_state\" type=\"s\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_CHARGER_TYPE_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"charger_type\" type=\"s\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_CHARGER_STATE_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"charger_state\" type=\"s\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_BATTERY_STATUS_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"battery_status\" type=\"s\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_BATTERY_STATE_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"battery_state\" type=\"s\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_BATTERY_LEVEL_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"battery_level\" type=\"i\"/>\n"
},
{
.interface = MCE_SIGNAL_IF,
.name = MCE_MEMORY_LEVEL_SIG,
.type = DBUS_MESSAGE_TYPE_SIGNAL,
.args =
" <arg name=\"memory_level\" type=\"s\"/>\n"
},
/* method calls */
{
.interface = MCE_REQUEST_IF,
.name = MCE_USB_CABLE_STATE_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_usb_cable_state_cb,
.args =
" <arg direction=\"out\" name=\"usb_cable_state\" type=\"s\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_CHARGER_TYPE_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_charger_type_cb,
.args =
" <arg direction=\"out\" name=\"charger_type\" type=\"s\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_CHARGER_STATE_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_charger_state_cb,
.args =
" <arg direction=\"out\" name=\"charger_state\" type=\"s\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_BATTERY_STATUS_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_battery_status_cb,
.args =
" <arg direction=\"out\" name=\"battery_status\" type=\"s\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_BATTERY_STATE_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_battery_state_cb,
.args =
" <arg direction=\"out\" name=\"battery_state\" type=\"s\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_BATTERY_LEVEL_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_battery_level_cb,
.args =
" <arg direction=\"out\" name=\"battery_level\" type=\"i\"/>\n"
},
{
.interface = MCE_REQUEST_IF,
.name = MCE_MEMORY_LEVEL_GET,
.type = DBUS_MESSAGE_TYPE_METHOD_CALL,
.callback = common_dbus_get_memnotify_level_cb,
.args =
" <arg direction=\"out\" name=\"memory_level\" type=\"s\"/>\n"
},
/* sentinel */
{
.interface = 0
}
};
/** Timer callback id for broadcasting initial states */
static guint common_dbus_initial_id = 0;
/** Timer callback function for broadcasting initial states
*
* @param aptr (not used)
*
* @return FALSE to stop idle callback from repeating
*/
static gboolean common_dbus_initial_cb(gpointer aptr)
{
(void)aptr;
/* Do explicit broadcast of initial states.
*
* Note that we expect nothing to happen here, unless the
* datapipe initialization for some reason ends up leaving
* some values to undefined state.
*/
common_dbus_send_usb_cable_state(0);
common_dbus_send_charger_type(0);
common_dbus_send_charger_state(0);
common_dbus_send_battery_status(0);
common_dbus_send_battery_state(0);
common_dbus_send_battery_level(0);
common_dbus_send_memnotify_level();
common_dbus_initial_id = 0;
return FALSE;
}
/** Add dbus handlers
*/
static void common_dbus_init(void)
{
mce_dbus_handler_register_array(common_dbus_handlers);
/* To avoid unnecessary jitter on startup, allow dbus service tracking
* and datapipe initialization some time to come up with proper initial
* state values before forcing broadcasting to dbus */
if( !common_dbus_initial_id )
common_dbus_initial_id = g_timeout_add(1000, common_dbus_initial_cb, 0);
}
/** Remove dbus handlers
*/
static void common_dbus_quit(void)
{
if( common_dbus_initial_id ) {
g_source_remove(common_dbus_initial_id),
common_dbus_initial_id = 0;
}
mce_dbus_handler_unregister_array(common_dbus_handlers);
}
/* ========================================================================= *
* DATAPIPE_FUNCTIONS
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* usb_cable_state
* ------------------------------------------------------------------------- */
/** Callback for handling usb_cable_state_pipe state changes
*
* @param data usb_cable_state (as void pointer)
*/
static void common_datapipe_usb_cable_state_cb(gconstpointer data)
{
usb_cable_state_t prev = usb_cable_state;
usb_cable_state = GPOINTER_TO_INT(data);
if( usb_cable_state == prev )
goto EXIT;
/* The enumerated states do not have 1:1 string mapping, so to
* avoid sending duplicate signals also the representation
* values need to be checked. */
const char *value_old = usb_cable_state_to_dbus(prev);
const char *value_new = usb_cable_state_to_dbus(usb_cable_state);
if( !strcmp(value_old, value_new) )
goto EXIT;
mce_log(LL_DEBUG, "usb_cable_state = %s -> %s",
value_old, value_new);
common_dbus_send_usb_cable_state(0);
EXIT:
return;
}