blob: 87ab4813c9dcb8b55d3cfa9185e2b771857e97a4 [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>
6#include"./include/format_change.h"
7
8
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
14extern "C" ssize_t wakealarm(char *buffer);
15
16
17extern "C" ssize_t poweralarm(char *buffer);
18
19ssize_t poweralarm(char *buffer);
20
21ssize_t wakealarm(char *buffer);
qjbc9b9a962023-08-03 05:54:38 -070022
23int cancel_wakealarm(void);
24
xj79176bd2022-05-26 14:45:57 +080025/*****************************************************************************
26*
27* Prototype : poweralarm
28* Description : set shutdown wake-up alarm clock
29* Input : char *buffer ; input format : 04-23-15-30-00 ( Mon-Day-Hour-Min-Sec ) Or 1200 ( seconds )
30* Output : None
31* Return Value : -1: error ; >0: set to wake up the devices after seconds
32*
33*****************************************************************************/
34ssize_t poweralarm(char *buffer)
35{
36 ssize_t sec;
37 char *time_buff = NULL;
38
39 sec = format_change(buffer); //computing seconds for shutdown alarm
jb.qi18e2ba62022-11-06 19:09:40 -080040 if(sec < 60)
xj79176bd2022-05-26 14:45:57 +080041 {
42 ALOGI("No Mattch\n");
43 return -1;
44 }
45
46 time_buff = (char*)malloc(100);
47 bzero(time_buff,100);
48
49 sprintf(time_buff,"echo +%ld > %s",sec,RTCFILE_POWERALARM); //write formatted data into time_buff
50 system(time_buff);
51 ALOGI(time_buff);
52
53 free(time_buff);
54
55 return sec; // wake-up devices after sec seconds
56}
57
58
59/*****************************************************************************
60* Prototype : wakealarm
61* Description : set the wake-up alarm clock in low power mode
qjbc9b9a962023-08-03 05:54:38 -070062* Input : char *buffer ; input format : 04-23-15-30-00 ( Mon-Day-Hour-Min-Sec ) Or 1200 ( seconds )
xj79176bd2022-05-26 14:45:57 +080063* Output : None
64* Return Value : -1: error ; >0: set to wake up the devices after seconds
65*
66*****************************************************************************/
67ssize_t wakealarm(char *buffer)
68{
69 ssize_t sec;
70 char *time_buff = NULL;
qjbc9b9a962023-08-03 05:54:38 -070071 int ret;
xj79176bd2022-05-26 14:45:57 +080072 sec = format_change(buffer); //computing seconds for lowpower alarm
jb.qi18e2ba62022-11-06 19:09:40 -080073 if(sec < 60)
xj79176bd2022-05-26 14:45:57 +080074 {
75 ALOGI("No Mattch\n");
76 return -1;
77 }
qjbc9b9a962023-08-03 05:54:38 -070078 ret = system("echo +0 > /sys/class/rtc/rtc0/wakealarm");
79 RLOGD("close wakealarm ret= %d\n", ret);
xj79176bd2022-05-26 14:45:57 +080080 time_buff = (char*)malloc(100);
81 bzero(time_buff,100);
82
83 sprintf(time_buff,"echo +%ld > %s",sec,RTCFILE_WAKEALARM); //write formatted data into time_buff
84 system(time_buff);
85 ALOGI(time_buff);
86
87 free(time_buff);
88
89 return sec; // wake-up devices after sec seconds
90}
qjbc9b9a962023-08-03 05:54:38 -070091
92
93/*****************************************************************************
94* Prototype : cancel wakealarm
95* Description : cancel the wake-up alarm clock in low power mode
96* Input : void
97* Output : int
98* Return Value : -1: error ; 0: cannel wakealarm success
99*
100*****************************************************************************/
101
102int cancel_wakealarm(void)
103{
jb.qifa8c8d42023-08-22 18:54:35 -0700104 int ret;
qjbc9b9a962023-08-03 05:54:38 -0700105 ret = system("echo +0 > /sys/class/rtc/rtc0/wakealarm");
jb.qifa8c8d42023-08-22 18:54:35 -0700106 ret = system("echo +315360000 > /sys/class/rtc/rtc0/wakealarm");
qjbc9b9a962023-08-03 05:54:38 -0700107 RLOGD("close wakealarm ret= %d\n", ret);
108 return ret;
109
110}
111
112/*****************************************************************************
113* Prototype : check wake up by rtc
114* Description : check weather AP is waked up by RTC
115* Input : void
116* Output : int
117* Return Value : 1: AP is waked up by rtc ; 0: AP is not waked up by rtc
118*
119*****************************************************************************/
120
121int check_wakeupbydtr(void)
122{
123 FILE *fp;
124 char buf[4];
125 int ret;
126 fp = popen("cat /proc/driver/rtc_wakeup","r");
127 fgets(buf, sizeof(buf), fp);
128 RLOGD("buf=%s\n", buf);
129 ret=atoi(buf);
130 pclose(fp);
131 return ret;
132
133}