Merge "Added tools - mockup of cloud and injector. Changed code to support task-worker."
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-parse-cmd.c
index 2d7f2e7..54c4ec2 100644 (file)
@@ -38,11 +38,11 @@ static int __parse_cmd_get_action(json_object *obj)
        json_object *action_obj = NULL;
        int action = -1;
 
-       content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
+       json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
        retvm_if(!content_obj, -1, "%s", "failed to get content");
 
-       action_obj =
-               json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_ACTION);
+       json_object_object_get_ex(
+               content_obj, TTD_CMD_KEY_CONTENT_ACTION, &action_obj);
        retvm_if(!action_obj, -1, "%s", "failed to get action");
 
        action = json_object_get_int(action_obj);
@@ -74,11 +74,7 @@ static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
 
 static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
 {
-       json_object *content_obj = NULL;
-       json_object *info_obj = NULL;
-       void *info_data = NULL;
        int action = -1;
-       int ret = 0;
 
        action = __parse_cmd_get_action(obj);
        retv_if(action < 0, -1);
@@ -87,16 +83,22 @@ static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
        case TTD_CMD_CONFIG_SET_SERVER_URL:
        case TTD_CMD_CONFIG_SET_APP_ID:
        {
+               json_object *content_obj = NULL;
+               json_object *info_obj = NULL;
+               void *info_data = NULL;
+               int ret = 0;
+
                ret = ttd_cmd_set_command(cmd, action);
                if (ret < 0) {
                        _E("Failed to set action");
                        return -1;
                }
 
-               content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
+               json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
                retvm_if(!content_obj, -1, "%s", "failed to get content");
 
-               info_obj = json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_INFO);
+               json_object_object_get_ex(
+                       content_obj, TTD_CMD_KEY_CONTENT_INFO, &info_obj);
                retvm_if(!info_obj, -1, "failed to get content information");
 
                info_data = g_strdup(json_object_to_json_string(info_obj));
@@ -138,7 +140,7 @@ static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
                gchar *extra_data = NULL;
 
                ret = ttd_cmd_set_command(cmd, action);
-               extra_obj = json_object_object_get(obj, TTD_CMD_KEY_EXTRA);
+               json_object_object_get_ex(obj, TTD_CMD_KEY_EXTRA, &extra_obj);
                retvm_if(!extra_obj, -1, "failed to get extra information");
 
                extra_data = g_strdup(json_object_to_json_string(extra_obj));
@@ -271,7 +273,13 @@ int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
                return -1;
        }
 
-       items_obj = json_object_object_get(root_obj, TTD_CMD_KEY_ITEMS);
+       json_object_object_get_ex(root_obj, TTD_CMD_KEY_ITEMS, &items_obj);
+       if (!items_obj) {
+               _E("There are no cmd items");
+               json_object_put(root_obj);
+               return -1;
+       }
+
        items_type = json_object_get_type(items_obj);
        if (items_type != json_type_array) {
                _E("cmd items are not array type (is %d)", items_type);
@@ -299,12 +307,26 @@ int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
                        continue;
                }
 
-               temp_obj = json_object_object_get(obj, TTD_CMD_KEY_ID);
+               json_object_object_get_ex(obj, TTD_CMD_KEY_ID, &temp_obj);
+               if (!temp_obj) {
+                       _E("failed to get cmd id object - %s", json_object_get_string(obj));
+                       continue;
+               }
+
                cmd_id = json_object_get_string(temp_obj);
                if (!cmd_id) {
                        _E("failed to get cmd id - %s", json_object_get_string(obj));
                        continue;
                }
+               temp_obj = NULL;
+
+               json_object_object_get_ex(obj, TTD_CMD_KEY_TYPE, &temp_obj);
+               if (!temp_obj) {
+                       _E("failed to get cmd type object - %s", json_object_get_string(obj));
+                       continue;
+               }
+               cmd_type = json_object_get_int(temp_obj);
+               temp_obj = NULL;
 
                cmd_data = ttd_cmd_new(cmd_id);
                if (!cmd_data) {
@@ -313,9 +335,6 @@ int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
                }
 
                ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
-
-               temp_obj = json_object_object_get(obj, TTD_CMD_KEY_TYPE);
-               cmd_type = json_object_get_int(temp_obj);
                ttd_cmd_set_type(cmd_data, cmd_type);
 
                ret = __parse_cmd_by_type(obj, cmd_data);