-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm-manager.c
executable file
·2717 lines (2233 loc) · 70.6 KB
/
alarm-manager.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
/*
* alarm-manager
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
* Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#define _BSD_SOURCE /*for localtime_r*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<signal.h>
#include<string.h>
#include<sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include<dbus/dbus.h>
#include<dbus/dbus-glib-lowlevel.h>
#include<glib.h>
#if !GLIB_CHECK_VERSION (2, 31, 0)
#include <glib/gmacros.h>
#else
#endif
/*#include "global-gconf.h"*/
#include"alarm.h"
#include"alarm-internal.h"
/*#include"limo-event-delivery.h"*/
#include <aul.h>
#include <bundle.h>
#include <heynoti.h>
#include <security-server.h>
#include <db-util.h>
#include <vconf.h>
#include <vconf-keys.h>
#define SIG_TIMER 0x32
#define WAKEUP_ALARM_APP_ID "org.tizen.alarm.ALARM"
/* alarm ui application's alarm's dbus_service name instead of 21
(alarm application's app_id) value */
#define WAKEUP_ALARMBOOTING_APP_ID "org.tizen.alarmboot.ui.ALARM"
/*alrmbooting ui application's dbus_service name instead of 121(alarmbooting
application's app_id) value */
/*
#include "tapi_misc_ext.h"
#include "TelMisc.h"
#include "tapi_misc_data.h"
#include "tapi_power.h"
*/
#include "pmapi.h"
__alarm_server_context_t alarm_context;
bool g_dummy_timer_is_set = FALSE;
GSList *g_scheduled_alarm_list = NULL;
GSList *g_expired_alarm_list = NULL;
#ifndef RTC_WKALM_BOOT_SET
#define RTC_WKALM_BOOT_SET _IOW('p', 0x80, struct rtc_wkalrm)
#endif
#ifdef __ALARM_BOOT
bool enable_power_on_alarm;
bool alarm_boot;
time_t ab_due_time = -1;
bool poweron_alarm_expired = false;
#endif
/* 2008. 6. 3 sewook7.park
When the alarm becoms sleeping mode, alarm timer is not expired.
So using RTC, phone is awaken before alarm rings.
*/
#define __WAKEUP_USING_RTC__
#ifdef __WAKEUP_USING_RTC__
#include<errno.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
static const char default_rtc[] = "/dev/rtc1";
static const char power_rtc[] = "/dev/rtc0";
#endif /*__WAKEUP_USING_RTC__*/
static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
alarm_id_t *alarm_id);
static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
__alarm_info_t *__alarm_info,
int *error_code);
static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
int *error_code);
static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
__alarm_info_t *__alarm_info);
static bool __alarm_update_due_time_of_all_items_in_list(double diff_time);
static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
int pid, char *app_service_name, char *app_service_name_mod,
const char *dst_service_name, const char *dst_service_name_mod, int *error_code);
static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
int pid, char *bundle_data, int *error_code);
static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code);
static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
alarm_info_t *alarm_info, int *error_code);
static bool __alarm_power_on(int app_id, bool on_off, int *error_code);
static bool __alarm_power_off(int app_id, int *error_code);
static bool __alarm_check_next_duetime(int app_id, int *error_code);
static void __alarm_send_noti_to_application(const char *app_service_name,
alarm_id_t alarm_id);
static void __alarm_expired();
static gboolean __alarm_handler_idle();
static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext);
static void __clean_registry();
static bool __alarm_manager_reset();
static void __on_system_time_changed(keynode_t *node, void *data);
static void __initialize_timer();
static void __initialize_alarm_list();
static void __initialize_scheduled_alarm_lsit();
static void __hibernation_leave_callback();
static bool __initialize_noti();
static bool __initialize_dbus();
static bool __initialize_db();
static void __initialize();
static bool __check_false_alarm();
static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
DBusMessage *message,
void *user_data);
static void __rtc_set()
{
#ifdef __WAKEUP_USING_RTC__
const char *rtc = default_rtc;
int fd = 0;
struct rtc_time rtc_tm;
struct rtc_wkalrm rtc_wk;
struct tm due_tm;
#ifdef _SIMUL /*if build is simulator, we don't need to set
RTC because RTC does not work in simulator.*/
ALARM_MGR_EXCEPTION_PRINT("because it is simulator's mode, "
"we don't set RTC.\n");
return;
#endif
fd = open(rtc, O_RDONLY);
if (fd == -1) {
ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
return;
}
/* Read the RTC time/date */
int retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
if (retval == -1) {
ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
close(fd);
return;
}
ALARM_MGR_LOG_PRINT("\n\nCurrent RTC date/time is %d-%d-%d, "
"%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min,
rtc_tm.tm_sec);
ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
alarm_context.c_due_time);
if (alarm_context.c_due_time != -1) {
time_t due_time = alarm_context.c_due_time - 1;
gmtime_r(&due_time, &due_tm);
rtc_tm.tm_mday = due_tm.tm_mday;
rtc_tm.tm_mon = due_tm.tm_mon;
rtc_tm.tm_year = due_tm.tm_year;
rtc_tm.tm_hour = due_tm.tm_hour;
rtc_tm.tm_min = due_tm.tm_min;
rtc_tm.tm_sec = due_tm.tm_sec;
memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
rtc_wk.enabled = 1;
rtc_wk.pending = 0;
ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
"%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday,
rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
if (retval == -1) {
if (errno == ENOTTY) {
ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
"supported.\n");
}
ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
close(fd);
return;
}
ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
/* Enable alarm interrupts */
retval = ioctl(fd, RTC_AIE_ON, 0);
if (retval == -1) {
ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
close(fd);
return;
}
ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
} else
ALARM_MGR_LOG_PRINT("[alarm-server]alarm_context.c_due_time is"
"less than 10 sec. RTC alarm does not need to be set\n");
close(fd);
#endif /* __WAKEUP_USING_RTC__ */
}
int _set_rtc_time(time_t _time)
{
int fd0 = 0;
int fd1 = 0;
int retval0 = 0;
int retval1 = 0;
struct rtc_time rtc_tm;
const char *rtc0 = power_rtc;
const char *rtc1 = default_rtc;
struct tm *_tm;
struct tm time_r;
fd0 = open(rtc0, O_RDONLY);
fd1 = open(rtc1, O_RDONLY);
if (fd0 == -1) {
ALARM_MGR_LOG_PRINT("error to open /dev/rtc0.");
perror("\t");
}
if (fd1 == -1) {
ALARM_MGR_LOG_PRINT("error to open /dev/rtc1.");
perror("\t");
}
memset(&rtc_tm, 0, sizeof(struct rtc_time));
_tm = gmtime_r(&_time, &time_r);
/* Write the RTC time/date 2008:05:21 19:20:00 */
rtc_tm.tm_mday = time_r.tm_mday;
rtc_tm.tm_mon = time_r.tm_mon;
rtc_tm.tm_year = time_r.tm_year;
rtc_tm.tm_hour = time_r.tm_hour;
rtc_tm.tm_min = time_r.tm_min;
rtc_tm.tm_sec = 0;
retval0 = ioctl(fd0, RTC_SET_TIME, &rtc_tm);
if (retval0 == -1) {
close(fd0);
ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
perror("\t");
}
close(fd0);
retval1 = ioctl(fd1, RTC_SET_TIME, &rtc_tm);
if (retval1 == -1) {
close(fd1);
ALARM_MGR_LOG_PRINT("error to ioctl fd1.");
perror("\t");
}
close(fd1);
return 1;
}
bool __alarm_clean_list()
{
GSList *iter = NULL;
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
free(iter->data);
}
g_slist_free(alarm_context.alarms);
return true;
}
static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
alarm_id_t *alarm_id)
{
bool unique_id = false;
alarm_info_t *alarm_info = &__alarm_info->alarm_info;
__alarm_info_t *entry = NULL;
GSList *iter = NULL;
/* FIXME: alarm id must be unique. */
__alarm_info->alarm_id = (int)(void *)__alarm_info;
ALARM_MGR_LOG_PRINT("__alarm_info->alarm_id is %d", \
__alarm_info->alarm_id);
while (unique_id == false) {
unique_id = true;
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
if (entry->alarm_id == __alarm_info->alarm_id) {
__alarm_info->alarm_id++;
unique_id = false;
}
}
}
/* list alarms */
ALARM_MGR_LOG_PRINT("[alarm-server]: before add\n");
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
/*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
entry->alarm_id); */
}
alarm_context.alarms =
g_slist_append(alarm_context.alarms, __alarm_info);
/*list alarms */
ALARM_MGR_LOG_PRINT("[alarm-server]: after add\n");
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
entry->alarm_id);
}
if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
_save_alarms(__alarm_info);
}
*alarm_id = __alarm_info->alarm_id;
return true;
}
static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
__alarm_info_t *__alarm_info,
int *error_code)
{
bool found = false;
alarm_info_t *alarm_info = &__alarm_info->alarm_info;
GSList *iter = NULL;
__alarm_info_t *entry = NULL;
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
if (entry->alarm_id == alarm_id) {
found = true;
__alarm_info->quark_app_unique_name =
entry->quark_app_unique_name;
__alarm_info->quark_dst_service_name =
entry->quark_dst_service_name;
memcpy(entry, __alarm_info, sizeof(__alarm_info_t));
break;
}
}
if (!found) {
if (error_code)
*error_code = ERR_ALARM_INVALID_ID;
return false;
}
if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
_update_alarms(__alarm_info);
}
return true;
}
static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
int *error_code)
{
bool found = false;
alarm_info_t *alarm_info = NULL;
GSList *iter = NULL;
__alarm_info_t *entry = NULL;
/*list alarms */
ALARM_MGR_LOG_PRINT("[alarm-server]: before del : alarm id(%d)\n",
alarm_id);
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
if (entry->alarm_id == alarm_id) {
alarm_info = &entry->alarm_info;
ALARM_MGR_LOG_PRINT("[alarm-server]: "
"__alarm_remove_from_list : alarm id(%d)\n",
entry->alarm_id);
if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
_delete_alarms(alarm_id);
}
alarm_context.alarms =
g_slist_remove(alarm_context.alarms, iter->data);
found = true;
break;
}
}
ALARM_MGR_LOG_PRINT("[alarm-server]: after del\n");
if (!found) {
if (error_code)
*error_code = ERR_ALARM_INVALID_ID;
return false;
}
return true;
}
static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
__alarm_info_t *__alarm_info)
{
alarm_date_t *start = &alarm_info->start;
alarm_date_t *end = &alarm_info->end;
struct tm alarm_tm = { 0, };
if (start->year != 0) {
alarm_tm.tm_year = start->year - 1900;
alarm_tm.tm_mon = start->month - 1;
alarm_tm.tm_mday = start->day;
alarm_tm.tm_hour = 0;
alarm_tm.tm_min = 0;
alarm_tm.tm_sec = 0;
__alarm_info->start = mktime(&alarm_tm);
} else {
__alarm_info->start = 0;
}
if (end->year != 0) {
alarm_tm.tm_year = end->year - 1900;
alarm_tm.tm_mon = end->month - 1;
alarm_tm.tm_mday = end->day;
alarm_tm.tm_hour = 23;
alarm_tm.tm_min = 59;
alarm_tm.tm_sec = 59;
__alarm_info->end = mktime(&alarm_tm);
} else {
__alarm_info->end = 0;
}
return true;
}
/*
static bool alarm_get_tz_info(int *gmt_idx, int *dst)
{
GConfValue *value1 = NULL;
GConfValue *value2 = NULL;
GConfClient* gConfClient = NULL;
GError* err = NULL;
gConfClient = gconf_client_get_default();
if(gConfClient) {
value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE,
&err);
if (err) {
ALARM_MGR_LOG_PRINT("__on_system_time_changed:
gconf_client_get() failed:
error:[%s]\n", err->message);
g_error_free(err);
err = NULL;
}
*gmt_idx = gconf_value_get_int(value1);
ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
value2 = gconf_client_get(gConfClient,
SETTINGS_DAYLIGHTSTATUS, &err);
if (err) {
ALARM_MGR_LOG_PRINT("__on_system_time_changed:
gconf_client_get() failed: error:[%s]\n", err->message);
g_error_free(err);
err = NULL;
}
*dst = gconf_value_get_int(value2);
ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
if(gConfClient != NULL) {
g_object_unref(gConfClient);
gConfClient = NULL;
}
}
else
ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
if(value1) {
gconf_value_free(value1);
value1 = NULL;
}
if(value2) {
gconf_value_free(value2);
value2 = NULL;
}
return true;
}
*/
static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
{
time_t current_time;
time_t min_time = -1;
time_t due_time = 0;
GSList *iter = NULL;
__alarm_info_t *entry = NULL;
struct tm *p_time = NULL ;
struct tm due_time_result ;
struct tm fixed_time ;
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
alarm_info_t *alarm_info = &(entry->alarm_info);
if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE) {
/*diff_time ó¸® */
entry->due_time += diff_time;
alarm_date_t *start = &alarm_info->start; /**< start
time of the alarm */
alarm_date_t *end = &alarm_info->end;;
/**< end time of the alarm */
tzset();
p_time = localtime_r(&entry->due_time, &due_time_result);
if (p_time != NULL) {
start->year = p_time->tm_year + 1900;
start->month = p_time->tm_mon + 1;
start->day = p_time->tm_mday;
start->hour = p_time->tm_hour;
start->min = p_time->tm_min;
start->sec = p_time->tm_sec;
end->year = p_time->tm_year + 1900;
end->month = p_time->tm_mon + 1;
end->day = p_time->tm_mday;
memset(&fixed_time, 0, sizeof(fixed_time));
fixed_time.tm_year = p_time->tm_year;
fixed_time.tm_mon = p_time->tm_mon;
fixed_time.tm_mday = p_time->tm_mday;
fixed_time.tm_hour = 0;
fixed_time.tm_min = 0;
fixed_time.tm_sec = 0;
}
entry->start = mktime(&fixed_time);
fixed_time.tm_hour = 23;
fixed_time.tm_min = 59;
fixed_time.tm_sec = 59;
entry->end = mktime(&fixed_time);
ALARM_MGR_LOG_PRINT("alarm_info->alarm_type is "
"ALARM_TYPE_RELATIVE\n");
_update_alarms(entry);
}
_alarm_next_duetime(entry);
ALARM_MGR_LOG_PRINT("entry->due_time is %d\n", entry->due_time);
}
time(¤t_time);
for (iter = alarm_context.alarms; iter != NULL;
iter = g_slist_next(iter)) {
entry = iter->data;
due_time = entry->due_time;
double interval = 0;
ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
"current(%u)\n", entry->alarm_id, due_time, current_time);
if (due_time == 0) { /* 0 means this alarm has been
disabled */
continue;
}
interval = difftime(due_time, current_time);
if (interval <= 0) {
ALARM_MGR_LOG_PRINT("this may be error.. alarm[%d]\n", \
entry->alarm_id);
continue;
}
interval = difftime(due_time, min_time);
if ((interval < 0) || min_time == -1) {
min_time = due_time;
}
}
alarm_context.c_due_time = min_time;
return true;
}
static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
int pid,char *bundle_data, int *error_code){
time_t current_time;
time_t due_time;
struct tm ts_ret;
char due_time_r[100] = { 0 };
char proc_file[512] = { 0 };
char process_name[512] = { 0 };
char app_name[512] = { 0 };
char *word = NULL;
char *proc_name_ptr = NULL;
int fd = 0;
int ret = 0;
int i = 0;
__alarm_info_t *__alarm_info = NULL;
__alarm_info = malloc(sizeof(__alarm_info_t));
if (__alarm_info == NULL) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
"failed. it seems to be OOM\n", pid);
*error_code = -1; /* -1 means that system
failed internally. */
return false;
}
__alarm_info->pid = pid;
__alarm_info->alarm_id = -1;
/* we should consider to check whether pid is running or Not
*/
memset(process_name, '\0', 512);
memset(proc_file, '\0', 512);
snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
fd = open(proc_file, O_RDONLY);
if (fd > 0) {
ret = read(fd, process_name, 512);
close(fd);
if (ret <=0)
{
ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
*error_code = -1;
free(__alarm_info);
return false;
}
while (process_name[i] != '\0') {
if (process_name[i] == ' ') {
process_name[i] = '\0';
break;
}
i++;
}
word = strtok_r(process_name, "/", &proc_name_ptr);
while (word != NULL) {
memset(app_name, 0, 512);
snprintf(app_name, 512, "%s", word);
word = strtok_r(NULL, "/", &proc_name_ptr);
}
__alarm_info->quark_app_unique_name =
g_quark_from_string(app_name);
} else { /* failure */
ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
"killed, so we failed to get proc file(%s) and do not create "
"alarm_info\n", pid, proc_file);
*error_code = -1; /*-1 means that system failed
internally.*/
free(__alarm_info);
return false;
}
__alarm_info->quark_bundle=g_quark_from_string(bundle_data);
__alarm_info->quark_app_service_name = g_quark_from_string("null");
__alarm_info->quark_dst_service_name = g_quark_from_string("null");
__alarm_info->quark_app_service_name_mod = g_quark_from_string("null");
__alarm_info->quark_dst_service_name_mod = g_quark_from_string("null");
__alarm_set_start_and_end_time(alarm_info, __alarm_info);
memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
time(¤t_time);
if (alarm_context.c_due_time < current_time) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
"(%d) is less than current time(%d)", alarm_context.c_due_time,
current_time);
alarm_context.c_due_time = -1;
}
due_time = _alarm_next_duetime(__alarm_info);
if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
free(__alarm_info);
*error_code = -1;
return false;
}
if (due_time == 0) {
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
"due_time is 0, alarm(%d) \n", *alarm_id);
return true;
} else if (current_time == due_time) {
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
"current_time(%d) is same as due_time(%d)", current_time,
due_time);
return true;
}else if (difftime(due_time, current_time) < 0){
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
return true;
}else {
localtime_r(&due_time, &ts_ret);
strftime(due_time_r, 30, "%c", &ts_ret);
ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
"alarm(%d) due_time(%s)", *alarm_id,
due_time_r);
}
ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
"due_time(%d)", alarm_context.c_due_time, due_time);
if (alarm_context.c_due_time == -1
|| due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
_alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
*alarm_id);
alarm_context.c_due_time = due_time;
} else if (due_time == alarm_context.c_due_time) {
_add_to_scheduled_alarm_list(__alarm_info);
}
__rtc_set();
#ifdef __ALARM_BOOT
/*alarm boot */
if (enable_power_on_alarm) {
__alarm_power_on(0, enable_power_on_alarm, NULL);
}
#endif
return true;
}
static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
int pid, char *app_service_name, char *app_service_name_mod,
const char *dst_service_name,const char *dst_service_name_mod, int *error_code)
{
time_t current_time;
time_t due_time;
char proc_file[256] = { 0 };
char process_name[512] = { 0 };
char app_name[256] = { 0 };
char *word = NULL;
char *proc_name_ptr = NULL;
__alarm_info_t *__alarm_info = NULL;
__alarm_info = malloc(sizeof(__alarm_info_t));
if (__alarm_info == NULL) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
"failed. it seems to be OOM\n", pid);
*error_code = -1; /* -1 means that system
failed internally. */
return false;
}
__alarm_info->pid = pid;
__alarm_info->alarm_id = -1;
/* we should consider to check whether pid is running or Not
*/
memset(process_name, '\0', 512);
memset(proc_file, '\0', 256);
snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
int fd;
int ret;
int i = 0;
fd = open(proc_file, O_RDONLY);
if (fd > 0) {
ret = read(fd, process_name, 512);
close(fd);
while (process_name[i] != '\0') {
if (process_name[i] == ' ') {
process_name[i] = '\0';
break;
}
i++;
}
/* if (readlink(proc_file, process_name, 256)!=-1) */
/*success */
word = strtok_r(process_name, "/", &proc_name_ptr);
while (word != NULL) {
memset(app_name, 0, 256);
snprintf(app_name, 256, "%s", word);
word = strtok_r(NULL, "/", &proc_name_ptr);
}
__alarm_info->quark_app_unique_name =
g_quark_from_string(app_name);
} else { /* failure */
__alarm_info->quark_app_unique_name =
g_quark_from_string("unknown");
ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
"killed, so we failed to get proc file(%s) and do not create "
"alarm_info\n", pid, proc_file);
*error_code = -1; /*-1 means that system failed
internally.*/
free(__alarm_info);
return false;
}
__alarm_info->quark_app_service_name =
g_quark_from_string(app_service_name);
__alarm_info->quark_app_service_name_mod =
g_quark_from_string(app_service_name_mod);
__alarm_info->quark_dst_service_name =
g_quark_from_string(dst_service_name);
__alarm_info->quark_dst_service_name_mod =
g_quark_from_string(dst_service_name_mod);
__alarm_info->quark_bundle = g_quark_from_string("null");
__alarm_set_start_and_end_time(alarm_info, __alarm_info);
memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
time(¤t_time);
ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
"app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
g_quark_to_string(__alarm_info->quark_app_service_name), \
g_quark_to_string(__alarm_info->quark_dst_service_name), \
alarm_context.c_due_time);
if (alarm_context.c_due_time < current_time) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
"(%d) is less than current time(%d)", alarm_context.c_due_time,
current_time);
alarm_context.c_due_time = -1;
}
due_time = _alarm_next_duetime(__alarm_info);
if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
free(__alarm_info);
return false;
}
if (due_time == 0) {
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
"due_time is 0, alarm(%d) \n", *alarm_id);
return true;
} else if (current_time == due_time) {
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
"current_time(%d) is same as due_time(%d)", current_time,
due_time);
return true;
}else if (difftime(due_time, current_time) < 0){
ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
return true;
} else {
char due_time_r[100] = { 0 };
struct tm ts_ret;
localtime_r(&due_time, &ts_ret);
strftime(due_time_r, 30, "%c", &ts_ret);
ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
"alarm(%d) due_time(%s)", *alarm_id,
due_time_r);
}
ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
"due_time(%d)", alarm_context.c_due_time, due_time);
if (alarm_context.c_due_time == -1
|| due_time < alarm_context.c_due_time) {
_clear_scheduled_alarm_list();
_add_to_scheduled_alarm_list(__alarm_info);
_alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
*alarm_id);
alarm_context.c_due_time = due_time;
} else if (due_time == alarm_context.c_due_time) {
_add_to_scheduled_alarm_list(__alarm_info);
}
__rtc_set();
#ifdef __ALARM_BOOT
/*alarm boot */
if (enable_power_on_alarm) {
/* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
* platform with app-server.because __alarm_power_on(..) fuction don't
* use first parameter internally, we set this value to 0(zero)
*/
__alarm_power_on(0, enable_power_on_alarm, NULL);
}
#endif
return true;
}
static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
alarm_info_t *alarm_info, int *error_code)
{
time_t current_time;
time_t due_time;
__alarm_info_t *__alarm_info = NULL;
bool result = false;
__alarm_info = malloc(sizeof(__alarm_info_t));
if (__alarm_info == NULL) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
"malloc failed. it seems to be OOM\n", pid);
*error_code = -1; /*-1 means that system failed
internally.*/
return false;
}
__alarm_info->pid = pid;
__alarm_info->alarm_id = alarm_id;
/* we should consider to check whether pid is running or Not
*/
__alarm_info->quark_app_service_name =
g_quark_from_string(app_service_name);
__alarm_set_start_and_end_time(alarm_info, __alarm_info);
memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
time(¤t_time);
if (alarm_context.c_due_time < current_time) {
ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
"(%d) is less than current time(%d)", alarm_context.c_due_time,
current_time);
alarm_context.c_due_time = -1;
}
due_time = _alarm_next_duetime(__alarm_info);
if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
free(__alarm_info);