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