6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
16 #if defined(__OpenBSD__)
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/fcntl.h>
20 #include <machine/apmvar.h>
24 * Get battery information from /sys. Note that it uses the design capacity to
25 * calculate the percentage, not the last full capacity, so you can see how
26 * worn off your battery is.
29 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, bool last_full_capacity) {
34 char percentagebuf[16];
35 char remainingbuf[256];
36 char emptytimebuf[256];
37 const char *walk, *last;
38 char *outwalk = buffer;
42 charging_status_t status = CS_DISCHARGING;
44 memset(statusbuf, '\0', sizeof(statusbuf));
45 memset(percentagebuf, '\0', sizeof(percentagebuf));
46 memset(remainingbuf, '\0', sizeof(remainingbuf));
47 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
52 static char batpath[512];
53 sprintf(batpath, path, number);
54 if (!slurp(batpath, buf, sizeof(buf))) {
55 OUTPUT_FULL_TEXT("No battery");
59 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
68 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
69 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
70 remaining = atoi(walk+1);
71 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
72 present_rate = atoi(walk+1);
73 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
74 present_rate = atoi(walk+1);
75 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
77 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
80 /* The only thing left is the full capacity */
81 if (last_full_capacity) {
82 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
83 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
86 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
87 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
91 full_design = atoi(walk+1);
95 if ((full_design == -1) || (remaining == -1)) {
96 OUTPUT_FULL_TEXT("No battery");
100 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
101 (status == CS_CHARGING ? "CHR" :
102 (status == CS_DISCHARGING ? "BAT" : "FULL")));
104 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
105 (((float)remaining / (float)full_design) * 100));
107 if (present_rate > 0) {
108 float remaining_time;
109 int seconds, hours, minutes, seconds_remaining;
110 if (status == CS_CHARGING)
111 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
112 else if (status == CS_DISCHARGING)
113 remaining_time = ((float)remaining / (float)present_rate);
114 else remaining_time = 0;
116 seconds_remaining = (int)(remaining_time * 3600.0);
118 hours = seconds_remaining / 3600;
119 seconds = seconds_remaining - (hours * 3600);
120 minutes = seconds / 60;
121 seconds -= (minutes * 60);
123 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
124 max(hours, 0), max(minutes, 0), max(seconds, 0));
126 empty_time = time(NULL);
127 empty_time += seconds_remaining;
128 empty_tm = localtime(&empty_time);
130 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
131 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
133 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
136 size_t sysctl_size = sizeof(sysctl_rslt);
138 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
139 OUTPUT_FULL_TEXT("No battery");
143 present_rate = sysctl_rslt;
144 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
145 OUTPUT_FULL_TEXT("No battery");
149 remaining = sysctl_rslt;
150 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
151 OUTPUT_FULL_TEXT("No battery");
156 if (state == 0 && present_rate == 100)
158 else if (state == 0 && present_rate < 100)
159 status = CS_CHARGING;
161 status = CS_DISCHARGING;
163 full_design = sysctl_rslt;
165 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
166 (status == CS_CHARGING ? "CHR" :
167 (status == CS_DISCHARGING ? "BAT" : "FULL")));
169 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
175 hours = minutes / 60;
176 minutes -= (hours * 60);
177 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
178 max(hours, 0), max(minutes, 0));
180 #elif defined(__OpenBSD__)
182 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
183 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
184 * probing acpi(4) devices.
186 struct apm_power_info apm_info;
187 int apm_fd, ac_status, charging;
189 apm_fd = open("/dev/apm", O_RDONLY);
191 OUTPUT_FULL_TEXT("can't open /dev/apm");
194 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
195 OUTPUT_FULL_TEXT("can't read power info");
199 /* Don't bother to go further if there's no battery present. */
200 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
201 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
202 OUTPUT_FULL_TEXT("No battery");
206 switch(apm_info.ac_state) {
208 ac_status = CS_DISCHARGING;
211 ac_status = CS_CHARGING;
214 /* If we don't know what's going on, just assume we're discharging. */
215 ac_status = CS_DISCHARGING;
219 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
220 (ac_status == CS_CHARGING ? "CHR" :
221 (ac_status == CS_DISCHARGING ? "BAT" : "FULL")));
223 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
225 /* Can't give a meaningful value for remaining minutes if we're charging. */
226 if (ac_status == CS_CHARGING)
229 (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
230 (charging ? "(CHR)" : apm_info.minutes_left));
233 for (walk = format; *walk != '\0'; walk++) {
235 *(outwalk++) = *walk;
239 if (strncmp(walk+1, "status", strlen("status")) == 0) {
240 outwalk += sprintf(outwalk, "%s", statusbuf);
241 walk += strlen("status");
242 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
243 outwalk += sprintf(outwalk, "%s", percentagebuf);
244 walk += strlen("percentage");
245 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
246 outwalk += sprintf(outwalk, "%s", remainingbuf);
247 walk += strlen("remaining");
248 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
249 outwalk += sprintf(outwalk, "%s", emptytimebuf);
250 walk += strlen("emptytime");
254 OUTPUT_FULL_TEXT(buffer);