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 #if defined(LINUX) || defined(__NetBSD__)
44 static int seconds_remaining_from_rate(charging_status_t status, float full_design, float remaining, float present_rate) {
45 if (status == CS_CHARGING)
46 return 3600.0 * (full_design - remaining) / present_rate;
47 else if (status == CS_DISCHARGING)
48 return 3600.0 * remaining / present_rate;
54 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, bool last_full_capacity) {
55 char *outwalk = buffer;
59 const char *walk, *last;
60 bool watt_as_unit = false;
65 sprintf(batpath, path, number);
68 if (!slurp(batpath, buf, sizeof(buf))) {
69 OUTPUT_FULL_TEXT(format_down);
73 for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
82 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW")) {
84 remaining = atoi(walk + 1);
85 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW")) {
87 remaining = atoi(walk + 1);
88 } else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
89 batt_info->present_rate = abs(atoi(walk + 1));
90 else if (BEGINS_WITH(last, "POWER_SUPPLY_VOLTAGE_NOW"))
91 voltage = abs(atoi(walk + 1));
92 /* on some systems POWER_SUPPLY_POWER_NOW does not exist, but actually
93 * it is the same as POWER_SUPPLY_CURRENT_NOW but with μWh as
94 * unit instead of μAh. We will calculate it as we need it
96 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
97 batt_info->present_rate = abs(atoi(walk + 1));
98 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
99 batt_info->status = CS_CHARGING;
100 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
101 batt_info->status = CS_FULL;
102 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Discharging"))
103 batt_info->status = CS_DISCHARGING;
104 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS="))
105 batt_info->status = CS_UNKNOWN;
107 /* The only thing left is the full capacity */
108 if (last_full_capacity) {
109 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
110 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
113 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
114 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
118 full_design = atoi(walk + 1);
122 /* the difference between POWER_SUPPLY_ENERGY_NOW and
123 * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is
124 * given in mWh, the charge in mAh. So calculate every value given in
127 batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
130 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
131 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
135 if ((full_design == -1) || (remaining == -1)) {
136 OUTPUT_FULL_TEXT(format_down);
140 batt_info->percentage_remaining = (((float)remaining / (float)full_design) * 100);
141 /* Some batteries report POWER_SUPPLY_CHARGE_NOW=<full_design> when fully
142 * charged, even though that’s plainly wrong. For people who chose to see
143 * the percentage calculated based on the last full capacity, we clamp the
144 * value to 100%, as that makes more sense.
145 * See http://bugs.debian.org/785398 */
146 if (last_full_capacity && batt_info->percentage_remaining > 100) {
147 batt_info->percentage_remaining = 100;
150 if (batt_info->present_rate > 0 && batt_info->status != CS_FULL) {
151 batt_info->seconds_remaining = seconds_remaining_from_rate(batt_info->status, full_design, remaining, batt_info->present_rate);
153 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
156 size_t sysctl_size = sizeof(sysctl_rslt);
158 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
159 OUTPUT_FULL_TEXT(format_down);
163 batt_info->percentage_remaining = sysctl_rslt;
164 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
165 OUTPUT_FULL_TEXT(format_down);
169 batt_info->seconds_remaining = sysctl_rslt * 60;
170 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
171 OUTPUT_FULL_TEXT(format_down);
176 if (state == 0 && batt_info->percentage_remaining == 100)
177 batt_info->status = CS_FULL;
178 else if ((state & ACPI_BATT_STAT_CHARGING) && batt_info->percentage_remaining < 100)
179 batt_info->status = CS_CHARGING;
181 batt_info->status = CS_DISCHARGING;
182 #elif defined(__OpenBSD__)
184 * We're using apm(4) here, which is the interface to acpi(4) on amd64/i386 and
185 * the generic interface on macppc/sparc64/zaurus, instead of using sysctl(3) and
186 * probing acpi(4) devices.
188 struct apm_power_info apm_info;
191 apm_fd = open("/dev/apm", O_RDONLY);
193 OUTPUT_FULL_TEXT("can't open /dev/apm");
196 if (ioctl(apm_fd, APM_IOC_GETPOWER, &apm_info) < 0)
197 OUTPUT_FULL_TEXT("can't read power info");
201 /* Don't bother to go further if there's no battery present. */
202 if ((apm_info.battery_state == APM_BATTERY_ABSENT) ||
203 (apm_info.battery_state == APM_BATT_UNKNOWN)) {
204 OUTPUT_FULL_TEXT(format_down);
208 switch (apm_info.ac_state) {
210 batt_info->status = CS_DISCHARGING;
213 batt_info->status = CS_CHARGING;
216 /* If we don't know what's going on, just assume we're discharging. */
217 batt_info->status = CS_DISCHARGING;
221 batt_info->percentage_remaining = apm_info.battery_life;
223 /* Can't give a meaningful value for remaining minutes if we're charging. */
224 if (batt_info->status != CS_CHARGING) {
225 batt_info->seconds_remaining = apm_info.minutes_left * 60;
227 #elif defined(__NetBSD__)
229 * Using envsys(4) via sysmon(4).
231 bool watt_as_unit = false;
232 int full_design = -1,
235 int fd, rval, last_full_cap;
236 bool is_found = false;
238 bool is_full = false;
240 prop_dictionary_t dict;
242 prop_object_iterator_t iter;
243 prop_object_iterator_t iter2;
244 prop_object_t obj, obj2, obj3, obj4, obj5;
246 asprintf(&sensor_desc, "acpibat%d", number);
248 fd = open("/dev/sysmon", O_RDONLY);
250 OUTPUT_FULL_TEXT("can't open /dev/sysmon");
254 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
260 if (prop_dictionary_count(dict) == 0) {
261 prop_object_release(dict);
266 iter = prop_dictionary_iterator(dict);
268 prop_object_release(dict);
272 /* iterate over the dictionary returned by the kernel */
273 while ((obj = prop_object_iterator_next(iter)) != NULL) {
274 /* skip this dict if it's not what we're looking for */
275 if (strcmp(sensor_desc,
276 prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
281 array = prop_dictionary_get_keysym(dict, obj);
282 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
283 prop_object_iterator_release(iter);
284 prop_object_release(dict);
289 iter2 = prop_array_iterator(array);
291 prop_object_iterator_release(iter);
292 prop_object_release(dict);
297 /* iterate over array of dicts specific to target battery */
298 while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
299 obj3 = prop_dictionary_get(obj2, "description");
304 if (strcmp("charging", prop_string_cstring_nocopy(obj3)) == 0) {
305 obj3 = prop_dictionary_get(obj2, "cur-value");
307 if (prop_number_integer_value(obj3))
308 batt_info->status = CS_CHARGING;
310 batt_info->status = CS_DISCHARGING;
311 } else if (strcmp("charge", prop_string_cstring_nocopy(obj3)) == 0) {
312 obj3 = prop_dictionary_get(obj2, "cur-value");
313 obj4 = prop_dictionary_get(obj2, "max-value");
314 obj5 = prop_dictionary_get(obj2, "type");
316 remaining = prop_number_integer_value(obj3);
317 full_design = prop_number_integer_value(obj4);
319 if (remaining == full_design)
322 if (strcmp("Ampere hour", prop_string_cstring_nocopy(obj5)) == 0)
323 watt_as_unit = false;
326 } else if (strcmp("discharge rate", prop_string_cstring_nocopy(obj3)) == 0) {
327 obj3 = prop_dictionary_get(obj2, "cur-value");
328 batt_info->present_rate = prop_number_integer_value(obj3);
329 } else if (strcmp("last full cap", prop_string_cstring_nocopy(obj3)) == 0) {
330 obj3 = prop_dictionary_get(obj2, "cur-value");
331 last_full_cap = prop_number_integer_value(obj3);
332 } else if (strcmp("voltage", prop_string_cstring_nocopy(obj3)) == 0) {
333 obj3 = prop_dictionary_get(obj2, "cur-value");
334 voltage = prop_number_integer_value(obj3);
337 prop_object_iterator_release(iter2);
340 prop_object_iterator_release(iter);
341 prop_object_release(dict);
345 OUTPUT_FULL_TEXT(format_down);
349 if (last_full_capacity)
350 full_design = last_full_cap;
353 batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0));
354 remaining = (((float)voltage / 1000.0) * ((float)remaining / 1000.0));
355 full_design = (((float)voltage / 1000.0) * ((float)full_design / 1000.0));
358 batt_info->percentage_remaining =
359 (((float)remaining / (float)full_design) * 100);
362 batt_info->status = CS_FULL;
365 * The envsys(4) ACPI routines do not appear to provide a 'time
366 * remaining' figure, so we must deduce it.
368 batt_info->seconds_remaining = seconds_remaining_from_rate(batt_info->status, full_design, remaining, batt_info->present_rate);
374 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) {
376 char *outwalk = buffer;
377 struct battery_info batt_info = {
379 .seconds_remaining = -1,
380 .percentage_remaining = -1,
381 .status = CS_DISCHARGING,
383 bool colorful_output = false;
385 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__OpenBSD__)
386 /* These OSes report battery stats in whole percent. */
387 integer_battery_capacity = true;
389 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
390 /* These OSes report battery time in minutes. */
394 if (!slurp_battery_info(&batt_info, json_gen, buffer, number, path, format_down, last_full_capacity))
397 if (batt_info.status == CS_DISCHARGING && low_threshold > 0) {
398 if (batt_info.percentage_remaining >= 0 && strcasecmp(threshold_type, "percentage") == 0 && batt_info.percentage_remaining < low_threshold) {
399 START_COLOR("color_bad");
400 colorful_output = true;
401 } else if (batt_info.seconds_remaining >= 0 && strcasecmp(threshold_type, "time") == 0 && batt_info.seconds_remaining < 60 * low_threshold) {
402 START_COLOR("color_bad");
403 colorful_output = true;
407 #define EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT() \
409 if (outwalk == prevoutwalk) { \
410 if (outwalk > buffer && isspace(outwalk[-1])) \
412 else if (isspace(*(walk + 1))) \
417 for (walk = format; *walk != '\0'; walk++) {
418 char *prevoutwalk = outwalk;
421 *(outwalk++) = *walk;
425 if (BEGINS_WITH(walk + 1, "status")) {
426 const char *statusstr;
427 switch (batt_info.status) {
429 statusstr = status_chr;
432 statusstr = status_bat;
435 statusstr = status_full;
438 statusstr = status_unk;
441 outwalk += sprintf(outwalk, "%s", statusstr);
442 walk += strlen("status");
443 } else if (BEGINS_WITH(walk + 1, "percentage")) {
444 if (integer_battery_capacity) {
445 outwalk += sprintf(outwalk, "%.00f%s", batt_info.percentage_remaining, pct_mark);
447 outwalk += sprintf(outwalk, "%.02f%s", batt_info.percentage_remaining, pct_mark);
449 walk += strlen("percentage");
450 } else if (BEGINS_WITH(walk + 1, "remaining")) {
451 if (batt_info.seconds_remaining >= 0) {
452 int seconds, hours, minutes;
454 hours = batt_info.seconds_remaining / 3600;
455 seconds = batt_info.seconds_remaining - (hours * 3600);
456 minutes = seconds / 60;
457 seconds -= (minutes * 60);
460 outwalk += sprintf(outwalk, "%02d:%02d",
461 max(hours, 0), max(minutes, 0));
463 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
464 max(hours, 0), max(minutes, 0), max(seconds, 0));
466 walk += strlen("remaining");
467 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
468 } else if (BEGINS_WITH(walk + 1, "emptytime")) {
469 if (batt_info.seconds_remaining >= 0) {
470 time_t empty_time = time(NULL) + batt_info.seconds_remaining;
471 struct tm *empty_tm = localtime(&empty_time);
474 outwalk += sprintf(outwalk, "%02d:%02d",
475 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0));
477 outwalk += sprintf(outwalk, "%02d:%02d:%02d",
478 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
480 walk += strlen("emptytime");
481 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
482 } else if (BEGINS_WITH(walk + 1, "consumption")) {
483 if (batt_info.present_rate >= 0)
484 outwalk += sprintf(outwalk, "%1.2fW", batt_info.present_rate / 1e6);
486 walk += strlen("consumption");
487 EAT_SPACE_FROM_OUTPUT_IF_NO_OUTPUT();
494 OUTPUT_FULL_TEXT(buffer);