2024-02-23 22:56:10 -08:00
|
|
|
/*
|
|
|
|
* util.h - Utility functions
|
|
|
|
* Copyright (C) 2024 Alexander Rosenberg
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version. See the LICENSE file for more information.
|
|
|
|
*/
|
|
|
|
#ifndef INCLUDED_UTIL_H
|
|
|
|
#define INCLUDED_UTIL_H
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2024-02-25 23:59:26 -08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <libgpio.h>
|
2024-03-05 00:54:32 -08:00
|
|
|
#include <sqlite3.h>
|
2024-04-13 14:48:14 -07:00
|
|
|
#include <sys/wait.h>
|
2024-02-23 22:56:10 -08:00
|
|
|
|
2024-04-05 01:31:05 -07:00
|
|
|
#define _STRINGIFY_LIT(s) (#s)
|
|
|
|
#define STRINGIFY(v) _STRINGIFY_LIT(v)
|
|
|
|
|
2024-04-02 14:53:04 -07:00
|
|
|
typedef enum {
|
|
|
|
TEMP_UNIT_F = 'F',
|
|
|
|
TEMP_UNIT_C = 'C'
|
|
|
|
} TemperatureUnit;
|
|
|
|
|
2024-02-25 23:59:26 -08:00
|
|
|
typedef struct {
|
|
|
|
char *config_path; // path to config file
|
|
|
|
bool strict_config; // exit if unknown config options is found
|
|
|
|
bool verbose; // be more verbose
|
|
|
|
|
|
|
|
char *gpio_path; // path of GPIO device file to use
|
|
|
|
gpio_pin_t en_pin;
|
|
|
|
gpio_pin_t rs_pin;
|
|
|
|
gpio_pin_t rw_pin;
|
|
|
|
gpio_pin_t data_pins[8];
|
2024-04-02 16:17:43 -07:00
|
|
|
gpio_pin_t bl_pin;
|
2024-03-01 05:43:37 -08:00
|
|
|
char *database_location; // location of sqlite3 database
|
2024-02-25 23:59:26 -08:00
|
|
|
char *temp_key; // sysctl key for temperature
|
|
|
|
char *humid_key; // sysctl key for humidity
|
|
|
|
char *fail_key; // sysctl key for number of fails
|
|
|
|
uint32_t fail_limit; // limit for number of failures before exit
|
2024-02-28 12:09:18 -08:00
|
|
|
uint32_t refresh_time; // time between temp and humid refresh
|
2024-03-01 00:21:20 -08:00
|
|
|
|
|
|
|
// menu navigation pins
|
|
|
|
gpio_pin_t up_pin;
|
|
|
|
gpio_pin_t down_pin;
|
|
|
|
gpio_pin_t back_pin;
|
|
|
|
gpio_pin_t sel_pin;
|
2024-04-02 14:53:04 -07:00
|
|
|
|
|
|
|
TemperatureUnit temp_unit;
|
2024-04-04 00:53:29 -07:00
|
|
|
|
|
|
|
char *export_file_name; // file to export to
|
2024-04-08 01:11:15 -07:00
|
|
|
char *timezone; // default timezone
|
2024-02-25 23:59:26 -08:00
|
|
|
} Options;
|
|
|
|
extern Options GLOBAL_OPTS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup an Options struct.
|
|
|
|
*/
|
|
|
|
void cleanup_options(Options *opts);
|
2024-02-23 22:56:10 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like malloc(3), except that if allocation fails, an error will be written to
|
|
|
|
* standard error and abort(3) called
|
|
|
|
*/
|
|
|
|
void *malloc_checked(size_t n);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like realloc(3), except that if allocation fails, an error will be written to
|
|
|
|
* standard error and abort(3) called
|
|
|
|
*/
|
|
|
|
void *realloc_checked(void *old_ptr, size_t n);
|
|
|
|
|
2024-02-25 23:59:26 -08:00
|
|
|
/*
|
|
|
|
* Like strdup(3), except that if allocation fails, an error will be written to
|
|
|
|
* standard error and abort(3) called
|
|
|
|
*/
|
|
|
|
void *strdup_checked(const char *str);
|
|
|
|
|
|
|
|
/*
|
2024-04-04 00:53:29 -07:00
|
|
|
* Like asprintf(3), except that if allocation fails, an error will be written to
|
|
|
|
* standard error and abort(3) called
|
2024-02-25 23:59:26 -08:00
|
|
|
*/
|
2024-04-04 00:53:29 -07:00
|
|
|
int asprintf_checked(char **str, const char *format, ...)
|
|
|
|
__attribute__((format(printf, 2, 3)));
|
2024-02-25 23:59:26 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Call fprintf(3) to stderr only if verbose mode was enabled.
|
|
|
|
*/
|
|
|
|
#define LOG_VERBOSE(...) if (GLOBAL_OPTS.verbose) {fprintf(stderr, __VA_ARGS__);}
|
|
|
|
|
2024-03-05 00:54:32 -08:00
|
|
|
/*
|
|
|
|
* Return: the number of days in month M. 1 is January. Y is the year.
|
|
|
|
*/
|
|
|
|
int days_in_month(int m, int y);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize SQL queries used by this file.
|
|
|
|
*/
|
|
|
|
void initialize_util_queries(sqlite3 *db);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup SQL queries used by this file.
|
|
|
|
*/
|
|
|
|
void cleanup_util_queries(void);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int64_t utc;
|
|
|
|
int utc_year;
|
|
|
|
int utc_month;
|
|
|
|
int utc_day;
|
2024-03-31 17:20:07 -07:00
|
|
|
int utc_hour;
|
2024-04-01 21:14:47 -07:00
|
|
|
int utc_minute;
|
2024-04-02 00:20:35 -07:00
|
|
|
int utc_second;
|
2024-03-05 00:54:32 -08:00
|
|
|
int64_t local;
|
|
|
|
int local_year;
|
|
|
|
int local_month;
|
|
|
|
int local_day;
|
2024-03-31 17:20:07 -07:00
|
|
|
int local_hour;
|
2024-04-01 21:14:47 -07:00
|
|
|
int local_minute;
|
2024-04-02 00:20:35 -07:00
|
|
|
int local_second;
|
2024-03-05 00:54:32 -08:00
|
|
|
} UtilDate;
|
|
|
|
|
2024-03-31 17:20:07 -07:00
|
|
|
enum {
|
2024-03-31 04:21:31 -07:00
|
|
|
PERIOD_HOUR = 0,
|
|
|
|
PERIOD_DAY,
|
|
|
|
PERIOD_WEEK,
|
|
|
|
PERIOD_MONTH,
|
|
|
|
PERIOD_YEAR,
|
2024-03-31 17:20:07 -07:00
|
|
|
};
|
|
|
|
typedef int UtilPeriod;
|
2024-03-31 04:21:31 -07:00
|
|
|
extern const char *PERIOD_LABELS[];
|
|
|
|
extern const size_t NPERIOD;
|
|
|
|
|
2024-03-05 00:54:32 -08:00
|
|
|
/*
|
|
|
|
* Return the START of the first and END of the last PERIOD (ex. week) of DB.
|
2024-04-02 00:20:35 -07:00
|
|
|
* Also get the hour, minute, and second limits for the first and last day.
|
2024-03-05 00:54:32 -08:00
|
|
|
* Return: false if an error occurred, true otherwise.
|
|
|
|
*/
|
2024-04-01 21:14:47 -07:00
|
|
|
bool get_database_limits(sqlite3 *db, UtilPeriod period, UtilDate *start,
|
2024-03-05 00:54:32 -08:00
|
|
|
UtilDate *end);
|
|
|
|
|
2024-03-31 04:21:31 -07:00
|
|
|
typedef struct {
|
2024-04-02 00:20:35 -07:00
|
|
|
int64_t npoints;
|
2024-04-02 18:23:18 -07:00
|
|
|
int avgtemp;
|
|
|
|
int avghumid;
|
|
|
|
int maxtemp;
|
|
|
|
int maxhumid;
|
|
|
|
int mintemp;
|
|
|
|
int minhumid;
|
2024-03-31 04:21:31 -07:00
|
|
|
int year;
|
|
|
|
int month;
|
|
|
|
int day;
|
|
|
|
int hour;
|
|
|
|
bool lower_bound;
|
|
|
|
bool upper_bound;
|
2024-04-02 00:20:35 -07:00
|
|
|
} UtilAveragePeriod;
|
2024-03-31 04:21:31 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the average temp. and humid. between YEAR-MONTH-DAY + COUNT * PERIOD and
|
|
|
|
* YEAR-MONTH-DAY + (COUNT + 1) * PERIOD.
|
|
|
|
* Return: false if an error occurred, true otherwise.
|
|
|
|
*/
|
2024-04-02 00:20:35 -07:00
|
|
|
bool get_average_for_period(sqlite3 *db, int year, int month, int day,
|
2024-04-02 18:23:18 -07:00
|
|
|
int64_t count, UtilPeriod period,
|
|
|
|
UtilAveragePeriod *data);
|
2024-03-31 04:21:31 -07:00
|
|
|
|
2024-04-01 21:14:47 -07:00
|
|
|
typedef struct {
|
|
|
|
int64_t time;
|
|
|
|
int temp;
|
|
|
|
int humid;
|
|
|
|
bool has_next;
|
|
|
|
int64_t next_time;
|
|
|
|
int next_temp;
|
|
|
|
int next_humid;
|
|
|
|
bool has_prev;
|
|
|
|
int64_t prev_time;
|
|
|
|
int prev_temp;
|
|
|
|
int prev_humid;
|
|
|
|
} UtilDataPointInfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the data, next point, and previous point for data point TIME. INFO must
|
|
|
|
* not be NULL.
|
|
|
|
* Return: false if an error occurred, true otherwise.
|
|
|
|
*/
|
|
|
|
bool get_data_point_info(sqlite3 *db, int64_t time, UtilDataPointInfo *info);
|
|
|
|
|
2024-04-02 00:20:35 -07:00
|
|
|
typedef struct {
|
|
|
|
int64_t npoints;
|
2024-04-02 18:23:18 -07:00
|
|
|
int avgtemp;
|
|
|
|
int avghumid;
|
|
|
|
int maxtemp;
|
|
|
|
int mintemp;
|
|
|
|
int maxhumid;
|
|
|
|
int minhumid;
|
2024-04-02 00:20:35 -07:00
|
|
|
} UtilAverageRange;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the average DATA for the range of UTC times START to END (inclusive).
|
|
|
|
* Return: false if an error occurred, true otherwise.
|
|
|
|
*/
|
2024-04-05 04:16:42 -07:00
|
|
|
bool get_average_for_range(sqlite3 *db, int64_t start, int64_t end,
|
2024-04-02 00:20:35 -07:00
|
|
|
UtilAverageRange *data);
|
|
|
|
|
2024-04-02 14:53:04 -07:00
|
|
|
/*
|
|
|
|
* Convert the temperature in deciKelvin DK to either F or C.
|
|
|
|
*/
|
|
|
|
float convert_temperature(int dk);
|
|
|
|
|
2024-04-02 18:23:18 -07:00
|
|
|
/*
|
|
|
|
* Fill BUF with at most BUF_SIZE characters (including the null byte), by
|
|
|
|
* padding "HUMID%" to BUF_SIZE - 1 characters.
|
|
|
|
*/
|
|
|
|
char *pad_humid_str(int humid, char *buf, size_t buf_size);
|
|
|
|
|
2024-04-04 00:53:29 -07:00
|
|
|
/*
|
|
|
|
* Like recursive_rm (see below), except that PATH is resolved relative to DFD.
|
|
|
|
*/
|
|
|
|
bool recursive_rm_at(int dfd, const char *path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call recursive_rm on all of FILE's children, the remove FILE
|
|
|
|
* Return: false on error, true otherwise
|
|
|
|
*/
|
|
|
|
bool recursive_rm(const char *path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the sqlite3 backup API to export DB to dest. This function is safe to
|
|
|
|
* call in a multi-threaded environment where pthread_cancel may be used.
|
|
|
|
* Return: true on success, false on error
|
|
|
|
*/
|
|
|
|
bool export_database(sqlite3 *db, const char *dest);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export DB as a CSV file to DEST. This function is safe to call in a
|
|
|
|
* multi-threaded environment where pthread_cancel may be used.
|
|
|
|
* Return: true on success, false on error
|
|
|
|
*/
|
|
|
|
bool export_database_as_csv(sqlite3 *db, const char *dest);
|
|
|
|
|
2024-04-05 01:31:05 -07:00
|
|
|
/*
|
|
|
|
* Create all the missing directories along path.
|
|
|
|
* Return: true on success, false otherwise
|
|
|
|
*/
|
|
|
|
bool mkdirs(const char *path, mode_t mode);
|
|
|
|
|
2024-04-08 01:11:15 -07:00
|
|
|
/*
|
|
|
|
* Request that this program re-invoke itself with the same arguments. Used to
|
|
|
|
* re-initialize configuration values.
|
|
|
|
*/
|
|
|
|
void request_restart(void);
|
2024-04-13 14:48:14 -07:00
|
|
|
/*
|
|
|
|
* waitpid(2) for PID for MICRO microseconds. If STATUS is not NULL, store the
|
|
|
|
* exit status there. Use the various WIF* macros to test the status. This
|
|
|
|
* function sets errno to the errno value of waitpid(2) on error.
|
|
|
|
* Return: true if successful, false otherwise and the error is in errno
|
|
|
|
*/
|
|
|
|
bool timed_wait(pid_t pid, int *out_status, useconds_t micro);
|
2024-04-08 01:11:15 -07:00
|
|
|
|
2024-02-23 22:56:10 -08:00
|
|
|
#endif
|