blob: 481c550ba5a1c2fd6d08a2808c2a60f67b93badd [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{
xf.libdd93d52023-05-12 07:10:14 -0700304 struct rtc_time rtc_tm = {0};
305 int ret = 0;
306 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
307
lh9ed821d2023-04-07 01:36:19 -0700308 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_get_time NOOPS.\n");
xf.libdd93d52023-05-12 07:10:14 -0700309
310 if(buf->cpu == RTC_CPU_CAP)
311 {
312 int cmd = RTC_MSG_GET_TIME;
313 RTC_DATA_BUF sendbuf = {0};
314 unsigned long cur_sec =0;
315
316 ret = ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm);
317 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_get_time tm_year:%d, tm_mon:%d, tm_hour:%d, tm_min:%d, tm_yday:%d, tm_mday:%d, tm_wday:%d.\n",
318 rtc_tm.tm_year, rtc_tm.tm_mon, rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_yday, rtc_tm.tm_mday, rtc_tm.tm_wday);
319 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_get_time src_id:0x%x, buf->cpu:%d.\n",buf->srcModule,buf->cpu);
320
321 cur_sec = mktime(&rtc_tm);
322 sendbuf.srcModule = buf->srcModule;
323 sendbuf.cpu = RTC_CPU_AP;
324 sendbuf.ulSec = cur_sec;
325
326 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_get_time: send to cap cmd:0x%x, srcMod:0x%x, cur_sec:%lu \n", cmd, sendbuf.srcModule, sendbuf.ulSec);
327 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
328 if (ret != 0)
329 {
330 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n",ret);;
331 }
332 }
333
lh9ed821d2023-04-07 01:36:19 -0700334 return 0;
335}
336
337/********************************************
338 * ¹¦ÄÜ:ÉèÖÃrtcʱ¼ä
339 *******************************************/
340static int handle_set_time(MSG_BUF *pmsg_buf)
341{
342 time_t os_time = 0;
343 struct tm tmTime = {0};
344 struct rtc_time rtc_tm = {0};
345 int ret = 0;
346
347 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter.\n");
348 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
349 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter: set time: %ld \n", os_time);
350 convert_time(os_time, &tmTime, CONVERT_TYPE_RTC);
351 memcpy(&rtc_tm, &tmTime, sizeof(struct rtc_time));
352 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",
353 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);
354
355 ret = ioctl(g_rtc_fd, RTC_SET_TIME, &rtc_tm);
356 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: RTC_SET_TIME ioctl ret = %d exit.\n", ret);
357 {
358 char buf[32] = {0};
359 snprintf(buf,32,"AT+ZMMI=2,%d,%d\r\n",1900 + rtc_tm.tm_year,1 + rtc_tm.tm_mon);
360 ret = get_modem_info(buf, NULL, NULL);
361 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_set_time ret=%d %s\n",ret,buf);
362 }
363 return 0;
364}
365
366/********************************************
xf.li742dd022023-06-08 01:43:32 -0700367 * ¹¦ÄÜ:sntpͬ²½ÏµÍ³Ê±¼ä
368 *******************************************/
369static int handle_sntp_update(MSG_BUF *pmsg_buf)
370{
371 time_t os_time = 0;
372 struct timeval time_tv = {0};
373 int ret = 0;
374
375 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
376
377 time_tv.tv_sec = os_time;
378 ret = settimeofday(&time_tv,NULL);
379 if(ret!=0)
380 {
381 slog(RTC_PRINT, SLOG_ERR, "handle_sntp_update is fail,ret:%d \n",ret);
382 }
383 return 0;
384}
385
386/********************************************
lh9ed821d2023-04-07 01:36:19 -0700387 * ¹¦ÄÜ:Ôö¼Ó¶¨Ê±Æ÷
388 *******************************************/
389static int handle_alarm_add(MSG_BUF *pmsg_buf)
390{
391 //long cur_rtc_time = 0;
392
393 struct rtc_time rtc_tm = {0};
394 ALARM_T *pAlarm = 0;
395
396 RTC_DATA_BUF *buf = pmsg_buf->aucDataBuf;
397 ALARM_T *temp = NULL;
398 int mark = 0;
399
400 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_add enter.\n");
401
402 if (buf->ulSec <= 0) {
403 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add invalid interval.\n");
404 return -1;
405 }
406
407 pAlarm = malloc(sizeof(ALARM_T));
408 if (pAlarm == NULL) {
409 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no memory.\n");
410 return -1;
411 }
412
413 //»ñÈ¡µ±Ç°Ê±¼ä second
414 if(ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
415 {
416 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
417 }
418 memcpy(&pAlarm->rtc_tm, &rtc_tm, sizeof(rtc_tm));
419
420 pAlarm->rtc_id = buf->module;
xf.libdd93d52023-05-12 07:10:14 -0700421 pAlarm->module_id = buf->srcModule;
lh9ed821d2023-04-07 01:36:19 -0700422 pAlarm->interval = buf->ulSec ; // count down time
423 pAlarm->remain_time = buf->ulSec; // remain count down time
424 pAlarm->wakeup = buf->wakeup;
425 pAlarm->is_utc = buf->is_utc;
426 pAlarm->cpu = buf->cpu;
427 pAlarm->time_val = buf->time_val;// cur_rtc_time is mktime(&local_tm)
428 //pAlarm->time_val = get_RtcTime();// cur_rtc_time is mktime(&local_tm)
429
430 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_add pAlarm->interval is %ld ,pAlarm->time_val() is %ld, is_utc:%d.\n",
431 pAlarm->interval, pAlarm->time_val, pAlarm->is_utc);
432/*
433 if (pAlarm->time_val + pAlarm->remain_time < cur_rtc_time) {
434 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);
435 }
436*/
437
438#ifdef PRODUCT_NOT_USE_RTC
439 pAlarm->is_utc = 0; //´Ëģʽ϶¼°´timerÉèÖö¨Ê±Æ÷£¬²»Ö§³Öutc
440 pAlarm->wakeup = 0;
441#endif
442
443 if (pAlarm->is_utc == 0) {
444 if (!list_empty(&rtc_timer_list)) {
445 //update_timer_remain(pAlarm->remain_time);
446 list_for_each_entry(temp, &rtc_timer_list, list) {
447 if (temp->remain_time > pAlarm->remain_time) {
448 mark = 1;
449 list_add(&pAlarm->list, temp->list.prev);
450 break;
451 }
452 }
453 }
454 if (mark == 0) {
455 list_add_tail(&pAlarm->list, &rtc_timer_list);
456 }
457 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc timer\n");
458 alarm_restart(&rtc_timer_list);
459 } else {
460 if (!list_empty(&rtc_alarm_list)) {
461 list_for_each_entry(temp, &rtc_alarm_list, list) {
462 if (temp->interval + temp->time_val > pAlarm->interval + pAlarm->time_val) {
463 mark = 1;
464 list_add(&pAlarm->list, temp->list.prev);
465 break;
466 }
467 }
468 }
469 if (mark == 0) {
470 list_add_tail(&pAlarm->list, &rtc_alarm_list);
471 }
472
473 if(pAlarm->wakeup)
474 save_rtc_alarm();
475
476 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc alarm \n");
477 alarm_restart(&rtc_alarm_list);
478 }
479
480 return 0;
481}
482
483/********************************************
484 * ¹¦ÄÜ:°´µ±Ç°¶¨Ê±Æ÷Ê£Óàʱ¼ä¸üж¨Ê±Æ÷
485 *******************************************/
486static void update_timer_remain(int new_remian)
487{
488 if (list_empty(&rtc_timer_list)) {
489 return;
490 }
491 int ret, remain_time;
492 ALARM_T *pTmp;
493 //list_entry(pTmp, &rtc_timer_list, list);
494 pTmp = list_entry(rtc_timer_list.next, ALARM_T, list);
495
496#ifdef PRODUCT_NOT_USE_RTC
497 time_t cur = time(NULL);
498 remain_time = g_set_softtime + pTmp->remain_time - cur;
499 if ((remain_time > pTmp->remain_time) || (remain_time <= 0)) {
500 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);
501 //ʱ¼ä·¢Éú¹ýÖØÖõȣ¬Ö±½ÓÉèÖö¨Ê±Æ÷µ½£¬¸÷Ä£¿éÓ¦ÔÚÊÕµ½RTC_MSG_TIME_CHANGEDÏûϢʱ¼°Ê±¸üж¨Ê±Æ÷
502 remain_time = 0;
503 }
504#else
505 ret = ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time);
506#endif
507 if (ret < 0) {
508 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no free alarm remain_time %d %s errno.\n", errno, strerror(errno));
509 // return -1;
510 }
511 //if(remain_time > new_remian)
512 {
513 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: pTmp->remain_time: %d, rtc remain_time: %d \n", pTmp->remain_time, remain_time);
514 alarm_adjust_value(pTmp->remain_time - remain_time);
515 alarm_fix_value(&rtc_timer_list);
516 }
517}
518
519/********************************************
520 * ¹¦ÄÜ:ɾ³ýÏûÏ¢Ô´Ä£¿éµÄËùÓж¨Ê±Æ÷
521 *******************************************/
522static int handle_alarm_del(MSG_BUF *pmsg_buf)
523{
524 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
525 ALARM_T *pTmp, *pTmp1;
526 int wakeup = 0;
527
528 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del enter.\n");
529 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
530 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del timer module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
531 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
xf.libdd93d52023-05-12 07:10:14 -0700532 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id)&& (buf->cpu == pTmp->cpu)) {
533 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del timer module:0x%x , src:0x%x , cpu:%d \n",
534 buf->module, buf->srcModule, buf->cpu);
lh9ed821d2023-04-07 01:36:19 -0700535 list_del(&pTmp->list);
536 free(pTmp);
537 break;
538 }
539 }
540
541 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
542 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del alarm module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
543 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
xf.libdd93d52023-05-12 07:10:14 -0700544 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id)&& (buf->cpu == pTmp->cpu)) {
545 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del alarm module:0x%x , src:0x%x , cpu:%d \n",
546 buf->module, buf->srcModule, buf->cpu);
lh9ed821d2023-04-07 01:36:19 -0700547 wakeup = pTmp->wakeup;
548 list_del(&pTmp->list);
549 free(pTmp);
550 break;
551 }
552 }
553
554 if(wakeup)
555 save_rtc_alarm();
556
557 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del exit.\n");
558
559 return 0;
560}
561
xf.libdd93d52023-05-12 07:10:14 -0700562void send_ok_del_rsp(MSG_BUF *pmsg_buf)
563{
564 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
565
566 if(buf->cpu == RTC_CPU_CAP)
567 {
568 int ret = 0;
569 RTC_DATA_BUF sendbuf = {0};
570 sendbuf.cpu = RTC_CPU_AP;
571 sendbuf.module = buf->module;
572 sendbuf.srcModule = buf->srcModule;
573 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, RTC_MSG_DEL_ALARM, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
574 if (ret != 0)
575 {
576 slog(RTC_PRINT, SLOG_ERR, "send_ok_del_rsp: failed,ret:%d\n",ret);
577 }
578 }
579}
lh9ed821d2023-04-07 01:36:19 -0700580/********************************************
581 *¹¦ÄÜ:¶¨Ê±Æ÷³¬Ê±´¦Àí
582 *******************************************/
583static int handle_alarm_timeout(MSG_BUF *pmsg_buf)
584{
585 long ret = -1;
586 ALARM_T *pAlarm;
587 RTC_DATA_BUF buf = {0};
588 long rtc_report_time = 0;
589 long alarm_node_time = 0;
590 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
591 int wakeup = 0;
592
593 if (cmd == RTC_MSG_TIMER_TIMEOUT && !list_empty(&rtc_timer_list)) {
594 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc timer timeout\n");
595 pAlarm = list_entry(rtc_timer_list.next, ALARM_T, list);
596 } else if (cmd == RTC_MSG_ALARM_TIMEOUT && !list_empty(&rtc_alarm_list)) {
597 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc alarm timeout\n");
598 pAlarm = list_entry(rtc_alarm_list.next, ALARM_T, list);
599 rtc_report_time = atol(pmsg_buf->aucDataBuf);
600 if (rtc_report_time < 0 || rtc_report_time > LONG_MAX-1)
601 {
602 rtc_report_time = 0;
603 }
604 alarm_node_time = pAlarm->interval + pAlarm->time_val;
605 if((rtc_report_time>alarm_node_time && (rtc_report_time-alarm_node_time)>g_time_offset)
606 ||(alarm_node_time>rtc_report_time && (alarm_node_time-rtc_report_time)>g_time_offset))
607 {
608 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc node was deleted, timeout unexpected; node:%ld, rtc:%ld \n", alarm_node_time, rtc_report_time);
609 alarm_fix_value(&rtc_alarm_list);
610
611 if(pAlarm->wakeup)
612 save_rtc_alarm();
613
614 alarm_restart(&rtc_alarm_list);
615 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
616 return 0;
617 }
618 buf.time_val = alarm_node_time;
619 } else {
620 slog(RTC_PRINT, SLOG_ERR, "rtc-service: timeout unexpected\n");
621 return -1;
622 }
623
624 wakeup = pAlarm->wakeup;
625 list_del(&pAlarm->list);
626 buf.module = pAlarm->rtc_id;
xf.libdd93d52023-05-12 07:10:14 -0700627 buf.srcModule = pAlarm->module_id;
lh9ed821d2023-04-07 01:36:19 -0700628 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);
xf.libdd93d52023-05-12 07:10:14 -0700629 if(pAlarm->cpu == RTC_CPU_AP)
630 {
631 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, pAlarm->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
632 if (ret != 0) {
633 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n",ret);
634 }
lh9ed821d2023-04-07 01:36:19 -0700635 }
xf.libdd93d52023-05-12 07:10:14 -0700636 else
637 {
638 buf.module = pAlarm->rtc_id;
639 buf.srcModule = pAlarm->module_id;
640 buf.cpu = RTC_CPU_AP;
641 buf.is_utc = pAlarm->is_utc;
lh9ed821d2023-04-07 01:36:19 -0700642
xf.libdd93d52023-05-12 07:10:14 -0700643 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n", cmd, buf.srcModule, buf.module, pAlarm->is_utc);
644 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
645 if (ret != 0)
646 {
647 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n",ret);;
648 }
649 }
lh9ed821d2023-04-07 01:36:19 -0700650 if (pAlarm->is_utc == 0) {
651 alarm_adjust_value(pAlarm->remain_time);
652 alarm_fix_value(&rtc_timer_list);
653 alarm_restart(&rtc_timer_list);
654 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart imer.\n");
655 } else {
656 alarm_fix_value(&rtc_alarm_list);
657 if(wakeup)
658 save_rtc_alarm();
659 alarm_restart(&rtc_alarm_list);
660 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
661 }
662
663 free(pAlarm);
664 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout exit.\n");
665 return 0;
666}
667
668/********************************************
669 * ¹¦ÄÜ:ɾ³ý¹Ø»ú²»±£´æµÄ¶¨Ê±Æ÷
670 *******************************************/
671static void handle_del_all(MSG_BUF *pmsg_buf)
672{
673 ALARM_T *pTmp, *pTmp1;
674
675 update_timer_remain(0);
676 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
677 if (pTmp->wakeup == 0) {
678 list_del(&pTmp->list);
679 free(pTmp);
680 }
681 }
682 alarm_restart(&rtc_timer_list);
683
684 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
685 if (pTmp->wakeup == 0) {
686 list_del(&pTmp->list);
687 free(pTmp);
688 }
689 }
690 alarm_restart(&rtc_alarm_list);
691}
692
693/********************************************
694 * ¹¦ÄÜ:sntpͬ²½³É¹¦ºó£¬¸üÐÂalarm_list
695 *******************************************/
696static void update_alarm()
697{
698 ALARM_T *pTmp, *pTmp1;
699 //long sec = get_RtcTime();
700 long sec = time(0);
701 RTC_DATA_BUF buf = {0};
702
703#ifdef PRODUCT_NOT_USE_RTC
704 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list) {
705 buf.module = pTmp->rtc_id;
706 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
707 if (pTmp->interval + pTmp->time_val < sec) {
708 list_del(&pTmp->list);
709 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));
710 free(pTmp);
711 } else {
712 pTmp->remain_time = pTmp->interval + pTmp->time_val - sec;
713 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);
714 }
715 }
716 alarm_restart(&rtc_timer_list);
717#else
718 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
719 buf.module = pTmp->rtc_id;
720 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
721 if (pTmp->interval + pTmp->time_val < sec) {
722 list_del(&pTmp->list);
723 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));
724 free(pTmp);
725 }
726 }
727 alarm_restart(&rtc_alarm_list);
728#endif
729 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: update_alarm exit cur_rtc_time: % ld.\n", sec);
730}
731
732/********************************************
733 * ¹¦ÄÜ:¸üж¨Ê±Æ÷Ê£Óàʱ¼ä
734 *******************************************/
735static void alarm_adjust_value(int time)
736{
737 ALARM_T * temp;
738 list_for_each_entry(temp, &rtc_timer_list, list) {
739 temp->remain_time -= time;
740 }
741}
742
743/********************************************
744 *¹¦ÄÜ: ɾ³ýÎÞЧ¶¨Ê±Æ÷
745 *******************************************/
746static void alarm_fix_value(struct list_head *rtc_list)
747{
748 ALARM_T *pTmp, *pTmp1;
749 RTC_DATA_BUF buf = {0};
750 int wakeup = 0;
751
752 if (rtc_list == &rtc_timer_list) {
753 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
754 if (pTmp->remain_time <= 0) {
755 buf.module = pTmp->rtc_id;
756 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
757 list_del(&pTmp->list);
758 free(pTmp);
759 }
760 }
761 } else if (rtc_list == &rtc_alarm_list) {
762 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
763 if (pTmp->time_val + pTmp->interval <= (time(0) + g_time_offset)) {
764 if(pTmp->wakeup)
765 wakeup = pTmp->wakeup;
766
767 buf.module = pTmp->rtc_id;
768 buf.time_val = pTmp->time_val + pTmp->interval;
769 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
770 list_del(&pTmp->list);
771 free(pTmp);
772 }
773 }
774
775 if(wakeup)
776 save_rtc_alarm();
777 }
778 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_fix_value exit.\n");
779}
780
781#ifdef PRODUCT_NOT_USE_RTC
782/*¶ÔӦûÓÐrtcµÄ²úÆ·£¬rtc¶¨Ê±Æ÷ÊÊÅäµ½softtimerµÄ»Øµ÷º¯Êý*/
783static void rtc_set_softtimer_cb(void *param)
784{
785 int ret = -1;
786
787 slog(NET_PRINT, SLOG_ERR, "rtc-service: rtc_adapt_softtimer_cb \n");
788
789 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, 0, 0);;
790 if (ret != 0) {
791 slog(MISC_PRINT, SLOG_ERR, "rtc-service: set rtc timer send msg failed \n");
792 }
793 return 0;
794}
795#endif
796
797/********************************************
798 * ¹¦ÄÜ:ÖØÆô¶¨Ê±Æ÷£¬Ã»ÓÐÔò¹Ø±Õ¶¨Ê±Æ÷
799 *******************************************/
800static void alarm_restart(struct list_head *rtc_list)
801{
802 int ret = 0;
803 //struct tm *p_tm = 0;
804 struct tm ret_tm = {0};
805 struct rtc_wkalrm alarm = {0};
806 long tVal = 0;
807 long remain = -1;
808 ALARM_T *ptmp = NULL;
809 long cur_rtc_time = time(0);
810 RTC_DATA_BUF buf = {0};
811NEXT:
812 if (!list_empty(rtc_list)) {
813 ptmp = list_entry(rtc_list->next, ALARM_T, list);
814 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart enter.\n");
815 memset(&buf, 0, sizeof(RTC_DATA_BUF));
816 if (ptmp->is_utc == 0) {
817#ifdef PRODUCT_NOT_USE_RTC
818 g_set_softtime = time(NULL);
819 sc_timer_delete(RTC_USE_SOFTTIMER_ID);
820 ret = sc_timer_create(RTC_USE_SOFTTIMER_ID, TIMER_FLAG_ONCE, ptmp->remain_time*1000, rtc_set_softtimer_cb, NULL);
821#else
822 ret = ioctl(g_rtc_fd, ZX234290_SET_TIMER, ptmp->remain_time);
823#endif
824 if (ret != 0) {
825 slog(RTC_PRINT, SLOG_ERR, "rtc-service: alarm_restart timer: set failed, ret:%d \n", ret);
826 return;
827 }
828 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);
829 } else {
830 tVal = ptmp->interval + ptmp->time_val;
831 remain = tVal - cur_rtc_time;
832 if (remain <= g_time_offset) {
833 //¹ýʱ¶¨Ê±Æ÷¶ªÆú´¦Àí£¬Èç¹û¶àÄ£¿éÕýºÃÉèµÄ»ù±¾ÔÚͬһ¸öʱ¼äµã£¬ºóÕ߻ᱻ¶ªÆú£¬¿¼ÂÇÐ޸ijÉÖ±½Ó֪ͨģ¿éʱ¼äµ½
834 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);
835 buf.module = ptmp->rtc_id;
836 buf.time_val = ptmp->time_val + ptmp->interval;
837 ipc_send_message2(MODULE_ID_RTC_SERVICE, ptmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
838 list_del(&ptmp->list);
839 free(ptmp);
840 ptmp = NULL;
841 goto NEXT;
842 }
843
844 //localtime_r(&tVal, &ret_tm);
845 gmtime_r(&tVal, &ret_tm);
846 memcpy(&alarm.time, &ret_tm, sizeof(struct rtc_time));
847 alarm.enabled = 1;
848
849 /*Ê×ÏÈ´ò¿ªalarm ÖжÏ*/
850 ret = ioctl(g_rtc_fd, RTC_AIE_ON);
851 if (ret != 0) {
852 slog(RTC_PRINT, SLOG_ERR, "rtc-service: open alarm failed, ret:%d\n", ret);
853 return;
854 }
855
856 /*ÉèÖÃalarm ʱ¼ä*/
857 ret = ioctl(g_rtc_fd, RTC_WKALM_SET, &alarm);
858 if (ret != 0) {
859 slog(RTC_PRINT, SLOG_ERR, "rtc-service: set alarm failed, ret:%d\n", ret);
860 return;
861 }
862 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);
863 }
864 } else {
865 if (rtc_list == &rtc_timer_list) {
866 //¹Ø±Õrtc time, Éè¼ä¸ôʱ¼äΪ0
867 ret = ioctl(g_rtc_fd, ZX234290_TIMER_ENABLED, 0);
868 if (ret != 0) {
869 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable timer failed, ret:%d\n", ret);
870 return;
871 }
872 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable timer ok \n");
873 } else {
874 /*¹Ø±Õalarm ÖжÏ*/
875 ret = ioctl(g_rtc_fd, RTC_AIE_OFF);
876 if (ret != 0) {
877 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable alarm failed, ret:%d\n", ret);
878 return;
879 }
880 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable alarm ok \n");
881 }
882 }
883 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart exit.\n");
884}
885
886static void rtc_set_local_time(MSG_BUF *msg)
887{
888 struct tm set_tm = {0};
889 struct timeval time_tv;
890
891 slog(RTC_PRINT, SLOG_DEBUG, "rtc_set_local_time, %d, %d \n", sizeof(struct tm), msg->usDataLen);
892
893 memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen);
894
895 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n",
896 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);
897
898 time_tv.tv_sec = mktime(&set_tm);
899 if(time_tv.tv_sec < 0)
900 {
901
902 slog(RTC_PRINT, SLOG_DEBUG, "mktime fail, set time_tv.tv_sec to 0 \n");
903 time_tv.tv_sec = 0;
904 }
905 time_tv.tv_usec = 0;
906
907 if (0 != settimeofday(&time_tv, NULL))
908 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time failed \n");
909}
910
911/********************************************
912 * process_msg
913 * handle the specified MSG.
914 *******************************************/
915static int process_msg(MSG_BUF *pmsg_buf)
916{
917 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
918 unsigned short req_cmd = 0;
919
920 switch (cmd) {
921 case RTC_MSG_GET_TIME:
922 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_GET_TIME, src:0x%x.\n", pmsg_buf->src_id);
923 handle_get_time(pmsg_buf);
924 break;
925 case RTC_MSG_SET_TIME:
926 security_log(pmsg_buf->src_id,"set time");
927 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_SET_TIME, src:0x%x.\n", pmsg_buf->src_id);
928 handle_set_time(pmsg_buf);
929 update_alarm();
930 break;
xf.li742dd022023-06-08 01:43:32 -0700931 case RTC_MSG_SNTP_UPDATE:
932 handle_sntp_update(pmsg_buf);
933 break;
lh9ed821d2023-04-07 01:36:19 -0700934 case RTC_MSG_ADD_ALARM:
935 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);
936 //Ìí¼ÓrtcǰÏÈɾ³ý´Ërtc_idÒÑÌí¼Ó¼Ç¼£¬Ã¿¸örtc_idÖ»±£Áô1¸ö¶¨Ê±Æ÷
937 if(0 == g_clean_flag){
938 update_timer_remain(0);
939 handle_alarm_del(pmsg_buf);
940 handle_alarm_add(pmsg_buf);
941 }
942 else{
943 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: g_clean_flag is nonzero:0x%x, dont add alarm.\n", g_clean_flag);
944 }
945
946 //wifi´¦ÀíÍêǰ²»ÄÜÐÝÃߣ¬ÔÝʱʹÓÃÑÓÊ±Ëø£¬´ý·½°¸×鏸³ö·½°¸ºóÔÙµ÷Õû
947 //if (pmsg_buf->usSourceModuleID == MODULE_ID_WDT)
948 //write_lockfile("/sys/power/wake_unlock", "rtc_io_time_timeout_lock");
949
950 break;
951 case RTC_MSG_DEL_ALARM:
952 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALARM, src:0x%x.\n", pmsg_buf->src_id);
953 update_timer_remain(0);
954 handle_alarm_del(pmsg_buf);
xf.libdd93d52023-05-12 07:10:14 -0700955 send_ok_del_rsp(pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -0700956 alarm_restart(&rtc_timer_list);
957 alarm_restart(&rtc_alarm_list);
958 break;
959 case RTC_MSG_ALARM_TIMEOUT:
960 case RTC_MSG_TIMER_TIMEOUT:
961 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_TIMEOUT, src:0x%x, cmd:0x%x.\n", pmsg_buf->src_id, cmd);
962 handle_alarm_timeout(pmsg_buf);
963 break;
964 case RTC_MSG_DEL_ALL:
965 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
966 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
967 if(MSG_CMD_RESET_RSP == req_cmd || MSG_CMD_POWEROFF_RSP == req_cmd || MSG_CMD_RESTART_RSP == req_cmd){
968 g_clean_flag = req_cmd;
969 }
970 handle_del_all(pmsg_buf);
971 save_rtc_alarm();
972 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
973 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
974 break;
975
976 default:
977 break;
978 }
979 return 0;
980}
981
982static int rtc_service_sock_init()
983{
984 int sockfd = 0;
985 int ret;
986 struct sockaddr_nl snl;
987
988 bzero(&snl, sizeof(struct sockaddr_nl));
989 snl.nl_family = AF_NETLINK;
990 snl.nl_pid = getpid();
991 snl.nl_groups = 1;
992
993 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
994 perror ("signal");
995
996 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
997 if (sockfd < 0)
998 {
999 slog(RTC_PRINT,SLOG_ERR, "create rtc service socket failed!\n");
1000 return -1;
1001 }
1002// setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
1003
1004 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
1005 if (ret < 0)
1006 {
1007 slog(RTC_PRINT,SLOG_ERR, "rtc service socket bind fail!\n");
1008 close(sockfd);
1009 return -1;
1010 }
1011
1012 return sockfd;
1013}
1014//change@/devices/platform/zx29_i2c.0/i2c-0/0-0012/zx234290-rtc
1015static int rtc_time_msg_parse(const char *msg, int msglen)
1016{
1017 int offlen = 0;
1018 const char *keys = NULL;
1019 char* timeStr = NULL;
1020 char *pSec = NULL;
1021 slog(RTC_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc \n", __func__, __LINE__);
1022
1023 offlen += strlen((char *)msg) + 1;
1024 while (offlen <= msglen) {
1025 keys = (char *)(msg + offlen);
1026 //slog(NET_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc%s, %d\n", __func__, __LINE__, keys, offlen);
1027 if (strncmp(keys, RTC_ALARM_TIMEOUT_MSG, strlen(RTC_ALARM_TIMEOUT_MSG)) == 0)
1028 {
1029#if 1 // cov M DEADCODE
1030 pSec = strstr(keys, "sec:");
1031 if (pSec == NULL) {
1032 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
1033 return -1;
1034 }
1035 timeStr = pSec + strlen("sec:");
1036
1037#else
1038 timeStr = strstr(keys, "sec:") + strlen("sec:");
1039 if (timeStr == NULL) {
1040 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
1041 return -1;
1042 }
1043#endif
1044 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_ALARM_TIMEOUT, strlen(timeStr), timeStr, 0);
1045 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc alarm timeout \n");
1046 return 0;
1047 }
1048 else if (strncmp(keys, RTC_TIMER_TIMEOUT_MSG, strlen(keys)) == 0)
1049 {
1050 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, NULL, 0);
1051 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc timer timeout \n");
1052 return 0;
1053 }
1054 offlen += strlen((char *)keys) + 1;
1055 }
1056 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event not supported \n");
1057 return -1;
1058}
1059
1060/********************************************
1061 * rtc_service_thread_process
1062 * looper for processing zx234290-rtc msg.
1063 *******************************************/
1064void rtc_event_process(void)
1065{
1066 int rtc_service_fd = 0;
1067 fd_set readfds;
1068 int maxfd = 0;
1069 char buf[RTC_MSG_BUFFER_SIZE] = {0};
1070 int len = 0;
1071 int ret = 0;
1072 prctl(PR_SET_NAME, "rtceventhandle", 0, 0, 0);
1073 rtc_service_fd = rtc_service_sock_init();
1074 if(rtc_service_fd < 0)
1075 {
1076 slog(RTC_PRINT,SLOG_ERR, "rtc service socket init fail!\n");
1077 return ;
1078 }
1079
1080 maxfd = rtc_service_fd;
1081 while (1)
1082 {
1083 FD_ZERO(&readfds);
1084 FD_SET(rtc_service_fd, &readfds);
1085
1086 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1087 if (ret == -1 && errno == EINTR)
1088 continue;
1089
1090 if(FD_ISSET(rtc_service_fd, &readfds))
1091 {
1092 memset(buf, 0, sizeof(buf));
1093 len = recv(rtc_service_fd, &buf, sizeof(buf), 0);
1094 if(len <= 0)
1095 {
1096 slog(RTC_PRINT,SLOG_ERR, "rtc service socket recv msg fail!\n");
1097 continue;
1098 }
1099
1100 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
1101 if (strstr(buf, "zx234290-rtc")){
1102 rtc_time_msg_parse(buf, len);
1103 }
1104 continue;
1105 }
1106 }
1107}
1108
1109/********************************************
1110 * msg_looper
1111 * main looper for processing MSG.
1112 *******************************************/
1113static void msg_looper(void)
1114{
1115 int ret = 0;
1116 int msg_id = 0;
1117 MSG_BUF msg_buf = {0};
1118// key_t msgkey = get_msg_key();
1119
1120 msg_id = msgget(MODULE_ID_RTC_SERVICE, IPC_CREAT | 0600);
1121
1122 memset(&msg_buf, 0, sizeof(msg_buf));
1123
1124 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper enter(msgid=%d)&waiting...\n", msg_id);
1125 while (1) {
1126 ret = msgrcv(msg_id, &msg_buf, sizeof(MSG_BUF) - sizeof(long), 0, 0);
1127 wake_rtc_lock();
1128 if (ret <= 0) {
1129 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper recv msg break(%d)\n", ret);
1130 wake_rtc_unlock();
1131 continue;
1132 }
1133 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);
1134
1135 process_msg(&msg_buf);
1136
1137 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
1138 wake_rtc_unlock();
1139 }
1140}
1141
1142static void print_rtc_list_info(void)
1143{
1144 ALARM_T *temp = NULL;
1145 int remain_time;
1146
1147 if(ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time) < 0) // cov M CHECKED_RETURN
1148 {
1149 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
1150 }
1151
1152
1153 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc remain_time :%ld \n", remain_time);
1154
1155 if (!list_empty(&rtc_timer_list)) {
1156 list_for_each_entry(temp, &rtc_timer_list, list) {
1157 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",
1158 temp->rtc_id,
1159 temp->module_id,
1160 temp->time_val,
1161 temp->remain_time,
1162 temp->cpu);
1163 }
1164 }
1165 else {
1166 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_timer_list is empty!");
1167 }
1168
1169 if (!list_empty(&rtc_alarm_list)) {
1170 list_for_each_entry(temp, &rtc_alarm_list, list) {
1171 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, \
1172 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",
1173 temp->rtc_id,
1174 temp->module_id,
1175 temp->time_val,
1176 temp->remain_time,
1177 temp->cpu,
1178 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);
1179 }
1180 }
1181 else {
1182 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_alarm_list is empty!");
1183 }
1184}
1185
1186/* ÐźŴ¦Àí*/
1187static void sig_usr(int signo)
1188{
1189#if 0//(APP_OS_TYPE == APP_OS_LINUX)
1190 if (signo == SIGUSR1)
1191 {
1192 slog(RTC_PRINT,SLOG_NORMAL, "rtc-service receive SIGUSR1! \n");
1193 print_rtc_list_info();
1194 }
1195 else
1196 {
1197 slog(RTC_PRINT,SLOG_ERR, "rtc-service receive signo:%d", signo);
1198 }
1199#endif
1200}
1201
1202/*RTCÏûÏ¢½âÎö*/
1203int rtc_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
1204{
1205 if (strstr(msg, "zx234290-rtc")){
1206 rtc_time_msg_parse(msg, msglen);
1207 }
1208 return -1;
1209}
1210
1211/********************************************
1212 * main
1213 * entry of RTC-Service!
1214 *******************************************/
1215int rtc_service_main(int argc, char **argv)
1216{
1217
1218 slog(RTC_PRINT,SLOG_DEBUG, "rtc-service enter...\n");
1219 pthread_t rtcservice_thread_id;
1220 pthread_attr_t attr;
1221 int result = 0;
1222 prctl(PR_SET_NAME, "rtc-service", 0, 0, 0);
1223
1224 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
1225 loglevel_init();
1226
1227 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
1228 {
1229 slog(RTC_PRINT,SLOG_ERR, "rtc-service not catch SIGUSR1 \n");
1230 }
1231
1232 INIT_LIST_HEAD(&rtc_timer_list);
1233 INIT_LIST_HEAD(&rtc_alarm_list);
1234
1235 g_clean_flag = 0;
1236
1237 //rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
1238 if ((g_rtc_fd = open_rtc()) < 0) {
1239 slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
1240 return -1;
1241 }
1242 init_rtc_alarm();
1243/*
1244 pthread_attr_init(&attr);
1245 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1246 result = pthread_create(&rtcservice_thread_id, &attr, rtc_event_process, NULL);
1247 pthread_attr_destroy(&attr);
1248 if(result != 0)
1249 {
1250 slog(RTC_PRINT,SLOG_ERR, "[rtc-service] rtcservice_thread pthread_create faild!\n");
1251 return -1;
1252 }
1253 else
1254 {
1255 printf("[rtc-service] rtcservice_thread pthread_create SUCCESS!\n");
1256 }
1257*/
1258 hotplug_parse_register(DEVICE_TYPE_APP_RTC, rtc_app_msg_parse);
1259 msg_looper();
1260 //klocwork
1261 //close_rtc(g_rtc_fd);
1262
1263 //slog(RTC_PRINT, SLOG_ERR, "rtc-service exit...\n");
1264
1265 return 0;
1266}