-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpower_manager.c
More file actions
146 lines (113 loc) · 2.42 KB
/
power_manager.c
File metadata and controls
146 lines (113 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#define DEVICE_FILE_NAME "/dev/my_battery"
void* thresholdBtn(void *arg);
pthread_t ntid;
static char power_save_mode = 0;
static void sig_usr(int);
static char wbuf[10] = "Write";
static char rbuf[10] = "Read";
static char choice = 0;
int main(int argc, char *argv[]) {
int fd, i, err, status;
fd = open("/proc/battery_notify", O_RDWR | O_NDELAY);
if (fd < 0)
{
fprintf(stderr, "/proc/battery_notify open error!!\n");
return 1;
}
pid_t pid = getpid();
sprintf(wbuf, "%ld", (long)pid);
write(fd, wbuf, 10);
close(fd);
err = pthread_create(&ntid, NULL, thresholdBtn, NULL);
if (err != 0)
{
fprintf(stderr, "pthread_create error!!\n");
exit(1);
}
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
{
fprintf(stderr, "can't catch SIGUSR1\n");
return 1;
}
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
{
fprintf(stderr, "can't catch SIGUSR2\n");
return 1;
}
while(1)
{
system("clear");
fd = open(DEVICE_FILE_NAME, O_RDWR | O_NDELAY);
read(fd, rbuf, 4);
status = atoi(rbuf);
printf("If you want to change threshold, press y\n");
printf("battery status : %d%\n", status);
if (power_save_mode)
printf("power saving mode\n");
printf("----------\n");
for (i=0; i<=status/10; ++i)
printf("|");
printf("\n----------\n");
sleep(1);
if (choice == 'y')
sleep(5);
close(fd);
}
return 0;
}
void* thresholdBtn(void *arg)
{
int fd, threshold;
while(1)
{
scanf("%c", &choice);
if (choice == 'y')
{
fd = open("/proc/battery_threshold", O_RDWR | O_NDELAY);
printf("-----\tchange battery threshold\t-----\n");
read(fd, rbuf, 10);
threshold = atoi(rbuf);
printf("Max : 100, Min : 0, Now : %d\n", threshold);
printf("Enter threshold : ");
scanf("%d", &threshold);
if (threshold >= 0 && threshold <= 100)
{
sprintf(wbuf, "%d", threshold);
write(fd, wbuf, 10);
}
close(fd);
}
choice = 0;
}
}
static void sig_usr(int signo)
{
if (signo == SIGUSR1)
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
{
fprintf(stderr, "can't catch SIGUSR1\n");
return 1;
}
power_save_mode = 1;
}
else if (signo == SIGUSR2)
{
if (signal(SIGUSR2, sig_usr) == SIG_ERR)
{
fprintf(stderr, "can't catch SIGUSR2\n");
return 1;
}
power_save_mode = 0;
}
return;
}