blob: bda4d93907d49b524a5b05850703b28e962ab261 [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>
xf.lidf7f8ba2024-09-12 23:53:34 -070020#include "pub_debug_info.h"
lh9ed821d2023-04-07 01:36:19 -070021
22/*******************************************************************************
23 * Global variable definitions *
24 ******************************************************************************/
25int g_rtc_fd = 0;
26struct list_head rtc_timer_list;
xf.li9d1a0e12023-09-20 01:43:20 -070027struct list_head rtc_alarm_list;
lh9ed821d2023-04-07 01:36:19 -070028#define RTC_ALARM_TIMEOUT_MSG "PMIC RTC ALARM IRQ COME"
29#define RTC_TIMER_TIMEOUT_MSG "PMIC RTC TIMER IRQ COME"
30
31#ifdef PRODUCT_NOT_USE_RTC
32time_t g_set_softtime;
33#define RTC_USE_SOFTTIMER_ID 300 //µ±Ã»ÓÐrtcʱ£¬Ô­ÏÈʹÓÃrtc¶¨Ê±Æ÷µÄÓÐrtcͳһÊÊÅäµ½softtimerÉÏ£¬Õû¸ö¿ò¼Ü²»±ä
34#endif
35/*******************************************************************************
36 * Local variable definitions *
37 ******************************************************************************/
38static pthread_t rtc_looper_id = 0;
39static int g_clean_flag = 0;
40static int g_time_offset = 2; //ÔÊÐíÎó²î·¶Î§£¬ÊÊÅärtcºÍOSʱÖÓÆµÂʲ»Ò»ÖÂÎÊÌâ
41/*******************************************************************************
42 * Local function implementations *
43 ******************************************************************************/
44static int open_rtc(void);
45static void close_rtc(int fd);
46static void* rtc_looper();
47static int start_rtc_thread();
48static int handle_get_time(MSG_BUF *pmsg_buf);
49static int handle_set_time(MSG_BUF *pmsg_buf);
50static int handle_alarm_add(MSG_BUF *pmsg_buf);
51static int handle_alarm_del(MSG_BUF *pmsg_buf);
52static int handle_alarm_timeout(MSG_BUF *pmsg_buf);
53static int process_msg(MSG_BUF *pmsg_buf);
xf.li6c8fc1e2023-08-12 00:11:09 -070054static int process_msg_rtc_cap(MSG_BUF *pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -070055static void msg_looper(void);
56static void update_timer_remain(int new_remian);
57static void alarm_fix_value(struct list_head *rtc_list);
58static void alarm_restart(struct list_head *rtc_list);
59static void update_alarm();
60static void alarm_adjust_value(int time);
61static long get_RtcTime();
62static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type);
63static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type)
64{
65 time_t tmpTime = 0;
xf.lie31de8b2023-12-26 23:38:58 -080066 time_t tmpTime2 = 0;
xf.li9d1a0e12023-09-20 01:43:20 -070067 if (tmTime == NULL)
68 {
lh9ed821d2023-04-07 01:36:19 -070069 slog(RTC_PRINT, SLOG_ERR, "rtc-service: wrong input param, check it! \n");
70 return ;
71 }
72 tmpTime = timeSec;
xf.li9d1a0e12023-09-20 01:43:20 -070073
74 if (type == CONVERT_TYPE_RTC)
75 {
lh9ed821d2023-04-07 01:36:19 -070076 gmtime_r((time_t*)&tmpTime, tmTime);
77 }
xf.li9d1a0e12023-09-20 01:43:20 -070078 else
79 {
lh9ed821d2023-04-07 01:36:19 -070080 localtime_r((time_t*)&tmpTime, tmTime);
81 }
xf.li9d1a0e12023-09-20 01:43:20 -070082
xf.lie31de8b2023-12-26 23:38:58 -080083 tmpTime2 = mktime(tmTime);
xf.li9d1a0e12023-09-20 01:43:20 -070084 slog(RTC_PRINT, SLOG_DEBUG,
85 "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",
xf.lie31de8b2023-12-26 23:38:58 -080086 tmpTime2, tmTime->tm_year, tmTime->tm_mon, tmTime->tm_hour, tmTime->tm_min, tmTime->tm_sec, tmTime->tm_yday,
xf.li9d1a0e12023-09-20 01:43:20 -070087 tmTime->tm_mday, tmTime->tm_wday);
lh9ed821d2023-04-07 01:36:19 -070088
89}
90
91static long get_RtcTime()
92{
xf.li9d1a0e12023-09-20 01:43:20 -070093 long curRtcTime = 0;
lh9ed821d2023-04-07 01:36:19 -070094 struct rtc_time rtc_tm = {0};
95 struct tm tmp = {0};
xf.li9d1a0e12023-09-20 01:43:20 -070096 if (g_rtc_fd <= 0)
97 {
lh9ed821d2023-04-07 01:36:19 -070098 slog(RTC_PRINT, SLOG_ERR, "rtc-service: g_rtc_fd: %d , invalid fd. \n", g_rtc_fd);
99 return -1;
100 }
xf.li9d1a0e12023-09-20 01:43:20 -0700101
102 if (ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
103 {
104 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
105 }
lh9ed821d2023-04-07 01:36:19 -0700106 tmp.tm_sec = rtc_tm.tm_sec;
xf.li9d1a0e12023-09-20 01:43:20 -0700107 tmp.tm_min = rtc_tm.tm_min;
108 tmp.tm_hour = rtc_tm.tm_hour;
109 tmp.tm_mon = rtc_tm.tm_mon;
110 tmp.tm_year = rtc_tm.tm_year;
111 tmp.tm_mday = rtc_tm.tm_mday;
112 tmp.tm_wday = rtc_tm.tm_wday;
113 tmp.tm_yday = rtc_tm.tm_yday;
114 tmp.tm_isdst = rtc_tm.tm_isdst;
115
lh9ed821d2023-04-07 01:36:19 -0700116 curRtcTime = mktime(&tmp);
117 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: curRtcTime: %ld . \n", curRtcTime);
118 return curRtcTime;
119}
120static void write_lockfile(char *filepath, char *setbuf)
121{
xf.li9d1a0e12023-09-20 01:43:20 -0700122 int f, len = 0;
lh9ed821d2023-04-07 01:36:19 -0700123
xf.li9d1a0e12023-09-20 01:43:20 -0700124 f = open(filepath, O_RDWR);
125 if (f == -1)
126 {
127 return;
128 }
lh9ed821d2023-04-07 01:36:19 -0700129
xf.li9d1a0e12023-09-20 01:43:20 -0700130 len = strlen(setbuf);
131 if (write(f, setbuf, len) != len)
132 {
133 }
134 else
135 {
136 }
137 close(f);
lh9ed821d2023-04-07 01:36:19 -0700138}
139
140
141static void wake_rtc_lock()
142{
xf.li9d1a0e12023-09-20 01:43:20 -0700143 write_lockfile("/sys/power/wake_lock", "rtc_lock");
lh9ed821d2023-04-07 01:36:19 -0700144}
145
146static void wake_rtc_unlock()
147{
xf.li9d1a0e12023-09-20 01:43:20 -0700148 write_lockfile("/sys/power/wake_unlock", "rtc_lock");
lh9ed821d2023-04-07 01:36:19 -0700149}
150
151static void wake_rtc_io_lock()
152{
xf.li9d1a0e12023-09-20 01:43:20 -0700153 write_lockfile("/sys/power/wake_lock", "rtc_io_lock");
lh9ed821d2023-04-07 01:36:19 -0700154}
155
156static void wake_rtc_io_unlock()
157{
xf.li9d1a0e12023-09-20 01:43:20 -0700158 write_lockfile("/sys/power/wake_unlock", "rtc_io_lock");
lh9ed821d2023-04-07 01:36:19 -0700159}
160
161static void wake_timeout_lock(char *lockname, unsigned long interval)
162{
xf.li9d1a0e12023-09-20 01:43:20 -0700163 char cmd[100] = {0};
164 sprintf(cmd, "echo %s %lu > /sys/power/wake_lock", lockname, interval);
165 soft_system(cmd);
lh9ed821d2023-04-07 01:36:19 -0700166}
167
168static int init_rtc_alarm(void)
169{
xf.li9d1a0e12023-09-20 01:43:20 -0700170 FILE* fp = NULL;
171 int gap_start = 0;
172 int gap_end = 0;
173 size_t size = 0;
174 long cur_rtc_time = 0;
175 ALARM_T* iter = NULL;
176 RTC_DATA_BUF buf = {0};
177 int ret = -1;
lh9ed821d2023-04-07 01:36:19 -0700178
xf.li9d1a0e12023-09-20 01:43:20 -0700179 // cur_rtc_time = get_RtcTime();
180 cur_rtc_time = time(0);
181 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "rb")) == NULL)
182 {
183 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
184 return -1;
185 }
lh9ed821d2023-04-07 01:36:19 -0700186
xf.li9d1a0e12023-09-20 01:43:20 -0700187 while (1)
188 {
189 size = fread(&gap_start, sizeof(int), 1, fp);//0xa5a5
190 if (0 == size || gap_start != DATA_S || feof(fp) != 0 )
191 {
192 break;
193 }
lh9ed821d2023-04-07 01:36:19 -0700194
xf.li9d1a0e12023-09-20 01:43:20 -0700195 iter = (ALARM_T*)malloc(sizeof(ALARM_T));
196 if (iter)
197 {
198 memset(iter, 0, sizeof(ALARM_T));
199 }
200 else
201 {
202 fclose(fp);
203 return -1;
204 }
lh9ed821d2023-04-07 01:36:19 -0700205
xf.li9d1a0e12023-09-20 01:43:20 -0700206 size = fread(iter, sizeof(ALARM_T), 1, fp);//struct
207 if (0 == size || feof(fp) != 0) //check the end of the file
208 {
209 memset(iter, 0, sizeof(ALARM_T));//kw
210 free(iter);
211 break;
212 }
lh9ed821d2023-04-07 01:36:19 -0700213
xf.li9d1a0e12023-09-20 01:43:20 -0700214 size = fread(&gap_end, sizeof(int), 1, fp);//0x5a5a
215 if (0 == size || gap_end != DATA_E || feof(fp) != 0)
216 {
217 memset(iter, 0, sizeof(ALARM_T));//kw
218 free(iter);
219 break;
220 }
lh9ed821d2023-04-07 01:36:19 -0700221
xf.li9d1a0e12023-09-20 01:43:20 -0700222 if (iter->interval < 0 || iter->interval > LONG_MAX - 1)
lh9ed821d2023-04-07 01:36:19 -0700223 {
224 iter->interval = 0 ;
225 }
226
227
xf.li9d1a0e12023-09-20 01:43:20 -0700228 if (iter->time_val < 0 || iter->time_val > LONG_MAX - 1)
lh9ed821d2023-04-07 01:36:19 -0700229 {
230 iter->time_val = 0 ;
231 }
232
lh9ed821d2023-04-07 01:36:19 -0700233
xf.li9d1a0e12023-09-20 01:43:20 -0700234 if (cur_rtc_time > iter->interval + iter->time_val)
235 {
236 buf.module = iter->rtc_id;
237 buf.time_val = iter->time_val + iter->interval;
238 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d, cur_rtc_time:%ld \n",
239 iter->module_id, buf.module, iter->is_utc, cur_rtc_time);
240 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, iter->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
241 (unsigned char*)&buf, 0);
242 if (ret != 0)
243 {
244 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected\n");
245 }
246 }
247 list_add_tail(&iter->list, &rtc_alarm_list);
248 if (feof(fp) != 0) //check the end of the file
249 {
250 break;
251 }
252 }
253 fclose(fp);
254
255 return 0;
lh9ed821d2023-04-07 01:36:19 -0700256}
257
258static int save_rtc_alarm(void)
259{
xf.li9d1a0e12023-09-20 01:43:20 -0700260 FILE* fp = NULL;
261 int gap_start = DATA_S;
262 int gap_end = DATA_E;
263 ALARM_T *temp = NULL;
lh9ed821d2023-04-07 01:36:19 -0700264
xf.li9d1a0e12023-09-20 01:43:20 -0700265 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "wb")) == NULL)
266 {
267 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
268 return -1;
269 }
lh9ed821d2023-04-07 01:36:19 -0700270
xf.li9d1a0e12023-09-20 01:43:20 -0700271 if (!list_empty(&rtc_alarm_list))
272 {
273 list_for_each_entry(temp, &rtc_alarm_list, list)
274 {
275 if (temp->wakeup)
276 {
277 fwrite(&gap_start, sizeof(int), 1, fp);
278 fwrite(temp, sizeof(ALARM_T), 1, fp);
279 fwrite(&gap_end, sizeof(int), 1, fp);
280 }
281 }
282 }
lh9ed821d2023-04-07 01:36:19 -0700283
xf.li9d1a0e12023-09-20 01:43:20 -0700284 fflush(fp);
285 if (fsync(fileno(fp)) < 0)
lh9ed821d2023-04-07 01:36:19 -0700286 {
287 // todo: cov M
xf.li9d1a0e12023-09-20 01:43:20 -0700288 }
289 fclose(fp);
lh9ed821d2023-04-07 01:36:19 -0700290
xf.li9d1a0e12023-09-20 01:43:20 -0700291 return 0;
lh9ed821d2023-04-07 01:36:19 -0700292}
293
294/********************************************
295 ¹¦ÄÜ:´ò¿ªrtc
296 *******************************************/
297static int open_rtc(void)
298{
xf.li9d1a0e12023-09-20 01:43:20 -0700299 int fd = 0;
300 fd = open(RTC_DEV_NAME, O_RDWR);
lh9ed821d2023-04-07 01:36:19 -0700301
xf.li9d1a0e12023-09-20 01:43:20 -0700302 if (fd < 0)
303 {
304 slog(RTC_PRINT, SLOG_ERR, "rtc-service: Open %s error!\n", RTC_DEV_NAME);
305 return fd;
306 }
307 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: Open %s OK!\n", RTC_DEV_NAME);
308 return fd;
lh9ed821d2023-04-07 01:36:19 -0700309}
310
311/********************************************
312 ¹¦ÄÜ:¹Ø±Õrtc
313 *******************************************/
314static void close_rtc(int fd)
315{
xf.li9d1a0e12023-09-20 01:43:20 -0700316 if (fd < 0)
317 {
318 return;
319 }
lh9ed821d2023-04-07 01:36:19 -0700320
xf.li9d1a0e12023-09-20 01:43:20 -0700321 close(fd);
lh9ed821d2023-04-07 01:36:19 -0700322}
323
324
325/********************************************
326 * handle_get_time
327 * get RTC time.
328 *******************************************/
329static int handle_get_time(MSG_BUF *pmsg_buf)
330{
xf.li9d1a0e12023-09-20 01:43:20 -0700331 struct rtc_time rtc_tm = {0};
332 int ret = 0;
333 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
xf.libdd93d52023-05-12 07:10:14 -0700334
xf.li9d1a0e12023-09-20 01:43:20 -0700335 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_get_time NOOPS.\n");
336
337 if (buf->cpu == RTC_CPU_CAP)
338 {
339 int cmd = RTC_MSG_GET_TIME;
340 RTC_DATA_BUF sendbuf = {0};
341 unsigned long cur_sec = 0;
342
343 ret = ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm);
344 slog(RTC_PRINT, SLOG_NORMAL,
345 "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",
346 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);
347 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_get_time src_id:0x%x, buf->cpu:%d.\n", buf->srcModule, buf->cpu);
348
349 cur_sec = mktime(&rtc_tm);
350 sendbuf.srcModule = buf->srcModule;
351 sendbuf.cpu = RTC_CPU_AP;
352 sendbuf.ulSec = cur_sec;
353
354 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_get_time: send to cap cmd:0x%x, srcMod:0x%x, cur_sec:%lu \n", cmd,
355 sendbuf.srcModule, sendbuf.ulSec);
356 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
357 if (ret != 0)
358 {
359 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);;
360 }
361 }
362
363 return 0;
lh9ed821d2023-04-07 01:36:19 -0700364}
365
366/********************************************
367 * ¹¦ÄÜ:ÉèÖÃrtcʱ¼ä
368 *******************************************/
369static int handle_set_time(MSG_BUF *pmsg_buf)
370{
xf.li9d1a0e12023-09-20 01:43:20 -0700371 time_t os_time = 0;
372 struct tm tmTime = {0};
373 struct rtc_time rtc_tm = {0};
374 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700375
xf.li9d1a0e12023-09-20 01:43:20 -0700376 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter.\n");
377 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
378 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter: set time: %ld \n", os_time);
379 convert_time(os_time, &tmTime, CONVERT_TYPE_RTC);
380 memcpy(&rtc_tm, &tmTime, sizeof(struct rtc_time));
381 slog(RTC_PRINT, SLOG_ERR,
382 "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",
383 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,
384 rtc_tm.tm_mday, rtc_tm.tm_wday);
lh9ed821d2023-04-07 01:36:19 -0700385
xf.li9d1a0e12023-09-20 01:43:20 -0700386 ret = ioctl(g_rtc_fd, RTC_SET_TIME, &rtc_tm);
387 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: RTC_SET_TIME ioctl ret = %d exit.\n", ret);
388 {
389 char buf[32] = {0};
390 snprintf(buf, 32, "AT+ZMMI=2,%d,%d\r\n", 1900 + rtc_tm.tm_year, 1 + rtc_tm.tm_mon);
391 ret = get_modem_info(buf, NULL, NULL);
392 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_set_time ret=%d %s\n", ret, buf);
393 }
394 return 0;
lh9ed821d2023-04-07 01:36:19 -0700395}
396
397/********************************************
xf.li742dd022023-06-08 01:43:32 -0700398 * ¹¦ÄÜ:sntpͬ²½ÏµÍ³Ê±¼ä
399 *******************************************/
400static int handle_sntp_update(MSG_BUF *pmsg_buf)
401{
402 time_t os_time = 0;
xf.li9d1a0e12023-09-20 01:43:20 -0700403 struct timeval time_tv = {0};
404 int ret = 0;
xf.li742dd022023-06-08 01:43:32 -0700405
xf.li9d1a0e12023-09-20 01:43:20 -0700406 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
xf.li742dd022023-06-08 01:43:32 -0700407
xf.li9d1a0e12023-09-20 01:43:20 -0700408 time_tv.tv_sec = os_time;
409 ret = settimeofday(&time_tv, NULL);
410 if (ret != 0)
411 {
412 slog(RTC_PRINT, SLOG_ERR, "handle_sntp_update is fail,ret:%d \n", ret);
413 }
xf.lidf7f8ba2024-09-12 23:53:34 -0700414 sc_debug_info_record("ap_sntp", "sntp time synchronization successful!\n");
415
xf.li9d1a0e12023-09-20 01:43:20 -0700416 return 0;
xf.li742dd022023-06-08 01:43:32 -0700417}
418
419/********************************************
lh9ed821d2023-04-07 01:36:19 -0700420 * ¹¦ÄÜ:Ôö¼Ó¶¨Ê±Æ÷
421 *******************************************/
422static int handle_alarm_add(MSG_BUF *pmsg_buf)
423{
xf.li9d1a0e12023-09-20 01:43:20 -0700424 //long cur_rtc_time = 0;
lh9ed821d2023-04-07 01:36:19 -0700425
xf.li9d1a0e12023-09-20 01:43:20 -0700426 struct rtc_time rtc_tm = {0};
427 ALARM_T *pAlarm = 0;
lh9ed821d2023-04-07 01:36:19 -0700428
xf.li9d1a0e12023-09-20 01:43:20 -0700429 RTC_DATA_BUF *buf = pmsg_buf->aucDataBuf;
430 ALARM_T *temp = NULL;
431 int mark = 0;
lh9ed821d2023-04-07 01:36:19 -0700432
xf.li9d1a0e12023-09-20 01:43:20 -0700433 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_add enter.\n");
lh9ed821d2023-04-07 01:36:19 -0700434
xf.li9d1a0e12023-09-20 01:43:20 -0700435 if (buf->ulSec <= 0)
436 {
437 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add invalid interval.\n");
438 return -1;
439 }
lh9ed821d2023-04-07 01:36:19 -0700440
xf.li9d1a0e12023-09-20 01:43:20 -0700441 pAlarm = malloc(sizeof(ALARM_T));
442 if (pAlarm == NULL)
443 {
444 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no memory.\n");
445 return -1;
446 }
lh9ed821d2023-04-07 01:36:19 -0700447
xf.li9d1a0e12023-09-20 01:43:20 -0700448 //»ñÈ¡µ±Ç°Ê±¼ä second
449 if (ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
450 {
451 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
452 }
453 memcpy(&pAlarm->rtc_tm, &rtc_tm, sizeof(rtc_tm));
lh9ed821d2023-04-07 01:36:19 -0700454
xf.li9d1a0e12023-09-20 01:43:20 -0700455 pAlarm->rtc_id = buf->module;
456 pAlarm->module_id = buf->srcModule;
457 pAlarm->interval = buf->ulSec ; // count down time
458 pAlarm->remain_time = buf->ulSec; // remain count down time
459 pAlarm->wakeup = buf->wakeup;
460 pAlarm->is_utc = buf->is_utc;
461 pAlarm->cpu = buf->cpu;
462 pAlarm->time_val = buf->time_val;// cur_rtc_time is mktime(&local_tm)
463 //pAlarm->time_val = get_RtcTime();// cur_rtc_time is mktime(&local_tm)
lh9ed821d2023-04-07 01:36:19 -0700464
xf.li9d1a0e12023-09-20 01:43:20 -0700465 slog(RTC_PRINT, SLOG_NORMAL,
466 "rtc-service: handle_alarm_add pAlarm->interval is %ld ,pAlarm->time_val() is %ld, is_utc:%d.\n",
467 pAlarm->interval, pAlarm->time_val, pAlarm->is_utc);
468 /*
469 if (pAlarm->time_val + pAlarm->remain_time < cur_rtc_time) {
470 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);
471 }
472 */
lh9ed821d2023-04-07 01:36:19 -0700473
474#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700475 pAlarm->is_utc = 0; //´Ëģʽ϶¼°´timerÉèÖö¨Ê±Æ÷£¬²»Ö§³Öutc
476 pAlarm->wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700477#endif
478
xf.li9d1a0e12023-09-20 01:43:20 -0700479 if (pAlarm->is_utc == 0)
480 {
481 if (!list_empty(&rtc_timer_list))
482 {
483 //update_timer_remain(pAlarm->remain_time);
484 list_for_each_entry(temp, &rtc_timer_list, list)
485 {
486 if (temp->remain_time > pAlarm->remain_time)
487 {
488 mark = 1;
489 list_add(&pAlarm->list, temp->list.prev);
490 break;
491 }
492 }
493 }
494 if (mark == 0)
495 {
496 list_add_tail(&pAlarm->list, &rtc_timer_list);
497 }
498 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc timer\n");
499 alarm_restart(&rtc_timer_list);
500 }
501 else
502 {
503 if (!list_empty(&rtc_alarm_list))
504 {
505 list_for_each_entry(temp, &rtc_alarm_list, list)
506 {
507 if (temp->interval + temp->time_val > pAlarm->interval + pAlarm->time_val)
508 {
509 mark = 1;
510 list_add(&pAlarm->list, temp->list.prev);
511 break;
512 }
513 }
514 }
515 if (mark == 0)
516 {
517 list_add_tail(&pAlarm->list, &rtc_alarm_list);
518 }
lh9ed821d2023-04-07 01:36:19 -0700519
xf.li9d1a0e12023-09-20 01:43:20 -0700520 if (pAlarm->wakeup)
521 {
522 save_rtc_alarm();
523 }
lh9ed821d2023-04-07 01:36:19 -0700524
xf.li9d1a0e12023-09-20 01:43:20 -0700525 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc alarm \n");
526 alarm_restart(&rtc_alarm_list);
527 }
528
529 return 0;
lh9ed821d2023-04-07 01:36:19 -0700530}
531
532/********************************************
533 * ¹¦ÄÜ:°´µ±Ç°¶¨Ê±Æ÷Ê£Óàʱ¼ä¸üж¨Ê±Æ÷
534 *******************************************/
535static void update_timer_remain(int new_remian)
536{
xf.li9d1a0e12023-09-20 01:43:20 -0700537 if (list_empty(&rtc_timer_list))
538 {
539 return;
540 }
541 int ret, remain_time;
542 ALARM_T *pTmp;
543 //list_entry(pTmp, &rtc_timer_list, list);
544 pTmp = list_entry(rtc_timer_list.next, ALARM_T, list);
lh9ed821d2023-04-07 01:36:19 -0700545
546#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700547 time_t cur = time(NULL);
548 remain_time = g_set_softtime + pTmp->remain_time - cur;
549 if ((remain_time > pTmp->remain_time) || (remain_time <= 0))
550 {
551 slog(RTC_PRINT, SLOG_ERR, "rtc-service: cur:%u, set:%u, remain:%u, inveral:%u\n", cur, g_set_softtime, remain_time,
552 pTmp->remain_time);
553 //ʱ¼ä·¢Éú¹ýÖØÖõȣ¬Ö±½ÓÉèÖö¨Ê±Æ÷µ½£¬¸÷Ä£¿éÓ¦ÔÚÊÕµ½RTC_MSG_TIME_CHANGEDÏûϢʱ¼°Ê±¸üж¨Ê±Æ÷
554 remain_time = 0;
555 }
lh9ed821d2023-04-07 01:36:19 -0700556#else
xf.li9d1a0e12023-09-20 01:43:20 -0700557 ret = ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time);
lh9ed821d2023-04-07 01:36:19 -0700558#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700559 if (ret < 0)
560 {
561 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no free alarm remain_time %d %s errno.\n", errno,
562 strerror(errno));
563 // return -1;
564 }
565 //if(remain_time > new_remian)
566 {
567 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: pTmp->remain_time: %d, rtc remain_time: %d \n", pTmp->remain_time,
568 remain_time);
569 alarm_adjust_value(pTmp->remain_time - remain_time);
570 alarm_fix_value(&rtc_timer_list);
571 }
lh9ed821d2023-04-07 01:36:19 -0700572}
573
574/********************************************
575 * ¹¦ÄÜ:ɾ³ýÏûÏ¢Ô´Ä£¿éµÄËùÓж¨Ê±Æ÷
576 *******************************************/
577static int handle_alarm_del(MSG_BUF *pmsg_buf)
578{
xf.li9d1a0e12023-09-20 01:43:20 -0700579 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
580 ALARM_T *pTmp, *pTmp1;
581 int wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700582
xf.li9d1a0e12023-09-20 01:43:20 -0700583 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del enter.\n");
584 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
585 {
586 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del timer module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
587 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
588 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id) && (buf->cpu == pTmp->cpu))
589 {
590 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del timer module:0x%x , src:0x%x , cpu:%d \n",
591 buf->module, buf->srcModule, buf->cpu);
592 list_del(&pTmp->list);
593 free(pTmp);
594 break;
595 }
596 }
lh9ed821d2023-04-07 01:36:19 -0700597
xf.li9d1a0e12023-09-20 01:43:20 -0700598 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
599 {
600 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del alarm module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
601 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
602 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id) && (buf->cpu == pTmp->cpu))
603 {
604 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del alarm module:0x%x , src:0x%x , cpu:%d \n",
605 buf->module, buf->srcModule, buf->cpu);
606 wakeup = pTmp->wakeup;
607 list_del(&pTmp->list);
608 free(pTmp);
609 break;
610 }
611 }
lh9ed821d2023-04-07 01:36:19 -0700612
xf.li9d1a0e12023-09-20 01:43:20 -0700613 if (wakeup)
614 {
615 save_rtc_alarm();
616 }
lh9ed821d2023-04-07 01:36:19 -0700617
xf.li9d1a0e12023-09-20 01:43:20 -0700618 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del exit.\n");
619
620 return 0;
lh9ed821d2023-04-07 01:36:19 -0700621}
622
xf.libdd93d52023-05-12 07:10:14 -0700623void send_ok_del_rsp(MSG_BUF *pmsg_buf)
624{
xf.li9d1a0e12023-09-20 01:43:20 -0700625 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
xf.libdd93d52023-05-12 07:10:14 -0700626
xf.li9d1a0e12023-09-20 01:43:20 -0700627 if (buf->cpu == RTC_CPU_CAP)
628 {
629 int ret = 0;
630 RTC_DATA_BUF sendbuf = {0};
631 sendbuf.cpu = RTC_CPU_AP;
632 sendbuf.module = buf->module;
633 sendbuf.srcModule = buf->srcModule;
634 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, RTC_MSG_DEL_ALARM, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
635 if (ret != 0)
636 {
637 slog(RTC_PRINT, SLOG_ERR, "send_ok_del_rsp: failed,ret:%d\n", ret);
638 }
639 }
xf.libdd93d52023-05-12 07:10:14 -0700640}
lh9ed821d2023-04-07 01:36:19 -0700641/********************************************
642 *¹¦ÄÜ:¶¨Ê±Æ÷³¬Ê±´¦Àí
643 *******************************************/
644static int handle_alarm_timeout(MSG_BUF *pmsg_buf)
645{
xf.li9d1a0e12023-09-20 01:43:20 -0700646 long ret = -1;
647 ALARM_T *pAlarm;
648 RTC_DATA_BUF buf = {0};
lh9ed821d2023-04-07 01:36:19 -0700649 long rtc_report_time = 0;
650 long alarm_node_time = 0;
xf.li9d1a0e12023-09-20 01:43:20 -0700651 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
652 int wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700653
xf.li9d1a0e12023-09-20 01:43:20 -0700654 if (cmd == RTC_MSG_TIMER_TIMEOUT && !list_empty(&rtc_timer_list))
655 {
656 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc timer timeout\n");
657 pAlarm = list_entry(rtc_timer_list.next, ALARM_T, list);
658 }
659 else if (cmd == RTC_MSG_ALARM_TIMEOUT && !list_empty(&rtc_alarm_list))
660 {
661 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc alarm timeout\n");
662 pAlarm = list_entry(rtc_alarm_list.next, ALARM_T, list);
lh9ed821d2023-04-07 01:36:19 -0700663 rtc_report_time = atol(pmsg_buf->aucDataBuf);
xf.li9d1a0e12023-09-20 01:43:20 -0700664 if (rtc_report_time < 0 || rtc_report_time > LONG_MAX - 1)
665 {
666 rtc_report_time = 0;
lh9ed821d2023-04-07 01:36:19 -0700667 }
668 alarm_node_time = pAlarm->interval + pAlarm->time_val;
xf.li9d1a0e12023-09-20 01:43:20 -0700669 if ((rtc_report_time > alarm_node_time && (rtc_report_time - alarm_node_time) > g_time_offset)
670 || (alarm_node_time > rtc_report_time && (alarm_node_time - rtc_report_time) > g_time_offset))
lh9ed821d2023-04-07 01:36:19 -0700671 {
xf.li9d1a0e12023-09-20 01:43:20 -0700672 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc node was deleted, timeout unexpected; node:%ld, rtc:%ld \n",
673 alarm_node_time, rtc_report_time);
674 alarm_fix_value(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700675
xf.li9d1a0e12023-09-20 01:43:20 -0700676 if (pAlarm->wakeup)
677 {
678 save_rtc_alarm();
679 }
680
681 alarm_restart(&rtc_alarm_list);
682 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
683 return 0;
684 }
685 buf.time_val = alarm_node_time;
686 }
687 else
688 {
689 slog(RTC_PRINT, SLOG_ERR, "rtc-service: timeout unexpected\n");
690 return -1;
691 }
692
693 wakeup = pAlarm->wakeup;
694 list_del(&pAlarm->list);
695 buf.module = pAlarm->rtc_id;
696 buf.srcModule = pAlarm->module_id;
697 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d \n",
698 pAlarm->module_id, buf.module, pAlarm->is_utc);
699 if (pAlarm->cpu == RTC_CPU_AP)
700 {
701 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, pAlarm->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
702 (unsigned char*)&buf, 0);
703 if (ret != 0)
704 {
705 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);
706 }
707 }
708 else
709 {
710 buf.module = pAlarm->rtc_id;
xf.libdd93d52023-05-12 07:10:14 -0700711 buf.srcModule = pAlarm->module_id;
712 buf.cpu = RTC_CPU_AP;
713 buf.is_utc = pAlarm->is_utc;
lh9ed821d2023-04-07 01:36:19 -0700714
xf.li9d1a0e12023-09-20 01:43:20 -0700715 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
716 cmd, buf.srcModule, buf.module, pAlarm->is_utc);
xf.libdd93d52023-05-12 07:10:14 -0700717 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
718 if (ret != 0)
719 {
xf.li9d1a0e12023-09-20 01:43:20 -0700720 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);;
xf.libdd93d52023-05-12 07:10:14 -0700721 }
xf.li9d1a0e12023-09-20 01:43:20 -0700722 }
723 if (pAlarm->is_utc == 0)
724 {
725 alarm_adjust_value(pAlarm->remain_time);
726 alarm_fix_value(&rtc_timer_list);
727 alarm_restart(&rtc_timer_list);
728 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart imer.\n");
729 }
730 else
731 {
732 alarm_fix_value(&rtc_alarm_list);
733 if (wakeup)
734 {
735 save_rtc_alarm();
736 }
737 alarm_restart(&rtc_alarm_list);
738 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
739 }
lh9ed821d2023-04-07 01:36:19 -0700740
xf.li9d1a0e12023-09-20 01:43:20 -0700741 free(pAlarm);
742 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout exit.\n");
743 return 0;
lh9ed821d2023-04-07 01:36:19 -0700744}
745
746/********************************************
747 * ¹¦ÄÜ:ɾ³ý¹Ø»ú²»±£´æµÄ¶¨Ê±Æ÷
748 *******************************************/
749static void handle_del_all(MSG_BUF *pmsg_buf)
750{
xf.li9d1a0e12023-09-20 01:43:20 -0700751 ALARM_T *pTmp, *pTmp1;
lh9ed821d2023-04-07 01:36:19 -0700752
xf.li9d1a0e12023-09-20 01:43:20 -0700753 update_timer_remain(0);
754 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
755 {
756 if (pTmp->wakeup == 0)
757 {
758 list_del(&pTmp->list);
759 free(pTmp);
760 }
761 }
762 alarm_restart(&rtc_timer_list);
lh9ed821d2023-04-07 01:36:19 -0700763
xf.li9d1a0e12023-09-20 01:43:20 -0700764 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
765 {
766 if (pTmp->wakeup == 0)
767 {
768 list_del(&pTmp->list);
769 free(pTmp);
770 }
771 }
772 alarm_restart(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700773}
774
775/********************************************
776 * ¹¦ÄÜ:sntpͬ²½³É¹¦ºó£¬¸üÐÂalarm_list
777 *******************************************/
778static void update_alarm()
779{
xf.li9d1a0e12023-09-20 01:43:20 -0700780 ALARM_T *pTmp, *pTmp1;
781 //long sec = get_RtcTime();
782 long sec = time(0);
783 RTC_DATA_BUF buf = {0};
784
lh9ed821d2023-04-07 01:36:19 -0700785#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700786 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
787 {
788 buf.module = pTmp->rtc_id;
789 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF),
790 (unsigned char*)&buf, 0);
791 if (pTmp->is_utc)
792 {
793 if (pTmp->interval + pTmp->time_val < sec)
794 {
795 list_del(&pTmp->list);
796 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED timer, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id,
797 (pTmp->interval + pTmp->time_val));
798 free(pTmp);
799 }
800 else
801 {
802 pTmp->remain_time = pTmp->interval + pTmp->time_val - sec;
803 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: timer adjust, alarm_id(%d), alarm_time(%ld), remain(%ld).\n", pTmp->rtc_id,
804 (pTmp->interval + pTmp->time_val), pTmp->remain_time);
805 }
806 }
807 }
808 alarm_restart(&rtc_timer_list);
lh9ed821d2023-04-07 01:36:19 -0700809#else
xf.li9d1a0e12023-09-20 01:43:20 -0700810 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
811 {
812 buf.module = pTmp->rtc_id;
813 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF),
814 (unsigned char*)&buf, 0);
815 if (pTmp->interval + pTmp->time_val < sec)
816 {
817 list_del(&pTmp->list);
818 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED alarm, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id,
819 (pTmp->interval + pTmp->time_val));
820 free(pTmp);
821 }
822 }
823 alarm_restart(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700824#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700825 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: update_alarm exit cur_rtc_time: % ld.\n", sec);
lh9ed821d2023-04-07 01:36:19 -0700826}
827
828/********************************************
829 * ¹¦ÄÜ:¸üж¨Ê±Æ÷Ê£Óàʱ¼ä
830 *******************************************/
831static void alarm_adjust_value(int time)
832{
xf.li9d1a0e12023-09-20 01:43:20 -0700833 ALARM_T * temp;
834 list_for_each_entry(temp, &rtc_timer_list, list)
835 {
836 temp->remain_time -= time;
837 }
lh9ed821d2023-04-07 01:36:19 -0700838}
839
840/********************************************
841 *¹¦ÄÜ: ɾ³ýÎÞЧ¶¨Ê±Æ÷
842 *******************************************/
843static void alarm_fix_value(struct list_head *rtc_list)
844{
xf.li9d1a0e12023-09-20 01:43:20 -0700845 ALARM_T *pTmp, *pTmp1;
846 RTC_DATA_BUF buf = {0};
847 int wakeup = 0;
848 int ret = 0;
849 enum RTC_MSG_CMD cmd = 0;
lh9ed821d2023-04-07 01:36:19 -0700850
xf.li9d1a0e12023-09-20 01:43:20 -0700851 if (rtc_list == &rtc_timer_list)
852 {
853 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list)
854 {
855 if (pTmp->remain_time <= 0)
856 {
857 buf.module = pTmp->rtc_id;
858 if (pTmp->cpu == RTC_CPU_CAP)
859 {
860 buf.srcModule = pTmp->module_id;
861 buf.cpu = RTC_CPU_AP;
862 buf.is_utc = pTmp->is_utc;
863 cmd = RTC_MSG_TIMER_TIMEOUT;
864 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
865 cmd, buf.srcModule, buf.module, buf.is_utc);
866 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
867 if (ret != 0)
868 {
869 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n", ret);;
870 }
871 }
872 else
873 {
874 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
875 (unsigned char*)&buf, 0);
876 }
877 list_del(&pTmp->list);
878 free(pTmp);
879 }
880 }
881 }
882 else if (rtc_list == &rtc_alarm_list)
883 {
884 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list)
885 {
886 if (pTmp->time_val + pTmp->interval <= (time(0) + g_time_offset))
887 {
888 if (pTmp->wakeup)
889 {
890 wakeup = pTmp->wakeup;
891 }
892
893 buf.module = pTmp->rtc_id;
lh758261d2023-07-13 05:52:04 -0700894 buf.time_val = pTmp->time_val + pTmp->interval;
xf.li9d1a0e12023-09-20 01:43:20 -0700895 if (pTmp->cpu == RTC_CPU_CAP)
896 {
897 buf.srcModule = pTmp->module_id;
898 buf.cpu = RTC_CPU_AP;
899 buf.is_utc = pTmp->is_utc;
900 cmd = RTC_MSG_ALARM_TIMEOUT;
901 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
902 cmd, buf.srcModule, buf.module, buf.is_utc);
903 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
904 if (ret != 0)
905 {
906 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n", ret);;
907 }
908 }
909 else
910 {
911 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
912 (unsigned char*)&buf, 0);
913 }
914 list_del(&pTmp->list);
915 free(pTmp);
916 }
917 }
lh9ed821d2023-04-07 01:36:19 -0700918
xf.li9d1a0e12023-09-20 01:43:20 -0700919 if (wakeup)
920 {
921 save_rtc_alarm();
922 }
923 }
924 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_fix_value exit.\n");
lh9ed821d2023-04-07 01:36:19 -0700925}
926
927#ifdef PRODUCT_NOT_USE_RTC
928/*¶ÔӦûÓÐrtcµÄ²úÆ·£¬rtc¶¨Ê±Æ÷ÊÊÅäµ½softtimerµÄ»Øµ÷º¯Êý*/
929static void rtc_set_softtimer_cb(void *param)
930{
xf.li9d1a0e12023-09-20 01:43:20 -0700931 int ret = -1;
lh9ed821d2023-04-07 01:36:19 -0700932
xf.li9d1a0e12023-09-20 01:43:20 -0700933 slog(NET_PRINT, SLOG_ERR, "rtc-service: rtc_adapt_softtimer_cb \n");
934
935 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, 0, 0);;
936 if (ret != 0)
937 {
938 slog(MISC_PRINT, SLOG_ERR, "rtc-service: set rtc timer send msg failed \n");
939 }
940 return 0;
lh9ed821d2023-04-07 01:36:19 -0700941}
942#endif
943
944/********************************************
945 * ¹¦ÄÜ:ÖØÆô¶¨Ê±Æ÷£¬Ã»ÓÐÔò¹Ø±Õ¶¨Ê±Æ÷
946 *******************************************/
947static void alarm_restart(struct list_head *rtc_list)
948{
xf.li9d1a0e12023-09-20 01:43:20 -0700949 int ret = 0;
950 //struct tm *p_tm = 0;
951 struct tm ret_tm = {0};
952 struct rtc_wkalrm alarm = {0};
953 long tVal = 0;
954 long remain = -1;
955 ALARM_T *ptmp = NULL;
lh9ed821d2023-04-07 01:36:19 -0700956 long cur_rtc_time = time(0);
xf.li9d1a0e12023-09-20 01:43:20 -0700957 RTC_DATA_BUF buf = {0};
lh9ed821d2023-04-07 01:36:19 -0700958NEXT:
xf.li9d1a0e12023-09-20 01:43:20 -0700959 if (!list_empty(rtc_list))
960 {
961 ptmp = list_entry(rtc_list->next, ALARM_T, list);
962 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart enter.\n");
963 memset(&buf, 0, sizeof(RTC_DATA_BUF));
964 if (ptmp->is_utc == 0)
965 {
lh9ed821d2023-04-07 01:36:19 -0700966#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700967 g_set_softtime = time(NULL);
968 sc_timer_delete(RTC_USE_SOFTTIMER_ID);
969 ret = sc_timer_create(RTC_USE_SOFTTIMER_ID, TIMER_FLAG_ONCE, ptmp->remain_time * 1000, rtc_set_softtimer_cb, NULL);
lh9ed821d2023-04-07 01:36:19 -0700970#else
xf.li9d1a0e12023-09-20 01:43:20 -0700971 ret = ioctl(g_rtc_fd, ZX234290_SET_TIMER, ptmp->remain_time);
lh9ed821d2023-04-07 01:36:19 -0700972#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700973 if (ret != 0)
974 {
975 slog(RTC_PRINT, SLOG_ERR, "rtc-service: alarm_restart timer: set failed, ret:%d \n", ret);
976 return;
977 }
978 slog(RTC_PRINT, SLOG_NORMAL,
979 "rtc-service: alarm_restart: set timer ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x\n",
980 ptmp->remain_time, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
981 }
982 else
983 {
984 tVal = ptmp->interval + ptmp->time_val;
985 remain = tVal - cur_rtc_time;
986 if (remain <= g_time_offset)
987 {
988 //¹ýʱ¶¨Ê±Æ÷¶ªÆú´¦Àí£¬Èç¹û¶àÄ£¿éÕýºÃÉèµÄ»ù±¾ÔÚͬһ¸öʱ¼äµã£¬ºóÕ߻ᱻ¶ªÆú£¬¿¼ÂÇÐ޸ijÉÖ±½Ó֪ͨģ¿éʱ¼äµ½
989 slog(RTC_PRINT, SLOG_ERR,
990 "rtc-service: remain time error, time_set:%ld, remain:%ld, alarmer:%ld, interval:%ld, current:%ld, module:0x%x, rtc_id:0x%x\n",
991 ptmp->time_val, remain, tVal, ptmp->interval, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
992 buf.module = ptmp->rtc_id;
993 buf.time_val = ptmp->time_val + ptmp->interval;
994 ipc_send_message2(MODULE_ID_RTC_SERVICE, ptmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
995 (unsigned char*)&buf, 0);
996 list_del(&ptmp->list);
997 free(ptmp);
998 ptmp = NULL;
999 goto NEXT;
1000 }
lh9ed821d2023-04-07 01:36:19 -07001001
xf.li9d1a0e12023-09-20 01:43:20 -07001002 //localtime_r(&tVal, &ret_tm);
1003 gmtime_r(&tVal, &ret_tm);
1004 memcpy(&alarm.time, &ret_tm, sizeof(struct rtc_time));
1005 alarm.enabled = 1;
lh9ed821d2023-04-07 01:36:19 -07001006
xf.li9d1a0e12023-09-20 01:43:20 -07001007 /*Ê×ÏÈ´ò¿ªalarm ÖжÏ*/
1008 ret = ioctl(g_rtc_fd, RTC_AIE_ON);
1009 if (ret != 0)
1010 {
1011 slog(RTC_PRINT, SLOG_ERR, "rtc-service: open alarm failed, ret:%d\n", ret);
1012 return;
1013 }
lh9ed821d2023-04-07 01:36:19 -07001014
xf.li9d1a0e12023-09-20 01:43:20 -07001015 /*ÉèÖÃalarm ʱ¼ä*/
1016 ret = ioctl(g_rtc_fd, RTC_WKALM_SET, &alarm);
1017 if (ret != 0)
1018 {
1019 slog(RTC_PRINT, SLOG_ERR, "rtc-service: set alarm failed, ret:%d\n", ret);
1020 return;
1021 }
1022 slog(RTC_PRINT, SLOG_NORMAL,
1023 "rtc-service: alarm_restart set alarm ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x \n", remain,
1024 cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
1025 }
1026 }
1027 else
1028 {
1029 if (rtc_list == &rtc_timer_list)
1030 {
1031 //¹Ø±Õrtc time, Éè¼ä¸ôʱ¼äΪ0
1032 ret = ioctl(g_rtc_fd, ZX234290_TIMER_ENABLED, 0);
1033 if (ret != 0)
1034 {
1035 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable timer failed, ret:%d\n", ret);
1036 return;
1037 }
1038 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable timer ok \n");
1039 }
1040 else
1041 {
1042 /*¹Ø±Õalarm ÖжÏ*/
1043 ret = ioctl(g_rtc_fd, RTC_AIE_OFF);
1044 if (ret != 0)
1045 {
1046 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable alarm failed, ret:%d\n", ret);
1047 return;
1048 }
1049 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable alarm ok \n");
1050 }
1051 }
1052 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart exit.\n");
lh9ed821d2023-04-07 01:36:19 -07001053}
1054
1055static void rtc_set_local_time(MSG_BUF *msg)
1056{
xf.li9d1a0e12023-09-20 01:43:20 -07001057 struct tm set_tm = {0};
1058 struct timeval time_tv;
lh9ed821d2023-04-07 01:36:19 -07001059
xf.li9d1a0e12023-09-20 01:43:20 -07001060 slog(RTC_PRINT, SLOG_DEBUG, "rtc_set_local_time, %d, %d \n", sizeof(struct tm), msg->usDataLen);
lh9ed821d2023-04-07 01:36:19 -07001061
xf.li9d1a0e12023-09-20 01:43:20 -07001062 memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen);
lh9ed821d2023-04-07 01:36:19 -07001063
xf.li9d1a0e12023-09-20 01:43:20 -07001064 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n",
1065 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);
lh9ed821d2023-04-07 01:36:19 -07001066
xf.li9d1a0e12023-09-20 01:43:20 -07001067 time_tv.tv_sec = mktime(&set_tm);
1068 if (time_tv.tv_sec < 0)
1069 {
lh9ed821d2023-04-07 01:36:19 -07001070
xf.li9d1a0e12023-09-20 01:43:20 -07001071 slog(RTC_PRINT, SLOG_DEBUG, "mktime fail, set time_tv.tv_sec to 0 \n");
1072 time_tv.tv_sec = 0;
1073 }
1074 time_tv.tv_usec = 0;
1075
1076 if (0 != settimeofday(&time_tv, NULL))
1077 {
1078 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time failed \n");
1079 }
lh9ed821d2023-04-07 01:36:19 -07001080}
1081
1082/********************************************
1083 * process_msg
1084 * handle the specified MSG.
1085 *******************************************/
1086static int process_msg(MSG_BUF *pmsg_buf)
1087{
xf.li9d1a0e12023-09-20 01:43:20 -07001088 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
1089 unsigned short req_cmd = 0;
lh9ed821d2023-04-07 01:36:19 -07001090
xf.li9d1a0e12023-09-20 01:43:20 -07001091 switch (cmd)
1092 {
1093 case RTC_MSG_GET_TIME:
1094 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_GET_TIME, src:0x%x.\n", pmsg_buf->src_id);
1095 handle_get_time(pmsg_buf);
1096 break;
1097 case RTC_MSG_SET_TIME:
1098 security_log(pmsg_buf->src_id, "set time");
1099 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_SET_TIME, src:0x%x.\n", pmsg_buf->src_id);
1100 handle_set_time(pmsg_buf);
1101 update_alarm();
1102 break;
1103 case RTC_MSG_SNTP_UPDATE:
1104 handle_sntp_update(pmsg_buf);
1105 break;
1106 case RTC_MSG_ADD_ALARM:
1107 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_ADD_ALARM, src:0x%x, g_clean_flag = %d.\n",
1108 pmsg_buf->src_id, g_clean_flag);
1109 //Ìí¼ÓrtcǰÏÈɾ³ý´Ërtc_idÒÑÌí¼Ó¼Ç¼£¬Ã¿¸örtc_idÖ»±£Áô1¸ö¶¨Ê±Æ÷
1110 if (0 == g_clean_flag)
1111 {
1112 update_timer_remain(0);
1113 handle_alarm_del(pmsg_buf);
1114 handle_alarm_add(pmsg_buf);
1115 }
1116 else
1117 {
1118 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: g_clean_flag is nonzero:0x%x, dont add alarm.\n", g_clean_flag);
1119 }
lh9ed821d2023-04-07 01:36:19 -07001120
xf.li9d1a0e12023-09-20 01:43:20 -07001121 //wifi´¦ÀíÍêǰ²»ÄÜÐÝÃߣ¬ÔÝʱʹÓÃÑÓÊ±Ëø£¬´ý·½°¸×鏸³ö·½°¸ºóÔÙµ÷Õû
1122 //if (pmsg_buf->usSourceModuleID == MODULE_ID_WDT)
1123 //write_lockfile("/sys/power/wake_unlock", "rtc_io_time_timeout_lock");
1124
1125 break;
1126 case RTC_MSG_DEL_ALARM:
1127 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALARM, src:0x%x.\n", pmsg_buf->src_id);
1128 update_timer_remain(0);
1129 handle_alarm_del(pmsg_buf);
1130 send_ok_del_rsp(pmsg_buf);
1131 alarm_restart(&rtc_timer_list);
1132 alarm_restart(&rtc_alarm_list);
1133 break;
1134 case RTC_MSG_ALARM_TIMEOUT:
1135 case RTC_MSG_TIMER_TIMEOUT:
1136 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_TIMEOUT, src:0x%x, cmd:0x%x.\n", pmsg_buf->src_id, cmd);
1137 handle_alarm_timeout(pmsg_buf);
1138 break;
1139 case RTC_MSG_DEL_ALL:
1140 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
1141 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
1142 if (MSG_CMD_RESET_RSP == req_cmd || MSG_CMD_POWEROFF_RSP == req_cmd || MSG_CMD_RESTART_RSP == req_cmd)
1143 {
1144 g_clean_flag = req_cmd;
1145 }
1146 handle_del_all(pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -07001147 save_rtc_alarm();
xf.li9d1a0e12023-09-20 01:43:20 -07001148 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
1149 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
1150 break;
lh9ed821d2023-04-07 01:36:19 -07001151
xf.li9d1a0e12023-09-20 01:43:20 -07001152 default:
1153 break;
1154 }
1155 return 0;
lh9ed821d2023-04-07 01:36:19 -07001156}
1157
xf.li6c8fc1e2023-08-12 00:11:09 -07001158/********************************************
1159 * process_msg_rtc_ap
1160 * handle the specified MSG to cap, because rtc driver is at cap
1161 *******************************************/
1162static int process_msg_rtc_cap(MSG_BUF *pmsg_buf)
1163{
xf.li9d1a0e12023-09-20 01:43:20 -07001164 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
1165 unsigned short req_cmd = 0;
1166 int ret = 0;
1167 struct tm *ptm = NULL;
1168 RTC_DATA_BUF *buf = NULL;
1169 int dst_id = 0;
1170
1171 switch (cmd)
1172 {
1173 case RTC_MSG_GET_TIME:
1174 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1175 slog(RTC_PRINT, SLOG_NORMAL, "to cap: RTC_MSG_GET_TIME \n");
1176 if (buf->cpu == RTC_CPU_AP)
1177 {
1178 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1179 if (ret != 0)
1180 {
1181 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1182 }
1183 }
1184 else
1185 {
1186 slog(RTC_PRINT, SLOG_NORMAL, "receive rtc time from cap core : RTC_MSG_GET_TIME :%lu \n", buf->ulSec);
1187
1188 }
1189 break;
1190 case RTC_MSG_SET_TIME:
1191 ptm = (struct tm *)pmsg_buf->aucDataBuf;
1192 if (pmsg_buf->src_id == MODULE_ID_CP_RTC_SERVICE)
1193 {
1194 rtc_set_local_time(pmsg_buf);
1195 }
1196 else
1197 {
1198 /*
1199 slog(RTC_PRINT, SLOG_NORMAL, "to cap: cmd:0x%x, %d-%d-%d %d:%d:%d \n", cmd, ptm->tm_year, ptm->tm_mon, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
1200 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1201 if (ret != 0)
1202 {
1203 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
1204 }
1205 */
1206 }
1207 break;
1208 case RTC_MSG_SNTP_UPDATE:
1209 handle_sntp_update(pmsg_buf);
1210 break;
1211 case RTC_MSG_ADD_ALARM:
1212 case RTC_MSG_DEL_ALARM:
1213 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1214 if (buf->cpu == RTC_CPU_AP)
1215 {
1216 slog(RTC_PRINT, SLOG_NORMAL, "to cap: cmd:0x%x, module:0x%x, msg_id:0x%x, sec:%ld: cpu:%d, utc:%d, wakeup:%d \n", cmd,
1217 buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
1218 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1219 if (ret != 0)
1220 {
1221 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1222 }
1223 }
1224 else
1225 {
1226 slog(RTC_PRINT, SLOG_NORMAL, "from cap: cmd:0x%x, module:0x%x, msg_id:0x%x, del ok \n", cmd, buf->module,
1227 buf->srcModule);
1228 }
1229 break;
1230 case RTC_MSG_ALARM_TIMEOUT:
1231 case RTC_MSG_TIMER_TIMEOUT:
1232 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1233 if (buf->cpu == RTC_CPU_CAP)
1234 {
1235 slog(RTC_PRINT, SLOG_NORMAL, "from cap: cmd:0x%x(timeout), module:0x%x, msg_id:0x%x, sec:%ld: %d, %d, %d \n", cmd,
1236 buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
1237
1238 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), buf, 0);
1239 if (ret != 0)
1240 {
1241 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1242 }
1243 }
1244 break;
1245 case RTC_MSG_DEL_ALL:
1246 slog(RTC_PRINT, SLOG_NORMAL, "process_msg_to_cap RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
1247 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
1248 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
1249 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
1250 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1251 if (ret != 0)
1252 {
1253 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1254 }
1255 break;
1256 case RTC_MSG_TIME_CHANGED:
1257 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1258 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: RTC_MSG_TIME_CHANGED, dst_id:0x%x.\n", buf->srcModule);
1259 ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_TIME_CHANGED, 0, 0, 0);
1260 break;
1261 default:
1262 break;
1263 }
1264 return 0;
xf.li6c8fc1e2023-08-12 00:11:09 -07001265}
1266
lh9ed821d2023-04-07 01:36:19 -07001267static int rtc_service_sock_init()
1268{
1269 int sockfd = 0;
xf.li9d1a0e12023-09-20 01:43:20 -07001270 int ret;
1271 struct sockaddr_nl snl;
1272
1273 bzero(&snl, sizeof(struct sockaddr_nl));
1274 snl.nl_family = AF_NETLINK;
lh9ed821d2023-04-07 01:36:19 -07001275 snl.nl_pid = getpid();
xf.li9d1a0e12023-09-20 01:43:20 -07001276 snl.nl_groups = 1;
lh9ed821d2023-04-07 01:36:19 -07001277
1278 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
xf.li9d1a0e12023-09-20 01:43:20 -07001279 {
lh9ed821d2023-04-07 01:36:19 -07001280 perror ("signal");
lh9ed821d2023-04-07 01:36:19 -07001281 }
lh9ed821d2023-04-07 01:36:19 -07001282
xf.li9d1a0e12023-09-20 01:43:20 -07001283 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1284 if (sockfd < 0)
1285 {
1286 slog(RTC_PRINT, SLOG_ERR, "create rtc service socket failed!\n");
1287 return -1;
1288 }
1289 // setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
lh9ed821d2023-04-07 01:36:19 -07001290
xf.li9d1a0e12023-09-20 01:43:20 -07001291 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
1292 if (ret < 0)
1293 {
1294 slog(RTC_PRINT, SLOG_ERR, "rtc service socket bind fail!\n");
1295 close(sockfd);
1296 return -1;
1297 }
1298
1299 return sockfd;
lh9ed821d2023-04-07 01:36:19 -07001300}
1301//change@/devices/platform/zx29_i2c.0/i2c-0/0-0012/zx234290-rtc
1302static int rtc_time_msg_parse(const char *msg, int msglen)
1303{
xf.li9d1a0e12023-09-20 01:43:20 -07001304 int offlen = 0;
1305 const char *keys = NULL;
1306 char* timeStr = NULL;
1307 char *pSec = NULL;
1308 slog(RTC_PRINT, SLOG_ERR, "[%s][%d] zx234290-rtc \n", __func__, __LINE__);
1309
1310 offlen += strlen((char *)msg) + 1;
1311 while (offlen <= msglen)
1312 {
1313 keys = (char *)(msg + offlen);
1314 //slog(NET_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc%s, %d\n", __func__, __LINE__, keys, offlen);
1315 if (strncmp(keys, RTC_ALARM_TIMEOUT_MSG, strlen(RTC_ALARM_TIMEOUT_MSG)) == 0)
1316 {
lh9ed821d2023-04-07 01:36:19 -07001317#if 1 // cov M DEADCODE
xf.li9d1a0e12023-09-20 01:43:20 -07001318 pSec = strstr(keys, "sec:");
1319 if (pSec == NULL)
1320 {
1321 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event no sec \n");
1322 return -1;
lh9ed821d2023-04-07 01:36:19 -07001323 }
xf.li9d1a0e12023-09-20 01:43:20 -07001324 timeStr = pSec + strlen("sec:");
lh9ed821d2023-04-07 01:36:19 -07001325
1326#else
1327 timeStr = strstr(keys, "sec:") + strlen("sec:");
xf.li9d1a0e12023-09-20 01:43:20 -07001328 if (timeStr == NULL)
1329 {
1330 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event no sec \n");
1331 return -1;
1332 }
1333#endif
1334 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_ALARM_TIMEOUT, strlen(timeStr), timeStr, 0);
1335 slog(RTC_PRINT, SLOG_NORMAL, "zx234290-rtc alarm timeout \n");
1336 return 0;
1337 }
1338 else if (strncmp(keys, RTC_TIMER_TIMEOUT_MSG, strlen(keys)) == 0)
1339 {
1340 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, NULL, 0);
1341 slog(RTC_PRINT, SLOG_NORMAL, "zx234290-rtc timer timeout \n");
1342 return 0;
1343 }
1344 offlen += strlen((char *)keys) + 1;
1345 }
1346 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event not supported \n");
1347 return -1;
lh9ed821d2023-04-07 01:36:19 -07001348}
1349
1350/********************************************
1351 * rtc_service_thread_process
1352 * looper for processing zx234290-rtc msg.
1353 *******************************************/
1354void rtc_event_process(void)
1355{
xf.li9d1a0e12023-09-20 01:43:20 -07001356 int rtc_service_fd = 0;
1357 fd_set readfds;
1358 int maxfd = 0;
1359 char buf[RTC_MSG_BUFFER_SIZE] = {0};
1360 int len = 0;
1361 int ret = 0;
1362 prctl(PR_SET_NAME, "rtceventhandle", 0, 0, 0);
1363 rtc_service_fd = rtc_service_sock_init();
1364 if (rtc_service_fd < 0)
1365 {
1366 slog(RTC_PRINT, SLOG_ERR, "rtc service socket init fail!\n");
1367 return ;
1368 }
lh9ed821d2023-04-07 01:36:19 -07001369
xf.li9d1a0e12023-09-20 01:43:20 -07001370 maxfd = rtc_service_fd;
1371 while (1)
1372 {
1373 FD_ZERO(&readfds);
1374 FD_SET(rtc_service_fd, &readfds);
lh9ed821d2023-04-07 01:36:19 -07001375
xf.li9d1a0e12023-09-20 01:43:20 -07001376 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1377 if (ret == -1 && errno == EINTR)
1378 {
1379 continue;
1380 }
lh9ed821d2023-04-07 01:36:19 -07001381
xf.li9d1a0e12023-09-20 01:43:20 -07001382 if (FD_ISSET(rtc_service_fd, &readfds))
1383 {
1384 memset(buf, 0, sizeof(buf));
1385 len = recv(rtc_service_fd, &buf, sizeof(buf), 0);
1386 if (len <= 0)
1387 {
1388 slog(RTC_PRINT, SLOG_ERR, "rtc service socket recv msg fail!\n");
1389 continue;
1390 }
lh9ed821d2023-04-07 01:36:19 -07001391
xf.li9d1a0e12023-09-20 01:43:20 -07001392 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
1393 if (strstr(buf, "zx234290-rtc"))
1394 {
1395 rtc_time_msg_parse(buf, len);
1396 }
1397 continue;
1398 }
1399 }
lh9ed821d2023-04-07 01:36:19 -07001400}
1401
1402/********************************************
1403 * msg_looper
1404 * main looper for processing MSG.
1405 *******************************************/
1406static void msg_looper(void)
1407{
xf.li9d1a0e12023-09-20 01:43:20 -07001408 int ret = 0;
1409 int msg_id = 0;
1410 MSG_BUF msg_buf = {0};
1411 // key_t msgkey = get_msg_key();
lh9ed821d2023-04-07 01:36:19 -07001412
xf.li9d1a0e12023-09-20 01:43:20 -07001413 msg_id = msgget(MODULE_ID_RTC_SERVICE, IPC_CREAT | 0600);
lh9ed821d2023-04-07 01:36:19 -07001414
xf.li9d1a0e12023-09-20 01:43:20 -07001415 memset(&msg_buf, 0, sizeof(msg_buf));
lh9ed821d2023-04-07 01:36:19 -07001416
xf.li9d1a0e12023-09-20 01:43:20 -07001417 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper enter(msgid=%d)&waiting...\n", msg_id);
1418 while (1)
1419 {
1420 ret = msgrcv(msg_id, &msg_buf, sizeof(MSG_BUF) - sizeof(long), 0, 0);
1421 wake_rtc_lock();
1422 if (ret <= 0)
1423 {
1424 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper recv msg break(%d)\n", ret);
1425 wake_rtc_unlock();
1426 continue;
1427 }
1428 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper process_msg in, src:0x%x, target:0x%x.\n", msg_buf.src_id,
1429 msg_buf.dst_id);
lh9ed821d2023-04-07 01:36:19 -07001430
xf.li9d1a0e12023-09-20 01:43:20 -07001431#ifdef USE_CAP_SUPPORT
1432 process_msg_rtc_cap(&msg_buf);
1433#else
1434 process_msg(&msg_buf);
1435#endif
1436
1437 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
1438 wake_rtc_unlock();
1439 }
lh9ed821d2023-04-07 01:36:19 -07001440}
1441
1442static void print_rtc_list_info(void)
1443{
xf.li9d1a0e12023-09-20 01:43:20 -07001444 ALARM_T *temp = NULL;
1445 int remain_time;
lh9ed821d2023-04-07 01:36:19 -07001446
xf.li9d1a0e12023-09-20 01:43:20 -07001447 if (ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time) < 0) // cov M CHECKED_RETURN
1448 {
1449 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
1450 }
lh9ed821d2023-04-07 01:36:19 -07001451
lh9ed821d2023-04-07 01:36:19 -07001452
xf.li9d1a0e12023-09-20 01:43:20 -07001453 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc remain_time :%ld \n", remain_time);
1454
1455 if (!list_empty(&rtc_timer_list))
1456 {
1457 list_for_each_entry(temp, &rtc_timer_list, list)
1458 {
1459 slog(RTC_PRINT, SLOG_ERR,
1460 "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d\n",
1461 temp->rtc_id,
1462 temp->module_id,
1463 temp->time_val,
1464 temp->remain_time,
1465 temp->cpu);
1466 }
1467 }
1468 else
1469 {
1470 slog(RTC_PRINT, SLOG_ERR, "rtc-service rtc_timer_list is empty!");
1471 }
1472
1473 if (!list_empty(&rtc_alarm_list))
1474 {
1475 list_for_each_entry(temp, &rtc_alarm_list, list)
1476 {
1477 slog(RTC_PRINT, SLOG_ERR,
1478 "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d, \
1479 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",
1480 temp->rtc_id,
1481 temp->module_id,
1482 temp->time_val,
1483 temp->remain_time,
1484 temp->cpu,
1485 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);
1486 }
1487 }
1488 else
1489 {
1490 slog(RTC_PRINT, SLOG_ERR, "rtc-service rtc_alarm_list is empty!");
1491 }
lh9ed821d2023-04-07 01:36:19 -07001492}
1493
1494/* ÐźŴ¦Àí*/
1495static void sig_usr(int signo)
1496{
1497#if 0//(APP_OS_TYPE == APP_OS_LINUX)
xf.li9d1a0e12023-09-20 01:43:20 -07001498 if (signo == SIGUSR1)
1499 {
1500 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service receive SIGUSR1! \n");
1501 print_rtc_list_info();
1502 }
1503 else
1504 {
1505 slog(RTC_PRINT, SLOG_ERR, "rtc-service receive signo:%d", signo);
1506 }
lh9ed821d2023-04-07 01:36:19 -07001507#endif
1508}
1509
1510/*RTCÏûÏ¢½âÎö*/
1511int rtc_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
1512{
xf.li9d1a0e12023-09-20 01:43:20 -07001513 if (strstr(msg, "zx234290-rtc"))
1514 {
1515 rtc_time_msg_parse(msg, msglen);
1516 }
1517 return -1;
lh9ed821d2023-04-07 01:36:19 -07001518}
1519
1520/********************************************
1521 * main
1522 * entry of RTC-Service!
1523 *******************************************/
1524int rtc_service_main(int argc, char **argv)
1525{
1526
xf.li9d1a0e12023-09-20 01:43:20 -07001527 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service enter...\n");
1528 pthread_t rtcservice_thread_id;
1529 pthread_attr_t attr;
1530 int result = 0;
1531 prctl(PR_SET_NAME, "rtc-service", 0, 0, 0);
lh9ed821d2023-04-07 01:36:19 -07001532
xf.li9d1a0e12023-09-20 01:43:20 -07001533 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
1534 loglevel_init();
lh9ed821d2023-04-07 01:36:19 -07001535
xf.li9d1a0e12023-09-20 01:43:20 -07001536 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
1537 {
1538 slog(RTC_PRINT, SLOG_ERR, "rtc-service not catch SIGUSR1 \n");
1539 }
1540
lh9ed821d2023-04-07 01:36:19 -07001541 INIT_LIST_HEAD(&rtc_timer_list);
1542 INIT_LIST_HEAD(&rtc_alarm_list);
1543
1544 g_clean_flag = 0;
xf.li9d1a0e12023-09-20 01:43:20 -07001545
1546 //rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
xf.li6c8fc1e2023-08-12 00:11:09 -07001547#ifndef USE_CAP_SUPPORT
xf.li9d1a0e12023-09-20 01:43:20 -07001548 if ((g_rtc_fd = open_rtc()) < 0)
1549 {
1550 slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
1551 return -1;
1552 }
1553 init_rtc_alarm();
xf.li6c8fc1e2023-08-12 00:11:09 -07001554#endif
xf.li9d1a0e12023-09-20 01:43:20 -07001555 /*
1556 pthread_attr_init(&attr);
1557 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1558 result = pthread_create(&rtcservice_thread_id, &attr, rtc_event_process, NULL);
1559 pthread_attr_destroy(&attr);
1560 if(result != 0)
1561 {
1562 slog(RTC_PRINT,SLOG_ERR, "[rtc-service] rtcservice_thread pthread_create faild!\n");
1563 return -1;
1564 }
1565 else
1566 {
1567 printf("[rtc-service] rtcservice_thread pthread_create SUCCESS!\n");
1568 }
1569 */
lh9ed821d2023-04-07 01:36:19 -07001570 hotplug_parse_register(DEVICE_TYPE_APP_RTC, rtc_app_msg_parse);
1571 msg_looper();
xf.li9d1a0e12023-09-20 01:43:20 -07001572 //klocwork
1573 //close_rtc(g_rtc_fd);
lh9ed821d2023-04-07 01:36:19 -07001574
xf.li9d1a0e12023-09-20 01:43:20 -07001575 //slog(RTC_PRINT, SLOG_ERR, "rtc-service exit...\n");
lh9ed821d2023-04-07 01:36:19 -07001576
xf.li9d1a0e12023-09-20 01:43:20 -07001577 return 0;
lh9ed821d2023-04-07 01:36:19 -07001578}