blob: af24cd9bba2467e643abdddb4b80f47144902745 [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
l.yangc9fc6df2024-06-20 14:16:23 +080027#include "sc_rtc_timer.h"
28
l.yangb488ebc2023-08-21 17:58:00 +080029#define USER_LOG_TAG "LYNQ_POWERALARM"
30
l.yang168f8d22023-09-06 14:16:13 +080031#define MIN_TIME 1
32#define MAX_TIME 268435456
33
l.yangb488ebc2023-08-21 17:58:00 +080034
l.yangc9fc6df2024-06-20 14:16:23 +080035//typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
l.yang3a9c6612023-11-14 11:28:02 +080036typedef int (*lynq_wakealarm_add_cb)(unsigned int src_id, int rtc_id);
37
l.yangc9fc6df2024-06-20 14:16:23 +080038/*
l.yangb488ebc2023-08-21 17:58:00 +080039extern int sc_rtc_timer_init(void);
40extern int sc_rtc_timer_uninit(void );
41extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
42extern int sc_rtc_timer_del(int srcid, int rtc_id);
43
l.yang168f8d22023-09-06 14:16:13 +080044extern 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 +080045
l.yang989cbdb2023-09-18 11:29:39 +080046extern int sc_rtc_get_poweroff_alarm_modid(int src_id,char *name);
47
48extern 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);
49
l.yangc9fc6df2024-06-20 14:16:23 +080050*/
l.yang3a9c6612023-11-14 11:28:02 +080051
l.yangb488ebc2023-08-21 17:58:00 +080052/*****************************************
l.yang3a9c6612023-11-14 11:28:02 +080053* @brief:lynq_rtc_service_init
54* @param count [IN]:NA
l.yangb488ebc2023-08-21 17:58:00 +080055* @param sum [OUT]:NA
l.yang3a9c6612023-11-14 11:28:02 +080056* @return :success src_id, failed -1
l.yangb488ebc2023-08-21 17:58:00 +080057* @todo:
58* @see:NA
59* @warning:
l.yang3a9c6612023-11-14 11:28:02 +080060*****************************************/
61int lynq_rtc_service_init(void)
l.yangb488ebc2023-08-21 17:58:00 +080062{
l.yang3a9c6612023-11-14 11:28:02 +080063 int src_id = -1;
64
65 LYLOGSET(LOG_INFO);
66 LYLOGEINIT(USER_LOG_TAG);
67
68 src_id = sc_rtc_timer_init();
69 if (src_id <= 0)
70 {
71 LYERRLOG("rtc_timer_init fail!");
72 return -1;
73 }
74 return src_id;
75
76}
77
78/*****************************************
79* @brief:lynq_rtc_service_init
80* @param count [IN]:NA
81* @param sum [OUT]:NA
82* @return :success src_id, failed -1
83* @todo:
84* @see:NA
85* @warning:
86*****************************************/
87int lynq_rtc_service_deinit(void)
88{
89 int ret = -1;
90 ret = sc_rtc_timer_uninit();
91 if(ret != 0)
92 {
93 LYINFLOG("Deinit failed!!!");
94 return -1;
95 }
96
l.yangb488ebc2023-08-21 17:58:00 +080097 return 0;
l.yang3a9c6612023-11-14 11:28:02 +080098
l.yangb488ebc2023-08-21 17:58:00 +080099}
100
101
102/*****************************************
103* @brief:Set wakealarm time
l.yang3a9c6612023-11-14 11:28:02 +0800104* @param count [IN]:char *buffer,int src_id ...
l.yangb488ebc2023-08-21 17:58:00 +0800105* @param sum [OUT]:NA
106* @return :success 0, failed -1
107* @todo:
108* @see:NA
109* @warning:
110******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800111ssize_t wakealarm(char *buffer,int src_id,int rtc_id,lynq_wakealarm_add_cb wakealarm_notify )
l.yangb488ebc2023-08-21 17:58:00 +0800112{
113 unsigned long time_sec = 0;
114 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800115 char *buf_bak = buffer;
116 time_t tmp_time = 0;
l.yang989cbdb2023-09-18 11:29:39 +0800117
l.yang168f8d22023-09-06 14:16:13 +0800118 struct tm *wake_arlarm = NULL;
119 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800120 localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800121
l.yangb488ebc2023-08-21 17:58:00 +0800122 if(buffer == NULL)
123 {
124 LYINFLOG("Bad input parameter,exit!!!!");
125 return -1;
126 }
127
l.yang168f8d22023-09-06 14:16:13 +0800128 while(*buffer != '\0' )
129 {
130 if((*buffer < '0') || (*buffer > '9'))
131 {
132 LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
133 return -1;
134 }
135 else
136 {
137 buffer++;
138 }
139 }
140
141
142 buffer = buf_bak;
143 time_sec = strtoul(buffer,NULL,10);
144 if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
145 {
146 LYERRLOG("Illeagle input: too large or too small !!!");
147 return -1;
148 }
149
150 tmp_time += time_sec;
151 wake_arlarm = localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800152
l.yang4c4e7382023-08-22 16:13:09 +0800153 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
l.yang3a9c6612023-11-14 11:28:02 +0800154
155 ret = sc_rtc_timer_add_utc(src_id, rtc_id,wake_arlarm,0,wakealarm_notify);
l.yangb488ebc2023-08-21 17:58:00 +0800156 if(ret < 0)
157 {
158 LYINFLOG("Add rtc timer failed!!!!");
159 return -1;
160 }
161
162 LYINFLOG("Set wakealarm success !!!");
163
l.yangb488ebc2023-08-21 17:58:00 +0800164 return 0;
165
166}
167
168
169/*****************************************
170* @brief:set poweralarm time
l.yang3a9c6612023-11-14 11:28:02 +0800171* @param count [IN]:char *buffer, int srcid
l.yangb488ebc2023-08-21 17:58:00 +0800172* @param sum [OUT]:NA
173* @return :success 0, failed -1
174* @todo:
175* @see:NA
176* @warning:
177******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800178ssize_t poweralarm(char *buffer,int src_id )
l.yangb488ebc2023-08-21 17:58:00 +0800179{
l.yang3a9c6612023-11-14 11:28:02 +0800180
l.yangb488ebc2023-08-21 17:58:00 +0800181 unsigned long time_sec = 0;
182 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800183 char *buf_bak = buffer;
l.yang3a9c6612023-11-14 11:28:02 +0800184
l.yang989cbdb2023-09-18 11:29:39 +0800185 int alarmid = 0;
186 char username[8] = "user";
187 time_t tmp_time = 0;
188 struct tm *power_arlarm = NULL;
189 tmp_time = time(NULL);
190 localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800191 LYLOGSET(LOG_INFO);
192 LYLOGEINIT(USER_LOG_TAG);
l.yang168f8d22023-09-06 14:16:13 +0800193
l.yangb488ebc2023-08-21 17:58:00 +0800194 if(buffer == NULL)
195 {
l.yang168f8d22023-09-06 14:16:13 +0800196 LYERRLOG("Bad input parameter,exit!!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800197 return -1;
198 }
l.yang168f8d22023-09-06 14:16:13 +0800199
200 while(*buffer != '\0' )
201 {
202 if(( *buffer < '0' )|| ( *buffer > '9'))
203 {
204 LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
205 return -1;
206 }
207 else
208 {
209 buffer++;
210 }
211 }
212
l.yangb488ebc2023-08-21 17:58:00 +0800213
l.yang168f8d22023-09-06 14:16:13 +0800214 buffer = buf_bak;
215 time_sec = strtoul(buffer,NULL,10);
l.yang989cbdb2023-09-18 11:29:39 +0800216
217 tmp_time += time_sec;
218 power_arlarm = localtime(&tmp_time);
219
l.yang168f8d22023-09-06 14:16:13 +0800220 if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
221 {
222 LYERRLOG("Illeagle input: too large or too small !!!");
223 return -1;
224 }
l.yangb488ebc2023-08-21 17:58:00 +0800225
l.yang4c4e7382023-08-22 16:13:09 +0800226 LYINFLOG("Set poweralarm %lu seconds",time_sec);
l.yang989cbdb2023-09-18 11:29:39 +0800227
l.yang3a9c6612023-11-14 11:28:02 +0800228
229 alarmid = sc_rtc_get_poweroff_alarm_modid(src_id, username);
l.yang989cbdb2023-09-18 11:29:39 +0800230 if(alarmid < 0)
l.yangb488ebc2023-08-21 17:58:00 +0800231 {
l.yang989cbdb2023-09-18 11:29:39 +0800232 LYERRLOG("Get poweroff alarm id failed !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800233 return -1;
234 }
l.yang989cbdb2023-09-18 11:29:39 +0800235
l.yang3a9c6612023-11-14 11:28:02 +0800236 ret = sc_rtc_add_poweroff_alarm(src_id, alarmid, 0, power_arlarm, NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800237 if(ret < 0)
238 {
239 LYERRLOG("Set power alarm failed !!!");
240 return -1;
241 }
242 LYINFLOG("Set power alarm success !!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800243
l.yangb488ebc2023-08-21 17:58:00 +0800244 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800245
l.yangb488ebc2023-08-21 17:58:00 +0800246}
247
248/**********************************************
249* @brief:cancel_wakealarm,
l.yang3a9c6612023-11-14 11:28:02 +0800250* @param count [IN]:int src_id, int rtc_id
l.yangb488ebc2023-08-21 17:58:00 +0800251* @param sum [OUT]:NA
252* @return :success 0, failed -1
253* @todo:
254* @see:NA
255* @warning:
256********************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800257ssize_t cancel_wakealarm(int src_id, int rtc_id)
l.yangb488ebc2023-08-21 17:58:00 +0800258{
259 int ret = 0;
260
261 LYINFLOG("Enter cancel_wakealarm ");
l.yang3a9c6612023-11-14 11:28:02 +0800262 ret = sc_rtc_timer_del(src_id, rtc_id);
l.yangb488ebc2023-08-21 17:58:00 +0800263 if(ret < 0)
264 {
l.yang168f8d22023-09-06 14:16:13 +0800265 LYERRLOG("Del wakealarm failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800266 return -1;
267 }
l.yang3a9c6612023-11-14 11:28:02 +0800268 LYINFLOG("Cancel success ");
l.yangb488ebc2023-08-21 17:58:00 +0800269 return 0;
270
271}
272
273
274/*****************************************
275* @brief:lynq_set_poweralarm
l.yang3a9c6612023-11-14 11:28:02 +0800276* @param count [IN]:unsigned long time_sec , int src_id
l.yangb488ebc2023-08-21 17:58:00 +0800277* @param sum [OUT]:NA
278* @return :success 0, failed -1
279* @todo:
280* @see:NA
281* @warning:
282******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800283int lynq_set_poweralarm(unsigned long time_sec,int src_id)
l.yangb488ebc2023-08-21 17:58:00 +0800284{
l.yang168f8d22023-09-06 14:16:13 +0800285
l.yangb488ebc2023-08-21 17:58:00 +0800286 int ret = 0;
l.yang989cbdb2023-09-18 11:29:39 +0800287 int alarmid = 0;
288 char username[8] = "user";
289
290 time_t tmp_time = 0;
291 struct tm *power_arlarm = NULL;
292 tmp_time = time(NULL);
293 localtime(&tmp_time);
l.yang989cbdb2023-09-18 11:29:39 +0800294
l.yang3a9c6612023-11-14 11:28:02 +0800295
l.yang989cbdb2023-09-18 11:29:39 +0800296 tmp_time += time_sec;
297 power_arlarm = localtime(&tmp_time);
l.yang3a9c6612023-11-14 11:28:02 +0800298
l.yang168f8d22023-09-06 14:16:13 +0800299 if(time_sec < MIN_TIME || time_sec > MAX_TIME )
300 {
301 LYERRLOG("Illeagle input: too large or too small !!!");
302 return -1;
303 }
l.yang989cbdb2023-09-18 11:29:39 +0800304
305 LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
l.yang989cbdb2023-09-18 11:29:39 +0800306
l.yang3a9c6612023-11-14 11:28:02 +0800307
308 alarmid = sc_rtc_get_poweroff_alarm_modid(src_id, username);
l.yang989cbdb2023-09-18 11:29:39 +0800309 if(alarmid < 0)
310 {
311 LYERRLOG("Get poweroff alarm id failed !!!");
312 return -1;
313 }
314
l.yang3a9c6612023-11-14 11:28:02 +0800315 ret = sc_rtc_add_poweroff_alarm(src_id, alarmid, 0, power_arlarm, NULL);
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 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800325
l.yangb488ebc2023-08-21 17:58:00 +0800326
327}
328
329
330/*****************************************
331* @brief:lynq_set_wakealarm
l.yang3a9c6612023-11-14 11:28:02 +0800332* @param count [IN]:unsigned long time_sec,....
l.yangb488ebc2023-08-21 17:58:00 +0800333* @param sum [OUT]:NA
334* @return :success 0, failed -1
335* @todo:
336* @see:NA
337* @warning:
338******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800339int lynq_set_wakealarm(unsigned long time_sec,int src_id,int rtc_id,lynq_wakealarm_add_cb wakealarm_notify )
l.yangb488ebc2023-08-21 17:58:00 +0800340{
341 int ret = 0;
l.yang4c4e7382023-08-22 16:13:09 +0800342
l.yang168f8d22023-09-06 14:16:13 +0800343 time_t tmp_time = 0;
l.yang168f8d22023-09-06 14:16:13 +0800344 struct tm *wake_arlarm = NULL;
345 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800346 localtime(&tmp_time);
l.yang3a9c6612023-11-14 11:28:02 +0800347
l.yang168f8d22023-09-06 14:16:13 +0800348 if(time_sec < MIN_TIME || time_sec > MAX_TIME)
349 {
350 LYERRLOG("Illeagle input: too large or too small !!!");
351 return -1;
352 }
353
l.yang4c4e7382023-08-22 16:13:09 +0800354 LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
l.yang3a9c6612023-11-14 11:28:02 +0800355
l.yang168f8d22023-09-06 14:16:13 +0800356 tmp_time += time_sec;
357 wake_arlarm = localtime(&tmp_time);
358 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
359
l.yang3a9c6612023-11-14 11:28:02 +0800360 ret = sc_rtc_timer_add_utc(src_id, rtc_id,wake_arlarm,0,wakealarm_notify);
l.yangb488ebc2023-08-21 17:58:00 +0800361 if(ret < 0)
362 {
363 LYINFLOG("Add rtc timer failed!!!!");
364 return -1;
365 }
366
367 LYINFLOG("Set wakealarm success !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800368
369 return 0;
370
371}
372
l.yang3a9c6612023-11-14 11:28:02 +0800373
you.chen21c62b72023-09-08 09:41:11 +0800374DEFINE_LYNQ_LIB_LOG(LYNQ_POWERALARM)
l.yangb488ebc2023-08-21 17:58:00 +0800375
376#ifdef __cplusplus
377}
378#endif
379