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 json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
41 retvm_if(!content_obj, -1, "%s", "failed to get content");
43 json_object_object_get_ex(
44 content_obj, TTD_CMD_KEY_CONTENT_ACTION, &action_obj);
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)
78 action = __parse_cmd_get_action(obj);
79 retv_if(action < 0, -1);
82 case TTD_CMD_CONFIG_SET_SERVER_URL:
83 case TTD_CMD_CONFIG_SET_APP_ID:
85 json_object *content_obj = NULL;
86 json_object *info_obj = NULL;
87 void *info_data = NULL;
90 ret = ttd_cmd_set_command(cmd, action);
92 _E("Failed to set action");
96 json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
97 retvm_if(!content_obj, -1, "%s", "failed to get content");
99 json_object_object_get_ex(
100 content_obj, TTD_CMD_KEY_CONTENT_INFO, &info_obj);
101 retvm_if(!info_obj, -1, "failed to get content information");
103 info_data = g_strdup(json_object_to_json_string(info_obj));
105 ttd_cmd_set_data(cmd, info_data, strlen(info_data), g_free);
107 _E("failed to get extra content information");
112 case TTD_CMD_CONFIG_ACTION_MAX:
114 _E("Unknown action : %d", action);
122 static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
127 action = __parse_cmd_get_action(obj);
128 retv_if(action < 0, -1);
131 case TTD_CMD_PACKAGE_REMOVE:
132 case TTD_CMD_PACKAGE_GET_APP_LIST:
133 case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
134 ret = ttd_cmd_set_command(cmd, action);
136 case TTD_CMD_PACKAGE_INSTALL:
138 json_object *extra_obj = NULL;
139 gchar *extra_data = NULL;
141 ret = ttd_cmd_set_command(cmd, action);
142 json_object_object_get_ex(obj, TTD_CMD_KEY_EXTRA, &extra_obj);
143 retvm_if(!extra_obj, -1, "failed to get extra information");
145 extra_data = g_strdup(json_object_to_json_string(extra_obj));
147 ttd_cmd_set_data(cmd, extra_data, strlen(extra_data), g_free);
149 _E("failed to get extra information");
154 case TTD_CMD_PACKAGE_MAX:
156 _E("Unknown action : %d", action);
163 static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
168 action = __parse_cmd_get_action(obj);
169 retv_if(action < 0, -1);
172 case TTD_CMD_DIAGNOSIS_GET_LOG:
173 ret = ttd_cmd_set_command(cmd, action);
175 case TTD_CMD_DIAGNOSIS_MAX:
177 _E("Unknown action : %d", action);
184 static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
189 action = __parse_cmd_get_action(obj);
190 retv_if(action < 0, -1);
193 case TTD_CMD_INFO_GET_SYSINFO:
194 case TTD_CMD_INFO_GET_TASKINFO:
195 ret = ttd_cmd_set_command(cmd, action);
197 case TTD_CMD_INFO_GET_MAX:
199 _E("Unknown action : %d", action);
206 static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
209 retvm_if(!obj, -1, "obj is NULL");
210 retvm_if(!cmd, -1, "cmd is NULL");
212 switch (ttd_cmd_get_type(cmd)) {
213 case TTD_CMD_TYPE_POWER:
214 ret = __parse_cmd_power(obj, cmd);
216 case TTD_CMD_TYPE_CONFIG:
217 ret = __parse_cmd_config(obj, cmd);
219 case TTD_CMD_TYPE_PACKAGE:
220 ret = __parse_cmd_package(obj, cmd);
222 case TTD_CMD_TYPE_DIAGNOSIS:
223 ret = __parse_cmd_diagnosis(obj, cmd);
225 case TTD_CMD_TYPE_INFO:
226 ret = __parse_cmd_info(obj, cmd);
228 case TTD_CMD_TYPE_LOCAL:
229 case TTD_CMD_TYPE_UNKNOWN:
230 case TTD_CMD_TYPE_MAX:
232 _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
239 int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
241 json_object *root_obj = NULL;
242 json_object *items_obj = NULL;
243 ttd_cmd_data *cmd_data = NULL;
244 enum json_type items_type = json_type_null;
249 retvm_if(!json_str, -1, "json_str is NULL");
250 retvm_if(!cmd_list, -1, "cmd is NULL");
252 root_obj = json_tokener_parse(json_str);
254 _E("cmd body is not json");
258 json_object_object_get_ex(root_obj, TTD_CMD_KEY_ITEMS, &items_obj);
260 _E("There are no cmd items");
261 json_object_put(root_obj);
265 items_type = json_object_get_type(items_obj);
266 if (items_type != json_type_array) {
267 _E("cmd items are not array type");
268 json_object_put(root_obj);
272 cmd_length = json_object_array_length(items_obj);
273 if (cmd_length <= 0) {
274 _W("Nothing in the cmd array");
275 json_object_put(root_obj);
279 for (i = 0; i < cmd_length; i++) {
280 json_object *obj = NULL;
281 json_object *temp_obj = NULL;
283 const char *cmd_id = NULL;
286 obj = json_object_array_get_idx(items_obj, i);
288 _E("failed to get object in cmd array");
292 json_object_object_get_ex(obj, TTD_CMD_KEY_ID, &temp_obj);
294 _E("failed to get cmd id object - %s", json_object_get_string(obj));
298 cmd_id = json_object_get_string(temp_obj);
300 _E("failed to get cmd id - %s", json_object_get_string(obj));
305 json_object_object_get_ex(obj, TTD_CMD_KEY_TYPE, &temp_obj);
307 _E("failed to get cmd type object - %s", json_object_get_string(obj));
310 cmd_type = json_object_get_int(temp_obj);
313 cmd_data = ttd_cmd_new(cmd_id);
315 _E("failed to create cmd - %s", json_object_get_string(obj));
319 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
320 ttd_cmd_set_type(cmd_data, cmd_type);
322 ret = __parse_cmd_by_type(obj, cmd_data);
324 _E("failed to parse cmd - %s", json_object_get_string(obj));
325 ttd_cmd_free(cmd_data);
329 list = g_list_append(list, cmd_data);
331 json_object_put(root_obj);