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>
32 * Estimate the number of seconds remaining in state 'status'.
34 * Assumes a constant (dis)charge rate.
36 static int seconds_remaining_from_rate(charging_status_t status, float full_design, float remaining, float present_rate) {
37 if (status == CS_CHARGING)
38 return 3600.0 * (full_design - remaining) / present_rate;
39 else if (status == CS_DISCHARGING)
40 return 3600.0 * remaining / present_rate;
46 * Get battery information from /sys. Note that it uses the design capacity to
47 * calculate the percentage, not the last full capacity, so you can see how
48 * worn off your battery is.
51 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) {
53 const char *walk, *last;
54 char *outwalk = buffer;
55 bool watt_as_unit = false;
60 seconds_remaining = -1;
61 float percentage_remaining = -1;
62 charging_status_t status = CS_DISCHARGING;
64 static char batpath[512];
65 sprintf(batpath, path, number);
69 if (!slurp(batpath, buf, sizeof(buf))) {
70 OUTPUT_FULL_TEXT(format_down);
74 for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
83 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
85 remaining = atoi(walk + 1);
86 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
88 remaining = atoi(walk + 1);
89 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
90 present_rate = abs(atoi(walk + 1));
91 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
92 voltage = abs(atoi(walk + 1));
93 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
94 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
95 * unit instead of μAh. We will calculate it as we need it
97 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
98 present_rate = abs(atoi(walk + 1));
99 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
100 status = CS_CHARGING;
101 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
103 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
104 status = CS_DISCHARGING;
105 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
108 /* The only thing left is the full capacity */
109 if (last_full_capacity) {
110 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
111 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
114 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
115 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
119 full_design = atoi(walk + 1);
123 /* the difference between POWER_SUPPLY_ENERGY_NOW and
124 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
125 * given in mWh, the charge in mAh. So calculate every value given in
128 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
131 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
132 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
136 if ((full_design == -1) || (remaining == -1)) {
137 OUTPUT_FULL_TEXT(format_down);
141 percentage_remaining = (((float)remaining / (float)full_design) * 100);
142 /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
143 * charged, even though that’s plainly wrong. For people who chose to see
144 * the percentage calculated based on the last full capacity, we clamp the
145 * value to 100%, as that makes more sense.
146 * See http://bugs.debian.org/785398 */
147 if (last_full_capacity && percentage_remaining > 100) {
148 percentage_remaining = 100;
151 if (present_rate > 0 && status != CS_FULL) {
152 seconds_remaining = seconds_remaining_from_rate(status, full_design, remaining, present_rate);
154 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
157 size_t sysctl_size = sizeof(sysctl_rslt);
159 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
160 OUTPUT_FULL_TEXT(format_down);
164 integer_battery_capacity = true;
165 percentage_remaining = sysctl_rslt;
166 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
167 OUTPUT_FULL_TEXT(format_down);
172 seconds_remaining = sysctl_rslt * 60;
173 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
174 OUTPUT_FULL_TEXT(format_down);
179 if (state == 0 && percentage_remaining == 100)
181 else if ((state & ACPI_BATT_STAT_CHARGING) && percentage_remaining < 100)
182 status = CS_CHARGING;
184 status = CS_DISCHARGING;
186 full_design = sysctl_rslt;
187 #elif defined(__OpenBSD__)
189 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
190 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
191 * probing acpi(4) devices.
193 struct apm_power_info apm_info;
196 apm_fd = open("/dev/apm", O_RDONLY);
198 OUTPUT_FULL_TEXT("can't open /dev/apm");
201 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
202 OUTPUT_FULL_TEXT("can't read power info");
206 /* Don't bother to go further if there's no battery present. */
207 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
208 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
209 OUTPUT_FULL_TEXT(format_down);
213 switch (apm_info.ac_state) {
215 status = CS_DISCHARGING;
218 status = CS_CHARGING;
221 /* If we don't know what's going on, just assume we're discharging. */
222 status = CS_DISCHARGING;
226 integer_battery_capacity = true;
227 percentage_remaining = apm_info.battery_life;
229 /* Can't give a meaningful value for remaining minutes if we're charging. */
230 if (status != CS_CHARGING) {
231 seconds_remaining = apm_info.minutes_left * 60;
233 #elif defined(__NetBSD__)
235 * Using envsys(4) via sysmon(4).
237 int fd, rval, last_full_cap;
238 bool is_found = false;
240 bool is_full = false;
242 prop_dictionary_t dict;
244 prop_object_iterator_t iter;
245 prop_object_iterator_t iter2;
246 prop_object_t obj, obj2, obj3, obj4, obj5;
248 asprintf(&sensor_desc, "acpibat%d", number);
250 fd = open("/dev/sysmon", O_RDONLY);
252 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
256 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
262 if (prop_dictionary_count(dict) == 0) {
263 prop_object_release(dict);
268 iter = prop_dictionary_iterator(dict);
270 prop_object_release(dict);
274 /* iterate over the dictionary returned by the kernel */
275 while ((obj = prop_object_iterator_next(iter)) != NULL) {
276 /* skip this dict if it's not what we're looking for */
277 if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) == strlen(sensor_desc)) &&
278 (strncmp(sensor_desc,
279 prop_dictionary_keysym_cstring_nocopy(obj),
280 strlen(sensor_desc)) != 0))
285 array = prop_dictionary_get_keysym(dict, obj);
286 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
287 prop_object_iterator_release(iter);
288 prop_object_release(dict);
293 iter2 = prop_array_iterator(array);
295 prop_object_iterator_release(iter);
296 prop_object_release(dict);
301 /* iterate over array of dicts specific to target battery */
302 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
303 obj3 = prop_dictionary_get(obj2, "description");
306 strlen(prop_string_cstring_nocopy(obj3)) == 8 &&
308 prop_string_cstring_nocopy(obj3),
310 obj3 = prop_dictionary_get(obj2, "cur-value");
312 if (prop_number_integer_value(obj3))
313 status = CS_CHARGING;
315 status = CS_DISCHARGING;
321 strlen(prop_string_cstring_nocopy(obj3)) == 6 &&
323 prop_string_cstring_nocopy(obj3),
325 obj3 = prop_dictionary_get(obj2, "cur-value");
326 obj4 = prop_dictionary_get(obj2, "max-value");
327 obj5 = prop_dictionary_get(obj2, "type");
329 remaining = prop_number_integer_value(obj3);
330 full_design = prop_number_integer_value(obj4);
332 if (remaining == full_design)
335 if (strncmp("Ampere hour",
336 prop_string_cstring_nocopy(obj5),
338 watt_as_unit = false;
346 strlen(prop_string_cstring_nocopy(obj3)) == 14 &&
347 strncmp("discharge rate",
348 prop_string_cstring_nocopy(obj3),
350 obj3 = prop_dictionary_get(obj2, "cur-value");
351 present_rate = prop_number_integer_value(obj3);
356 strlen(prop_string_cstring_nocopy(obj3)) == 13 &&
357 strncmp("last full cap",
358 prop_string_cstring_nocopy(obj3),
360 obj3 = prop_dictionary_get(obj2, "cur-value");
361 last_full_cap = prop_number_integer_value(obj3);
366 strlen(prop_string_cstring_nocopy(obj3)) == 7 &&
368 prop_string_cstring_nocopy(obj3),
370 obj3 = prop_dictionary_get(obj2, "cur-value");
371 voltage = prop_number_integer_value(obj3);
375 prop_object_iterator_release(iter2);
378 prop_object_iterator_release(iter);
379 prop_object_release(dict);
383 OUTPUT_FULL_TEXT(format_down);
387 if (last_full_capacity)
388 full_design = last_full_cap;
391 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
392 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
393 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
396 percentage_remaining =
397 (((float)remaining / (float)full_design) * 100);
403 * The envsys(4) ACPI routines do not appear to provide a 'time
404 * remaining' figure, so we must deduce it.
406 seconds_remaining = seconds_remaining_from_rate(status, full_design, remaining, present_rate);
409 bool colorful_output = false;
411 if (status == CS_DISCHARGING && low_threshold > 0) {
412 if (percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
413 START_COLOR("color_bad");
414 colorful_output = true;
415 } else if (seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
416 START_COLOR("color_bad");
417 colorful_output = true;
421 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
423 if (outwalk == prevoutwalk) { \
424 if (outwalk > buffer && isspace(outwalk[-1])) \
426 else if (isspace(*(walk + 1))) \
431 for (walk = format; *walk != '\0'; walk++) {
432 char *prevoutwalk = outwalk;
435 *(outwalk++) = *walk;
439 if (BEGINS_WITH(walk + 1, "status")) {
440 const char *statusstr;
443 statusstr = status_chr;
446 statusstr = status_bat;
449 statusstr = status_full;
452 statusstr = status_unk;
455 outwalk += sprintf(outwalk, "%s", statusstr);
456 walk += strlen("status");
457 } else if (BEGINS_WITH(walk + 1, "percentage")) {
458 if (integer_battery_capacity) {
459 outwalk += sprintf(outwalk, "%.00f%s", percentage_remaining, pct_mark);
461 outwalk += sprintf(outwalk, "%.02f%s", percentage_remaining, pct_mark);
463 walk += strlen("percentage");
464 } else if (BEGINS_WITH(walk + 1, "remaining")) {
465 if (seconds_remaining >= 0) {
466 int seconds, hours, minutes;
468 hours = seconds_remaining / 3600;
469 seconds = seconds_remaining - (hours * 3600);
470 minutes = seconds / 60;
471 seconds -= (minutes * 60);
474 outwalk += sprintf(outwalk, "%02d:%02d",
475 max(hours, 0), max(minutes, 0));
477 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
478 max(hours, 0), max(minutes, 0), max(seconds, 0));
480 walk += strlen("remaining");
481 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
482 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
483 if (seconds_remaining >= 0) {
484 time_t empty_time = time(NULL) + seconds_remaining;
485 struct tm *empty_tm = localtime(&empty_time);
488 outwalk += sprintf(outwalk, "%02d:%02d",
489 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
491 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
492 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
494 walk += strlen("emptytime");
495 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
496 } else if (BEGINS_WITH(walk + 1, "consumption")) {
497 if (present_rate >= 0)
498 outwalk += sprintf(outwalk, "%1.2fW", present_rate / 1e6);
500 walk += strlen("consumption");
501 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
508 OUTPUT_FULL_TEXT(buffer);