1 // vim:ts=4:sw=4:expandtab
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
15 #include <sys/types.h>
18 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
19 #include <dev/acpica/acpiio.h>
20 #include <sys/sysctl.h>
21 #include <sys/types.h>
24 #if defined(__DragonFly__)
25 #include <sys/fcntl.h>
28 #if defined(__OpenBSD__)
29 #include <machine/apmvar.h>
30 #include <sys/fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
35 #if defined(__NetBSD__)
37 #include <prop/proplib.h>
38 #include <sys/envsys.h>
48 /* A description of the state of one or more batteries. */
50 /* measured properties */
51 int full_design; /* in uAh */
52 int full_last; /* in uAh */
53 int remaining; /* in uAh */
54 int present_rate; /* in uA, always non-negative */
56 /* derived properties */
57 int seconds_remaining;
58 float percentage_remaining;
59 charging_status_t status;
62 #if defined(__DragonFly__)
63 #define ACPIDEV "/dev/acpi"
66 static bool acpi_init(void) {
68 acpifd = open(ACPIDEV, O_RDWR);
70 acpifd = open(ACPIDEV, O_RDONLY);
78 #if defined(LINUX) || defined(__NetBSD__)
80 * Add batt_info data to acc.
82 static void add_battery_info(struct battery_info *acc, const struct battery_info *batt_info) {
83 if (acc->remaining < 0) {
84 /* initialize accumulator so we can add to it */
88 acc->present_rate = 0;
91 acc->full_design += batt_info->full_design;
92 acc->full_last += batt_info->full_last;
93 acc->remaining += batt_info->remaining;
95 /* make present_rate negative for discharging and positive for charging */
96 int present_rate = (acc->status == CS_DISCHARGING ? -1 : 1) * acc->present_rate;
97 present_rate += (batt_info->status == CS_DISCHARGING ? -1 : 1) * batt_info->present_rate;
100 switch (acc->status) {
102 acc->status = batt_info->status;
106 if (present_rate > 0)
107 acc->status = CS_CHARGING;
108 /* else if batt_info is DISCHARGING: no conflict
109 * else if batt_info is CHARGING: present_rate should indicate that
110 * else if batt_info is FULL: but something else is discharging */
114 if (present_rate < 0)
115 acc->status = CS_DISCHARGING;
116 /* else if batt_info is DISCHARGING: present_rate should indicate that
117 * else if batt_info is CHARGING: no conflict
118 * else if batt_info is FULL: but something else is charging */
122 if (batt_info->status != CS_UNKNOWN)
123 acc->status = batt_info->status;
124 /* else: retain FULL, since it is more specific than UNKNOWN */
128 acc->present_rate = abs(present_rate);
132 static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, int number, const char *path, const char *format_down) {
133 char *outwalk = buffer;
137 const char *walk, *last;
138 bool watt_as_unit = false;
141 sprintf(batpath, path, number);
144 if (!slurp(batpath, buf, sizeof(buf))) {
145 OUTPUT_FULL_TEXT(format_down);
149 for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
158 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW=")) {
160 batt_info->remaining = atoi(walk + 1);
161 batt_info->percentage_remaining = -1;
162 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW=")) {
163 watt_as_unit = false;
164 batt_info->remaining = atoi(walk + 1);
165 batt_info->percentage_remaining = -1;
166 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CAPACITY=") && batt_info->remaining == -1) {
167 batt_info->percentage_remaining = atoi(walk + 1);
168 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW="))
169 batt_info->present_rate = abs(atoi(walk + 1));
170 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW="))
171 voltage = abs(atoi(walk + 1));
172 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
173 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
174 * unit instead of μAh. We will calculate it as we need it
176 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW="))
177 batt_info->present_rate = abs(atoi(walk + 1));
178 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
179 batt_info->status = CS_CHARGING;
180 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
181 batt_info->status = CS_FULL;
182 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging") || BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Not charging"))
183 batt_info->status = CS_DISCHARGING;
184 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
185 batt_info->status = CS_UNKNOWN;
186 else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN=") ||
187 BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN="))
188 batt_info->full_design = atoi(walk + 1);
189 else if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL=") ||
190 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL="))
191 batt_info->full_last = atoi(walk + 1);
194 /* the difference between POWER_SUPPLY_ENERGY_NOW and
195 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
196 * given in mWh, the charge in mAh. So calculate every value given in
198 if (!watt_as_unit && voltage >= 0) {
199 if (batt_info->present_rate > 0) {
200 batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
202 if (batt_info->remaining > 0) {
203 batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0));
205 if (batt_info->full_design > 0) {
206 batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0));
208 if (batt_info->full_last > 0) {
209 batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0));
212 #elif defined(__DragonFly__)
213 union acpi_battery_ioctl_arg battio;
215 battio.unit = number;
216 ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio);
217 batt_info->full_design = battio.bif.dcap;
218 batt_info->full_last = battio.bif.lfcap;
219 battio.unit = number;
220 ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio);
221 batt_info->percentage_remaining = battio.battinfo.cap;
222 batt_info->present_rate = battio.battinfo.rate;
223 batt_info->seconds_remaining = battio.battinfo.min * 60;
224 switch (battio.battinfo.state) {
226 batt_info->status = CS_FULL;
228 case ACPI_BATT_STAT_CHARGING:
229 batt_info->status = CS_CHARGING;
231 case ACPI_BATT_STAT_DISCHARG:
232 batt_info->status = CS_DISCHARGING;
235 batt_info->status = CS_UNKNOWN;
237 OUTPUT_FULL_TEXT(format_down);
239 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
242 size_t sysctl_size = sizeof(sysctl_rslt);
244 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
245 OUTPUT_FULL_TEXT(format_down);
249 batt_info->percentage_remaining = sysctl_rslt;
250 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
251 OUTPUT_FULL_TEXT(format_down);
255 batt_info->seconds_remaining = sysctl_rslt * 60;
256 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
257 OUTPUT_FULL_TEXT(format_down);
262 if (state == 0 && batt_info->percentage_remaining == 100)
263 batt_info->status = CS_FULL;
264 else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
265 batt_info->status = CS_CHARGING;
267 batt_info->status = CS_DISCHARGING;
268 #elif defined(__OpenBSD__)
270 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
271 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
272 * probing acpi(4) devices.
274 struct apm_power_info apm_info;
277 apm_fd = open("/dev/apm", O_RDONLY);
279 OUTPUT_FULL_TEXT("can't open /dev/apm");
282 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
283 OUTPUT_FULL_TEXT("can't read power info");
287 /* Don't bother to go further if there's no battery present. */
288 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
289 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
290 OUTPUT_FULL_TEXT(format_down);
294 switch (apm_info.ac_state) {
296 batt_info->status = CS_DISCHARGING;
299 batt_info->status = CS_CHARGING;
302 /* If we don't know what's going on, just assume we're discharging. */
303 batt_info->status = CS_DISCHARGING;
307 batt_info->percentage_remaining = apm_info.battery_life;
309 /* Can't give a meaningful value for remaining minutes if we're charging. */
310 if (batt_info->status != CS_CHARGING) {
311 batt_info->seconds_remaining = apm_info.minutes_left * 60;
313 #elif defined(__NetBSD__)
315 * Using envsys(4) via sysmon(4).
318 bool is_found = false;
319 char sensor_desc[16];
321 prop_dictionary_t dict;
323 prop_object_iterator_t iter;
324 prop_object_iterator_t iter2;
325 prop_object_t obj, obj2, obj3, obj4, obj5;
328 (void)snprintf(sensor_desc, sizeof(sensor_desc), "acpibat%d", number);
330 fd = open("/dev/sysmon", O_RDONLY);
332 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
336 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
342 if (prop_dictionary_count(dict) == 0) {
343 prop_object_release(dict);
348 iter = prop_dictionary_iterator(dict);
350 prop_object_release(dict);
354 /* iterate over the dictionary returned by the kernel */
355 while ((obj = prop_object_iterator_next(iter)) != NULL) {
356 /* skip this dict if it's not what we're looking for */
358 /* we want all batteries */
359 if (!BEGINS_WITH(prop_dictionary_keysym_cstring_nocopy(obj),
363 /* we want a specific battery */
364 if (strcmp(sensor_desc,
365 prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
371 array = prop_dictionary_get_keysym(dict, obj);
372 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
373 prop_object_iterator_release(iter);
374 prop_object_release(dict);
379 iter2 = prop_array_iterator(array);
381 prop_object_iterator_release(iter);
382 prop_object_release(dict);
387 struct battery_info batt_buf = {
392 .status = CS_UNKNOWN,
395 bool watt_as_unit = false;
397 /* iterate over array of dicts specific to target battery */
398 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
399 obj3 = prop_dictionary_get(obj2, "description");
404 if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) {
405 obj3 = prop_dictionary_get(obj2, "cur-value");
407 if (prop_number_integer_value(obj3))
408 batt_buf.status = CS_CHARGING;
410 batt_buf.status = CS_DISCHARGING;
411 } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) {
412 obj3 = prop_dictionary_get(obj2, "cur-value");
413 obj4 = prop_dictionary_get(obj2, "max-value");
414 obj5 = prop_dictionary_get(obj2, "type");
416 batt_buf.remaining = prop_number_integer_value(obj3);
417 batt_buf.full_design = prop_number_integer_value(obj4);
419 if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0)
420 watt_as_unit = false;
423 } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) {
424 obj3 = prop_dictionary_get(obj2, "cur-value");
425 batt_buf.present_rate = prop_number_integer_value(obj3);
426 } else if (strcmp("charge rate", prop_string_cstring_nocopy(obj3)) == 0) {
427 obj3 = prop_dictionary_get(obj2, "cur-value");
428 batt_info->present_rate = prop_number_integer_value(obj3);
429 } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) {
430 obj3 = prop_dictionary_get(obj2, "cur-value");
431 batt_buf.full_last = prop_number_integer_value(obj3);
432 } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) {
433 obj3 = prop_dictionary_get(obj2, "cur-value");
434 voltage = prop_number_integer_value(obj3);
437 prop_object_iterator_release(iter2);
439 if (!watt_as_unit && voltage != -1) {
440 batt_buf.present_rate = (((float)voltage / 1000.0) * ((float)batt_buf.present_rate / 1000.0));
441 batt_buf.remaining = (((float)voltage / 1000.0) * ((float)batt_buf.remaining / 1000.0));
442 batt_buf.full_design = (((float)voltage / 1000.0) * ((float)batt_buf.full_design / 1000.0));
443 batt_buf.full_last = (((float)voltage / 1000.0) * ((float)batt_buf.full_last / 1000.0));
446 if (batt_buf.remaining == batt_buf.full_design)
447 batt_buf.status = CS_FULL;
449 add_battery_info(batt_info, &batt_buf);
452 prop_object_iterator_release(iter);
453 prop_object_release(dict);
457 OUTPUT_FULL_TEXT(format_down);
461 batt_info->present_rate = abs(batt_info->present_rate);
468 * Populate batt_info with aggregate information about all batteries.
469 * Returns false on error, and an error message will have been written.
471 static bool slurp_all_batteries(struct battery_info *batt_info, yajl_gen json_gen, char *buffer, const char *path, const char *format_down) {
473 char *outwalk = buffer;
474 bool is_found = false;
477 char *globpath = sstrdup(path);
478 if ((placeholder = strstr(path, "%d")) != NULL) {
479 char *globplaceholder = globpath + (placeholder - path);
480 *globplaceholder = '*';
481 strcpy(globplaceholder + 1, placeholder + 2);
484 if (!strcmp(globpath, path)) {
485 OUTPUT_FULL_TEXT("no '%d' in battery path");
490 if (glob(globpath, 0, NULL, &globbuf) == 0) {
491 for (size_t i = 0; i < globbuf.gl_pathc; i++) {
492 /* Probe to see if there is such a battery. */
493 struct battery_info batt_buf = {
498 .status = CS_UNKNOWN,
500 if (!slurp_battery_info(&batt_buf, json_gen, buffer, i, globbuf.gl_pathv[i], format_down)) {
507 add_battery_info(batt_info, &batt_buf);
514 OUTPUT_FULL_TEXT(format_down);
518 batt_info->present_rate = abs(batt_info->present_rate);
520 /* FreeBSD and OpenBSD only report aggregates. NetBSD always
521 * iterates through all batteries, so it's more efficient to
522 * aggregate in slurp_battery_info. */
523 return slurp_battery_info(batt_info, json_gen, buffer, -1, path, format_down);
529 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds) {
531 char *outwalk = buffer;
532 struct battery_info batt_info = {
537 .seconds_remaining = -1,
538 .percentage_remaining = -1,
539 .status = CS_UNKNOWN,
541 bool colorful_output = false;
543 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
544 /* These OSes report battery stats in whole percent. */
545 integer_battery_capacity = true;
547 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
548 /* These OSes report battery time in minutes. */
553 if (!slurp_all_batteries(&batt_info, json_gen, buffer, path, format_down))
556 if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down))
560 // *Choose* a measure of the 'full' battery. It is whichever is better of
561 // the battery's (hardware-given) design capacity (batt_info.full_design)
562 // and the battery's last known good charge (batt_info.full_last).
563 // We prefer the design capacity, but use the last capacity if we don't have it,
564 // or if we are asked to (last_full_capacity == true); but similarly we use
565 // the design capacity if we don't have the last capacity.
566 // If we don't have either then both full_design and full_last <= 0,
567 // which implies full <= 0, which bails out on the following line.
568 int full = batt_info.full_design;
569 if (full <= 0 || (last_full_capacity && batt_info.full_last > 0)) {
570 full = batt_info.full_last;
572 if (full <= 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) {
573 /* We have no physical measurements and no estimates. Nothing
574 * much we can report, then. */
575 OUTPUT_FULL_TEXT(format_down);
579 if (batt_info.percentage_remaining < 0) {
580 batt_info.percentage_remaining = (((float)batt_info.remaining / (float)full) * 100);
581 /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
582 * charged, even though that’s plainly wrong. For people who chose to see
583 * the percentage calculated based on the last full capacity, we clamp the
584 * value to 100%, as that makes more sense.
585 * See http://bugs.debian.org/785398 */
586 if (last_full_capacity && batt_info.percentage_remaining > 100) {
587 batt_info.percentage_remaining = 100;
591 if (batt_info.seconds_remaining < 0 && batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
592 if (batt_info.status == CS_CHARGING)
593 batt_info.seconds_remaining = 3600.0 * (full - batt_info.remaining) / batt_info.present_rate;
594 else if (batt_info.status == CS_DISCHARGING)
595 batt_info.seconds_remaining = 3600.0 * batt_info.remaining / batt_info.present_rate;
597 batt_info.seconds_remaining = 0;
600 if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
601 if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
602 START_COLOR("color_bad");
603 colorful_output = true;
604 } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
605 START_COLOR("color_bad");
606 colorful_output = true;
610 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
612 if (outwalk == prevoutwalk) { \
613 if (outwalk > buffer && isspace((int)outwalk[-1])) \
615 else if (isspace((int)*(walk + 1))) \
620 for (walk = format; *walk != '\0'; walk++) {
621 char *prevoutwalk = outwalk;
624 *(outwalk++) = *walk;
626 } else if (BEGINS_WITH(walk + 1, "status")) {
627 const char *statusstr;
628 switch (batt_info.status) {
630 statusstr = status_chr;
633 statusstr = status_bat;
636 statusstr = status_full;
639 statusstr = status_unk;
642 outwalk += sprintf(outwalk, "%s", statusstr);
643 walk += strlen("status");
645 } else if (BEGINS_WITH(walk + 1, "percentage")) {
646 if (integer_battery_capacity) {
647 outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
649 outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
651 walk += strlen("percentage");
653 } else if (BEGINS_WITH(walk + 1, "remaining")) {
654 if (batt_info.seconds_remaining >= 0) {
655 int seconds, hours, minutes;
657 hours = batt_info.seconds_remaining / 3600;
658 seconds = batt_info.seconds_remaining - (hours * 3600);
659 minutes = seconds / 60;
660 seconds -= (minutes * 60);
663 outwalk += sprintf(outwalk, "%02d:%02d",
664 max(hours, 0), max(minutes, 0));
666 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
667 max(hours, 0), max(minutes, 0), max(seconds, 0));
669 walk += strlen("remaining");
670 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
672 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
673 if (batt_info.seconds_remaining >= 0) {
674 time_t empty_time = time(NULL) + batt_info.seconds_remaining;
675 set_timezone(NULL); /* Use local time. */
676 struct tm *empty_tm = localtime(&empty_time);
679 outwalk += sprintf(outwalk, "%02d:%02d",
680 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
682 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
683 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
685 walk += strlen("emptytime");
686 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
688 } else if (BEGINS_WITH(walk + 1, "consumption")) {
689 if (batt_info.present_rate >= 0)
690 outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);
692 walk += strlen("consumption");
693 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
703 OUTPUT_FULL_TEXT(buffer);