l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 1 | /*******************************************************
|
| 2 | *
|
| 3 | * @brief: none
|
| 4 | * @details: Add wakealrm and poweralarm api code
|
| 5 | * @author: l.yang
|
| 6 | * @date: 2023.8.17
|
| 7 | * @version: V1.0
|
| 8 | * @copyright: Copyright (c) MobileTek
|
| 9 | *
|
| 10 | *********************************************/
|
| 11 |
|
| 12 | #include<stdio.h>
|
| 13 | #include<stdlib.h>
|
| 14 | #include<unistd.h>
|
| 15 | #include<stdbool.h>
|
| 16 | #include<log/log.h>
|
| 17 | #include "liblog/lynq_deflog.h"
|
| 18 | #include <include/libpoweralarm.h>
|
| 19 | #include <pthread.h>
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 20 | #include <string.h>
|
| 21 | #include <errno.h>
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 22 |
|
| 23 | #ifdef __cplusplus
|
| 24 | extern "C" {
|
| 25 | #endif
|
| 26 |
|
| 27 | #define USER_LOG_TAG "LYNQ_POWERALARM"
|
| 28 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 29 | #define MIN_TIME 1
|
| 30 | #define MAX_TIME 268435456
|
| 31 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 32 |
|
| 33 | static int wk_rtc_id = 1;
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 34 | static int wk_srcid = 0;
|
| 35 |
|
| 36 | typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
|
| 37 | extern int sc_rtc_timer_init(void);
|
| 38 | extern int sc_rtc_timer_uninit(void );
|
| 39 | extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
|
| 40 | extern int sc_rtc_timer_del(int srcid, int rtc_id);
|
| 41 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 42 | extern int sc_rtc_timer_add_utc(int srcid, int rtc_id, struct tm *utc_sec, int wakeup, sc_rtc_timer_exp_cb rtc_notify);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 43 |
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 44 | extern int sc_rtc_get_poweroff_alarm_modid(int src_id,char *name);
|
| 45 |
|
| 46 | extern int sc_rtc_add_poweroff_alarm(int srcid, int alarm_modid, int rtc_id, struct tm *utc_sec, sc_rtc_timer_exp_cb rtc_notify);
|
| 47 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 48 | /*****************************************
|
| 49 | * @brief:sc_rtc_timer_add_cb
|
| 50 | * @param count [IN]:src_id, rtc_id,
|
| 51 | * @param sum [OUT]:NA
|
| 52 | * @return :0
|
| 53 | * @todo:
|
| 54 | * @see:NA
|
| 55 | * @warning:
|
| 56 | ******************************************/
|
| 57 | int sc_rtc_timer_add_cb(unsigned int src_id, int rtc_id)
|
| 58 | {
|
| 59 | return 0;
|
| 60 | }
|
| 61 |
|
| 62 |
|
| 63 | /*****************************************
|
| 64 | * @brief:Set wakealarm time
|
| 65 | * @param count [IN]:buffer,
|
| 66 | * @param sum [OUT]:NA
|
| 67 | * @return :success 0, failed -1
|
| 68 | * @todo:
|
| 69 | * @see:NA
|
| 70 | * @warning:
|
| 71 | ******************************************/
|
| 72 | ssize_t wakealarm(char *buffer)
|
| 73 | {
|
| 74 | unsigned long time_sec = 0;
|
| 75 | int ret = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 76 | char *buf_bak = buffer;
|
| 77 | time_t tmp_time = 0;
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 78 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 79 | struct tm *wake_arlarm = NULL;
|
| 80 | tmp_time = time(NULL);
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 81 | localtime(&tmp_time);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 82 |
|
| 83 | LYLOGSET(LOG_INFO);
|
| 84 | LYLOGEINIT(USER_LOG_TAG);
|
| 85 |
|
| 86 | if(buffer == NULL)
|
| 87 | {
|
| 88 | LYINFLOG("Bad input parameter,exit!!!!");
|
| 89 | return -1;
|
| 90 | }
|
| 91 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 92 | while(*buffer != '\0' )
|
| 93 | {
|
| 94 | if((*buffer < '0') || (*buffer > '9'))
|
| 95 | {
|
| 96 | LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
|
| 97 | return -1;
|
| 98 | }
|
| 99 | else
|
| 100 | {
|
| 101 | buffer++;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 |
|
| 106 | buffer = buf_bak;
|
| 107 | time_sec = strtoul(buffer,NULL,10);
|
| 108 | if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
|
| 109 | {
|
| 110 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 111 | return -1;
|
| 112 | }
|
| 113 |
|
| 114 | tmp_time += time_sec;
|
| 115 | wake_arlarm = localtime(&tmp_time);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 116 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 117 | LYINFLOG("Set wakealarm %lu seconds ",time_sec);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 118 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 119 | wk_srcid = sc_rtc_timer_init();
|
| 120 | if (wk_srcid <= 0)
|
| 121 | {
|
| 122 | LYINFLOG("rtc_timer_init fail!");
|
| 123 | return -1;
|
| 124 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 125 |
|
| 126 | ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 127 | if(ret < 0)
|
| 128 | {
|
| 129 | LYINFLOG("Add rtc timer failed!!!!");
|
| 130 | return -1;
|
| 131 | }
|
| 132 |
|
| 133 | LYINFLOG("Set wakealarm success !!!");
|
| 134 |
|
| 135 | ret = sc_rtc_timer_uninit();
|
| 136 | if(ret != 0)
|
| 137 | {
|
| 138 | LYINFLOG("Deinit failed");
|
| 139 | }
|
| 140 |
|
| 141 | return 0;
|
| 142 |
|
| 143 | }
|
| 144 |
|
| 145 |
|
| 146 | /*****************************************
|
| 147 | * @brief:set poweralarm time
|
| 148 | * @param count [IN]:buffer,
|
| 149 | * @param sum [OUT]:NA
|
| 150 | * @return :success 0, failed -1
|
| 151 | * @todo:
|
| 152 | * @see:NA
|
| 153 | * @warning:
|
| 154 | ******************************************/
|
| 155 | ssize_t poweralarm(char *buffer)
|
| 156 | {
|
| 157 | int srcid = 0;
|
| 158 | unsigned long time_sec = 0;
|
| 159 | int ret = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 160 | char *buf_bak = buffer;
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 161 |
|
| 162 | int alarmid = 0;
|
| 163 | char username[8] = "user";
|
| 164 | time_t tmp_time = 0;
|
| 165 | struct tm *power_arlarm = NULL;
|
| 166 | tmp_time = time(NULL);
|
| 167 | localtime(&tmp_time);
|
| 168 |
|
| 169 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 170 | LYLOGSET(LOG_INFO);
|
| 171 | LYLOGEINIT(USER_LOG_TAG);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 172 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 173 | if(buffer == NULL)
|
| 174 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 175 | LYERRLOG("Bad input parameter,exit!!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 176 | return -1;
|
| 177 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 178 |
|
| 179 | while(*buffer != '\0' )
|
| 180 | {
|
| 181 | if(( *buffer < '0' )|| ( *buffer > '9'))
|
| 182 | {
|
| 183 | LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
|
| 184 | return -1;
|
| 185 | }
|
| 186 | else
|
| 187 | {
|
| 188 | buffer++;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 192 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 193 | buffer = buf_bak;
|
| 194 | time_sec = strtoul(buffer,NULL,10);
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 195 |
|
| 196 | tmp_time += time_sec;
|
| 197 | power_arlarm = localtime(&tmp_time);
|
| 198 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 199 | if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
|
| 200 | {
|
| 201 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 202 | return -1;
|
| 203 | }
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 204 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 205 | LYINFLOG("Set poweralarm %lu seconds",time_sec);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 206 | srcid = sc_rtc_timer_init();
|
| 207 | if (srcid <= 0)
|
| 208 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 209 | LYERRLOG("rtc_timer_init fail!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 210 | return -1;
|
| 211 | }
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 212 |
|
| 213 | alarmid = sc_rtc_get_poweroff_alarm_modid(srcid, username);
|
| 214 | if(alarmid < 0)
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 215 | {
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 216 | LYERRLOG("Get poweroff alarm id failed !!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 217 | return -1;
|
| 218 | }
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 219 |
|
| 220 | ret = sc_rtc_add_poweroff_alarm(srcid, alarmid, 0, power_arlarm, sc_rtc_timer_add_cb);
|
| 221 | if(ret < 0)
|
| 222 | {
|
| 223 | LYERRLOG("Set power alarm failed !!!");
|
| 224 | return -1;
|
| 225 | }
|
| 226 | LYINFLOG("Set power alarm success !!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 227 |
|
| 228 | ret = sc_rtc_timer_uninit();
|
| 229 | if(ret != 0)
|
| 230 | {
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 231 | LYINFLOG("Deinit failed!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 232 | }
|
| 233 |
|
| 234 | return 0;
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 235 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 236 | }
|
| 237 |
|
| 238 | /**********************************************
|
| 239 | * @brief:cancel_wakealarm,
|
| 240 | * @param count [IN]:void
|
| 241 | * @param sum [OUT]:NA
|
| 242 | * @return :success 0, failed -1
|
| 243 | * @todo:
|
| 244 | * @see:NA
|
| 245 | * @warning:
|
| 246 | ********************************************/
|
| 247 | ssize_t cancel_wakealarm(void)
|
| 248 | {
|
| 249 | int ret = 0;
|
| 250 |
|
| 251 | LYINFLOG("Enter cancel_wakealarm ");
|
| 252 | ret = sc_rtc_timer_del(wk_srcid, wk_rtc_id);
|
| 253 | if(ret < 0)
|
| 254 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 255 | LYERRLOG("Del wakealarm failed!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 256 | return -1;
|
| 257 | }
|
| 258 |
|
| 259 | return 0;
|
| 260 |
|
| 261 | }
|
| 262 |
|
| 263 |
|
| 264 | /*****************************************
|
| 265 | * @brief:lynq_set_poweralarm
|
| 266 | * @param count [IN]:unsigned long time_sec
|
| 267 | * @param sum [OUT]:NA
|
| 268 | * @return :success 0, failed -1
|
| 269 | * @todo:
|
| 270 | * @see:NA
|
| 271 | * @warning:
|
| 272 | ******************************************/
|
| 273 | int lynq_set_poweralarm(unsigned long time_sec)
|
| 274 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 275 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 276 | int ret = 0;
|
| 277 | int srcid = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 278 |
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 279 | int alarmid = 0;
|
| 280 | char username[8] = "user";
|
| 281 |
|
| 282 | time_t tmp_time = 0;
|
| 283 | struct tm *power_arlarm = NULL;
|
| 284 | tmp_time = time(NULL);
|
| 285 | localtime(&tmp_time);
|
| 286 |
|
| 287 |
|
| 288 | tmp_time += time_sec;
|
| 289 | power_arlarm = localtime(&tmp_time);
|
| 290 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 291 | LYLOGSET(LOG_INFO);
|
| 292 | LYLOGEINIT(USER_LOG_TAG);
|
| 293 |
|
| 294 | if(time_sec < MIN_TIME || time_sec > MAX_TIME )
|
| 295 | {
|
| 296 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 297 | return -1;
|
| 298 | }
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 299 |
|
| 300 | LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 301 | srcid = sc_rtc_timer_init();
|
| 302 | if (srcid <= 0)
|
| 303 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 304 | LYERRLOG("rtc_timer_init fail!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 305 | return -1;
|
| 306 | }
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 307 |
|
| 308 | alarmid = sc_rtc_get_poweroff_alarm_modid(srcid, username);
|
| 309 | if(alarmid < 0)
|
| 310 | {
|
| 311 | LYERRLOG("Get poweroff alarm id failed !!!");
|
| 312 | return -1;
|
| 313 | }
|
| 314 |
|
| 315 | ret = sc_rtc_add_poweroff_alarm(srcid, alarmid, 0, power_arlarm, sc_rtc_timer_add_cb);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 316 | if(ret < 0)
|
| 317 | {
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 318 | LYERRLOG("Set power alarm failed !!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 319 | return -1;
|
| 320 | }
|
| 321 |
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 322 | LYINFLOG("Set power alarm success !!!!");
|
| 323 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 324 | ret = sc_rtc_timer_uninit();
|
| 325 | if(ret != 0)
|
| 326 | {
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 327 | LYINFLOG("Deinit rtc_timer failed!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 328 | }
|
| 329 |
|
| 330 | return 0;
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 331 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 332 |
|
| 333 | }
|
| 334 |
|
| 335 |
|
| 336 | /*****************************************
|
| 337 | * @brief:lynq_set_wakealarm
|
| 338 | * @param count [IN]:unsigned long time_sec
|
| 339 | * @param sum [OUT]:NA
|
| 340 | * @return :success 0, failed -1
|
| 341 | * @todo:
|
| 342 | * @see:NA
|
| 343 | * @warning:
|
| 344 | ******************************************/
|
| 345 | int lynq_set_wakealarm(unsigned long time_sec)
|
| 346 | {
|
| 347 | int ret = 0;
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 348 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 349 | time_t tmp_time = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 350 | struct tm *wake_arlarm = NULL;
|
| 351 | tmp_time = time(NULL);
|
l.yang | 989cbdb | 2023-09-18 11:29:39 +0800 | [diff] [blame] | 352 | localtime(&tmp_time);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 353 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 354 | LYLOGSET(LOG_INFO);
|
| 355 | LYLOGEINIT(USER_LOG_TAG);
|
| 356 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 357 | if(time_sec < MIN_TIME || time_sec > MAX_TIME)
|
| 358 | {
|
| 359 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 360 | return -1;
|
| 361 | }
|
| 362 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 363 | LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 364 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 365 | wk_srcid = sc_rtc_timer_init();
|
| 366 | if (wk_srcid <= 0)
|
| 367 | {
|
| 368 | LYINFLOG("rtc_timer_init fail!");
|
| 369 | return -1;
|
| 370 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame] | 371 |
|
| 372 | tmp_time += time_sec;
|
| 373 | wake_arlarm = localtime(&tmp_time);
|
| 374 | LYINFLOG("Set wakealarm %lu seconds ",time_sec);
|
| 375 |
|
| 376 | ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 377 | if(ret < 0)
|
| 378 | {
|
| 379 | LYINFLOG("Add rtc timer failed!!!!");
|
| 380 | return -1;
|
| 381 | }
|
| 382 |
|
| 383 | LYINFLOG("Set wakealarm success !!!");
|
| 384 |
|
| 385 | ret = sc_rtc_timer_uninit();
|
| 386 | if(ret != 0)
|
| 387 | {
|
| 388 | LYINFLOG("Deinit failed!!!");
|
| 389 | }
|
| 390 |
|
| 391 | return 0;
|
| 392 |
|
| 393 | }
|
| 394 |
|
you.chen | 21c62b7 | 2023-09-08 09:41:11 +0800 | [diff] [blame] | 395 | DEFINE_LYNQ_LIB_LOG(LYNQ_POWERALARM)
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 396 |
|
| 397 | #ifdef __cplusplus
|
| 398 | }
|
| 399 | #endif
|
| 400 |
|