1 // vim:ts=4:sw=4:expandtab
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
12 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
15 #include <dev/acpica/acpiio.h>
18 #if defined(__OpenBSD__)
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/fcntl.h>
22 #include <machine/apmvar.h>
25 #if defined(__NetBSD__)
27 #include <prop/proplib.h>
28 #include <sys/envsys.h>
33 int seconds_remaining;
34 float percentage_remaining;
35 charging_status_t status;
39 * Estimate the number of seconds remaining in state 'status'.
41 * Assumes a constant (dis)charge rate.
43 static int seconds_remaining_from_rate(charging_status_t status, float full_design, float remaining, float present_rate) {
44 if (status == CS_CHARGING)
45 return 3600.0 * (full_design - remaining) / present_rate;
46 else if (status == CS_DISCHARGING)
47 return 3600.0 * remaining / present_rate;
53 * Get battery information from /sys. Note that it uses the design capacity to
54 * calculate the percentage, not the last full capacity, so you can see how
55 * worn off your battery is.
58 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) {
60 const char *walk, *last;
61 char *outwalk = buffer;
62 bool watt_as_unit = false;
66 struct battery_info batt_info = {
68 .seconds_remaining = -1,
69 .percentage_remaining = -1,
70 .status = CS_DISCHARGING,
73 static char batpath[512];
74 sprintf(batpath, path, number);
78 if (!slurp(batpath, buf, sizeof(buf))) {
79 OUTPUT_FULL_TEXT(format_down);
83 for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
92 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
94 remaining = atoi(walk + 1);
95 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
97 remaining = atoi(walk + 1);
98 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
99 batt_info.present_rate = abs(atoi(walk + 1));
100 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
101 voltage = abs(atoi(walk + 1));
102 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
103 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
104 * unit instead of μAh. We will calculate it as we need it
106 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
107 batt_info.present_rate = abs(atoi(walk + 1));
108 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
109 batt_info.status = CS_CHARGING;
110 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
111 batt_info.status = CS_FULL;
112 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
113 batt_info.status = CS_DISCHARGING;
114 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
115 batt_info.status = CS_UNKNOWN;
117 /* The only thing left is the full capacity */
118 if (last_full_capacity) {
119 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
120 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
123 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
124 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
128 full_design = atoi(walk + 1);
132 /* the difference between POWER_SUPPLY_ENERGY_NOW and
133 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
134 * given in mWh, the charge in mAh. So calculate every value given in
137 batt_info.present_rate = (((float)voltage / 1000.0) * ((float)batt_info.present_rate / 1000.0));
140 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
141 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
145 if ((full_design == -1) || (remaining == -1)) {
146 OUTPUT_FULL_TEXT(format_down);
150 batt_info.percentage_remaining = (((float)remaining / (float)full_design) * 100);
151 /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
152 * charged, even though that’s plainly wrong. For people who chose to see
153 * the percentage calculated based on the last full capacity, we clamp the
154 * value to 100%, as that makes more sense.
155 * See http://bugs.debian.org/785398 */
156 if (last_full_capacity && batt_info.percentage_remaining > 100) {
157 batt_info.percentage_remaining = 100;
160 if (batt_info.present_rate > 0 && batt_info.status != CS_FULL) {
161 batt_info.seconds_remaining = seconds_remaining_from_rate(batt_info.status, full_design, remaining, batt_info.present_rate);
163 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
166 size_t sysctl_size = sizeof(sysctl_rslt);
168 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
169 OUTPUT_FULL_TEXT(format_down);
173 integer_battery_capacity = true;
174 batt_info.percentage_remaining = sysctl_rslt;
175 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
176 OUTPUT_FULL_TEXT(format_down);
181 batt_info.seconds_remaining = sysctl_rslt * 60;
182 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
183 OUTPUT_FULL_TEXT(format_down);
188 if (state == 0 && batt_info.percentage_remaining == 100)
189 batt_info.status = CS_FULL;
190 else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info.percentage_remaining < 100)
191 batt_info.status = CS_CHARGING;
193 batt_info.status = CS_DISCHARGING;
195 full_design = sysctl_rslt;
196 #elif defined(__OpenBSD__)
198 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
199 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
200 * probing acpi(4) devices.
202 struct apm_power_info apm_info;
205 apm_fd = open("/dev/apm", O_RDONLY);
207 OUTPUT_FULL_TEXT("can't open /dev/apm");
210 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
211 OUTPUT_FULL_TEXT("can't read power info");
215 /* Don't bother to go further if there's no battery present. */
216 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
217 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
218 OUTPUT_FULL_TEXT(format_down);
222 switch (apm_info.ac_state) {
224 batt_info.status = CS_DISCHARGING;
227 batt_info.status = CS_CHARGING;
230 /* If we don't know what's going on, just assume we're discharging. */
231 batt_info.status = CS_DISCHARGING;
235 integer_battery_capacity = true;
236 batt_info.percentage_remaining = apm_info.battery_life;
238 /* Can't give a meaningful value for remaining minutes if we're charging. */
239 if (batt_info.status != CS_CHARGING) {
240 batt_info.seconds_remaining = apm_info.minutes_left * 60;
242 #elif defined(__NetBSD__)
244 * Using envsys(4) via sysmon(4).
246 int fd, rval, last_full_cap;
247 bool is_found = false;
249 bool is_full = false;
251 prop_dictionary_t dict;
253 prop_object_iterator_t iter;
254 prop_object_iterator_t iter2;
255 prop_object_t obj, obj2, obj3, obj4, obj5;
257 asprintf(&sensor_desc, "acpibat%d", number);
259 fd = open("/dev/sysmon", O_RDONLY);
261 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
265 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
271 if (prop_dictionary_count(dict) == 0) {
272 prop_object_release(dict);
277 iter = prop_dictionary_iterator(dict);
279 prop_object_release(dict);
283 /* iterate over the dictionary returned by the kernel */
284 while ((obj = prop_object_iterator_next(iter)) != NULL) {
285 /* skip this dict if it's not what we're looking for */
286 if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) == strlen(sensor_desc)) &&
287 (strncmp(sensor_desc,
288 prop_dictionary_keysym_cstring_nocopy(obj),
289 strlen(sensor_desc)) != 0))
294 array = prop_dictionary_get_keysym(dict, obj);
295 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
296 prop_object_iterator_release(iter);
297 prop_object_release(dict);
302 iter2 = prop_array_iterator(array);
304 prop_object_iterator_release(iter);
305 prop_object_release(dict);
310 /* iterate over array of dicts specific to target battery */
311 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
312 obj3 = prop_dictionary_get(obj2, "description");
315 strlen(prop_string_cstring_nocopy(obj3)) == 8 &&
317 prop_string_cstring_nocopy(obj3),
319 obj3 = prop_dictionary_get(obj2, "cur-value");
321 if (prop_number_integer_value(obj3))
322 batt_info.status = CS_CHARGING;
324 batt_info.status = CS_DISCHARGING;
330 strlen(prop_string_cstring_nocopy(obj3)) == 6 &&
332 prop_string_cstring_nocopy(obj3),
334 obj3 = prop_dictionary_get(obj2, "cur-value");
335 obj4 = prop_dictionary_get(obj2, "max-value");
336 obj5 = prop_dictionary_get(obj2, "type");
338 remaining = prop_number_integer_value(obj3);
339 full_design = prop_number_integer_value(obj4);
341 if (remaining == full_design)
344 if (strncmp("Ampere hour",
345 prop_string_cstring_nocopy(obj5),
347 watt_as_unit = false;
355 strlen(prop_string_cstring_nocopy(obj3)) == 14 &&
356 strncmp("discharge rate",
357 prop_string_cstring_nocopy(obj3),
359 obj3 = prop_dictionary_get(obj2, "cur-value");
360 batt_info.present_rate = prop_number_integer_value(obj3);
365 strlen(prop_string_cstring_nocopy(obj3)) == 13 &&
366 strncmp("last full cap",
367 prop_string_cstring_nocopy(obj3),
369 obj3 = prop_dictionary_get(obj2, "cur-value");
370 last_full_cap = prop_number_integer_value(obj3);
375 strlen(prop_string_cstring_nocopy(obj3)) == 7 &&
377 prop_string_cstring_nocopy(obj3),
379 obj3 = prop_dictionary_get(obj2, "cur-value");
380 voltage = prop_number_integer_value(obj3);
384 prop_object_iterator_release(iter2);
387 prop_object_iterator_release(iter);
388 prop_object_release(dict);
392 OUTPUT_FULL_TEXT(format_down);
396 if (last_full_capacity)
397 full_design = last_full_cap;
400 batt_info.present_rate = (((float)voltage / 1000.0) * ((float)batt_info.present_rate / 1000.0));
401 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
402 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
405 batt_info.percentage_remaining =
406 (((float)remaining / (float)full_design) * 100);
409 batt_info.status = CS_FULL;
412 * The envsys(4) ACPI routines do not appear to provide a 'time
413 * remaining' figure, so we must deduce it.
415 batt_info.seconds_remaining = seconds_remaining_from_rate(batt_info.status, full_design, remaining, batt_info.present_rate);
418 bool colorful_output = false;
420 if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
421 if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
422 START_COLOR("color_bad");
423 colorful_output = true;
424 } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
425 START_COLOR("color_bad");
426 colorful_output = true;
430 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
432 if (outwalk == prevoutwalk) { \
433 if (outwalk > buffer && isspace(outwalk[-1])) \
435 else if (isspace(*(walk + 1))) \
440 for (walk = format; *walk != '\0'; walk++) {
441 char *prevoutwalk = outwalk;
444 *(outwalk++) = *walk;
448 if (BEGINS_WITH(walk + 1, "status")) {
449 const char *statusstr;
450 switch (batt_info.status) {
452 statusstr = status_chr;
455 statusstr = status_bat;
458 statusstr = status_full;
461 statusstr = status_unk;
464 outwalk += sprintf(outwalk, "%s", statusstr);
465 walk += strlen("status");
466 } else if (BEGINS_WITH(walk + 1, "percentage")) {
467 if (integer_battery_capacity) {
468 outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
470 outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
472 walk += strlen("percentage");
473 } else if (BEGINS_WITH(walk + 1, "remaining")) {
474 if (batt_info.seconds_remaining >= 0) {
475 int seconds, hours, minutes;
477 hours = batt_info.seconds_remaining / 3600;
478 seconds = batt_info.seconds_remaining - (hours * 3600);
479 minutes = seconds / 60;
480 seconds -= (minutes * 60);
483 outwalk += sprintf(outwalk, "%02d:%02d",
484 max(hours, 0), max(minutes, 0));
486 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
487 max(hours, 0), max(minutes, 0), max(seconds, 0));
489 walk += strlen("remaining");
490 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
491 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
492 if (batt_info.seconds_remaining >= 0) {
493 time_t empty_time = time(NULL) + batt_info.seconds_remaining;
494 struct tm *empty_tm = localtime(&empty_time);
497 outwalk += sprintf(outwalk, "%02d:%02d",
498 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
500 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
501 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
503 walk += strlen("emptytime");
504 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
505 } else if (BEGINS_WITH(walk + 1, "consumption")) {
506 if (batt_info.present_rate >= 0)
507 outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);
509 walk += strlen("consumption");
510 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
517 OUTPUT_FULL_TEXT(buffer);