/*- * Copyright (c) 2005 Ariff Abdullah * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ * * acpibif(8): Print ACPI battery status information. */ #include #include #include #include #include #include #include #include #include #define ACPIDEV "/dev/acpi" /* Shamelessly ripped from acpiconf.c . */ static void acpi_battinfo(int acpifd, int num) { union acpi_battery_ioctl_arg battio; struct acpi_battinfo battinfo; struct acpi_bif bif; struct acpi_bst bst; const char *pwr_units; int hours, min; if (num < 0 || num > 64) { warnx("invalid battery %d", num); return; } /* Print battery design information. */ battio.unit = num; if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &battio) == -1) { warn("get battery info (%d) failed", num); return; } bif = battio.bif; if (bif.units == 0) pwr_units = "mW"; else pwr_units = "mA"; if (bif.dcap == ACPI_BATT_UNKNOWN) printf("Design capacity:\tunknown\n"); else printf("Design capacity:\t%d %sh\n", bif.dcap, pwr_units); if (bif.lfcap == ACPI_BATT_UNKNOWN) printf("Last full capacity:\tunknown\n"); else printf("Last full capacity:\t%d %sh\n", bif.lfcap, pwr_units); printf("Technology:\t\t%s\n", bif.btech == 0 ? "primary (non-rechargeable)" : "secondary (rechargeable)"); if (bif.dvol == ACPI_BATT_UNKNOWN) printf("Design voltage:\t\tunknown\n"); else printf("Design voltage:\t\t%d mV\n", bif.dvol); printf("Capacity (warn):\t%d %sh\n", bif.wcap, pwr_units); printf("Capacity (low):\t\t%d %sh\n", bif.lcap, pwr_units); printf("Low/warn granularity:\t%d %sh\n", bif.gra1, pwr_units); printf("Warn/full granularity:\t%d %sh\n", bif.gra2, pwr_units); printf("Model number:\t\t%s\n", bif.model); printf("Serial number:\t\t%s\n", bif.serial); printf("Type:\t\t\t%s\n", bif.type); printf("OEM info:\t\t%s\n", bif.oeminfo); /* Print current battery state information. */ battio.unit = num; if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) == -1) { warn("get battery user info (%d) failed", num); return; } battinfo = battio.battinfo; if (battinfo.state != ACPI_BATT_STAT_NOT_PRESENT) { printf("State:\t\t\t"); if (battinfo.state == 0) printf("high "); if (battinfo.state & ACPI_BATT_STAT_CRITICAL) printf("critical "); if (battinfo.state & ACPI_BATT_STAT_DISCHARG) printf("discharging "); if (battinfo.state & ACPI_BATT_STAT_CHARGING) printf("charging "); printf("\n"); if (battinfo.cap == -1) printf("Remaining capacity:\tunknown\n"); else printf("Remaining capacity:\t%d%%\n", battinfo.cap); if (battinfo.min == -1) printf("Remaining time:\t\tunknown\n"); else { hours = battinfo.min / 60; min = battinfo.min % 60; printf("Remaining time:\t\t%d:%02d\n", hours, min); } if (battinfo.rate == -1) printf("Present rate:\t\tunknown\n"); else printf("Present rate:\t\t%d %s\n", battinfo.rate, pwr_units); } /* Print battery voltage information. */ battio.unit = num; if (ioctl(acpifd, ACPIIO_BATT_GET_BST, &battio) == -1) { warn("get battery status (%d) failed", num); return; } bst = battio.bst; if (bst.state != ACPI_BATT_STAT_NOT_PRESENT) { if (battinfo.state == ACPI_BATT_STAT_NOT_PRESENT) { printf("State:\t\t\t"); if (bst.state == 0) printf("high "); if (bst.state & ACPI_BATT_STAT_CRITICAL) printf("critical "); if (bst.state & ACPI_BATT_STAT_DISCHARG) printf("discharging "); if (bst.state & ACPI_BATT_STAT_CHARGING) printf("charging "); printf("\n"); if (bst.rate == ACPI_BATT_UNKNOWN) printf("Present rate:\t\tunknown\n"); else printf("Present rate:\t\t%d %s\n", bst.rate, pwr_units); if (bst.cap == ACPI_BATT_UNKNOWN || bif.lfcap == ACPI_BATT_UNKNOWN) printf("Remaining capacity:\tunknown\n"); else printf("Remaining capacity:\t%d%%\n", (100 * bst.cap) / bif.lfcap); } if (bst.volt == ACPI_BATT_UNKNOWN) printf("Voltage:\t\tunknown\n"); else printf("Voltage:\t\t%d mV\n", bst.volt); } if (battinfo.state == ACPI_BATT_STAT_NOT_PRESENT && bst.state == ACPI_BATT_STAT_NOT_PRESENT) printf("State:\t\t\tnot present\n"); return; } static void usage(const char* prog) { fprintf(stderr, "usage: %s [-h] [-i batt]\n", prog); exit(EX_USAGE); } int main(int argc, char *argv[]) { int acpifd, acline; char c, *prog; prog = argv[0]; if (argc < 2) usage(prog); acpifd = open(ACPIDEV, O_RDWR); if (acpifd == -1) acpifd = open(ACPIDEV, O_RDONLY); if (acpifd == -1) err(EX_OSFILE, ACPIDEV); while ((c = getopt(argc, argv, "hi:")) != -1) { switch (c) { case 'i': acpi_battinfo(acpifd, atoi(optarg)); break; case 'h': default: usage(prog); } } argc -= optind; argv += optind; if (ioctl(acpifd, ACPIIO_ACAD_GET_STATUS, &acline) == -1) warn("get ac status failed"); else printf("AC line:\t\t%s-line\n", (acline == 0) ? "off" : "on"); close(acpifd); exit(EX_OK); }