Skip to content

Instantly share code, notes, and snippets.

@agrajag9
Created September 14, 2019 20:12
Show Gist options
  • Save agrajag9/2fed3328699c1e16b1b6641d72f4bf34 to your computer and use it in GitHub Desktop.
Save agrajag9/2fed3328699c1e16b1b6641d72f4bf34 to your computer and use it in GitHub Desktop.
Get ACPI battery BST data in FreeBSD
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sysexits.h>
#include <unistd.h>
#include <dev/acpica/acpiio.h>
#include <contrib/dev/acpica/include/acpi.h>
#define ACPIDEV "/dev/acpi"
#define UNKNOWN_VOLTAGE 0xffffffff
//struct acpi_bst {
// uint32_t state; /* Battery State */
// uint32_t rate; /* Present Rate */
// uint32_t cap; /* Remaining Capacity */
// uint32_t volt; /* Present Voltage */
//};
//union acpi_battery_ioctl_arg {
// int unit; /* Device unit or ACPI_BATTERY_ALL_UNITS. */
// struct acpi_battinfo battinfo;
// struct acpi_bif bif;
// struct acpi_bst bst;
//};
static int acpifd;
static void acpi_init(void) {
printf("DEBUG: begin acpi_init\n");
printf("DEBUG: Opening /dev/acpi\n");
acpifd = open(ACPIDEV, O_RDONLY);
if (acpifd == -1) {
printf("ERROR: Failed to open /dev/acpi\n");
err(EX_OSFILE, ACPIDEV);
} else {
printf("DEBUG: Success!\n");
}
printf("DEBUG: end acpi_init\n");
}
static int acpi_battinfo(int num) {
printf("DEBUG: begin acpi_battinfo\n");
union acpi_battery_ioctl_arg battio;
const char *pwr_units;
int hours, min, amp;
uint32_t volt;
if (num < 0 || num > 64)
errx(EX_USAGE, "invalid battery %d", num);
/* Fetch BST information. */
volt = UNKNOWN_VOLTAGE; // 0xffffffff
battio.unit = num;
if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1) {
printf("get battery status (%d) failed\n", num);
err(EX_IOERR, "get battery status (%d) failed", num);
}
if (battio.bst.state != ACPI_BATT_STAT_NOT_PRESENT) {
printf("DEBUG: we can use BST for voltage!\n");
volt = battio.bst.volt;
}
/* Print BST information */
printf("BST_STATE: charging/discharging and critical low\n");
printf("BST_RATE: power/current supplied or accepted through terminals\n");
printf("BST_CAP: estimated remaining capacity\n");
printf("BST_VOLT: voltage across terminals\n");
printf("BST_STATE: %10u : %-#10x\n", battio.bst.state, battio.bst.state);
printf("BST_RATE: %10u : %-#10x\n", battio.bst.rate, battio.bst.rate);
printf("BST_CAP: %10u : %-#10x\n", battio.bst.cap, battio.bst.cap);
printf("BST_VOLT: %10u : %-#10x\n", battio.bst.volt, battio.bst.volt);
printf("DEBUG: end acpi_battinfo\n");
return (0);
}
int main(int argc, char *argv[]) {
printf("DEBUG: begin main\n");
int battery = 0;
acpi_init();
acpi_battinfo(battery);
close(acpifd);
printf("DEBUG: end main\n");
exit (0);
}
$ cc -o get_bst -I/usr/src/sys get_bst.c && ./get_bst
DEBUG: begin main
DEBUG: begin acpi_init
DEBUG: Opening /dev/acpi
DEBUG: Success!
DEBUG: end acpi_init
DEBUG: begin acpi_battinfo
DEBUG: we can use BST for voltage!
BST_STATE: charging/discharging and critical low
BST_RATE: power/current supplied or accepted through terminals
BST_CAP: estimated remaining capacity
BST_VOLT: voltage across terminals
BST_STATE: 1 : 0x1
BST_RATE: 15983 : 0x3e6f
BST_CAP: 16970 : 0x424a
BST_VOLT: 4294967295 : 0xffffffff
DEBUG: end acpi_battinfo
DEBUG: end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment