/
iphb.c
2834 lines (2281 loc) · 69.9 KB
/
iphb.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
/**
@brief IP heartbeat service dsme plug-in
@file iphbd.c
IP heartbeat service dsme plug-in
<p>
Copyright (C) 2010 Nokia. All rights reserved.
@author Raimo Vuonnala <raimo.vuonnala@nokia.com>
@author Semi Malinen <semi.malinen@nokia.com>
@author Simo Piiroinen <simo.piiroinen@jollamobile.com>
This file is part of Dsme.
Dsme 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.
Dsme 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 Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#define _ISOC99_SOURCE
#define _GNU_SOURCE
#include <iphbd/libiphb.h>
#include <iphbd/iphb_internal.h>
#include "dbusproxy.h"
#include "dsme_dbus.h"
#include "heartbeat.h"
#include "dsme/modules.h"
#include "dsme/modulebase.h"
#include "dsme/logging.h"
#include "dsme/timers.h"
#include "dsme/dsme-wdd-wd.h"
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/time.h>
#include <time.h>
#include <glib.h>
#include <sys/ioctl.h>
#include <linux/rtc.h>
#include <limits.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <mce/dbus-names.h>
#include "../include/android/android_alarm.h"
/* ------------------------------------------------------------------------- *
* Constants
* ------------------------------------------------------------------------- */
/** Prefix string for diagnostic messages from this module */
#define PFIX "IPHB: "
/** Maximum number of epoll events to process in one go */
#define DSME_MAX_EPOLL_EVENTS 10
/** How long it takes to power up to act dead mode to show alarms */
#define STARTUP_TIME_ESTIMATE_SECS 60
/** How long it takes to shutdown the device */
#define SHUTDOWN_TIME_ESTIMATE_SECS 60
/** How long we give mce time to take over RTC wakeup wakelock
*
* The time is specified in milliseconds, or -1 for no timeout
*/
#define RTC_WAKEUP_TIMEOUT_MS 2000
/** Maximum time to stay in suspend [seconds]; zero for no limit */
#define RTC_MAXIMUM_WAKEUP_TIME (30*60) // 30 minutes
/** Image create time = mtime of mer-release file */
#define IMAGE_TIME_STAMP_FILE "/etc/mer-release"
/** Saved system time = mtime of saved-time file */
#define SAVED_TIME_FILE "/var/tmp/saved-time"
/** Where to persistently store alarm state data received from timed */
#define XTIMED_STATE_FILE "/var/lib/dsme/timed_state"
/* ------------------------------------------------------------------------- *
* Custom types
* ------------------------------------------------------------------------- */
/** @brief Allocated structure of one client in the linked client list in iphbd
*/
typedef struct _client_t {
int fd; /*!< IPC (Unix domain) socket or -1 */
endpoint_t *conn; /*!< internal client endpoint (if fd == -1) */
void *data; /*!< internal client cookie (if fd == -1) */
char *pidtxt; /*!< Client description, for debugging */
struct timeval reqtime; /*!< {0,0} if client has not subscribed to wake-up call */
struct timeval mintime; /*!< min end of sleep period */
struct timeval maxtime; /*!< max end of sleep period */
pid_t pid; /*!< client process ID */
bool wakeup; /*!< resume to handle */
struct _client_t *next; /*!< pointer to the next client in the list (NULL if none) */
} client_t;
/* ------------------------------------------------------------------------- *
* Function prototypes
* ------------------------------------------------------------------------- */
static void clientlist_wakeup_clients_if(const struct timeval *now);
static bool epollfd_add_fd(int fd, void *ptr);
static void epollfd_remove_fd(int fd);
static void systemtime_init(void);
static char *tm_repr(const struct tm *tm, char *buff, size_t size);
static char *t_repr(time_t t, char *buff, size_t size);
/* ------------------------------------------------------------------------- *
* Variables
* ------------------------------------------------------------------------- */
/** Path to android alarm device node */
static const char android_alarm_path[] = "/dev/alarm";
/** File descriptor for android alarm device node */
static int android_alarm_fd = -1;
/** Handle to the epoll instance */
static int epollfd = -1;
/** I/O watch for epollfd */
static guint epoll_watch = 0;
/** IPC client listen/accept handle */
static int listenfd = -1;
/** Handle to the kernel */
static int kernelfd = -1;
/** Path to RTC device node */
static const char rtc_path[] = "/dev/rtc0";
/** File descriptor for RTC device node */
static int rtc_fd = -1;
/** Linked lits of connected clients */
static client_t *clients = NULL;
/** Timer for serving wakeups with shorter than heartbeat range */
static guint wakeup_timer = 0;
/** System bus connection (for IPC with mce) */
static DBusConnection *systembus = 0;
/** Status of com.nokia.mce on systembus */
static bool mce_is_running = false;
/** Sysfs entry for acquiring wakelocks */
static const char lock_path[] = "/sys/power/wake_lock";
/** Sysfs entry for releasing wakelocks */
static const char unlock_path[] = "/sys/power/wake_unlock";
/** RTC wakeup wakelock - acquired by dsme and released by mce / timeout */
static const char rtc_wakeup[] = "mce_rtc_wakeup";
/** RTC input wakelock - acquired / released by dsme */
static const char rtc_input[] = "dsme_rtc_input";
/** When the next alarm that should powerup/resume the device is due [systime] */
static time_t alarm_powerup = 0;
/** When the next alarm that should resume the device is due [systime] */
static time_t alarm_resume = 0;
/* ------------------------------------------------------------------------- *
* Generic utility functions
* ------------------------------------------------------------------------- */
/** Helper for testing if tv1 < tv2
*
* @param tv1 time value
* @param tv2 time value
*
* @return true if tv1 < tv2, false otherwise
*/
static inline bool tv_lt(const struct timeval *tv1, const struct timeval *tv2)
{
return timercmp(tv1, tv2, <);
}
/** Helper for testing if tv1 > tv2
*
* @param tv1 time value
* @param tv2 time value
*
* @return true if tv1 > tv2, false otherwise
*/
static inline bool tv_gt(const struct timeval *tv1, const struct timeval *tv2)
{
return timercmp(tv1, tv2, >);
}
/** Helper for testing if tv1 >= tv2
*
* @param tv1 time value
* @param tv2 time value
*
* @return true if tv1 >= tv2, false otherwise
*/
static inline bool tv_ge(const struct timeval *tv1, const struct timeval *tv2)
{
return !tv_lt(tv1, tv2);
}
/** Helper for getting monotonic time as struct timeval
*
* @param tv place to store the monotonic time
*
* @return true on success, or false on failure
*/
static bool monotime_get_tv(struct timeval *tv)
{
bool res = false;
struct timespec ts;
#if defined(CLOCK_BOOTTIME)
if( clock_gettime(CLOCK_BOOTTIME, &ts) < 0 ) {
if( clock_gettime(CLOCK_MONOTONIC, &ts) < 0 )
timerclear(tv);
}
#else
if( clock_gettime(CLOCK_MONOTONIC, &ts) < 0 )
timerclear(tv);
#endif
else {
TIMESPEC_TO_TIMEVAL(tv, &ts);
res = true;
}
return res;
}
/** Helper for getting system time as struct timeval
*
* @param tv place to store the system time
*
* @return true on success, or false on failure
*/
static bool realtime_get_tv(struct timeval *tv)
{
bool res = false;
if( gettimeofday(tv, 0) < 0 )
timerclear(tv);
else
res = true;
return res;
}
/** Emit wakeup time stamp to log
*
* @param lev logging level
* @param title string to emit before time stamp
* @param t time stamp to print
* @param now time now
*/
static void log_time_t(int lev, const char *title, time_t t, time_t now)
{
struct tm tm;
char left[32];
if( t <= 0 ) {
dsme_log(lev, PFIX"%s: not set", title);
goto cleanup;
}
snprintf(left, sizeof left, " (T%+ld)", (long)(now - t));
memset(&tm, 0, sizeof tm);
gmtime_r(&t, &tm);
dsme_log(lev, PFIX"%s: %04d-%02d-%02d %02d:%02d:%02d%s",
title,
tm.tm_year + 1900,
tm.tm_mon + 1,
tm.tm_mday,
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
left);
cleanup:
return;
}
/* ------------------------------------------------------------------------- *
* ipc with hwwd kicker process
* ------------------------------------------------------------------------- */
/** Tell hwwd kicker process that we're still alive */
static void hwwd_feeder_sync(void)
{
/* The parent process is hwwd kicker, and the SIGHUP will interrupt
* the nanosleep() it is most likely at */
kill(getppid(), SIGHUP);
}
/* ------------------------------------------------------------------------- *
* Utilities for manipulating wakelocks
* ------------------------------------------------------------------------- */
/** Whether the wakelock sysfs paths are available
*
* @return true if wakelocks are supported, false otherwise
*/
static bool wakelock_supported(void)
{
static bool checked = false;
static bool supported = false;
if( !checked ) {
checked = true;
supported = (access(lock_path, W_OK) == 0 &&
access(unlock_path, W_OK) == 0);
}
return supported;
}
/** Helper for writing to sysfs files
*
* @param path file to write to
* @param data string to write
*/
static void wakelock_write(const char *path, const char *data, int ignore)
{
int file;
if( (file = TEMP_FAILURE_RETRY(open(path, O_WRONLY))) == -1 ) {
dsme_log(LOG_WARNING, PFIX"%s: open: %m", path);
}
else {
int size = strlen(data);
errno = 0;
if( TEMP_FAILURE_RETRY(write(file, data, size)) != size ) {
if( errno != ignore )
dsme_log(LOG_WARNING, PFIX"%s: write: %m", path);
}
if( TEMP_FAILURE_RETRY(close(file)) == -1 ) {
dsme_log(LOG_WARNING, PFIX"%s: close: %m", path);
}
}
}
/** Use sysfs interface to create and enable a wakelock.
*
* @param name The name of the wakelock to obtain
* @param ms Time in milliseconds before the wakelock gets released
* automatically, or negative value for no timeout.
*/
static void wakelock_lock(const char *name, int ms)
{
dsme_log(LOG_DEBUG, PFIX"LOCK: %s %d", name, ms);
if( wakelock_supported() ) {
char tmp[256];
if( ms < 0 ) {
snprintf(tmp, sizeof tmp, "%s\n", name);
}
else {
long long ns = ms * 1000000LL;
snprintf(tmp, sizeof tmp, "%s %lld\n", name, ns);
}
wakelock_write(lock_path, tmp, -1);
}
}
/** Use sysfs interface to disable a wakelock.
*
* @param name The name of the wakelock to release
*
* Note: This will not delete the wakelocks.
*/
static void wakelock_unlock(const char *name)
{
dsme_log(LOG_DEBUG, PFIX"UNLK: %s", name);
if( wakelock_supported() ) {
char tmp[256];
snprintf(tmp, sizeof tmp, "%s\n", name);
/* assume EINVAL == the wakelock did not exist */
wakelock_write(unlock_path, tmp, EINVAL);
}
}
/* ------------------------------------------------------------------------- *
* D-Bus helper functions
* ------------------------------------------------------------------------- */
/** Create a GetNameOwner method call message
*
* @param name the dbus name to query
*
* @return DBusMessage pointer
*/
static
DBusMessage *
xdbus_create_name_owner_req(const char *name)
{
DBusMessage *req = 0;
req = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS,
"GetNameOwner");
dbus_message_append_args(req,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
return req;
}
/** Parse a reply message to GetNameOwner method call
*
* @param rsp method call reply message
*
* @return dbus name of the name owner, or NULL in case of errors
*/
static
gchar *
xdbus_parse_name_owner_rsp(DBusMessage *rsp)
{
char *res = 0;
DBusError err = DBUS_ERROR_INIT;
char *dta = NULL;
if( dbus_set_error_from_message(&err, rsp) ||
!dbus_message_get_args(rsp, &err,
DBUS_TYPE_STRING, &dta,
DBUS_TYPE_INVALID) ) {
if( strcmp(err.name, DBUS_ERROR_NAME_HAS_NO_OWNER) ) {
dsme_log(LOG_WARNING, PFIX"%s: %s", err.name, err.message);
}
goto cleanup;
}
res = g_strdup(dta);
cleanup:
dbus_error_free(&err);
return res;
}
/* ------------------------------------------------------------------------- *
* IPC with MCE
* ------------------------------------------------------------------------- */
/** Change availability of mce on system bus status
*
* @param running whether com.nokia.mce has an owner or not
*/
static void xmce_set_runstate(bool running)
{
if( mce_is_running != running ) {
mce_is_running = running;
dsme_log(LOG_NOTICE, PFIX"mce state -> %s",
running ? "running" : "terminated");
}
}
/** Call back for handling asynchronous client verification via GetNameOwner
*
* @param pending control structure for asynchronous d-bus methdod call
* @param user_data (unused)
*/
static
void
xmce_verify_name_cb(DBusPendingCall *pending, void *user_data)
{
(void)user_data;
gchar *owner = 0;
DBusMessage *rsp = 0;
if( !(rsp = dbus_pending_call_steal_reply(pending)) )
goto cleanup;
owner = xdbus_parse_name_owner_rsp(rsp);
xmce_set_runstate(owner && *owner);
cleanup:
g_free(owner);
if( rsp ) dbus_message_unref(rsp);
}
/** Verify that a mce exists via an asynchronous GetNameOwner method call
*
* @return true if the method call was initiated, or false in case of errors
*/
static
bool
xmce_verify_name(void)
{
bool res = false;
DBusMessage *req = 0;
DBusPendingCall *pc = 0;
const char *name = MCE_SERVICE;
if( !systembus )
goto cleanup;
if( !(req = xdbus_create_name_owner_req(name)) )
goto cleanup;
if( !dbus_connection_send_with_reply(systembus, req, &pc, -1) )
goto cleanup;
if( !dbus_pending_call_set_notify(pc, xmce_verify_name_cb, 0, 0) )
goto cleanup;
res = true;
cleanup:
if( pc ) dbus_pending_call_unref(pc);
if( req ) dbus_message_unref(req);
return res;
}
/** Signal cpu-keepalive plugin at mce side that rtc wakeup has occurred
*
* @return true if method call was sent, false in case of errors
*/
static bool xmce_cpu_keepalive_wakeup(void)
{
bool res = false;
DBusMessage *req = 0;
if( !systembus )
goto cleanup;
if( !mce_is_running )
goto cleanup;
req = dbus_message_new_method_call(MCE_SERVICE,
MCE_REQUEST_PATH,
MCE_REQUEST_IF,
MCE_CPU_KEEPALIVE_WAKEUP_REQ);
if( !req )
goto cleanup;
dbus_message_set_auto_start(req, false);
dbus_message_set_no_reply(req, true);
if( !dbus_connection_send(systembus, req, 0) ) {
dsme_log(LOG_WARNING, PFIX"failed to send %s.%s", MCE_REQUEST_IF,
MCE_CPU_KEEPALIVE_WAKEUP_REQ);
goto cleanup;
}
res = true;
cleanup:
if( req ) dbus_message_unref(req);
return res;
}
/** D-Bus message filter for handling mce NameOwnerChanged signals
*
* @param con dbus connection
* @param msg message to be acted upon
* @param user_data (not used)
*
* @return DBUS_HANDLER_RESULT_NOT_YET_HANDLED (other filters see the msg too)
*/
static
DBusHandlerResult
xmce_dbus_filter_cb(DBusConnection *con, DBusMessage *msg, void *user_data)
{
(void)user_data;
DBusHandlerResult res = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
const char *sender = 0;
const char *object = 0;
const char *name = 0;
const char *prev = 0;
const char *curr = 0;
DBusError err = DBUS_ERROR_INIT;
if( con != systembus )
goto cleanup;
if( !dbus_message_is_signal(msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged") )
goto cleanup;
sender = dbus_message_get_sender(msg);
if( strcmp(sender, DBUS_SERVICE_DBUS) )
goto cleanup;
object = dbus_message_get_path(msg);
if( strcmp(object, DBUS_PATH_DBUS) )
goto cleanup;
if( !dbus_message_get_args(msg, &err,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &prev,
DBUS_TYPE_STRING, &curr,
DBUS_TYPE_INVALID) ) {
dsme_log(LOG_WARNING, PFIX"%s: %s", err.name, err.message);
goto cleanup;
}
if( !strcmp(name, MCE_SERVICE) ) {
xmce_set_runstate(*curr != 0);
}
cleanup:
dbus_error_free(&err);
return res;
}
/** Signal matching rule for mce name ownership changes */
static const char xmce_name_owner_match[] =
"type='signal'"
",sender='"DBUS_SERVICE_DBUS"'"
",interface='"DBUS_INTERFACE_DBUS"'"
",member='NameOwnerChanged'"
",path='"DBUS_PATH_DBUS"'"
",arg0='"MCE_SERVICE"'"
;
/** Start tracking mce state on systembus
*/
static void xmce_handle_dbus_connect(void)
{
/* Register signal handling filter */
dbus_connection_add_filter(systembus, xmce_dbus_filter_cb, 0, 0);
/* NULL error -> match will be added asynchronously */
dbus_bus_add_match(systembus, xmce_name_owner_match, 0);
xmce_verify_name();
}
/** Stop tracking mce state on systembus
*/
static void xmce_handle_dbus_disconnect(void)
{
/* NULL error -> match will be removed asynchronously */
dbus_bus_remove_match(systembus, xmce_name_owner_match, 0);
/* Remove signal handling filter */
dbus_connection_remove_filter(systembus, xmce_dbus_filter_cb, 0);
}
/* ------------------------------------------------------------------------- *
* Android alarm management functionality
* ------------------------------------------------------------------------- */
/** Open android alarm device
*/
static void android_alarm_init(void)
{
if( (android_alarm_fd = open(android_alarm_path, O_RDWR)) == -1 ) {
/* Using /dev/alarm is optional; do not complain if it is missing */
if( errno != ENOENT )
dsme_log(LOG_WARNING, PFIX"%s: %m", android_alarm_path);
}
}
/** Close android alarm device
*/
static void android_alarm_quit(void)
{
if( android_alarm_fd != -1 )
close(android_alarm_fd), android_alarm_fd = -1;
}
static time_t android_alarm_prev = -1;
/** Add rtc wakeup via android alarm device
*
* @param t time stamp of the wakeup
*/
static void android_alarm_set(time_t delay)
{
struct timespec now = { .tv_sec = 0, .tv_nsec = 0 };
if( android_alarm_fd == -1 )
goto cleanup;
char tmp[32];
int get_time = ANDROID_ALARM_GET_TIME(ANDROID_ALARM_RTC);
if( ioctl(android_alarm_fd, get_time, &now) != 0 ) {
dsme_log(LOG_ERR, PFIX"%s: %m", "ANDROID_ALARM_TIME_GET");
goto cleanup;
}
struct timespec wup = now;
wup.tv_sec += delay;
int set_alarm = ANDROID_ALARM_SET(ANDROID_ALARM_RTC_WAKEUP);
if( ioctl(android_alarm_fd, set_alarm, &wup) != 0 ) {
dsme_log(LOG_ERR, PFIX"%s: %m", "ANDROID_ALARM_SET");
goto cleanup;
}
if( android_alarm_prev != wup.tv_sec ) {
android_alarm_prev = wup.tv_sec;
dsme_log(LOG_INFO, PFIX"android: %s", t_repr(now.tv_sec, tmp, sizeof tmp));
dsme_log(LOG_INFO, PFIX"alarm : %s", t_repr(wup.tv_sec, tmp, sizeof tmp));
}
cleanup:
return;
}
/** Remove rtc wakeup via android alarm device
*/
static void android_alarm_clear(void)
{
if( android_alarm_fd == -1 )
goto cleanup;
int cmd = ANDROID_ALARM_CLEAR(ANDROID_ALARM_RTC_WAKEUP);
if( ioctl(android_alarm_fd, cmd) != 0 ) {
dsme_log(LOG_ERR, PFIX"%s: %m", "ANDROID_ALARM_CLEAR");
goto cleanup;
}
if( android_alarm_prev != -1 ) {
android_alarm_prev = -1;
dsme_log(LOG_INFO, PFIX"android alarm wakeup removed");
}
cleanup:
return;
}
/* ------------------------------------------------------------------------- *
* RTC management functionality
* ------------------------------------------------------------------------- */
/** Human readable representation of struct tm
*/
static char *tm_repr(const struct tm *tm, char *buff, size_t size)
{
snprintf(buff, size, "%04d-%02d-%02d %02d:%02d:%02d %s",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday + 0,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
tm->tm_zone ?: "???");
return buff;
}
/** Human readable representation of time_t
*/
static char *t_repr(time_t t, char *buff, size_t size)
{
struct tm tm;
return tm_repr(gmtime_r(&t, &tm), buff, size);
}
/** Helper for logging rtc time values via dsme_log()
*
* @param lev syslog logging priority
* @param tod rtc time data
* @param msg string to add before timestamp
*/
static void rtc_log_time(int lev, const char *msg, const struct rtc_time *tod)
{
dsme_log(lev, "%s%04d-%02d-%02d %02d:%02d:%02d",
msg ?: "", tod->tm_year + 1900, tod->tm_mon + 1, tod->tm_mday,
tod->tm_hour, tod->tm_min, tod->tm_sec);
}
/** Convert struct tm to struct rtc_time
*
* @param tod where to store rtc time
* @param tm broken down time
*
* @return tm as seconds from start of epoch, or -1 if tm is invalid
*/
static time_t rtc_time_from_tm(struct rtc_time *tod, const struct tm *tm)
{
/* normalize broken down time */
struct tm tmp = *tm;
time_t res = timegm(&tmp);
/* copy values */
memset(tod, 0, sizeof *tod);
tod->tm_sec = tmp.tm_sec;
tod->tm_min = tmp.tm_min;
tod->tm_hour = tmp.tm_hour;
tod->tm_mday = tmp.tm_mday;
tod->tm_mon = tmp.tm_mon;
tod->tm_year = tmp.tm_year;
return res;
}
/** Convert struct rtc_time to struct tm
*
* @param tod rtc time
* @param tm where to store broken down time
*
* @return tod as seconds from start of epoch, or -1 if tod is invalid
*/
static time_t rtc_time_to_tm(const struct rtc_time *tod, struct tm *tm)
{
/* copy values */
memset(tm, 0, sizeof *tm);
tm->tm_sec = tod->tm_sec;
tm->tm_min = tod->tm_min;
tm->tm_hour = tod->tm_hour;
tm->tm_mday = tod->tm_mday;
tm->tm_mon = tod->tm_mon;
tm->tm_year = tod->tm_year;
/* normalize broken down time */
time_t res = timegm(tm);
return res;
}
/** Get rtc time as struct rtc_time
*
* @param tod where to store broken down rtc time
*
* @return true on success, or false in case of errors
*/
static bool rtc_get_time_raw(struct rtc_time *tod)
{
bool result = false;
if( rtc_fd == -1 )
goto cleanup;
memset(tod, 0, sizeof *tod);
if( ioctl(rtc_fd, RTC_RD_TIME, tod) == -1 ) {
dsme_log(LOG_ERR, PFIX"%s: %s: %m", rtc_path, "RTC_RD_TIME");
goto cleanup;
}
rtc_log_time(LOG_DEBUG, PFIX"rtc time is: ", tod);
result = true;
cleanup:
return result;
}
/** Get rtc time as struct tm
*
* @param tm where to store broken down time, or NULL
*
* @return seconds since epoch, or (time_t)-1 in case of errors
*/
static time_t rtc_get_time_tm(struct tm *tm)
{
time_t result = -1;
struct rtc_time tod;
if( !rtc_get_time_raw(&tod) )
goto cleanup;
result = rtc_time_to_tm(&tod, tm);
cleanup:
return result;
}
/** Set rtc time from struct rtc_time
*
* @param tod broken down rtc time
*
* @return true on success, or false in case of errors
*/
static bool rtc_set_time_raw(struct rtc_time *tod)
{
bool result = false;
if( rtc_fd == -1 )
goto cleanup;
if( ioctl(rtc_fd, RTC_SET_TIME, tod) == -1 ) {
dsme_log(LOG_ERR, PFIX"%s: %s: %m", rtc_path, "RTC_SET_TIME");
goto cleanup;
}
rtc_log_time(LOG_INFO, PFIX"set rtc time to: ", tod);
result = true;
cleanup:
return result;
}
/** Set rtc time from struct tm
*
* @param tm broken down time
*
* @return true on success, or false in case of errors
*/
static bool rtc_set_time_tm(struct tm *tm)
{
bool result = false;
struct rtc_time tod;
memset(&tod, 0, sizeof tod);
if( rtc_time_from_tm(&tod, tm) < 0 )
goto cleanup;
if( !rtc_set_time_raw(&tod) )
goto cleanup;
result = true;
cleanup:
return result;
}
/** Set rtc time from time_t
*
* @param t seconds since epoch
*
* @return true on success, or false in case of errors
*/
static bool rtc_set_time_t(time_t t)
{
bool result = false;
struct tm tm;
memset(&tm, 0, sizeof tm);
if( gmtime_r(&t, &tm) == 0 )
goto cleanup;
if( !rtc_set_time_tm(&tm) )
goto cleanup;
result = true;
cleanup:
return result;
}
static bool rtc_set_time_tv(const struct timeval *rtc)
{
bool result = false;
if( !rtc_set_time_t(rtc->tv_sec) )
goto cleanup;
result = true;
cleanup:
return result;
}
static bool rtc_get_time_tv(struct timeval *rtc)
{
bool result = false;