blob: b3b7827adaef08bd6d7d92649e5854c8c2e47a09 [file] [log] [blame]
l.yangb488ebc2023-08-21 17:58:00 +08001/*******************************************************
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.yang168f8d22023-09-06 14:16:13 +080020#include <string.h>
21#include <errno.h>
l.yangb488ebc2023-08-21 17:58:00 +080022
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27#define USER_LOG_TAG "LYNQ_POWERALARM"
28
l.yang168f8d22023-09-06 14:16:13 +080029#define MIN_TIME 1
30#define MAX_TIME 268435456
31
l.yangb488ebc2023-08-21 17:58:00 +080032
33static int wk_rtc_id = 1;
l.yangb488ebc2023-08-21 17:58:00 +080034static int wk_srcid = 0;
35
36typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
37extern int sc_rtc_timer_init(void);
38extern int sc_rtc_timer_uninit(void );
39extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
40extern int sc_rtc_timer_del(int srcid, int rtc_id);
41
l.yang168f8d22023-09-06 14:16:13 +080042extern 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.yangb488ebc2023-08-21 17:58:00 +080043
l.yang989cbdb2023-09-18 11:29:39 +080044extern int sc_rtc_get_poweroff_alarm_modid(int src_id,char *name);
45
46extern 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.yangb488ebc2023-08-21 17:58:00 +080048/*****************************************
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******************************************/
57int 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******************************************/
72ssize_t wakealarm(char *buffer)
73{
74 unsigned long time_sec = 0;
75 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +080076 char *buf_bak = buffer;
77 time_t tmp_time = 0;
l.yang989cbdb2023-09-18 11:29:39 +080078
l.yang168f8d22023-09-06 14:16:13 +080079 struct tm *wake_arlarm = NULL;
80 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +080081 localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +080082
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.yang168f8d22023-09-06 14:16:13 +080092 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.yangb488ebc2023-08-21 17:58:00 +0800116
l.yang4c4e7382023-08-22 16:13:09 +0800117 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
l.yang168f8d22023-09-06 14:16:13 +0800118
l.yangb488ebc2023-08-21 17:58:00 +0800119 wk_srcid = sc_rtc_timer_init();
120 if (wk_srcid <= 0)
121 {
122 LYINFLOG("rtc_timer_init fail!");
123 return -1;
124 }
l.yang168f8d22023-09-06 14:16:13 +0800125
126 ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);
l.yangb488ebc2023-08-21 17:58:00 +0800127 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******************************************/
155ssize_t poweralarm(char *buffer)
156{
157 int srcid = 0;
158 unsigned long time_sec = 0;
159 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800160 char *buf_bak = buffer;
l.yang989cbdb2023-09-18 11:29:39 +0800161
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.yangb488ebc2023-08-21 17:58:00 +0800170 LYLOGSET(LOG_INFO);
171 LYLOGEINIT(USER_LOG_TAG);
l.yang168f8d22023-09-06 14:16:13 +0800172
l.yangb488ebc2023-08-21 17:58:00 +0800173 if(buffer == NULL)
174 {
l.yang168f8d22023-09-06 14:16:13 +0800175 LYERRLOG("Bad input parameter,exit!!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800176 return -1;
177 }
l.yang168f8d22023-09-06 14:16:13 +0800178
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.yangb488ebc2023-08-21 17:58:00 +0800192
l.yang168f8d22023-09-06 14:16:13 +0800193 buffer = buf_bak;
194 time_sec = strtoul(buffer,NULL,10);
l.yang989cbdb2023-09-18 11:29:39 +0800195
196 tmp_time += time_sec;
197 power_arlarm = localtime(&tmp_time);
198
l.yang168f8d22023-09-06 14:16:13 +0800199 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.yangb488ebc2023-08-21 17:58:00 +0800204
l.yang4c4e7382023-08-22 16:13:09 +0800205 LYINFLOG("Set poweralarm %lu seconds",time_sec);
l.yangb488ebc2023-08-21 17:58:00 +0800206 srcid = sc_rtc_timer_init();
207 if (srcid <= 0)
208 {
l.yang168f8d22023-09-06 14:16:13 +0800209 LYERRLOG("rtc_timer_init fail!");
l.yangb488ebc2023-08-21 17:58:00 +0800210 return -1;
211 }
l.yang989cbdb2023-09-18 11:29:39 +0800212
213 alarmid = sc_rtc_get_poweroff_alarm_modid(srcid, username);
214 if(alarmid < 0)
l.yangb488ebc2023-08-21 17:58:00 +0800215 {
l.yang989cbdb2023-09-18 11:29:39 +0800216 LYERRLOG("Get poweroff alarm id failed !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800217 return -1;
218 }
l.yang989cbdb2023-09-18 11:29:39 +0800219
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.yangb488ebc2023-08-21 17:58:00 +0800227
228 ret = sc_rtc_timer_uninit();
229 if(ret != 0)
230 {
l.yang989cbdb2023-09-18 11:29:39 +0800231 LYINFLOG("Deinit failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800232 }
233
234 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800235
l.yangb488ebc2023-08-21 17:58:00 +0800236}
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********************************************/
247ssize_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.yang168f8d22023-09-06 14:16:13 +0800255 LYERRLOG("Del wakealarm failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800256 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******************************************/
273int lynq_set_poweralarm(unsigned long time_sec)
274{
l.yang168f8d22023-09-06 14:16:13 +0800275
l.yangb488ebc2023-08-21 17:58:00 +0800276 int ret = 0;
277 int srcid = 0;
l.yang168f8d22023-09-06 14:16:13 +0800278
l.yang989cbdb2023-09-18 11:29:39 +0800279 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.yang168f8d22023-09-06 14:16:13 +0800291 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.yang989cbdb2023-09-18 11:29:39 +0800299
300 LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
l.yangb488ebc2023-08-21 17:58:00 +0800301 srcid = sc_rtc_timer_init();
302 if (srcid <= 0)
303 {
l.yang168f8d22023-09-06 14:16:13 +0800304 LYERRLOG("rtc_timer_init fail!");
l.yangb488ebc2023-08-21 17:58:00 +0800305 return -1;
306 }
l.yang989cbdb2023-09-18 11:29:39 +0800307
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.yangb488ebc2023-08-21 17:58:00 +0800316 if(ret < 0)
317 {
l.yang989cbdb2023-09-18 11:29:39 +0800318 LYERRLOG("Set power alarm failed !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800319 return -1;
320 }
321
l.yang989cbdb2023-09-18 11:29:39 +0800322 LYINFLOG("Set power alarm success !!!!");
323
l.yangb488ebc2023-08-21 17:58:00 +0800324 ret = sc_rtc_timer_uninit();
325 if(ret != 0)
326 {
l.yang989cbdb2023-09-18 11:29:39 +0800327 LYINFLOG("Deinit rtc_timer failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800328 }
329
330 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800331
l.yangb488ebc2023-08-21 17:58:00 +0800332
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******************************************/
345int lynq_set_wakealarm(unsigned long time_sec)
346{
347 int ret = 0;
l.yang4c4e7382023-08-22 16:13:09 +0800348
l.yang168f8d22023-09-06 14:16:13 +0800349 time_t tmp_time = 0;
l.yang168f8d22023-09-06 14:16:13 +0800350 struct tm *wake_arlarm = NULL;
351 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800352 localtime(&tmp_time);
l.yang168f8d22023-09-06 14:16:13 +0800353
l.yang4c4e7382023-08-22 16:13:09 +0800354 LYLOGSET(LOG_INFO);
355 LYLOGEINIT(USER_LOG_TAG);
356
l.yang168f8d22023-09-06 14:16:13 +0800357 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.yang4c4e7382023-08-22 16:13:09 +0800363 LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
l.yang168f8d22023-09-06 14:16:13 +0800364
l.yangb488ebc2023-08-21 17:58:00 +0800365 wk_srcid = sc_rtc_timer_init();
366 if (wk_srcid <= 0)
367 {
368 LYINFLOG("rtc_timer_init fail!");
369 return -1;
370 }
l.yang168f8d22023-09-06 14:16:13 +0800371
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.yangb488ebc2023-08-21 17:58:00 +0800377 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.chen21c62b72023-09-08 09:41:11 +0800395DEFINE_LYNQ_LIB_LOG(LYNQ_POWERALARM)
l.yangb488ebc2023-08-21 17:58:00 +0800396
397#ifdef __cplusplus
398}
399#endif
400