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>
23 #include "sa_common.h"
25 #include "sa_systemdata.h"
28 static int json_get_int_from_obj(json_object *inputObj, char *key)
30 struct json_object *bodyObj;
34 type = json_object_get_type(inputObj);
36 if (type == json_type_object) {
37 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
38 ret = json_object_get_int(bodyObj);
44 static int json_get_boolean_from_obj(json_object *inputObj, char *key)
46 struct json_object *bodyObj;
50 type = json_object_get_type(inputObj);
52 if (type == json_type_object) {
53 if (json_object_object_get_ex(inputObj, key, &bodyObj)) {
54 ret = json_object_get_boolean(bodyObj);
60 static char *json_get_string_from_obj(json_object *inputObj, char *key)
62 const char *buf = NULL;
64 json_object *elementObj = NULL;
67 if (json_object_object_get_ex(inputObj, key, &elementObj)) {
68 buf = json_object_to_json_string_ext(elementObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
69 ret_buf = malloc(strlen(buf) - 1);
70 if (ret_buf != NULL) {
71 memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude
72 for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude
75 ret_buf[j++] = buf[i];
84 _D("string object [%s]", ret_buf);
87 static int __parse_network_proxy(json_object *inputObj, sa_network_s *network)
89 char *httpProxyHost = NULL;
92 if (inputObj == NULL || network == NULL) {
93 _E("__parse_network_proxy input error");
98 httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
99 if (httpProxyHost != NULL) {
100 memcpy(network->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(network->httpProxyHost)-1));
102 httpProxyHost = NULL;
106 httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
110 static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo)
112 char *ipAddress = NULL;
113 char *netmask = NULL;
114 char *defaultGateway = NULL;
115 char *primaryDnsServer = NULL;
116 char *secondaryDnsServer = NULL;
118 if (inputObj == NULL || staticInfo == NULL) {
119 _E("__parse_network_static_info input error");
124 ipAddress = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_IPADDRESS);
125 if (ipAddress != NULL) {
126 memcpy(staticInfo->ipAddress, ipAddress, MIN(strlen(ipAddress), sizeof(staticInfo->ipAddress)-1));
132 netmask = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_NETMASK);
133 if (netmask != NULL) {
134 memcpy(staticInfo->netmask, netmask, MIN(strlen(netmask), sizeof(staticInfo->netmask)-1));
140 defaultGateway = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DEFAULTGATEWAY);
141 if (defaultGateway != NULL) {
142 memcpy(staticInfo->defaultGateway, defaultGateway, MIN(strlen(defaultGateway), sizeof(staticInfo->defaultGateway)-1));
143 free(defaultGateway);
144 defaultGateway = NULL;
148 primaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER);
149 if (primaryDnsServer != NULL) {
150 memcpy(staticInfo->primaryDnsServer, primaryDnsServer, MIN(strlen(primaryDnsServer), sizeof(staticInfo->primaryDnsServer)-1));
151 free(primaryDnsServer);
152 primaryDnsServer = NULL;
156 secondaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER);
157 if (secondaryDnsServer != NULL) {
158 memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, MIN(strlen(secondaryDnsServer), sizeof(staticInfo->secondaryDnsServer)-1));
159 free(secondaryDnsServer);
160 secondaryDnsServer = NULL;
166 static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
170 if (inputObj == NULL || eth == NULL) {
171 _E("__parse_network_eth input error");
176 eth->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
179 eth->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
180 if (eth->dhcpEnabled == FALSE) {
181 eth->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
182 if (eth->staticInfo != NULL) {
183 ret = __parse_network_static_info(inputObj, eth->staticInfo);
194 static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
197 char *password = NULL;
200 if (inputObj == NULL || wifi == NULL) {
201 _E("__parse_network_wifi input error");
205 wifi->enabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_ENABLED);
208 ssid = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_SSID);
210 memcpy(wifi->ssid, ssid, MIN(strlen(ssid), sizeof(wifi->ssid)-1));
218 password = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_WIFI_PASSWORD);
219 if (password != NULL) {
220 memcpy(wifi->password, password, MIN(strlen(password), sizeof(wifi->password)-1));
228 wifi->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED);
229 if (wifi->dhcpEnabled == FALSE) {
230 wifi->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
231 if (wifi->staticInfo != NULL) {
232 ret = __parse_network_static_info(inputObj, wifi->staticInfo);
243 static int __parse_network_data(json_object *inputObj, sa_network_s *network)
245 struct json_object *wifiObj = NULL;
246 struct json_object *ethernetObj = NULL;
247 int wifiRet = 0, ethRet = 0;
249 if (inputObj == NULL || network == NULL) {
250 _E("__parse_network_data input error");
255 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_WIFI, &wifiObj)) {
256 if (json_object_get_type(wifiObj) == json_type_object) {
258 network->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s));
259 if (network->wifi != NULL) {
260 wifiRet = __parse_network_wifi(wifiObj, network->wifi);
262 _E("network->wifi malloc fail");
268 if (json_object_object_get_ex(inputObj, SA_CONFIG_NETWORKDATA_ETHERNET, ðernetObj)) {
269 if (json_object_get_type(ethernetObj) == json_type_object) {
271 network->eth = (sa_eth_s *)malloc(sizeof(sa_eth_s));
272 if (network->eth != NULL) {
273 ethRet = __parse_network_eth(ethernetObj, network->eth);
275 _E("network->eth malloc fail");
281 __parse_network_proxy(inputObj, network);
283 // if both of network interfaces are failed, it would return -1
284 if (wifiRet != 0 && ethRet != 0) {
285 _E("__parse_network_data fail");
293 static int __parse_system_data(json_object *inputObj, sa_systemdata_s *system)
295 if (inputObj == NULL || system == NULL) {
296 _E("__parse_system_data input error");
304 static int __parse_version(json_object *inputObj, sa_config_s *setupConfig)
306 char *version = NULL;
308 if (inputObj == NULL || setupConfig == NULL) {
309 _E("__parse_version input error");
313 version = json_get_string_from_obj(inputObj, SA_CONFIG_VERSION);
315 if (version != NULL) {
316 memcpy(setupConfig->version, version, MIN(strlen(version), sizeof(setupConfig->version)-1));
324 static int __parse_config(char *file, sa_config_s *setupConfig)
326 struct json_object *configObj = NULL;
327 struct json_object *networkObj = NULL;
328 struct json_object *systemObj = NULL;
329 char *jsonData = NULL;
335 fd = open(file, O_RDONLY);
337 len = lseek(fd, 0L, SEEK_END);
340 lseek(fd, 0L, SEEK_SET);
341 jsonData = (char *)malloc(len + 1);
342 if (jsonData != NULL) {
343 memset(jsonData, 0x00, len + 1);
344 readLen = read(fd, jsonData, len);
345 _D("JSON full data[%s]", jsonData);
347 configObj = json_tokener_parse(jsonData);
349 if (configObj != NULL) {
351 ret = __parse_version(configObj, setupConfig);
355 if (json_object_object_get_ex(configObj, SA_CONFIG_SYSTEMDATA, &systemObj)) {
356 if (json_object_get_type(systemObj) == json_type_object) {
358 setupConfig->systemData = (sa_systemdata_s *)malloc(sizeof(sa_systemdata_s));
359 ret = __parse_system_data(systemObj, setupConfig->systemData);
364 // parse network data
366 if (json_object_object_get_ex(configObj, SA_CONFIG_NETWORKDATA, &networkObj)) {
367 if (json_object_get_type(networkObj) == json_type_object) {
369 setupConfig->networkData = (sa_network_s *)malloc(sizeof(sa_network_s));
370 ret = __parse_network_data(networkObj, setupConfig->networkData);
376 _E("ConfigObj is not existed");
379 json_object_put(configObj);
383 _E("memory allocation fail for jsonData");
390 _E("config file can't be opened");
396 int sa_systemdata_get_config_info(char *file, sa_config_s *config)
398 sa_error_e ret = SA_ERROR_NONE;
401 if (access(file, 0) != 0) {
402 _D("there is no config file");
403 return SA_ERROR_UNKNOWN;
405 // parsing and fill into struct
406 ret = __parse_config(file, config);
408 _D("ret:DOCKER_STATUS_UNKNOWN --> exit for user recovery");
409 return SA_ERROR_UNKNOWN;