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