6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
17 * Get battery information from /sys. Note that it uses the design capacity to
18 * calculate the percentage, not the last full capacity, so you can see how
19 * worn off your battery is.
22 void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, bool last_full_capacity) {
27 char percentagebuf[16];
28 char remainingbuf[256];
29 char emptytimebuf[256];
30 const char *walk, *last;
31 char *outwalk = buffer;
35 charging_status_t status = CS_DISCHARGING;
37 memset(statusbuf, '\0', sizeof(statusbuf));
38 memset(percentagebuf, '\0', sizeof(percentagebuf));
39 memset(remainingbuf, '\0', sizeof(remainingbuf));
40 memset(emptytimebuf, '\0', sizeof(emptytimebuf));
45 static char batpath[512];
46 sprintf(batpath, path, number);
47 if (!slurp(batpath, buf, sizeof(buf))) {
48 OUTPUT_FULL_TEXT("No battery");
52 for (walk = buf, last = buf; (walk-buf) < 1024; walk++) {
61 if (BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_NOW") ||
62 BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_NOW"))
63 remaining = atoi(walk+1);
64 else if (BEGINS_WITH(last, "POWER_SUPPLY_CURRENT_NOW"))
65 present_rate = atoi(walk+1);
66 else if (BEGINS_WITH(last, "POWER_SUPPLY_POWER_NOW"))
67 present_rate = atoi(walk+1);
68 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Charging"))
70 else if (BEGINS_WITH(last, "POWER_SUPPLY_STATUS=Full"))
73 /* The only thing left is the full capacity */
74 if (last_full_capacity) {
75 if (!BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL") &&
76 !BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL"))
79 if (!BEGINS_WITH(last, "POWER_SUPPLY_CHARGE_FULL_DESIGN") &&
80 !BEGINS_WITH(last, "POWER_SUPPLY_ENERGY_FULL_DESIGN"))
84 full_design = atoi(walk+1);
88 if ((full_design == 1) || (remaining == -1))
91 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
92 (status == CS_CHARGING ? "CHR" :
93 (status == CS_DISCHARGING ? "BAT" : "FULL")));
95 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%.02f%%",
96 (((float)remaining / (float)full_design) * 100));
98 if (present_rate > 0) {
100 int seconds, hours, minutes, seconds_remaining;
101 if (status == CS_CHARGING)
102 remaining_time = ((float)full_design - (float)remaining) / (float)present_rate;
103 else if (status == CS_DISCHARGING)
104 remaining_time = ((float)remaining / (float)present_rate);
105 else remaining_time = 0;
107 seconds_remaining = (int)(remaining_time * 3600.0);
109 hours = seconds_remaining / 3600;
110 seconds = seconds_remaining - (hours * 3600);
111 minutes = seconds / 60;
112 seconds -= (minutes * 60);
114 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02d:%02d:%02d",
115 max(hours, 0), max(minutes, 0), max(seconds, 0));
117 empty_time = time(NULL);
118 empty_time += seconds_remaining;
119 empty_tm = localtime(&empty_time);
121 (void)snprintf(emptytimebuf, sizeof(emptytimebuf), "%02d:%02d:%02d",
122 max(empty_tm->tm_hour, 0), max(empty_tm->tm_min, 0), max(empty_tm->tm_sec, 0));
124 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
127 size_t sysctl_size = sizeof(sysctl_rslt);
129 if (sysctlbyname(BATT_LIFE, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
130 printf("No battery");
134 present_rate = sysctl_rslt;
135 if (sysctlbyname(BATT_TIME, &sysctl_rslt, &sysctl_size, NULL, 0) != 0) {
136 printf("No battery");
140 remaining = sysctl_rslt;
141 if (sysctlbyname(BATT_STATE, &sysctl_rslt, &sysctl_size, NULL,0) != 0) {
142 printf("No battery");
147 if (state == 0 && present_rate == 100)
149 else if (state == 0 && present_rate < 100)
150 status = CS_CHARGING;
152 status = CS_DISCHARGING;
154 full_design = sysctl_rslt;
156 (void)snprintf(statusbuf, sizeof(statusbuf), "%s",
157 (status == CS_CHARGING ? "CHR" :
158 (status == CS_DISCHARGING ? "BAT" : "FULL")));
160 (void)snprintf(percentagebuf, sizeof(percentagebuf), "%02d%%",
166 hours = minutes / 60;
167 minutes -= (hours * 60);
168 (void)snprintf(remainingbuf, sizeof(remainingbuf), "%02dh%02d",
169 max(hours, 0), max(minutes, 0));
173 for (walk = format; *walk != '\0'; walk++) {
175 *(outwalk++) = *walk;
179 if (strncmp(walk+1, "status", strlen("status")) == 0) {
180 outwalk += sprintf(outwalk, "%s", statusbuf);
181 walk += strlen("status");
182 } else if (strncmp(walk+1, "percentage", strlen("percentage")) == 0) {
183 outwalk += sprintf(outwalk, "%s", percentagebuf);
184 walk += strlen("percentage");
185 } else if (strncmp(walk+1, "remaining", strlen("remaining")) == 0) {
186 outwalk += sprintf(outwalk, "%s", remainingbuf);
187 walk += strlen("remaining");
188 } else if (strncmp(walk+1, "emptytime", strlen("emptytime")) == 0) {
189 outwalk += sprintf(outwalk, "%s", emptytimebuf);
190 walk += strlen("emptytime");
194 OUTPUT_FULL_TEXT(buffer);