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 * Get battery information from /sys. Note that it uses the design capacity to
33 * calculate the percentage, not the last full capacity, so you can see how
34 * worn off your battery is.
37 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) {
39 char consumptionbuf[256];
40 const char *walk, *last;
41 char *outwalk = buffer;
42 bool watt_as_unit = false;
43 bool colorful_output = false;
48 seconds_remaining = -1;
49 float percentage_remaining = -1;
50 charging_status_t status = CS_DISCHARGING;
52 memset(consumptionbuf, '\0', sizeof(consumptionbuf));
54 static char batpath[512];
55 sprintf(batpath, path, number);
59 if (!slurp(batpath, buf, sizeof(buf))) {
60 OUTPUT_FULL_TEXT(format_down);
64 for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
73 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
75 remaining = atoi(walk + 1);
76 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
78 remaining = atoi(walk + 1);
79 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
80 present_rate = abs(atoi(walk + 1));
81 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
82 voltage = abs(atoi(walk + 1));
83 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
84 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
85 * unit instead of μAh. We will calculate it as we need it
87 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
88 present_rate = abs(atoi(walk + 1));
89 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
91 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
93 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
94 status = CS_DISCHARGING;
95 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
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));
121 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
122 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
126 if ((full_design == -1) || (remaining == -1)) {
127 OUTPUT_FULL_TEXT(format_down);
131 percentage_remaining = (((float)remaining / (float)full_design) * 100);
132 /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
133 * charged, even though that’s plainly wrong. For people who chose to see
134 * the percentage calculated based on the last full capacity, we clamp the
135 * value to 100%, as that makes more sense.
136 * See http://bugs.debian.org/785398 */
137 if (last_full_capacity && percentage_remaining > 100) {
138 percentage_remaining = 100;
141 if (present_rate > 0 && status != CS_FULL) {
142 float remaining_time;
143 if (status == CS_CHARGING)
144 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
145 else if (status == CS_DISCHARGING)
146 remaining_time = ((float)remaining / (float)present_rate);
150 seconds_remaining = (int)(remaining_time * 3600.0);
152 if (status == CS_DISCHARGING && low_threshold > 0) {
153 if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
154 START_COLOR("color_bad");
155 colorful_output = true;
156 } else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
157 START_COLOR("color_bad");
158 colorful_output = true;
160 colorful_output = false;
164 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
165 ((float)present_rate / 1000.0 / 1000.0));
167 /* On some systems, present_rate may not exist. Still, make sure
168 * we colorize the output if threshold_type is set to percentage
169 * (since we don't have any information on remaining time). */
170 if (status == CS_DISCHARGING && low_threshold > 0) {
171 if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
172 START_COLOR("color_bad");
173 colorful_output = true;
177 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
180 size_t sysctl_size = sizeof(sysctl_rslt);
182 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
183 OUTPUT_FULL_TEXT(format_down);
187 integer_battery_capacity = true;
188 percentage_remaining = sysctl_rslt;
189 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
190 OUTPUT_FULL_TEXT(format_down);
195 seconds_remaining = sysctl_rslt * 60;
196 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
197 OUTPUT_FULL_TEXT(format_down);
202 if (state == 0 && percentage_remaining == 100)
204 else if ((state & ACPI_BATT_STAT_CHARGING) && percentage_remaining < 100)
205 status = CS_CHARGING;
207 status = CS_DISCHARGING;
209 full_design = sysctl_rslt;
211 if (state == ACPI_BATT_STAT_DISCHARG) {
212 if (strcasecmp(threshold_type, "percentage") == 0 && percentage_remaining < low_threshold) {
213 START_COLOR("color_bad");
214 colorful_output = true;
215 } else if (strcasecmp(threshold_type, "time") == 0 && seconds_remaining < 60 * low_threshold) {
216 START_COLOR("color_bad");
217 colorful_output = true;
220 #elif defined(__OpenBSD__)
222 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
223 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
224 * probing acpi(4) devices.
226 struct apm_power_info apm_info;
229 apm_fd = open("/dev/apm", O_RDONLY);
231 OUTPUT_FULL_TEXT("can't open /dev/apm");
234 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
235 OUTPUT_FULL_TEXT("can't read power info");
239 /* Don't bother to go further if there's no battery present. */
240 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
241 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
242 OUTPUT_FULL_TEXT(format_down);
246 switch (apm_info.ac_state) {
248 status = CS_DISCHARGING;
251 status = CS_CHARGING;
254 /* If we don't know what's going on, just assume we're discharging. */
255 status = CS_DISCHARGING;
259 integer_battery_capacity = true;
260 percentage_remaining = apm_info.battery_life;
262 if (status == CS_DISCHARGING && low_threshold > 0) {
263 if (strcasecmp(threshold_type, "percentage") == 0 && apm_info.battery_life < low_threshold) {
264 START_COLOR("color_bad");
265 colorful_output = true;
266 } else if (strcasecmp(threshold_type, "time") == 0 && apm_info.minutes_left < (u_int)low_threshold) {
267 START_COLOR("color_bad");
268 colorful_output = true;
272 /* Can't give a meaningful value for remaining minutes if we're charging. */
273 if (status != CS_CHARGING) {
274 seconds_remaining = apm_info.minutes_left * 60;
279 #elif defined(__NetBSD__)
281 * Using envsys(4) via sysmon(4).
283 int fd, rval, last_full_cap;
284 bool is_found = false;
286 bool is_full = false;
288 prop_dictionary_t dict;
290 prop_object_iterator_t iter;
291 prop_object_iterator_t iter2;
292 prop_object_t obj, obj2, obj3, obj4, obj5;
294 asprintf(&sensor_desc, "acpibat%d", number);
296 fd = open("/dev/sysmon", O_RDONLY);
298 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
302 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
308 if (prop_dictionary_count(dict) == 0) {
309 prop_object_release(dict);
314 iter = prop_dictionary_iterator(dict);
316 prop_object_release(dict);
320 /* iterate over the dictionary returned by the kernel */
321 while ((obj = prop_object_iterator_next(iter)) != NULL) {
322 /* skip this dict if it's not what we're looking for */
323 if ((strlen(prop_dictionary_keysym_cstring_nocopy(obj)) == strlen(sensor_desc)) &&
324 (strncmp(sensor_desc,
325 prop_dictionary_keysym_cstring_nocopy(obj),
326 strlen(sensor_desc)) != 0))
331 array = prop_dictionary_get_keysym(dict, obj);
332 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
333 prop_object_iterator_release(iter);
334 prop_object_release(dict);
339 iter2 = prop_array_iterator(array);
341 prop_object_iterator_release(iter);
342 prop_object_release(dict);
347 /* iterate over array of dicts specific to target battery */
348 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
349 obj3 = prop_dictionary_get(obj2, "description");
352 strlen(prop_string_cstring_nocopy(obj3)) == 8 &&
354 prop_string_cstring_nocopy(obj3),
356 obj3 = prop_dictionary_get(obj2, "cur-value");
358 if (prop_number_integer_value(obj3))
359 status = CS_CHARGING;
361 status = CS_DISCHARGING;
367 strlen(prop_string_cstring_nocopy(obj3)) == 6 &&
369 prop_string_cstring_nocopy(obj3),
371 obj3 = prop_dictionary_get(obj2, "cur-value");
372 obj4 = prop_dictionary_get(obj2, "max-value");
373 obj5 = prop_dictionary_get(obj2, "type");
375 remaining = prop_number_integer_value(obj3);
376 full_design = prop_number_integer_value(obj4);
378 if (remaining == full_design)
381 if (strncmp("Ampere hour",
382 prop_string_cstring_nocopy(obj5),
384 watt_as_unit = false;
392 strlen(prop_string_cstring_nocopy(obj3)) == 14 &&
393 strncmp("discharge rate",
394 prop_string_cstring_nocopy(obj3),
396 obj3 = prop_dictionary_get(obj2, "cur-value");
397 present_rate = prop_number_integer_value(obj3);
402 strlen(prop_string_cstring_nocopy(obj3)) == 13 &&
403 strncmp("last full cap",
404 prop_string_cstring_nocopy(obj3),
406 obj3 = prop_dictionary_get(obj2, "cur-value");
407 last_full_cap = prop_number_integer_value(obj3);
412 strlen(prop_string_cstring_nocopy(obj3)) == 7 &&
414 prop_string_cstring_nocopy(obj3),
416 obj3 = prop_dictionary_get(obj2, "cur-value");
417 voltage = prop_number_integer_value(obj3);
421 prop_object_iterator_release(iter2);
424 prop_object_iterator_release(iter);
425 prop_object_release(dict);
429 OUTPUT_FULL_TEXT(format_down);
433 if (last_full_capacity)
434 full_design = last_full_cap;
437 present_rate = (((float)voltage / 1000.0) * ((float)present_rate / 1000.0));
438 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
439 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
442 percentage_remaining =
443 (((float)remaining / (float)full_design) * 100);
446 * Handle percentage low_threshold here, and time low_threshold when
449 if (status == CS_DISCHARGING && low_threshold > 0) {
450 if (strcasecmp(threshold_type, "percentage") == 0 && (((float)remaining / (float)full_design) * 100) < low_threshold) {
451 START_COLOR("color_bad");
452 colorful_output = true;
460 * The envsys(4) ACPI routines do not appear to provide a 'time
461 * remaining' figure, so we must deduce it.
463 float remaining_time;
465 if (status == CS_CHARGING)
466 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
467 else if (status == CS_DISCHARGING)
468 remaining_time = ((float)remaining / (float)present_rate);
472 seconds_remaining = (int)(remaining_time * 3600.0);
474 if (status != CS_CHARGING) {
475 if (low_threshold > 0) {
476 if (strcasecmp(threshold_type, "time") == 0 && ((float)seconds_remaining / 60.0) < (u_int)low_threshold) {
477 START_COLOR("color_bad");
478 colorful_output = true;
483 (void)snprintf(consumptionbuf, sizeof(consumptionbuf), "%1.2fW",
484 ((float)present_rate / 1000.0 / 1000.0));
487 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
489 if (outwalk == prevoutwalk) { \
490 if (outwalk > buffer && isspace(outwalk[-1])) \
492 else if (isspace(*(walk + 1))) \
497 for (walk = format; *walk != '\0'; walk++) {
498 char *prevoutwalk = outwalk;
501 *(outwalk++) = *walk;
505 if (BEGINS_WITH(walk + 1, "status")) {
506 const char *statusstr;
509 statusstr = status_chr;
512 statusstr = status_bat;
515 statusstr = status_full;
518 statusstr = status_unk;
521 outwalk += sprintf(outwalk, "%s", statusstr);
522 walk += strlen("status");
523 } else if (BEGINS_WITH(walk + 1, "percentage")) {
524 if (integer_battery_capacity) {
525 outwalk += sprintf(outwalk, "%.00f%s", percentage_remaining, pct_mark);
527 outwalk += sprintf(outwalk, "%.02f%s", percentage_remaining, pct_mark);
529 walk += strlen("percentage");
530 } else if (BEGINS_WITH(walk + 1, "remaining")) {
531 if (seconds_remaining >= 0) {
532 int seconds, hours, minutes;
534 hours = seconds_remaining / 3600;
535 seconds = seconds_remaining - (hours * 3600);
536 minutes = seconds / 60;
537 seconds -= (minutes * 60);
540 outwalk += sprintf(outwalk, "%02d:%02d",
541 max(hours, 0), max(minutes, 0));
543 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
544 max(hours, 0), max(minutes, 0), max(seconds, 0));
546 walk += strlen("remaining");
547 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
548 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
549 if (seconds_remaining >= 0) {
550 time_t empty_time = time(NULL) + seconds_remaining;
551 struct tm *empty_tm = localtime(&empty_time);
554 outwalk += sprintf(outwalk, "%02d:%02d",
555 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
557 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
558 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
560 walk += strlen("emptytime");
561 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
562 } else if (BEGINS_WITH(walk + 1, "consumption")) {
563 outwalk += sprintf(outwalk, "%s", consumptionbuf);
564 walk += strlen("consumption");
565 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
572 OUTPUT_FULL_TEXT(buffer);