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;
|
| 34 | static int pw_rtc_id = 2;
|
| 35 | static int wk_srcid = 0;
|
| 36 |
|
| 37 | typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
|
| 38 | extern int sc_rtc_timer_init(void);
|
| 39 | extern int sc_rtc_timer_uninit(void );
|
| 40 | extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
|
| 41 | extern int sc_rtc_timer_del(int srcid, int rtc_id);
|
| 42 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 43 | 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] | 44 |
|
| 45 | /*****************************************
|
| 46 | * @brief:sc_rtc_timer_add_cb
|
| 47 | * @param count [IN]:src_id, rtc_id,
|
| 48 | * @param sum [OUT]:NA
|
| 49 | * @return :0
|
| 50 | * @todo:
|
| 51 | * @see:NA
|
| 52 | * @warning:
|
| 53 | ******************************************/
|
| 54 | int sc_rtc_timer_add_cb(unsigned int src_id, int rtc_id)
|
| 55 | {
|
| 56 | return 0;
|
| 57 | }
|
| 58 |
|
| 59 |
|
| 60 | /*****************************************
|
| 61 | * @brief:Set wakealarm time
|
| 62 | * @param count [IN]:buffer,
|
| 63 | * @param sum [OUT]:NA
|
| 64 | * @return :success 0, failed -1
|
| 65 | * @todo:
|
| 66 | * @see:NA
|
| 67 | * @warning:
|
| 68 | ******************************************/
|
| 69 | ssize_t wakealarm(char *buffer)
|
| 70 | {
|
| 71 | unsigned long time_sec = 0;
|
| 72 | int ret = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 73 | char *buf_bak = buffer;
|
| 74 | time_t tmp_time = 0;
|
| 75 | struct tm *ptime = NULL;
|
| 76 | struct tm *wake_arlarm = NULL;
|
| 77 | tmp_time = time(NULL);
|
| 78 | ptime = localtime(&tmp_time);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 79 |
|
| 80 | LYLOGSET(LOG_INFO);
|
| 81 | LYLOGEINIT(USER_LOG_TAG);
|
| 82 |
|
| 83 | if(buffer == NULL)
|
| 84 | {
|
| 85 | LYINFLOG("Bad input parameter,exit!!!!");
|
| 86 | return -1;
|
| 87 | }
|
| 88 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 89 | while(*buffer != '\0' )
|
| 90 | {
|
| 91 | if((*buffer < '0') || (*buffer > '9'))
|
| 92 | {
|
| 93 | LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
|
| 94 | return -1;
|
| 95 | }
|
| 96 | else
|
| 97 | {
|
| 98 | buffer++;
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 |
|
| 103 | buffer = buf_bak;
|
| 104 | time_sec = strtoul(buffer,NULL,10);
|
| 105 | if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
|
| 106 | {
|
| 107 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 108 | return -1;
|
| 109 | }
|
| 110 |
|
| 111 | tmp_time += time_sec;
|
| 112 | wake_arlarm = localtime(&tmp_time);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 113 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 114 | LYINFLOG("Set wakealarm %lu seconds ",time_sec);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 115 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 116 | wk_srcid = sc_rtc_timer_init();
|
| 117 | if (wk_srcid <= 0)
|
| 118 | {
|
| 119 | LYINFLOG("rtc_timer_init fail!");
|
| 120 | return -1;
|
| 121 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 122 |
|
| 123 | 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] | 124 | if(ret < 0)
|
| 125 | {
|
| 126 | LYINFLOG("Add rtc timer failed!!!!");
|
| 127 | return -1;
|
| 128 | }
|
| 129 |
|
| 130 | LYINFLOG("Set wakealarm success !!!");
|
| 131 |
|
| 132 | ret = sc_rtc_timer_uninit();
|
| 133 | if(ret != 0)
|
| 134 | {
|
| 135 | LYINFLOG("Deinit failed");
|
| 136 | }
|
| 137 |
|
| 138 | return 0;
|
| 139 |
|
| 140 | }
|
| 141 |
|
| 142 |
|
| 143 | /*****************************************
|
| 144 | * @brief:set poweralarm time
|
| 145 | * @param count [IN]:buffer,
|
| 146 | * @param sum [OUT]:NA
|
| 147 | * @return :success 0, failed -1
|
| 148 | * @todo:
|
| 149 | * @see:NA
|
| 150 | * @warning:
|
| 151 | ******************************************/
|
| 152 | ssize_t poweralarm(char *buffer)
|
| 153 | {
|
| 154 | int srcid = 0;
|
| 155 | unsigned long time_sec = 0;
|
| 156 | int ret = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 157 | char *buf_bak = buffer;
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 158 |
|
| 159 | LYLOGSET(LOG_INFO);
|
| 160 | LYLOGEINIT(USER_LOG_TAG);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 161 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 162 | if(buffer == NULL)
|
| 163 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 164 | LYERRLOG("Bad input parameter,exit!!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 165 | return -1;
|
| 166 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 167 |
|
| 168 | while(*buffer != '\0' )
|
| 169 | {
|
| 170 | if(( *buffer < '0' )|| ( *buffer > '9'))
|
| 171 | {
|
| 172 | LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
|
| 173 | return -1;
|
| 174 | }
|
| 175 | else
|
| 176 | {
|
| 177 | buffer++;
|
| 178 | }
|
| 179 | }
|
| 180 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 181 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 182 | buffer = buf_bak;
|
| 183 | time_sec = strtoul(buffer,NULL,10);
|
| 184 | if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
|
| 185 | {
|
| 186 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 187 | return -1;
|
| 188 | }
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 189 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 190 | LYINFLOG("Set poweralarm %lu seconds",time_sec);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 191 | srcid = sc_rtc_timer_init();
|
| 192 | if (srcid <= 0)
|
| 193 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 194 | LYERRLOG("rtc_timer_init fail!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 195 | return -1;
|
| 196 | }
|
| 197 | ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);
|
| 198 | if(ret < 0)
|
| 199 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 200 | LYERRLOG("Add rtc timer failed!!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 201 | return -1;
|
| 202 | }
|
| 203 |
|
| 204 | LYINFLOG("Set poweralarm success !!!");
|
| 205 |
|
| 206 | ret = sc_rtc_timer_uninit();
|
| 207 | if(ret != 0)
|
| 208 | {
|
| 209 | LYINFLOG("Deinit failed");
|
| 210 | }
|
| 211 |
|
| 212 | return 0;
|
| 213 |
|
| 214 | }
|
| 215 |
|
| 216 | /**********************************************
|
| 217 | * @brief:cancel_wakealarm,
|
| 218 | * @param count [IN]:void
|
| 219 | * @param sum [OUT]:NA
|
| 220 | * @return :success 0, failed -1
|
| 221 | * @todo:
|
| 222 | * @see:NA
|
| 223 | * @warning:
|
| 224 | ********************************************/
|
| 225 | ssize_t cancel_wakealarm(void)
|
| 226 | {
|
| 227 | int ret = 0;
|
| 228 |
|
| 229 | LYINFLOG("Enter cancel_wakealarm ");
|
| 230 | ret = sc_rtc_timer_del(wk_srcid, wk_rtc_id);
|
| 231 | if(ret < 0)
|
| 232 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 233 | LYERRLOG("Del wakealarm failed!!!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 234 | return -1;
|
| 235 | }
|
| 236 |
|
| 237 | return 0;
|
| 238 |
|
| 239 | }
|
| 240 |
|
| 241 |
|
| 242 | /*****************************************
|
| 243 | * @brief:lynq_set_poweralarm
|
| 244 | * @param count [IN]:unsigned long time_sec
|
| 245 | * @param sum [OUT]:NA
|
| 246 | * @return :success 0, failed -1
|
| 247 | * @todo:
|
| 248 | * @see:NA
|
| 249 | * @warning:
|
| 250 | ******************************************/
|
| 251 | int lynq_set_poweralarm(unsigned long time_sec)
|
| 252 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 253 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 254 | int ret = 0;
|
| 255 | int srcid = 0;
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 256 |
|
| 257 | LYLOGSET(LOG_INFO);
|
| 258 | LYLOGEINIT(USER_LOG_TAG);
|
| 259 |
|
| 260 | if(time_sec < MIN_TIME || time_sec > MAX_TIME )
|
| 261 | {
|
| 262 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 263 | return -1;
|
| 264 | }
|
| 265 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 266 | LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 267 | srcid = sc_rtc_timer_init();
|
| 268 | if (srcid <= 0)
|
| 269 | {
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 270 | LYERRLOG("rtc_timer_init fail!");
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 271 | return -1;
|
| 272 | }
|
| 273 | ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);
|
| 274 | if(ret < 0)
|
| 275 | {
|
| 276 | LYINFLOG("Add rtc timer failed!!!!");
|
| 277 | return -1;
|
| 278 | }
|
| 279 |
|
| 280 | LYINFLOG("Set poweralarm success !!!");
|
| 281 |
|
| 282 | ret = sc_rtc_timer_uninit();
|
| 283 | if(ret != 0)
|
| 284 | {
|
| 285 | LYINFLOG("Deinit failed");
|
| 286 | }
|
| 287 |
|
| 288 | return 0;
|
| 289 |
|
| 290 | }
|
| 291 |
|
| 292 |
|
| 293 | /*****************************************
|
| 294 | * @brief:lynq_set_wakealarm
|
| 295 | * @param count [IN]:unsigned long time_sec
|
| 296 | * @param sum [OUT]:NA
|
| 297 | * @return :success 0, failed -1
|
| 298 | * @todo:
|
| 299 | * @see:NA
|
| 300 | * @warning:
|
| 301 | ******************************************/
|
| 302 | int lynq_set_wakealarm(unsigned long time_sec)
|
| 303 | {
|
| 304 | int ret = 0;
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 305 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 306 | time_t tmp_time = 0;
|
| 307 | struct tm *ptime = NULL;
|
| 308 | struct tm *wake_arlarm = NULL;
|
| 309 | tmp_time = time(NULL);
|
| 310 | ptime = localtime(&tmp_time);
|
| 311 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 312 | LYLOGSET(LOG_INFO);
|
| 313 | LYLOGEINIT(USER_LOG_TAG);
|
| 314 |
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 315 | if(time_sec < MIN_TIME || time_sec > MAX_TIME)
|
| 316 | {
|
| 317 | LYERRLOG("Illeagle input: too large or too small !!!");
|
| 318 | return -1;
|
| 319 | }
|
| 320 |
|
l.yang | 4c4e738 | 2023-08-22 16:13:09 +0800 | [diff] [blame] | 321 | LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 322 |
|
l.yang | b488ebc | 2023-08-21 17:58:00 +0800 | [diff] [blame] | 323 | wk_srcid = sc_rtc_timer_init();
|
| 324 | if (wk_srcid <= 0)
|
| 325 | {
|
| 326 | LYINFLOG("rtc_timer_init fail!");
|
| 327 | return -1;
|
| 328 | }
|
l.yang | 168f8d2 | 2023-09-06 14:16:13 +0800 | [diff] [blame^] | 329 |
|
| 330 | tmp_time += time_sec;
|
| 331 | wake_arlarm = localtime(&tmp_time);
|
| 332 | LYINFLOG("Set wakealarm %lu seconds ",time_sec);
|
| 333 |
|
| 334 | 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] | 335 | if(ret < 0)
|
| 336 | {
|
| 337 | LYINFLOG("Add rtc timer failed!!!!");
|
| 338 | return -1;
|
| 339 | }
|
| 340 |
|
| 341 | LYINFLOG("Set wakealarm success !!!");
|
| 342 |
|
| 343 | ret = sc_rtc_timer_uninit();
|
| 344 | if(ret != 0)
|
| 345 | {
|
| 346 | LYINFLOG("Deinit failed!!!");
|
| 347 | }
|
| 348 |
|
| 349 | return 0;
|
| 350 |
|
| 351 | }
|
| 352 |
|
| 353 |
|
| 354 | #ifdef __cplusplus
|
| 355 | }
|
| 356 | #endif
|
| 357 |
|