add project name as argument for create token
[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         json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
41         retvm_if(!content_obj, -1, "%s", "failed to get content");
42
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");
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         int action = -1;
77
78         action = __parse_cmd_get_action(obj);
79         retv_if(action < 0, -1);
80
81         switch (action) {
82         case TTD_CMD_CONFIG_SET_SERVER_URL:
83         case TTD_CMD_CONFIG_SET_APP_ID:
84         {
85                 json_object *content_obj = NULL;
86                 json_object *info_obj = NULL;
87                 void *info_data = NULL;
88                 int ret = 0;
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                 json_object_object_get_ex(obj, TTD_CMD_KEY_CONTENT, &content_obj);
97                 retvm_if(!content_obj, -1, "%s", "failed to get content");
98
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");
102
103                 info_data = g_strdup(json_object_to_json_string(info_obj));
104                 if (info_data)
105                         ttd_cmd_set_data(cmd, info_data, strlen(info_data), g_free);
106                 else {
107                         _E("failed to get extra content information");
108                         return -1;
109                 }
110                 break;
111         }
112         case TTD_CMD_CONFIG_ACTION_MAX:
113         default:
114                 _E("Unknown action : %d", action);
115                 return -1;
116                 break;
117         }
118
119         return 0;
120 }
121
122 static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
123 {
124         int action = 0;
125         int ret = 0;
126
127         action = __parse_cmd_get_action(obj);
128         retv_if(action < 0, -1);
129
130         switch (action) {
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);
135                 break;
136         case TTD_CMD_PACKAGE_INSTALL:
137         {
138                 json_object *extra_obj = NULL;
139                 gchar *extra_data = NULL;
140
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");
144
145                 extra_data = g_strdup(json_object_to_json_string(extra_obj));
146                 if (extra_data)
147                         ttd_cmd_set_data(cmd, extra_data, strlen(extra_data), g_free);
148                 else {
149                         _E("failed to get extra information");
150                         return -1;
151                 }
152         }
153                 break;
154         case TTD_CMD_PACKAGE_MAX:
155         default:
156                 _E("Unknown action : %d", action);
157                 return -1;
158                 break;
159         }
160         return ret;
161 }
162
163 static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
164 {
165         int action = 0;
166         int ret = 0;
167
168         action = __parse_cmd_get_action(obj);
169         retv_if(action < 0, -1);
170
171         switch (action) {
172         case TTD_CMD_DIAGNOSIS_GET_LOG:
173                 ret = ttd_cmd_set_command(cmd, action);
174                 break;
175         case TTD_CMD_DIAGNOSIS_MAX:
176         default:
177                 _E("Unknown action : %d", action);
178                 return -1;
179                 break;
180         }
181         return ret;
182 }
183
184 static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
185 {
186         int action = 0;
187         int ret = 0;
188
189         action = __parse_cmd_get_action(obj);
190         retv_if(action < 0, -1);
191
192         switch (action) {
193         case TTD_CMD_INFO_GET_SYSINFO:
194         case TTD_CMD_INFO_GET_TASKINFO:
195                 ret = ttd_cmd_set_command(cmd, action);
196                 break;
197         case TTD_CMD_INFO_GET_MAX:
198         default:
199                 _E("Unknown action : %d", action);
200                 return -1;
201                 break;
202         }
203         return ret;
204 }
205
206 static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
207 {
208         int ret = 0;
209         retvm_if(!obj, -1, "obj is NULL");
210         retvm_if(!cmd, -1, "cmd is NULL");
211
212         switch (ttd_cmd_get_type(cmd)) {
213         case TTD_CMD_TYPE_POWER:
214                 ret = __parse_cmd_power(obj, cmd);
215                 break;
216         case TTD_CMD_TYPE_CONFIG:
217                 ret = __parse_cmd_config(obj, cmd);
218                 break;
219         case TTD_CMD_TYPE_PACKAGE:
220                 ret = __parse_cmd_package(obj, cmd);
221                 break;
222         case TTD_CMD_TYPE_DIAGNOSIS:
223                 ret = __parse_cmd_diagnosis(obj, cmd);
224                 break;
225         case TTD_CMD_TYPE_INFO:
226                 ret = __parse_cmd_info(obj, cmd);
227                 break;
228         case TTD_CMD_TYPE_LOCAL:
229         case TTD_CMD_TYPE_UNKNOWN:
230         case TTD_CMD_TYPE_MAX:
231         default:
232                 _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
233                 ret = -1;
234                 break;
235         }
236         return ret;
237 }
238
239 int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
240 {
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;
245         int cmd_length = 0;
246         int i;
247         GList *list = NULL;
248
249         retvm_if(!json_str, -1, "json_str is NULL");
250         retvm_if(!cmd_list, -1, "cmd is NULL");
251
252         root_obj = json_tokener_parse(json_str);
253         if (!root_obj) {
254                 _E("cmd body is not json");
255                 return -1;
256         }
257
258         json_object_object_get_ex(root_obj, TTD_CMD_KEY_ITEMS, &items_obj);
259         if (!items_obj) {
260                 _E("There are no cmd items");
261                 json_object_put(root_obj);
262                 return -1;
263         }
264
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);
269                 return -1;
270         }
271
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);
276                 return 0;
277         }
278
279         for (i = 0; i < cmd_length; i++) {
280                 json_object *obj = NULL;
281                 json_object *temp_obj = NULL;
282                 int cmd_type = 0;
283                 const char *cmd_id = NULL;
284                 int ret = 0;
285
286                 obj = json_object_array_get_idx(items_obj, i);
287                 if (!obj) {
288                         _E("failed to get object in cmd array");
289                         continue;
290                 }
291
292                 json_object_object_get_ex(obj, TTD_CMD_KEY_ID, &temp_obj);
293                 if (!temp_obj) {
294                         _E("failed to get cmd id object - %s", json_object_get_string(obj));
295                         continue;
296                 }
297
298                 cmd_id = json_object_get_string(temp_obj);
299                 if (!cmd_id) {
300                         _E("failed to get cmd id - %s", json_object_get_string(obj));
301                         continue;
302                 }
303                 temp_obj = NULL;
304
305                 json_object_object_get_ex(obj, TTD_CMD_KEY_TYPE, &temp_obj);
306                 if (!temp_obj) {
307                         _E("failed to get cmd type object - %s", json_object_get_string(obj));
308                         continue;
309                 }
310                 cmd_type = json_object_get_int(temp_obj);
311                 temp_obj = NULL;
312
313                 cmd_data = ttd_cmd_new(cmd_id);
314                 if (!cmd_data) {
315                         _E("failed to create cmd - %s", json_object_get_string(obj));
316                         continue;
317                 }
318
319                 ttd_cmd_set_state(cmd_data, TTD_CMD_STATE_CREATED);
320                 ttd_cmd_set_type(cmd_data, cmd_type);
321
322                 ret = __parse_cmd_by_type(obj, cmd_data);
323                 if (ret) {
324                         _E("failed to parse cmd - %s", json_object_get_string(obj));
325                         ttd_cmd_free(cmd_data);
326                         continue;
327                 }
328
329                 list = g_list_append(list, cmd_data);
330         }
331         json_object_put(root_obj);
332         *cmd_list = list;
333
334         return 0;
335 }