blob: f60984d575fd7ae8d7a1bcb155429ee0b86dd8db [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);
lh758261d2023-07-13 05:52:04 -0700707 if(pTmp->is_utc){
lh9ed821d2023-04-07 01:36:19 -0700708 if (pTmp->interval + pTmp->time_val < sec) {
709 list_del(&pTmp->list);
710 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));
711 free(pTmp);
712 } else {
713 pTmp->remain_time = pTmp->interval + pTmp->time_val - sec;
714 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);
715 }
lh758261d2023-07-13 05:52:04 -0700716 }
lh9ed821d2023-04-07 01:36:19 -0700717 }
718 alarm_restart(&rtc_timer_list);
719#else
720 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list) {
721 buf.module = pTmp->rtc_id;
722 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
723 if (pTmp->interval + pTmp->time_val < sec) {
724 list_del(&pTmp->list);
725 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));
726 free(pTmp);
727 }
728 }
729 alarm_restart(&rtc_alarm_list);
730#endif
731 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: update_alarm exit cur_rtc_time: % ld.\n", sec);
732}
733
734/********************************************
735 * ¹¦ÄÜ:¸üж¨Ê±Æ÷Ê£Óàʱ¼ä
736 *******************************************/
737static void alarm_adjust_value(int time)
738{
739 ALARM_T * temp;
740 list_for_each_entry(temp, &rtc_timer_list, list) {
741 temp->remain_time -= time;
742 }
743}
744
745/********************************************
746 *¹¦ÄÜ: ɾ³ýÎÞЧ¶¨Ê±Æ÷
747 *******************************************/
748static void alarm_fix_value(struct list_head *rtc_list)
749{
750 ALARM_T *pTmp, *pTmp1;
751 RTC_DATA_BUF buf = {0};
752 int wakeup = 0;
lh758261d2023-07-13 05:52:04 -0700753 int ret =0;
754 enum RTC_MSG_CMD cmd = 0;
lh9ed821d2023-04-07 01:36:19 -0700755
756 if (rtc_list == &rtc_timer_list) {
757 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
758 if (pTmp->remain_time <= 0) {
759 buf.module = pTmp->rtc_id;
lh758261d2023-07-13 05:52:04 -0700760 if(pTmp->cpu == RTC_CPU_CAP)
761 {
762 buf.srcModule = pTmp->module_id;
763 buf.cpu = RTC_CPU_AP;
764 buf.is_utc = pTmp->is_utc;
765 cmd = RTC_MSG_TIMER_TIMEOUT;
766 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, buf.is_utc);
767 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
768 if (ret != 0)
769 {
770 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n",ret);;
771 }
772 }
773 else
774 {
775 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
776 }
lh9ed821d2023-04-07 01:36:19 -0700777 list_del(&pTmp->list);
778 free(pTmp);
779 }
780 }
781 } else if (rtc_list == &rtc_alarm_list) {
782 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list) {
783 if (pTmp->time_val + pTmp->interval <= (time(0) + g_time_offset)) {
784 if(pTmp->wakeup)
785 wakeup = pTmp->wakeup;
786
787 buf.module = pTmp->rtc_id;
lh758261d2023-07-13 05:52:04 -0700788 buf.time_val = pTmp->time_val + pTmp->interval;
789 if(pTmp->cpu == RTC_CPU_CAP)
790 {
791 buf.srcModule = pTmp->module_id;
792 buf.cpu = RTC_CPU_AP;
793 buf.is_utc = pTmp->is_utc;
794 cmd = RTC_MSG_ALARM_TIMEOUT;
795 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, buf.is_utc);
796 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
797 if (ret != 0)
798 {
799 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n",ret);;
800 }
801 }
802 else
803 {
804 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
805 }
lh9ed821d2023-04-07 01:36:19 -0700806 list_del(&pTmp->list);
807 free(pTmp);
808 }
809 }
810
811 if(wakeup)
812 save_rtc_alarm();
813 }
814 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_fix_value exit.\n");
815}
816
817#ifdef PRODUCT_NOT_USE_RTC
818/*¶ÔӦûÓÐrtcµÄ²úÆ·£¬rtc¶¨Ê±Æ÷ÊÊÅäµ½softtimerµÄ»Øµ÷º¯Êý*/
819static void rtc_set_softtimer_cb(void *param)
820{
821 int ret = -1;
822
823 slog(NET_PRINT, SLOG_ERR, "rtc-service: rtc_adapt_softtimer_cb \n");
824
825 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, 0, 0);;
826 if (ret != 0) {
827 slog(MISC_PRINT, SLOG_ERR, "rtc-service: set rtc timer send msg failed \n");
828 }
829 return 0;
830}
831#endif
832
833/********************************************
834 * ¹¦ÄÜ:ÖØÆô¶¨Ê±Æ÷£¬Ã»ÓÐÔò¹Ø±Õ¶¨Ê±Æ÷
835 *******************************************/
836static void alarm_restart(struct list_head *rtc_list)
837{
838 int ret = 0;
839 //struct tm *p_tm = 0;
840 struct tm ret_tm = {0};
841 struct rtc_wkalrm alarm = {0};
842 long tVal = 0;
843 long remain = -1;
844 ALARM_T *ptmp = NULL;
845 long cur_rtc_time = time(0);
846 RTC_DATA_BUF buf = {0};
847NEXT:
848 if (!list_empty(rtc_list)) {
849 ptmp = list_entry(rtc_list->next, ALARM_T, list);
850 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart enter.\n");
851 memset(&buf, 0, sizeof(RTC_DATA_BUF));
852 if (ptmp->is_utc == 0) {
853#ifdef PRODUCT_NOT_USE_RTC
854 g_set_softtime = time(NULL);
855 sc_timer_delete(RTC_USE_SOFTTIMER_ID);
856 ret = sc_timer_create(RTC_USE_SOFTTIMER_ID, TIMER_FLAG_ONCE, ptmp->remain_time*1000, rtc_set_softtimer_cb, NULL);
857#else
858 ret = ioctl(g_rtc_fd, ZX234290_SET_TIMER, ptmp->remain_time);
859#endif
860 if (ret != 0) {
861 slog(RTC_PRINT, SLOG_ERR, "rtc-service: alarm_restart timer: set failed, ret:%d \n", ret);
862 return;
863 }
864 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);
865 } else {
866 tVal = ptmp->interval + ptmp->time_val;
867 remain = tVal - cur_rtc_time;
868 if (remain <= g_time_offset) {
869 //¹ýʱ¶¨Ê±Æ÷¶ªÆú´¦Àí£¬Èç¹û¶àÄ£¿éÕýºÃÉèµÄ»ù±¾ÔÚͬһ¸öʱ¼äµã£¬ºóÕ߻ᱻ¶ªÆú£¬¿¼ÂÇÐ޸ijÉÖ±½Ó֪ͨģ¿éʱ¼äµ½
870 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);
871 buf.module = ptmp->rtc_id;
872 buf.time_val = ptmp->time_val + ptmp->interval;
873 ipc_send_message2(MODULE_ID_RTC_SERVICE, ptmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), (unsigned char*)&buf, 0);
874 list_del(&ptmp->list);
875 free(ptmp);
876 ptmp = NULL;
877 goto NEXT;
878 }
879
880 //localtime_r(&tVal, &ret_tm);
881 gmtime_r(&tVal, &ret_tm);
882 memcpy(&alarm.time, &ret_tm, sizeof(struct rtc_time));
883 alarm.enabled = 1;
884
885 /*Ê×ÏÈ´ò¿ªalarm ÖжÏ*/
886 ret = ioctl(g_rtc_fd, RTC_AIE_ON);
887 if (ret != 0) {
888 slog(RTC_PRINT, SLOG_ERR, "rtc-service: open alarm failed, ret:%d\n", ret);
889 return;
890 }
891
892 /*ÉèÖÃalarm ʱ¼ä*/
893 ret = ioctl(g_rtc_fd, RTC_WKALM_SET, &alarm);
894 if (ret != 0) {
895 slog(RTC_PRINT, SLOG_ERR, "rtc-service: set alarm failed, ret:%d\n", ret);
896 return;
897 }
898 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);
899 }
900 } else {
901 if (rtc_list == &rtc_timer_list) {
902 //¹Ø±Õrtc time, Éè¼ä¸ôʱ¼äΪ0
903 ret = ioctl(g_rtc_fd, ZX234290_TIMER_ENABLED, 0);
904 if (ret != 0) {
905 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable timer failed, ret:%d\n", ret);
906 return;
907 }
908 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable timer ok \n");
909 } else {
910 /*¹Ø±Õalarm ÖжÏ*/
911 ret = ioctl(g_rtc_fd, RTC_AIE_OFF);
912 if (ret != 0) {
913 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable alarm failed, ret:%d\n", ret);
914 return;
915 }
916 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable alarm ok \n");
917 }
918 }
919 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart exit.\n");
920}
921
922static void rtc_set_local_time(MSG_BUF *msg)
923{
924 struct tm set_tm = {0};
925 struct timeval time_tv;
926
927 slog(RTC_PRINT, SLOG_DEBUG, "rtc_set_local_time, %d, %d \n", sizeof(struct tm), msg->usDataLen);
928
929 memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen);
930
931 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n",
932 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);
933
934 time_tv.tv_sec = mktime(&set_tm);
935 if(time_tv.tv_sec < 0)
936 {
937
938 slog(RTC_PRINT, SLOG_DEBUG, "mktime fail, set time_tv.tv_sec to 0 \n");
939 time_tv.tv_sec = 0;
940 }
941 time_tv.tv_usec = 0;
942
943 if (0 != settimeofday(&time_tv, NULL))
944 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time failed \n");
945}
946
947/********************************************
948 * process_msg
949 * handle the specified MSG.
950 *******************************************/
951static int process_msg(MSG_BUF *pmsg_buf)
952{
953 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
954 unsigned short req_cmd = 0;
955
956 switch (cmd) {
957 case RTC_MSG_GET_TIME:
958 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_GET_TIME, src:0x%x.\n", pmsg_buf->src_id);
959 handle_get_time(pmsg_buf);
960 break;
961 case RTC_MSG_SET_TIME:
962 security_log(pmsg_buf->src_id,"set time");
963 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_SET_TIME, src:0x%x.\n", pmsg_buf->src_id);
964 handle_set_time(pmsg_buf);
965 update_alarm();
966 break;
xf.li742dd022023-06-08 01:43:32 -0700967 case RTC_MSG_SNTP_UPDATE:
968 handle_sntp_update(pmsg_buf);
969 break;
lh9ed821d2023-04-07 01:36:19 -0700970 case RTC_MSG_ADD_ALARM:
971 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);
972 //Ìí¼ÓrtcǰÏÈɾ³ý´Ërtc_idÒÑÌí¼Ó¼Ç¼£¬Ã¿¸örtc_idÖ»±£Áô1¸ö¶¨Ê±Æ÷
973 if(0 == g_clean_flag){
974 update_timer_remain(0);
975 handle_alarm_del(pmsg_buf);
976 handle_alarm_add(pmsg_buf);
977 }
978 else{
979 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: g_clean_flag is nonzero:0x%x, dont add alarm.\n", g_clean_flag);
980 }
981
982 //wifi´¦ÀíÍêǰ²»ÄÜÐÝÃߣ¬ÔÝʱʹÓÃÑÓÊ±Ëø£¬´ý·½°¸×鏸³ö·½°¸ºóÔÙµ÷Õû
983 //if (pmsg_buf->usSourceModuleID == MODULE_ID_WDT)
984 //write_lockfile("/sys/power/wake_unlock", "rtc_io_time_timeout_lock");
985
986 break;
987 case RTC_MSG_DEL_ALARM:
988 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALARM, src:0x%x.\n", pmsg_buf->src_id);
989 update_timer_remain(0);
990 handle_alarm_del(pmsg_buf);
xf.libdd93d52023-05-12 07:10:14 -0700991 send_ok_del_rsp(pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -0700992 alarm_restart(&rtc_timer_list);
993 alarm_restart(&rtc_alarm_list);
994 break;
995 case RTC_MSG_ALARM_TIMEOUT:
996 case RTC_MSG_TIMER_TIMEOUT:
997 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_TIMEOUT, src:0x%x, cmd:0x%x.\n", pmsg_buf->src_id, cmd);
998 handle_alarm_timeout(pmsg_buf);
999 break;
1000 case RTC_MSG_DEL_ALL:
1001 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
1002 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
1003 if(MSG_CMD_RESET_RSP == req_cmd || MSG_CMD_POWEROFF_RSP == req_cmd || MSG_CMD_RESTART_RSP == req_cmd){
1004 g_clean_flag = req_cmd;
1005 }
1006 handle_del_all(pmsg_buf);
1007 save_rtc_alarm();
1008 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
1009 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
1010 break;
1011
1012 default:
1013 break;
1014 }
1015 return 0;
1016}
1017
1018static int rtc_service_sock_init()
1019{
1020 int sockfd = 0;
1021 int ret;
1022 struct sockaddr_nl snl;
1023
1024 bzero(&snl, sizeof(struct sockaddr_nl));
1025 snl.nl_family = AF_NETLINK;
1026 snl.nl_pid = getpid();
1027 snl.nl_groups = 1;
1028
1029 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1030 perror ("signal");
1031
1032 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1033 if (sockfd < 0)
1034 {
1035 slog(RTC_PRINT,SLOG_ERR, "create rtc service socket failed!\n");
1036 return -1;
1037 }
1038// setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
1039
1040 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
1041 if (ret < 0)
1042 {
1043 slog(RTC_PRINT,SLOG_ERR, "rtc service socket bind fail!\n");
1044 close(sockfd);
1045 return -1;
1046 }
1047
1048 return sockfd;
1049}
1050//change@/devices/platform/zx29_i2c.0/i2c-0/0-0012/zx234290-rtc
1051static int rtc_time_msg_parse(const char *msg, int msglen)
1052{
1053 int offlen = 0;
1054 const char *keys = NULL;
1055 char* timeStr = NULL;
1056 char *pSec = NULL;
1057 slog(RTC_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc \n", __func__, __LINE__);
1058
1059 offlen += strlen((char *)msg) + 1;
1060 while (offlen <= msglen) {
1061 keys = (char *)(msg + offlen);
1062 //slog(NET_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc%s, %d\n", __func__, __LINE__, keys, offlen);
1063 if (strncmp(keys, RTC_ALARM_TIMEOUT_MSG, strlen(RTC_ALARM_TIMEOUT_MSG)) == 0)
1064 {
1065#if 1 // cov M DEADCODE
1066 pSec = strstr(keys, "sec:");
1067 if (pSec == NULL) {
1068 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
1069 return -1;
1070 }
1071 timeStr = pSec + strlen("sec:");
1072
1073#else
1074 timeStr = strstr(keys, "sec:") + strlen("sec:");
1075 if (timeStr == NULL) {
1076 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event no sec \n");
1077 return -1;
1078 }
1079#endif
1080 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_ALARM_TIMEOUT, strlen(timeStr), timeStr, 0);
1081 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc alarm timeout \n");
1082 return 0;
1083 }
1084 else if (strncmp(keys, RTC_TIMER_TIMEOUT_MSG, strlen(keys)) == 0)
1085 {
1086 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, NULL, 0);
1087 slog(RTC_PRINT,SLOG_NORMAL, "zx234290-rtc timer timeout \n");
1088 return 0;
1089 }
1090 offlen += strlen((char *)keys) + 1;
1091 }
1092 slog(RTC_PRINT,SLOG_ERR, "zx234290-rtc event not supported \n");
1093 return -1;
1094}
1095
1096/********************************************
1097 * rtc_service_thread_process
1098 * looper for processing zx234290-rtc msg.
1099 *******************************************/
1100void rtc_event_process(void)
1101{
1102 int rtc_service_fd = 0;
1103 fd_set readfds;
1104 int maxfd = 0;
1105 char buf[RTC_MSG_BUFFER_SIZE] = {0};
1106 int len = 0;
1107 int ret = 0;
1108 prctl(PR_SET_NAME, "rtceventhandle", 0, 0, 0);
1109 rtc_service_fd = rtc_service_sock_init();
1110 if(rtc_service_fd < 0)
1111 {
1112 slog(RTC_PRINT,SLOG_ERR, "rtc service socket init fail!\n");
1113 return ;
1114 }
1115
1116 maxfd = rtc_service_fd;
1117 while (1)
1118 {
1119 FD_ZERO(&readfds);
1120 FD_SET(rtc_service_fd, &readfds);
1121
1122 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1123 if (ret == -1 && errno == EINTR)
1124 continue;
1125
1126 if(FD_ISSET(rtc_service_fd, &readfds))
1127 {
1128 memset(buf, 0, sizeof(buf));
1129 len = recv(rtc_service_fd, &buf, sizeof(buf), 0);
1130 if(len <= 0)
1131 {
1132 slog(RTC_PRINT,SLOG_ERR, "rtc service socket recv msg fail!\n");
1133 continue;
1134 }
1135
1136 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
1137 if (strstr(buf, "zx234290-rtc")){
1138 rtc_time_msg_parse(buf, len);
1139 }
1140 continue;
1141 }
1142 }
1143}
1144
1145/********************************************
1146 * msg_looper
1147 * main looper for processing MSG.
1148 *******************************************/
1149static void msg_looper(void)
1150{
1151 int ret = 0;
1152 int msg_id = 0;
1153 MSG_BUF msg_buf = {0};
1154// key_t msgkey = get_msg_key();
1155
1156 msg_id = msgget(MODULE_ID_RTC_SERVICE, IPC_CREAT | 0600);
1157
1158 memset(&msg_buf, 0, sizeof(msg_buf));
1159
1160 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper enter(msgid=%d)&waiting...\n", msg_id);
1161 while (1) {
1162 ret = msgrcv(msg_id, &msg_buf, sizeof(MSG_BUF) - sizeof(long), 0, 0);
1163 wake_rtc_lock();
1164 if (ret <= 0) {
1165 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper recv msg break(%d)\n", ret);
1166 wake_rtc_unlock();
1167 continue;
1168 }
1169 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);
1170
1171 process_msg(&msg_buf);
1172
1173 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
1174 wake_rtc_unlock();
1175 }
1176}
1177
1178static void print_rtc_list_info(void)
1179{
1180 ALARM_T *temp = NULL;
1181 int remain_time;
1182
1183 if(ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time) < 0) // cov M CHECKED_RETURN
1184 {
1185 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
1186 }
1187
1188
1189 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc remain_time :%ld \n", remain_time);
1190
1191 if (!list_empty(&rtc_timer_list)) {
1192 list_for_each_entry(temp, &rtc_timer_list, list) {
1193 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",
1194 temp->rtc_id,
1195 temp->module_id,
1196 temp->time_val,
1197 temp->remain_time,
1198 temp->cpu);
1199 }
1200 }
1201 else {
1202 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_timer_list is empty!");
1203 }
1204
1205 if (!list_empty(&rtc_alarm_list)) {
1206 list_for_each_entry(temp, &rtc_alarm_list, list) {
1207 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, \
1208 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",
1209 temp->rtc_id,
1210 temp->module_id,
1211 temp->time_val,
1212 temp->remain_time,
1213 temp->cpu,
1214 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);
1215 }
1216 }
1217 else {
1218 slog(RTC_PRINT,SLOG_ERR, "rtc-service rtc_alarm_list is empty!");
1219 }
1220}
1221
1222/* ÐźŴ¦Àí*/
1223static void sig_usr(int signo)
1224{
1225#if 0//(APP_OS_TYPE == APP_OS_LINUX)
1226 if (signo == SIGUSR1)
1227 {
1228 slog(RTC_PRINT,SLOG_NORMAL, "rtc-service receive SIGUSR1! \n");
1229 print_rtc_list_info();
1230 }
1231 else
1232 {
1233 slog(RTC_PRINT,SLOG_ERR, "rtc-service receive signo:%d", signo);
1234 }
1235#endif
1236}
1237
1238/*RTCÏûÏ¢½âÎö*/
1239int rtc_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
1240{
1241 if (strstr(msg, "zx234290-rtc")){
1242 rtc_time_msg_parse(msg, msglen);
1243 }
1244 return -1;
1245}
1246
1247/********************************************
1248 * main
1249 * entry of RTC-Service!
1250 *******************************************/
1251int rtc_service_main(int argc, char **argv)
1252{
1253
1254 slog(RTC_PRINT,SLOG_DEBUG, "rtc-service enter...\n");
1255 pthread_t rtcservice_thread_id;
1256 pthread_attr_t attr;
1257 int result = 0;
1258 prctl(PR_SET_NAME, "rtc-service", 0, 0, 0);
1259
1260 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
1261 loglevel_init();
1262
1263 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
1264 {
1265 slog(RTC_PRINT,SLOG_ERR, "rtc-service not catch SIGUSR1 \n");
1266 }
1267
1268 INIT_LIST_HEAD(&rtc_timer_list);
1269 INIT_LIST_HEAD(&rtc_alarm_list);
1270
1271 g_clean_flag = 0;
1272
1273 //rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
1274 if ((g_rtc_fd = open_rtc()) < 0) {
1275 slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
1276 return -1;
1277 }
1278 init_rtc_alarm();
1279/*
1280 pthread_attr_init(&attr);
1281 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1282 result = pthread_create(&rtcservice_thread_id, &attr, rtc_event_process, NULL);
1283 pthread_attr_destroy(&attr);
1284 if(result != 0)
1285 {
1286 slog(RTC_PRINT,SLOG_ERR, "[rtc-service] rtcservice_thread pthread_create faild!\n");
1287 return -1;
1288 }
1289 else
1290 {
1291 printf("[rtc-service] rtcservice_thread pthread_create SUCCESS!\n");
1292 }
1293*/
1294 hotplug_parse_register(DEVICE_TYPE_APP_RTC, rtc_app_msg_parse);
1295 msg_looper();
1296 //klocwork
1297 //close_rtc(g_rtc_fd);
1298
1299 //slog(RTC_PRINT, SLOG_ERR, "rtc-service exit...\n");
1300
1301 return 0;
1302}