blob: d9db201376fbde8a25c1cef4ae28e32efd8a2edb [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;
xf.li9d1a0e12023-09-20 01:43:20 -070026struct list_head rtc_alarm_list;
lh9ed821d2023-04-07 01:36:19 -070027#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);
xf.li6c8fc1e2023-08-12 00:11:09 -070053static int process_msg_rtc_cap(MSG_BUF *pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -070054static void msg_looper(void);
55static void update_timer_remain(int new_remian);
56static void alarm_fix_value(struct list_head *rtc_list);
57static void alarm_restart(struct list_head *rtc_list);
58static void update_alarm();
59static void alarm_adjust_value(int time);
60static long get_RtcTime();
61static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type);
62static void convert_time(time_t timeSec, struct tm * tmTime, TIME_CONVERT_TYPE type)
63{
64 time_t tmpTime = 0;
xf.lie31de8b2023-12-26 23:38:58 -080065 time_t tmpTime2 = 0;
xf.li9d1a0e12023-09-20 01:43:20 -070066 if (tmTime == NULL)
67 {
lh9ed821d2023-04-07 01:36:19 -070068 slog(RTC_PRINT, SLOG_ERR, "rtc-service: wrong input param, check it! \n");
69 return ;
70 }
71 tmpTime = timeSec;
xf.li9d1a0e12023-09-20 01:43:20 -070072
73 if (type == CONVERT_TYPE_RTC)
74 {
lh9ed821d2023-04-07 01:36:19 -070075 gmtime_r((time_t*)&tmpTime, tmTime);
76 }
xf.li9d1a0e12023-09-20 01:43:20 -070077 else
78 {
lh9ed821d2023-04-07 01:36:19 -070079 localtime_r((time_t*)&tmpTime, tmTime);
80 }
xf.li9d1a0e12023-09-20 01:43:20 -070081
xf.lie31de8b2023-12-26 23:38:58 -080082 tmpTime2 = mktime(tmTime);
xf.li9d1a0e12023-09-20 01:43:20 -070083 slog(RTC_PRINT, SLOG_DEBUG,
84 "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 -080085 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 -070086 tmTime->tm_mday, tmTime->tm_wday);
lh9ed821d2023-04-07 01:36:19 -070087
88}
89
90static long get_RtcTime()
91{
xf.li9d1a0e12023-09-20 01:43:20 -070092 long curRtcTime = 0;
lh9ed821d2023-04-07 01:36:19 -070093 struct rtc_time rtc_tm = {0};
94 struct tm tmp = {0};
xf.li9d1a0e12023-09-20 01:43:20 -070095 if (g_rtc_fd <= 0)
96 {
lh9ed821d2023-04-07 01:36:19 -070097 slog(RTC_PRINT, SLOG_ERR, "rtc-service: g_rtc_fd: %d , invalid fd. \n", g_rtc_fd);
98 return -1;
99 }
xf.li9d1a0e12023-09-20 01:43:20 -0700100
101 if (ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
102 {
103 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
104 }
lh9ed821d2023-04-07 01:36:19 -0700105 tmp.tm_sec = rtc_tm.tm_sec;
xf.li9d1a0e12023-09-20 01:43:20 -0700106 tmp.tm_min = rtc_tm.tm_min;
107 tmp.tm_hour = rtc_tm.tm_hour;
108 tmp.tm_mon = rtc_tm.tm_mon;
109 tmp.tm_year = rtc_tm.tm_year;
110 tmp.tm_mday = rtc_tm.tm_mday;
111 tmp.tm_wday = rtc_tm.tm_wday;
112 tmp.tm_yday = rtc_tm.tm_yday;
113 tmp.tm_isdst = rtc_tm.tm_isdst;
114
lh9ed821d2023-04-07 01:36:19 -0700115 curRtcTime = mktime(&tmp);
116 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: curRtcTime: %ld . \n", curRtcTime);
117 return curRtcTime;
118}
119static void write_lockfile(char *filepath, char *setbuf)
120{
xf.li9d1a0e12023-09-20 01:43:20 -0700121 int f, len = 0;
lh9ed821d2023-04-07 01:36:19 -0700122
xf.li9d1a0e12023-09-20 01:43:20 -0700123 f = open(filepath, O_RDWR);
124 if (f == -1)
125 {
126 return;
127 }
lh9ed821d2023-04-07 01:36:19 -0700128
xf.li9d1a0e12023-09-20 01:43:20 -0700129 len = strlen(setbuf);
130 if (write(f, setbuf, len) != len)
131 {
132 }
133 else
134 {
135 }
136 close(f);
lh9ed821d2023-04-07 01:36:19 -0700137}
138
139
140static void wake_rtc_lock()
141{
xf.li9d1a0e12023-09-20 01:43:20 -0700142 write_lockfile("/sys/power/wake_lock", "rtc_lock");
lh9ed821d2023-04-07 01:36:19 -0700143}
144
145static void wake_rtc_unlock()
146{
xf.li9d1a0e12023-09-20 01:43:20 -0700147 write_lockfile("/sys/power/wake_unlock", "rtc_lock");
lh9ed821d2023-04-07 01:36:19 -0700148}
149
150static void wake_rtc_io_lock()
151{
xf.li9d1a0e12023-09-20 01:43:20 -0700152 write_lockfile("/sys/power/wake_lock", "rtc_io_lock");
lh9ed821d2023-04-07 01:36:19 -0700153}
154
155static void wake_rtc_io_unlock()
156{
xf.li9d1a0e12023-09-20 01:43:20 -0700157 write_lockfile("/sys/power/wake_unlock", "rtc_io_lock");
lh9ed821d2023-04-07 01:36:19 -0700158}
159
160static void wake_timeout_lock(char *lockname, unsigned long interval)
161{
xf.li9d1a0e12023-09-20 01:43:20 -0700162 char cmd[100] = {0};
163 sprintf(cmd, "echo %s %lu > /sys/power/wake_lock", lockname, interval);
164 soft_system(cmd);
lh9ed821d2023-04-07 01:36:19 -0700165}
166
167static int init_rtc_alarm(void)
168{
xf.li9d1a0e12023-09-20 01:43:20 -0700169 FILE* fp = NULL;
170 int gap_start = 0;
171 int gap_end = 0;
172 size_t size = 0;
173 long cur_rtc_time = 0;
174 ALARM_T* iter = NULL;
175 RTC_DATA_BUF buf = {0};
176 int ret = -1;
lh9ed821d2023-04-07 01:36:19 -0700177
xf.li9d1a0e12023-09-20 01:43:20 -0700178 // cur_rtc_time = get_RtcTime();
179 cur_rtc_time = time(0);
180 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "rb")) == NULL)
181 {
182 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
183 return -1;
184 }
lh9ed821d2023-04-07 01:36:19 -0700185
xf.li9d1a0e12023-09-20 01:43:20 -0700186 while (1)
187 {
188 size = fread(&gap_start, sizeof(int), 1, fp);//0xa5a5
189 if (0 == size || gap_start != DATA_S || feof(fp) != 0 )
190 {
191 break;
192 }
lh9ed821d2023-04-07 01:36:19 -0700193
xf.li9d1a0e12023-09-20 01:43:20 -0700194 iter = (ALARM_T*)malloc(sizeof(ALARM_T));
195 if (iter)
196 {
197 memset(iter, 0, sizeof(ALARM_T));
198 }
199 else
200 {
201 fclose(fp);
202 return -1;
203 }
lh9ed821d2023-04-07 01:36:19 -0700204
xf.li9d1a0e12023-09-20 01:43:20 -0700205 size = fread(iter, sizeof(ALARM_T), 1, fp);//struct
206 if (0 == size || feof(fp) != 0) //check the end of the file
207 {
208 memset(iter, 0, sizeof(ALARM_T));//kw
209 free(iter);
210 break;
211 }
lh9ed821d2023-04-07 01:36:19 -0700212
xf.li9d1a0e12023-09-20 01:43:20 -0700213 size = fread(&gap_end, sizeof(int), 1, fp);//0x5a5a
214 if (0 == size || gap_end != DATA_E || feof(fp) != 0)
215 {
216 memset(iter, 0, sizeof(ALARM_T));//kw
217 free(iter);
218 break;
219 }
lh9ed821d2023-04-07 01:36:19 -0700220
xf.li9d1a0e12023-09-20 01:43:20 -0700221 if (iter->interval < 0 || iter->interval > LONG_MAX - 1)
lh9ed821d2023-04-07 01:36:19 -0700222 {
223 iter->interval = 0 ;
224 }
225
226
xf.li9d1a0e12023-09-20 01:43:20 -0700227 if (iter->time_val < 0 || iter->time_val > LONG_MAX - 1)
lh9ed821d2023-04-07 01:36:19 -0700228 {
229 iter->time_val = 0 ;
230 }
231
lh9ed821d2023-04-07 01:36:19 -0700232
xf.li9d1a0e12023-09-20 01:43:20 -0700233 if (cur_rtc_time > iter->interval + iter->time_val)
234 {
235 buf.module = iter->rtc_id;
236 buf.time_val = iter->time_val + iter->interval;
237 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d, cur_rtc_time:%ld \n",
238 iter->module_id, buf.module, iter->is_utc, cur_rtc_time);
239 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, iter->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
240 (unsigned char*)&buf, 0);
241 if (ret != 0)
242 {
243 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected\n");
244 }
245 }
246 list_add_tail(&iter->list, &rtc_alarm_list);
247 if (feof(fp) != 0) //check the end of the file
248 {
249 break;
250 }
251 }
252 fclose(fp);
253
254 return 0;
lh9ed821d2023-04-07 01:36:19 -0700255}
256
257static int save_rtc_alarm(void)
258{
xf.li9d1a0e12023-09-20 01:43:20 -0700259 FILE* fp = NULL;
260 int gap_start = DATA_S;
261 int gap_end = DATA_E;
262 ALARM_T *temp = NULL;
lh9ed821d2023-04-07 01:36:19 -0700263
xf.li9d1a0e12023-09-20 01:43:20 -0700264 if ((fp = (FILE*)fopen(RTC_ALARM_DATA, "wb")) == NULL)
265 {
266 slog(RTC_PRINT, SLOG_ERR, "rtc-service: failed to open %s \n", RTC_ALARM_DATA);
267 return -1;
268 }
lh9ed821d2023-04-07 01:36:19 -0700269
xf.li9d1a0e12023-09-20 01:43:20 -0700270 if (!list_empty(&rtc_alarm_list))
271 {
272 list_for_each_entry(temp, &rtc_alarm_list, list)
273 {
274 if (temp->wakeup)
275 {
276 fwrite(&gap_start, sizeof(int), 1, fp);
277 fwrite(temp, sizeof(ALARM_T), 1, fp);
278 fwrite(&gap_end, sizeof(int), 1, fp);
279 }
280 }
281 }
lh9ed821d2023-04-07 01:36:19 -0700282
xf.li9d1a0e12023-09-20 01:43:20 -0700283 fflush(fp);
284 if (fsync(fileno(fp)) < 0)
lh9ed821d2023-04-07 01:36:19 -0700285 {
286 // todo: cov M
xf.li9d1a0e12023-09-20 01:43:20 -0700287 }
288 fclose(fp);
lh9ed821d2023-04-07 01:36:19 -0700289
xf.li9d1a0e12023-09-20 01:43:20 -0700290 return 0;
lh9ed821d2023-04-07 01:36:19 -0700291}
292
293/********************************************
294 ¹¦ÄÜ:´ò¿ªrtc
295 *******************************************/
296static int open_rtc(void)
297{
xf.li9d1a0e12023-09-20 01:43:20 -0700298 int fd = 0;
299 fd = open(RTC_DEV_NAME, O_RDWR);
lh9ed821d2023-04-07 01:36:19 -0700300
xf.li9d1a0e12023-09-20 01:43:20 -0700301 if (fd < 0)
302 {
303 slog(RTC_PRINT, SLOG_ERR, "rtc-service: Open %s error!\n", RTC_DEV_NAME);
304 return fd;
305 }
306 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: Open %s OK!\n", RTC_DEV_NAME);
307 return fd;
lh9ed821d2023-04-07 01:36:19 -0700308}
309
310/********************************************
311 ¹¦ÄÜ:¹Ø±Õrtc
312 *******************************************/
313static void close_rtc(int fd)
314{
xf.li9d1a0e12023-09-20 01:43:20 -0700315 if (fd < 0)
316 {
317 return;
318 }
lh9ed821d2023-04-07 01:36:19 -0700319
xf.li9d1a0e12023-09-20 01:43:20 -0700320 close(fd);
lh9ed821d2023-04-07 01:36:19 -0700321}
322
323
324/********************************************
325 * handle_get_time
326 * get RTC time.
327 *******************************************/
328static int handle_get_time(MSG_BUF *pmsg_buf)
329{
xf.li9d1a0e12023-09-20 01:43:20 -0700330 struct rtc_time rtc_tm = {0};
331 int ret = 0;
332 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
xf.libdd93d52023-05-12 07:10:14 -0700333
xf.li9d1a0e12023-09-20 01:43:20 -0700334 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_get_time NOOPS.\n");
335
336 if (buf->cpu == RTC_CPU_CAP)
337 {
338 int cmd = RTC_MSG_GET_TIME;
339 RTC_DATA_BUF sendbuf = {0};
340 unsigned long cur_sec = 0;
341
342 ret = ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm);
343 slog(RTC_PRINT, SLOG_NORMAL,
344 "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",
345 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);
346 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_get_time src_id:0x%x, buf->cpu:%d.\n", buf->srcModule, buf->cpu);
347
348 cur_sec = mktime(&rtc_tm);
349 sendbuf.srcModule = buf->srcModule;
350 sendbuf.cpu = RTC_CPU_AP;
351 sendbuf.ulSec = cur_sec;
352
353 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_get_time: send to cap cmd:0x%x, srcMod:0x%x, cur_sec:%lu \n", cmd,
354 sendbuf.srcModule, sendbuf.ulSec);
355 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
356 if (ret != 0)
357 {
358 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);;
359 }
360 }
361
362 return 0;
lh9ed821d2023-04-07 01:36:19 -0700363}
364
365/********************************************
366 * ¹¦ÄÜ:ÉèÖÃrtcʱ¼ä
367 *******************************************/
368static int handle_set_time(MSG_BUF *pmsg_buf)
369{
xf.li9d1a0e12023-09-20 01:43:20 -0700370 time_t os_time = 0;
371 struct tm tmTime = {0};
372 struct rtc_time rtc_tm = {0};
373 int ret = 0;
lh9ed821d2023-04-07 01:36:19 -0700374
xf.li9d1a0e12023-09-20 01:43:20 -0700375 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter.\n");
376 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
377 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_set_time enter: set time: %ld \n", os_time);
378 convert_time(os_time, &tmTime, CONVERT_TYPE_RTC);
379 memcpy(&rtc_tm, &tmTime, sizeof(struct rtc_time));
380 slog(RTC_PRINT, SLOG_ERR,
381 "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",
382 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,
383 rtc_tm.tm_mday, rtc_tm.tm_wday);
lh9ed821d2023-04-07 01:36:19 -0700384
xf.li9d1a0e12023-09-20 01:43:20 -0700385 ret = ioctl(g_rtc_fd, RTC_SET_TIME, &rtc_tm);
386 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: RTC_SET_TIME ioctl ret = %d exit.\n", ret);
387 {
388 char buf[32] = {0};
389 snprintf(buf, 32, "AT+ZMMI=2,%d,%d\r\n", 1900 + rtc_tm.tm_year, 1 + rtc_tm.tm_mon);
390 ret = get_modem_info(buf, NULL, NULL);
391 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_set_time ret=%d %s\n", ret, buf);
392 }
393 return 0;
lh9ed821d2023-04-07 01:36:19 -0700394}
395
396/********************************************
xf.li742dd022023-06-08 01:43:32 -0700397 * ¹¦ÄÜ:sntpͬ²½ÏµÍ³Ê±¼ä
398 *******************************************/
399static int handle_sntp_update(MSG_BUF *pmsg_buf)
400{
401 time_t os_time = 0;
xf.li9d1a0e12023-09-20 01:43:20 -0700402 struct timeval time_tv = {0};
403 int ret = 0;
xf.li742dd022023-06-08 01:43:32 -0700404
xf.li9d1a0e12023-09-20 01:43:20 -0700405 memcpy(&os_time, pmsg_buf->aucDataBuf, sizeof(time_t));
xf.li742dd022023-06-08 01:43:32 -0700406
xf.li9d1a0e12023-09-20 01:43:20 -0700407 time_tv.tv_sec = os_time;
408 ret = settimeofday(&time_tv, NULL);
409 if (ret != 0)
410 {
411 slog(RTC_PRINT, SLOG_ERR, "handle_sntp_update is fail,ret:%d \n", ret);
412 }
413 return 0;
xf.li742dd022023-06-08 01:43:32 -0700414}
415
416/********************************************
lh9ed821d2023-04-07 01:36:19 -0700417 * ¹¦ÄÜ:Ôö¼Ó¶¨Ê±Æ÷
418 *******************************************/
419static int handle_alarm_add(MSG_BUF *pmsg_buf)
420{
xf.li9d1a0e12023-09-20 01:43:20 -0700421 //long cur_rtc_time = 0;
lh9ed821d2023-04-07 01:36:19 -0700422
xf.li9d1a0e12023-09-20 01:43:20 -0700423 struct rtc_time rtc_tm = {0};
424 ALARM_T *pAlarm = 0;
lh9ed821d2023-04-07 01:36:19 -0700425
xf.li9d1a0e12023-09-20 01:43:20 -0700426 RTC_DATA_BUF *buf = pmsg_buf->aucDataBuf;
427 ALARM_T *temp = NULL;
428 int mark = 0;
lh9ed821d2023-04-07 01:36:19 -0700429
xf.li9d1a0e12023-09-20 01:43:20 -0700430 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_add enter.\n");
lh9ed821d2023-04-07 01:36:19 -0700431
xf.li9d1a0e12023-09-20 01:43:20 -0700432 if (buf->ulSec <= 0)
433 {
434 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add invalid interval.\n");
435 return -1;
436 }
lh9ed821d2023-04-07 01:36:19 -0700437
xf.li9d1a0e12023-09-20 01:43:20 -0700438 pAlarm = malloc(sizeof(ALARM_T));
439 if (pAlarm == NULL)
440 {
441 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no memory.\n");
442 return -1;
443 }
lh9ed821d2023-04-07 01:36:19 -0700444
xf.li9d1a0e12023-09-20 01:43:20 -0700445 //»ñÈ¡µ±Ç°Ê±¼ä second
446 if (ioctl(g_rtc_fd, RTC_RD_TIME, &rtc_tm) < 0) // cov M CHECKED_RETURN
447 {
448 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
449 }
450 memcpy(&pAlarm->rtc_tm, &rtc_tm, sizeof(rtc_tm));
lh9ed821d2023-04-07 01:36:19 -0700451
xf.li9d1a0e12023-09-20 01:43:20 -0700452 pAlarm->rtc_id = buf->module;
453 pAlarm->module_id = buf->srcModule;
454 pAlarm->interval = buf->ulSec ; // count down time
455 pAlarm->remain_time = buf->ulSec; // remain count down time
456 pAlarm->wakeup = buf->wakeup;
457 pAlarm->is_utc = buf->is_utc;
458 pAlarm->cpu = buf->cpu;
459 pAlarm->time_val = buf->time_val;// cur_rtc_time is mktime(&local_tm)
460 //pAlarm->time_val = get_RtcTime();// cur_rtc_time is mktime(&local_tm)
lh9ed821d2023-04-07 01:36:19 -0700461
xf.li9d1a0e12023-09-20 01:43:20 -0700462 slog(RTC_PRINT, SLOG_NORMAL,
463 "rtc-service: handle_alarm_add pAlarm->interval is %ld ,pAlarm->time_val() is %ld, is_utc:%d.\n",
464 pAlarm->interval, pAlarm->time_val, pAlarm->is_utc);
465 /*
466 if (pAlarm->time_val + pAlarm->remain_time < cur_rtc_time) {
467 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);
468 }
469 */
lh9ed821d2023-04-07 01:36:19 -0700470
471#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700472 pAlarm->is_utc = 0; //´Ëģʽ϶¼°´timerÉèÖö¨Ê±Æ÷£¬²»Ö§³Öutc
473 pAlarm->wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700474#endif
475
xf.li9d1a0e12023-09-20 01:43:20 -0700476 if (pAlarm->is_utc == 0)
477 {
478 if (!list_empty(&rtc_timer_list))
479 {
480 //update_timer_remain(pAlarm->remain_time);
481 list_for_each_entry(temp, &rtc_timer_list, list)
482 {
483 if (temp->remain_time > pAlarm->remain_time)
484 {
485 mark = 1;
486 list_add(&pAlarm->list, temp->list.prev);
487 break;
488 }
489 }
490 }
491 if (mark == 0)
492 {
493 list_add_tail(&pAlarm->list, &rtc_timer_list);
494 }
495 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc timer\n");
496 alarm_restart(&rtc_timer_list);
497 }
498 else
499 {
500 if (!list_empty(&rtc_alarm_list))
501 {
502 list_for_each_entry(temp, &rtc_alarm_list, list)
503 {
504 if (temp->interval + temp->time_val > pAlarm->interval + pAlarm->time_val)
505 {
506 mark = 1;
507 list_add(&pAlarm->list, temp->list.prev);
508 break;
509 }
510 }
511 }
512 if (mark == 0)
513 {
514 list_add_tail(&pAlarm->list, &rtc_alarm_list);
515 }
lh9ed821d2023-04-07 01:36:19 -0700516
xf.li9d1a0e12023-09-20 01:43:20 -0700517 if (pAlarm->wakeup)
518 {
519 save_rtc_alarm();
520 }
lh9ed821d2023-04-07 01:36:19 -0700521
xf.li9d1a0e12023-09-20 01:43:20 -0700522 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: add rtc alarm \n");
523 alarm_restart(&rtc_alarm_list);
524 }
525
526 return 0;
lh9ed821d2023-04-07 01:36:19 -0700527}
528
529/********************************************
530 * ¹¦ÄÜ:°´µ±Ç°¶¨Ê±Æ÷Ê£Óàʱ¼ä¸üж¨Ê±Æ÷
531 *******************************************/
532static void update_timer_remain(int new_remian)
533{
xf.li9d1a0e12023-09-20 01:43:20 -0700534 if (list_empty(&rtc_timer_list))
535 {
536 return;
537 }
538 int ret, remain_time;
539 ALARM_T *pTmp;
540 //list_entry(pTmp, &rtc_timer_list, list);
541 pTmp = list_entry(rtc_timer_list.next, ALARM_T, list);
lh9ed821d2023-04-07 01:36:19 -0700542
543#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700544 time_t cur = time(NULL);
545 remain_time = g_set_softtime + pTmp->remain_time - cur;
546 if ((remain_time > pTmp->remain_time) || (remain_time <= 0))
547 {
548 slog(RTC_PRINT, SLOG_ERR, "rtc-service: cur:%u, set:%u, remain:%u, inveral:%u\n", cur, g_set_softtime, remain_time,
549 pTmp->remain_time);
550 //ʱ¼ä·¢Éú¹ýÖØÖõȣ¬Ö±½ÓÉèÖö¨Ê±Æ÷µ½£¬¸÷Ä£¿éÓ¦ÔÚÊÕµ½RTC_MSG_TIME_CHANGEDÏûϢʱ¼°Ê±¸üж¨Ê±Æ÷
551 remain_time = 0;
552 }
lh9ed821d2023-04-07 01:36:19 -0700553#else
xf.li9d1a0e12023-09-20 01:43:20 -0700554 ret = ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time);
lh9ed821d2023-04-07 01:36:19 -0700555#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700556 if (ret < 0)
557 {
558 slog(RTC_PRINT, SLOG_ERR, "rtc-service: handle_alarm_add no free alarm remain_time %d %s errno.\n", errno,
559 strerror(errno));
560 // return -1;
561 }
562 //if(remain_time > new_remian)
563 {
564 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: pTmp->remain_time: %d, rtc remain_time: %d \n", pTmp->remain_time,
565 remain_time);
566 alarm_adjust_value(pTmp->remain_time - remain_time);
567 alarm_fix_value(&rtc_timer_list);
568 }
lh9ed821d2023-04-07 01:36:19 -0700569}
570
571/********************************************
572 * ¹¦ÄÜ:ɾ³ýÏûÏ¢Ô´Ä£¿éµÄËùÓж¨Ê±Æ÷
573 *******************************************/
574static int handle_alarm_del(MSG_BUF *pmsg_buf)
575{
xf.li9d1a0e12023-09-20 01:43:20 -0700576 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
577 ALARM_T *pTmp, *pTmp1;
578 int wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700579
xf.li9d1a0e12023-09-20 01:43:20 -0700580 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del enter.\n");
581 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
582 {
583 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del timer module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
584 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
585 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id) && (buf->cpu == pTmp->cpu))
586 {
587 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del timer module:0x%x , src:0x%x , cpu:%d \n",
588 buf->module, buf->srcModule, buf->cpu);
589 list_del(&pTmp->list);
590 free(pTmp);
591 break;
592 }
593 }
lh9ed821d2023-04-07 01:36:19 -0700594
xf.li9d1a0e12023-09-20 01:43:20 -0700595 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
596 {
597 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del alarm module:0x%x - 0x%x, src:0x%x - 0x%x.\n",
598 buf->module, pTmp->rtc_id, buf->srcModule, pTmp->module_id);
599 if ((buf->module == pTmp->rtc_id) && (buf->srcModule == pTmp->module_id) && (buf->cpu == pTmp->cpu))
600 {
601 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_del alarm module:0x%x , src:0x%x , cpu:%d \n",
602 buf->module, buf->srcModule, buf->cpu);
603 wakeup = pTmp->wakeup;
604 list_del(&pTmp->list);
605 free(pTmp);
606 break;
607 }
608 }
lh9ed821d2023-04-07 01:36:19 -0700609
xf.li9d1a0e12023-09-20 01:43:20 -0700610 if (wakeup)
611 {
612 save_rtc_alarm();
613 }
lh9ed821d2023-04-07 01:36:19 -0700614
xf.li9d1a0e12023-09-20 01:43:20 -0700615 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_del exit.\n");
616
617 return 0;
lh9ed821d2023-04-07 01:36:19 -0700618}
619
xf.libdd93d52023-05-12 07:10:14 -0700620void send_ok_del_rsp(MSG_BUF *pmsg_buf)
621{
xf.li9d1a0e12023-09-20 01:43:20 -0700622 RTC_DATA_BUF * buf = pmsg_buf->aucDataBuf;
xf.libdd93d52023-05-12 07:10:14 -0700623
xf.li9d1a0e12023-09-20 01:43:20 -0700624 if (buf->cpu == RTC_CPU_CAP)
625 {
626 int ret = 0;
627 RTC_DATA_BUF sendbuf = {0};
628 sendbuf.cpu = RTC_CPU_AP;
629 sendbuf.module = buf->module;
630 sendbuf.srcModule = buf->srcModule;
631 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, RTC_MSG_DEL_ALARM, sizeof(RTC_DATA_BUF), (void *)&sendbuf);
632 if (ret != 0)
633 {
634 slog(RTC_PRINT, SLOG_ERR, "send_ok_del_rsp: failed,ret:%d\n", ret);
635 }
636 }
xf.libdd93d52023-05-12 07:10:14 -0700637}
lh9ed821d2023-04-07 01:36:19 -0700638/********************************************
639 *¹¦ÄÜ:¶¨Ê±Æ÷³¬Ê±´¦Àí
640 *******************************************/
641static int handle_alarm_timeout(MSG_BUF *pmsg_buf)
642{
xf.li9d1a0e12023-09-20 01:43:20 -0700643 long ret = -1;
644 ALARM_T *pAlarm;
645 RTC_DATA_BUF buf = {0};
lh9ed821d2023-04-07 01:36:19 -0700646 long rtc_report_time = 0;
647 long alarm_node_time = 0;
xf.li9d1a0e12023-09-20 01:43:20 -0700648 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
649 int wakeup = 0;
lh9ed821d2023-04-07 01:36:19 -0700650
xf.li9d1a0e12023-09-20 01:43:20 -0700651 if (cmd == RTC_MSG_TIMER_TIMEOUT && !list_empty(&rtc_timer_list))
652 {
653 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc timer timeout\n");
654 pAlarm = list_entry(rtc_timer_list.next, ALARM_T, list);
655 }
656 else if (cmd == RTC_MSG_ALARM_TIMEOUT && !list_empty(&rtc_alarm_list))
657 {
658 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: rtc alarm timeout\n");
659 pAlarm = list_entry(rtc_alarm_list.next, ALARM_T, list);
lh9ed821d2023-04-07 01:36:19 -0700660 rtc_report_time = atol(pmsg_buf->aucDataBuf);
xf.li9d1a0e12023-09-20 01:43:20 -0700661 if (rtc_report_time < 0 || rtc_report_time > LONG_MAX - 1)
662 {
663 rtc_report_time = 0;
lh9ed821d2023-04-07 01:36:19 -0700664 }
665 alarm_node_time = pAlarm->interval + pAlarm->time_val;
xf.li9d1a0e12023-09-20 01:43:20 -0700666 if ((rtc_report_time > alarm_node_time && (rtc_report_time - alarm_node_time) > g_time_offset)
667 || (alarm_node_time > rtc_report_time && (alarm_node_time - rtc_report_time) > g_time_offset))
lh9ed821d2023-04-07 01:36:19 -0700668 {
xf.li9d1a0e12023-09-20 01:43:20 -0700669 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc node was deleted, timeout unexpected; node:%ld, rtc:%ld \n",
670 alarm_node_time, rtc_report_time);
671 alarm_fix_value(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700672
xf.li9d1a0e12023-09-20 01:43:20 -0700673 if (pAlarm->wakeup)
674 {
675 save_rtc_alarm();
676 }
677
678 alarm_restart(&rtc_alarm_list);
679 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
680 return 0;
681 }
682 buf.time_val = alarm_node_time;
683 }
684 else
685 {
686 slog(RTC_PRINT, SLOG_ERR, "rtc-service: timeout unexpected\n");
687 return -1;
688 }
689
690 wakeup = pAlarm->wakeup;
691 list_del(&pAlarm->list);
692 buf.module = pAlarm->rtc_id;
693 buf.srcModule = pAlarm->module_id;
694 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: handle_alarm_timeout: target:0x%x, rtc_id:0x%x, utc:%d \n",
695 pAlarm->module_id, buf.module, pAlarm->is_utc);
696 if (pAlarm->cpu == RTC_CPU_AP)
697 {
698 ret = ipc_send_message2(MODULE_ID_RTC_SERVICE, pAlarm->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
699 (unsigned char*)&buf, 0);
700 if (ret != 0)
701 {
702 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);
703 }
704 }
705 else
706 {
707 buf.module = pAlarm->rtc_id;
xf.libdd93d52023-05-12 07:10:14 -0700708 buf.srcModule = pAlarm->module_id;
709 buf.cpu = RTC_CPU_AP;
710 buf.is_utc = pAlarm->is_utc;
lh9ed821d2023-04-07 01:36:19 -0700711
xf.li9d1a0e12023-09-20 01:43:20 -0700712 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
713 cmd, buf.srcModule, buf.module, pAlarm->is_utc);
xf.libdd93d52023-05-12 07:10:14 -0700714 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
715 if (ret != 0)
716 {
xf.li9d1a0e12023-09-20 01:43:20 -0700717 slog(RTC_PRINT, SLOG_ERR, "rtc-service: ipc_send_message unexpected,ret:%d\n", ret);;
xf.libdd93d52023-05-12 07:10:14 -0700718 }
xf.li9d1a0e12023-09-20 01:43:20 -0700719 }
720 if (pAlarm->is_utc == 0)
721 {
722 alarm_adjust_value(pAlarm->remain_time);
723 alarm_fix_value(&rtc_timer_list);
724 alarm_restart(&rtc_timer_list);
725 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart imer.\n");
726 }
727 else
728 {
729 alarm_fix_value(&rtc_alarm_list);
730 if (wakeup)
731 {
732 save_rtc_alarm();
733 }
734 alarm_restart(&rtc_alarm_list);
735 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout restart alarm.\n");
736 }
lh9ed821d2023-04-07 01:36:19 -0700737
xf.li9d1a0e12023-09-20 01:43:20 -0700738 free(pAlarm);
739 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: handle_alarm_timeout exit.\n");
740 return 0;
lh9ed821d2023-04-07 01:36:19 -0700741}
742
743/********************************************
744 * ¹¦ÄÜ:ɾ³ý¹Ø»ú²»±£´æµÄ¶¨Ê±Æ÷
745 *******************************************/
746static void handle_del_all(MSG_BUF *pmsg_buf)
747{
xf.li9d1a0e12023-09-20 01:43:20 -0700748 ALARM_T *pTmp, *pTmp1;
lh9ed821d2023-04-07 01:36:19 -0700749
xf.li9d1a0e12023-09-20 01:43:20 -0700750 update_timer_remain(0);
751 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
752 {
753 if (pTmp->wakeup == 0)
754 {
755 list_del(&pTmp->list);
756 free(pTmp);
757 }
758 }
759 alarm_restart(&rtc_timer_list);
lh9ed821d2023-04-07 01:36:19 -0700760
xf.li9d1a0e12023-09-20 01:43:20 -0700761 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
762 {
763 if (pTmp->wakeup == 0)
764 {
765 list_del(&pTmp->list);
766 free(pTmp);
767 }
768 }
769 alarm_restart(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700770}
771
772/********************************************
773 * ¹¦ÄÜ:sntpͬ²½³É¹¦ºó£¬¸üÐÂalarm_list
774 *******************************************/
775static void update_alarm()
776{
xf.li9d1a0e12023-09-20 01:43:20 -0700777 ALARM_T *pTmp, *pTmp1;
778 //long sec = get_RtcTime();
779 long sec = time(0);
780 RTC_DATA_BUF buf = {0};
781
lh9ed821d2023-04-07 01:36:19 -0700782#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700783 list_for_each_entry_safe(pTmp, pTmp1, &rtc_timer_list, list)
784 {
785 buf.module = pTmp->rtc_id;
786 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF),
787 (unsigned char*)&buf, 0);
788 if (pTmp->is_utc)
789 {
790 if (pTmp->interval + pTmp->time_val < sec)
791 {
792 list_del(&pTmp->list);
793 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED timer, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id,
794 (pTmp->interval + pTmp->time_val));
795 free(pTmp);
796 }
797 else
798 {
799 pTmp->remain_time = pTmp->interval + pTmp->time_val - sec;
800 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: timer adjust, alarm_id(%d), alarm_time(%ld), remain(%ld).\n", pTmp->rtc_id,
801 (pTmp->interval + pTmp->time_val), pTmp->remain_time);
802 }
803 }
804 }
805 alarm_restart(&rtc_timer_list);
lh9ed821d2023-04-07 01:36:19 -0700806#else
xf.li9d1a0e12023-09-20 01:43:20 -0700807 list_for_each_entry_safe(pTmp, pTmp1, &rtc_alarm_list, list)
808 {
809 buf.module = pTmp->rtc_id;
810 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_TIME_CHANGED, sizeof(RTC_DATA_BUF),
811 (unsigned char*)&buf, 0);
812 if (pTmp->interval + pTmp->time_val < sec)
813 {
814 list_del(&pTmp->list);
815 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: delete ELAPSED alarm, alarm_id(%d), alarm_time(%ld).\n", pTmp->rtc_id,
816 (pTmp->interval + pTmp->time_val));
817 free(pTmp);
818 }
819 }
820 alarm_restart(&rtc_alarm_list);
lh9ed821d2023-04-07 01:36:19 -0700821#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700822 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: update_alarm exit cur_rtc_time: % ld.\n", sec);
lh9ed821d2023-04-07 01:36:19 -0700823}
824
825/********************************************
826 * ¹¦ÄÜ:¸üж¨Ê±Æ÷Ê£Óàʱ¼ä
827 *******************************************/
828static void alarm_adjust_value(int time)
829{
xf.li9d1a0e12023-09-20 01:43:20 -0700830 ALARM_T * temp;
831 list_for_each_entry(temp, &rtc_timer_list, list)
832 {
833 temp->remain_time -= time;
834 }
lh9ed821d2023-04-07 01:36:19 -0700835}
836
837/********************************************
838 *¹¦ÄÜ: ɾ³ýÎÞЧ¶¨Ê±Æ÷
839 *******************************************/
840static void alarm_fix_value(struct list_head *rtc_list)
841{
xf.li9d1a0e12023-09-20 01:43:20 -0700842 ALARM_T *pTmp, *pTmp1;
843 RTC_DATA_BUF buf = {0};
844 int wakeup = 0;
845 int ret = 0;
846 enum RTC_MSG_CMD cmd = 0;
lh9ed821d2023-04-07 01:36:19 -0700847
xf.li9d1a0e12023-09-20 01:43:20 -0700848 if (rtc_list == &rtc_timer_list)
849 {
850 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list)
851 {
852 if (pTmp->remain_time <= 0)
853 {
854 buf.module = pTmp->rtc_id;
855 if (pTmp->cpu == RTC_CPU_CAP)
856 {
857 buf.srcModule = pTmp->module_id;
858 buf.cpu = RTC_CPU_AP;
859 buf.is_utc = pTmp->is_utc;
860 cmd = RTC_MSG_TIMER_TIMEOUT;
861 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
862 cmd, buf.srcModule, buf.module, buf.is_utc);
863 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
864 if (ret != 0)
865 {
866 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n", ret);;
867 }
868 }
869 else
870 {
871 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
872 (unsigned char*)&buf, 0);
873 }
874 list_del(&pTmp->list);
875 free(pTmp);
876 }
877 }
878 }
879 else if (rtc_list == &rtc_alarm_list)
880 {
881 list_for_each_entry_safe(pTmp, pTmp1, rtc_list, list)
882 {
883 if (pTmp->time_val + pTmp->interval <= (time(0) + g_time_offset))
884 {
885 if (pTmp->wakeup)
886 {
887 wakeup = pTmp->wakeup;
888 }
889
890 buf.module = pTmp->rtc_id;
lh758261d2023-07-13 05:52:04 -0700891 buf.time_val = pTmp->time_val + pTmp->interval;
xf.li9d1a0e12023-09-20 01:43:20 -0700892 if (pTmp->cpu == RTC_CPU_CAP)
893 {
894 buf.srcModule = pTmp->module_id;
895 buf.cpu = RTC_CPU_AP;
896 buf.is_utc = pTmp->is_utc;
897 cmd = RTC_MSG_ALARM_TIMEOUT;
898 slog(RTC_PRINT, SLOG_NORMAL, "[rtc] handle_alarm_timeout: send to cap cmd:0x%x, srcMod:0x%x, module:0x%x, utc:%d \n",
899 cmd, buf.srcModule, buf.module, buf.is_utc);
900 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, sizeof(RTC_DATA_BUF), (void *)&buf);
901 if (ret != 0)
902 {
903 slog(RTC_PRINT, SLOG_ERR, "rtc-service: send_soc_msg unexpected,ret:%d\n", ret);;
904 }
905 }
906 else
907 {
908 ipc_send_message2(MODULE_ID_RTC_SERVICE, pTmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
909 (unsigned char*)&buf, 0);
910 }
911 list_del(&pTmp->list);
912 free(pTmp);
913 }
914 }
lh9ed821d2023-04-07 01:36:19 -0700915
xf.li9d1a0e12023-09-20 01:43:20 -0700916 if (wakeup)
917 {
918 save_rtc_alarm();
919 }
920 }
921 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_fix_value exit.\n");
lh9ed821d2023-04-07 01:36:19 -0700922}
923
924#ifdef PRODUCT_NOT_USE_RTC
925/*¶ÔӦûÓÐrtcµÄ²úÆ·£¬rtc¶¨Ê±Æ÷ÊÊÅäµ½softtimerµÄ»Øµ÷º¯Êý*/
926static void rtc_set_softtimer_cb(void *param)
927{
xf.li9d1a0e12023-09-20 01:43:20 -0700928 int ret = -1;
lh9ed821d2023-04-07 01:36:19 -0700929
xf.li9d1a0e12023-09-20 01:43:20 -0700930 slog(NET_PRINT, SLOG_ERR, "rtc-service: rtc_adapt_softtimer_cb \n");
931
932 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, 0, 0);;
933 if (ret != 0)
934 {
935 slog(MISC_PRINT, SLOG_ERR, "rtc-service: set rtc timer send msg failed \n");
936 }
937 return 0;
lh9ed821d2023-04-07 01:36:19 -0700938}
939#endif
940
941/********************************************
942 * ¹¦ÄÜ:ÖØÆô¶¨Ê±Æ÷£¬Ã»ÓÐÔò¹Ø±Õ¶¨Ê±Æ÷
943 *******************************************/
944static void alarm_restart(struct list_head *rtc_list)
945{
xf.li9d1a0e12023-09-20 01:43:20 -0700946 int ret = 0;
947 //struct tm *p_tm = 0;
948 struct tm ret_tm = {0};
949 struct rtc_wkalrm alarm = {0};
950 long tVal = 0;
951 long remain = -1;
952 ALARM_T *ptmp = NULL;
lh9ed821d2023-04-07 01:36:19 -0700953 long cur_rtc_time = time(0);
xf.li9d1a0e12023-09-20 01:43:20 -0700954 RTC_DATA_BUF buf = {0};
lh9ed821d2023-04-07 01:36:19 -0700955NEXT:
xf.li9d1a0e12023-09-20 01:43:20 -0700956 if (!list_empty(rtc_list))
957 {
958 ptmp = list_entry(rtc_list->next, ALARM_T, list);
959 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart enter.\n");
960 memset(&buf, 0, sizeof(RTC_DATA_BUF));
961 if (ptmp->is_utc == 0)
962 {
lh9ed821d2023-04-07 01:36:19 -0700963#ifdef PRODUCT_NOT_USE_RTC
xf.li9d1a0e12023-09-20 01:43:20 -0700964 g_set_softtime = time(NULL);
965 sc_timer_delete(RTC_USE_SOFTTIMER_ID);
966 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 -0700967#else
xf.li9d1a0e12023-09-20 01:43:20 -0700968 ret = ioctl(g_rtc_fd, ZX234290_SET_TIMER, ptmp->remain_time);
lh9ed821d2023-04-07 01:36:19 -0700969#endif
xf.li9d1a0e12023-09-20 01:43:20 -0700970 if (ret != 0)
971 {
972 slog(RTC_PRINT, SLOG_ERR, "rtc-service: alarm_restart timer: set failed, ret:%d \n", ret);
973 return;
974 }
975 slog(RTC_PRINT, SLOG_NORMAL,
976 "rtc-service: alarm_restart: set timer ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x\n",
977 ptmp->remain_time, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
978 }
979 else
980 {
981 tVal = ptmp->interval + ptmp->time_val;
982 remain = tVal - cur_rtc_time;
983 if (remain <= g_time_offset)
984 {
985 //¹ýʱ¶¨Ê±Æ÷¶ªÆú´¦Àí£¬Èç¹û¶àÄ£¿éÕýºÃÉèµÄ»ù±¾ÔÚͬһ¸öʱ¼äµã£¬ºóÕ߻ᱻ¶ªÆú£¬¿¼ÂÇÐ޸ijÉÖ±½Ó֪ͨģ¿éʱ¼äµ½
986 slog(RTC_PRINT, SLOG_ERR,
987 "rtc-service: remain time error, time_set:%ld, remain:%ld, alarmer:%ld, interval:%ld, current:%ld, module:0x%x, rtc_id:0x%x\n",
988 ptmp->time_val, remain, tVal, ptmp->interval, cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
989 buf.module = ptmp->rtc_id;
990 buf.time_val = ptmp->time_val + ptmp->interval;
991 ipc_send_message2(MODULE_ID_RTC_SERVICE, ptmp->module_id, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF),
992 (unsigned char*)&buf, 0);
993 list_del(&ptmp->list);
994 free(ptmp);
995 ptmp = NULL;
996 goto NEXT;
997 }
lh9ed821d2023-04-07 01:36:19 -0700998
xf.li9d1a0e12023-09-20 01:43:20 -0700999 //localtime_r(&tVal, &ret_tm);
1000 gmtime_r(&tVal, &ret_tm);
1001 memcpy(&alarm.time, &ret_tm, sizeof(struct rtc_time));
1002 alarm.enabled = 1;
lh9ed821d2023-04-07 01:36:19 -07001003
xf.li9d1a0e12023-09-20 01:43:20 -07001004 /*Ê×ÏÈ´ò¿ªalarm ÖжÏ*/
1005 ret = ioctl(g_rtc_fd, RTC_AIE_ON);
1006 if (ret != 0)
1007 {
1008 slog(RTC_PRINT, SLOG_ERR, "rtc-service: open alarm failed, ret:%d\n", ret);
1009 return;
1010 }
lh9ed821d2023-04-07 01:36:19 -07001011
xf.li9d1a0e12023-09-20 01:43:20 -07001012 /*ÉèÖÃalarm ʱ¼ä*/
1013 ret = ioctl(g_rtc_fd, RTC_WKALM_SET, &alarm);
1014 if (ret != 0)
1015 {
1016 slog(RTC_PRINT, SLOG_ERR, "rtc-service: set alarm failed, ret:%d\n", ret);
1017 return;
1018 }
1019 slog(RTC_PRINT, SLOG_NORMAL,
1020 "rtc-service: alarm_restart set alarm ok, remain_time:%ld, cur_rtc_time:%ld, module:0x%x, rtc_id:0x%x \n", remain,
1021 cur_rtc_time, ptmp->module_id, ptmp->rtc_id);
1022 }
1023 }
1024 else
1025 {
1026 if (rtc_list == &rtc_timer_list)
1027 {
1028 //¹Ø±Õrtc time, Éè¼ä¸ôʱ¼äΪ0
1029 ret = ioctl(g_rtc_fd, ZX234290_TIMER_ENABLED, 0);
1030 if (ret != 0)
1031 {
1032 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable timer failed, ret:%d\n", ret);
1033 return;
1034 }
1035 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable timer ok \n");
1036 }
1037 else
1038 {
1039 /*¹Ø±Õalarm ÖжÏ*/
1040 ret = ioctl(g_rtc_fd, RTC_AIE_OFF);
1041 if (ret != 0)
1042 {
1043 slog(RTC_PRINT, SLOG_ERR, "rtc-service: disable alarm failed, ret:%d\n", ret);
1044 return;
1045 }
1046 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: alarm_restart disable alarm ok \n");
1047 }
1048 }
1049 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: alarm_restart exit.\n");
lh9ed821d2023-04-07 01:36:19 -07001050}
1051
1052static void rtc_set_local_time(MSG_BUF *msg)
1053{
xf.li9d1a0e12023-09-20 01:43:20 -07001054 struct tm set_tm = {0};
1055 struct timeval time_tv;
lh9ed821d2023-04-07 01:36:19 -07001056
xf.li9d1a0e12023-09-20 01:43:20 -07001057 slog(RTC_PRINT, SLOG_DEBUG, "rtc_set_local_time, %d, %d \n", sizeof(struct tm), msg->usDataLen);
lh9ed821d2023-04-07 01:36:19 -07001058
xf.li9d1a0e12023-09-20 01:43:20 -07001059 memcpy(&set_tm, msg->aucDataBuf, msg->usDataLen);
lh9ed821d2023-04-07 01:36:19 -07001060
xf.li9d1a0e12023-09-20 01:43:20 -07001061 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time, year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d, wday:%d \n",
1062 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 -07001063
xf.li9d1a0e12023-09-20 01:43:20 -07001064 time_tv.tv_sec = mktime(&set_tm);
1065 if (time_tv.tv_sec < 0)
1066 {
lh9ed821d2023-04-07 01:36:19 -07001067
xf.li9d1a0e12023-09-20 01:43:20 -07001068 slog(RTC_PRINT, SLOG_DEBUG, "mktime fail, set time_tv.tv_sec to 0 \n");
1069 time_tv.tv_sec = 0;
1070 }
1071 time_tv.tv_usec = 0;
1072
1073 if (0 != settimeofday(&time_tv, NULL))
1074 {
1075 slog(RTC_PRINT, SLOG_ERR, "rtc_set_local_time failed \n");
1076 }
lh9ed821d2023-04-07 01:36:19 -07001077}
1078
1079/********************************************
1080 * process_msg
1081 * handle the specified MSG.
1082 *******************************************/
1083static int process_msg(MSG_BUF *pmsg_buf)
1084{
xf.li9d1a0e12023-09-20 01:43:20 -07001085 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
1086 unsigned short req_cmd = 0;
lh9ed821d2023-04-07 01:36:19 -07001087
xf.li9d1a0e12023-09-20 01:43:20 -07001088 switch (cmd)
1089 {
1090 case RTC_MSG_GET_TIME:
1091 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_GET_TIME, src:0x%x.\n", pmsg_buf->src_id);
1092 handle_get_time(pmsg_buf);
1093 break;
1094 case RTC_MSG_SET_TIME:
1095 security_log(pmsg_buf->src_id, "set time");
1096 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_SET_TIME, src:0x%x.\n", pmsg_buf->src_id);
1097 handle_set_time(pmsg_buf);
1098 update_alarm();
1099 break;
1100 case RTC_MSG_SNTP_UPDATE:
1101 handle_sntp_update(pmsg_buf);
1102 break;
1103 case RTC_MSG_ADD_ALARM:
1104 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_ADD_ALARM, src:0x%x, g_clean_flag = %d.\n",
1105 pmsg_buf->src_id, g_clean_flag);
1106 //Ìí¼ÓrtcǰÏÈɾ³ý´Ërtc_idÒÑÌí¼Ó¼Ç¼£¬Ã¿¸örtc_idÖ»±£Áô1¸ö¶¨Ê±Æ÷
1107 if (0 == g_clean_flag)
1108 {
1109 update_timer_remain(0);
1110 handle_alarm_del(pmsg_buf);
1111 handle_alarm_add(pmsg_buf);
1112 }
1113 else
1114 {
1115 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: g_clean_flag is nonzero:0x%x, dont add alarm.\n", g_clean_flag);
1116 }
lh9ed821d2023-04-07 01:36:19 -07001117
xf.li9d1a0e12023-09-20 01:43:20 -07001118 //wifi´¦ÀíÍêǰ²»ÄÜÐÝÃߣ¬ÔÝʱʹÓÃÑÓÊ±Ëø£¬´ý·½°¸×鏸³ö·½°¸ºóÔÙµ÷Õû
1119 //if (pmsg_buf->usSourceModuleID == MODULE_ID_WDT)
1120 //write_lockfile("/sys/power/wake_unlock", "rtc_io_time_timeout_lock");
1121
1122 break;
1123 case RTC_MSG_DEL_ALARM:
1124 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALARM, src:0x%x.\n", pmsg_buf->src_id);
1125 update_timer_remain(0);
1126 handle_alarm_del(pmsg_buf);
1127 send_ok_del_rsp(pmsg_buf);
1128 alarm_restart(&rtc_timer_list);
1129 alarm_restart(&rtc_alarm_list);
1130 break;
1131 case RTC_MSG_ALARM_TIMEOUT:
1132 case RTC_MSG_TIMER_TIMEOUT:
1133 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_TIMEOUT, src:0x%x, cmd:0x%x.\n", pmsg_buf->src_id, cmd);
1134 handle_alarm_timeout(pmsg_buf);
1135 break;
1136 case RTC_MSG_DEL_ALL:
1137 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service: process_msg RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
1138 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
1139 if (MSG_CMD_RESET_RSP == req_cmd || MSG_CMD_POWEROFF_RSP == req_cmd || MSG_CMD_RESTART_RSP == req_cmd)
1140 {
1141 g_clean_flag = req_cmd;
1142 }
1143 handle_del_all(pmsg_buf);
lh9ed821d2023-04-07 01:36:19 -07001144 save_rtc_alarm();
xf.li9d1a0e12023-09-20 01:43:20 -07001145 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
1146 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
1147 break;
lh9ed821d2023-04-07 01:36:19 -07001148
xf.li9d1a0e12023-09-20 01:43:20 -07001149 default:
1150 break;
1151 }
1152 return 0;
lh9ed821d2023-04-07 01:36:19 -07001153}
1154
xf.li6c8fc1e2023-08-12 00:11:09 -07001155/********************************************
1156 * process_msg_rtc_ap
1157 * handle the specified MSG to cap, because rtc driver is at cap
1158 *******************************************/
1159static int process_msg_rtc_cap(MSG_BUF *pmsg_buf)
1160{
xf.li9d1a0e12023-09-20 01:43:20 -07001161 enum RTC_MSG_CMD cmd = pmsg_buf->usMsgCmd;
1162 unsigned short req_cmd = 0;
1163 int ret = 0;
1164 struct tm *ptm = NULL;
1165 RTC_DATA_BUF *buf = NULL;
1166 int dst_id = 0;
1167
1168 switch (cmd)
1169 {
1170 case RTC_MSG_GET_TIME:
1171 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1172 slog(RTC_PRINT, SLOG_NORMAL, "to cap: RTC_MSG_GET_TIME \n");
1173 if (buf->cpu == RTC_CPU_AP)
1174 {
1175 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1176 if (ret != 0)
1177 {
1178 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1179 }
1180 }
1181 else
1182 {
1183 slog(RTC_PRINT, SLOG_NORMAL, "receive rtc time from cap core : RTC_MSG_GET_TIME :%lu \n", buf->ulSec);
1184
1185 }
1186 break;
1187 case RTC_MSG_SET_TIME:
1188 ptm = (struct tm *)pmsg_buf->aucDataBuf;
1189 if (pmsg_buf->src_id == MODULE_ID_CP_RTC_SERVICE)
1190 {
1191 rtc_set_local_time(pmsg_buf);
1192 }
1193 else
1194 {
1195 /*
1196 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);
1197 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1198 if (ret != 0)
1199 {
1200 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n",ret);
1201 }
1202 */
1203 }
1204 break;
1205 case RTC_MSG_SNTP_UPDATE:
1206 handle_sntp_update(pmsg_buf);
1207 break;
1208 case RTC_MSG_ADD_ALARM:
1209 case RTC_MSG_DEL_ALARM:
1210 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1211 if (buf->cpu == RTC_CPU_AP)
1212 {
1213 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,
1214 buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
1215 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1216 if (ret != 0)
1217 {
1218 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1219 }
1220 }
1221 else
1222 {
1223 slog(RTC_PRINT, SLOG_NORMAL, "from cap: cmd:0x%x, module:0x%x, msg_id:0x%x, del ok \n", cmd, buf->module,
1224 buf->srcModule);
1225 }
1226 break;
1227 case RTC_MSG_ALARM_TIMEOUT:
1228 case RTC_MSG_TIMER_TIMEOUT:
1229 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1230 if (buf->cpu == RTC_CPU_CAP)
1231 {
1232 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,
1233 buf->module, buf->srcModule, buf->ulSec, buf->cpu, buf->is_utc, buf->wakeup);
1234
1235 ret = ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_ALARM_ELAPSED, sizeof(RTC_DATA_BUF), buf, 0);
1236 if (ret != 0)
1237 {
1238 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1239 }
1240 }
1241 break;
1242 case RTC_MSG_DEL_ALL:
1243 slog(RTC_PRINT, SLOG_NORMAL, "process_msg_to_cap RTC_MSG_DEL_ALL, src:0x%x.\n", pmsg_buf->src_id);
1244 memcpy(&req_cmd, pmsg_buf->aucDataBuf, sizeof(unsigned short));
1245 //ÕâÀﻹÐèҪ֪ͨÖ÷¿Ø
1246 ipc_send_message(MODULE_ID_RTC_SERVICE, pmsg_buf->src_id, req_cmd, 0, 0, 0);
1247 ret = send_soc_msg(FAR_PS, MODULE_ID_RTC_SERVICE, cmd, pmsg_buf->usDataLen, (void *)pmsg_buf->aucDataBuf);
1248 if (ret != 0)
1249 {
1250 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: ipc_send_message unexpected,ret:%d\n", ret);
1251 }
1252 break;
1253 case RTC_MSG_TIME_CHANGED:
1254 buf = (RTC_DATA_BUF *)pmsg_buf->aucDataBuf;
1255 slog(RTC_PRINT, SLOG_ERR, "process_msg_to_cap: RTC_MSG_TIME_CHANGED, dst_id:0x%x.\n", buf->srcModule);
1256 ipc_send_message(MODULE_ID_RTC_SERVICE, buf->srcModule, RTC_MSG_TIME_CHANGED, 0, 0, 0);
1257 break;
1258 default:
1259 break;
1260 }
1261 return 0;
xf.li6c8fc1e2023-08-12 00:11:09 -07001262}
1263
lh9ed821d2023-04-07 01:36:19 -07001264static int rtc_service_sock_init()
1265{
1266 int sockfd = 0;
xf.li9d1a0e12023-09-20 01:43:20 -07001267 int ret;
1268 struct sockaddr_nl snl;
1269
1270 bzero(&snl, sizeof(struct sockaddr_nl));
1271 snl.nl_family = AF_NETLINK;
lh9ed821d2023-04-07 01:36:19 -07001272 snl.nl_pid = getpid();
xf.li9d1a0e12023-09-20 01:43:20 -07001273 snl.nl_groups = 1;
lh9ed821d2023-04-07 01:36:19 -07001274
1275 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
xf.li9d1a0e12023-09-20 01:43:20 -07001276 {
lh9ed821d2023-04-07 01:36:19 -07001277 perror ("signal");
lh9ed821d2023-04-07 01:36:19 -07001278 }
lh9ed821d2023-04-07 01:36:19 -07001279
xf.li9d1a0e12023-09-20 01:43:20 -07001280 sockfd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1281 if (sockfd < 0)
1282 {
1283 slog(RTC_PRINT, SLOG_ERR, "create rtc service socket failed!\n");
1284 return -1;
1285 }
1286 // setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));
lh9ed821d2023-04-07 01:36:19 -07001287
xf.li9d1a0e12023-09-20 01:43:20 -07001288 ret = bind(sockfd, (struct sockaddr *)&snl, sizeof(struct sockaddr_nl));
1289 if (ret < 0)
1290 {
1291 slog(RTC_PRINT, SLOG_ERR, "rtc service socket bind fail!\n");
1292 close(sockfd);
1293 return -1;
1294 }
1295
1296 return sockfd;
lh9ed821d2023-04-07 01:36:19 -07001297}
1298//change@/devices/platform/zx29_i2c.0/i2c-0/0-0012/zx234290-rtc
1299static int rtc_time_msg_parse(const char *msg, int msglen)
1300{
xf.li9d1a0e12023-09-20 01:43:20 -07001301 int offlen = 0;
1302 const char *keys = NULL;
1303 char* timeStr = NULL;
1304 char *pSec = NULL;
1305 slog(RTC_PRINT, SLOG_ERR, "[%s][%d] zx234290-rtc \n", __func__, __LINE__);
1306
1307 offlen += strlen((char *)msg) + 1;
1308 while (offlen <= msglen)
1309 {
1310 keys = (char *)(msg + offlen);
1311 //slog(NET_PRINT,SLOG_ERR, "[%s][%d] zx234290-rtc%s, %d\n", __func__, __LINE__, keys, offlen);
1312 if (strncmp(keys, RTC_ALARM_TIMEOUT_MSG, strlen(RTC_ALARM_TIMEOUT_MSG)) == 0)
1313 {
lh9ed821d2023-04-07 01:36:19 -07001314#if 1 // cov M DEADCODE
xf.li9d1a0e12023-09-20 01:43:20 -07001315 pSec = strstr(keys, "sec:");
1316 if (pSec == NULL)
1317 {
1318 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event no sec \n");
1319 return -1;
lh9ed821d2023-04-07 01:36:19 -07001320 }
xf.li9d1a0e12023-09-20 01:43:20 -07001321 timeStr = pSec + strlen("sec:");
lh9ed821d2023-04-07 01:36:19 -07001322
1323#else
1324 timeStr = strstr(keys, "sec:") + strlen("sec:");
xf.li9d1a0e12023-09-20 01:43:20 -07001325 if (timeStr == NULL)
1326 {
1327 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event no sec \n");
1328 return -1;
1329 }
1330#endif
1331 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_ALARM_TIMEOUT, strlen(timeStr), timeStr, 0);
1332 slog(RTC_PRINT, SLOG_NORMAL, "zx234290-rtc alarm timeout \n");
1333 return 0;
1334 }
1335 else if (strncmp(keys, RTC_TIMER_TIMEOUT_MSG, strlen(keys)) == 0)
1336 {
1337 ipc_send_message2(MODULE_ID_HOTPLUG, MODULE_ID_RTC_SERVICE, RTC_MSG_TIMER_TIMEOUT, 0, NULL, 0);
1338 slog(RTC_PRINT, SLOG_NORMAL, "zx234290-rtc timer timeout \n");
1339 return 0;
1340 }
1341 offlen += strlen((char *)keys) + 1;
1342 }
1343 slog(RTC_PRINT, SLOG_ERR, "zx234290-rtc event not supported \n");
1344 return -1;
lh9ed821d2023-04-07 01:36:19 -07001345}
1346
1347/********************************************
1348 * rtc_service_thread_process
1349 * looper for processing zx234290-rtc msg.
1350 *******************************************/
1351void rtc_event_process(void)
1352{
xf.li9d1a0e12023-09-20 01:43:20 -07001353 int rtc_service_fd = 0;
1354 fd_set readfds;
1355 int maxfd = 0;
1356 char buf[RTC_MSG_BUFFER_SIZE] = {0};
1357 int len = 0;
1358 int ret = 0;
1359 prctl(PR_SET_NAME, "rtceventhandle", 0, 0, 0);
1360 rtc_service_fd = rtc_service_sock_init();
1361 if (rtc_service_fd < 0)
1362 {
1363 slog(RTC_PRINT, SLOG_ERR, "rtc service socket init fail!\n");
1364 return ;
1365 }
lh9ed821d2023-04-07 01:36:19 -07001366
xf.li9d1a0e12023-09-20 01:43:20 -07001367 maxfd = rtc_service_fd;
1368 while (1)
1369 {
1370 FD_ZERO(&readfds);
1371 FD_SET(rtc_service_fd, &readfds);
lh9ed821d2023-04-07 01:36:19 -07001372
xf.li9d1a0e12023-09-20 01:43:20 -07001373 ret = select(maxfd + 1, &readfds, NULL, NULL, NULL);
1374 if (ret == -1 && errno == EINTR)
1375 {
1376 continue;
1377 }
lh9ed821d2023-04-07 01:36:19 -07001378
xf.li9d1a0e12023-09-20 01:43:20 -07001379 if (FD_ISSET(rtc_service_fd, &readfds))
1380 {
1381 memset(buf, 0, sizeof(buf));
1382 len = recv(rtc_service_fd, &buf, sizeof(buf), 0);
1383 if (len <= 0)
1384 {
1385 slog(RTC_PRINT, SLOG_ERR, "rtc service socket recv msg fail!\n");
1386 continue;
1387 }
lh9ed821d2023-04-07 01:36:19 -07001388
xf.li9d1a0e12023-09-20 01:43:20 -07001389 /*½øÐÐueventÏûÏ¢½âÎö£¬·µ»Ø½âÎö½á¹û*/
1390 if (strstr(buf, "zx234290-rtc"))
1391 {
1392 rtc_time_msg_parse(buf, len);
1393 }
1394 continue;
1395 }
1396 }
lh9ed821d2023-04-07 01:36:19 -07001397}
1398
1399/********************************************
1400 * msg_looper
1401 * main looper for processing MSG.
1402 *******************************************/
1403static void msg_looper(void)
1404{
xf.li9d1a0e12023-09-20 01:43:20 -07001405 int ret = 0;
1406 int msg_id = 0;
1407 MSG_BUF msg_buf = {0};
1408 // key_t msgkey = get_msg_key();
lh9ed821d2023-04-07 01:36:19 -07001409
xf.li9d1a0e12023-09-20 01:43:20 -07001410 msg_id = msgget(MODULE_ID_RTC_SERVICE, IPC_CREAT | 0600);
lh9ed821d2023-04-07 01:36:19 -07001411
xf.li9d1a0e12023-09-20 01:43:20 -07001412 memset(&msg_buf, 0, sizeof(msg_buf));
lh9ed821d2023-04-07 01:36:19 -07001413
xf.li9d1a0e12023-09-20 01:43:20 -07001414 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper enter(msgid=%d)&waiting...\n", msg_id);
1415 while (1)
1416 {
1417 ret = msgrcv(msg_id, &msg_buf, sizeof(MSG_BUF) - sizeof(long), 0, 0);
1418 wake_rtc_lock();
1419 if (ret <= 0)
1420 {
1421 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper recv msg break(%d)\n", ret);
1422 wake_rtc_unlock();
1423 continue;
1424 }
1425 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service: msg_looper process_msg in, src:0x%x, target:0x%x.\n", msg_buf.src_id,
1426 msg_buf.dst_id);
lh9ed821d2023-04-07 01:36:19 -07001427
xf.li9d1a0e12023-09-20 01:43:20 -07001428#ifdef USE_CAP_SUPPORT
1429 process_msg_rtc_cap(&msg_buf);
1430#else
1431 process_msg(&msg_buf);
1432#endif
1433
1434 slog(RTC_PRINT, SLOG_ERR, "rtc-service: msg_looper process_msg out.\n");
1435 wake_rtc_unlock();
1436 }
lh9ed821d2023-04-07 01:36:19 -07001437}
1438
1439static void print_rtc_list_info(void)
1440{
xf.li9d1a0e12023-09-20 01:43:20 -07001441 ALARM_T *temp = NULL;
1442 int remain_time;
lh9ed821d2023-04-07 01:36:19 -07001443
xf.li9d1a0e12023-09-20 01:43:20 -07001444 if (ioctl(g_rtc_fd, ZX234290_GET_TIMER_REMAIN, &remain_time) < 0) // cov M CHECKED_RETURN
1445 {
1446 slog(RTC_PRINT, SLOG_ERR, "ioctl fail.\n");
1447 }
lh9ed821d2023-04-07 01:36:19 -07001448
lh9ed821d2023-04-07 01:36:19 -07001449
xf.li9d1a0e12023-09-20 01:43:20 -07001450 slog(RTC_PRINT, SLOG_ERR, "rtc-service: rtc remain_time :%ld \n", remain_time);
1451
1452 if (!list_empty(&rtc_timer_list))
1453 {
1454 list_for_each_entry(temp, &rtc_timer_list, list)
1455 {
1456 slog(RTC_PRINT, SLOG_ERR,
1457 "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d\n",
1458 temp->rtc_id,
1459 temp->module_id,
1460 temp->time_val,
1461 temp->remain_time,
1462 temp->cpu);
1463 }
1464 }
1465 else
1466 {
1467 slog(RTC_PRINT, SLOG_ERR, "rtc-service rtc_timer_list is empty!");
1468 }
1469
1470 if (!list_empty(&rtc_alarm_list))
1471 {
1472 list_for_each_entry(temp, &rtc_alarm_list, list)
1473 {
1474 slog(RTC_PRINT, SLOG_ERR,
1475 "rtc-service rtc_alarm_list node rtc_id:0x%x, module_id:0x%x, time_val:%ld, remain_time:%ld, cpu:%d, \
1476 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",
1477 temp->rtc_id,
1478 temp->module_id,
1479 temp->time_val,
1480 temp->remain_time,
1481 temp->cpu,
1482 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);
1483 }
1484 }
1485 else
1486 {
1487 slog(RTC_PRINT, SLOG_ERR, "rtc-service rtc_alarm_list is empty!");
1488 }
lh9ed821d2023-04-07 01:36:19 -07001489}
1490
1491/* ÐźŴ¦Àí*/
1492static void sig_usr(int signo)
1493{
1494#if 0//(APP_OS_TYPE == APP_OS_LINUX)
xf.li9d1a0e12023-09-20 01:43:20 -07001495 if (signo == SIGUSR1)
1496 {
1497 slog(RTC_PRINT, SLOG_NORMAL, "rtc-service receive SIGUSR1! \n");
1498 print_rtc_list_info();
1499 }
1500 else
1501 {
1502 slog(RTC_PRINT, SLOG_ERR, "rtc-service receive signo:%d", signo);
1503 }
lh9ed821d2023-04-07 01:36:19 -07001504#endif
1505}
1506
1507/*RTCÏûÏ¢½âÎö*/
1508int rtc_app_msg_parse(const char *msg, int msglen, struct hotplug_event *event)
1509{
xf.li9d1a0e12023-09-20 01:43:20 -07001510 if (strstr(msg, "zx234290-rtc"))
1511 {
1512 rtc_time_msg_parse(msg, msglen);
1513 }
1514 return -1;
lh9ed821d2023-04-07 01:36:19 -07001515}
1516
1517/********************************************
1518 * main
1519 * entry of RTC-Service!
1520 *******************************************/
1521int rtc_service_main(int argc, char **argv)
1522{
1523
xf.li9d1a0e12023-09-20 01:43:20 -07001524 slog(RTC_PRINT, SLOG_DEBUG, "rtc-service enter...\n");
1525 pthread_t rtcservice_thread_id;
1526 pthread_attr_t attr;
1527 int result = 0;
1528 prctl(PR_SET_NAME, "rtc-service", 0, 0, 0);
lh9ed821d2023-04-07 01:36:19 -07001529
xf.li9d1a0e12023-09-20 01:43:20 -07001530 //¸ù¾ÝNV³õʼ»¯´òÓ¡¼¶±ð£¬²¢×¢²á¶¯Ì¬µ÷Õû´òÓ¡¼¶±ðÐźÅÁ¿
1531 loglevel_init();
lh9ed821d2023-04-07 01:36:19 -07001532
xf.li9d1a0e12023-09-20 01:43:20 -07001533 if (signal(SIGUSR1, sig_usr) == SIG_ERR)
1534 {
1535 slog(RTC_PRINT, SLOG_ERR, "rtc-service not catch SIGUSR1 \n");
1536 }
1537
lh9ed821d2023-04-07 01:36:19 -07001538 INIT_LIST_HEAD(&rtc_timer_list);
1539 INIT_LIST_HEAD(&rtc_alarm_list);
1540
1541 g_clean_flag = 0;
xf.li9d1a0e12023-09-20 01:43:20 -07001542
1543 //rtcÔÚAPʱ£¬½Úµã±ØÐë´ò¿ª³É¹¦£»ÔÚÆäËûºËʱÓÉÆäËûºËÅжÏ
xf.li6c8fc1e2023-08-12 00:11:09 -07001544#ifndef USE_CAP_SUPPORT
xf.li9d1a0e12023-09-20 01:43:20 -07001545 if ((g_rtc_fd = open_rtc()) < 0)
1546 {
1547 slog(RTC_PRINT, SLOG_ERR, "Cannt open rtc dev!\n");
1548 return -1;
1549 }
1550 init_rtc_alarm();
xf.li6c8fc1e2023-08-12 00:11:09 -07001551#endif
xf.li9d1a0e12023-09-20 01:43:20 -07001552 /*
1553 pthread_attr_init(&attr);
1554 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1555 result = pthread_create(&rtcservice_thread_id, &attr, rtc_event_process, NULL);
1556 pthread_attr_destroy(&attr);
1557 if(result != 0)
1558 {
1559 slog(RTC_PRINT,SLOG_ERR, "[rtc-service] rtcservice_thread pthread_create faild!\n");
1560 return -1;
1561 }
1562 else
1563 {
1564 printf("[rtc-service] rtcservice_thread pthread_create SUCCESS!\n");
1565 }
1566 */
lh9ed821d2023-04-07 01:36:19 -07001567 hotplug_parse_register(DEVICE_TYPE_APP_RTC, rtc_app_msg_parse);
1568 msg_looper();
xf.li9d1a0e12023-09-20 01:43:20 -07001569 //klocwork
1570 //close_rtc(g_rtc_fd);
lh9ed821d2023-04-07 01:36:19 -07001571
xf.li9d1a0e12023-09-20 01:43:20 -07001572 //slog(RTC_PRINT, SLOG_ERR, "rtc-service exit...\n");
lh9ed821d2023-04-07 01:36:19 -07001573
xf.li9d1a0e12023-09-20 01:43:20 -07001574 return 0;
lh9ed821d2023-04-07 01:36:19 -07001575}