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))
98 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
99 (status == CS_CHARGING ? "CHR" :
100 (status == CS_DISCHARGING ? "BAT" : "FULL")));
102 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
103 (((float)remaining / (float)full_design) * 100));
105 if (present_rate > 0) {
106 float remaining_time;
107 int seconds, hours, minutes, seconds_remaining;
108 if (status == CS_CHARGING)
109 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
110 else if (status == CS_DISCHARGING)
111 remaining_time = ((float)remaining / (float)present_rate);
112 else remaining_time = 0;
114 seconds_remaining = (int)(remaining_time * 3600.0);
116 hours = seconds_remaining / 3600;
117 seconds = seconds_remaining - (hours * 3600);
118 minutes = seconds / 60;
119 seconds -= (minutes * 60);
121 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
122 max(hours, 0), max(minutes, 0), max(seconds, 0));
124 empty_time = time(NULL);
125 empty_time += seconds_remaining;
126 empty_tm = localtime(&empty_time);
128 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
129 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
131 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
134 size_t sysctl_size = sizeof(sysctl_rslt);
136 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
137 printf("No battery");
141 present_rate = sysctl_rslt;
142 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
143 printf("No battery");
147 remaining = sysctl_rslt;
148 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
149 printf("No battery");
154 if (state == 0 && present_rate == 100)
156 else if (state == 0 && present_rate < 100)
157 status = CS_CHARGING;
159 status = CS_DISCHARGING;
161 full_design = sysctl_rslt;
163 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
164 (status == CS_CHARGING ? "CHR" :
165 (status == CS_DISCHARGING ? "BAT" : "FULL")));
167 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
173 hours = minutes / 60;
174 minutes -= (hours * 60);
175 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
176 max(hours, 0), max(minutes, 0));
178 #elif defined(__OpenBSD__)
180 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
181 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
182 * probing acpi(4) devices.
184 struct apm_power_info apm_info;
185 int apm_fd, ac_status, charging;
187 apm_fd = open("/dev/apm", O_RDONLY);
189 OUTPUT_FULL_TEXT("can't open /dev/apm");
192 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
193 OUTPUT_FULL_TEXT("can't read power info");
197 /* Don't bother to go further if there's no battery present. */
198 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
199 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
200 OUTPUT_FULL_TEXT("No battery");
204 switch(apm_info.ac_state) {
206 ac_status = CS_DISCHARGING;
209 ac_status = CS_CHARGING;
212 /* If we don't know what's going on, just assume we're discharging. */
213 ac_status = CS_DISCHARGING;
217 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
218 (ac_status == CS_CHARGING ? "CHR" :
219 (ac_status == CS_DISCHARGING ? "BAT" : "FULL")));
221 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
223 /* Can't give a meaningful value for remaining minutes if we're charging. */
224 if (ac_status == CS_CHARGING)
227 (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
228 (charging ? "(CHR)" : apm_info.minutes_left));
231 for (walk = format; *walk != '\0'; walk++) {
233 *(outwalk++) = *walk;
237 if (strncmp(walk+1, "status", strlen("status")) == 0) {
238 outwalk += sprintf(outwalk, "%s", statusbuf);
239 walk += strlen("status");
240 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
241 outwalk += sprintf(outwalk, "%s", percentagebuf);
242 walk += strlen("percentage");
243 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
244 outwalk += sprintf(outwalk, "%s", remainingbuf);
245 walk += strlen("remaining");
246 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
247 outwalk += sprintf(outwalk, "%s", emptytimebuf);
248 walk += strlen("emptytime");
252 OUTPUT_FULL_TEXT(buffer);