libacpi(3)

πŸ’– libacpi(3) πŸ’–

okay bestie, welcome to libacpi β€” it's a Library Functions Manual situation and it is the general purpose library for ACPI. she's iconic, she's tiny, she's literally gonna tell you your battery percentage. let's get into it πŸ¦‹


✨ NAME ✨

libacpi β€” general purpose library for ACPI. that's it, that's the girl.


🌸 SYNOPSIS 🌸

#include <libacpi.h>

πŸŽ€ DESCRIPTION πŸŽ€

so the libacpi library routines give you a way to read ACPI values in a comfortable way. comfortable!! like a lil weighted blanket for your sysfs. that includes:

  • battery information
  • fan states
  • thermal zones
  • AC states

a program using these routines must be linked with the -lacpi option. don't forget it, she will not link herself πŸ’…

to initialize this functionality you have to first allocate space for a global_t structure:

global_t *global = malloc (sizeof (global_t));

now use the check_acpi_support() function to check if the system actually supports ACPI. in case it does, the function will return SUCCESS, or NOT_SUPPORTED on failure. vibe check complete ✨

now you can use the following functions to initialize the values you get from the ACPI interface:

init_acpi_batt(global);
init_acpi_acadapt(global);
init_acpi_fan(global);
init_acpi_thermal(global);

to know if some of the features is not supported, it is a good idea to store the return values in some variable. lowkey essential, ngl.

if you want to read non-static values the ACPI interface provides (for example battery percentage), use the following functions to do this:

read_acpi_batt(int num)
read_acpi_zone(int num, global_t *globals);
read_acpi_fan(int num)
read_acpi_acstate(global_t *globals);

since you can have more than one battery, fan and thermal zone, num is the number you want to read, for example in a loop:

thermal_t *tp;
....
for(i=0; i<global->thermal_count; i++){
    read_acpi_zone(i, global);
    tp = &thermals[i];
...
}

🧚 Structures

  • struct fan_t β€” fan data
  • struct battery_t β€” information found about battery
  • struct thermal_t β€” information about thermal zone
  • struct adapter_t β€” information about ac adapter
  • struct global_t β€” global acpi structure

πŸ¦‹ Functions

int init_acpi_batt (global_t *globals)
int init_acpi_acadapt (global_t *globals)
int init_acpi_thermal (global_t *globals)
int init_acpi_fan (global_t *globals)
int check_acpi_support (void)
int read_acpi_batt (const int num)
void read_acpi_acstate (global_t *globals)
int read_acpi_zone (const int num, global_t *globals)
int read_acpi_fan (const int num)

πŸ“ Variables

battery_t batteries [MAX_ITEMS]
thermal_t thermals [MAX_ITEMS]
fan_t fans [MAX_ITEMS]

πŸ’œ Enumerations

enum { ITEM_EXCEED =  -5, DISABLED =  -4, NOT_PRESENT =  -3, ALLOC_ERR =
    -2, NOT_SUPPORTED =  -1, SUCCESS }
enum power_state_t { P_AC, P_BATT, P_ERR }
enum thermal_state_t { T_CRIT, T_HOT, T_PASS, T_ACT, T_OK, T_ERR }
enum charge_state_t { C_CHARGE, C_DISCHARGE, C_CHARGED, C_NOINFO, C_ERR
    }
enum batt_state_t { B_HIGH, B_MED, B_LOW, B_CRIT, B_HARD_CRIT, B_ERR }
enum thermal_mode_t { CO_ACT, CO_PASS, CO_CRIT, CO_ERR }
enum fan_state_t { F_ON, F_OFF, F_ERR }

🌈 Detailed Description 🌈

now follows a more detailed description of functions, structures and enums. grab a snack, we're going deep 🫢

Definition in file libacpi.h.


πŸ‘‘ Function Documentation πŸ‘‘

int check_acpi_support (void)

checks if the system does support ACPI or not. the ultimate "do you even go here" moment.

Returns:

  • SUCCESS if the system supports ACPI or, NOT_SUPPORTED.

int init_acpi_acadapt (global_t * globals)

