2 * Network Configuration Module
4 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
31 } netconfig_setting_data_s;
34 SETTING_TYPE_LAST_POWER = 0,
35 SETTING_TYPE_WIFI_OFF_BY_AIRPLANE,
36 SETTING_TYPE_WIFI_OFF_BY_RESTRICTED,
37 SETTING_TYPE_WIFI_OFF_BY_EMERGENCY,
39 // Extension data for socket buffer size. This might be used on Robot profile only.
40 SETTING_TYPE_READ_BUFFER_SIZE_DEFAULT,
41 SETTING_TYPE_READ_BUFFER_SIZE_MAX,
42 SETTING_TYPE_WRITE_BUFFER_SIZE_DEFAULT,
43 SETTING_TYPE_WRITE_BUFFER_SIZE_MAX,
44 SETTING_TYPE_IPV4_UDP_BUFFER_SIZE,
45 } netconfig_setting_type_e;
47 netconfig_setting_data_s netconfig_setting_data[] = {
48 { "LastPoweredInterfaces", {NULL} },
49 { "WifiOffByAirplaneInterfaces", {NULL} },
50 { "WifiOffByRestrictedInterfaces", {NULL} },
51 { "WifiOffByEmergencyInterfaces", {NULL} },
54 { "ReadBufferSizeDefault", {0} },
55 { "ReadBufferSizeMax", {0} },
56 { "WriteBufferSizeDefault", {0} },
57 { "WriteBufferSizeMax", {0} },
58 { "IPv4UdpBufferSize", {NULL} },
61 void netconfig_setting_init(void)
63 netconfig_setting_load();
65 INFO("setting is initialized");
70 void netconfig_setting_deinit(void)
74 netconfig_setting_save();
76 size = SETTING_TYPE_WIFI_OFF_BY_EMERGENCY;
77 for (i = 0; i <= size; i++)
78 g_slist_free_full(netconfig_setting_data[i].data.string_list, g_free);
80 g_free(netconfig_setting_data[SETTING_TYPE_IPV4_UDP_BUFFER_SIZE].data.size_list);
82 INFO("setting is deinitialized");
87 static void __setting_set_string_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
89 GString *string_str = NULL;
90 char **string_list = NULL;
94 string_str = g_string_new(NULL);
96 list = netconfig_setting_data[type].data.string_list;
97 for (; list; list = list->next) {
98 if (string_str->len > 0)
99 g_string_append_printf(string_str, " %s", (char *)list->data);
101 g_string_append(string_str, list->data);
104 if (string_str->len > 0) {
105 string_list = g_strsplit_set(string_str->str, " ", 0);
106 length = g_strv_length(string_list);
107 g_key_file_set_string_list(keyfile, group,
108 netconfig_setting_data[type].key,
109 (const gchar **)string_list, length);
111 g_key_file_remove_key(keyfile, group,
112 netconfig_setting_data[type].key, NULL);
115 g_strfreev(string_list);
116 g_string_free(string_str, TRUE);
120 static void __setting_get_string_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
122 char **string_list = NULL;
126 INFO("Key [%s]", netconfig_setting_data[type].key);
128 string_list = g_key_file_get_string_list(keyfile, group,
129 netconfig_setting_data[type].key, &length, NULL);
131 ERR("%d key cannot be found from %s.", type, group);
136 for (i = 0; string_list[i]; i++) {
137 netconfig_setting_data[type].data.string_list = g_slist_append(
138 netconfig_setting_data[type].data.string_list,
139 g_strdup(string_list[i]));
141 INFO("%s", string_list[i]);
145 g_strfreev(string_list);
148 static void __setting_get_integer(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
151 GError *error = NULL;
153 INFO("Key [%s]", netconfig_setting_data[type].key);
154 value = g_key_file_get_integer(keyfile, group, netconfig_setting_data[type].key, &error);
156 ERR("Failed to get integer value from keyfile. %s", error->message);
160 netconfig_setting_data[type].data.size = value;
163 static void __setting_get_integer_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
165 int *value_list = NULL;
167 GError *error = NULL;
169 INFO("Key [%s]", netconfig_setting_data[type].key);
170 value_list = g_key_file_get_integer_list(keyfile, group,
171 netconfig_setting_data[type].key, &length, &error);
172 if (error || length != 3) {
173 ERR("Failed to get integer value from keyfile. %s",
174 error ? error->message : "length isn't 3.");
179 netconfig_setting_data[type].data.size_list = value_list;
182 int netconfig_setting_save(void)
184 GKeyFile *keyfile = NULL;
186 INFO("Save setting [%s]", NETCONFIG_SETTINGS_PATH);
188 keyfile = netconfig_keyfile_load(NETCONFIG_SETTINGS_PATH);
189 if (keyfile == NULL) {
190 ERR("Fail to get settings keyfile");
194 __setting_set_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME, SETTING_TYPE_LAST_POWER);
195 __setting_set_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
196 SETTING_TYPE_WIFI_OFF_BY_AIRPLANE);
197 __setting_set_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
198 SETTING_TYPE_WIFI_OFF_BY_RESTRICTED);
199 __setting_set_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
200 SETTING_TYPE_WIFI_OFF_BY_EMERGENCY);
202 netconfig_keyfile_save(keyfile, NETCONFIG_SETTINGS_PATH);
204 g_key_file_free(keyfile);
208 int netconfig_setting_load(void)
210 GKeyFile *keyfile = NULL;
212 INFO("Load setting [%s]", NETCONFIG_SETTINGS_PATH);
214 keyfile = netconfig_keyfile_load(NETCONFIG_SETTINGS_PATH);
215 if (keyfile == NULL) {
216 ERR("Fail to get settings keyfile");
220 __setting_get_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME, SETTING_TYPE_LAST_POWER);
221 __setting_get_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
222 SETTING_TYPE_WIFI_OFF_BY_AIRPLANE);
223 __setting_get_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
224 SETTING_TYPE_WIFI_OFF_BY_RESTRICTED);
225 __setting_get_string_list(keyfile, SETTINGS_WIFI_GROUP_NAME,
226 SETTING_TYPE_WIFI_OFF_BY_EMERGENCY);
228 __setting_get_integer(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
229 SETTING_TYPE_READ_BUFFER_SIZE_DEFAULT);
230 __setting_get_integer(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
231 SETTING_TYPE_READ_BUFFER_SIZE_MAX);
232 __setting_get_integer(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
233 SETTING_TYPE_WRITE_BUFFER_SIZE_DEFAULT);
234 __setting_get_integer(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
235 SETTING_TYPE_WRITE_BUFFER_SIZE_MAX);
237 __setting_get_integer_list(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
238 SETTING_TYPE_IPV4_UDP_BUFFER_SIZE);
240 g_key_file_free(keyfile);
245 void netconfig_setting_update_interface_for_last_powered(const char *interface_name,
248 GSList *list = netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list;
251 for ( ; list; list = list->next) {
252 const char *ifname = list->data;
253 if (g_strcmp0(ifname, interface_name) == 0) {
255 netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list = g_slist_remove(
256 netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list, ifname);
263 if (mode == TRUE && find == FALSE)
264 netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list = g_slist_append(
265 netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list, g_strdup(interface_name));
267 netconfig_setting_save();
270 GSList *netconfig_setting_get_interfaces_for_last_powered(void)
272 return netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list;
275 void netconfig_setting_update_interface_off_for_airplane(const char *interface_name,
278 GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list;
281 for ( ; list; list = list->next) {
282 const char *ifname = list->data;
283 if (g_strcmp0(ifname, interface_name) == 0) {
285 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list = g_slist_remove(
286 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list, ifname);
293 if (mode == TRUE && find == FALSE)
294 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list = g_slist_append(
295 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list, g_strdup(interface_name));
297 netconfig_setting_save();
300 GSList *netconfig_setting_get_interfaces_off_by_airplane(void)
302 return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list;
305 void netconfig_setting_update_interface_off_for_restricted(const char *interface_name,
308 GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list;
311 for ( ; list; list = list->next) {
312 const char *ifname = list->data;
313 if (g_strcmp0(ifname, interface_name) == 0) {
315 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list = g_slist_remove(
316 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list, ifname);
323 if (mode == TRUE && find == FALSE)
324 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list = g_slist_append(
325 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list, g_strdup(interface_name));
327 netconfig_setting_save();
330 GSList *netconfig_setting_get_interfaces_off_by_restricted(void)
332 return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list;
335 void netconfig_setting_update_interface_off_for_emergency(const char *interface_name,
338 GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list;
341 for ( ; list; list = list->next) {
342 const char *ifname = list->data;
343 if (g_strcmp0(ifname, interface_name) == 0) {
345 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list
347 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list,
355 if (mode == TRUE && find == FALSE)
356 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list
358 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list,
359 g_strdup(interface_name));
361 netconfig_setting_save();
364 GSList *netconfig_setting_get_interfaces_off_by_emergency(void)
366 return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list;
369 int netconfig_setting_get_read_buffer_size_default(void)
371 return netconfig_setting_data[SETTING_TYPE_READ_BUFFER_SIZE_DEFAULT].data.size;
374 int netconfig_setting_get_read_buffer_size_max(void)
376 return netconfig_setting_data[SETTING_TYPE_READ_BUFFER_SIZE_MAX].data.size;
379 int netconfig_setting_get_write_buffer_size_default(void)
381 return netconfig_setting_data[SETTING_TYPE_WRITE_BUFFER_SIZE_DEFAULT].data.size;
384 int netconfig_setting_get_write_buffer_size_max(void)
386 return netconfig_setting_data[SETTING_TYPE_WRITE_BUFFER_SIZE_MAX].data.size;
389 int *netconfig_setting_get_ipv4_udp_buffer_size(void)
391 return netconfig_setting_data[SETTING_TYPE_IPV4_UDP_BUFFER_SIZE].data.size_list;