Skip to content

Commit

Permalink
Merge pull request #10 from OpenIPC/config
Browse files Browse the repository at this point in the history
Add yaml compatibility
  • Loading branch information
wberube authored May 31, 2024
2 parents 21af12b + eda5b7e commit 62d74ca
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 193 deletions.
72 changes: 0 additions & 72 deletions divinus.ini

This file was deleted.

68 changes: 68 additions & 0 deletions divinus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
system:
sensor_config: /etc/sensors/imx415.bin
web_port: 80
web_enable_static: false
isp_thread_stack_size: 16384
venc_stream_thread_stack_size: 16384
web_server_thread_stack_size: 65536

isp:
mirror: false
flip: false

osd:
enable: true

night_mode:
enable: false
ir_sensor_pin: 62
check_interval_s: 10
ir_cut_pin1: 1
ir_cut_pin2: 2
pin_switch_delay_us: 150

record:
enable: false
path: /sdcard/records
file_duration: 10
width: 3840
height: 2160
fps: 20
bitrate: 1024
profile: 2

http_post:
enable: false
host: <your host>
url: /~example/000000000000/%Y/%m/%d/%H.%M.jpg
width: 640
height: 360
qfactor: 90
interval: 60
login: <your login>
password: <yout pass>

rtsp:
enable: false

mp4:
enable: false
codec: H265
width: 3840
height: 2160
fps: 20
bitrate: 1024
profile: 2

jpeg:
enable: false
width: 1920
height: 1080
qfactor: 70

mjpeg:
enable: true
width: 3840
height: 2160
fps: 20
bitrate: 1024
44 changes: 7 additions & 37 deletions src/app_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

struct AppConfig app_config;

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

