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