![]() |
|
|
#34 |
|
Sep 2009
2×1,039 Posts |
The code would also have "interesting" behavior on a system using EBCDIC. But that's not very likely to occur in the real world. But I could believe some unicode variant doing strange things.
Chris |
|
|
|
|
|
#35 |
|
Sep 2010
Annapolis, MD, USA
18910 Posts |
Modern Linux seems to use /sys/class/power_supply/ instead of /proc/acpi/battery/ -- is there any chance we can have support for Linux battery detection updated?
Looks like you could either examine /sys/class/power_supply/BAT0/status or just /sys/class/power_supply/AC/online ... instead of parsing the old /proc/acpi/battery/BAT0/state, it looks like /sys/class/power_supply/AC/online returns a 0 (battery) or 1 (AC). So this should actually simplify the code a bit, and it will stop us from using a deprecated interface. Anyone using a later 2.6 or any 3.x kernel without the deprecated CONFIG_ACPI_PROCFS_POWER option enabled will not have a /proc/acpi/battery/BAT0/state at all! I had always wondered why battery detection wasn't working right, and now I know. :) Hopefully it's an easy fix. Let me know if I can be of any assistance in testing. I would be happy to unset that kernel option and check a new build. |
|
|
|
|
|
#36 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
3×29×83 Posts |
Quote:
|
|
|
|
|
|
|
#37 | |
|
Sep 2010
Annapolis, MD, USA
2758 Posts |
Quote:
I don't know why Ubuntu chose to keep that interface around, but I just checked a 10.10 VM that I have and yes, it is enabled there too. On my personal machine, it is not enabled. I am a little surprised that Ubuntu would keep that deprecated code active in a 2012 release, but I guess they would have to support people who don't understand why things that used the old interface suddenly stopped working. I am reluctant to enable such an option when it is clear that the way forward is through /sys rather than /proc.http://cateee.net/lkddb/web-lkddb/AC...CFS_POWER.html Perhaps we should read from /sys, if that fails then we read from /proc, and if that fails then we assume AC. (Today, we already do exactly that except we don't check /sys first...) The changes to the software should be limited to just a few lines of the OnBattery function in linux/os_routines.c and linux64/os_routines.c (which seem to be identical). |
|
|
|
|
|
|
#38 | |
|
Basketry That Evening!
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88
1C3516 Posts |
Quote:
Code:
CONFIG_ACPI_PROCFS_POWER=y [/QUOTE] |
|
|
|
|
|
|
#39 |
|
P90 years forever!
Aug 2002
Yeehaw, FL
2×53×71 Posts |
If anyone can provide updated code for me to include that would be great. The new code should handle both the old and new standards.
|
|
|
|
|
|
#40 |
|
"Mike"
Aug 2002
3×2,741 Posts |
12.04 LTS
Code:
$ grep ACPI_PROCFS_POWER /boot/config-`uname -r` CONFIG_ACPI_PROCFS_POWER=y $ uname -a Linux m 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux |
|
|
|
|
|
#41 | |
|
Sep 2010
Annapolis, MD, USA
18910 Posts |
Quote:
![]() As part of this section of code in linux/os_routines.c and linux64/os_routines.c: Code:
#elif defined (__linux__)
FILE *fd;
char buf[180];
int ac_state;
ac_state = -1;
fd = fopen ("/proc/acpi/battery/BAT0/state", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
char *p;
p = strstr (buf, "charging state:");
if (p == NULL) continue;
if (strstr (p+14, "discharging") != NULL) ac_state = 0;
else if (strstr (p+14, "charging") != NULL) ac_state = 1;
else if (strstr (p+14, "charged") != NULL) ac_state = 1;
}
fclose (fd);
} ####### NEW CODE GOES HERE ########
return (ac_state == 0);
#else
Code:
else {
fd = fopen ("/sys/class/power_supply/AC/online", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
ac_state = atoi(buf);
}
}
}
Code:
#elif defined (__linux__)
FILE *fd;
char buf[180];
int ac_state;
ac_state = -1;
fd = fopen ("/proc/acpi/battery/BAT0/state", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
char *p;
p = strstr (buf, "charging state:");
if (p == NULL) continue;
if (strstr (p+14, "discharging") != NULL) ac_state = 0;
else if (strstr (p+14, "charging") != NULL) ac_state = 1;
else if (strstr (p+14, "charged") != NULL) ac_state = 1;
}
fclose (fd);
} else {
fd = fopen ("/sys/class/power_supply/AC/online", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
ac_state = atoi(buf);
}
}
}
return (ac_state == 0);
#else
Code:
/* actest.c, no warranty, etc */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int onBattery(void);
int main()
{
int battery;
battery = onBattery();
if (battery == TRUE)
printf("BATTERY\n");
else if (battery == FALSE)
printf("AC\n");
else
printf("Uh oh\n");
return 0;
}
int onBattery(void)
{
FILE *fd;
char buf[180];
int ac_state;
ac_state = -1;
fd = fopen ("/proc/acpi/battery/BAT0/state", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
char *p;
p = strstr (buf, "charging state:");
if (p == NULL) continue;
if (strstr (p+14, "discharging") != NULL) ac_state = 0;
else if (strstr (p+14, "charging") != NULL) ac_state = 1;
else if (strstr (p+14, "charged") != NULL) ac_state = 1;
}
fclose (fd);
} else {
fd = fopen ("/sys/class/power_supply/AC/online", "r");
if (fd != NULL) {
while (fgets (buf, sizeof (buf), fd) != NULL) {
ac_state = atoi(buf);
}
}
}
return (ac_state == 0);
}
And if the call to atoi is too slow, there are other ways to do it. The file is simply a 1 or a 0.I am too used to coding in Ruby; I forgot how picky C is in certain situations...! |
|
|
|
|
|
|
#42 | |
|
If I May
"Chris Halsall"
Sep 2002
Barbados
2×5×7×139 Posts |
Quote:
Code:
FILE *fd;
char buf[180];
int ac_state;
ac_state = -1;
fd = fopen ("/sys/class/power_supply/AC/online", "r");
if (fd != NULL) {
fscanf(fd, "%d", &ac_state);
fclose (fd);
return (ac_state == 0);
}
fd = fopen ("/proc/acpi/battery/BAT0/state", "r");
|
|
|
|
|
|
|
#43 |
|
Sep 2010
Annapolis, MD, USA
33·7 Posts |
Prime95: In case it's not obvious from the code, this will attempt the old way first, and then if that didn't work, it will try the new way. You might decide to flip it around. I didn't check how often this gets called, so I don't know how tight it needs to be. Maybe it is worth caching which one exists and using it every time? Or maybe that is overkill.
Everyone: If anyone wants to help out, copy/paste the contents of the last code block (the one labeled actest.c) into a file called actest.c. Change into the directory containing that file and type 'make actest'. Then type './actest' both while on battery and on AC. Results from Ubuntu would be especially appreciated, or perhaps I can convince my stepfather to let me borrow his Ubuntu laptop for a few minutes... |
|
|
|
|
|
#44 |
|
Sep 2010
Annapolis, MD, USA
101111012 Posts |
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Prime95 version 27.3 | Prime95 | Software | 148 | 2012-03-18 19:24 |
| Prime95 version 26.3 | Prime95 | Software | 76 | 2010-12-11 00:11 |
| Prime95 version 25.5 | Prime95 | PrimeNet | 369 | 2008-02-26 05:21 |
| Prime95 version 25.4 | Prime95 | PrimeNet | 143 | 2007-09-24 21:01 |
| When the next prime95 version ? | pacionet | Software | 74 | 2006-12-07 20:30 |