2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 "ttd-cmd-type.h"
24 #define TTD_CMD_KEY_ITEMS "Items"
25 #define TTD_CMD_KEY_TYPE "type"
26 #define TTD_CMD_KEY_ID "id"
27 #define TTD_CMD_KEY_STATE "state"
28 #define TTD_CMD_KEY_STATE_CREATED "created"
29 #define TTD_CMD_KEY_CONTENT "content"
30 #define TTD_CMD_KEY_CONTENT_ACTION "action"
31 #define TTD_CMD_KEY_CONTENT_INFO "info"
32 #define TTD_CMD_KEY_EXTRA "extra"
34 static int __parse_cmd_get_action(json_object *obj)
36 json_object *content_obj = NULL;
37 json_object *action_obj = NULL;
40 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
41 retvm_if(!content_obj, -1, "%s", "failed to get content");
44 json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_ACTION);
45 retvm_if(!action_obj, -1, "%s", "failed to get action");
47 action = json_object_get_int(action_obj);
52 static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
57 action = __parse_cmd_get_action(obj);
58 retv_if(action < 0, -1);
61 case TTD_CMD_POWER_OFF:
62 case TTD_CMD_POWER_RESTART:
63 ret = ttd_cmd_set_command(cmd, action);
65 case TTD_CMD_POWER_MAX:
67 _E("Unknown action : %d", action);
74 static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
76 json_object *content_obj = NULL;
77 json_object *info_obj = NULL;
78 void *info_data = NULL;
82 action = __parse_cmd_get_action(obj);
83 retv_if(action < 0, -1);
86 case TTD_CMD_CONFIG_SET_SERVER_URL:
87 case TTD_CMD_CONFIG_SET_APP_ID:
89 ret = ttd_cmd_set_command(cmd, action);
91 _E("Failed to set action");
95 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
96 retvm_if(!content_obj, -1, "%s", "failed to get content");
98 info_obj = json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_INFO);
99 retvm_if(!info_obj, -1, "failed to get content information");
101 info_data = g_strdup(json_object_to_json_string(info_obj));
103 ttd_cmd_set_data(cmd, info_data, strlen(info_data), g_free);
105 _E("failed to get extra content information");
110 case TTD_CMD_CONFIG_ACTION_MAX:
112 _E("Unknown action : %d", action);
120 static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
125 action = __parse_cmd_get_action(obj);
126 retv_if(action < 0, -1);
129 case TTD_CMD_PACKAGE_REMOVE:
130 case TTD_CMD_PACKAGE_GET_APP_LIST:
131 case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
132 ret = ttd_cmd_set_command(cmd, action);
134 case TTD_CMD_PACKAGE_INSTALL:
136 json_object *extra_obj = NULL;
137 gchar *extra_data = NULL;
139 ret = ttd_cmd_set_command(cmd, action);
140 extra_obj = json_object_object_get(obj, TTD_CMD_KEY_EXTRA);
141 retvm_if(!extra_obj, -1, "failed to get extra information");
143 extra_data = g_strdup(json_object_to_json_string(extra_obj));
145 ttd_cmd_set_data(cmd, extra_data, strlen(extra_data), g_free);
147 _E("failed to get extra information");
152 case TTD_CMD_PACKAGE_MAX:
154 _E("Unknown action : %d", action);
161 static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
166 action = __parse_cmd_get_action(obj);
167 retv_if(action < 0, -1);
170 case TTD_CMD_DIAGNOSIS_GET_LOG:
171 ret = ttd_cmd_set_command(cmd, action);
173 case TTD_CMD_DIAGNOSIS_MAX:
175 _E("Unknown action : %d", action);
182 static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
187 action = __parse_cmd_get_action(obj);
188 retv_if(action < 0, -1);
191 case TTD_CMD_INFO_GET_SYSINFO:
192 case TTD_CMD_INFO_GET_TASKINFO:
193 ret = ttd_cmd_set_command(cmd, action);
195 case TTD_CMD_INFO_GET_MAX:
197 _E("Unknown action : %d", action);
204 static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
207 retvm_if(!obj, -1, "obj is NULL");
208 retvm_if(!cmd, -1, "cmd is NULL");
210 switch (ttd_cmd_get_type(cmd)) {
211 case TTD_CMD_TYPE_POWER:
212 ret = __parse_cmd_power(obj, cmd);
214 case TTD_CMD_TYPE_CONFIG:
215 ret = __parse_cmd_config(obj, cmd);
217 case TTD_CMD_TYPE_PACKAGE:
218 ret = __parse_cmd_package(obj, cmd);
220 case TTD_CMD_TYPE_DIAGNOSIS:
221 ret = __parse_cmd_diagnosis(obj, cmd);
223 case TTD_CMD_TYPE_INFO:
224 ret = __parse_cmd_info(obj, cmd);
226 case TTD_CMD_TYPE_LOCAL:
227 case TTD_CMD_TYPE_UNKNOWN:
228 case TTD_CMD_TYPE_MAX:
230 _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
237 int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
239 json_object *root_obj = NULL;
240 json_object *items_obj = NULL;
241 ttd_cmd_data *cmd_data = NULL;
242 enum json_type items_type = json_type_null;
247 retvm_if(!json_str, -1, "json_str is NULL");
248 retvm_if(!cmd_list, -1, "cmd is NULL");
250 root_obj = json_tokener_parse(json_str);
252 _E("cmd body is not json");
256 items_obj = json_object_object_get(root_obj, TTD_CMD_KEY_ITEMS);
257 items_type = json_object_get_type(items_obj);
258 if (items_type != json_type_array) {
259 _E("cmd items are not array type");
260 json_object_put(root_obj);
264 cmd_length = json_object_array_length(items_obj);
265 if (cmd_length <= 0) {
266 _E("Nothing in the cmd array");
267 json_object_put(root_obj);
271 for (i = 0; i < cmd_length; i++) {
272 json_object *obj = NULL;
273 json_object *temp_obj = NULL;
275 const char *cmd_id = NULL;
278 obj = json_object_array_get_idx(items_obj, i);
280 _E("failed to get object in cmd array");
284 temp_obj = json_object_object_get(obj, TTD_CMD_KEY_ID);
285 cmd_id = json_object_get_string(temp_obj);
287 _E("failed to get cmd id - %s", json_object_get_string(obj));
291 cmd_data = ttd_cmd_new(cmd_id);
293 _E("failed to create cmd - %s", json_object_get_string(obj));
297 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
299 temp_obj = json_object_object_get(obj, TTD_CMD_KEY_TYPE);
300 cmd_type = json_object_get_int(temp_obj);
301 ttd_cmd_set_type(cmd_data, cmd_type);
303 ret = __parse_cmd_by_type(obj, cmd_data);
305 _E("failed to parse cmd - %s", json_object_get_string(obj));
306 ttd_cmd_free(cmd_data);
310 list = g_list_append(list, cmd_data);
312 json_object_put(root_obj);