blob: 5aeb4966f4d5b383b835c79a05e261086a5285cb [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;
34static int pw_rtc_id = 2;
35static int wk_srcid = 0;
36
37typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
38extern int sc_rtc_timer_init(void);
39extern int sc_rtc_timer_uninit(void );
40extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
41extern int sc_rtc_timer_del(int srcid, int rtc_id);
42
l.yang168f8d22023-09-06 14:16:13 +080043extern 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 +080044
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******************************************/
54int 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******************************************/
69ssize_t wakealarm(char *buffer)
70{
71 unsigned long time_sec = 0;
72 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +080073 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.yangb488ebc2023-08-21 17:58:00 +080079
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.yang168f8d22023-09-06 14:16:13 +080089 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.yangb488ebc2023-08-21 17:58:00 +0800113
l.yang4c4e7382023-08-22 16:13:09 +0800114 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
l.yang168f8d22023-09-06 14:16:13 +0800115
l.yangb488ebc2023-08-21 17:58:00 +0800116 wk_srcid = sc_rtc_timer_init();
117 if (wk_srcid <= 0)
118 {
119 LYINFLOG("rtc_timer_init fail!");
120 return -1;
121 }
l.yang168f8d22023-09-06 14:16:13 +0800122
123 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 +0800124 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******************************************/
152ssize_t poweralarm(char *buffer)
153{
154 int srcid = 0;
155 unsigned long time_sec = 0;
156 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800157 char *buf_bak = buffer;
l.yangb488ebc2023-08-21 17:58:00 +0800158
159 LYLOGSET(LOG_INFO);
160 LYLOGEINIT(USER_LOG_TAG);
l.yang168f8d22023-09-06 14:16:13 +0800161
l.yangb488ebc2023-08-21 17:58:00 +0800162 if(buffer == NULL)
163 {
l.yang168f8d22023-09-06 14:16:13 +0800164 LYERRLOG("Bad input parameter,exit!!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800165 return -1;
166 }
l.yang168f8d22023-09-06 14:16:13 +0800167
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.yangb488ebc2023-08-21 17:58:00 +0800181
l.yang168f8d22023-09-06 14:16:13 +0800182 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.yangb488ebc2023-08-21 17:58:00 +0800189
l.yang4c4e7382023-08-22 16:13:09 +0800190 LYINFLOG("Set poweralarm %lu seconds",time_sec);
l.yangb488ebc2023-08-21 17:58:00 +0800191 srcid = sc_rtc_timer_init();
192 if (srcid <= 0)
193 {
l.yang168f8d22023-09-06 14:16:13 +0800194 LYERRLOG("rtc_timer_init fail!");
l.yangb488ebc2023-08-21 17:58:00 +0800195 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.yang168f8d22023-09-06 14:16:13 +0800200 LYERRLOG("Add rtc timer failed!!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800201 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********************************************/
225ssize_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.yang168f8d22023-09-06 14:16:13 +0800233 LYERRLOG("Del wakealarm failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800234 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******************************************/
251int lynq_set_poweralarm(unsigned long time_sec)
252{
l.yang168f8d22023-09-06 14:16:13 +0800253
l.yangb488ebc2023-08-21 17:58:00 +0800254 int ret = 0;
255 int srcid = 0;
l.yang168f8d22023-09-06 14:16:13 +0800256
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.yang4c4e7382023-08-22 16:13:09 +0800266 LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
l.yangb488ebc2023-08-21 17:58:00 +0800267 srcid = sc_rtc_timer_init();
268 if (srcid <= 0)
269 {
l.yang168f8d22023-09-06 14:16:13 +0800270 LYERRLOG("rtc_timer_init fail!");
l.yangb488ebc2023-08-21 17:58:00 +0800271 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******************************************/
302int lynq_set_wakealarm(unsigned long time_sec)
303{
304 int ret = 0;
l.yang4c4e7382023-08-22 16:13:09 +0800305
l.yang168f8d22023-09-06 14:16:13 +0800306 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.yang4c4e7382023-08-22 16:13:09 +0800312 LYLOGSET(LOG_INFO);
313 LYLOGEINIT(USER_LOG_TAG);
314
l.yang168f8d22023-09-06 14:16:13 +0800315 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.yang4c4e7382023-08-22 16:13:09 +0800321 LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
l.yang168f8d22023-09-06 14:16:13 +0800322
l.yangb488ebc2023-08-21 17:58:00 +0800323 wk_srcid = sc_rtc_timer_init();
324 if (wk_srcid <= 0)
325 {
326 LYINFLOG("rtc_timer_init fail!");
327 return -1;
328 }
l.yang168f8d22023-09-06 14:16:13 +0800329
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.yangb488ebc2023-08-21 17:58:00 +0800335 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