Skip to content

Commit

Permalink
Preparing a static way to save the app configuration, fixing inconsis…
Browse files Browse the repository at this point in the history
…tent use of path array loops
  • Loading branch information
wberube committed Aug 16, 2024
1 parent 030dfb7 commit aa6a5ba
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
104 changes: 104 additions & 0 deletions src/app_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,110 @@

struct AppConfig app_config;

static inline void *open_app_config(FILE **file, const char *flags) {
const char *paths[] = {"./divinus.yaml", "/etc/divinus.yaml"};
char **path = paths;
*file = NULL;

while (*path) {
if (access(*path++, F_OK)) continue;
if (*flags == 'w') {
remove("./divinus.yaml.bak");
rename("./divinus.yaml", "./divinus.yaml.bak");
}
*file = fopen(*path, flags);
break;
}
return *file;
}

int save_app_config(void) {
FILE *file;

open_app_config(&file, "w");
if (!file)
HAL_ERROR("app_config", "Can't open config file for writing\n");

fputs(file, "system:\n");
fprintf(file, " sensor_config: %s\n", app_config.sensor_config);
fprintf(file, " web_port: %d\n", app_config.web_port);
fprintf(file, " web_enable_auth: %s\n", app_config.web_enable_auth ? "true" : "false");
fprintf(file, " web_auth_user: %s\n", app_config.web_auth_user);
fprintf(file, " web_auth_pass: %s\n", app_config.web_auth_pass);
fprintf(file, " web_enable_static: %s\n", app_config.web_enable_static ? "true" : "false");
fprintf(file, " isp_thread_stack_size: %d\n", app_config.isp_thread_stack_size);
fprintf(file, " venc_stream_thread_stack_size: %d\n", app_config.venc_stream_thread_stack_size);
fprintf(file, " web_server_thread_stack_size: %d\n", app_config.web_server_thread_stack_size);
fprintf(file, " watchdog: %d\n", app_config.watchdog);

fputs(file, "night_mode:\n");
fprintf(file, " enable: %s\n", app_config.night_mode_enable ? "true" : "false");
fprintf(file, " ir_sensor_pin: %d\n", app_config.ir_sensor_pin);
fprintf(file, " check_interval_s: %d\n", app_config.check_interval_s);
fprintf(file, " ir_cut_pin1: %d\n", app_config.ir_cut_pin1);
fprintf(file, " ir_cut_pin2: %d\n", app_config.ir_cut_pin2);
fprintf(file, " pin_switch_delay_us: %d\n", app_config.pin_switch_delay_us);
fprintf(file, " adc_device: %s\n", app_config.adc_device);
fprintf(file, " adc_threshold: %d\n", app_config.adc_threshold);

fputs(file, "isp:\n");
fprintf(file, " mirror: %s\n", app_config.mirror ? "true" : "false");
fprintf(file, " flip: %s\n", app_config.flip ? "true" : "false");
fprintf(file, " antiflicker: %d\n", app_config.antiflicker);

fputs(file, "rtsp:\n");
fprintf(file, " enable: %s\n", app_config.rtsp_enable ? "true" : "false");

fputs(file, "mdns:\n");
fprintf(file, " enable: %s\n", app_config.mdns_enable ? "true" : "false");

fputs(file, "audio:\n");
fprintf(file, " enable: %s\n", app_config.audio_enable ? "true" : "false");
fprintf(file, " bitrate: %d\n", app_config.audio_bitrate);
fprintf(file, " srate: %d\n", app_config.audio_srate);

fputs(file, "mp4:\n");
fprintf(file, " enable: %s\n", app_config.mp4_enable ? "true" : "false");
fprintf(file, " codec: %s\n", app_config.mp4_codecH265 ? "H.265" : "H.264");
fprintf(file, " mode: %d\n", app_config.mp4_mode);
fprintf(file, " width: %d\n", app_config.mp4_width);
fprintf(file, " height: %d\n", app_config.mp4_height);
fprintf(file, " fps: %d\n", app_config.mp4_fps);
fprintf(file, " profile: %s\n", app_config.mp4_profile);
fprintf(file, " bitrate: %d\n", app_config.mp4_bitrate);

fputs(file, "osd:\n");
fprintf(file, " enable: %s\n", app_config.osd_enable ? "true" : "false");

fputs(file, "jpeg:\n");
fprintf(file, " enable: %s\n", app_config.jpeg_enable ? "true" : "false");
fprintf(file, " width: %d\n", app_config.jpeg_width);
fprintf(file, " height: %d\n", app_config.jpeg_height);
fprintf(file, " qfactor: %d\n", app_config.jpeg_qfactor);

fputs(file, "mjpeg:\n");
fprintf(file, " enable: %s\n", app_config.mjpeg_enable ? "true" : "false");
fprintf(file, " mode: %d\n", app_config.mjpeg_mode);
fprintf(file, " width: %d\n", app_config.mjpeg_width);
fprintf(file, " height: %d\n", app_config.mjpeg_height);
fprintf(file, " fps: %d\n", app_config.mjpeg_fps);
fprintf(file, " bitrate: %d\n", app_config.mjpeg_bitrate);

fputs(file, "http_post:\n");
fprintf(file, " enable: %s\n", app_config.http_post_enable ? "true" : "false");
fprintf(file, " host: %s\n", app_config.http_post_host);
fprintf(file, " url: %s\n", app_config.http_post_url);
fprintf(file, " login: %s\n", app_config.http_post_login);
fprintf(file, " password: %s\n", app_config.http_post_password);
fprintf(file, " width: %d\n", app_config.http_post_width);
fprintf(file, " height: %d\n", app_config.http_post_height);
fprintf(file, " interval: %d\n", app_config.http_post_interval);
fprintf(file, " qfactor: %d\n", app_config.http_post_qfactor);

fclose(file);
return EXIT_SUCCESS;
}

enum ConfigError parse_app_config(void) {
memset(&app_config, 0, sizeof(struct AppConfig));

Expand Down
5 changes: 2 additions & 3 deletions src/gpio.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "gpio.h"

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
const char *paths[] = {"/dev/gpiochip0", "/sys/class/gpio/gpiochip0"};
const char **path = paths;

int fd_gpio = 0;

char gpio_count = 0;
Expand All @@ -15,6 +12,8 @@ void gpio_deinit(void) {
}

int gpio_init(void) {
const char *paths[] = {"/dev/gpiochip0", "/sys/class/gpio/gpiochip0"};
char **path = paths;
while (*path) {
if (access(*path++, F_OK)) continue;
if ((fd_gpio = open(*(path - 1), O_RDWR)) < 0)
Expand Down
2 changes: 1 addition & 1 deletion src/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void watchdog_reset(void) {

int watchdog_start(int timeout) {
if (fd) return EXIT_SUCCESS;
char* paths[] = {"/dev/watchdog0", "/dev/watchdog"};
const char* paths[] = {"/dev/watchdog0", "/dev/watchdog"};
char **path = paths;

while (*path) {
Expand Down

0 comments on commit aa6a5ba

Please sign in to comment.