blob: cae1f7ebe1da49f3fcbe6526c8fe914431d11906 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 * @file rtc-service.c
3 * @brief Implementation of Sanechips
4 *
5 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
6 * @author linxu Gebin
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14/*******************************************************************************
15 * Include header files *
16 ******************************************************************************/
17#include "rtc-service.h"
18#include "hotplug.h"
19#include <limits.h>
20
21/*******************************************************************************
22 * Global variable definitions *
23 ******************************************************************************/
24int g_rtc_fd = 0;
25struct list_head rtc_timer_list;
26struct list_head rtc_alarm_list;
27#define RTC_ALARM_TIMEOUT_MSG "PMIC RTC ALARM IRQ COME"
28#define RTC_TIMER_TIMEOUT_MSG "PMIC RTC TIMER IRQ COME"
29
30#ifdef PRODUCT_NOT_USE_RTC
31time_t g_set_softtime;
32#define RTC_USE_SOFTTIMER_ID 300 //µ±Ã»ÓÐrtcʱ£¬Ô­ÏÈʹÓÃrtc¶¨Ê±Æ÷µÄÓÐrtcͳһÊÊÅäµ½softtimerÉÏ£¬Õû¸ö¿ò¼Ü²»±ä
33#endif
34/*******************************************************************************
35 * Local variable definitions *
36 ******************************************************************************/
37static pthread_t rtc_looper_id = 0;
38static int g_clean_flag = 0;
39static int g_time_offset = 2; //ÔÊÐíÎó²î·¶Î§£¬ÊÊÅärtcºÍOSʱÖÓÆµÂʲ»Ò»ÖÂÎÊÌâ
40/*******************************************************************************
41 * Local function implementations *
42 ******************************************************************************/
43static int open_rtc(void);
44static void close_rtc(int fd);
45static void* rtc_looper();
46static int start_rtc_thread();
47static int handle_get_time(MSG_BUF *pmsg_buf);
48static int handle_set_time(MSG_BUF *pmsg_buf);
49static int handle_alarm_add(MSG_BUF *pmsg_buf);
50static int handle_alarm_del(MSG_BUF *pmsg_buf);
51static int handle_alarm_timeout(MSG_BUF *pmsg_buf);
52static int process_msg(MSG_BUF *pmsg_buf);
53static void msg_looper(void);
54static void update_timer_remain(int new_remian);
55static void alarm_fix_value(struct list_head *rtc_list);
56static void alarm_restart(struct list_head *rtc_list);
57static void update_alarm();
58static void alarm_adjust_value(int time);
59static long get_RtcTime();
60static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type);
61static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type)
62{
63 time_t tmpTime = 0;
64 if(tmTime == NULL) {
65 slog(RTC_PRINT, SLOG_ERR, "rtc-service: wrong input param, check it! \n");
66 return ;
67 }
68 tmpTime = timeSec;
69
70 if(type == CONVERT_TYPE_RTC) {
71 gmtime_r((time_t*)&tmpTime, tmTime);
72 }
73 else {
74 localtime_r((time_t*)&tmpTime, tmTime);
75 }
76
77 slog(RTC_PRINT, SLOG_DEBUG, "Time:%ld, tm_year:%d, tm_mon:%d, tm_hour:%d, tm_min:%d, tm_sec:%d, tm_yday:%d, tm_mday:%d, tm_wday:%d \n",
78 mktime(tmTime), tmTime->tm_year, tmTime->tm_mon, tmTime->tm_hour, tmTime->tm_min, tmTime->tm_sec, tmTime->tm_yday, tmTime->tm_mday, tmTime->tm_wday);
79
80}
81
82static long get_RtcTime()
83{
84 long curRtcTime = 0;
85 struct rtc_time rtc_tm = {0};
86 struct tm tmp = {0};
87 if(g_rtc_fd <= 0) {
88 slog(RTC_PRINT, SLOG_ERR, "rtc-service: g_rtc_fd: %d , invalid fd. \n", g_rtc_fd);
89 return -1;
90 }
91
92 if(ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
93 {
94 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
95 }
96 tmp.tm_sec = rtc_tm.tm_sec;
97 tmp.tm_min= rtc_tm.tm_min;
98 tmp.tm_hour= rtc_tm.tm_hour;
99 tmp.tm_mon= rtc_tm.tm_mon;
100 tmp.tm_year= rtc_tm.tm_year;
101 tmp.tm_mday= rtc_tm.tm_mday;
102 tmp.tm_wday= rtc_tm.tm_wday;
103 tmp.tm_yday= rtc_tm.tm_yday;
104 tmp.tm_isdst= rtc_tm.tm_isdst;
105
106 curRtcTime = mktime(&tmp);
107 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: curRtcTime: %ld . \n", curRtcTime);
108 return curRtcTime;
109}
110static void write_lockfile(char *filepath, char *setbuf)
111{
112 int f, len = 0;
113
114 f = open(filepath, O_RDWR);
115 if (f == -1) {
116 return;
117 }
118
119 len = strlen(setbuf);
120 if (write(f, setbuf, len) != len) {
121 } else {
122 }
123 close(f);
124}
125
126
127static void wake_rtc_lock()
128{
129 write_lockfile("/sys/power/wake_lock", "rtc_lock");
130}
131
132static void wake_rtc_unlock()
133{
134 write_lockfile("/sys/power/wake_unlock", "rtc_lock");
135}
136
137static void wake_rtc_io_lock()
138{
139 write_lockfile("/sys/power/wake_lock", "rtc_io_lock");
140}
141
142static void wake_rtc_io_unlock()
143{
144 write_lockfile("/sys/power/wake_unlock", "rtc_io_lock");
145}
146
147static void wake_timeout_lock(char *lockname, unsigned long interval)
148{
149 char cmd[100] = {0};
150 sprintf(cmd, "echo %s %lu > /sys/power/wake_lock", lockname, interval);
151 soft_system(cmd);
152}
153
154static int init_rtc_alarm(void)
155{
156 FILE* fp = NULL;
157 int gap_start = 0;
158 int gap_end = 0;
159 size_t size = 0;
160 long cur_rtc_time = 0;
161 ALARM_T* iter = NULL;
162 RTC_DATA_BUF buf = {0};
163 int ret = -1;
164
165// cur_rtc_time = get_RtcTime();
166 cur_rtc_time = time(0);
167 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "rb")) == NULL)
168 {
169 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
170 return -1;
171 }
172
173 while(1)
174 {
175 size = fread(&gap_start, sizeof(int), 1, fp);//0xa5a5
176 if(0 == size || gap_start != DATA_S ||feof(fp) != 0 )
177 break;
178
179 iter = (ALARM_T*)malloc(sizeof(ALARM_T));
180 if(iter)
181 memset(iter,0,sizeof(ALARM_T));
182 else
183 {
184 fclose(fp);
185 return -1;
186 }
187
188 size = fread(iter, sizeof(ALARM_T), 1, fp);//struct
189 if(0 ==size||feof(fp) != 0)//check the end of the file
190 {
191 memset(iter, 0, sizeof(ALARM_T));//kw
192 free(iter);
193 break;
194 }
195
196 size = fread(&gap_end, sizeof(int), 1, fp);//0x5a5a
197 if(0 == size || gap_end != DATA_E ||feof(fp) != 0)
198 {
199 memset(iter, 0, sizeof(ALARM_T));//kw
200 free(iter);
201 break;
202 }
203
204 if(iter->interval < 0 || iter->interval > LONG_MAX-1)
205 {
206 iter->interval = 0 ;
207 }
208
209
210 if(iter->time_val < 0 || iter->time_val > LONG_MAX-1)
211 {
212 iter->time_val = 0 ;
213 }
214
215
216 if(cur_rtc_time > iter->interval + iter->time_val)
217 {
218 buf.module = iter->rtc_id;
219 buf.time_val = iter->time_val + iter->interval;
220 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d, cur_rtc_time:%ld \n", iter->module_id, buf.module, iter->is_utc, cur_rtc_time);
221 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, iter->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
222 if (ret != 0) {
223 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected\n");
224 }
225 }
226 list_add_tail(&iter->list, &rtc_alarm_list);
227 if(feof(fp) != 0)//check the end of the file
228 break;
229 }
230 fclose(fp);
231
232 return 0;
233}
234
235static int save_rtc_alarm(void)
236{
237 FILE* fp = NULL;
238 int gap_start = DATA_S;
239 int gap_end = DATA_E;
240 ALARM_T *temp = NULL;
241
242 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "wb")) == NULL)
243 {
244 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
245 return -1;
246 }
247
248 if (!list_empty(&rtc_alarm_list)) {
249 list_for_each_entry(temp, &rtc_alarm_list, list) {
250 if(temp->wakeup)
251 {
252 fwrite(&gap_start, sizeof(int), 1, fp);
253 fwrite(temp, sizeof(ALARM_T), 1, fp);
254 fwrite(&gap_end, sizeof(int), 1, fp);
255 }
256 }
257 }
258
259 fflush(fp);
260 if(fsync(fileno(fp)) < 0)
261 {
262 // todo: cov M
263 }
264 fclose(fp);
265
266 return 0;
267}
268
269/********************************************
270 ¹¦ÄÜ:´ò¿ªrtc
271 *******************************************/
272static int open_rtc(void)
273{
274 int fd = 0;
275 fd = open(RTC_DEV_NAME, O_RDWR);
276
277 if (fd < 0) {
278 slog(RTC_PRINT, SLOG_ERR, "rtc-service: Open %s error!\n", RTC_DEV_NAME);
279 return fd;
280 }
281 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: Open %s OK!\n", RTC_DEV_NAME);
282 return fd;
283}
284
285/********************************************
286 ¹¦ÄÜ:¹Ø±Õrtc
287 *******************************************/
288static void close_rtc(int fd)
289{
290 if (fd < 0) {
291 return;
292 }
293
294 close(fd);
295}
296
297
298/********************************************
299 * handle_get_time
300 * get RTC time.
301 *******************************************/
302static int handle_get_time(MSG_BUF *pmsg_buf)
303{
304 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_get_time NOOPS.\n");
305 return 0;
306}
307
308/********************************************
309 * ¹¦ÄÜ:ÉèÖÃrtcʱ¼ä
310 *******************************************/
311static int handle_set_time(MSG_BUF *pmsg_buf)
312{
313 time_t os_time = 0;
314 struct tm tmTime = {0};
315 struct rtc_time rtc_tm = {0};
316 int ret = 0;
317
318 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter.\n");
319 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
320 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter: set time: %ld \n", os_time);
321 convert_time(os_time, &tmTime, CONVERT_TYPE_RTC);
322 memcpy(&rtc_tm, &tmTime, sizeof(struct rtc_time));
323 slog(RTC_PRINT, SLOG_ERR, "Time:%ld, tm_year:%d, tm_mon:%d, tm_hour:%d, tm_min:%d, tm_sec:%d, tm_yday:%d, tm_mday:%d, tm_wday:%d \n",
324 mktime(&tmTime), rtc_tm.tm_year, rtc_tm.tm_mon, rtc_tm.tm_hour, rtc_tm.tm_min, tmTime.tm_sec, rtc_tm.tm_yday, rtc_tm.tm_mday, rtc_tm.tm_wday);
325
326 ret = ioctl(g_rtc_fd, RTC_SET_TIME, &rtc_tm);
327 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: RTC_SET_TIME ioctl ret = %d exit.\n", ret);
328 {
329 char buf[32] = {0};
330 snprintf(buf,32,"AT+ZMMI=2,%d,%d\r\n",1900 + rtc_tm.tm_year,1 + rtc_tm.tm_mon);
331 ret = get_modem_info(buf, NULL, NULL);
332 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_set_time ret=%d %s\n",ret,buf);
333 }
334 return 0;
335}
336
337/********************************************
338 * ¹¦ÄÜ:Ôö¼Ó¶¨Ê±Æ÷
339 *******************************************/
340static int handle_alarm_add(MSG_BUF *pmsg_buf)
341{
342 //long cur_rtc_time = 0;
343
344 struct rtc_time rtc_tm = {0};
345 ALARM_T *pAlarm = 0;
346
347 RTC_DATA_BUF *buf = pmsg_buf->aucDataBuf;
348 ALARM_T *temp = NULL;
349 int mark = 0;
350
351 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_add enter.\n");
352
353 if (buf->ulSec <= 0) {
354 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add invalid interval.\n");
355 return -1;
356 }
357
358 pAlarm = malloc(sizeof(ALARM_T));
359 if (pAlarm == NULL) {
360 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no memory.\n");
361 return -1;
362 }
363
364 //»ñÈ¡µ±Ç°Ê±¼ä second
365 if(ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
366 {
367 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
368 }
369 memcpy(&pAlarm->rtc_tm, &rtc_tm, sizeof(rtc_tm));
370
371 pAlarm->rtc_id = buf->module;
372 pAlarm->module_id = pmsg_buf->src_id;
373 pAlarm->interval = buf->ulSec ; // count down time
374 pAlarm->remain_time = buf->ulSec; // remain count down time
375 pAlarm->wakeup = buf->wakeup;
376 pAlarm->is_utc = buf->is_utc;
377 pAlarm->cpu = buf->cpu;
378 pAlarm->time_val = buf->time_val;// cur_rtc_time is mktime(&local_tm)
379 //pAlarm->time_val = get_RtcTime();// cur_rtc_time is mktime(&local_tm)
380
381 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_add pAlarm->interval is %ld ,pAlarm->time_val() is %ld, is_utc:%d.\n",
382 pAlarm->interval, pAlarm->time_val, pAlarm->is_utc);
383/*
384 if (pAlarm->time_val + pAlarm->remain_time < cur_rtc_time) {
385 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service handle_alarm_add error, pAlarm->remain_time = %ld, pAlarm->time_val = %ld, current= %ld\n", pAlarm->remain_time, pAlarm->time_val, cur_rtc_time);
386 }
387*/
388
389#ifdef PRODUCT_NOT_USE_RTC
390 pAlarm->is_utc = 0; //´Ëģʽ϶¼°´timerÉèÖö¨Ê±Æ÷£¬²»Ö§³Öutc
391 pAlarm->wakeup = 0;
392#endif
393
394 if (pAlarm->is_utc == 0) {
395 if (!list_empty(&rtc_timer_list)) {
396 //update_timer_remain(pAlarm->remain_time);
397 list_for_each_entry(temp, &rtc_timer_list, list) {
398 if (temp->remain_time > pAlarm->remain_time) {
399 mark = 1;
400 list_add(&pAlarm->list, temp->list.prev);
401 break;
402 }
403 }
404 }
405 if (mark == 0) {
406 list_add_tail(&pAlarm->list, &rtc_timer_list);
407 }
408 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc timer\n");
409 alarm_restart(&rtc_timer_list);
410 } else {
411 if (!list_empty(&rtc_alarm_list)) {
412 list_for_each_entry(temp, &rtc_alarm_list, list) {
413 if (temp->interval + temp->time_val > pAlarm->interval + pAlarm->time_val) {
414 mark = 1;
415 list_add(&pAlarm->list, temp->list.prev);
416 break;
417 }
418 }
419 }
420 if (mark == 0) {
421 list_add_tail(&pAlarm->list, &rtc_alarm_list);
422 }
423
424 if(pAlarm->wakeup)
425 save_rtc_alarm();
426
427 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc alarm \n");
428 alarm_restart(&rtc_alarm_list);
429 }
430
431 return 0;
432}
433
434/********************************************
435 * ¹¦ÄÜ:°´µ±Ç°¶¨Ê±Æ÷Ê£Óàʱ¼ä¸üж¨Ê±Æ÷
436 *******************************************/
437static void update_timer_remain(int new_remian)
438{
439 if (list_empty(&rtc_timer_list)) {
440 return;
441 }
442 int ret, remain_time;
443 ALARM_T *pTmp;
444 //list_entry(pTmp, &rtc_timer_list, list);
445 pTmp = list_entry(rtc_timer_list.next, ALARM_T, list);
446
447#ifdef PRODUCT_NOT_USE_RTC
448 time_t cur = time(NULL);
449 remain_time = g_set_softtime + pTmp->remain_time - cur;
450 if ((remain_time > pTmp->remain_time) || (remain_time <= 0)) {
451 slog(RTC_PRINT, SLOG_ERR, "rtc-service: cur:%u, set:%u, remain:%u, inveral:%u\n", cur, g_set_softtime, remain_time, pTmp->remain_time);
452 //ʱ¼ä·¢Éú¹ýÖØÖõȣ¬Ö±½ÓÉèÖö¨Ê±Æ÷µ½£¬¸÷Ä£¿éÓ¦ÔÚÊÕµ½RTC_MSG_TIME_CHANGEDÏûϢʱ¼°Ê±¸üж¨Ê±Æ÷
453 remain_time = 0;
454 }
455#else
456 ret = ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time);
457#endif
458 if (ret < 0) {
459 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no free alarm remain_time %d %s errno.\n", errno, strerror(errno));
460 // return -1;
461 }
462 //if(remain_time > new_remian)
463 {
464 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: pTmp->remain_time: %d, rtc remain_time: %d \n", pTmp->remain_time, remain_time);
465 alarm_adjust_value(pTmp->remain_time - remain_time);
466 alarm_fix_value(&rtc_timer_list);
467 }
468}
469
470/********************************************
471 * ¹¦ÄÜ:ɾ³ýÏûÏ¢Ô´Ä£¿éµÄËùÓж¨Ê±Æ÷
472 *******************************************/
473static int handle_alarm_del(MSG_BUF *pmsg_buf)
474{
475 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
476 ALARM_T *pTmp, *pTmp1;
477 int wakeup = 0;
478
479 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del enter.\n");
480 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
481 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del timer module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
482 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
483 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id)) {
484 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del timer module:0x%x , src:0x%x \n",
485 buf->module, buf->srcModule );
486 list_del(&pTmp->list);
487 free(pTmp);
488 break;
489 }
490 }
491
492 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
493 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del alarm module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
494 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
495 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id)) {
496 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del alarm module:0x%x , src:0x%x \n",
497 buf->module, buf->srcModule );
498 wakeup = pTmp->wakeup;
499 list_del(&pTmp->list);
500 free(pTmp);
501 break;
502 }
503 }
504
505 if(wakeup)
506 save_rtc_alarm();
507
508 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del exit.\n");
509
510 return 0;
511}
512
513/********************************************
514 *¹¦ÄÜ:¶¨Ê±Æ÷³¬Ê±´¦Àí
515 *******************************************/
516static int handle_alarm_timeout(MSG_BUF *pmsg_buf)
517{
518 long ret = -1;
519 ALARM_T *pAlarm;
520 RTC_DATA_BUF buf = {0};
521 long rtc_report_time = 0;
522 long alarm_node_time = 0;
523 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
524 int wakeup = 0;
525
526 if (cmd == RTC_MSG_TIMER_TIMEOUT && !list_empty(&rtc_timer_list)) {
527 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc timer timeout\n");
528 pAlarm = list_entry(rtc_timer_list.next, ALARM_T, list);
529 } else if (cmd == RTC_MSG_ALARM_TIMEOUT && !list_empty(&rtc_alarm_list)) {
530 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc alarm timeout\n");
531 pAlarm = list_entry(rtc_alarm_list.next, ALARM_T, list);
532 rtc_report_time = atol(pmsg_buf->aucDataBuf);
533 if (rtc_report_time < 0 || rtc_report_time > LONG_MAX-1)
534 {
535 rtc_report_time = 0;
536 }
537 alarm_node_time = pAlarm->interval + pAlarm->time_val;
538 if((rtc_report_time>alarm_node_time && (rtc_report_time-alarm_node_time)>g_time_offset)
539 ||(alarm_node_time>rtc_report_time && (alarm_node_time-rtc_report_time)>g_time_offset))
540 {
541 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc node was deleted, timeout unexpected; node:%ld, rtc:%ld \n", alarm_node_time, rtc_report_time);
542 alarm_fix_value(&rtc_alarm_list);
543
544 if(pAlarm->wakeup)
545 save_rtc_alarm();
546
547 alarm_restart(&rtc_alarm_list);
548 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
549 return 0;
550 }
551 buf.time_val = alarm_node_time;
552 } else {
553 slog(RTC_PRINT, SLOG_ERR, "rtc-service: timeout unexpected\n");
554 return -1;
555 }
556
557 wakeup = pAlarm->wakeup;
558 list_del(&pAlarm->list);
559 buf.module = pAlarm->rtc_id;
560 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d \n", pAlarm->module_id, buf.module, pAlarm->is_utc);
561 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, pAlarm->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
562 if (ret != 0) {
563 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected\n");
564 }
565
566 if (pAlarm->is_utc == 0) {
567 alarm_adjust_value(pAlarm->remain_time);
568 alarm_fix_value(&rtc_timer_list);
569 alarm_restart(&rtc_timer_list);
570 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart imer.\n");
571 } else {
572 alarm_fix_value(&rtc_alarm_list);
573 if(wakeup)
574 save_rtc_alarm();
575 alarm_restart(&rtc_alarm_list);
576 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
577 }
578
579 free(pAlarm);
580 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout exit.\n");
581 return 0;
582}
583
584/********************************************
585 * ¹¦ÄÜ:ɾ³ý¹Ø»ú²»±£´æµÄ¶¨Ê±Æ÷
586 *******************************************/
587static void handle_del_all(MSG_BUF *pmsg_buf)
588{
589 ALARM_T *pTmp, *pTmp1;
590
591 update_timer_remain(0);
592 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
593 if (pTmp->wakeup == 0) {
594 list_del(&pTmp->list);
595 free(pTmp);
596 }
597 }
598 alarm_restart(&rtc_timer_list);
599
600 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
601 if (pTmp->wakeup == 0) {
602 list_del(&pTmp->list);
603 free(pTmp);
604 }
605 }
606 alarm_restart(&rtc_alarm_list);
607}
608
609/********************************************
610 * ¹¦ÄÜ:sntpͬ²½³É¹¦ºó£¬¸üÐÂalarm_list
611 *******************************************/
612static void update_alarm()
613{
614 ALARM_T *pTmp, *pTmp1;
615 //long sec = get_RtcTime();
616 long sec = time(0);
617 RTC_DATA_BUF buf = {0};
618
619#ifdef PRODUCT_NOT_USE_RTC
620 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
621 buf.module = pTmp->rtc_id;
622 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
623 if (pTmp->interval + pTmp->time_val < sec) {
624 list_del(&pTmp->list);
625 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED timer, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id, (pTmp->interval + pTmp->time_val));
626 free(pTmp);
627 } else {
628 pTmp->remain_time = pTmp->interval + pTmp->time_val - sec;
629 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: timer adjust, alarm_id(%d), alarm_time(%ld), remain(%ld).\n", pTmp->rtc_id, (pTmp->interval + pTmp->time_val), pTmp->remain_time);
630 }
631 }
632 alarm_restart(&rtc_timer_list);
633#else
634 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
635 buf.module = pTmp->rtc_id;
636 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
637 if (pTmp->interval + pTmp->time_val < sec) {
638 list_del(&pTmp->list);
639 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED alarm, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id, (pTmp->interval + pTmp->time_val));
640 free(pTmp);
641 }
642 }
643 alarm_restart(&rtc_alarm_list);
644#endif
645 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: update_alarm exit cur_rtc_time: % ld.\n", sec);
646}
647
648/********************************************
649 * ¹¦ÄÜ:¸üж¨Ê±Æ÷Ê£Óàʱ¼ä
650 *******************************************/
651static void alarm_adjust_value(int time)
652{
653 ALARM_T * temp;
654 list_for_each_entry(temp, &rtc_timer_list, list) {
655 temp->remain_time -= time;
656 }
657}
658
659/********************************************
660 *¹¦ÄÜ: ɾ³ýÎÞЧ¶¨Ê±Æ÷
661 *******************************************/
662static void alarm_fix_value(struct list_head *rtc_list)
663{
664 ALARM_T *pTmp, *pTmp1;
665 RTC_DATA_BUF buf = {0};
666 int wakeup = 0;
667
668 if (rtc_list == &rtc_timer_list) {
669 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
670 if (pTmp->remain_time <= 0) {
671 buf.module = pTmp->rtc_id;
672 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
673 list_del(&pTmp->list);
674 free(pTmp);
675 }
676 }
677 } else if (rtc_list == &rtc_alarm_list) {
678 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
679 if (pTmp->time_val + pTmp->interval <= (time(0) + g_time_offset)) {
680 if(pTmp->wakeup)
681 wakeup = pTmp->wakeup;
682
683 buf.module = pTmp->rtc_id;
684 buf.time_val = pTmp->time_val + pTmp->interval;
685 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
686 list_del(&pTmp->list);
687 free(pTmp);
688 }
689 }
690
691 if(wakeup)
692 save_rtc_alarm();
693 }
694 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_fix_value exit.\n");
695}
696
697#ifdef PRODUCT_NOT_USE_RTC
698/*¶ÔӦûÓÐrtcµÄ²úÆ·£¬rtc¶¨Ê±Æ÷ÊÊÅäµ½softtimerµÄ»Øµ÷º¯Êý*/
699static void rtc_set_softtimer_cb(void *param)
700{
701 int ret = -1;
702
703 slog(NET_PRINT, SLOG_ERR, "rtc-service: rtc_adapt_softtimer_cb \n");
704
705 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, 0, 0);;
706 if (ret != 0) {
707 slog(MISC_PRINT, SLOG_ERR, "rtc-service: set rtc timer send msg failed \n");
708 }
709 return 0;
710}
711#endif
712
713/********************************************
714 * ¹¦ÄÜ:ÖØÆô¶¨Ê±Æ÷£¬Ã»ÓÐÔò¹Ø±Õ¶¨Ê±Æ÷
715 *******************************************/
716static void alarm_restart(struct list_head *rtc_list)
717{
718 int ret = 0;
719 //struct tm *p_tm = 0;
720 struct tm ret_tm = {0};
721 struct rtc_wkalrm alarm = {0};
722 long tVal = 0;
723 long remain = -1;
724 ALARM_T *ptmp = NULL;
725 long cur_rtc_time = time(0);
726 RTC_DATA_BUF buf = {0};
727NEXT:
728 if (!list_empty(rtc_list)) {
729 ptmp = list_entry(rtc_list->next, ALARM_T, list);
730 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart enter.\n");
731 memset(&buf, 0, sizeof(RTC_DATA_BUF));
732 if (ptmp->is_utc == 0) {
733#ifdef PRODUCT_NOT_USE_RTC
734 g_set_softtime = time(NULL);
735 sc_timer_delete(RTC_USE_SOFTTIMER_ID);
736 ret = sc_timer_create(RTC_USE_SOFTTIMER_ID, TIMER_FLAG_ONCE, ptmp->remain_time*1000, rtc_set_softtimer_cb, NULL);
737#else
738 ret = ioctl(g_rtc_fd, ZX234290_SET_TIMER, ptmp->remain_time);
739#endif
740 if (ret != 0) {
741 slog(RTC_PRINT, SLOG_ERR, "rtc-service: alarm_restart timer: set failed, ret:%d \n", ret);
742 return;
743 }
744 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart: set timer ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x\n", ptmp->remain_time, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
745 } else {
746 tVal = ptmp->interval + ptmp->time_val;
747 remain = tVal - cur_rtc_time;
748 if (remain <= g_time_offset) {
749 //¹ýʱ¶¨Ê±Æ÷¶ªÆú´¦Àí£¬Èç¹û¶àÄ£¿éÕýºÃÉèµÄ»ù±¾ÔÚͬһ¸öʱ¼äµã£¬ºóÕ߻ᱻ¶ªÆú£¬¿¼ÂÇÐ޸ijÉÖ±½Ó֪ͨģ¿éʱ¼äµ½
750 slog(RTC_PRINT, SLOG_ERR, "rtc-service: remain time error, time_set:%ld, remain:%ld, alarmer:%ld, interval:%ld, current:%ld, module:0x%x, rtc_id:0x%x\n", ptmp->time_val, remain, tVal, ptmp->interval, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
751 buf.module = ptmp->rtc_id;
752 buf.time_val = ptmp->time_val + ptmp->interval;
753 ipc_send_message2(MODULE_ID_RTC_SERVICE, ptmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
754 list_del(&ptmp->list);
755 free(ptmp);
756 ptmp = NULL;
757 goto NEXT;
758 }
759
760 //localtime_r(&tVal, &ret_tm);
761 gmtime_r(&tVal, &ret_tm);
762 memcpy(&alarm.time, &ret_tm, sizeof(struct rtc_time));
763 alarm.enabled = 1;
764
765 /*Ê×ÏÈ´ò¿ªalarm ÖжÏ*/
766 ret = ioctl(g_rtc_fd, RTC_AIE_ON);
767 if (ret != 0) {
768 slog(RTC_PRINT, SLOG_ERR, "rtc-service: open alarm failed, ret:%d\n", ret);
769 return;
770 }
771
772 /*ÉèÖÃalarm ʱ¼ä*/
773 ret = ioctl(g_rtc_fd, RTC_WKALM_SET, &alarm);
774 if (ret != 0) {
775 slog(RTC_PRINT, SLOG_ERR, "rtc-service: set alarm failed, ret:%d\n", ret);
776 return;
777 }
778 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart set alarm ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x \n", remain, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
779 }
780 } else {
781 if (rtc_list == &rtc_timer_list) {
782 //¹Ø±Õrtc time, Éè¼ä¸ôʱ¼äΪ0
783 ret = ioctl(g_rtc_fd, ZX234290_TIMER_ENABLED, 0);
784 if (ret != 0) {
785 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable timer failed, ret:%d\n", ret);
786 return;
787 }
788 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable timer ok \n");
789 } else {
790 /*¹Ø±Õalarm ÖжÏ*/
791 ret = ioctl(g_rtc_fd, RTC_AIE_OFF);
792 if (ret != 0) {
793 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable alarm failed, ret:%d\n", ret);
794 return;
795 }
796 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable alarm ok \n");
797 }
798 }
799 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart exit.\n");
800}
801
802static void rtc_set_local_time(MSG_BUF *msg)
803{
804 struct tm set_tm = {0};
805 struct timeval time_tv;
806
807 slog(RTC_PRINT, SLOG_DEBUG, "rtc_set_local_time, %d, %d \n", sizeof(struct tm), msg->usDataLen);
808
809 memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen);
810
811 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n",
812 set_tm.tm_year, set_tm.tm_mon, set_tm.tm_mday, set_tm.tm_hour, set_tm.tm_min, set_tm.tm_sec, set_tm.tm_wday);
813
814 time_tv.tv_sec = mktime(&set_tm);
815 if(time_tv.tv_sec < 0)
816 {
817
818 slog(RTC_PRINT, SLOG_DEBUG, "mktime fail, set time_tv.tv_sec to 0 \n");
819 time_tv.tv_sec = 0;
820 }
821 time_tv.tv_usec = 0;
822
823 if (0 != settimeofday(&time_tv, NULL))
824 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time failed \n");
825}
826
827/********************************************
828 * process_msg
829 * handle the specified MSG.
830 *******************************************/
831static int process_msg(MSG_BUF *pmsg_buf)
832{
833 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
834 unsigned short req_cmd = 0;
835
836 switch (cmd) {
837 case RTC_MSG_GET_TIME:
838 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_GET_TIME, src:0x%x.\n", pmsg_buf->src_id);
839 handle_get_time(pmsg_buf);
840 break;
841 case RTC_MSG_SET_TIME:
842 security_log(pmsg_buf->src_id,"set time");
843 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_SET_TIME, src:0x%x.\n", pmsg_buf->src_id);
844 handle_set_time(pmsg_buf);
845 update_alarm();
846 break;
847 case RTC_MSG_ADD_ALARM:
848 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_ADD_ALARM, src:0x%x, g_clean_flag = %d.\n", pmsg_buf->src_id, g_clean_flag);
849 //Ìí¼ÓrtcǰÏÈɾ³ý´Ërtc_idÒÑÌí¼Ó¼Ç¼£¬Ã¿¸örtc_idÖ»±£Áô1¸ö¶¨Ê±Æ÷
850 if(0 == g_clean_flag){
851 update_timer_remain(0);
852 handle_alarm_del(pmsg_buf);
853 handle_alarm_add(pmsg_buf);
854 }
855 else{
856 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: g_clean_flag is nonzero:0x%x, dont add alarm.\n", g_clean_flag);
857 }
858
859 //wifi´¦ÀíÍêǰ²»ÄÜÐÝÃߣ¬ÔÝʱʹÓÃÑÓÊ±Ëø£¬´ý·½°¸×鏸³ö·½°¸ºóÔÙµ÷Õû
860 //if (pmsg_buf->usSourceModuleID == MODULE_ID_WDT)
861 //write_lockfile("/sys/power/wake_unlock", "rtc_io_time_timeout_lock");
862
863 break;
864 case RTC_MSG_DEL_ALARM:
865 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALARM, src:0x%x.\n", pmsg_buf->src_id);
866 update_timer_remain(0);
867 handle_alarm_del(pmsg_buf);
868 alarm_restart(&rtc_timer_list);
869 alarm_restart(&rtc_alarm_list);
870 break;
871 case RTC_MSG_ALARM_TIMEOUT:
872 case RTC_MSG_TIMER_TIMEOUT:
873 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_TIMEOUT, src:0x%x, cmd:0x%x.\n", pmsg_buf->src_id, cmd);
874 handle_alarm_timeout(pmsg_buf);
875 break;
876 case RTC_MSG_DEL_ALL:
877 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
878 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
879 if(MSG_CMD_RESET_RSP == req_cmd || MSG_CMD_POWEROFF_RSP == req_cmd || MSG_CMD_RESTART_RSP == req_cmd){
880 g_clean_flag = req_cmd;
881 }
882 handle_del_all(pmsg_buf);
883 save_rtc_alarm();
884 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
885 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
886 break;
887
888 default:
889 break;
890 }
891 return 0;
892}
893
894static int rtc_service_sock_init()
895{
896 int sockfd = 0;
897 int ret;
898 struct sockaddr_nl snl;
899
900 bzero(&snl, sizeof(struct sockaddr_nl));
901 snl.nl_family = AF_NETLINK;
902 snl.nl_pid = getpid();
903 snl.nl_groups = 1;
904
905 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
906 perror ("signal");
907
908 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
909 if (sockfd < 0)
910 {
911 slog(RTC_PRINT,SLOG_ERR, "create rtc service socket failed!\n");
912 return -1;
913 }
914// setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
915
916 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
917 if (ret < 0)
918 {
919 slog(RTC_PRINT,SLOG_ERR, "rtc service socket bind fail!\n");
920 close(sockfd);
921 return -1;
922 }
923
924 return sockfd;
925}
926//change@/devices/platform/zx29_i2c.0/i2c-0/0-0012/zx234290-rtc
927static int rtc_time_msg_parse(const char *msg, int msglen)
928{
929 int offlen = 0;
930 const char *keys = NULL;
931 char* timeStr = NULL;
932 char *pSec = NULL;
933 slog(RTC_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc \n", __func__, __LINE__);
934
935 offlen += strlen((char *)msg) + 1;
936 while (offlen <= msglen) {
937 keys = (char *)(msg + offlen);
938 //slog(NET_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc%s, %d\n", __func__, __LINE__, keys, offlen);
939 if (strncmp(keys, RTC_ALARM_TIMEOUT_MSG, strlen(RTC_ALARM_TIMEOUT_MSG)) == 0)
940 {
941#if 1 // cov M DEADCODE
942 pSec = strstr(keys, "sec:");
943 if (pSec == NULL) {
944 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
945 return -1;
946 }
947 timeStr = pSec + strlen("sec:");
948
949#else
950 timeStr = strstr(keys, "sec:") + strlen("sec:");
951 if (timeStr == NULL) {
952 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
953 return -1;
954 }
955#endif
956 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_ALARM_TIMEOUT, strlen(timeStr), timeStr, 0);
957 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc alarm timeout \n");
958 return 0;
959 }
960 else if (strncmp(keys, RTC_TIMER_TIMEOUT_MSG, strlen(keys)) == 0)
961 {
962 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, NULL, 0);
963 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc timer timeout \n");
964 return 0;
965 }
966 offlen += strlen((char *)keys) + 1;
967 }
968 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event not supported \n");
969 return -1;
970}
971
972/********************************************
973 * rtc_service_thread_process
974 * looper for processing zx234290-rtc msg.
975 *******************************************/
976void rtc_event_process(void)
977{
978 int rtc_service_fd = 0;
979 fd_set readfds;
980 int maxfd = 0;
981 char buf[RTC_MSG_BUFFER_SIZE] = {0};
982 int len = 0;
983 int ret = 0;
984 prctl(PR_SET_NAME, "rtceventhandle", 0, 0, 0);
985 rtc_service_fd = rtc_service_sock_init();
986 if(rtc_service_fd < 0)
987 {
988 slog(RTC_PRINT,SLOG_ERR, "rtc service socket init fail!\n");
989 return ;
990 }
991
992 maxfd = rtc_service_fd;
993 while (1)
994 {
995 FD_ZERO(&readfds);
996 FD_SET(rtc_service_fd, &readfds);
997
998 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
999 if (ret == -1 && errno == EINTR)
1000 continue;
1001
1002 if(FD_ISSET(rtc_service_fd, &readfds))
1003 {
1004 memset(buf, 0, sizeof(buf));
1005 len = recv(rtc_service_fd, &buf, sizeof(buf), 0);
1006 if(len <= 0)
1007 {
1008 slog(RTC_PRINT,SLOG_ERR, "rtc service socket recv msg fail!\n");
1009 continue;
1010 }
1011
1012 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
1013 if (strstr(buf, "zx234290-rtc")){
1014 rtc_time_msg_parse(buf, len);
1015 }
1016 continue;
1017 }
1018 }
1019}
1020
1021/********************************************
1022 * msg_looper
1023 * main looper for processing MSG.
1024 *******************************************/
1025static void msg_looper(void)
1026{
1027 int ret = 0;
1028 int msg_id = 0;
1029 MSG_BUF msg_buf = {0};
1030// key_t msgkey = get_msg_key();
1031
1032 msg_id = msgget(MODULE_ID_RTC_SERVICE, IPC_CREAT | 0600);
1033
1034 memset(&msg_buf, 0, sizeof(msg_buf));
1035
1036 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper enter(msgid=%d)&waiting...\n", msg_id);
1037 while (1) {
1038 ret = msgrcv(msg_id, &msg_buf, sizeof(MSG_BUF) - sizeof(long), 0, 0);
1039 wake_rtc_lock();
1040 if (ret <= 0) {
1041 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper recv msg break(%d)\n", ret);
1042 wake_rtc_unlock();
1043 continue;
1044 }
1045 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper process_msg in, src:0x%x, target:0x%x.\n", msg_buf.src_id, msg_buf.dst_id);
1046
1047 process_msg(&msg_buf);
1048
1049 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
1050 wake_rtc_unlock();
1051 }
1052}
1053
1054static void print_rtc_list_info(void)
1055{
1056 ALARM_T *temp = NULL;
1057 int remain_time;
1058
1059 if(ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time) < 0) // cov M CHECKED_RETURN
1060 {
1061 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
1062 }
1063
1064
1065 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc remain_time :%ld \n", remain_time);
1066
1067 if (!list_empty(&rtc_timer_list)) {
1068 list_for_each_entry(temp, &rtc_timer_list, list) {
1069 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d\n",
1070 temp->rtc_id,
1071 temp->module_id,
1072 temp->time_val,
1073 temp->remain_time,
1074 temp->cpu);
1075 }
1076 }
1077 else {
1078 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_timer_list is empty!");
1079 }
1080
1081 if (!list_empty(&rtc_alarm_list)) {
1082 list_for_each_entry(temp, &rtc_alarm_list, list) {
1083 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d, \
1084 rtc_tm.tm_year:%d, rtc_tm.tm_mon:%d, rtc_tm.tm_mday:%d, rtc_tm.tm_min:%d, rtc_tm.tm_sec:%d \n",
1085 temp->rtc_id,
1086 temp->module_id,
1087 temp->time_val,
1088 temp->remain_time,
1089 temp->cpu,
1090 temp->rtc_tm.tm_year, temp->rtc_tm.tm_mon, temp->rtc_tm.tm_mday, temp->rtc_tm.tm_min, temp->rtc_tm.tm_sec);
1091 }
1092 }
1093 else {
1094 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_alarm_list is empty!");
1095 }
1096}
1097
1098/* ÐźŴ¦Àí*/
1099static void sig_usr(int signo)
1100{
1101#if 0//(APP_OS_TYPE == APP_OS_LINUX)
1102 if (signo == SIGUSR1)
1103 {
1104 slog(RTC_PRINT,SLOG_NORMAL, "rtc-service receive SIGUSR1! \n");
1105 print_rtc_list_info();
1106 }
1107 else
1108 {
1109 slog(RTC_PRINT,SLOG_ERR, "rtc-service receive signo:%d", signo);
1110 }
1111#endif
1112}
1113
1114/*RTCÏûÏ¢½âÎö*/
1115int rtc_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
1116{
1117 if (strstr(msg, "zx234290-rtc")){
1118 rtc_time_msg_parse(msg, msglen);
1119 }
1120 return -1;
1121}
1122
1123/********************************************
1124 * main
1125 * entry of RTC-Service!
1126 *******************************************/
1127int rtc_service_main(int argc, char **argv)
1128{
1129
1130 slog(RTC_PRINT,SLOG_DEBUG, "rtc-service enter...\n");
1131 pthread_t rtcservice_thread_id;
1132 pthread_attr_t attr;
1133 int result = 0;
1134 prctl(PR_SET_NAME, "rtc-service", 0, 0, 0);
1135
1136 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
1137 loglevel_init();
1138
1139 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
1140 {
1141 slog(RTC_PRINT,SLOG_ERR, "rtc-service not catch SIGUSR1 \n");
1142 }
1143
1144 INIT_LIST_HEAD(&rtc_timer_list);
1145 INIT_LIST_HEAD(&rtc_alarm_list);
1146
1147 g_clean_flag = 0;
1148
1149 //rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
1150 if ((g_rtc_fd = open_rtc()) < 0) {
1151 slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
1152 return -1;
1153 }
1154 init_rtc_alarm();
1155/*
1156 pthread_attr_init(&attr);
1157 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1158 result = pthread_create(&rtcservice_thread_id, &attr, rtc_event_process, NULL);
1159 pthread_attr_destroy(&attr);
1160 if(result != 0)
1161 {
1162 slog(RTC_PRINT,SLOG_ERR, "[rtc-service] rtcservice_thread pthread_create faild!\n");
1163 return -1;
1164 }
1165 else
1166 {
1167 printf("[rtc-service] rtcservice_thread pthread_create SUCCESS!\n");
1168 }
1169*/
1170 hotplug_parse_register(DEVICE_TYPE_APP_RTC, rtc_app_msg_parse);
1171 msg_looper();
1172 //klocwork
1173 //close_rtc(g_rtc_fd);
1174
1175 //slog(RTC_PRINT, SLOG_ERR, "rtc-service exit...\n");
1176
1177 return 0;
1178}