-
Notifications
You must be signed in to change notification settings - Fork 0
/
av_config.c
115 lines (90 loc) · 2.99 KB
/
av_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: GPL-2.0-or-later
/* libconfig header */
#include <libconfig.h>
/* AV headers */
#include <av_config.h>
static void av_config_deinit(config_t **c) {
config_destroy(*c);
g_clear_pointer(c, g_free);
}
static struct config_t *av_config_init(const gchar *filename) {
config_t *lc_context;
const gchar *error_file;
const gchar *error_text;
lc_context = g_try_malloc0(sizeof *lc_context);
if (!lc_context) {
g_printerr("Failure allocating libconfig context\n");
return lc_context;
}
config_init(lc_context);
config_set_option(lc_context, CONFIG_OPTION_AUTOCONVERT, CONFIG_FALSE);
if (config_read_file(lc_context, filename) != CONFIG_TRUE) {
error_file = config_error_file(lc_context);
error_text = config_error_text(lc_context);
g_printerr("Failure parsing %s configuration file: %s (line %d)\n",error_file ? error_file : "main",error_text ? error_text : "unknown error",config_error_line(lc_context));
goto failure;
}
return lc_context;
failure:
av_config_deinit(&lc_context);
return lc_context;
}
static gchar *av_config_search(config_t *l, const gchar *base, const gchar *value) {
gchar *config_path;
gchar *result = NULL;
const gchar *config_value;
config_path = g_strdup_printf("MM_%s.%s", base, value);
if (config_lookup_string(l, config_path, &config_value) == CONFIG_TRUE)
result = g_strdup(config_value);
g_clear_pointer(&config_path, g_free);
return result;
}
static struct av_modem_config *av_config_extract_data(AvModem *m, config_t *lc) {
struct av_modem_config *mc = NULL;
const gchar *equipment_id;
MMModem *modem;
modem = avmodem_get_mmmodem(m);
g_assert(modem);
equipment_id = mm_modem_get_equipment_identifier(modem);
if (!equipment_id) {
g_printerr("No equipment ID for this modem; unable to determine it's configuration data!\n");
goto failure;
}
mc = g_try_malloc0(sizeof *mc);
if (!mc) {
g_printerr("Failure allocating AVModem config structure\n");
goto failure;
}
mc->username = av_config_search(lc, equipment_id, "username");
mc->password = av_config_search(lc, equipment_id, "password");
mc->sip_host = av_config_search(lc, equipment_id, "sip_host");
mc->sip_id = av_config_search(lc, equipment_id, "sip_id");
mc->modem_audio_port = av_config_search(lc, equipment_id, "audio_port");
mc->sip_local_ip_addr = av_config_search(lc, equipment_id, "local_ip");
return mc;
failure:
av_config_free(&mc);
return mc;
}
struct av_modem_config *av_config_parse(AvModem *m) {
config_t *lc;
struct av_modem_config *mc = NULL;
lc = av_config_init("AirVoice.cfg");
if (lc) {
mc = av_config_extract_data(m, lc);
av_config_deinit(&lc);
}
return mc;
}
void av_config_free(struct av_modem_config **c) {
if (*c) {
g_clear_pointer(&(*c)->username, g_free);
g_clear_pointer(&(*c)->password, g_free);
g_clear_pointer(&(*c)->sip_host, g_free);
g_clear_pointer(&(*c)->sip_id, g_free);
g_clear_pointer(&(*c)->modem_audio_port, g_free);
g_clear_pointer(&(*c)->sip_local_ip_addr, g_free);
g_clear_pointer(c, g_free);
}
return;
}