Skip to content

Commit aa6a5ba

Browse files
committed
Preparing a static way to save the app configuration, fixing inconsistent use of path array loops
1 parent 030dfb7 commit aa6a5ba

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

src/app_config.c

+104
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,110 @@
77

88
struct AppConfig app_config;
99

10+
static inline void *open_app_config(FILE **file, const char *flags) {
11+
const char *paths[] = {"./divinus.yaml", "/etc/divinus.yaml"};
12+
char **path = paths;
13+
*file = NULL;
14+
15+
while (*path) {
16+
if (access(*path++, F_OK)) continue;
17+
if (*flags == 'w') {
18+
remove("./divinus.yaml.bak");
19+
rename("./divinus.yaml", "./divinus.yaml.bak");
20+
}
21+
*file = fopen(*path, flags);
22+
break;
23+
}
24+
return *file;
25+
}
26+
27+
int save_app_config(void) {
28+
FILE *file;
29+
30+
open_app_config(&file, "w");
31+
if (!file)
32+
HAL_ERROR("app_config", "Can't open config file for writing\n");
33+
34+
fputs(file, "system:\n");
35+
fprintf(file, " sensor_config: %s\n", app_config.sensor_config);
36+
fprintf(file, " web_port: %d\n", app_config.web_port);
37+
fprintf(file, " web_enable_auth: %s\n", app_config.web_enable_auth ? "true" : "false");
38+
fprintf(file, " web_auth_user: %s\n", app_config.web_auth_user);
39+
fprintf(file, " web_auth_pass: %s\n", app_config.web_auth_pass);
40+
fprintf(file, " web_enable_static: %s\n", app_config.web_enable_static ? "true" : "false");
41+
fprintf(file, " isp_thread_stack_size: %d\n", app_config.isp_thread_stack_size);
42+
fprintf(file, " venc_stream_thread_stack_size: %d\n", app_config.venc_stream_thread_stack_size);
43+
fprintf(file, " web_server_thread_stack_size: %d\n", app_config.web_server_thread_stack_size);
44+
fprintf(file, " watchdog: %d\n", app_config.watchdog);
45+
46+
fputs(file, "night_mode:\n");
47+
fprintf(file, " enable: %s\n", app_config.night_mode_enable ? "true" : "false");
48+
fprintf(file, " ir_sensor_pin: %d\n", app_config.ir_sensor_pin);
49+
fprintf(file, " check_interval_s: %d\n", app_config.check_interval_s);
50+
fprintf(file, " ir_cut_pin1: %d\n", app_config.ir_cut_pin1);
51+
fprintf(file, " ir_cut_pin2: %d\n", app_config.ir_cut_pin2);
52+
fprintf(file, " pin_switch_delay_us: %d\n", app_config.pin_switch_delay_us);
53+
fprintf(file, " adc_device: %s\n", app_config.adc_device);
54+
fprintf(file, " adc_threshold: %d\n", app_config.adc_threshold);
55+
56+
fputs(file, "isp:\n");
57+
fprintf(file, " mirror: %s\n", app_config.mirror ? "true" : "false");
58+
fprintf(file, " flip: %s\n", app_config.flip ? "true" : "false");
59+
fprintf(file, " antiflicker: %d\n", app_config.antiflicker);
60+
61+
fputs(file, "rtsp:\n");
62+
fprintf(file, " enable: %s\n", app_config.rtsp_enable ? "true" : "false");
63+
64+
fputs(file, "mdns:\n");
65+
fprintf(file, " enable: %s\n", app_config.mdns_enable ? "true" : "false");
66+
67+
fputs(file, "audio:\n");
68+
fprintf(file, " enable: %s\n", app_config.audio_enable ? "true" : "false");
69+
fprintf(file, " bitrate: %d\n", app_config.audio_bitrate);
70+
fprintf(file, " srate: %d\n", app_config.audio_srate);
71+
72+
fputs(file, "mp4:\n");
73+
fprintf(file, " enable: %s\n", app_config.mp4_enable ? "true" : "false");
74+
fprintf(file, " codec: %s\n", app_config.mp4_codecH265 ? "H.265" : "H.264");
75+
fprintf(file, " mode: %d\n", app_config.mp4_mode);
76+
fprintf(file, " width: %d\n", app_config.mp4_width);
77+
fprintf(file, " height: %d\n", app_config.mp4_height);
78+
fprintf(file, " fps: %d\n", app_config.mp4_fps);
79+
fprintf(file, " profile: %s\n", app_config.mp4_profile);
80+
fprintf(file, " bitrate: %d\n", app_config.mp4_bitrate);
81+
82+
fputs(file, "osd:\n");
83+
fprintf(file, " enable: %s\n", app_config.osd_enable ? "true" : "false");
84+
85+
fputs(file, "jpeg:\n");
86+
fprintf(file, " enable: %s\n", app_config.jpeg_enable ? "true" : "false");
87+
fprintf(file, " width: %d\n", app_config.jpeg_width);
88+
fprintf(file, " height: %d\n", app_config.jpeg_height);
89+
fprintf(file, " qfactor: %d\n", app_config.jpeg_qfactor);
90+
91+
fputs(file, "mjpeg:\n");
92+
fprintf(file, " enable: %s\n", app_config.mjpeg_enable ? "true" : "false");
93+
fprintf(file, " mode: %d\n", app_config.mjpeg_mode);
94+
fprintf(file, " width: %d\n", app_config.mjpeg_width);
95+
fprintf(file, " height: %d\n", app_config.mjpeg_height);
96+
fprintf(file, " fps: %d\n", app_config.mjpeg_fps);
97+
fprintf(file, " bitrate: %d\n", app_config.mjpeg_bitrate);
98+
99+
fputs(file, "http_post:\n");
100+
fprintf(file, " enable: %s\n", app_config.http_post_enable ? "true" : "false");
101+
fprintf(file, " host: %s\n", app_config.http_post_host);
102+
fprintf(file, " url: %s\n", app_config.http_post_url);
103+
fprintf(file, " login: %s\n", app_config.http_post_login);
104+
fprintf(file, " password: %s\n", app_config.http_post_password);
105+
fprintf(file, " width: %d\n", app_config.http_post_width);
106+
fprintf(file, " height: %d\n", app_config.http_post_height);
107+
fprintf(file, " interval: %d\n", app_config.http_post_interval);
108+
fprintf(file, " qfactor: %d\n", app_config.http_post_qfactor);
109+
110+
fclose(file);
111+
return EXIT_SUCCESS;
112+
}
113+
10114
enum ConfigError parse_app_config(void) {
11115
memset(&app_config, 0, sizeof(struct AppConfig));
12116

src/gpio.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "gpio.h"
22

33
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 8, 0)
4-
const char *paths[] = {"/dev/gpiochip0", "/sys/class/gpio/gpiochip0"};
5-
const char **path = paths;
6-
74
int fd_gpio = 0;
85

96
char gpio_count = 0;
@@ -15,6 +12,8 @@ void gpio_deinit(void) {
1512
}
1613

1714
int gpio_init(void) {
15+
const char *paths[] = {"/dev/gpiochip0", "/sys/class/gpio/gpiochip0"};
16+
char **path = paths;
1817
while (*path) {
1918
if (access(*path++, F_OK)) continue;
2019
if ((fd_gpio = open(*(path - 1), O_RDWR)) < 0)

src/watchdog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void watchdog_reset(void) {
99

1010
int watchdog_start(int timeout) {
1111
if (fd) return EXIT_SUCCESS;
12-
char* paths[] = {"/dev/watchdog0", "/dev/watchdog"};
12+
const char* paths[] = {"/dev/watchdog0", "/dev/watchdog"};
1313
char **path = paths;
1414

1515
while (*path) {

0 commit comments

Comments
 (0)