blob: aeb3933e9a37c4cef81d48303381d228779c0436 [file] [log] [blame]
xj79176bd2022-05-26 14:45:57 +08001#include<stdio.h>
2#include<stdlib.h>
3#include<unistd.h>
4#include<stdbool.h>
5#include<log/log.h>
jb.qi8d946942023-08-24 19:17:44 -07006#include"./include/lynq_alarm.h"
7#include"format_change.h"
xj79176bd2022-05-26 14:45:57 +08008
9#define LOG_TAG "libpoweralarm"
10#define RTCFILE_POWERALARM "/sys/class/rtc/rtc0/poweralarm"
11
12#define RTCFILE_WAKEALARM "/sys/class/rtc/rtc0/wakealarm"
13
qjbc9b9a962023-08-03 05:54:38 -070014
xj79176bd2022-05-26 14:45:57 +080015/*****************************************************************************
16*
17* Prototype : poweralarm
18* Description : set shutdown wake-up alarm clock
19* Input : char *buffer ; input format : 04-23-15-30-00 ( Mon-Day-Hour-Min-Sec ) Or 1200 ( seconds )
20* Output : None
21* Return Value : -1: error ; >0: set to wake up the devices after seconds
22*
23*****************************************************************************/
jb.qi8d946942023-08-24 19:17:44 -070024
xj79176bd2022-05-26 14:45:57 +080025ssize_t poweralarm(char *buffer)
26{
27 ssize_t sec;
28 char *time_buff = NULL;
29
30 sec = format_change(buffer); //computing seconds for shutdown alarm
jb.qi18e2ba62022-11-06 19:09:40 -080031 if(sec < 60)
xj79176bd2022-05-26 14:45:57 +080032 {
33 ALOGI("No Mattch\n");
34 return -1;
35 }
36
37 time_buff = (char*)malloc(100);
38 bzero(time_buff,100);
39
40 sprintf(time_buff,"echo +%ld > %s",sec,RTCFILE_POWERALARM); //write formatted data into time_buff
41 system(time_buff);
42 ALOGI(time_buff);
43
44 free(time_buff);
45
46 return sec; // wake-up devices after sec seconds
47}
48
49
50/*****************************************************************************
51* Prototype : wakealarm
52* Description : set the wake-up alarm clock in low power mode
qjbc9b9a962023-08-03 05:54:38 -070053* Input : char *buffer ; input format : 04-23-15-30-00 ( Mon-Day-Hour-Min-Sec ) Or 1200 ( seconds )
xj79176bd2022-05-26 14:45:57 +080054* Output : None
55* Return Value : -1: error ; >0: set to wake up the devices after seconds
56*
57*****************************************************************************/
58ssize_t wakealarm(char *buffer)
59{
60 ssize_t sec;
61 char *time_buff = NULL;
qjbc9b9a962023-08-03 05:54:38 -070062 int ret;
xj79176bd2022-05-26 14:45:57 +080063 sec = format_change(buffer); //computing seconds for lowpower alarm
jb.qi18e2ba62022-11-06 19:09:40 -080064 if(sec < 60)
xj79176bd2022-05-26 14:45:57 +080065 {
66 ALOGI("No Mattch\n");
67 return -1;
68 }
qjbc9b9a962023-08-03 05:54:38 -070069 ret = system("echo +0 > /sys/class/rtc/rtc0/wakealarm");
70 RLOGD("close wakealarm ret= %d\n", ret);
xj79176bd2022-05-26 14:45:57 +080071 time_buff = (char*)malloc(100);
72 bzero(time_buff,100);
73
74 sprintf(time_buff,"echo +%ld > %s",sec,RTCFILE_WAKEALARM); //write formatted data into time_buff
75 system(time_buff);
76 ALOGI(time_buff);
77
78 free(time_buff);
79
80 return sec; // wake-up devices after sec seconds
81}
qjbc9b9a962023-08-03 05:54:38 -070082
83
84/*****************************************************************************
85* Prototype : cancel wakealarm
86* Description : cancel the wake-up alarm clock in low power mode
87* Input : void
88* Output : int
89* Return Value : -1: error ; 0: cannel wakealarm success
90*
91*****************************************************************************/
92
93int cancel_wakealarm(void)
94{
jb.qifa8c8d42023-08-22 18:54:35 -070095 int ret;
qjbc9b9a962023-08-03 05:54:38 -070096 ret = system("echo +0 > /sys/class/rtc/rtc0/wakealarm");
jb.qifa8c8d42023-08-22 18:54:35 -070097 ret = system("echo +315360000 > /sys/class/rtc/rtc0/wakealarm");
qjbc9b9a962023-08-03 05:54:38 -070098 RLOGD("close wakealarm ret= %d\n", ret);
99 return ret;
100
101}
102
103/*****************************************************************************
104* Prototype : check wake up by rtc
105* Description : check weather AP is waked up by RTC
106* Input : void
107* Output : int
108* Return Value : 1: AP is waked up by rtc ; 0: AP is not waked up by rtc
109*
110*****************************************************************************/
111
112int check_wakeupbydtr(void)
113{
114 FILE *fp;
115 char buf[4];
116 int ret;
117 fp = popen("cat /proc/driver/rtc_wakeup","r");
118 fgets(buf, sizeof(buf), fp);
119 RLOGD("buf=%s\n", buf);
120 ret=atoi(buf);
121 pclose(fp);
122 return ret;
123
124}