blob: 8c91c577bd7aea5abb6daeb4cac59ee0ceaef58d [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.yang42c000c2025-03-12 15:10:25 +080051extern void loglevel_init(void);
l.yang3a9c6612023-11-14 11:28:02 +080052
l.yangb488ebc2023-08-21 17:58:00 +080053/*****************************************
l.yang3a9c6612023-11-14 11:28:02 +080054* @brief:lynq_rtc_service_init
55* @param count [IN]:NA
l.yangb488ebc2023-08-21 17:58:00 +080056* @param sum [OUT]:NA
l.yang3a9c6612023-11-14 11:28:02 +080057* @return :success src_id, failed -1
l.yangb488ebc2023-08-21 17:58:00 +080058* @todo:
59* @see:NA
60* @warning:
l.yang3a9c6612023-11-14 11:28:02 +080061*****************************************/
62int lynq_rtc_service_init(void)
l.yangb488ebc2023-08-21 17:58:00 +080063{
l.yang3a9c6612023-11-14 11:28:02 +080064 int src_id = -1;
65
66 LYLOGSET(LOG_INFO);
67 LYLOGEINIT(USER_LOG_TAG);
l.yang42c000c2025-03-12 15:10:25 +080068 loglevel_init();
l.yang3a9c6612023-11-14 11:28:02 +080069 src_id = sc_rtc_timer_init();
70 if (src_id <= 0)
71 {
72 LYERRLOG("rtc_timer_init fail!");
73 return -1;
74 }
75 return src_id;
76
77}
78
79/*****************************************
80* @brief:lynq_rtc_service_init
81* @param count [IN]:NA
82* @param sum [OUT]:NA
83* @return :success src_id, failed -1
84* @todo:
85* @see:NA
86* @warning:
87*****************************************/
88int lynq_rtc_service_deinit(void)
89{
90 int ret = -1;
91 ret = sc_rtc_timer_uninit();
92 if(ret != 0)
93 {
94 LYINFLOG("Deinit failed!!!");
95 return -1;
96 }
97
l.yangb488ebc2023-08-21 17:58:00 +080098 return 0;
l.yang3a9c6612023-11-14 11:28:02 +080099
l.yangb488ebc2023-08-21 17:58:00 +0800100}
101
102
103/*****************************************
104* @brief:Set wakealarm time
l.yang3a9c6612023-11-14 11:28:02 +0800105* @param count [IN]:char *buffer,int src_id ...
l.yangb488ebc2023-08-21 17:58:00 +0800106* @param sum [OUT]:NA
107* @return :success 0, failed -1
108* @todo:
109* @see:NA
110* @warning:
111******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800112ssize_t wakealarm(char *buffer,int src_id,int rtc_id,lynq_wakealarm_add_cb wakealarm_notify )
l.yangb488ebc2023-08-21 17:58:00 +0800113{
114 unsigned long time_sec = 0;
115 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800116 char *buf_bak = buffer;
117 time_t tmp_time = 0;
l.yang989cbdb2023-09-18 11:29:39 +0800118
l.yang168f8d22023-09-06 14:16:13 +0800119 struct tm *wake_arlarm = NULL;
120 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800121 localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800122
l.yangb488ebc2023-08-21 17:58:00 +0800123 if(buffer == NULL)
124 {
125 LYINFLOG("Bad input parameter,exit!!!!");
126 return -1;
127 }
128
l.yang168f8d22023-09-06 14:16:13 +0800129 while(*buffer != '\0' )
130 {
131 if((*buffer < '0') || (*buffer > '9'))
132 {
133 LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
134 return -1;
135 }
136 else
137 {
138 buffer++;
139 }
140 }
141
142
143 buffer = buf_bak;
144 time_sec = strtoul(buffer,NULL,10);
145 if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
146 {
147 LYERRLOG("Illeagle input: too large or too small !!!");
148 return -1;
149 }
150
151 tmp_time += time_sec;
152 wake_arlarm = localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800153
l.yang4c4e7382023-08-22 16:13:09 +0800154 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
l.yang3a9c6612023-11-14 11:28:02 +0800155
156 ret = sc_rtc_timer_add_utc(src_id, rtc_id,wake_arlarm,0,wakealarm_notify);
l.yangb488ebc2023-08-21 17:58:00 +0800157 if(ret < 0)
158 {
159 LYINFLOG("Add rtc timer failed!!!!");
160 return -1;
161 }
162
163 LYINFLOG("Set wakealarm success !!!");
164
l.yangb488ebc2023-08-21 17:58:00 +0800165 return 0;
166
167}
168
169
170/*****************************************
171* @brief:set poweralarm time
l.yang3a9c6612023-11-14 11:28:02 +0800172* @param count [IN]:char *buffer, int srcid
l.yangb488ebc2023-08-21 17:58:00 +0800173* @param sum [OUT]:NA
174* @return :success 0, failed -1
175* @todo:
176* @see:NA
177* @warning:
178******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800179ssize_t poweralarm(char *buffer,int src_id )
l.yangb488ebc2023-08-21 17:58:00 +0800180{
l.yang3a9c6612023-11-14 11:28:02 +0800181
l.yangb488ebc2023-08-21 17:58:00 +0800182 unsigned long time_sec = 0;
183 int ret = 0;
l.yang168f8d22023-09-06 14:16:13 +0800184 char *buf_bak = buffer;
l.yang3a9c6612023-11-14 11:28:02 +0800185
l.yang989cbdb2023-09-18 11:29:39 +0800186 int alarmid = 0;
187 char username[8] = "user";
188 time_t tmp_time = 0;
189 struct tm *power_arlarm = NULL;
190 tmp_time = time(NULL);
191 localtime(&tmp_time);
l.yangb488ebc2023-08-21 17:58:00 +0800192 LYLOGSET(LOG_INFO);
193 LYLOGEINIT(USER_LOG_TAG);
l.yang168f8d22023-09-06 14:16:13 +0800194
l.yangb488ebc2023-08-21 17:58:00 +0800195 if(buffer == NULL)
196 {
l.yang168f8d22023-09-06 14:16:13 +0800197 LYERRLOG("Bad input parameter,exit!!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800198 return -1;
199 }
l.yang168f8d22023-09-06 14:16:13 +0800200
201 while(*buffer != '\0' )
202 {
203 if(( *buffer < '0' )|| ( *buffer > '9'))
204 {
205 LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
206 return -1;
207 }
208 else
209 {
210 buffer++;
211 }
212 }
213
l.yangb488ebc2023-08-21 17:58:00 +0800214
l.yang168f8d22023-09-06 14:16:13 +0800215 buffer = buf_bak;
216 time_sec = strtoul(buffer,NULL,10);
l.yang989cbdb2023-09-18 11:29:39 +0800217
218 tmp_time += time_sec;
219 power_arlarm = localtime(&tmp_time);
220
l.yang168f8d22023-09-06 14:16:13 +0800221 if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
222 {
223 LYERRLOG("Illeagle input: too large or too small !!!");
224 return -1;
225 }
l.yangb488ebc2023-08-21 17:58:00 +0800226
l.yang4c4e7382023-08-22 16:13:09 +0800227 LYINFLOG("Set poweralarm %lu seconds",time_sec);
l.yang989cbdb2023-09-18 11:29:39 +0800228
l.yang3a9c6612023-11-14 11:28:02 +0800229
230 alarmid = sc_rtc_get_poweroff_alarm_modid(src_id, username);
l.yang989cbdb2023-09-18 11:29:39 +0800231 if(alarmid < 0)
l.yangb488ebc2023-08-21 17:58:00 +0800232 {
l.yang989cbdb2023-09-18 11:29:39 +0800233 LYERRLOG("Get poweroff alarm id failed !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800234 return -1;
235 }
l.yang989cbdb2023-09-18 11:29:39 +0800236
l.yang3a9c6612023-11-14 11:28:02 +0800237 ret = sc_rtc_add_poweroff_alarm(src_id, alarmid, 0, power_arlarm, NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800238 if(ret < 0)
239 {
240 LYERRLOG("Set power alarm failed !!!");
241 return -1;
242 }
243 LYINFLOG("Set power alarm success !!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800244
l.yangb488ebc2023-08-21 17:58:00 +0800245 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800246
l.yangb488ebc2023-08-21 17:58:00 +0800247}
248
249/**********************************************
250* @brief:cancel_wakealarm,
l.yang3a9c6612023-11-14 11:28:02 +0800251* @param count [IN]:int src_id, int rtc_id
l.yangb488ebc2023-08-21 17:58:00 +0800252* @param sum [OUT]:NA
253* @return :success 0, failed -1
254* @todo:
255* @see:NA
256* @warning:
257********************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800258ssize_t cancel_wakealarm(int src_id, int rtc_id)
l.yangb488ebc2023-08-21 17:58:00 +0800259{
260 int ret = 0;
261
262 LYINFLOG("Enter cancel_wakealarm ");
l.yang3a9c6612023-11-14 11:28:02 +0800263 ret = sc_rtc_timer_del(src_id, rtc_id);
l.yangb488ebc2023-08-21 17:58:00 +0800264 if(ret < 0)
265 {
l.yang168f8d22023-09-06 14:16:13 +0800266 LYERRLOG("Del wakealarm failed!!!");
l.yangb488ebc2023-08-21 17:58:00 +0800267 return -1;
268 }
l.yang3a9c6612023-11-14 11:28:02 +0800269 LYINFLOG("Cancel success ");
l.yangb488ebc2023-08-21 17:58:00 +0800270 return 0;
271
272}
273
274
275/*****************************************
276* @brief:lynq_set_poweralarm
l.yang3a9c6612023-11-14 11:28:02 +0800277* @param count [IN]:unsigned long time_sec , int src_id
l.yangb488ebc2023-08-21 17:58:00 +0800278* @param sum [OUT]:NA
279* @return :success 0, failed -1
280* @todo:
281* @see:NA
282* @warning:
283******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800284int lynq_set_poweralarm(unsigned long time_sec,int src_id)
l.yangb488ebc2023-08-21 17:58:00 +0800285{
l.yang168f8d22023-09-06 14:16:13 +0800286
l.yangb488ebc2023-08-21 17:58:00 +0800287 int ret = 0;
l.yang989cbdb2023-09-18 11:29:39 +0800288 int alarmid = 0;
289 char username[8] = "user";
290
291 time_t tmp_time = 0;
292 struct tm *power_arlarm = NULL;
293 tmp_time = time(NULL);
294 localtime(&tmp_time);
l.yang989cbdb2023-09-18 11:29:39 +0800295
l.yang3a9c6612023-11-14 11:28:02 +0800296
l.yang989cbdb2023-09-18 11:29:39 +0800297 tmp_time += time_sec;
298 power_arlarm = localtime(&tmp_time);
l.yang3a9c6612023-11-14 11:28:02 +0800299
l.yang168f8d22023-09-06 14:16:13 +0800300 if(time_sec < MIN_TIME || time_sec > MAX_TIME )
301 {
302 LYERRLOG("Illeagle input: too large or too small !!!");
303 return -1;
304 }
l.yang989cbdb2023-09-18 11:29:39 +0800305
306 LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
l.yang989cbdb2023-09-18 11:29:39 +0800307
l.yang3a9c6612023-11-14 11:28:02 +0800308
309 alarmid = sc_rtc_get_poweroff_alarm_modid(src_id, username);
l.yang989cbdb2023-09-18 11:29:39 +0800310 if(alarmid < 0)
311 {
312 LYERRLOG("Get poweroff alarm id failed !!!");
313 return -1;
314 }
315
l.yang3a9c6612023-11-14 11:28:02 +0800316 ret = sc_rtc_add_poweroff_alarm(src_id, alarmid, 0, power_arlarm, NULL);
l.yangb488ebc2023-08-21 17:58:00 +0800317 if(ret < 0)
318 {
l.yang989cbdb2023-09-18 11:29:39 +0800319 LYERRLOG("Set power alarm failed !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800320 return -1;
321 }
322
l.yang989cbdb2023-09-18 11:29:39 +0800323 LYINFLOG("Set power alarm success !!!!");
324
l.yangb488ebc2023-08-21 17:58:00 +0800325 return 0;
l.yang989cbdb2023-09-18 11:29:39 +0800326
l.yangb488ebc2023-08-21 17:58:00 +0800327
328}
329
330
331/*****************************************
332* @brief:lynq_set_wakealarm
l.yang3a9c6612023-11-14 11:28:02 +0800333* @param count [IN]:unsigned long time_sec,....
l.yangb488ebc2023-08-21 17:58:00 +0800334* @param sum [OUT]:NA
335* @return :success 0, failed -1
336* @todo:
337* @see:NA
338* @warning:
339******************************************/
l.yang3a9c6612023-11-14 11:28:02 +0800340int 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 +0800341{
342 int ret = 0;
l.yang4c4e7382023-08-22 16:13:09 +0800343
l.yang168f8d22023-09-06 14:16:13 +0800344 time_t tmp_time = 0;
l.yang168f8d22023-09-06 14:16:13 +0800345 struct tm *wake_arlarm = NULL;
346 tmp_time = time(NULL);
l.yang989cbdb2023-09-18 11:29:39 +0800347 localtime(&tmp_time);
l.yang3a9c6612023-11-14 11:28:02 +0800348
l.yang168f8d22023-09-06 14:16:13 +0800349 if(time_sec < MIN_TIME || time_sec > MAX_TIME)
350 {
351 LYERRLOG("Illeagle input: too large or too small !!!");
352 return -1;
353 }
354
l.yang4c4e7382023-08-22 16:13:09 +0800355 LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
l.yang3a9c6612023-11-14 11:28:02 +0800356
l.yang168f8d22023-09-06 14:16:13 +0800357 tmp_time += time_sec;
358 wake_arlarm = localtime(&tmp_time);
359 LYINFLOG("Set wakealarm %lu seconds ",time_sec);
360
l.yang3a9c6612023-11-14 11:28:02 +0800361 ret = sc_rtc_timer_add_utc(src_id, rtc_id,wake_arlarm,0,wakealarm_notify);
l.yangb488ebc2023-08-21 17:58:00 +0800362 if(ret < 0)
363 {
364 LYINFLOG("Add rtc timer failed!!!!");
365 return -1;
366 }
367
368 LYINFLOG("Set wakealarm success !!!");
l.yangb488ebc2023-08-21 17:58:00 +0800369
370 return 0;
371
372}
373
l.yang3a9c6612023-11-14 11:28:02 +0800374
you.chen21c62b72023-09-08 09:41:11 +0800375DEFINE_LYNQ_LIB_LOG(LYNQ_POWERALARM)
l.yangb488ebc2023-08-21 17:58:00 +0800376
377#ifdef __cplusplus
378}
379#endif
380