finds existing ac adapter and fills the adapt (in globa struct) structure with the paths of the important files to parse. (yes the original says "globa", she's quirky like that, we keep it πŸ’€)

Parameters:

  • globals pointer to global acpi structure

Returns:

  • SUCCESS if everything is ok, NOT_SUPPORTED if no information can be found, ALLOC_ERR on allocation errors.

int init_acpi_batt (global_t * globals)

finds existing batteries and fills the corresponding batteries structures with the paths of the important files to parse.

Parameters:

  • globals pointer to global acpi structure

Returns:

  • SUCCESS if everything is ok, NOT_SUPPORTED if no information can be found, ALLOC_ERR on allocation errors and ITEM_EXCEED if there are more batteries found than MAX_ITEMS, this should never happen.

int init_acpi_fan (global_t * globals)

finds existing fans and fills corresponding fan structures with the paths of the important files to parse for fan information.

Parameters:

  • globals pointer to global acpi structure

Returns:

  • SUCCESS if everything is ok, NOT_SUPPORTED if no information can be found, ALLOC_ERR on allocation errors and ITEM_EXCEED if there are more fans found than MAX_ITEMS, this should never happen.

int init_acpi_thermal (global_t * globals)

finds existing thermal zones and fills corresponding thermal structures with the paths of the important files to parse for thermal information.

Parameters:

  • globals pointer to global acpi structure

Returns:

  • SUCCESS if everything is ok, NOT_SUPPORTED if no information can be found, ALLOC_ERR on allocation errors and ITEM_EXCEED if there are more thermal zones found than MAX_ITEMS, this should never happen.

void read_acpi_acstate (global_t * globals)

looks up if the ac adapter is plugged in or not and sets the values in a struct. are you charging or are you free? πŸ”Œ

Parameters:

  • globals pointer to the global acpi structure

Referenced by init_acpi_acadapt().


int read_acpi_batt (const int num)

gathers all information of a given battery and filling a struct with it.

Parameters:

  • num number of battery

Returns:

  • SUCCESS if everything is ok or ITEM_EXCEED if there is not battery num.

int read_acpi_fan (const int num)

gathers all information about given fan and sets the corresponding values in a struct.

Parameters:

  • num number for the fan to read

Returns:

  • SUCCESS if everything is ok, ITEM_EXCEED if there is not fan num or NOT_SUPPORTED if the values can't be read. this should not happen if the init function returned SUCCESS if the ACPI implementation.

int read_acpi_zone (const int num, global_t * globals)

gathers all information of a given thermal zone and sets the corresponding values in a struct.

Parameters:

  • num zone
  • globals pointer to global acpi struct, needed if there is just one zone

Returns:

  • SUCCESS if everything is ok, ITEM_EXCEED if there is not thermal zone num or NOT_SUPPORTED if the values can't be read. this should not happen if the init function returned SUCCESS if the ACPI implementation.

πŸ’… Enumeration Type Documentation πŸ’…

possible function return values

Enumerator:

  • ITEM_EXCEED β€” maximum item count reached
  • DISABLED β€” features is disabled
  • NOT_PRESENT β€” something is not present
  • ALLOC_ERR β€” an error occurred while allocating space
  • NOT_SUPPORTED β€” a feature is not supported
  • SUCCESS β€” everything was fine

enum batt_state_t

Enumerator:

  • B_HIGH β€” remaining battery life is high
  • B_MED β€” remaining battery life is medium
  • B_LOW β€” remaining battery life is low
  • B_CRIT β€” remaining battery life is critical
  • B_HARD_CRIT β€” remaining battery life is hard critical, you have a few minutes to charge
  • B_ERR β€” some error occurred while reading the battery state
bestie if you see B_HARD_CRIT go find a charger. like now. πŸͺ«πŸ˜­

enum charge_state_t

Enumerator:

  • C_CHARGE β€” battery is currently charging
  • C_DISCHARGE β€” battery is currently discharging
  • C_CHARGED β€” battery is charged
  • C_NOINFO β€” hardware doesn't give information about the state
  • C_ERR β€” some error occurred while reading the charge state

enum fan_state_t

fan states

Enumerator:

  • F_ON β€” fan is on
  • F_OFF β€” fan is off
  • F_ERR β€” some error occurred with this fan

enum power_state_t

Enumerator:

  • P_AC β€” if computer runs on AC
  • P_BATT β€” if computer runs on battery
  • P_ERR β€” no information can be found

enum thermal_mode_t

Enumerator:

  • CO_ACT β€” fans will be turned after the temperature passes a critical point
  • CO_PASS β€” devices will be put in a lower power state after a critical point
  • CO_CRIT β€” system goes into suspend to disk if possible after a critical temperature
  • CO_ERR β€” some error occurred while reading the cooling mode

enum thermal_state_t

Enumerator:

  • T_CRIT β€” zone reports critical temperature, will cause system to go to S4
  • T_HOT β€” zone reports high temperature, will cause system to shutdown immediately
  • T_PASS β€” zone is on passive cooling
  • T_ACT β€” zone is on active cooling, more power consumption
  • T_OK β€” zone is ok
  • T_ERR β€” some error occurred while reading the state of the zone

⭐ Variable Documentation ⭐

battery_t batteries[MAX_ITEMS]

array for existing batteries, loop until globals->battery_count

fan_t fans[MAX_ITEMS]

array for existing fans, loop until globals->fan_count

thermal_t thermals[MAX_ITEMS]

array for existing thermal zones, loop until globals->thermal_count


πŸ”₯ Additional information πŸ”₯

the package ships a test-libacpi.c file which should be pretty self explanatory. also have a look into libacpi.h file and the doxygen documentation in the doc/ directory. iykyk πŸ“šβœ¨


🌐 Homepage 🌐

see http://www.ngolde.de/libacpi.html


🫢 Author 🫢

write to nico (at) ngolde (dot) de for suggestions, hardware donations (very welcome, my notebook doesn't support all features and I could need a second battery), questions, fixes, bug reports, thanks, etc. :) β€” yes that's a direct quote and yes he's literally asking for a second battery, respect the hustle πŸ’–


that's the whole page bestie, go read your battery like a main character πŸ‘‘

LIBACPI Β· libacpi(3) β€” mwah, no notes, periodt πŸ’…βœ¨