/
mce-sensorfw.c
2252 lines (1844 loc) · 49.8 KB
/
mce-sensorfw.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
#include "mce-sensorfw.h"
#include "mce.h"
#include "mce-log.h"
#include "mce-dbus.h"
#include "libwakelock.h"
#include <linux/input.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <glib.h>
/** org.freedesktop.DBus.NameOwnerChanged D-Bus signal */
#define DBUS_NAME_OWNER_CHANGED_SIG "NameOwnerChanged"
#define SENSORFW_SERVICE "com.nokia.SensorService"
#define SENSORFW_PATH "/SensorManager"
#define SENSOR_SOCKET "/var/run/sensord.sock"
#if 0 // DEBUG: make all logging from this module "critical"
# undef mce_log
# define mce_log(LEV, FMT, ARGS...) \
mce_log_file(LL_CRIT, __FILE__, __FUNCTION__, FMT , ## ARGS)
#endif
/** ALS data block as sensord sends them */
typedef struct als_data {
uint64_t timestamp; /* microseconds, monotonic */
uint32_t value;
} als_data_t;
/** PS data block as sensord sends them */
typedef struct ps_data {
uint64_t timestamp; /* microseconds, monotonic */
uint32_t value;
// This should be the size of a C++ bool on the same platform.
// Unfortunately there's no way to find out in a C program
uint8_t withinProximity;
} ps_data_t;
/** We need to differentiate multiple real and synthetic input sources */
typedef enum {
/** Synthetic input, for example when sensord is not running */
DS_FAKED,
/** Data read directly from kernel */
DS_EVDEV,
/** Data received from sensord */
DS_SENSORD,
/** Dummy data, use the last known good value instead */
DS_RESTORE,
} input_source_t;
/** Names of input sources; for debuggging purposes */
static const char * const input_source_name[] =
{
[DS_FAKED] = "SYNTH",
[DS_EVDEV] = "EVDEV",
[DS_SENSORD] = "SENSORD",
[DS_RESTORE] = "RESTORE",
};
/* ========================================================================= *
* STATE DATA
* ========================================================================= */
/** D-Bus System Bus connection */
static DBusConnection *systembus = 0;
/** Flag for sensord is on system bus */
static bool sensord_running = false;
/** Flag for system is suspended */
static bool system_suspended = false;
static void als_notify(unsigned lux, input_source_t srce);
static void ps_notify(bool covered, input_source_t srce);
/* ========================================================================= *
* EVDEV HOOKS
* ========================================================================= */
/** Callback function for processing evdev events
*
* @param chn io channel
* @param cnd (not used)
* @param aptr pointer to io watch
*
* @return TRUE to keep io watch active, FALSE to stop it
*/
static gboolean
mce_sensorfw_evdev_cb(GIOChannel *chn, GIOCondition cnd, gpointer aptr)
{
// we just want the wakeup
(void)cnd;
gboolean keep = FALSE;
int *id = aptr;
int fd = g_io_channel_unix_get_fd(chn);
int als = -1;
int ps = -1;
int rc;
struct input_event eve[256];
/* wakelock must be taken before reading the data */
wakelock_lock("mce_input_handler", -1);
rc = read(fd, eve, sizeof eve);
if( rc == -1 ) {
if( errno == EINTR || errno == EAGAIN )
keep = TRUE;
else
mce_log(LL_ERR, "read events: %m");
goto EXIT;
}
if( rc == 0 ) {
mce_log(LL_ERR, "read events: EOF");
goto EXIT;
}
keep = TRUE;
size_t n = rc / sizeof *eve;
for( size_t i = 0; i < n; ++i ) {
if( eve[i].type != EV_ABS )
continue;
switch( eve[i].code ) {
case ABS_MISC:
als = eve[i].value;
break;
case ABS_DISTANCE:
ps = eve[i].value;
break;
default:
break;
}
}
if( als != -1 )
als_notify(als, DS_EVDEV);
if( ps != -1 )
ps_notify(ps < 1, DS_EVDEV);
EXIT:
if( !keep && *id ) {
*id = 0;
mce_log(LL_CRIT, "stopping io watch");
}
/* wakelock must be released when we are done with the data */
wakelock_unlock("mce_input_handler");
return keep;
}
/** Helper function for registering io watch
*
* @param fd file descriptor to watch
* @param cb io watch callback function
* @param aptr context to pass to the callback
*
* @return source id, or 0 in case of errors
*/
static guint
mce_sensorfw_start_iomon(int fd, GIOFunc cb, void *aptr)
{
GIOChannel *chn = 0;
guint id = 0;
if( !(chn = g_io_channel_unix_new(fd)) ) {
goto EXIT;
}
if( !(id = g_io_add_watch(chn, G_IO_IN, cb, aptr)) )
goto EXIT;
g_io_channel_set_close_on_unref(chn, TRUE), fd = -1;
EXIT:
if( chn != 0 ) g_io_channel_unref(chn);
if( fd != -1 ) close(fd);
return id;
}
/* ========================================================================= *
* COMMON
* ========================================================================= */
/** Add input watch for sensord session
*
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
* @param datafunc glib io watch callback
*
* @param glib io watch source id, or 0 in case of failure
*/
static guint
mce_sensorfw_add_io_watch(int sessionid, GIOFunc datafunc)
{
guint wid = 0;
int fd = -1;
int32_t sid = sessionid;
char ack = 0;
GIOChannel *chn = 0;
struct sockaddr_un sa;
socklen_t sa_len;
mce_log(LL_INFO, "adding watch for session %d", sessionid);
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) {
mce_log(LL_ERR, "could not open local domain socket");
goto EXIT;
}
memset(&sa, 0, sizeof sa);
sa.sun_family = AF_UNIX;
snprintf(sa.sun_path, sizeof sa.sun_path, "%s", SENSOR_SOCKET);
sa_len = strchr(sa.sun_path, 0) + 1 - (char *)&sa;
if( connect(fd, (struct sockaddr *) &sa, sa_len) == -1 ) {
mce_log(LL_ERR, "could not connect to %s: %m",
SENSOR_SOCKET);
goto EXIT;
}
errno = 0;
if( write(fd, &sid, sizeof sid) != sizeof sid ) {
mce_log(LL_ERR, "could not initialize reader for"
" session %d: %m",
sessionid);
goto EXIT;
}
errno = 0;
if( read(fd, &ack, 1) != 1 || ack != '\n' ) {
mce_log(LL_ERR, "could not get handshake for"
" session %d: %m",
sessionid);
goto EXIT;
}
if( !(chn = g_io_channel_unix_new(fd)) ) {
goto EXIT;
}
if( !(wid = g_io_add_watch(chn, G_IO_IN, datafunc, 0)) ) {
goto EXIT;
}
g_io_channel_set_close_on_unref(chn, TRUE), fd = -1;
mce_log(LL_DEBUG, "io watch for %d = %u", sessionid, wid);
EXIT:
if( chn != 0 ) g_io_channel_unref(chn);
if( fd != -1 ) close(fd);
return wid;
}
/** Issue load sensor IPC to sensord
*
* @param id sensor name
*
* @return true on success, or false in case of errors
*/
static bool
mce_sensorfw_load_sensor(const char *id)
{
bool res = false;
DBusError err = DBUS_ERROR_INIT;
DBusMessage *msg = 0;
dbus_bool_t ack = FALSE;
mce_log(LL_INFO, "loadPlugin(%s)", id);
// FIXME: should not block ...
msg = dbus_send_with_block(SENSORFW_SERVICE,
SENSORFW_PATH,
"local.SensorManager",
"loadPlugin",
DBUS_TIMEOUT_USE_DEFAULT,
DBUS_TYPE_STRING, &id,
DBUS_TYPE_INVALID);
if( !msg ) {
mce_log(LOG_ERR, "loadPlugin(%s): no reply", id);
goto EXIT;
}
if( dbus_set_error_from_message(&err, msg) ) {
mce_log(LOG_ERR, "loadPlugin(%s): error reply: %s: %s",
id, err.name, err.message);
goto EXIT;
}
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_BOOLEAN, &ack,
DBUS_TYPE_INVALID) ) {
mce_log(LOG_ERR, "loadPlugin(%s): parse reply: %s: %s",
id, err.name, err.message);
goto EXIT;
}
if( !ack ) {
mce_log(LOG_WARNING, "loadPlugin(%s): request denied", id);
goto EXIT;
}
res = true;
EXIT:
if( msg ) dbus_message_unref(msg);
dbus_error_free(&err);
return res;
}
/** Issue request sensor IPC to sensord
*
* @param id sensor name
*
* @return session id, or -1 in case of errors
*/
static int
mce_sensorfw_request_sensor(const char *id)
{
int res = -1;
DBusMessage *msg = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_int64_t pid = getpid();
dbus_int32_t sid = -1;
mce_log(LL_INFO, "requestSensor(%s)", id);
// FIXME: should not block ...
msg = dbus_send_with_block(SENSORFW_SERVICE,
SENSORFW_PATH,
"local.SensorManager",
"requestSensor",
DBUS_TIMEOUT_USE_DEFAULT,
DBUS_TYPE_STRING, &id,
DBUS_TYPE_INT64, &pid,
DBUS_TYPE_INVALID);
if( !msg ) {
mce_log(LL_ERR, "requestSensor(%s): no reply", id);
goto EXIT;
}
if( dbus_set_error_from_message(&err, msg) ) {
mce_log(LL_ERR, "requestSensor(%s): error reply: %s: %s",
id, err.name, err.message);
goto EXIT;
}
// NOTE: sessionid is an 'int' so we should use DBUS_TYPE_INT64
// on a 64-bit platform.
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_INT32, &sid,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "requestSensor(%s): parse reply: %s: %s",
id, err.name, err.message);
goto EXIT;
}
if( sid == -1 ) {
mce_log(LL_ERR, "requestSensor(%s): failed", id);
goto EXIT;
}
res = (int)sid;
mce_log(LL_INFO, "requestSensor(%s): session=%d", id, res);
EXIT:
if( msg ) dbus_message_unref(msg);
return res;
}
/** Issue release sensor IPC to sensord
*
* @param id sensor name
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
*
* @return true on success, or false in case of errors
*/
static bool
mce_sensorfw_release_sensor(const char *id, int sessionid)
{
bool res = false;
DBusMessage *msg = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_int64_t pid = getpid();
dbus_int32_t sid = sessionid;
dbus_bool_t ack = FALSE;
mce_log(LL_INFO, "releaseSensor(%s, %d)", id, sessionid);
// FIXME: should not block ...
msg = dbus_send_with_block(SENSORFW_SERVICE,
SENSORFW_PATH,
"local.SensorManager",
"releaseSensor",
DBUS_TIMEOUT_USE_DEFAULT,
DBUS_TYPE_STRING, &id,
DBUS_TYPE_INT32, &sid,
DBUS_TYPE_INT64, &pid,
DBUS_TYPE_INVALID);
if( !msg ) {
mce_log(LL_ERR, "releaseSensor(%s, %d): no reply", id,
sessionid);
goto EXIT;
}
if( dbus_set_error_from_message(&err, msg) ) {
mce_log(LL_ERR, "releaseSensor(%s, %d): error reply: %s: %s",
id, sessionid, err.name, err.message);
goto EXIT;
}
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_BOOLEAN, &ack,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "releaseSensor(%s, %d): parse reply: %s: %s",
id, sessionid, err.name, err.message);
goto EXIT;
}
if( !ack ) {
mce_log(LL_WARN, "releaseSensor(%s, %d): failed", id,
sessionid);
goto EXIT;
}
res = true;
mce_log(LL_DEBUG, "releaseSensor(%s, %d): success", id, sessionid);
EXIT:
if( msg ) dbus_message_unref(msg);
return res;
}
/** Issue start sensor IPC to sensord
*
* @param id sensor name
* @param iface D-Bus interface for the sensor
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
*
* @return true on success, or false in case of errors
*/
static bool
mce_sensorfw_start_sensor(const char *id, const char *iface, int sessionid)
{
bool res = false;
char *path = 0;
dbus_int32_t sid = sessionid;
mce_log(LL_INFO, "start(%s, %d)", id, sessionid);
if( asprintf(&path, "%s/%s", SENSORFW_PATH, id) < 0 ) {
path = 0;
goto EXIT;
}
res = dbus_send(SENSORFW_SERVICE,
path,
iface,
"start",
NULL,
DBUS_TYPE_INT32, &sid,
DBUS_TYPE_INVALID);
EXIT:
free(path);
return res;
}
/** Issue stop sensor IPC to sensord
*
* @param id sensor name
* @param iface D-Bus interface for the sensor
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
*
* @return true on success, or false in case of errors
*/
static bool
mce_sensorfw_stop_sensor(const char *id, const char *iface, int sessionid)
{
bool res = false;
char *path = 0;
dbus_int32_t sid = sessionid;
mce_log(LL_INFO, "stop(%s, %d)",id, sessionid);
if( asprintf(&path, "%s/%s", SENSORFW_PATH, id) < 0 ) {
path = 0;
goto EXIT;
}
res = dbus_send(SENSORFW_SERVICE,
path,
iface,
"stop",
NULL,
DBUS_TYPE_INT32, &sid,
DBUS_TYPE_INVALID);
EXIT:
free(path);
return res;
}
/** Callback for handling replies to setStandbyOverride requests
*
* This is used only for logging possible error replies we
* might get from trying to set the standby override property.
*
* @param pc pending call object
* @param aptr (not used)
*/
static void
mce_sensorfw_set_standby_override_cb(DBusPendingCall *pc, void *aptr)
{
(void)aptr;
static const char method[] = "setStandbyOverride";
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_bool_t val = FALSE;
mce_log(LL_INFO, "Received %s() reply", method);
if( !(rsp = dbus_pending_call_steal_reply(pc)) )
goto EXIT;
if( dbus_set_error_from_message(&err, rsp) ) {
mce_log(LL_ERR, "%s(): %s: %s", method, err.name, err.message);
goto EXIT;
}
if( !dbus_message_get_args(rsp, &err,
DBUS_TYPE_BOOLEAN, &val,
DBUS_TYPE_INVALID) ) {
mce_log(LL_ERR, "%s(): %s: %s", method, err.name, err.message);
goto EXIT;
}
mce_log(LL_INFO, "%s() -> %s", method, val ? "success" : "failure");
EXIT:
if( rsp ) dbus_message_unref(rsp);
dbus_pending_call_unref(pc);
dbus_error_free(&err);
}
/** Issue sensor standby override request to sensord
*
* @param id sensor name
* @param iface D-Bus interface for the sensor
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
* @param value true to enable standby override, false to disable
*
* @return true on success, or false in case of errors
*/
static bool
mce_sensorfw_set_standby_override(const char *id, const char *iface,
int sessionid, bool value)
{
bool res = false;
char *path = 0;
dbus_int32_t sid = sessionid;
dbus_bool_t val = value;
mce_log(LL_INFO, "setStandbyOverride(%s, %d, %d)",
id, sessionid, val);
if( asprintf(&path, "%s/%s", SENSORFW_PATH, id) < 0 ) {
path = 0;
goto EXIT;
}
res = dbus_send(SENSORFW_SERVICE,
path,
iface,
"setStandbyOverride",
mce_sensorfw_set_standby_override_cb,
DBUS_TYPE_INT32, &sid,
DBUS_TYPE_BOOLEAN, &val,
DBUS_TYPE_INVALID);
EXIT:
free(path);
return res;
}
/* ========================================================================= *
* ALS
* ========================================================================= */
/** io watch id for ALS evdev file descriptor*/
static guint als_evdev_id = 0;
/** Sensord name for ALS */
static const char als_name[] = "alssensor";
/** Sensord D-Bus interface for ALS */
static const char als_iface[] = "local.ALSSensor";
/** Sensord session id for ALS */
static int als_sid = -1;
/** Input watch for ALS data */
static guint als_wid = 0;
/** Flag for MCE wants to enable ALS */
static bool als_want = false;
/** Flag for ALS enabled at sensord */
static bool als_have = false;
/** Callback for sending ALS data to where it is needed */
static void (*als_notify_cb)(unsigned lux) = 0;
/** Ambient light value to report when sensord is not available */
#define ALS_VALUE_WHEN_SENSORD_IS_DOWN 400
/** Wrapper for als_notify_cb hook
*
* @param lux ambient light value
* @param srce where does to value originate from
*/
static void
als_notify(unsigned lux, input_source_t srce)
{
static unsigned als_lux_last = ALS_VALUE_WHEN_SENSORD_IS_DOWN;
if( srce == DS_RESTORE )
lux = als_lux_last;
/* If we have evdev source, prefer that over sensord input */
if( als_evdev_id ) {
if( srce == DS_EVDEV )
als_lux_last = lux;
if( srce == DS_SENSORD ) {
if( lux != als_lux_last )
mce_log(LL_WARN, "sensord=%u vs evdev=%u",
lux, als_lux_last);
goto EXIT;
}
}
else {
if( srce == DS_SENSORD )
als_lux_last = lux;
if( srce == DS_EVDEV )
goto EXIT;
}
mce_log(LL_DEBUG, "ALS: %u lux (%s)", lux,input_source_name[srce]);
if( als_notify_cb )
als_notify_cb(lux);
else if( srce != DS_FAKED )
mce_log(LL_INFO, "ALS data without notify cb");
EXIT:
return;
}
/** Handle ALS input from sensord
*
* @param chn io channel
* @param cnd (not used)
* @param aptr (not used)
*
* @return TRUE to keep io watch, or FALSE to remove it
*/
static gboolean
als_input_cb(GIOChannel *chn, GIOCondition cnd, gpointer aptr)
{
mce_log(LL_DEBUG, "@%s()", __FUNCTION__);
(void)cnd; (void)aptr; // unused
gboolean keep_going = FALSE;
uint32_t count = 0;
int fd;
int rc;
als_data_t data;
memset(&data, 0, sizeof data);
if( (fd = g_io_channel_unix_get_fd(chn)) < 0 ) {
mce_log(LL_ERR, "io channel has no fd");
goto EXIT;
}
// FIXME: there should be only one read() ...
rc = read(fd, &count, sizeof count);
if( rc == -1 ) {
if( errno == EINTR || errno == EAGAIN )
keep_going = TRUE;
else
mce_log(LL_ERR, "read sample count: %m");
goto EXIT;
}
if( rc == 0 ) {
mce_log(LL_ERR, "read sample count: EOF");
goto EXIT;
}
if( rc != (int)sizeof count) {
mce_log(LL_ERR, "read sample count: got %d of %d bytes",
rc, (int)sizeof count);
goto EXIT;
}
mce_log(LL_DEBUG, "Got %u ALS values", (unsigned)count);
if( count < 1 ) {
keep_going = TRUE;
goto EXIT;
}
for( unsigned i = 0; i < count; ++i ) {
errno = 0, rc = read(fd, &data, sizeof data);
if( rc != sizeof data ) {
mce_log(LL_ERR, "failed to read sample: %m");
goto EXIT;
}
}
mce_log(LL_DEBUG, "last ALS value = %u", data.value);
als_notify(data.value, DS_SENSORD);
keep_going = TRUE;
EXIT:
if( !keep_going ) {
mce_log(LL_CRIT, "disabling io watch");
als_wid = 0;
}
return keep_going;
}
/** Handle reply to initial ALS value request
*
* @param pc pending call object
* @param aptr (not used)
*/
static void mce_sensorfw_als_read_cb(DBusPendingCall *pc, void *aptr)
{
(void)aptr;
mce_log(LL_INFO, "Received intial ALS lux reply");
bool res = false;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
dbus_uint64_t tck = 0;
dbus_uint32_t lux = 0;
DBusMessageIter body, var, rec;
if( !(rsp = dbus_pending_call_steal_reply(pc)) )
goto EXIT;
if( dbus_set_error_from_message(&err, rsp) ) {
mce_log(LL_ERR, "als lux error reply: %s: %s",
err.name, err.message);
goto EXIT;
}
if( !dbus_message_iter_init(rsp, &body) )
goto EXIT;
if( dbus_message_iter_get_arg_type(&body) != DBUS_TYPE_VARIANT )
goto EXIT;
dbus_message_iter_recurse(&body, &var);
dbus_message_iter_next(&body);
if( dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRUCT )
goto EXIT;
dbus_message_iter_recurse(&var, &rec);
dbus_message_iter_next(&var);
if( dbus_message_iter_get_arg_type(&rec) != DBUS_TYPE_UINT64 )
goto EXIT;
dbus_message_iter_get_basic(&rec, &tck);
dbus_message_iter_next(&rec);
if( dbus_message_iter_get_arg_type(&rec) != DBUS_TYPE_UINT32 )
goto EXIT;
dbus_message_iter_get_basic(&rec, &lux);
dbus_message_iter_next(&rec);
mce_log(LL_INFO, "initial ALS value = %u", lux);
als_notify(lux, DS_SENSORD);
res = true;
EXIT:
if( !res ) mce_log(LL_WARN, "did not get initial lux value");
if( rsp ) dbus_message_unref(rsp);
dbus_pending_call_unref(pc);
dbus_error_free(&err);
}
/** Issue get ALS value IPC to sensord
*
* @param id sensor name
* @param iface D-Bus interface for the sensor
* @param sessionid sensord session id from mce_sensorfw_request_sensor()
*/
static void
mce_sensorfw_als_read(const char *id, const char *iface, int sessionid)
{
(void)sessionid;
char *path = 0;
const char *prop = "lux";
mce_log(LL_INFO, "read(%s, %d)", id, sessionid);
if( asprintf(&path, "%s/%s", SENSORFW_PATH, id) < 0 ) {
path = 0;
goto EXIT;
}
dbus_send(SENSORFW_SERVICE,
path,
"org.freedesktop.DBus.Properties",
"Get",
mce_sensorfw_als_read_cb,
DBUS_TYPE_STRING, &iface,
DBUS_TYPE_STRING, &prop,
DBUS_TYPE_INVALID);
EXIT:
free(path);
}
/** Close ALS session with sensord
*/
static void mce_sensorfw_als_stop_session(void)
{
mce_log(LL_DEBUG, "@%s()", __FUNCTION__);
if( als_sid != -1 ) {
if( sensord_running )
mce_sensorfw_release_sensor(als_name, als_sid);
als_sid = -1;
}
if( als_wid ) {
g_source_remove(als_wid), als_wid = 0;
als_notify(ALS_VALUE_WHEN_SENSORD_IS_DOWN, DS_FAKED);
}
als_have = false;
}
/** Have ALS session with sensord predicate
*
* @return true if session exists, or false if not
*/
static bool mce_sensorfw_als_have_session(void)
{
return als_wid != 0;
}
/** Open ALS session with sensord
*
* @return true on success, or false in case of errors
*/
static bool mce_sensorfw_als_start_session(void)
{
if( als_wid )
goto EXIT;
if( !mce_sensorfw_load_sensor(als_name) )
goto EXIT;
als_sid = mce_sensorfw_request_sensor(als_name);
if( als_sid < 0 )
goto EXIT;
als_wid = mce_sensorfw_add_io_watch(als_sid, als_input_cb);
if( als_wid == 0 )
goto EXIT;
EXIT:
if( !als_wid ) {
// all or nothing
mce_sensorfw_als_stop_session();
}
return als_wid != 0;
}
/** Enable ALS via sensord
*/
static void mce_sensorfw_als_start_sensor(void)
{
if( als_have )
goto EXIT;
if( !mce_sensorfw_als_start_session() )
goto EXIT;
if( !mce_sensorfw_start_sensor(als_name, als_iface, als_sid) )
goto EXIT;
als_have = true;
/* There is no quarantee that we get sensor input
* anytime soon, so we make an explicit get current
* value request after starting sensor */
mce_sensorfw_als_read(als_name, als_iface, als_sid);
EXIT:
return;
}
/** Disable ALS via sensord
*/
static void mce_sensorfw_als_stop_sensor(void)
{
if( !als_have )
goto EXIT;
if( mce_sensorfw_als_have_session() )
mce_sensorfw_stop_sensor(als_name, als_iface, als_sid);
als_have = false;
EXIT:
return;
}
/** Rethink ALS enabled state
*/
static void
mce_sensorfw_als_rethink(void)
{
mce_log(LL_DEBUG, "@%s()", __FUNCTION__);
if( !sensord_running ) {
mce_log(LL_WARN, "skipping als enable/disable;"
" sensord not available");
goto EXIT;
}
if( als_want == als_have )
goto EXIT;
if( system_suspended ) {
mce_log(LL_NOTICE, "skipping als enable/disable;"
" should be suspended");
goto EXIT;
}
if( als_want ) {
als_notify(0, DS_RESTORE);
mce_sensorfw_als_start_sensor();
}
else
mce_sensorfw_als_stop_sensor();
EXIT:
return;
}
/** Try to enable ALS input
*/
void
mce_sensorfw_als_enable(void)
{
mce_log(LL_DEBUG, "@%s()", __FUNCTION__);
als_want = true;
mce_sensorfw_als_rethink();
}