Check if ckmc_get_data() provides NULL value
[platform/core/connectivity/net-config.git] / src / utils / setting.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "setting.h"
21 #include "util.h"
22 #include "log.h"
23
24 typedef struct {
25         const char *key;
26         union {
27                 GSList *string_list;
28                 int size;
29                 int *size_list;
30         } data;
31 } netconfig_setting_data_s;
32
33 typedef enum {
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,
38
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;
46
47 netconfig_setting_data_s netconfig_setting_data[] = {
48         { "LastPoweredInterfaces", {NULL} },
49         { "WifiOffByAirplaneInterfaces", {NULL} },
50         { "WifiOffByRestrictedInterfaces", {NULL} },
51         { "WifiOffByEmergencyInterfaces", {NULL} },
52
53         // Extension data.
54         { "ReadBufferSizeDefault", {0} },
55         { "ReadBufferSizeMax", {0} },
56         { "WriteBufferSizeDefault", {0} },
57         { "WriteBufferSizeMax", {0} },
58         { "IPv4UdpBufferSize", {NULL} },
59 };
60
61 void netconfig_setting_init(void)
62 {
63         netconfig_setting_load();
64
65         INFO("setting is initialized");
66
67         return;
68 }
69
70 void netconfig_setting_deinit(void)
71 {
72         int i, size;
73
74         netconfig_setting_save();
75
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);
79
80         g_free(netconfig_setting_data[SETTING_TYPE_IPV4_UDP_BUFFER_SIZE].data.size_list);
81
82         INFO("setting is deinitialized");
83
84         return;
85 }
86
87 static void __setting_set_string_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
88 {
89         GString *string_str = NULL;
90         char **string_list = NULL;
91         GSList *list = NULL;
92         guint length;
93
94         string_str = g_string_new(NULL);
95         if (string_str) {
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);
100                         else
101                                 g_string_append(string_str, list->data);
102                 }
103
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);
110                 } else {
111                         g_key_file_remove_key(keyfile, group,
112                                 netconfig_setting_data[type].key, NULL);
113                 }
114
115                 g_strfreev(string_list);
116                 g_string_free(string_str, TRUE);
117         }
118 }
119
120 static void __setting_get_string_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
121 {
122         char **string_list = NULL;
123         gsize length;
124         int i;
125
126         INFO("Key [%s]", netconfig_setting_data[type].key);
127
128         string_list = g_key_file_get_string_list(keyfile, group,
129                                         netconfig_setting_data[type].key, &length, NULL);
130         if (!string_list) {
131                 ERR("%d key cannot be found from %s.", type, group);
132                 return;
133         }
134
135         if (length != 0) {
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]));
140
141                         INFO("%s", string_list[i]);
142                 }
143         }
144
145         g_strfreev(string_list);
146 }
147
148 static void __setting_get_integer(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
149 {
150         int value;
151         GError *error = NULL;
152
153         INFO("Key [%s]", netconfig_setting_data[type].key);
154         value = g_key_file_get_integer(keyfile, group, netconfig_setting_data[type].key, &error);
155         if (error) {
156                 ERR("Failed to get integer value from keyfile. %s", error->message);
157                 return;
158         }
159
160         netconfig_setting_data[type].data.size = value;
161 }
162
163 static void __setting_get_integer_list(GKeyFile *keyfile, const char *group, netconfig_setting_type_e type)
164 {
165         int *value_list = NULL;
166         gsize length;
167         GError *error = NULL;
168
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.");
175                 g_free(value_list);
176                 return;
177         }
178
179         netconfig_setting_data[type].data.size_list = value_list;
180 }
181
182 int netconfig_setting_save(void)
183 {
184         GKeyFile *keyfile = NULL;
185
186         INFO("Save setting [%s]", NETCONFIG_SETTINGS_PATH);
187
188         keyfile = netconfig_keyfile_load(NETCONFIG_SETTINGS_PATH);
189         if (keyfile == NULL) {
190                 ERR("Fail to get settings keyfile");
191                 return -EIO;
192         }
193
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);
201
202         netconfig_keyfile_save(keyfile, NETCONFIG_SETTINGS_PATH);
203
204         g_key_file_free(keyfile);
205         return 0;
206 }
207
208 int netconfig_setting_load(void)
209 {
210         GKeyFile *keyfile = NULL;
211
212         INFO("Load setting [%s]", NETCONFIG_SETTINGS_PATH);
213
214         keyfile = netconfig_keyfile_load(NETCONFIG_SETTINGS_PATH);
215         if (keyfile == NULL) {
216                 ERR("Fail to get settings keyfile");
217                 return -EIO;
218         }
219
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);
227
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);
236
237         __setting_get_integer_list(keyfile, SETTINGS_EXTENSION_GROUP_NAME,
238                         SETTING_TYPE_IPV4_UDP_BUFFER_SIZE);
239
240         g_key_file_free(keyfile);
241
242         return 0;
243 }
244
245 void netconfig_setting_update_interface_for_last_powered(const char *interface_name,
246                 bool mode)
247 {
248         GSList *list = netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list;
249         bool find = FALSE;
250
251         for ( ; list; list = list->next) {
252                 const char *ifname = list->data;
253                 if (g_strcmp0(ifname, interface_name) == 0) {
254                         if (mode == FALSE)
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);
257
258                         find = TRUE;
259                         break;
260                 }
261         }
262
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));
266
267         netconfig_setting_save();
268 }
269
270 GSList *netconfig_setting_get_interfaces_for_last_powered(void)
271 {
272         return netconfig_setting_data[SETTING_TYPE_LAST_POWER].data.string_list;
273 }
274
275 void netconfig_setting_update_interface_off_for_airplane(const char *interface_name,
276                 bool mode)
277 {
278         GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list;
279         bool find = FALSE;
280
281         for ( ; list; list = list->next) {
282                 const char *ifname = list->data;
283                 if (g_strcmp0(ifname, interface_name) == 0) {
284                         if (mode == FALSE)
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);
287
288                         find = TRUE;
289                         break;
290                 }
291         }
292
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));
296
297         netconfig_setting_save();
298 }
299
300 GSList *netconfig_setting_get_interfaces_off_by_airplane(void)
301 {
302         return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_AIRPLANE].data.string_list;
303 }
304
305 void netconfig_setting_update_interface_off_for_restricted(const char *interface_name,
306                 bool mode)
307 {
308         GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list;
309         bool find = FALSE;
310
311         for ( ; list; list = list->next) {
312                 const char *ifname = list->data;
313                 if (g_strcmp0(ifname, interface_name) == 0) {
314                         if (mode == FALSE)
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);
317
318                         find = TRUE;
319                         break;
320                 }
321         }
322
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));
326
327         netconfig_setting_save();
328 }
329
330 GSList *netconfig_setting_get_interfaces_off_by_restricted(void)
331 {
332         return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_RESTRICTED].data.string_list;
333 }
334
335 void netconfig_setting_update_interface_off_for_emergency(const char *interface_name,
336                 bool mode)
337 {
338         GSList *list = netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list;
339         bool find = FALSE;
340
341         for ( ; list; list = list->next) {
342                 const char *ifname = list->data;
343                 if (g_strcmp0(ifname, interface_name) == 0) {
344                         if (mode == FALSE)
345                                 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list
346                                         = g_slist_remove(
347                                                 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list,
348                                                 ifname);
349
350                         find = TRUE;
351                         break;
352                 }
353         }
354
355         if (mode == TRUE && find == FALSE)
356                 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list
357                         = g_slist_append(
358                                 netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list,
359                                 g_strdup(interface_name));
360
361         netconfig_setting_save();
362 }
363
364 GSList *netconfig_setting_get_interfaces_off_by_emergency(void)
365 {
366         return netconfig_setting_data[SETTING_TYPE_WIFI_OFF_BY_EMERGENCY].data.string_list;
367 }
368
369 int netconfig_setting_get_read_buffer_size_default(void)
370 {
371         return netconfig_setting_data[SETTING_TYPE_READ_BUFFER_SIZE_DEFAULT].data.size;
372 }
373
374 int netconfig_setting_get_read_buffer_size_max(void)
375 {
376         return netconfig_setting_data[SETTING_TYPE_READ_BUFFER_SIZE_MAX].data.size;
377 }
378
379 int netconfig_setting_get_write_buffer_size_default(void)
380 {
381         return netconfig_setting_data[SETTING_TYPE_WRITE_BUFFER_SIZE_DEFAULT].data.size;
382 }
383
384 int netconfig_setting_get_write_buffer_size_max(void)
385 {
386         return netconfig_setting_data[SETTING_TYPE_WRITE_BUFFER_SIZE_MAX].data.size;
387 }
388
389 int *netconfig_setting_get_ipv4_udp_buffer_size(void)
390 {
391         return netconfig_setting_data[SETTING_TYPE_IPV4_UDP_BUFFER_SIZE].data.size_list;
392 }