Files
linuxSmartie/include/utils.h
2026-08-14 18:00:02 +02:00

61 lines
1.4 KiB
C

#ifndef LINUXSMARTIE_UTILS_H
#define LINUXSMARTIE_UTILS_H
#include <stdint.h>
#include <sys/types.h> // NOLINT yes its, needed for ssize_t
#if __STDC_VERSION__ >= 202000L
#define C_STD "C23"
#elif __STDC_VERSION__ >= 201710L
#define C_STD "C17"
#elif __STDC_VERSION__ >= 201112L
#define C_STD "C11"
#elif __STDC_VERSION__ >= 199901L
#define C_STD "C99"
#else
#define C_STD "C89"
#endif
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define P_BYTES(bytes, start) pretty_bytes((bytes), (start), (char[16]){0}, 16)
#define P_NUM(num) pretty_num((num), (char[16]){0}, 16)
typedef enum : uint8_t {
SENSOR_I16,
SENSOR_U8,
SENSOR_U16,
SENSOR_U64,
SENSOR_FLOAT,
} sensor_type_t;
typedef struct {
sensor_type_t type;
int fd;
void* value;
uint32_t divider;
} sensor_file_t;
int get_instance_lock();
void timespec_add_ms(struct timespec *ts, uint16_t ms);
int64_t timespec_diff_us(const struct timespec *a, const struct timespec *b);
char *pretty_num(double num, char *buf, size_t bufsz);
char *pretty_bytes(uint64_t bytes, int8_t start_suffix, char *buf, size_t bufsz);
int get_hwmon_dir(char *path, size_t bufsz);
void open_sensor(sensor_file_t* s, const char* dir, const char *file, void* value, uint32_t divider, sensor_type_t type);
void fast_read_sensor_into_mem(const sensor_file_t *s, void* val);
void fast_read_sensor(const sensor_file_t *s);
void close_sensor(const sensor_file_t* s);
#endif