implements json command parsing and adds new module for command data 55/177555/4
authorJeonghoon Park <jh1979.park@samsung.com>
Wed, 2 May 2018 02:20:52 +0000 (11:20 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 3 May 2018 05:41:21 +0000 (14:41 +0900)
Change-Id: I64d2cc268342f5a5ec64fce7b8d118a589c6229a

daemon/include/ttd-cmd-type.h
daemon/include/ttd-cmd.h [new file with mode: 0644]
daemon/include/ttd-parse-cmd.h
daemon/src/tizen-things-daemon.c
daemon/src/ttd-cmd.c [new file with mode: 0644]
daemon/src/ttd-parse-cmd.c

index e2617af..48d678d 100644 (file)
 #ifndef __TTD_CMD_TYPE_H__
 #define __TTD_CMD_TYPE_H__
 
+typedef enum {
+       TTD_CMD_TYPE_UNKNOWN = -1,
+       TTD_CMD_TYPE_POWER,
+       TTD_CMD_TYPE_CONFIG,
+       TTD_CMD_TYPE_PACKAGE,
+       TTD_CMD_TYPE_DIAGNOSIS,
+       TTD_CMD_TYPE_INFO,
+       TTD_CMD_TYPE_LOCAL,
+       TTD_CMD_TYPE_MAX,
+} ttd_cmd_type_e;
+
+typedef enum { /* TBD */
+       TTD_CMD_POWER_OFF = 0,
+       TTD_CMD_POWER_RESTART,
+       TTD_CMD_POWER_MAX,
+} ttd_cmd_power_e;
+
+typedef enum { /* TBD */
+       TTD_CMD_CONFIG_SET,
+       TTD_CMD_CONFIG_UNSET,
+       TTD_CMD_CONFIG_MAX,
+} ttd_cmd_config_e;
 
 typedef enum {
-       /* remote cmd */
-       TTD_CMD_GET_SYSINFO = 0,
-       TTD_CMD_INSTALL_PKG,
+       TTD_CMD_PACKAGE_INSTALL,
+       TTD_CMD_PACKAGE_REMOVE,
+       TTD_CMD_PACKAGE_GET_APP_LIST,
+       TTD_CMD_PACKAGE_GET_PACKAGE_LIST,
+       TTD_CMD_PACKAGE_MAX,
+} ttd_cmd_package_e;
+
+typedef enum { /* TBD */
+       TTD_CMD_DIAGNOSIS_GET_LOG,
+       TTD_CMD_DIAGNOSIS_MAX,
+} ttd_cmd_diagnosis_e;
 
-       /* local cmd */
-       TTD_CMD_POST_SYSINFO = 1000,
-       TTD_CMD_POST_RESULT,
-       /* TODO */
-       TTD_CMD_MAX,
-} ttd_cmd_e;
+typedef enum { /* TBD */
+       TTD_CMD_INFO_GET_SYSINFO,
+       TTD_CMD_INFO_GET_TASKINFO,
+       TTD_CMD_INFO_GET_MAX,
+} ttd_cmd_info_e;
+
+typedef enum { /* TBD */
+       TTD_CMD_LOCAL_SOMETHING,
+       TTD_CMD_LOCAL_MAX,
+} ttd_cmd_local_e;
 
 typedef enum {
-       TTD_CMD_RESULT_SYSINFO,
-       TTD_CMD_RESULT_INSTALL_PKG,
-       /* TODO */
-} ttd_result_e;
+       TTD_CMD_RESULT_SUCCESS,
+       TTD_CMD_RESULT_FAIL,
+} ttd_cmd_result_e;
 
-typedef struct _ttd_cmd_data_s {
-       ttd_cmd_e cmd;
-       void *data;
-       unsigned int data_len;
-} ttd_cmd_data_s;
+
+typedef struct _ttd_cmd_data_s ttd_cmd_data;
 
 typedef struct _ttd_result_data_s {
-       ttd_result_e result;
+       char *cmd_id;
+       ttd_cmd_result_e result;
        void *data;
        unsigned int data_len;
 } ttd_result_data_s;
