2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
22 #include <sys/inotify.h>
26 #include "sa_common.h"
28 #include "input_file.h"
30 #define CONFIG_FILE "/opt/etc/setup-adaptor/config.json"
31 #define CONFIG_FOLDER "/opt/etc/setup-adaptor"
32 #define CONFIG_NAME "config.json"
33 #define EVENT_NAME_MAX 256
34 #define EVENT_SIZE (sizeof(struct inotify_event))
35 #define EVENT_BUF_LEN (512 * (EVENT_SIZE + EVENT_NAME_MAX))
37 static void *__config_main_loop(void *arg)
42 file_state_cb callback;
45 _D("__config_main_loop start\n");
47 callback = (file_state_cb) arg;
49 if (callback == NULL) {
50 _D("Ccallback is null for event");
57 _E("inotify_init error");
61 wd = inotify_add_watch(fd, CONFIG_FOLDER, IN_MODIFY | IN_CREATE | IN_DELETE);
64 _E("inotify_add_watch fail");
69 buffer = (char *) malloc(EVENT_BUF_LEN);
73 _D("Registerd Callback Triggered");
74 callback(SA_FILE_STATE_REGISTERED, NULL, NULL);
77 length = read(fd, buffer, EVENT_BUF_LEN);
85 while (i < length && i < (EVENT_BUF_LEN-EVENT_SIZE)) {
86 struct inotify_event *event = (struct inotify_event *)&buffer[i];
87 if (event->len > 0 && event->len < EVENT_NAME_MAX) {
88 if (event->mask & IN_CREATE) {
89 if (event->mask & IN_ISDIR) {
90 _D("The directory was created");
92 if (!strcmp(event->name, CONFIG_NAME))
93 _D("config.json is created!!");
95 } else if (event->mask & IN_DELETE) {
96 if (event->mask & IN_ISDIR) {
97 _D("The directory was deleted.");
99 if (!strcmp(event->name, CONFIG_NAME))
100 _D("config.json is deleted!!");
102 } else if (event->mask & IN_MODIFY) {
103 if (event->mask & IN_ISDIR) {
104 _D("The directory was modified");
106 if (!strcmp(event->name, CONFIG_NAME))
107 callback(SA_FILE_STATE_CHANGED, NULL, NULL);
110 i += EVENT_SIZE + event->len;
112 _E("event->len is wrong");
118 _E("buffer is NULL");
121 inotify_rm_watch(fd, wd);
130 static int __init_event_listener(file_state_cb callback)
135 // Start thread to create in order to receive event
136 if (pthread_create(&p_thread, NULL, &__config_main_loop, callback) < 0) {
137 _D("__init_event_listener create error");
141 __config_main_loop(callback);
147 static int json_get_int_from_obj(json_object * inputObj, char *key)
149 struct json_object *bodyObj;
153 type = json_object_get_type(inputObj);
155 if (type == json_type_object) {
156 if (json_object_object_get_ex(inputObj, key, &bodyObj))
157 ret = json_object_get_int(bodyObj);
162 static int json_get_boolean_from_obj(json_object * inputObj, char *key)
164 struct json_object *bodyObj;
168 type = json_object_get_type(inputObj);
170 if (type == json_type_object) {
171 if (json_object_object_get_ex(inputObj, key, &bodyObj))
172 ret = json_object_get_boolean(bodyObj);
177 static char *json_get_string_from_obj(json_object * inputObj, char *key)
179 const char *buf = NULL;
180 char *ret_buf = NULL;
181 json_object *elementObj = NULL;
184 if (json_object_object_get_ex(inputObj, key, &elementObj)) {
185 buf = json_object_to_json_string_ext(elementObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
186 ret_buf = malloc(strlen(buf) - 1);
187 if (ret_buf != NULL) {
188 memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude
189 for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude
192 ret_buf[j++] = buf[i];
201 _D("string object [%s]", ret_buf);
205 static void print_config_network_static_info(sa_network_static_s * staticInfo)
207 if (staticInfo != NULL) {
208 if (strlen(staticInfo->ipAddress) > 0)
209 _D("static::ipAddress[%s]", staticInfo->ipAddress);
210 if (strlen(staticInfo->netmask) > 0)
211 _D("static::netmask[%s]", staticInfo->netmask);
212 if (strlen(staticInfo->defaultGateway) > 0)
213 _D("static::defaultGateway[%s]", staticInfo->defaultGateway);
214 if (strlen(staticInfo->primaryDnsServer) > 0)
215 _D("static::primaryDnsServer[%s]", staticInfo->primaryDnsServer);
216 if (strlen(staticInfo->secondaryDnsServer) > 0)
217 _D("static::secondaryDnsServer[%s]", staticInfo->secondaryDnsServer);
221 static void print_network_config(sa_network_s * network)
223 sa_wifi_s *wifi = NULL;
224 sa_eth_s *eth = NULL;
226 if (network != NULL) {
227 wifi = network->wifi;
229 _D("Network::wifi::enabled[%d]", wifi->enabled);
230 _D("Network::wifi::dhcpEnabled[%d]", wifi->dhcpEnabled);
231 _D("Network::wifi::ssid[%s]", wifi->ssid);
232 _D("Network::wifi::password[%s]", wifi->password);
234 if (wifi->dhcpEnabled == 0)
235 print_config_network_static_info(wifi->staticInfo);
240 _D("Network::eth::enabled[%d]", eth->enabled);
241 _D("Network::eth::dhcpEnabled[%d]", eth->dhcpEnabled);
243 if (eth->dhcpEnabled == 0)
244 print_config_network_static_info(eth->staticInfo);
249 static void print_system_config(sa_system_s * systemData)
251 if (systemData != NULL) {
252 if (systemData->proxy != NULL) {
253 _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost);
254 _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort);
259 static void print_config_info(sa_config_s * config)
261 if (config != NULL) {
262 _D("Version [%s]", config->version);
264 if (config->networkData)
265 print_network_config(config->networkData);
267 if (config->systemData)
268 print_system_config(config->systemData);
272 static int __parse_network_static_info(json_object * inputObj, sa_network_static_s * staticInfo)
274 char *ipAddress = NULL;
275 char *netmask = NULL;
276 char *defaultGateway = NULL;
277 char *primaryDnsServer = NULL;
278 char *secondaryDnsServer = NULL;
280 if (inputObj == NULL || staticInfo == NULL) {
281 _D("__parse_network_static_info input error");
285 ipAddress = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_IPADDRESS);
286 if (ipAddress != NULL) {
287 _D("ipaddress = %s", ipAddress);
289 memcpy(staticInfo->ipAddress, ipAddress, MIN(strlen(ipAddress), sizeof(staticInfo->ipAddress) - 1));
294 netmask = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_NETMASK);
295 if (netmask != NULL) {
296 memcpy(staticInfo->netmask, netmask, MIN(strlen(netmask), sizeof(staticInfo->netmask) - 1));
301 defaultGateway = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DEFAULTGATEWAY);
302 if (defaultGateway != NULL) {
303 memcpy(staticInfo->defaultGateway, defaultGateway, MIN(strlen(defaultGateway), sizeof(staticInfo->defaultGateway) - 1));
304 free(defaultGateway);
305 defaultGateway = NULL;
308 primaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER);
309 if (primaryDnsServer != NULL) {
310 memcpy(staticInfo->primaryDnsServer, primaryDnsServer, MIN(strlen(primaryDnsServer), sizeof(staticInfo->primaryDnsServer) - 1));
311 free(primaryDnsServer);
312 primaryDnsServer = NULL;
315 secondaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER);
316 if (secondaryDnsServer != NULL) {
317 memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, MIN(strlen(secondaryDnsServer), sizeof(staticInfo->secondaryDnsServer) - 1));
318 free(secondaryDnsServer);
319 secondaryDnsServer = NULL;
325 static int __parse_network_eth(json_object * inputObj, sa_eth_s * eth)
329 if (inputObj == NULL || eth == NULL) {
330 _D("__parse_network_eth input error");
334 eth->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
337 eth->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
338 if (eth->dhcpEnabled == FALSE) {
339 eth->staticInfo = (sa_network_static_s *) malloc(sizeof(sa_network_static_s));
340 if (eth->staticInfo != NULL)
341 ret = __parse_network_static_info(inputObj, eth->staticInfo);
349 static int __parse_network_wifi(json_object * inputObj, sa_wifi_s * wifi)
352 char *password = NULL;
355 if (inputObj == NULL || wifi == NULL) {
356 _D("__parse_network_wifi input error");
360 wifi->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
363 ssid = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_SSID);
365 memcpy(wifi->ssid, ssid, MIN(strlen(ssid), sizeof(wifi->ssid) - 1));
373 password = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_PASSWORD);
374 if (password != NULL) {
375 memcpy(wifi->password, password, MIN(strlen(password), sizeof(wifi->password) - 1));
383 wifi->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
384 if (wifi->dhcpEnabled == FALSE) {
385 wifi->staticInfo = (sa_network_static_s *) malloc(sizeof(sa_network_static_s));
386 if (wifi->staticInfo != NULL)
387 ret = __parse_network_static_info(inputObj, wifi->staticInfo);
395 static int __parse_network_data(json_object * inputObj, sa_config_s * setupConfig)
397 struct json_object *wifiObj = NULL;
398 struct json_object *ethernetObj = NULL;
399 int wifiRet = 0, ethRet = 0;
401 if (inputObj == NULL) {
402 _D("__parse_network_data input error");
406 setupConfig->networkData = (sa_network_s *) malloc(sizeof(sa_network_s));
407 if (setupConfig->networkData != NULL) {
409 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_WIFI, &wifiObj)) {
410 if (json_object_get_type(wifiObj) == json_type_object) {
412 setupConfig->networkData->wifi = (sa_wifi_s *) malloc(sizeof(sa_wifi_s));
413 if (setupConfig->networkData->wifi != NULL)
414 wifiRet = __parse_network_wifi(wifiObj, setupConfig->networkData->wifi);
416 _D("network->wifi malloc fail");
420 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_ETHERNET, ðernetObj)) {
421 if (json_object_get_type(ethernetObj) == json_type_object) {
423 setupConfig->networkData->eth = (sa_eth_s *) malloc(sizeof(sa_eth_s));
424 if (setupConfig->networkData->eth != NULL)
425 ethRet = __parse_network_eth(ethernetObj, setupConfig->networkData->eth);
427 _D("network->eth malloc fail");
431 _D("malloc fail etupConfig->networkData");
434 // if both of network interfaces are failed, it would return -1
435 if (wifiRet != 0 && ethRet != 0) {
436 _D("__parse_network_data fail");
443 static int __parse_system_data(json_object * inputObj, sa_config_s * setupConfig)
446 char *httpProxyHost = NULL;
447 if (inputObj == NULL) {
448 _D("__parse_system_data input error");
452 httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
453 if (httpProxyHost != NULL) {
454 setupConfig->systemData = (sa_system_s *) malloc(sizeof(sa_system_s));
455 if (setupConfig->systemData != NULL) {
456 setupConfig->systemData->proxy = (sa_proxy_s *) malloc(sizeof(sa_proxy_s));
457 if (setupConfig->systemData->proxy != NULL) {
458 memcpy(setupConfig->systemData->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(setupConfig->systemData->proxy->httpProxyHost) - 1));
460 setupConfig->systemData->proxy->httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
464 _D("malloc fail setupConfig->systemData->proxy");
468 _D("malloc fail setupConfig->systemData");
472 httpProxyHost = NULL;
478 static int __parse_version(json_object * inputObj, sa_config_s * setupConfig)
480 char *version = NULL;
482 if (inputObj == NULL) {
483 _D("__parse_version input error");
487 version = json_get_string_from_obj(inputObj, SA_CONFIG_VERSION);
489 if (version != NULL) {
490 memcpy(setupConfig->version, version, MIN(strlen(version), sizeof(setupConfig->version) - 1));
498 static int __parse_config(char *file, sa_config_s * setupConfig)
500 struct json_object *configObj = NULL;
501 char *jsonData = NULL;
507 fd = open(file, O_RDONLY);
509 _E("config file can't be opened");
513 len = lseek(fd, 0L, SEEK_END);
516 lseek(fd, 0L, SEEK_SET);
517 jsonData = (char *)malloc(len + 1);
518 if (jsonData != NULL) {
519 memset(jsonData, 0x00, len + 1);
520 readLen = read(fd, jsonData, len);
521 _D("JSON full data[%s]", jsonData);
523 configObj = json_tokener_parse(jsonData);
525 if (configObj != NULL && readLen > 0) {
527 ret = __parse_version(configObj, setupConfig);
529 ret = __parse_system_data(configObj, setupConfig);
532 ret = __parse_network_data(configObj, setupConfig);
536 _D("ConfigObj is not existed");
539 json_object_put(configObj);
543 _D("memory allocation fail for jsonData");
552 sa_error_e sa_inputfile_get_config_info(sa_config_s * config)
554 sa_error_e ret = SA_ERROR_NONE;
556 if (access(CONFIG_FILE, 0) != 0)
557 return SA_ERROR_NOT_SUPPORTED;
559 // parsing and fill into struct
560 ret = __parse_config(CONFIG_FILE, config);
562 _D("Config parsing error");
563 return SA_ERROR_PARSING;
566 print_config_info(config);
571 sa_file_state_e sa_inputfile_get_config_state(void)
573 sa_file_state_e ret = SA_FILE_STATE_NOT_EXISTED;
575 if (access(CONFIG_FILE, F_OK) != -1)
576 ret = SA_FILE_STATE_EXIST;
578 ret = SA_FILE_STATE_NOT_EXISTED;
583 static void release_system_resource(sa_system_s * system)
585 if (system != NULL) {
586 if (system->proxy != NULL)
593 static void release_network_resource(sa_network_s * network)
595 if (network != NULL) {
596 if (network->wifi != NULL) {
597 if (network->wifi->staticInfo != NULL)
598 free(network->wifi->staticInfo);
603 if (network->eth != NULL) {
604 if (network->eth->staticInfo != NULL)
605 free(network->eth->staticInfo);
614 gboolean sa_inputfile_thread(GSource * source, GSourceFunc callbackFuntion, gpointer user_data)
616 _D("GSourceFuncs>>>Execution !!!");
619 sa_error_e ret = SA_ERROR_NONE;
621 if (__init_event_listener(callback))
622 ret = SA_ERROR_UNKNOWN;
624 callbackFuntion(NULL);
628 g_main_loop_quit((GMainLoop *) user_data);
630 // if return value is "FALSE", function stop
631 // if return value is "TRUE", function restart
635 void sa_inputfile_release_resource(sa_config_s * config)
638 if (config != NULL) {
639 if (config->networkData != NULL)
640 release_network_resource(config->networkData);
642 if (config->systemData != NULL)
643 release_system_resource(config->systemData);
647 #define FLAG_FILE_SYSTEM "/opt/etc/setup-adaptor/system_executed"
648 #define FLAG_FILE_ETH "/opt/etc/setup-adaptor/ethernet_executed"
649 #define FLAG_FILE_WIFI "/opt/etc/setup-adaptor/wifi_executed"
651 void sa_inputfile_remove(void)
655 if (remove(CONFIG_FILE) != 0) {
656 if (strerror_r(errno, buff, sizeof(buff)) == 0)
657 _E("Can't remove file{%s}! %s", CONFIG_FILE, buff);
659 _D("file removed successfully..{%s}", CONFIG_FILE);
662 void sa_inputfile_clear_completion_flag(void)
664 if (remove(FLAG_FILE_SYSTEM) == 0)
665 _D("file removed successfully..{%s}", FLAG_FILE_SYSTEM);
667 if (remove(FLAG_FILE_ETH) == 0)
668 _D("file removed successfully..{%s}", FLAG_FILE_ETH);
670 if (remove(FLAG_FILE_WIFI) == 0)
671 _D("file removed successfully..{%s}", FLAG_FILE_WIFI);
675 void sa_inputfile_set_completion_flag(sa_file_config_e config_type)
680 switch (config_type) {
681 case SA_FILE_CONFIG_SYSTEM:
682 fd = creat(FLAG_FILE_SYSTEM, 0644);
684 case SA_FILE_CONFIG_ETHERNET:
685 fd = creat(FLAG_FILE_ETH, 0644);
687 case SA_FILE_CONFIG_WIFI:
688 fd = creat(FLAG_FILE_WIFI, 0644);
691 _E("unknown parameter (%d)", config_type);
696 if (strerror_r(errno, buff, sizeof(buff)) == 0)
697 _E("Can't create file for {%d}! %s", config_type, buff);
702 // return TRUE if set
703 gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type)
707 switch (config_type) {
708 case SA_FILE_CONFIG_SYSTEM:
709 ret = access(FLAG_FILE_SYSTEM, 0);
711 case SA_FILE_CONFIG_ETHERNET:
712 ret = access(FLAG_FILE_ETH, 0);
714 case SA_FILE_CONFIG_WIFI:
715 ret = access(FLAG_FILE_WIFI, 0);
717 case SA_FILE_CONFIG_ALL:
718 ret = access(FLAG_FILE_SYSTEM, 0);
720 ret = access(FLAG_FILE_ETH, 0);
723 ret = access(FLAG_FILE_WIFI, 0);
727 _E("unknown parameter (%d)", config_type);