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