diff --git a/daemon/include/ttd-cmd.h b/daemon/include/ttd-cmd.h
new file mode 100644 (file)
index 0000000..6186745
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#ifndef __TTD_CMD_H__
+#define __TTD_CMD_H__
+
+#include "ttd-cmd-type.h"
+
+ttd_cmd_data *ttd_cmd_new(void);
+int ttd_cmd_set_type(ttd_cmd_data *cmd, ttd_cmd_type_e type);
+int ttd_cmd_set_id(ttd_cmd_data *cmd, const char *id);
+int ttd_cmd_set_command(ttd_cmd_data *cmd, int command);
+int ttd_cmd_set_data(ttd_cmd_data *cmd, void *data, unsigned int length);
+void ttd_cmd_free(ttd_cmd_data *cmd);
+
+ttd_cmd_type_e ttd_cmd_get_type(ttd_cmd_data *cmd);
+const char *ttd_cmd_get_id(ttd_cmd_data *cmd);
+int ttd_cmd_get_command(ttd_cmd_data *cmd);
+int ttd_cmd_get_data(ttd_cmd_data *cmd, void **data, unsigned int *length);
+
+#endif /* __TTD_CMD_H__ */
index 7466240..ecd62ce 100644 (file)
@@ -17,6 +17,8 @@
 #ifndef __TT_DAEMON_PARSE_CMD_H__
 #define __TT_DAEMON_PARSE_CMD_H__
 
-int ttd_parse_json_to_cmd(const char *json_str, ttd_cmd_data_s **cmd);
+#include <glib.h>
+
+int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list);
 
 #endif /* __TT_DAEMON_PARSE_CMD_H__ */
