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_CONTENT_JSON "json"
33 #define TTD_CMD_KEY_EXTRA "extra"
35 static int __parse_cmd_get_action(json_object *obj)
37 json_object *content_obj = NULL;
38 json_object *action_obj = NULL;
41 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
42 retvm_if(!content_obj, -1, "%s", "failed to get content");
45 json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_ACTION);
46 retvm_if(!action_obj, -1, "%s", "failed to get action");
48 action = json_object_get_int(action_obj);
53 static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
58 action = __parse_cmd_get_action(obj);
59 retv_if(action < 0, -1);
62 case TTD_CMD_POWER_OFF:
63 case TTD_CMD_POWER_RESTART:
64 ret = ttd_cmd_set_command(cmd, action);
66 case TTD_CMD_POWER_MAX:
68 _E("Unknown action : %d", action);
75 static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
77 json_object *content_obj = NULL;
78 json_object *info_obj = NULL;
79 void *info_data = NULL;
83 action = __parse_cmd_get_action(obj);
84 retv_if(action < 0, -1);
87 case TTD_CMD_CONFIG_SET_SERVER_URL:
88 case TTD_CMD_CONFIG_SET_APP_ID:
90 ret = ttd_cmd_set_command(cmd, action);
92 _E("Failed to set action");
96 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
97 retvm_if(!content_obj, -1, "%s", "failed to get content");
99 info_obj = json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_INFO);
100 retvm_if(!info_obj, -1, "failed to get content information");
102 info_data = g_strdup(json_object_to_json_string(info_obj));
104 ttd_cmd_set_data(cmd, info_data, strlen(info_data), g_free);
106 _E("failed to get extra content information");
111 case TTD_CMD_CONFIG_ACTION_MAX:
113 _E("Unknown action : %d", action);
121 static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
126 action = __parse_cmd_get_action(obj);
127 retv_if(action < 0, -1);
130 case TTD_CMD_PACKAGE_REMOVE:
131 case TTD_CMD_PACKAGE_GET_APP_LIST:
132 case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
133 ret = ttd_cmd_set_command(cmd, action);
135 case TTD_CMD_PACKAGE_INSTALL:
137 json_object *extra_obj = NULL;
138 gchar *extra_data = NULL;
140 ret = ttd_cmd_set_command(cmd, action);
141 extra_obj = json_object_object_get(obj, TTD_CMD_KEY_EXTRA);
142 retvm_if(!extra_obj, -1, "failed to get extra information");
144 extra_data = g_strdup(json_object_to_json_string(extra_obj));
146 ttd_cmd_set_data(cmd, extra_data, strlen(extra_data), g_free);
148 _E("failed to get extra information");
153 case TTD_CMD_PACKAGE_MAX:
155 _E("Unknown action : %d", action);
162 static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
167 action = __parse_cmd_get_action(obj);
168 retv_if(action < 0, -1);
171 case TTD_CMD_DIAGNOSIS_GET_LOG:
172 ret = ttd_cmd_set_command(cmd, action);
174 case TTD_CMD_DIAGNOSIS_MAX:
176 _E("Unknown action : %d", action);
183 static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
185 json_object *content_obj = NULL;
186 json_object *json_obj = NULL;
187 void *json_data = NULL;
191 action = __parse_cmd_get_action(obj);
192 retv_if(action < 0, -1);
195 case TTD_CMD_INFO_GET_SYSINFO:
196 case TTD_CMD_INFO_GET_TASKINFO:
198 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
199 retvm_if(!content_obj, -1, "%s", "failed to get content");
201 json_obj = json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_JSON);
202 retvm_if(!json_obj, -1, "failed to get content json");
203 json_data = g_strdup(json_object_get_string(json_obj));
205 ttd_cmd_set_data(cmd, json_data, strlen(json_data), g_free);
207 _E("failed to get json from content");
211 ret = ttd_cmd_set_command(cmd, action);
213 case TTD_CMD_INFO_GET_MAX:
215 _E("Unknown action : %d", action);
222 static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
225 retvm_if(!obj, -1, "obj is NULL");
226 retvm_if(!cmd, -1, "cmd is NULL");
228 switch (ttd_cmd_get_type(cmd)) {
229 case TTD_CMD_TYPE_POWER:
230 ret = __parse_cmd_power(obj, cmd);
232 case TTD_CMD_TYPE_CONFIG:
233 ret = __parse_cmd_config(obj, cmd);
235 case TTD_CMD_TYPE_PACKAGE:
236 ret = __parse_cmd_package(obj, cmd);
238 case TTD_CMD_TYPE_DIAGNOSIS:
239 ret = __parse_cmd_diagnosis(obj, cmd);
241 case TTD_CMD_TYPE_INFO:
242 ret = __parse_cmd_info(obj, cmd);
244 case TTD_CMD_TYPE_LOCAL:
245 case TTD_CMD_TYPE_UNKNOWN:
246 case TTD_CMD_TYPE_MAX:
248 _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
255 int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
257 json_object *root_obj = NULL;
258 json_object *items_obj = NULL;
259 ttd_cmd_data *cmd_data = NULL;
260 enum json_type items_type = json_type_null;
265 retvm_if(!json_str, -1, "json_str is NULL");
266 retvm_if(!cmd_list, -1, "cmd is NULL");
268 root_obj = json_tokener_parse(json_str);
270 _E("cmd body is not json");
274 items_obj = json_object_object_get(root_obj, TTD_CMD_KEY_ITEMS);
275 items_type = json_object_get_type(items_obj);
276 if (items_type != json_type_array) {
277 _E("cmd items are not array type (is %d)", items_type);
278 json_object_put(root_obj);
282 cmd_length = json_object_array_length(items_obj);
283 if (cmd_length <= 0) {
284 _W("Nothing in the cmd array");
285 json_object_put(root_obj);
289 for (i = 0; i < cmd_length; i++) {
290 json_object *obj = NULL;
291 json_object *temp_obj = NULL;
293 const char *cmd_id = NULL;
296 obj = json_object_array_get_idx(items_obj, i);
298 _E("failed to get object in cmd array");
302 temp_obj = json_object_object_get(obj, TTD_CMD_KEY_ID);
303 cmd_id = json_object_get_string(temp_obj);
305 _E("failed to get cmd id - %s", json_object_get_string(obj));
309 cmd_data = ttd_cmd_new(cmd_id);
311 _E("failed to create cmd - %s", json_object_get_string(obj));
315 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
317 temp_obj = json_object_object_get(obj, TTD_CMD_KEY_TYPE);
318 cmd_type = json_object_get_int(temp_obj);
319 ttd_cmd_set_type(cmd_data, cmd_type);
321 ret = __parse_cmd_by_type(obj, cmd_data);
323 _E("failed to parse cmd - %s", json_object_get_string(obj));
324 ttd_cmd_free(cmd_data);
328 list = g_list_append(list, cmd_data);
330 json_object_put(root_obj);