From c12e0b08d4e3741ca783366de4c3c53b9e3a6eac Mon Sep 17 00:00:00 2001 From: Saurav Babu Date: Tue, 30 Jan 2018 09:25:32 +0530 Subject: [PATCH] comp-manager: Use enum instead of string for commands Signed-off-by: Saurav Babu --- src/companion-manager/include/comp_enum.h | 14 ++++++ src/companion-manager/include/comp_group.h | 11 ----- src/companion-manager/include/comp_iot.h | 2 +- src/companion-manager/src/comp_group.c | 10 ++-- src/companion-manager/src/comp_iot.c | 74 +++++++++++++++++++++--------- 5 files changed, 73 insertions(+), 38 deletions(-) diff --git a/src/companion-manager/include/comp_enum.h b/src/companion-manager/include/comp_enum.h index 7ae88a9..7ce1963 100755 --- a/src/companion-manager/include/comp_enum.h +++ b/src/companion-manager/include/comp_enum.h @@ -39,4 +39,18 @@ typedef enum { COMP_GROUP_REMOTE_DEVICE, } comp_group_type_e; +/** + * @brief The command type for request. + * + * @since_tizen 5.0 + */ +typedef enum { + COMP_REQ_UNKNOWN_COMMAND = -1, /**< Unknown Command */ + COMP_REQ_SEND_DATA = 0, /**< Send data */ + COMP_REQ_CREATE_GROUP, /**< Create Group */ + COMP_REQ_INVITE_DEVICE, /**< Invite Device */ + COMP_REQ_EJECT_DEVICE, /**< Exile Device */ + COMP_REQ_DELETE_GROUP, /**< Delete Group */ +} comp_request_type_e; + #endif diff --git a/src/companion-manager/include/comp_group.h b/src/companion-manager/include/comp_group.h index f4023a1..4b0b15e 100644 --- a/src/companion-manager/include/comp_group.h +++ b/src/companion-manager/include/comp_group.h @@ -42,17 +42,6 @@ typedef struct { int permission_2; } comp_group_invite_info_t; -/** - * @brief The command type for request. - * - * @since_tizen 5.0 - */ -#define COMP_REQ_SEND_DATA "0" /**< Send data */ -#define COMP_REQ_CREATE_GROUP "1" /**< Create Group */ -#define COMP_REQ_INVITE_DEVICE "2" /**< Invite Device */ -#define COMP_REQ_EJECT_DEVICE "3" /**< Exile Device */ -#define COMP_REQ_DELETE_GROUP "4" /**< Delete Group */ - /* Called when daemon is start. */ int comp_group_initialize(); diff --git a/src/companion-manager/include/comp_iot.h b/src/companion-manager/include/comp_iot.h index e3238d6..17f8efd 100644 --- a/src/companion-manager/include/comp_iot.h +++ b/src/companion-manager/include/comp_iot.h @@ -14,7 +14,7 @@ typedef struct _iot_discovery_t { typedef struct _comp_command_t { int tid; int resource_type; - gchar *command; + comp_request_type_e command; gchar *uuid; gchar *host; gchar *arg1; diff --git a/src/companion-manager/src/comp_group.c b/src/companion-manager/src/comp_group.c index bca713d..07890a8 100644 --- a/src/companion-manager/src/comp_group.c +++ b/src/companion-manager/src/comp_group.c @@ -547,7 +547,7 @@ int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data, percent[0] = '\0'; comp_command_t *cmd = g_new0(comp_command_t, 1); - cmd->command = g_strdup(COMP_REQ_SEND_DATA); + cmd->command = COMP_REQ_SEND_DATA; cmd->uuid = g_strdup(uuid_dev); if (strchr(ip, ':')) /* IPv6 Adress */ @@ -791,7 +791,7 @@ int comp_group_request_create_group(char *uuid, char *group_name) LOG_ERR("[Request Create Group] to %s", uuid); comp_command_t *cmd = g_new0(comp_command_t, 1); - cmd->command = g_strdup(COMP_REQ_CREATE_GROUP); + cmd->command = COMP_REQ_CREATE_GROUP; cmd->uuid = g_strdup(uuid); cmd->arg1 = g_strdup(group_name); @@ -812,7 +812,7 @@ int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, c LOG_ERR("[Request Invite] to %s", uuid); comp_command_t *cmd = g_new0(comp_command_t, 1); - cmd->command = g_strdup(COMP_REQ_INVITE_DEVICE); + cmd->command = COMP_REQ_INVITE_DEVICE; cmd->uuid = g_strdup(uuid); cmd->arg1 = g_strdup(group_name); cmd->arg2 = g_strdup(target_uuid); @@ -836,7 +836,7 @@ int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid) LOG_ERR("[Request Eject] to %s", uuid); comp_command_t *cmd = g_new0(comp_command_t, 1); - cmd->command = g_strdup(COMP_REQ_EJECT_DEVICE); + cmd->command = COMP_REQ_EJECT_DEVICE; cmd->uuid = g_strdup(uuid); cmd->arg1 = g_strdup(group_name); cmd->arg2 = g_strdup(target_uuid); @@ -860,7 +860,7 @@ int comp_group_request_delete_group(char *uuid, char *group_name) LOG_ERR("[Request Delete Group] to %s", uuid); comp_command_t *cmd = g_new0(comp_command_t, 1); - cmd->command = g_strdup(COMP_REQ_DELETE_GROUP); + cmd->command = COMP_REQ_DELETE_GROUP; cmd->uuid = g_strdup(uuid); cmd->arg1 = g_strdup(group_name); diff --git a/src/companion-manager/src/comp_iot.c b/src/companion-manager/src/comp_iot.c index d3f4b8d..9b107f5 100644 --- a/src/companion-manager/src/comp_iot.c +++ b/src/companion-manager/src/comp_iot.c @@ -16,6 +16,37 @@ int last_get_result; +#define CASE_TO_STR(x) case x: return #x; + +static const char *command2string(comp_request_type_e command) +{ + switch (command) { + CASE_TO_STR(COMP_REQ_SEND_DATA) + CASE_TO_STR(COMP_REQ_CREATE_GROUP) + CASE_TO_STR(COMP_REQ_INVITE_DEVICE) + CASE_TO_STR(COMP_REQ_EJECT_DEVICE) + CASE_TO_STR(COMP_REQ_DELETE_GROUP) + default: + return "Unknown Command"; + } +} + +static comp_request_type_e string2command(char *command) +{ + if (g_strcmp0(command, "COMP_REQ_SEND_DATA") == 0) + return COMP_REQ_SEND_DATA; + else if (g_strcmp0(command, "COMP_REQ_CREATE_GROUP") == 0) + return COMP_REQ_CREATE_GROUP; + else if (g_strcmp0(command, "COMP_REQ_INVITE_DEVICE") == 0) + return COMP_REQ_INVITE_DEVICE; + else if (g_strcmp0(command, "COMP_REQ_EJECT_DEVICE") == 0) + return COMP_REQ_EJECT_DEVICE; + else if (g_strcmp0(command, "COMP_REQ_DELETE_GROUP") == 0) + return COMP_REQ_DELETE_GROUP; + else + return COMP_REQ_UNKNOWN_COMMAND; +} + int comp_iot_initialize() { char *device_id = NULL; @@ -100,13 +131,16 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques return; if (IOTCON_ERROR_NONE == ret && query) { - char *command; + char *cmd; char *arg = NULL; int result; + comp_request_type_e command; + + iotcon_query_lookup(query, "CMD", &cmd); - iotcon_query_lookup(query, "CMD", &command); + command = string2command(cmd); - if (strcmp(command, COMP_REQ_CREATE_GROUP) == 0) { + if (command == COMP_REQ_CREATE_GROUP) { LOG_DEBUG("Request create group"); char *group_name; iotcon_query_lookup(query, "name", &group_name); @@ -114,7 +148,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques result = comp_group_create(group_name); arg = g_strdup(group_name); - } else if (strcmp(command, COMP_REQ_INVITE_DEVICE) == 0) { + } else if (command == COMP_REQ_INVITE_DEVICE) { LOG_DEBUG("Request invite"); char *group_name; char *uuid; @@ -127,7 +161,7 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques result = comp_group_invite(group_name, uuid, "12341234"); arg = g_strdup(uuid); - } else if (strcmp(command, COMP_REQ_EJECT_DEVICE) == 0) { + } else if (command == COMP_REQ_EJECT_DEVICE) { LOG_DEBUG("Request eject"); char *group_name; char *uuid; @@ -140,10 +174,10 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques result = comp_group_dismiss(group_name, uuid); arg = g_strdup(uuid); - } else if (strcmp(command, COMP_REQ_DELETE_GROUP) == 0) { + } else if (command == COMP_REQ_DELETE_GROUP) { LOG_DEBUG("Request delete group"); arg = g_strdup("DELETED"); - } else { + } else if (command == COMP_REQ_SEND_DATA) { #ifdef SUPPORT_BASE64_ENCODING int payload_len; char *data = NULL; @@ -184,9 +218,11 @@ static void _request_handler(iotcon_resource_h resource, iotcon_request_h reques LOG_DEBUG("Receive Data = %s", data); arg = g_strdup(data); #endif + } else { + LOG_ERR("Unknown request command"); } - notify_request_result(command, arg, result); + notify_request_result(cmd, arg, result); free(arg); } @@ -298,10 +334,6 @@ static void _clear_user_data(void *user_data) g_source_remove(cmd->tid); } - if (cmd->command) { - g_free(cmd->command); - cmd->command = NULL; - } if (cmd->uuid) { g_free(cmd->uuid); cmd->uuid = NULL; @@ -455,19 +487,19 @@ static bool _found_resource(iotcon_remote_resource_h resource, return IOTCON_FUNC_CONTINUE; } - iotcon_query_add(query, "CMD", cmd->command); - LOG_DEBUG("CMD = %s", cmd->command); + iotcon_query_add(query, "CMD", command2string(cmd->command)); + LOG_DEBUG("CMD = %s", command2string(cmd->command)); - if (strcmp(cmd->command, COMP_REQ_CREATE_GROUP) == 0) { //request create group + if (cmd->command == COMP_REQ_CREATE_GROUP) { //request create group iotcon_query_add(query, "name", cmd->arg1); - } else if (strcmp(cmd->command, COMP_REQ_INVITE_DEVICE) == 0) { //request invite + } else if (cmd->command == COMP_REQ_INVITE_DEVICE) { //request invite iotcon_query_add(query, "name", cmd->arg1); iotcon_query_add(query, "id", cmd->arg2); iotcon_query_add(query, "PIN", cmd->arg3); - } else if (strcmp(cmd->command, COMP_REQ_EJECT_DEVICE) == 0) { //request eject + } else if (cmd->command == COMP_REQ_EJECT_DEVICE) { //request eject iotcon_query_add(query, "name", cmd->arg1); iotcon_query_add(query, "id", cmd->arg2); - } else if (strcmp(cmd->command, COMP_REQ_DELETE_GROUP) == 0) { //request delete group + } else if (cmd->command == COMP_REQ_DELETE_GROUP) { //request delete group iotcon_query_add(query, "name", cmd->arg1); } else { /* Send Data */ #ifdef SUPPORT_BASE64_ENCODING @@ -476,7 +508,7 @@ static bool _found_resource(iotcon_remote_resource_h resource, char* b64Buf = g_malloc0(b64BufSize); if (NULL == b64Buf) { iotcon_remote_resource_destroy(resource_clone); - _clear_user_data(cmd); + _clear_user_data(cmd); return IOTCON_FUNC_CONTINUE; } b64Encode((const char *)cmd->arg1, cmd->arg1_len, b64Buf, b64BufSize, &outSize); @@ -561,8 +593,8 @@ int comp_iot_discovery_resource(comp_resource_type_e resource_type, int timeout, LOG_ERR("iotcon_set_timeout: Failed %s", get_error_message(ret)); } - if (cmd && (g_strcmp0(cmd->command, COMP_REQ_CREATE_GROUP) == 0 || - g_strcmp0(cmd->command, COMP_REQ_SEND_DATA) == 0)) + if (cmd && (cmd->command == COMP_REQ_CREATE_GROUP || + cmd->command == COMP_REQ_SEND_DATA)) ret = iotcon_find_resource(cmd->host, IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP, query, _found_resource, user_data); -- 2.7.4