index 9acf58f..33b479d 100644 (file)
@@ -108,7 +108,8 @@ static void __main_thread_pool_func(gpointer thread_data, gpointer pool_data)
 static gboolean __daemon_job_handler(gpointer data)
 {
        ttd_data *d_data = data;
-       ttd_cmd_data_s *cmd_data = NULL;
+
+       ttd_cmd_data *cmd_data = NULL;
        ttd_result_data_s *res_data = NULL;
 
        if (!data) {
@@ -144,9 +145,17 @@ static int __say_hello_to_cloud(void *data)
        retvm_if(ret, -1, "failed to get cmd");
 
        if (cmd) {
-               ttd_cmd_data_s *cmd_data = NULL;
-               ttd_parse_json_to_cmd(cmd, &cmd_data);
-               ttd_queue_push(TTD_QUEUE_TYPE_CMD, cmd_data);
+               GList *cmd_list = NULL;
+               GList *l = NULL;
+
+               ttd_parse_json_to_cmd(cmd, &cmd_list);
+               for (l = cmd_list; l != NULL; l = l->next) {
+                       ttd_cmd_data *cmd_data = NULL;
+
+                       cmd_data = (ttd_cmd_data *)l->data;
+                       if (cmd_data)
+                               ttd_queue_push(TTD_QUEUE_TYPE_CMD, cmd_data);
+               }
        }else
                _D("there is no cmd now");
 
diff --git a/daemon/src/ttd-cmd.c b/daemon/src/ttd-cmd.c
new file mode 100644 (file)
index 0000000..c8f6e8c
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include "ttd-log.h"
+#include "ttd-cmd-type.h"
+
+struct _ttd_cmd_data_s {
+       ttd_cmd_type_e type;
+       union {
+               ttd_cmd_power_e pwr_cmd;
+               ttd_cmd_config_e cfg_cmd;
+               ttd_cmd_package_e pkg_cmd;
+               ttd_cmd_diagnosis_e diag_cmd;
+               ttd_cmd_info_e info_cmd;
+               ttd_cmd_local_e local_cmd;
+       } command;
+       char *cmd_id;
+       void *data;
+       unsigned int data_len;
+};
+
+ttd_cmd_data *ttd_cmd_new(void)
+{
+       ttd_cmd_data *data = NULL;
+
+       data = g_malloc0(sizeof(struct _ttd_cmd_data_s));
+
+       return data;
+}
+
+int ttd_cmd_set_type(ttd_cmd_data *cmd, ttd_cmd_type_e type)
+{
+       retv_if(!cmd, -1);
+
+       cmd->type = type;
+
+       return 0;
+}
+
+int ttd_cmd_set_id(ttd_cmd_data *cmd, const char *id)
+{
+       retv_if(!cmd, -1);
+
+       if (cmd->cmd_id)
+               g_free(cmd->cmd_id);
+
+       cmd->cmd_id = g_strdup(id);
+
+       return 0;
+}
+
+int ttd_cmd_set_command(ttd_cmd_data *cmd, int command)
+{
+       retv_if(!cmd, -1);
+
+       switch (cmd->type) {
+       case TTD_CMD_TYPE_POWER:
+               cmd->command.pwr_cmd = command;
+               break;
+       case TTD_CMD_TYPE_CONFIG:
+               cmd->command.cfg_cmd = command;
+               break;
+       case TTD_CMD_TYPE_PACKAGE:
+               cmd->command.pkg_cmd = command;
+               break;
+       case TTD_CMD_TYPE_DIAGNOSIS:
+               cmd->command.diag_cmd = command;
+               break;
+       case TTD_CMD_TYPE_INFO:
+               cmd->command.info_cmd = command;
+               break;
+       case TTD_CMD_TYPE_LOCAL:
+               cmd->command.local_cmd = command;
+               break;
+       case TTD_CMD_TYPE_MAX:
+       default:
+               _E("Unknown cmd type : %d", cmd->type);
+               return -1;
+               break;
+       }
+       return 0;
+}
+
+int ttd_cmd_set_data(ttd_cmd_data *cmd, void *data, unsigned int length)
+{
+       retv_if(!cmd, -1);
+
+       cmd->data = data;
+       cmd->data_len = length;
+
+       return 0;
+}
+
+void ttd_cmd_free(ttd_cmd_data *cmd)
+{
+       if (!cmd)
+               return;
+
+       if (cmd->cmd_id)
+               g_free(cmd->cmd_id);
+
+       g_free(cmd);
+}
+
+ttd_cmd_type_e ttd_cmd_get_type(ttd_cmd_data *cmd)
+{
+       retv_if(!cmd, TTD_CMD_TYPE_UNKNOWN);
+
+       return cmd->type;
+}
+
+const char *ttd_cmd_get_id(ttd_cmd_data *cmd)
+{
+       retv_if(!cmd, NULL);
+
+       return cmd->cmd_id;
+}
+
+int ttd_cmd_get_command(ttd_cmd_data *cmd)
+{
+       int command = -1;
+       retv_if(!cmd, -1);
+
+       switch (cmd->type) {
+       case TTD_CMD_TYPE_POWER:
+               command = cmd->command.pwr_cmd;
+               break;
+       case TTD_CMD_TYPE_CONFIG:
+               command = cmd->command.cfg_cmd;
+               break;
+       case TTD_CMD_TYPE_PACKAGE:
+               command = cmd->command.pkg_cmd;
+               break;
+       case TTD_CMD_TYPE_DIAGNOSIS:
+               command = cmd->command.diag_cmd;
+               break;
+       case TTD_CMD_TYPE_INFO:
+               command = cmd->command.info_cmd;
+               break;
+       case TTD_CMD_TYPE_LOCAL:
+               command = cmd->command.local_cmd;
+               break;
+       case TTD_CMD_TYPE_MAX:
+       default:
+               _E("Unknown cmd type : %d", cmd->type);
+               return -1;
+               break;
+       }
+       return command;
+}
+
+int ttd_cmd_get_data(ttd_cmd_data *cmd, void **data, unsigned int *length)
+{
+       retv_if(!cmd, -1);
+
+       *data = cmd->data;
+       *length = cmd->data_len;
+
+       return 0;
+}
index 7897261..24cdef5 100644 (file)
 
 #include <glib.h>
 #include <json.h>
+#include <string.h>
 #include "ttd-log.h"
 #include "ttd-cmd-type.h"
+#include "ttd-cmd.h"
 
-int ttd_parse_json_to_cmd(const char *json_str, ttd_cmd_data_s **cmd)
+#define TTD_CMD_KEY_TYPE "type"
+#define TTD_CMD_KEY_STATE "state"
+#define TTD_CMD_KEY_STATE_CREATED "created"
+#define TTD_CMD_KEY_CONTENT "content"
+#define TTD_CMD_KEY_CONTENT_ACTION "action"
+#define TTD_CMD_KEY_EXTRA "extra"
+
+static int __parse_cmd_get_action(json_object *obj)
 {
-       json_object *obj = NULL;
+       json_object *content_obj = NULL;
+       json_object *action_obj = NULL;
+       int action = -1;
 
-       retvm_if(!json_str, -1,"json_str is NULL");
+       content_obj = json_object_object_get(obj, TTD_CMD_KEY_CONTENT);
+       retvm_if(!content_obj, -1, "%s", "failed to get content");
+
+       action_obj =
+               json_object_object_get(content_obj, TTD_CMD_KEY_CONTENT_ACTION);
+       retvm_if(!action_obj, -1, "%s", "failed to get action");
+
+       action = json_object_get_int(action_obj);
+
+       return action;
+}
+
+static int __parse_cmd_power(json_object *obj, ttd_cmd_data *cmd)
+{
+       int action = 0;
+       int ret = 0;
+
+       action = __parse_cmd_get_action(obj);
+       retv_if(action < 0, -1);
+
+       switch (action) {
+       case TTD_CMD_POWER_OFF:
+       case TTD_CMD_POWER_RESTART:
+               ret = ttd_cmd_set_command(cmd, action);
+               break;
+       case TTD_CMD_POWER_MAX:
+       default:
+               _E("Unknown action : %d", action);
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+static int __parse_cmd_config(json_object *obj, ttd_cmd_data *cmd)
+{
+       int action = 0;
+       int ret = 0;
+       action = __parse_cmd_get_action(obj);
+       retv_if(action < 0, -1);
+
+       switch (action) {
+       case TTD_CMD_CONFIG_SET:
+       case TTD_CMD_CONFIG_UNSET:
+               ret = ttd_cmd_set_command(cmd, action);
+               break;
+       case TTD_CMD_CONFIG_MAX:
+       default:
+               _E("Unknown action : %d", action);
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+static int __parse_cmd_package(json_object *obj, ttd_cmd_data *cmd)
+{
+       int action = 0;
+       int ret = 0;
+
+       action = __parse_cmd_get_action(obj);
+       retv_if(action < 0, -1);
+
+       switch (action) {
+       case TTD_CMD_PACKAGE_REMOVE:
+       case TTD_CMD_PACKAGE_GET_APP_LIST:
+       case TTD_CMD_PACKAGE_GET_PACKAGE_LIST:
+               ret = ttd_cmd_set_command(cmd, action);
+               break;
+       case TTD_CMD_PACKAGE_INSTALL:
+       {
+               json_object *extra_obj = NULL;
+               gchar *extra_data = NULL;
+
+               ret = ttd_cmd_set_command(cmd, action);
+               extra_obj = json_object_object_get(obj, TTD_CMD_KEY_EXTRA);
+               retvm_if(!extra_obj, -1, "failed to get extra information");
+
+               extra_data = g_strdup(json_object_to_json_string(extra_obj));
+               if (extra_data)
+                       ttd_cmd_set_data(cmd, extra_data, strlen(extra_data));
+               else {
+                       _E("failed to get extra information");
+                       return -1;
+               }
+       }
+               break;
+       case TTD_CMD_PACKAGE_MAX:
+       default:
+               _E("Unknown action : %d", action);
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+static int __parse_cmd_diagnosis(json_object *obj, ttd_cmd_data *cmd)
+{
+       int action = 0;
+       int ret = 0;
+
+       action = __parse_cmd_get_action(obj);
+       retv_if(action < 0, -1);
+
+       switch (action) {
+       case TTD_CMD_DIAGNOSIS_GET_LOG:
+               ret = ttd_cmd_set_command(cmd, action);
+               break;
+       case TTD_CMD_DIAGNOSIS_MAX:
+       default:
+               _E("Unknown action : %d", action);
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+static int __parse_cmd_info(json_object *obj, ttd_cmd_data *cmd)
+{
+       int action = 0;
+       int ret = 0;
+
+       action = __parse_cmd_get_action(obj);
+       retv_if(action < 0, -1);
+
+       switch (action) {
+       case TTD_CMD_INFO_GET_SYSINFO:
+       case TTD_CMD_INFO_GET_TASKINFO:
+               ret = ttd_cmd_set_command(cmd, action);
+               break;
+       case TTD_CMD_INFO_GET_MAX:
+       default:
+               _E("Unknown action : %d", action);
+               return -1;
+               break;
+       }
+       return ret;
+}
+
+static int __parse_cmd_by_type(json_object *obj, ttd_cmd_data *cmd)
+{
+       int ret = 0;
+       retvm_if(!obj, -1,"obj is NULL");
        retvm_if(!cmd, -1,"cmd is NULL");
 
-       obj = json_tokener_parse(json_str);
+       switch (ttd_cmd_get_type(cmd)) {
+       case TTD_CMD_TYPE_POWER:
+               ret = __parse_cmd_power(obj, cmd);
+               break;
+       case TTD_CMD_TYPE_CONFIG:
+               ret = __parse_cmd_config(obj, cmd);
+               break;
+       case TTD_CMD_TYPE_PACKAGE:
+               ret = __parse_cmd_package(obj, cmd);
+               break;
+       case TTD_CMD_TYPE_DIAGNOSIS:
+               ret = __parse_cmd_diagnosis(obj, cmd);
+               break;
+       case TTD_CMD_TYPE_INFO:
+               ret = __parse_cmd_info(obj, cmd);
+               break;
+       case TTD_CMD_TYPE_LOCAL:
+       case TTD_CMD_TYPE_UNKNOWN:
+       case TTD_CMD_TYPE_MAX:
+       default:
+               _E("Unknown cmd type : %d", ttd_cmd_get_type(cmd));
+               ret = -1;
+               break;
+       }
+       return ret;
+}
+
+int ttd_parse_json_to_cmd(const char *json_str, GList **cmd_list)
+{
+       json_object *root_obj = NULL;
+       ttd_cmd_data *cmd_data = NULL;
+       enum json_type root_type = json_type_null;
+       int cmd_length = 0;
+       int i;
+       GList *list = NULL;
+
+       retvm_if(!json_str, -1,"json_str is NULL");
+       retvm_if(!cmd_list, -1,"cmd is NULL");
+
+       root_obj = json_tokener_parse(json_str);
+       root_type = json_object_get_type(root_obj);
+       if (root_type != json_type_array) {
+               _E("cmd is not array type");
+               json_object_put(root_obj);
+               return -1;
+       }
+
+       cmd_length = json_object_array_length(root_obj);
+       if (cmd_length <= 0) {
+               _E("Nothing in the cmd array");
+               json_object_put(root_obj);
+               return -1;
+       }
+
+       for (i = 0; i < cmd_length; i++) {
+               json_object *obj = NULL;
+               json_object *temp_obj = NULL;
+               int cmd_type = 0;
+               const char *cmd_state = NULL;
+               int ret = 0;
+
+               obj = json_object_array_get_idx(root_obj, i);
+               if (!obj) {
+                       _E("failed to get object in cmd array");
+                       continue;
+               }
 
-       /* TODO : parse and fill out cmd */
+               temp_obj = json_object_object_get(obj, TTD_CMD_KEY_STATE);
+               cmd_state = json_object_get_string(temp_obj);
+               if ( 0 != g_strcmp0(TTD_CMD_KEY_STATE_CREATED, cmd_state)) {
+                       _D("passing cmd state - %s", cmd_state);
+                       continue; /* handling only 'created' state */
+               }
 
-       json_object_put(obj);
+               temp_obj = json_object_object_get(obj, TTD_CMD_KEY_TYPE);
+               cmd_type = json_object_get_int(temp_obj);
+               cmd_data = ttd_cmd_new();
+               ttd_cmd_set_type(cmd_data, cmd_type);
+               ret = __parse_cmd_by_type(obj, cmd_data);
+               if (ret) {
+                       _E("failed to parse cmd - %s", json_object_get_string(obj));
+                       ttd_cmd_free(cmd_data);
+                       continue;
+               }
+               list = g_list_append(list, cmd_data);
+       }
+       json_object_put(root_obj);
+       *cmd_list = list;
 
        return 0;
 }