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