16 #define PROC_DATA_SOURCE 0
17 #define SYSFS_DATA_SOURCE 1
18 static int data_source
;
21 int acpi_get_design_cap(int batt
);
23 static int read_sysfs_file(char *node
, char *prop
, char *buf
, size_t buflen
)
29 ret
= snprintf(tmp
, sizeof(tmp
), "/sys/class/power_supply/%s/%s", node
, prop
);
30 if (ret
>= (int)sizeof(tmp
)) {
31 perr("Path too long for %s/%s\n", node
, prop
);
37 perr("Could not open %s/%s\n", node
, prop
);
41 ret
= fread(buf
, 1, buflen
- 1, fp
);
46 perr("Could not read %s/%s\n", node
, prop
);
55 /* initialise the batteries */
56 static int sysfs_init_batteries(global_t
*globals
)
65 /* now enumerate batteries */
66 globals
->battery_count
= 0;
67 battdir
= opendir("/sys/class/power_supply");
68 if (battdir
== NULL
) {
69 pfatal("No batteries or ACPI not supported\n");
72 while ((batt
= readdir(battdir
))) {
73 /* there's a serious problem with this code when there's
74 * more than one battery: the readdir won't return the
75 * entries in sorted order, so battery one won't
76 * necessarily be the first one returned. So, we need
77 * to sort them ourselves before adding them to the
81 /* skip ., .. and dotfiles */
85 if (read_sysfs_file(name
, "type", ps_type
, sizeof(ps_type
)) < 0)
88 if (strncmp("Battery", ps_type
, 7) != 0)
91 names
[globals
->battery_count
] = strdup(name
);
92 globals
->battery_count
++;
96 /* A nice quick insertion sort, ala CLR. */
100 for (i
= 1; i
< globals
->battery_count
; i
++) {
103 while ((j
>= 0) && ((strcmp(tmp1
, names
[j
])) < 0)) {
105 names
[j
+1] = names
[j
];
111 for (i
= 0; i
< globals
->battery_count
; i
++) {
112 snprintf(batteries
[i
].name
, MAX_NAME
, "%s", names
[i
]);
113 pdebug("battery detected at /sys/class/power_supply/%s\n", batteries
[i
].name
);
114 pinfo("found battery %s\n", names
[i
]);
116 if (read_sysfs_file(batteries
[i
].name
, "energy_now", ps_type
, sizeof(ps_type
)) == 0)
117 batteries
[i
].sysfs_capa_mode
= SYSFS_CAPA_ENERGY
;
118 else if (read_sysfs_file(batteries
[i
].name
, "charge_now", ps_type
, sizeof(ps_type
)) == 0)
119 batteries
[i
].sysfs_capa_mode
= SYSFS_CAPA_CHARGE
;
120 else if (read_sysfs_file(batteries
[i
].name
, "capacity", ps_type
, sizeof(ps_type
)) == 0) {
121 batteries
[i
].sysfs_capa_mode
= SYSFS_CAPA_PERCENT
;
122 batteries
[i
].design_cap
= 100;
123 batteries
[i
].last_full_cap
= 100;
125 batteries
[i
].sysfs_capa_mode
= SYSFS_CAPA_ERR
;
128 /* tell user some info */
129 pdebug("%d batteries detected\n", globals
->battery_count
);
130 pinfo("libacpi: found %d batter%s\n", globals
->battery_count
,
131 (globals
->battery_count
== 1) ? "y" : "ies");
136 /* initialise the batteries */
137 static int procfs_init_batteries(global_t
*globals
)
142 char *names
[MAXBATT
];
145 /* now enumerate batteries */
146 globals
->battery_count
= 0;
147 battdir
= opendir("/proc/acpi/battery");
148 if (battdir
== NULL
) {
149 pfatal("No batteries or ACPI not supported\n");
152 while ((batt
= readdir(battdir
))) {
153 /* there's a serious problem with this code when there's
154 * more than one battery: the readdir won't return the
155 * entries in sorted order, so battery one won't
156 * necessarily be the first one returned. So, we need
157 * to sort them ourselves before adding them to the
158 * batteries array. */
162 if (!strncmp(".", name
, 1) || !strncmp("..", name
, 2))
165 names
[globals
->battery_count
] = strdup(name
);
166 globals
->battery_count
++;
170 /* A nice quick insertion sort, ala CLR. */
174 for (i
= 1; i
< globals
->battery_count
; i
++) {
177 while ((j
>= 0) && ((strcmp(tmp1
, names
[j
])) < 0)) {
179 names
[j
+1] = names
[j
];
185 for (i
= 0; i
< globals
->battery_count
; i
++) {
186 snprintf(batteries
[i
].name
, MAX_NAME
, "%s", names
[i
]);
187 snprintf(batteries
[i
].info_file
, MAX_NAME
,
188 "/proc/acpi/battery/%s/info", names
[i
]);
189 snprintf(batteries
[i
].state_file
, MAX_NAME
,
190 "/proc/acpi/battery/%s/state", names
[i
]);
191 pdebug("battery detected at %s\n", batteries
[i
].info_file
);
192 pinfo("found battery %s\n", names
[i
]);
195 /* tell user some info */
196 pdebug("%d batteries detected\n", globals
->battery_count
);
197 pinfo("libacpi: found %d batter%s\n", globals
->battery_count
,
198 (globals
->battery_count
== 1) ? "y" : "ies");
203 int init_batteries(global_t
*globals
)
205 if (data_source
== SYSFS_DATA_SOURCE
)
206 return sysfs_init_batteries(globals
);
208 return procfs_init_batteries(globals
);
211 /* a stub that just calls the current function */
212 int reinit_batteries(global_t
*globals
)
214 pdebug("reinitialising batteries\n");
215 return init_batteries(globals
);
218 /* the actual name of the subdirectory under power_supply may
219 * be anything, so we need to read the directory and use the
220 * name we find there. */
221 static int sysfs_init_ac_adapters(global_t
*globals
)
224 struct dirent
*adapter
;
225 adapter_t
*ap
= &globals
->adapter
;
229 acdir
= opendir("/sys/class/power_supply");
231 pfatal("Unable to open /sys/class/power_supply -"
232 " are you sure this system supports ACPI?\n");
236 while ((adapter
= readdir(acdir
)) != NULL
) {
237 name
= adapter
->d_name
;
239 if (name
[0] == '.') {
244 if (read_sysfs_file(name
, "type", ps_type
, sizeof(ps_type
)) < 0) {
249 if (strncmp("Mains", ps_type
, 5) == 0) {
250 pdebug("found adapter %s\n", name
);
259 perr("No AC adapter found !\n");
263 /* we'll just use the first adapter we find ... */
264 ap
->name
= strdup(name
);
265 pinfo("libacpi: found ac adapter %s\n", ap
->name
);
270 /* the actual name of the subdirectory under ac_adapter may
271 * be anything, so we need to read the directory and use the
272 * name we find there. */
273 static int procfs_init_ac_adapters(global_t
*globals
)
276 struct dirent
*adapter
;
277 adapter_t
*ap
= &globals
->adapter
;
280 acdir
= opendir("/proc/acpi/ac_adapter");
282 pfatal("Unable to open /proc/acpi/ac_adapter -"
283 " are you sure this system supports ACPI?\n");
287 while ((adapter
= readdir(acdir
)) != NULL
) {
288 name
= adapter
->d_name
;
290 if (!strncmp(".", name
, 1) || !strncmp("..", name
, 2))
292 pdebug("found adapter %s\n", name
);
295 /* we /should/ only see one filename other than . and .. so
296 * we'll just use the last value name acquires . . . */
297 ap
->name
= strdup(name
);
298 snprintf(ap
->state_file
, MAX_NAME
, "/proc/acpi/ac_adapter/%s/state",
300 pinfo("libacpi: found ac adapter %s\n", ap
->name
);
305 int init_ac_adapters(global_t
*globals
)
307 if (data_source
== SYSFS_DATA_SOURCE
)
308 return sysfs_init_ac_adapters(globals
);
310 return procfs_init_ac_adapters(globals
);
313 /* stub that does nothing but call the normal init function */
314 int reinit_ac_adapters(global_t
*globals
)
316 pdebug("reinitialising ac adapters\n");
317 return init_ac_adapters(globals
);
320 /* see if we have ACPI support and check version */
321 int power_init(global_t
*globals
)
327 unsigned int version_offset
= 0;
329 if (!(acpi
= fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
330 if (!(acpi
= fopen("/proc/acpi/info", "r"))) {
331 pfatal("This system does not support ACPI\n");
338 /* okay, now see if we got the right version */
339 fread(buf
, 4096, 1, acpi
);
340 acpi_ver
= strtol(buf
+ version_offset
, NULL
, 10);
341 pinfo("ACPI version detected: %d\n", acpi_ver
);
342 if (acpi_ver
< 20020214) {
343 pfatal("This version requires ACPI subsystem version 20020214\n");
350 /* determine data source */
351 if (access("/sys/class/power_supply", R_OK
| X_OK
) == 0) {
352 data_source
= SYSFS_DATA_SOURCE
;
353 pinfo("Selecting sysfs as the data source\n");
355 data_source
= PROC_DATA_SOURCE
;
356 pinfo("Selecting procfs as the data source\n");
359 if (!(retval
= init_batteries(globals
)))
360 retval
= init_ac_adapters(globals
);
365 /* reinitialise everything, to deal with changing batteries or ac adapters */
366 int power_reinit(global_t
*globals
)
371 if (!(acpi
= fopen("/sys/module/acpi/parameters/acpica_version", "r"))) {
372 if (!(acpi
= fopen("/proc/acpi/info", "r"))) {
373 pfatal("Could not reopen ACPI info file - does this system support ACPI?\n");
378 if (!(retval
= reinit_batteries(globals
)))
379 retval
= reinit_ac_adapters(globals
);
384 static char *get_value(char *string
)
393 while (string
[i
] != ':') i
++;
394 while (!isalnum(string
[i
])) i
++;
395 retval
= (string
+ i
);
400 static int check_error(char *buf
)
402 if(strstr(buf
, "ERROR") != NULL
)
407 static power_state_t
sysfs_get_power_status(global_t
*globals
)
410 adapter_t
*ap
= &globals
->adapter
;
412 if (read_sysfs_file(ap
->name
, "online", online
, sizeof(online
)) < 0)
421 static power_state_t
procfs_get_power_status(global_t
*globals
)
426 adapter_t
*ap
= &globals
->adapter
;
428 if ((file
= fopen(ap
->state_file
, "r")) == NULL
) {
429 snprintf(buf
, 1024, "Could not open state file %s", ap
->state_file
);
434 fgets(buf
, 1024, file
);
436 val
= get_value(buf
);
437 if ((strncmp(val
, "on-line", 7)) == 0)
443 power_state_t
get_power_status(global_t
*globals
)
445 if (data_source
== SYSFS_DATA_SOURCE
)
446 return sysfs_get_power_status(globals
);
448 return procfs_get_power_status(globals
);
451 static int sysfs_get_battery_info(global_t
*globals
, int batt_no
)
453 battery_t
*info
= &batteries
[batt_no
];
457 /* check to see if battery is present */
458 ret
= read_sysfs_file(info
->name
, "present", buf
, sizeof(buf
));
460 /* interestingly, when the battery is not present, the whole
461 * /sys/class/power_supply/BATn directory does not exist.
462 * Yes, this is broken.
467 /* reinit batteries, this one went away and it's very
468 possible there just isn't any other one */
469 reinit_batteries(globals
);
474 info
->present
= (*buf
== '1');
475 if (!info
->present
) {
476 pinfo("Battery %s not present\n", info
->name
);
480 /* get design capacity
481 * note that all these integer values can also contain the
482 * string 'unknown', so we need to check for this. */
483 if (info
->sysfs_capa_mode
== SYSFS_CAPA_ENERGY
) {
484 if (read_sysfs_file(info
->name
, "energy_full_design", buf
, sizeof(buf
)) < 0)
485 info
->design_cap
= -1;
487 info
->design_cap
= strtoul(buf
, NULL
, 10) / 1000;
489 /* get last full capacity */
490 if (read_sysfs_file(info
->name
, "energy_full", buf
, sizeof(buf
)) < 0)
491 info
->last_full_cap
= -1;
493 info
->last_full_cap
= strtoul(buf
, NULL
, 10) / 1000;
494 } else if (info
->sysfs_capa_mode
== SYSFS_CAPA_CHARGE
) {
495 /* get design capacity */
496 if (read_sysfs_file(info
->name
, "charge_full_design", buf
, sizeof(buf
)) < 0)
497 info
->design_cap
= -1;
499 info
->design_cap
= strtoul(buf
, NULL
, 10) / 1000;
501 /* get last full capacity */
502 if (read_sysfs_file(info
->name
, "charge_full", buf
, sizeof(buf
)) < 0)
503 info
->last_full_cap
= -1;
505 info
->last_full_cap
= strtoul(buf
, NULL
, 10) / 1000;
506 } else if (info
->sysfs_capa_mode
!= SYSFS_CAPA_PERCENT
) {
507 info
->design_cap
= -1;
508 info
->last_full_cap
= -1;
512 /* get design voltage */
513 if (read_sysfs_file(info
->name
, "voltage_min_design", buf
, sizeof(buf
)) < 0)
514 info
->design_voltage
= -1;
516 info
->design_voltage
= strtoul(buf
, NULL
, 10) / 1000;
518 /* get charging state */
519 if (read_sysfs_file(info
->name
, "status", buf
, sizeof(buf
)) < 0) {
520 info
->charge_state
= CH_ERR
;
522 if (strncmp(buf
, "Unknown", 7) == 0)
523 info
->charge_state
= CH_ERR
;
524 else if (strncmp(buf
, "Discharging", 11) == 0)
525 info
->charge_state
= DISCHARGE
;
526 else if (strncmp(buf
, "Charging", 8) == 0)
527 info
->charge_state
= CHARGE
;
528 else if (strncmp(buf
, "Not charging", 12) == 0)
529 info
->charge_state
= NO_CHARGE
;
530 else if (strncmp(buf
, "Full", 4) == 0)
531 info
->charge_state
= FULL
; /* DISCHARGE ? as per old comment ... */
534 /* get current rate of burn
535 * note that if it's on AC, this will report 0 */
536 if (read_sysfs_file(info
->name
, "current_now", buf
, sizeof(buf
)) < 0)
537 info
->present_rate
= -1;
540 rate
= strtoul(buf
, NULL
, 10) / 1000;
541 info
->present_rate
= (rate
!= 0) ? rate
: info
->present_rate
;
544 if (info
->sysfs_capa_mode
== SYSFS_CAPA_ENERGY
) {
545 /* get remaining capacity */
546 if (read_sysfs_file(info
->name
, "energy_now", buf
, sizeof(buf
)) < 0)
547 info
->remaining_cap
= -1;
549 info
->remaining_cap
= strtoul(buf
, NULL
, 10) / 1000;
551 } else if (info
->sysfs_capa_mode
== SYSFS_CAPA_CHARGE
) {
552 /* get remaining capacity */
553 if (read_sysfs_file(info
->name
, "charge_now", buf
, sizeof(buf
)) < 0)
554 info
->remaining_cap
= -1;
556 info
->remaining_cap
= strtoul(buf
, NULL
, 10) / 1000;
557 } else if (info
->sysfs_capa_mode
== SYSFS_CAPA_PERCENT
) {
558 /* get remaining capacity */
559 if (read_sysfs_file(info
->name
, "capacity", buf
, sizeof(buf
)) < 0)
560 info
->remaining_cap
= -1;
562 info
->remaining_cap
= strtoul(buf
, NULL
, 10) / 1000;
564 info
->remaining_cap
= -1;
567 /* get current voltage */
568 if (read_sysfs_file(info
->name
, "voltage_now", buf
, sizeof(buf
)) < 0)
569 info
->present_voltage
= -1;
571 info
->present_voltage
= strtoul(buf
, NULL
, 10) / 1000;
576 static int procfs_get_battery_info(global_t
*globals
, int batt_no
)
579 battery_t
*info
= &batteries
[batt_no
];
585 globals
= globals
; /* silencing a warning */
587 if ((file
= fopen(info
->info_file
, "r")) == NULL
) {
588 /* this is cheating, but string concatenation should work . . . */
589 pfatal("Could not open %s:", info
->info_file
);
594 /* grab the contents of the file */
595 buflen
= fread(buf
, sizeof(buf
), 1, file
);
598 /* check to see if there were any errors reported in the file */
599 if(check_error(buf
)) {
600 pinfo("Error reported in file %s - discarding data\n",
605 /* check to see if battery is present */
606 entry
= strstr(buf
, "present:");
607 val
= get_value(entry
);
608 if ((strncmp(val
, "yes", 3)) == 0) {
611 pinfo("Battery %s not present\n", info
->name
);
616 /* get design capacity
617 * note that all these integer values can also contain the
618 * string 'unknown', so we need to check for this. */
619 entry
= strstr(buf
, "design capacity:");
620 val
= get_value(entry
);
622 info
->design_cap
= -1;
624 info
->design_cap
= strtoul(val
, NULL
, 10);
626 /* get last full capacity */
627 entry
= strstr(buf
, "last full capacity:");
628 val
= get_value(entry
);
630 info
->last_full_cap
= -1;
632 info
->last_full_cap
= strtoul(val
, NULL
, 10);
634 /* get design voltage */
635 entry
= strstr(buf
, "design voltage:");
636 val
= get_value(entry
);
638 info
->design_voltage
= -1;
640 info
->design_voltage
= strtoul(val
, NULL
, 10);
643 if ((file
= fopen(info
->state_file
, "r")) == NULL
) {
644 perr("Could not open %s:", info
->state_file
);
649 /* grab the file contents */
650 memset(buf
, 0, sizeof(buf
));
651 buflen
= fread(buf
, sizeof(buf
), 1, file
);
654 /* check to see if there were any errors reported in the file */
655 if(check_error(buf
)) {
656 pinfo("Error reported in file %s - discarding data\n",
660 /* check to see if battery is present */
661 entry
= strstr(buf
, "present:");
662 val
= get_value(entry
);
663 if ((strncmp(val
, "yes", 3)) == 0) {
667 perr("Battery %s no longer present\n", info
->name
);
671 /* get charging state */
672 entry
= strstr(buf
, "charging state:");
673 val
= get_value(entry
);
675 info
->charge_state
= CH_ERR
;
676 else if ((strncmp(val
, "discharging", 10)) == 0)
677 info
->charge_state
= DISCHARGE
;
678 else if ((strncmp(val
, "charged", 7)) == 0)
679 /* this is a workaround for machines that report
680 * their charge state as 'charged', rather than
681 * what my laptop does, which is go straight to
682 * 'discharging'. dunno which matches the standard */
683 info
->charge_state
= DISCHARGE
;
685 info
->charge_state
= CHARGE
;
687 /* get current rate of burn
688 * note that if it's on AC, this will report 0 */
689 entry
= strstr(buf
, "present rate:");
690 val
= get_value(entry
);
692 info
->present_rate
= -1;
695 rate
= strtoul(val
, NULL
, 10);
697 info
->present_rate
= rate
;
700 /* get remaining capacity */
701 entry
= strstr(buf
, "remaining capacity:");
702 val
= get_value(entry
);
704 info
->remaining_cap
= -1;
706 info
->remaining_cap
= strtoul(val
, NULL
, 10);
708 /* get current voltage */
709 entry
= strstr(buf
, "present voltage:");
710 val
= get_value(entry
);
712 info
->present_voltage
= -1;
714 info
->present_voltage
= strtoul(val
, NULL
, 10);
719 int get_battery_info(global_t
*globals
, int batt_no
)
721 if (data_source
== SYSFS_DATA_SOURCE
)
722 return sysfs_get_battery_info(globals
, batt_no
);
724 return procfs_get_battery_info(globals
, batt_no
);
729 * In order to make this code more convenient for things other than
730 * just plain old wmacpi-ng I'm breaking the basic functionality
731 * up into several chunks: collecting and collating info for a
732 * single battery, calculating the global info (such as rtime), and
733 * some stuff to provide a similar interface to now.
736 /* calculate the percentage remaining, using the values of
737 * remaining capacity and last full capacity, as outlined in
738 * the ACPI spec v2.0a, section 3.9.3. */
739 static int calc_remaining_percentage(int batt
)
745 binfo
= &batteries
[batt
];
747 rcap
= (float)binfo
->remaining_cap
;
748 lfcap
= (float)binfo
->last_full_cap
;
750 /* we use -1 to indicate that the value is unknown . . . */
752 perr("unknown percentage value\n");
757 retval
= (int)((rcap
/lfcap
) * 100.0);
758 pdebug("percent: %d\n", retval
);
763 /* check to see if we've been getting bad data from the batteries - if
764 * we get more than some limit we switch to using the remaining capacity
765 * for the calculations. */
766 static enum rtime_mode
check_rt_mode(global_t
*globals
)
772 /* if we were told what to do, we should keep doing it */
773 if(globals
->rt_forced
)
774 return globals
->rt_mode
;
776 for(i
= 0; i
< MAXBATT
; i
++) {
777 binfo
= &batteries
[i
];
778 if(binfo
->present
&& globals
->adapter
.power
== BATT
) {
779 if(binfo
->present_rate
<= 0) {
780 pdebug("Bad report from %s\n", binfo
->name
);
785 for(i
= 0; i
< MAXBATT
; i
++) {
786 binfo
= &batteries
[i
];
787 if(binfo
->bad_count
> bad_limit
) {
788 if(globals
->rt_mode
!= RT_CAP
)
789 pinfo("More than %d bad reports from %s; "
790 "Switching to remaining capacity mode\n",
791 bad_limit
, binfo
->name
);
798 /* calculate remaining time until the battery is charged.
799 * when charging, the battery state file reports the
800 * current being used to charge the battery. We can use
801 * this and the remaining capacity to work out how long
802 * until it reaches the last full capacity of the battery.
803 * XXX: make sure this is actually portable . . . */
804 static int calc_charge_time_rate(int batt
)
810 binfo
= &batteries
[batt
];
812 if (binfo
->charge_state
== CHARGE
) {
813 if (binfo
->present_rate
== -1) {
814 perr("unknown present rate\n");
817 lfcap
= (float)binfo
->last_full_cap
;
818 rcap
= (float)binfo
->remaining_cap
;
820 charge_time
= (int)(((lfcap
- rcap
)/binfo
->present_rate
) * 60.0);
823 if (binfo
->charge_time
)
828 /* we need to calculate the present rate the same way we do in rt_cap
829 * mode, and then use that to estimate charge time. This will
830 * necessarily be even less accurate than it is for remaining time, but
831 * it's just as neessary . . . */
832 static int calc_charge_time_cap(int batt
)
834 static float cap_samples
[CAP_SAMPLES
];
835 static int time_samples
[CAP_SAMPLES
];
836 static int sample_count
= 0;
837 static int current
= 0;
843 battery_t
*binfo
= &batteries
[batt
];
845 cap_samples
[current
] = (float) binfo
->remaining_cap
;
846 time_samples
[current
] = time(NULL
);
848 if (sample_count
== 0) {
849 /* we can't do much if we don't have any data . . . */
851 } else if (sample_count
< CAP_SAMPLES
) {
852 /* if we have less than SAMPLES samples so far, we use the first
853 * sample and the current one */
854 cdiff
= cap_samples
[current
] - cap_samples
[0];
855 tdiff
= time_samples
[current
] - time_samples
[0];
856 current_rate
= cdiff
/tdiff
;
858 /* if we have more than SAMPLES samples, we use the oldest
859 * current one, which at this point is current + 1. This will
860 * wrap the same way that current will wrap, but one cycle
862 cdiff
= cap_samples
[current
] - cap_samples
[old
];
863 tdiff
= time_samples
[current
] - time_samples
[old
];
864 current_rate
= cdiff
/(float)tdiff
;
866 if (current_rate
== 0)
869 float cap_left
= (float)(binfo
->last_full_cap
- binfo
->remaining_cap
);
870 rtime
= (int)(cap_left
/(current_rate
* 60.0));
872 sample_count
++, current
++, old
++;
873 if (current
>= CAP_SAMPLES
)
875 if (old
>= CAP_SAMPLES
)
878 pdebug("cap charge time rem: %d\n", rtime
);
882 static int calc_charge_time(global_t
*globals
, int batt
)
886 globals
->rt_mode
= check_rt_mode(globals
);
888 switch(globals
->rt_mode
) {
890 ctime
= calc_charge_time_rate(batt
);
893 ctime
= calc_charge_time_cap(batt
);
899 void acquire_batt_info(global_t
*globals
, int batt
)
902 adapter_t
*ap
= &globals
->adapter
;
904 get_battery_info(globals
, batt
);
906 binfo
= &batteries
[batt
];
908 if (!binfo
->present
) {
909 binfo
->percentage
= 0;
911 binfo
->charge_time
= 0;
916 binfo
->percentage
= calc_remaining_percentage(batt
);
918 /* set the battery's capacity state, based (at present) on some
919 * guesstimated values: more than 75% == HIGH, 25% to 75% MED, and
920 * less than 25% is LOW. Less than globals->crit_level is CRIT. */
921 if (binfo
->percentage
== -1)
922 binfo
->state
= BS_ERR
;
923 if (binfo
->percentage
< globals
->crit_level
)
925 else if (binfo
->percentage
> 75)
927 else if (binfo
->percentage
> 25)
932 /* we need to /know/ that we've got a valid state for the
933 * globals->power value . . . .*/
934 ap
->power
= get_power_status(globals
);
936 binfo
->charge_time
= calc_charge_time(globals
, batt
);
938 /* and finally, we tell anyone who wants to use this information
939 * that it's now valid . . .*/
943 void acquire_all_batt_info(global_t
*globals
)
947 for(i
= 0; i
< globals
->battery_count
; i
++)
948 acquire_batt_info(globals
, i
);
952 * One of the feature requests I've had is for some way to deal with
953 * batteries that are too dumb or too b0rken to report a present rate
954 * value. The way to do this, obviously, is to record the time that
955 * samples were taken and use that information to calculate the rate
956 * at which the battery is draining/charging. This still won't help
957 * systems where the battery doesn't even report the remaining
958 * capacity, but without the present rate or the remaining capacity, I
959 * don't think there's /anything/ we can do to work around it.
961 * So, what we need to do is provide a way to use a different method
962 * to calculate the time remaining. What seems most sensible is to
963 * split out the code to calculate it into a seperate function, and
964 * then provide multiple implementations . . .
968 * the default implementation - if present rate and remaining capacity
969 * are both reported correctly, we use them.
971 int calc_time_remaining_rate(global_t
*globals
)
978 static float rate_samples
[SAMPLES
];
979 static int sample_count
= 0;
983 /* calculate the time remaining, using the battery's remaining
984 * capacity and the reported burn rate (3.9.3).
985 * For added accuracy, we average the value over the last
986 * SAMPLES number of calls, or for anything less than this we
987 * simply report the raw number. */
988 /* XXX: this needs to correctly handle the case where
989 * any of the values used is unknown (which we flag using
991 for (i
= 0; i
< globals
->battery_count
; i
++) {
992 binfo
= &batteries
[i
];
993 if (binfo
->present
&& binfo
->valid
) {
994 rcap
+= (float)binfo
->remaining_cap
;
995 rate
+= (float)binfo
->present_rate
;
998 rate_samples
[j
] = rate
;
1003 /* for the first SAMPLES number of calls we calculate the
1004 * average based on sample_count, then we use SAMPLES to
1005 * calculate the rolling average. */
1007 /* when this fails, n should be equal to SAMPLES. */
1008 if (sample_count
< SAMPLES
)
1010 for (i
= 0, rate
= 0; i
< n
; i
++) {
1011 /* if any of our samples are invalid, we drop
1012 * straight out, and flag our unknown values. */
1013 if (rate_samples
[i
] < 0) {
1018 rate
+= rate_samples
[i
];
1020 rate
= rate
/(float)n
;
1022 if ((rcap
< 1) || (rate
< 1)) {
1028 /* time remaining in minutes */
1029 rtime
= (int)((rcap
/rate
) * 60.0);
1033 pdebug("discharge time rem: %d\n", rtime
);
1038 * the alternative implementation - record the time at which each
1039 * sample was taken, and then use the difference between the latest
1040 * sample and the one SAMPLES ago to calculate the difference over
1041 * that time, and from there the rate of change of capacity.
1043 * XXX: this code sucks, but largely because batteries aren't exactly
1044 * precision instruments - mine only report with about 70mAH
1045 * resolution, so they don't report any changes until the difference
1046 * is 70mAH. This means that calculating the current rate from the
1047 * remaining capacity is very choppy . . .
1049 * To fix this, we should calculate an average over some number of
1050 * samples at the old end of the set - this would smooth out the
1053 int calc_time_remaining_cap(global_t
*globals
)
1055 static float cap_samples
[CAP_SAMPLES
];
1056 static int time_samples
[CAP_SAMPLES
];
1057 static int sample_count
= 0;
1058 static int current
= 0;
1068 for (i
= 0; i
< globals
->battery_count
; i
++) {
1069 binfo
= &batteries
[i
];
1070 if (binfo
->present
&& binfo
->valid
)
1071 cap
+= binfo
->remaining_cap
;
1073 cap_samples
[current
] = cap
;
1074 time_samples
[current
] = time(NULL
);
1076 if (sample_count
== 0) {
1077 /* we can't do much if we don't have any data . . . */
1079 } else if (sample_count
< CAP_SAMPLES
) {
1080 /* if we have less than SAMPLES samples so far, we use the first
1081 * sample and the current one */
1082 cdiff
= cap_samples
[0] - cap_samples
[current
];
1083 tdiff
= time_samples
[current
] - time_samples
[0];
1084 current_rate
= cdiff
/tdiff
;
1086 /* if we have more than SAMPLES samples, we use the oldest
1087 * current one, which at this point is current + 1. This will
1088 * wrap the same way that current will wrap, but one cycle
1090 cdiff
= cap_samples
[old
] - cap_samples
[current
];
1091 tdiff
= time_samples
[current
] - time_samples
[old
];
1092 current_rate
= cdiff
/tdiff
;
1094 if (current_rate
== 0)
1097 rtime
= (int)(cap_samples
[current
]/(current_rate
* 60.0));
1099 sample_count
++, current
++, old
++;
1100 if (current
>= CAP_SAMPLES
)
1102 if (old
>= CAP_SAMPLES
)
1105 pdebug("cap discharge time rem: %d\n", rtime
);
1109 void acquire_global_info(global_t
*globals
)
1111 adapter_t
*ap
= &globals
->adapter
;
1113 globals
->rt_mode
= check_rt_mode(globals
);
1115 switch(globals
->rt_mode
) {
1117 globals
->rtime
= calc_time_remaining_rate(globals
);
1120 globals
->rtime
= calc_time_remaining_cap(globals
);
1124 /* get the power status.
1125 * note that this is actually reported seperately from the
1126 * battery info, under /proc/acpi/ac_adapter/AC/state */
1127 ap
->power
= get_power_status(globals
);
1130 void acquire_all_info(global_t
*globals
)
1132 acquire_all_batt_info(globals
);
1133 acquire_global_info(globals
);