6 #include <yajl/yajl_gen.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
11 #include <sys/types.h>
12 #include <sys/sysctl.h>
16 * Get battery information from /sys. Note that it uses the design capacity to
17 * calculate the percentage, not the last full capacity, so you can see how
18 * worn off your battery is.
21 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, bool last_full_capacity) {
26 char percentagebuf[16];
27 char remainingbuf[256];
28 char emptytimebuf[256];
29 const char *walk, *last;
30 char *outwalk = buffer;
34 charging_status_t status = CS_DISCHARGING;
36 memset(statusbuf, '\0', sizeof(statusbuf));
37 memset(percentagebuf, '\0', sizeof(percentagebuf));
38 memset(remainingbuf, '\0', sizeof(remainingbuf));
39 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
44 static char batpath[512];
45 sprintf(batpath, path, number);
46 if (!slurp(batpath, buf, sizeof(buf))) {
47 OUTPUT_FULL_TEXT("No battery");
51 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
60 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
61 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
62 remaining = atoi(walk+1);
63 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
64 present_rate = atoi(walk+1);
65 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
66 present_rate = atoi(walk+1);
67 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
69 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
72 /* The only thing left is the full capacity */
73 if (last_full_capacity) {
74 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
75 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
78 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
79 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
83 full_design = atoi(walk+1);
87 if ((full_design == 1) || (remaining == -1))
90 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
91 (status == CS_CHARGING ? "CHR" :
92 (status == CS_DISCHARGING ? "BAT" : "FULL")));
94 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
95 (((float)remaining / (float)full_design) * 100));
97 if (present_rate > 0) {
99 int seconds, hours, minutes, seconds_remaining;
100 if (status == CS_CHARGING)
101 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
102 else if (status == CS_DISCHARGING)
103 remaining_time = ((float)remaining / (float)present_rate);
104 else remaining_time = 0;
106 seconds_remaining = (int)(remaining_time * 3600.0);
108 hours = seconds_remaining / 3600;
109 seconds = seconds_remaining - (hours * 3600);
110 minutes = seconds / 60;
111 seconds -= (minutes * 60);
113 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
114 max(hours, 0), max(minutes, 0), max(seconds, 0));
116 empty_time = time(NULL);
117 empty_time += seconds_remaining;
118 empty_tm = localtime(&empty_time);
120 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
121 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
123 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
126 size_t sysctl_size = sizeof(sysctl_rslt);
128 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
129 printf("No battery");
133 present_rate = sysctl_rslt;
134 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
135 printf("No battery");
139 remaining = sysctl_rslt;
140 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
141 printf("No battery");
146 if (state == 0 && present_rate == 100)
148 else if (state == 0 && present_rate < 100)
149 status = CS_CHARGING;
151 status = CS_DISCHARGING;
153 full_design = sysctl_rslt;
155 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
156 (status == CS_CHARGING ? "CHR" :
157 (status == CS_DISCHARGING ? "BAT" : "FULL")));
159 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
165 hours = minutes / 60;
166 minutes -= (hours * 60);
167 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
168 max(hours, 0), max(minutes, 0));
172 for (walk = format; *walk != '\0'; walk++) {
174 *(outwalk++) = *walk;
178 if (strncmp(walk+1, "status", strlen("status")) == 0) {
179 outwalk += sprintf(outwalk, "%s", statusbuf);
180 walk += strlen("status");
181 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
182 outwalk += sprintf(outwalk, "%s", percentagebuf);
183 walk += strlen("percentage");
184 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
185 outwalk += sprintf(outwalk, "%s", remainingbuf);
186 walk += strlen("remaining");
187 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
188 outwalk += sprintf(outwalk, "%s", emptytimebuf);
189 walk += strlen("emptytime");
193 OUTPUT_FULL_TEXT(buffer);