#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;
--- /dev/null
+/*
+ * 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__ */
--- /dev/null
+/*
+ * 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;
+}
#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;
}