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 "/etc/setup-adaptor/config.json"
31 #define CONFIG_FOLDER "/etc/setup-adaptor"
32 #define CONFIG_NAME "config.json"
33 #define EVENT_SIZE (sizeof(struct inotify_event))
34 #define BUF_LEN (1024 * (EVENT_SIZE + 16))
36 static void *__config_main_loop(void *arg)
40 file_state_cb callback;
43 _D("__config_main_loop start\n");
45 callback = (file_state_cb)arg;
47 if (callback == NULL) {
48 _D("Ccallback is null for event");
55 _D("inotify_init error");
59 wd = inotify_add_watch(fd, CONFIG_FOLDER, IN_MODIFY | IN_CREATE | IN_DELETE);
61 _D("Registerd Callback Triggered");
62 callback(SA_FILE_STATE_REGISTERED, NULL, NULL);
65 length = read(fd, buffer, BUF_LEN);
71 struct inotify_event *event = (struct inotify_event *)&buffer[i];
72 _D("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no");
74 if (event->mask & IN_CREATE) {
75 if (event->mask & IN_ISDIR) {
76 _D("The directory %s was created", event->name);
79 _D("The file %s was create.", event->name);
80 if (!strcmp(event->name, CONFIG_NAME)) {
81 _D("config.json is created!!");
84 } else if (event->mask & IN_DELETE) {
85 if (event->mask & IN_ISDIR) {
86 _D("The directory %s was deleted.", event->name);
89 _D("The file %s was deleted", event->name);
90 if (!strcmp(event->name, CONFIG_NAME)) {
91 _D("config.json is deleted!!");
94 } else if (event->mask & IN_MODIFY) {
95 if (event->mask & IN_ISDIR) {
96 _D("The directory %s was modified", event->name);
99 if (!strcmp(event->name, CONFIG_NAME)) {
100 _D("config.json is modified!!");
101 callback(SA_FILE_STATE_CHANGED, NULL, NULL);
107 i += EVENT_SIZE + event->len;
111 inotify_rm_watch(fd, wd);
117 static int __init_event_listener(file_state_cb callback)
122 // Start thread to create in order to receive event
123 if (pthread_create(&p_thread, NULL, &__config_main_loop, callback) < 0) {
124 _D("__init_event_listener create error");
128 __config_main_loop(callback);
134 static int json_get_int_from_obj(json_object *inputObj, char *key)
136 struct json_object *bodyObj;
140 type = json_object_get_type(inputObj);
142 if (type == json_type_object) {
143 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
144 ret = json_object_get_int(bodyObj);
150 static int json_get_boolean_from_obj(json_object *inputObj, char *key)
152 struct json_object *bodyObj;
156 type = json_object_get_type(inputObj);
158 if (type == json_type_object) {
159 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
160 ret = json_object_get_boolean(bodyObj);
166 static char *json_get_string_from_obj(json_object *inputObj, char *key)
168 const char *buf = NULL;
169 char *ret_buf = NULL;
170 json_object *elementObj = NULL;
173 if (json_object_object_get_ex(inputObj, key, &elementObj)) {
174 buf = json_object_to_json_string_ext(elementObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
175 ret_buf = malloc(strlen(buf) - 1);
176 if (ret_buf != NULL) {
177 memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude
178 for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude
181 ret_buf[j++] = buf[i];
190 _D("string object [%s]", ret_buf);
194 static void print_config_network_static_info(sa_network_static_s *staticInfo)
196 if (staticInfo != NULL) {
197 if (staticInfo->ipAddress != NULL)
198 _D("static::ipAddress[%s]");
199 if (staticInfo->netmask != NULL)
200 _D("static::netmask[%s]");
201 if (staticInfo->defaultGateway != NULL)
202 _D("static::defaultGateway[%s]");
203 if (staticInfo->primaryDnsServer != NULL)
204 _D("static::primaryDnsServer[%s]");
205 if (staticInfo->secondaryDnsServer != NULL)
206 _D("static::secondaryDnsServer[%s]");
210 static void print_network_config(sa_network_s *network)
212 sa_wifi_s *wifi = NULL;
213 sa_eth_s *eth = NULL;
215 if (network != NULL) {
216 wifi = network->wifi;
218 _D("Network::wifi::enabled[%d]", wifi->enabled);
219 _D("Network::wifi::dhcpEnabled[%d]", wifi->dhcpEnabled);
220 if (wifi->ssid != NULL)
221 _D("Network::wifi::ssid[%s]", wifi->ssid);
222 if (wifi->password != NULL)
223 _D("Network::wifi::password[%s]", wifi->password);
225 if (wifi->dhcpEnabled == 0)
226 print_config_network_static_info(wifi->staticInfo);
231 _D("Network::eth::enabled[%d]", eth->enabled);
232 _D("Network::eth::dhcpEnabled[%d]", eth->dhcpEnabled);
234 if (eth->dhcpEnabled == 0)
235 print_config_network_static_info(eth->staticInfo);
240 static void print_system_config(sa_system_s *systemData)
242 if (systemData != NULL) {
243 if (systemData->proxy != NULL) {
244 _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost);
245 _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort);
250 static void print_config_info(sa_config_s *config)
252 if (config != NULL) {
253 if (config->version != NULL) {
254 _D("Version [%s]", config->version);
256 if(config->networkData)
258 print_network_config(config->networkData);
260 if(config->systemData)
262 print_system_config(config->systemData);
267 static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo)
269 char *ipAddress = NULL;
270 char *netmask = NULL;
271 char *defaultGateway = NULL;
272 char *primaryDnsServer = NULL;
273 char *secondaryDnsServer = NULL;
275 if (inputObj == NULL || staticInfo == NULL) {
276 _D("__parse_network_static_info input error");
281 ipAddress = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_IPADDRESS);
282 if (ipAddress != NULL) {
283 _D("ipaddress = %s", ipAddress);
284 memcpy(staticInfo->ipAddress, ipAddress, strlen(ipAddress));
290 netmask = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_NETMASK);
291 if (netmask != NULL) {
292 memcpy(staticInfo->netmask, netmask,strlen(netmask));
298 defaultGateway = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DEFAULTGATEWAY);
299 if (defaultGateway != NULL) {
300 memcpy(staticInfo->defaultGateway, defaultGateway, strlen(defaultGateway));
301 free(defaultGateway);
302 defaultGateway = NULL;
306 primaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER);
307 if (primaryDnsServer != NULL) {
308 memcpy(staticInfo->primaryDnsServer, primaryDnsServer, strlen(primaryDnsServer));
309 free(primaryDnsServer);
310 primaryDnsServer = NULL;
314 secondaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER);
315 if (secondaryDnsServer != NULL) {
316 memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, strlen(secondaryDnsServer));
317 free(secondaryDnsServer);
318 secondaryDnsServer = NULL;
324 static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
328 if (inputObj == NULL || eth == NULL) {
329 _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);
350 static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
353 char *password = NULL;
356 if (inputObj == NULL || wifi == NULL) {
357 _D("__parse_network_wifi input error");
361 wifi->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
364 ssid = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_SSID);
366 memcpy(wifi->ssid, ssid, MIN(strlen(ssid), sizeof(wifi->ssid)-1));
374 password = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_PASSWORD);
375 if (password != NULL) {
376 memcpy(wifi->password, password, MIN(strlen(password), sizeof(wifi->password)-1));
384 wifi->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
385 if (wifi->dhcpEnabled == FALSE) {
386 wifi->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
387 if (wifi->staticInfo != NULL) {
388 ret = __parse_network_static_info(inputObj, wifi->staticInfo);
397 static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig)
399 struct json_object *wifiObj = NULL;
400 struct json_object *ethernetObj = NULL;
401 int wifiRet = 0, ethRet = 0;
403 if (inputObj == NULL) {
404 _D("__parse_network_data input error");
408 setupConfig->networkData = (sa_network_s *)malloc(sizeof(sa_network_s));
409 if (setupConfig->networkData != NULL) {
411 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_WIFI, &wifiObj)) {
412 if (json_object_get_type(wifiObj) == json_type_object) {
414 setupConfig->networkData->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s));
415 if (setupConfig->networkData->wifi != NULL) {
416 wifiRet = __parse_network_wifi(wifiObj, setupConfig->networkData->wifi);
418 _D("network->wifi malloc fail");
424 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_ETHERNET, ðernetObj)) {
425 if (json_object_get_type(ethernetObj) == json_type_object) {
427 setupConfig->networkData->eth = (sa_eth_s *)malloc(sizeof(sa_eth_s));
428 if (setupConfig->networkData->eth != NULL) {
429 ethRet = __parse_network_eth(ethernetObj, setupConfig->networkData->eth);
431 _D("network->eth malloc fail");
436 _D("malloc fail etupConfig->networkData");
439 // if both of network interfaces are failed, it would return -1
440 if (wifiRet != 0 && ethRet != 0) {
441 _D("__parse_network_data fail");
449 static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig)
452 char *httpProxyHost = NULL;
453 if (inputObj == NULL) {
454 _D("__parse_system_data input error");
459 httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
460 if (httpProxyHost != NULL) {
461 setupConfig->systemData = (sa_system_s *)malloc(sizeof(sa_system_s));
462 if (setupConfig->systemData != NULL) {
463 setupConfig->systemData->proxy = (sa_proxy_s *)malloc(sizeof(sa_proxy_s));
464 if (setupConfig->systemData->proxy != NULL) {
465 memcpy(setupConfig->systemData->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(setupConfig->systemData->proxy->httpProxyHost)-1));
467 httpProxyHost = NULL;
470 setupConfig->systemData->proxy->httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
474 _D("malloc fail setupConfig->systemData->proxy");
478 _D("malloc fail setupConfig->systemData");
486 static int __parse_version(json_object *inputObj, sa_config_s *setupConfig)
488 char *version = NULL;
490 if (inputObj == NULL) {
491 _D("__parse_version input error");
495 version = json_get_string_from_obj(inputObj, SA_CONFIG_VERSION);
497 if (version != NULL) {
498 memcpy(setupConfig->version, version, MIN(strlen(version), sizeof(setupConfig->version)-1));
506 static int __parse_config(char *file, sa_config_s *setupConfig)
508 struct json_object *configObj = NULL;
509 char *jsonData = NULL;
515 fd = open(file, O_RDONLY);
517 len = lseek(fd, 0L, SEEK_END);
520 lseek(fd, 0L, SEEK_SET);
521 jsonData = (char *)malloc(len + 1);
522 if (jsonData != NULL) {
523 memset(jsonData, 0x00, len + 1);
524 readLen = read(fd, jsonData, len);
525 _D("JSON full data[%s]", jsonData);
527 configObj = json_tokener_parse(jsonData);
529 if (configObj != NULL && readLen > 0) {
531 ret = __parse_version(configObj, setupConfig);
533 ret = __parse_system_data(configObj, setupConfig);
537 ret = __parse_network_data(configObj, setupConfig);
541 _D("ConfigObj is not existed");
544 json_object_put(configObj);
548 _D("memory allocation fail for jsonData");
555 _D("config file can't be opened");
561 sa_error_e sa_inputfile_get_config_info(sa_config_s *config)
563 sa_error_e ret = SA_ERROR_NONE;
565 if (access(CONFIG_FILE, 0) != 0) {
566 return SA_ERROR_NOT_SUPPORTED;
568 // parsing and fill into struct
569 ret = __parse_config(CONFIG_FILE, config);
571 _D("Config parsing error");
572 return SA_ERROR_PARSING;
575 print_config_info(config);
580 sa_file_state_e sa_inputfile_get_config_state(void)
582 sa_file_state_e ret = SA_FILE_STATE_NOT_EXISTED;
584 if (access(CONFIG_FILE, F_OK) != -1) {
585 ret = SA_FILE_STATE_EXIST;
587 ret = SA_FILE_STATE_NOT_EXISTED;
593 static void release_system_resource(sa_system_s *system)
595 if (system != NULL) {
596 if (system->proxy != NULL)
603 static void release_network_resource(sa_network_s *network)
605 if (network != NULL) {
606 if (network->wifi != NULL) {
607 if (network->wifi->staticInfo != NULL)
608 free(network->wifi->staticInfo);
613 if (network->eth != NULL) {
614 if (network->eth->staticInfo != NULL)
615 free(network->eth->staticInfo);
624 gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpointer user_data)
626 GMainLoop* loop = user_data;
629 _D("GSourceFuncs>>>Execution !!!");
632 sa_error_e ret = SA_ERROR_NONE;
634 if (__init_event_listener(callback)) {
635 ret = SA_ERROR_UNKNOWN;
638 callbackFuntion(NULL);
642 g_main_loop_quit( (GMainLoop*)user_data );
645 // if return value is "FALSE", function stop
646 // if return value is "TRUE", function restart
650 void sa_inputfile_release_resource(sa_config_s *config)
653 if (config != NULL) {
654 if (config->networkData != NULL)
655 release_network_resource(config->networkData);
657 if (config->systemData != NULL)
658 release_system_resource(config->systemData);
662 #define FLAG_FILE_SYSTEM "/etc/setup-adaptor/system_executed"
663 #define FLAG_FILE_ETH "/etc/setup-adaptor/ethernet_executed"
664 #define FLAG_FILE_WIFI "/etc/setup-adaptor/wifi_executed"
666 void sa_inputfile_remove(void)
668 if (remove(CONFIG_FILE) != 0) {
669 _E("Can't remove file{%s}! %s", CONFIG_FILE, strerror(errno));
672 _D("file removed successfully..{%s}", CONFIG_FILE);
676 void sa_inputfile_clear_completion_flag(void)
678 if (remove(FLAG_FILE_SYSTEM) == 0) {
679 _D("file removed successfully..{%s}", FLAG_FILE_SYSTEM);
681 if (remove(FLAG_FILE_ETH) == 0) {
682 _D("file removed successfully..{%s}", FLAG_FILE_ETH);
684 if (remove(FLAG_FILE_WIFI) == 0) {
685 _D("file removed successfully..{%s}", FLAG_FILE_WIFI);
689 void sa_inputfile_set_completion_flag(sa_file_config_e config_type)
695 case SA_FILE_CONFIG_SYSTEM:
696 fd = creat(FLAG_FILE_SYSTEM, NULL);
698 case SA_FILE_CONFIG_ETHERNET:
699 fd = creat(FLAG_FILE_ETH, NULL);
701 case SA_FILE_CONFIG_WIFI:
702 fd = creat(FLAG_FILE_WIFI, NULL);
705 _E("unknown parameter (%d)", config_type);
710 _E("Can't create file for (%d)! %s", config_type, strerror(errno));
714 // return TRUE if set
715 gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type)
721 case SA_FILE_CONFIG_SYSTEM:
722 ret = access(FLAG_FILE_SYSTEM, 0);
724 case SA_FILE_CONFIG_ETHERNET:
725 ret = access(FLAG_FILE_ETH, 0);
727 case SA_FILE_CONFIG_WIFI:
728 ret = access(FLAG_FILE_WIFI, 0);
730 case SA_FILE_CONFIG_ALL:
731 ret = access(FLAG_FILE_SYSTEM, 0);
733 ret = access(FLAG_FILE_ETH, 0);
736 ret = access(FLAG_FILE_WIFI, 0);
740 _E("unknown parameter (%d)", config_type);