Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AC/battery detection logic on FreeBSD. #1972

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -427,24 +427,50 @@ double get_acpi_temperature(int fd) {
return 0.0;
}

// If a leaf MIB in the sysctl tree returns ENOENT, that means the entry does
// not exist. On the contrary, if a non-leaf entry *does exist*, then EISDIR
// errno is returned, meaning it exists, but it is an array/directory with
// more elements hanging from it.
static int sysctl_mib_exists(const char *mib)
{
size_t len;
void *p = NULL;
sysctlbyname(mib, p, &len, NULL, 0);
return !(errno == ENOENT);
}

static void get_battery_stats(int *battime, int *batcapacity, int *batstate,
int *ac) {
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
}
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
int battery_present = sysctl_mib_exists("hw.acpi.battery");
int ac_present = sysctl_mib_exists("hw.acpi.acline");

if (!battery_present && !ac_present) {
// According to acpi(4), hw.acpi.acline is optional and only present
// if supported by the hardware. If no battery and acline is detected,
// for sure we are running on an AC line.
*ac = 1;
*batstate = 7;
return;
}
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");

if (battery_present) {
if (battime && GETSYSCTL("hw.acpi.battery.time", *battime)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.time\"\n");
NORM_ERR("Cannot read sysctl \"hw.acpi.battery.time\"");

}
if (batcapacity && GETSYSCTL("hw.acpi.battery.life", *batcapacity)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.life\"\n");
NORM_ERR("Cannot read sysctl \"hw.acpi.battery.life\"");

}
if (batstate && GETSYSCTL("hw.acpi.battery.state", *batstate)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fprintf(stderr, "Cannot read sysctl \"hw.acpi.battery.state\"\n");
NORM_ERR("Cannot read sysctl \"hw.acpi.battery.state\"");

}
}
if (ac && GETSYSCTL("hw.acpi.acline", *ac)) {
if (ac_present && ac && GETSYSCTL("hw.acpi.acline", *ac)) {
fprintf(stderr, "Cannot read sysctl \"hw.acpi.acline\"\n");
}
}

void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item) {
int battime, batcapacity, batstate, ac;
int battime = 0, batcapacity = 0, batstate = 0, ac = 0;
(void)bat;

get_battery_stats(&battime, &batcapacity, &batstate, &ac);
Expand Down
Loading