app_config.sensor_config[0] = 0;
Expand Down Expand Up @@ -43,40 +43,11 @@ enum ConfigError parse_app_config(const char *path) {
struct IniConfig ini;
memset(&ini, 0, sizeof(struct IniConfig));

// load config file to string
ini.str = NULL;
{
char config_path[50];
FILE *file = fopen("./divinus.ini", "rb");
if (!file) {
file = fopen("/etc/divinus.ini", "rb");
if (!file) {
printf(
"Can't find config divinus.ini in:\n"
" ./divinus.ini\n /etc/divinus.ini\n");
return -1;
}
}

fseek(file, 0, SEEK_END);
size_t length = (size_t)ftell(file);
fseek(file, 0, SEEK_SET);

ini.str = malloc(length + 1);
if (!ini.str) {
printf("Can't allocate buf in parse_app_config\n");
fclose(file);
return -1;
}
size_t n = fread(ini.str, 1, length, file);
if (n != length) {
printf("Can't read all file %s\n", path);
fclose(file);
free(ini.str);
return -1;
}
fclose(file);
ini.str[length] = 0;
if (!open_config(&ini, "./divinus.yaml") &&
!open_config(&ini, "/etc/divinus.yaml")) {
printf("Can't find config divinus.yaml in:\n"
" ./divinus.yaml\n /etc/divinus.yaml\n");
return -1;
}

enum ConfigError err;
Expand All @@ -87,8 +58,7 @@ enum ConfigError parse_app_config(const char *path) {
if (err != CONFIG_OK)
goto RET_ERR;
int port;
err =
parse_int(&ini, "system", "web_port", 1, INT_MAX, &port);
err = parse_int(&ini, "system", "web_port", 1, INT_MAX, &port);
if (err != CONFIG_OK)
goto RET_ERR;
app_config.web_port = (unsigned short)port;
Expand Down
3 changes: 1 addition & 2 deletions src/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,4 @@ struct AppConfig {
};

extern struct AppConfig app_config;

enum ConfigError parse_app_config(const char *path);
enum ConfigError parse_app_config(void);
93 changes: 48 additions & 45 deletions src/hal/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ enum ConfigError find_sections(struct IniConfig *ini) {
ini->sections[i].pos = -1;

regex_t regex;
if (compile_regex(
&regex, "^[[:space:]]*\\[([a-zA-Z0-9_]+)\\][[:space:]]*") < 0) {
if (compile_regex(&regex, REG_SECTION) < 0) {
printf("compile_regex error\n");
return CONFIG_REGEX_ERROR;
};
size_t n_matches = 2; // We have 1 capturing group + the whole match group

size_t n_matches = 4;
regmatch_t m[n_matches];

int section_pos = 0;
Expand All @@ -21,15 +21,18 @@ enum ConfigError find_sections(struct IniConfig *ini) {
int match = regexec(&regex, ini->str + section_pos, n_matches, m, 0);
if (match != 0)
break;
int len = sprintf(
ini->sections[section_index].name, "%.*s",
(int)(m[1].rm_eo - m[1].rm_so),
ini->str + section_pos + m[1].rm_so);

int i = 2;
if (m[i].rm_eo - m[i].rm_so == 0)
i++;
int len = sprintf(ini->sections[section_index].name, "%.*s",
(int)(m[i].rm_eo - m[i].rm_so), ini->str + section_pos + m[i].rm_so);
ini->sections[section_index].name[len] = 0;
section_pos = section_pos + (int)m[1].rm_eo;
ini->sections[section_index].pos = section_pos;
section_index++;
}

regfree(&regex);
return CONFIG_OK;
}
Expand All @@ -48,6 +51,7 @@ enum ConfigError section_pos(
return CONFIG_OK;
}
}

return CONFIG_SECTION_NOT_FOUND;
}

Expand All @@ -58,37 +62,30 @@ enum ConfigError parse_param_value(
int end_pos = 0;
if (strlen(section) > 0) {
enum ConfigError err = section_pos(ini, section, &start_pos, &end_pos);
if (err == CONFIG_SECTION_NOT_FOUND) {
printf(
"Section '%s' doesn't exists in config '%s'.\n", section,
ini->path);
return err;
} else if (err != CONFIG_OK)
if (err != CONFIG_OK)
return err;
}

regex_t regex;
char reg_buf[128];
ssize_t reg_buf_len = sprintf(
reg_buf, "^[[:space:]]*%s[[:space:]]*=[[:space:]]*(.[^[:space:];]*)",
param_name);
ssize_t reg_buf_len = sprintf(reg_buf, REG_PARAM, param_name);
reg_buf[reg_buf_len] = 0;
if (compile_regex(&regex, reg_buf) < 0) {
printf("compile_regex error\n");
return CONFIG_REGEX_ERROR;
};
size_t n_matches = 2; // We have 1 capturing group + the whole match group

size_t n_matches = 2;
regmatch_t m[n_matches];
int match = regexec(&regex, ini->str + start_pos, n_matches, m, 0);
regfree(&regex);
if (match > 0 || (end_pos >= 0 && end_pos - start_pos < m[1].rm_so)) {
printf("Can't find '%s' in section '%s'.\n", param_name, section);
return CONFIG_PARAM_NOT_FOUND;
}
int res = sprintf(
param_value, "%.*s", (int)(m[1].rm_eo - m[1].rm_so),

int res = sprintf(param_value, "%.*s", (int)(m[1].rm_eo - m[1].rm_so),
ini->str + start_pos + m[1].rm_so);
// if (res <= 0 ) { return -1; }
param_value[res] = 0;
return CONFIG_OK;
}
Expand All @@ -109,6 +106,7 @@ enum ConfigError parse_enum(
if (*end) {
res = strtol(param_value, &end, 16);
}

if (!*end) {
*(int *)enum_value = res;
return CONFIG_OK;
Expand All @@ -130,6 +128,7 @@ enum ConfigError parse_enum(
printf("'%s', ", possible_values[i]);
return CONFIG_ENUM_INCORRECT_STRING;
}

enum ConfigError parse_bool(
struct IniConfig *ini, const char *section, const char *param_name,
bool *bool_value) {
Expand Down Expand Up @@ -279,30 +278,34 @@ enum ConfigError parse_uint32(
return CONFIG_OK;
}

enum ConfigError read_sensor_from_proc_cmdline(char *sensor_type) {
FILE *file = fopen("/proc/cmdline", "r");
if (!file)
return CONFIG_CANT_OPEN_PROC_CMDLINE;
char cmdline[5 * 1024];
fread(cmdline, 1, 5 * 1024, file);
fclose(file);
bool open_config(struct IniConfig *ini, const char *path) {
FILE *file = fopen(path, "rb");
if (!file) {
printf("Can't open file %s\n", path);
return false;
}

regex_t regex;
if (compile_regex(
&regex, "[[:space:]]*sensor=([[:alnum:]_]+)[[:space:]]*") < 0) {
printf("compile_regex error\n");
return CONFIG_REGEX_ERROR;
};
size_t n_matches = 2; // We have 1 capturing group + the whole match group
regmatch_t m[n_matches];
int match = regexec(&regex, cmdline, n_matches, m, 0);
if (match != 0) {
regfree(&regex);
printf("Can't parse sensor=xxxx from cmdline: \n'%s'\n", cmdline);
return CONFIG_REGEX_ERROR;
fseek(file, 0, SEEK_END);
size_t length = ftell(file);
fseek(file, 0, SEEK_SET);

ini->str = malloc(length + 1);
if (!ini->str) {
printf("Can't allocate buf in parse_sensor_config\n");
fclose(file);
return false;
}
sprintf(
sensor_type, "%.*s", (int)(m[1].rm_eo - m[1].rm_so),
cmdline + m[1].rm_so);
return CONFIG_OK;
}

size_t n = fread(ini->str, 1, length, file);
if (n != length) {
printf("Can't read all file %s\n", path);
fclose(file);
free(ini->str);
return false;
}

fclose(file);
ini->str[length] = 0;

return true;
}
Loading

0 comments on commit 62d74ca

Please sign in to comment.