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>
23 #define BATT_STATUS_NAME(status) \
24 (status == CS_CHARGING ? "CHR" : \
25 (status == CS_DISCHARGING ? "BAT" : "FULL"))
27 * Get battery information from /sys. Note that it uses the design capacity to
28 * calculate the percentage, not the last full capacity, so you can see how
29 * worn off your battery is.
32 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, int low_threshold, char *threshold_type, bool last_full_capacity) {
37 char percentagebuf[16];
38 char remainingbuf[256];
39 char emptytimebuf[256];
40 char consumptionbuf[256];
41 const char *walk, *last;
42 char *outwalk = buffer;
48 charging_status_t status = CS_DISCHARGING;
50 memset(statusbuf, '\0', sizeof(statusbuf));
51 memset(percentagebuf, '\0', sizeof(percentagebuf));
52 memset(remainingbuf, '\0', sizeof(remainingbuf));
53 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
54 memset(consumptionbuf, '\0', sizeof(consumptionbuf));
59 static char batpath[512];
60 sprintf(batpath, path, number);
61 if (!slurp(batpath, buf, sizeof(buf))) {
62 OUTPUT_FULL_TEXT("No battery");
66 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
75 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
77 remaining = atoi(walk+1);
79 else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
81 remaining = atoi(walk+1);
83 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
84 present_rate = atoi(walk+1);
85 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
86 voltage = atoi(walk+1);
87 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
88 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
89 * unit instead of μAh. We will calculate it as we need it
91 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
92 present_rate = atoi(walk+1);
93 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
95 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
98 /* The only thing left is the full capacity */
99 if (last_full_capacity) {
100 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
101 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
104 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
105 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
109 full_design = atoi(walk+1);
113 /* the difference between POWER_SUPPLY_ENERGY_NOW and
114 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
115 * given in mWh, the charge in mAh. So calculate every value given in
118 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
119 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
120 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
123 if ((full_design == -1) || (remaining == -1)) {
124 OUTPUT_FULL_TEXT("No battery");
128 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
130 float percentage_remaining = (((float)remaining / (float)full_design) * 100);
131 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%", percentage_remaining);
133 if (present_rate > 0) {
134 float remaining_time;
135 int seconds, hours, minutes, seconds_remaining;
136 if (status == CS_CHARGING)
137 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
138 else if (status == CS_DISCHARGING)
139 remaining_time = ((float)remaining / (float)present_rate);
140 else remaining_time = 0;
142 seconds_remaining = (int)(remaining_time * 3600.0);
144 hours = seconds_remaining / 3600;
145 seconds = seconds_remaining - (hours * 3600);
146 minutes = seconds / 60;
147 seconds -= (minutes * 60);
149 if (status == CS_DISCHARGING && low_threshold > 0) {
150 if (strncmp(threshold_type, "percentage", strlen(threshold_type)) == 0
151 && percentage_remaining < low_threshold) {
152 START_COLOR("color_bad");
153 } else if (strncmp(threshold_type, "time", strlen(threshold_type)) == 0
154 && seconds_remaining < 60 * low_threshold) {
155 START_COLOR("color_bad");
159 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
160 max(hours, 0), max(minutes, 0), max(seconds, 0));
162 empty_time = time(NULL);
163 empty_time += seconds_remaining;
164 empty_tm = localtime(&empty_time);
166 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
167 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
169 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
170 ((float)present_rate / 1000.0 / 1000.0));
174 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
177 size_t sysctl_size = sizeof(sysctl_rslt);
179 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
180 OUTPUT_FULL_TEXT("No battery");
184 present_rate = sysctl_rslt;
185 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
186 OUTPUT_FULL_TEXT("No battery");
190 remaining = sysctl_rslt;
191 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
192 OUTPUT_FULL_TEXT("No battery");
197 if (state == 0 && present_rate == 100)
199 else if (state == 0 && present_rate < 100)
200 status = CS_CHARGING;
202 status = CS_DISCHARGING;
204 full_design = sysctl_rslt;
206 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
208 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
214 hours = minutes / 60;
215 minutes -= (hours * 60);
216 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
217 max(hours, 0), max(minutes, 0));
219 #elif defined(__OpenBSD__)
221 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
222 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
223 * probing acpi(4) devices.
225 struct apm_power_info apm_info;
226 int apm_fd, ac_status, charging;
228 apm_fd = open("/dev/apm", O_RDONLY);
230 OUTPUT_FULL_TEXT("can't open /dev/apm");
233 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
234 OUTPUT_FULL_TEXT("can't read power info");
238 /* Don't bother to go further if there's no battery present. */
239 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
240 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
241 OUTPUT_FULL_TEXT("No battery");
245 switch(apm_info.ac_state) {
247 ac_status = CS_DISCHARGING;
250 ac_status = CS_CHARGING;
253 /* If we don't know what's going on, just assume we're discharging. */
254 ac_status = CS_DISCHARGING;
258 (void)snprintf(statusbuf, sizeof(statusbuf), "%s", BATT_STATUS_NAME(status));
259 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%", apm_info.battery_life);
261 /* Can't give a meaningful value for remaining minutes if we're charging. */
262 if (ac_status == CS_CHARGING)
265 (void)snprintf(remainingbuf, sizeof(remainingbuf), (charging ? "%s" : "%d"),
266 (charging ? "(CHR)" : apm_info.minutes_left));
269 for (walk = format; *walk != '\0'; walk++) {
271 *(outwalk++) = *walk;
275 if (strncmp(walk+1, "status", strlen("status")) == 0) {
276 outwalk += sprintf(outwalk, "%s", statusbuf);
277 walk += strlen("status");
278 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
279 outwalk += sprintf(outwalk, "%s", percentagebuf);
280 walk += strlen("percentage");
281 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
282 outwalk += sprintf(outwalk, "%s", remainingbuf);
283 walk += strlen("remaining");
284 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
285 outwalk += sprintf(outwalk, "%s", emptytimebuf);
286 walk += strlen("emptytime");
287 } else if (strncmp(walk+1, "consumption", strlen("consumption")) == 0) {
288 outwalk += sprintf(outwalk, "%s", consumptionbuf);
289 walk += strlen("consumption");
293 OUTPUT_FULL_TEXT(buffer);