blob: 3d174115df0a7537a776615e35855c0dfdf52a5d [file] [log] [blame]
/*******************************************************
*
* @brief: none
* @details: Add wakealrm and poweralarm api code
* @author: l.yang
* @date: 2023.8.17
* @version: V1.0
* @copyright: Copyright (c) MobileTek
*
*********************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdbool.h>
#include<log/log.h>
#include "liblog/lynq_deflog.h"
#include <include/libpoweralarm.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
#define USER_LOG_TAG "LYNQ_POWERALARM"
#define MIN_TIME 1
#define MAX_TIME 268435456
static int wk_rtc_id = 1;
static int pw_rtc_id = 2;
static int wk_srcid = 0;
typedef int (*sc_rtc_timer_exp_cb)(unsigned int src_id, int rtc_id);
extern int sc_rtc_timer_init(void);
extern int sc_rtc_timer_uninit(void );
extern int sc_rtc_timer_add(int srcid, int rtc_id, unsigned long ulSec, sc_rtc_timer_exp_cb rtc_notify);
extern int sc_rtc_timer_del(int srcid, int rtc_id);
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);
/*****************************************
* @brief:sc_rtc_timer_add_cb
* @param count [IN]:src_id, rtc_id,
* @param sum [OUT]:NA
* @return :0
* @todo:
* @see:NA
* @warning:
******************************************/
int sc_rtc_timer_add_cb(unsigned int src_id, int rtc_id)
{
return 0;
}
/*****************************************
* @brief:Set wakealarm time
* @param count [IN]:buffer,
* @param sum [OUT]:NA
* @return :success 0, failed -1
* @todo:
* @see:NA
* @warning:
******************************************/
ssize_t wakealarm(char *buffer)
{
unsigned long time_sec = 0;
int ret = 0;
char *buf_bak = buffer;
time_t tmp_time = 0;
struct tm *ptime = NULL;
struct tm *wake_arlarm = NULL;
tmp_time = time(NULL);
ptime = localtime(&tmp_time);
LYLOGSET(LOG_INFO);
LYLOGEINIT(USER_LOG_TAG);
if(buffer == NULL)
{
LYINFLOG("Bad input parameter,exit!!!!");
return -1;
}
while(*buffer != '\0' )
{
if((*buffer < '0') || (*buffer > '9'))
{
LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
return -1;
}
else
{
buffer++;
}
}
buffer = buf_bak;
time_sec = strtoul(buffer,NULL,10);
if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
{
LYERRLOG("Illeagle input: too large or too small !!!");
return -1;
}
tmp_time += time_sec;
wake_arlarm = localtime(&tmp_time);
LYINFLOG("Set wakealarm %lu seconds ",time_sec);
wk_srcid = sc_rtc_timer_init();
if (wk_srcid <= 0)
{
LYINFLOG("rtc_timer_init fail!");
return -1;
}
ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);
if(ret < 0)
{
LYINFLOG("Add rtc timer failed!!!!");
return -1;
}
LYINFLOG("Set wakealarm success !!!");
ret = sc_rtc_timer_uninit();
if(ret != 0)
{
LYINFLOG("Deinit failed");
}
return 0;
}
/*****************************************
* @brief:set poweralarm time
* @param count [IN]:buffer,
* @param sum [OUT]:NA
* @return :success 0, failed -1
* @todo:
* @see:NA
* @warning:
******************************************/
ssize_t poweralarm(char *buffer)
{
int srcid = 0;
unsigned long time_sec = 0;
int ret = 0;
char *buf_bak = buffer;
LYLOGSET(LOG_INFO);
LYLOGEINIT(USER_LOG_TAG);
if(buffer == NULL)
{
LYERRLOG("Bad input parameter,exit!!!!");
return -1;
}
while(*buffer != '\0' )
{
if(( *buffer < '0' )|| ( *buffer > '9'))
{
LYERRLOG("Illeagel input buffer , there is invalid character!!!!");
return -1;
}
else
{
buffer++;
}
}
buffer = buf_bak;
time_sec = strtoul(buffer,NULL,10);
if(time_sec < MIN_TIME || time_sec > MAX_TIME || errno == ERANGE)
{
LYERRLOG("Illeagle input: too large or too small !!!");
return -1;
}
LYINFLOG("Set poweralarm %lu seconds",time_sec);
srcid = sc_rtc_timer_init();
if (srcid <= 0)
{
LYERRLOG("rtc_timer_init fail!");
return -1;
}
ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);
if(ret < 0)
{
LYERRLOG("Add rtc timer failed!!!!");
return -1;
}
LYINFLOG("Set poweralarm success !!!");
ret = sc_rtc_timer_uninit();
if(ret != 0)
{
LYINFLOG("Deinit failed");
}
return 0;
}
/**********************************************
* @brief:cancel_wakealarm,
* @param count [IN]:void
* @param sum [OUT]:NA
* @return :success 0, failed -1
* @todo:
* @see:NA
* @warning:
********************************************/
ssize_t cancel_wakealarm(void)
{
int ret = 0;
LYINFLOG("Enter cancel_wakealarm ");
ret = sc_rtc_timer_del(wk_srcid, wk_rtc_id);
if(ret < 0)
{
LYERRLOG("Del wakealarm failed!!!");
return -1;
}
return 0;
}
/*****************************************
* @brief:lynq_set_poweralarm
* @param count [IN]:unsigned long time_sec
* @param sum [OUT]:NA
* @return :success 0, failed -1
* @todo:
* @see:NA
* @warning:
******************************************/
int lynq_set_poweralarm(unsigned long time_sec)
{
int ret = 0;
int srcid = 0;
LYLOGSET(LOG_INFO);
LYLOGEINIT(USER_LOG_TAG);
if(time_sec < MIN_TIME || time_sec > MAX_TIME )
{
LYERRLOG("Illeagle input: too large or too small !!!");
return -1;
}
LYINFLOG("lynq_set_poweralarm %lu seconds",time_sec);
srcid = sc_rtc_timer_init();
if (srcid <= 0)
{
LYERRLOG("rtc_timer_init fail!");
return -1;
}
ret = sc_rtc_timer_add(srcid, pw_rtc_id, time_sec, sc_rtc_timer_add_cb);
if(ret < 0)
{
LYINFLOG("Add rtc timer failed!!!!");
return -1;
}
LYINFLOG("Set poweralarm success !!!");
ret = sc_rtc_timer_uninit();
if(ret != 0)
{
LYINFLOG("Deinit failed");
}
return 0;
}
/*****************************************
* @brief:lynq_set_wakealarm
* @param count [IN]:unsigned long time_sec
* @param sum [OUT]:NA
* @return :success 0, failed -1
* @todo:
* @see:NA
* @warning:
******************************************/
int lynq_set_wakealarm(unsigned long time_sec)
{
int ret = 0;
time_t tmp_time = 0;
struct tm *ptime = NULL;
struct tm *wake_arlarm = NULL;
tmp_time = time(NULL);
ptime = localtime(&tmp_time);
LYLOGSET(LOG_INFO);
LYLOGEINIT(USER_LOG_TAG);
if(time_sec < MIN_TIME || time_sec > MAX_TIME)
{
LYERRLOG("Illeagle input: too large or too small !!!");
return -1;
}
LYINFLOG("lynq_set_wakealarm %lu seconds ",time_sec);
wk_srcid = sc_rtc_timer_init();
if (wk_srcid <= 0)
{
LYINFLOG("rtc_timer_init fail!");
return -1;
}
tmp_time += time_sec;
wake_arlarm = localtime(&tmp_time);
LYINFLOG("Set wakealarm %lu seconds ",time_sec);
ret = sc_rtc_timer_add_utc(wk_srcid, wk_rtc_id,wake_arlarm,0,sc_rtc_timer_add_cb);
if(ret < 0)
{
LYINFLOG("Add rtc timer failed!!!!");
return -1;
}
LYINFLOG("Set wakealarm success !!!");
ret = sc_rtc_timer_uninit();
if(ret != 0)
{
LYINFLOG("Deinit failed!!!");
}
return 0;
}
DEFINE_LYNQ_LIB_LOG(LYNQ_POWERALARM)
#ifdef __cplusplus
}
#endif