-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsys.c
167 lines (143 loc) · 3.61 KB
/
sys.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "lifebar.h"
#define PS_PATH "/sys/class/power_supply"
#define TH_PATH "/sys/class/thermal"
#define NET_PATH "/sys/class/net"
int count_acpi_batteries() {
DIR *d;
struct dirent *de;
int count = 0;
d = opendir(PS_PATH);
if(d == NULL) return 0;
while((de = readdir(d)) != NULL) {
if(strstr(de->d_name, "BAT") == de->d_name) {
//name starts with BAT
count++;
}
}
closedir(d);
return count;
}
int count_acpi_thermal() {
DIR *d;
struct dirent *de;
int count = 0;
d = opendir(TH_PATH);
if(d == NULL) return 0;
while((de = readdir(d)) != NULL) {
if(strstr(de->d_name, "thermal") == de->d_name) {
//name starts with thermal
count++;
}
}
closedir(d);
return count;
}
void read_acpi_battery(int b, struct batt_info *bi) {
//save the index
bi->index = b;
//we assume the battery index to exist as filename BAT<index>
char path[128];
FILE *f;
//status
char status[2];
sprintf(path, "%s/BAT%d/status", PS_PATH, b);
f = fopen(path, "r");
if(f == NULL || fgets(status, 2, f) == NULL) {
fprintf(stderr, "%scould not read battery status: '%s'\n",
BAD_MSG, path);
bi->status = UNKNOWN;
}
else {
switch(status[0]) {
case 'C': bi->status = CHARGING; break;
case 'D': bi->status = DISCHARGING; break;
case 'F': bi->status = FULL; break;
default: bi->status = UNKNOWN;
}
fclose(f);
}
//energy when full
char energy_full_s[32];
long int energy_full = 0;
sprintf(path, "%s/BAT%d/energy_full", PS_PATH, b);
f = fopen(path, "r");
if(f == NULL) {
sprintf(path, "%s/BAT%d/charge_full", PS_PATH, b);
f = fopen(path, "r");
}
if(f == NULL || fgets(energy_full_s, 32, f) == NULL) {
fprintf(stderr, "%scould not read battery energy max: '%s'\n",
BAD_MSG, path);
}
else {
energy_full = strtol(energy_full_s, NULL, 10);
fclose(f);
}
//energy now
char energy_now_s[32];
long int energy_now = 0;
sprintf(path, "%s/BAT%d/energy_now", PS_PATH, b);
f = fopen(path, "r");
if(f == NULL) {
sprintf(path, "%s/BAT%d/charge_now", PS_PATH, b);
f = fopen(path, "r");
}
if(f == NULL || fgets(energy_now_s, 32, f) == NULL) {
fprintf(stderr, "%scould not read battery energy now: '%s'\n",
BAD_MSG, path);
}
else {
energy_now = strtol(energy_now_s, NULL, 10);
fclose(f);
}
bi->percent = (int)(energy_now * 100 / (double)energy_full);
}
void read_acpi_thermal(int t, struct thermal_info *therm) {
//save the index
therm->index = t;
//we assume the thermal index to exist as filename thermal_zone<index>
char path[128];
FILE *f;
//temp
char temp_s[32];
long int temp = 0;
sprintf(path, "%s/thermal_zone%d/temp", TH_PATH, t);
f = fopen(path, "r");
if(f == NULL || fgets(temp_s, 32, f) == NULL) {
fprintf(stderr, "%scould not read thermal status: '%s'\n",
BAD_MSG, path);
}
else {
temp = strtol(temp_s, NULL, 10);
fclose(f);
}
therm->temp_c = temp / 1000;
}
void read_net_speed(char *ifname, struct net_speed_info *net) {
char path[128];
FILE *f;
//download
char rxb_s[32];
sprintf(path, "%s/%s/statistics/rx_bytes", NET_PATH, ifname);
f = fopen(path, "r");
if(f == NULL || fgets(rxb_s, 32, f) == NULL) {
fprintf(stderr, "%scould not read interface speed: '%s'\n",
BAD_MSG, path);
}
else {
net->down_bytes = strtol(rxb_s, NULL, 10);
fclose(f);
}
//upload
char txb_s[32];
sprintf(path, "%s/%s/statistics/tx_bytes", NET_PATH, ifname);
f = fopen(path, "r");
if(f == NULL || fgets(txb_s, 32, f) == NULL) {
fprintf(stderr, "%scould not read interface speed: '%s'\n",
BAD_MSG, path);
}
else {
net->up_bytes = strtol(txb_s, NULL, 10);
fclose(f);
}
}