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
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <glib.h>
18 #include <json.h>
19 #include <string.h>
20 #include "ttd-log.h"
21 #include "ttd-cmd-type.h"
22 #include "ttd-cmd.h"
23
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"
34
35 static int __parse_cmd_get_action(json_object *obj)
36 {
37         json_object *content_obj = NULL;
38         json_object *action_obj = NULL;
39         int action = -1;
40
41         content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
42         retvm_if(!content_obj, -1, "%s", "failed to get content");
43
44         action_obj =
45                 json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_ACTION);
46         retvm_if(!action_obj, -1, "%s", "failed to get action");
47
48         action = json_object_get_int(action_obj);
49
50         return action;
51 }
52
53 static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
54 {
55         int action = 0;
56         int ret = 0;
57
58         action = __parse_cmd_get_action(obj);
59         retv_if(action < 0, -1);
60
61         switch (action) {
62         case TTD_CMD_POWER_OFF:
63         case TTD_CMD_POWER_RESTART:
64                 ret = ttd_cmd_set_command(cmd, action);
65                 break;
66         case TTD_CMD_POWER_MAX:
67         default:
68                 _E("Unknown action : %d", action);
69                 return -1;
70                 break;
71         }
72         return ret;
73 }
74
75 static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
76 {
77         json_object *content_obj = NULL;
78         json_object *info_obj = NULL;
79         void *info_data = NULL;
80         int action = -1;
81         int ret = 0;
82
83         action = __parse_cmd_get_action(obj);
84         retv_if(action < 0, -1);
85
86         switch (action) {
87         case TTD_CMD_CONFIG_SET_SERVER_URL:
88         case TTD_CMD_CONFIG_SET_APP_ID:
89         {
90                 ret = ttd_cmd_set_command(cmd, action);
91                 if (ret < 0) {
92                         _E("Failed to set action");
93                         return -1;
94                 }
95
96                 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
97                 retvm_if(!content_obj, -1, "%s", "failed to get content");
98
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");
101
102                 info_data = g_strdup(json_object_to_json_string(info_obj));
103                 if (info_data)
104                         ttd_cmd_set_data(cmd, info_data, strlen(info_data), g_free);
105                 else {
106                         _E("failed to get extra content information");
107                         return -1;
108                 }
109                 break;
110         }
111         case TTD_CMD_CONFIG_ACTION_MAX:
112         default:
113                 _E("Unknown action : %d", action);
114                 return -1;
115                 break;
116         }
117
118         return 0;
119 }
120
121 static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
122 {
123         int action = 0;
124         int ret = 0;
125
126         action = __parse_cmd_get_action(obj);
127         retv_if(action < 0, -1);
128
129         switch (action) {
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);
134                 break;
135         case TTD_CMD_PACKAGE_INSTALL:
136         {
137                 json_object *extra_obj = NULL;
138                 gchar *extra_data = NULL;
139
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");
143
144                 extra_data = g_strdup(json_object_to_json_string(extra_obj));
145                 if (extra_data)
146                         ttd_cmd_set_data(cmd, extra_data, strlen(extra_data), g_free);
147                 else {
148                         _E("failed to get extra information");
149                         return -1;
150                 }
151         }
152                 break;
153         case TTD_CMD_PACKAGE_MAX:
154         default:
155                 _E("Unknown action : %d", action);
156                 return -1;
157                 break;
158         }
159         return ret;
160 }
161
162 static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
163 {
164         int action = 0;
165         int ret = 0;
166
167         action = __parse_cmd_get_action(obj);
168         retv_if(action < 0, -1);
169
170         switch (action) {
171         case TTD_CMD_DIAGNOSIS_GET_LOG:
172                 ret = ttd_cmd_set_command(cmd, action);
173                 break;
174         case TTD_CMD_DIAGNOSIS_MAX:
175         default:
176                 _E("Unknown action : %d", action);
177                 return -1;
178                 break;
179         }
180         return ret;
181 }
182
183 static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
184 {
185         json_object *content_obj = NULL;
186         json_object *json_obj = NULL;
187         void *json_data = NULL;
188         int action = 0;
189         int ret = 0;
190
191         action = __parse_cmd_get_action(obj);
192         retv_if(action < 0, -1);
193
194         switch (action) {
195         case TTD_CMD_INFO_GET_SYSINFO:
196         case TTD_CMD_INFO_GET_TASKINFO:
197
198                 content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
199                 retvm_if(!content_obj, -1, "%s", "failed to get content");
200
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));
204                 if (json_data)
205                         ttd_cmd_set_data(cmd, json_data, strlen(json_data), g_free);
206                 else {
207                         _E("failed to get json from content");
208                         return -1;
209                 }
210
211                 ret = ttd_cmd_set_command(cmd, action);
212                 break;
213         case TTD_CMD_INFO_GET_MAX:
214         default:
215                 _E("Unknown action : %d", action);
216                 return -1;
217                 break;
218         }
219         return ret;
220 }
221
222 static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
223 {
224         int ret = 0;
225         retvm_if(!obj, -1, "obj is NULL");
226         retvm_if(!cmd, -1, "cmd is NULL");
227
228         switch (ttd_cmd_get_type(cmd)) {
229         case TTD_CMD_TYPE_POWER:
230                 ret = __parse_cmd_power(obj, cmd);
231                 break;
232         case TTD_CMD_TYPE_CONFIG:
233                 ret = __parse_cmd_config(obj, cmd);
234                 break;
235         case TTD_CMD_TYPE_PACKAGE:
236                 ret = __parse_cmd_package(obj, cmd);
237                 break;
238         case TTD_CMD_TYPE_DIAGNOSIS:
239                 ret = __parse_cmd_diagnosis(obj, cmd);
240                 break;
241         case TTD_CMD_TYPE_INFO:
242                 ret = __parse_cmd_info(obj, cmd);
243                 break;
244         case TTD_CMD_TYPE_LOCAL:
245         case TTD_CMD_TYPE_UNKNOWN:
246         case TTD_CMD_TYPE_MAX:
247         default:
248                 _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
249                 ret = -1;
250                 break;
251         }
252         return ret;
253 }
254
255 int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
256 {
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;
261         int cmd_length = 0;
262         int i;
263         GList *list = NULL;
264
265         retvm_if(!json_str, -1, "json_str is NULL");
266         retvm_if(!cmd_list, -1, "cmd is NULL");
267
268         root_obj = json_tokener_parse(json_str);
269         if (!root_obj) {
270                 _E("cmd body is not json");
271                 return -1;
272         }
273
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);
279                 return -1;
280         }
281
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);
286                 return 0;
287         }
288
289         for (i = 0; i < cmd_length; i++) {
290                 json_object *obj = NULL;
291                 json_object *temp_obj = NULL;
292                 int cmd_type = 0;
293                 const char *cmd_id = NULL;
294                 int ret = 0;
295
296                 obj = json_object_array_get_idx(items_obj, i);
297                 if (!obj) {
298                         _E("failed to get object in cmd array");
299                         continue;
300                 }
301
302                 temp_obj = json_object_object_get(obj, TTD_CMD_KEY_ID);
303                 cmd_id = json_object_get_string(temp_obj);
304                 if (!cmd_id) {
305                         _E("failed to get cmd id - %s", json_object_get_string(obj));
306                         continue;
307                 }
308
309                 cmd_data = ttd_cmd_new(cmd_id);
310                 if (!cmd_data) {
311                         _E("failed to create cmd - %s", json_object_get_string(obj));
312                         continue;
313                 }
314
315                 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
316
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);
320
321                 ret = __parse_cmd_by_type(obj, cmd_data);
322                 if (ret) {
323                         _E("failed to parse cmd - %s", json_object_get_string(obj));
324                         ttd_cmd_free(cmd_data);
325                         continue;
326                 }
327
328                 list = g_list_append(list, cmd_data);
329         }
330         json_object_put(root_obj);
331         *cmd_list = list;
332
333         return 0;
334 }