return ret;
}
-int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
- int len)
+static char *_addr2host(char *addr, int port)
{
- int ret;
char ip[50];
char *percent;
- LOG_BEGIN();
-
g_strlcpy(ip, addr, strlen(addr) + 1);
/* Remove % from address */
if (percent)
percent[0] = '\0';
- comp_command_t *cmd = g_new0(comp_command_t, 1);
- cmd->command = COMP_REQ_SEND_DATA;
- cmd->uuid = g_strdup(uuid_dev);
-
/* IPv6 Address should be encoded for RFC6874 */
if (strchr(ip, ':')) /* IPv6 Adress */
- cmd->host = g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
+ return g_strdup_printf("coaps://[%s%s%s]:%d", ip, "%25",
percent + 1, port);
else /* IPv4 Address */
- cmd->host = g_strdup_printf("coaps://%s:%d", ip, port);
+ return g_strdup_printf("coaps://%s:%d", ip, port);
+}
+
+int comp_group_send_data(gchar *uuid_dev, gchar *addr, int port, gchar *data,
+ int len)
+{
+ int ret;
+
+ LOG_BEGIN();
+
+ comp_command_t *cmd = g_new0(comp_command_t, 1);
+ cmd->command = COMP_REQ_SEND_DATA;
+ cmd->uuid = g_strdup(uuid_dev);
+
+ cmd->host = _addr2host(addr, port);
cmd->arg1 = g_malloc0(len + 1);
if (NULL == cmd->arg1) {
return ret;
}
+static comp_mot_device_t *_check_device_in_mot_enb_dev_list(char *uuid)
+{
+ comp_mot_device_t *device;
+ GList *iter;
+
+ iter = mot_enb_dev_list;
+
+ while (iter != NULL) {
+ device = (comp_mot_device_t *)iter->data;
+
+ if (g_strcmp0(device->device_id, uuid) == 0) {
+ LOG_DEBUG("Device %s is available in mot enable device list", uuid);
+ return device;
+ }
+ }
+
+ return NULL;
+}
+
int comp_group_request_create_group(char *uuid, char *group_name)
{
int ret = COMP_ERROR_NONE;
+ comp_mot_device_t *device;
LOG_ERR("[Request Create Group] %s", group_name);
LOG_ERR("[Request Create Group] to %s", uuid);
cmd->uuid = g_strdup(uuid);
cmd->arg1 = g_strdup(group_name);
- ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
- if (ret != COMP_ERROR_NONE) {
- LOG_ERR("Failed to discover resource : %s",
- comp_log_get_error_string(ret));
+ device = _check_device_in_mot_enb_dev_list(uuid);
+ if (device) {
+ cmd->host = _addr2host(device->addr, device->secure_port);
+ ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to send data : %s",
+ comp_log_get_error_string(ret));
+ }
+ } else {
+ ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to discover resource : %s",
+ comp_log_get_error_string(ret));
+ }
}
return ret;
int comp_group_request_invite(char *uuid, char *group_name, char *target_uuid, char *PIN)
{
int ret = COMP_ERROR_NONE;
+ comp_mot_device_t *device;
LOG_ERR("[Request Invite] %s, %s, %s", group_name, target_uuid, PIN);
LOG_ERR("[Request Invite] to %s", uuid);
cmd->arg2 = g_strdup(target_uuid);
cmd->arg3 = g_strdup(PIN);
- ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
- if (ret != COMP_ERROR_NONE) {
- LOG_ERR("Failed to discover resource : %s",
- comp_log_get_error_string(ret));
+ device = _check_device_in_mot_enb_dev_list(uuid);
+ if (device) {
+ cmd->host = _addr2host(device->addr, device->secure_port);
+ ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to send data : %s",
+ comp_log_get_error_string(ret));
+ }
+ } else {
+ ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to discover resource : %s",
+ comp_log_get_error_string(ret));
+ }
}
return ret;
int comp_group_request_eject(char *uuid, char *group_name, char *target_uuid)
{
int ret = COMP_ERROR_NONE;
+ comp_mot_device_t *device;
LOG_ERR("[Request Eject] %s, %s", group_name, target_uuid);
LOG_ERR("[Request Eject] to %s", uuid);
cmd->arg1 = g_strdup(group_name);
cmd->arg2 = g_strdup(target_uuid);
- ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
- if (ret != COMP_ERROR_NONE) {
- LOG_ERR("Failed to discover resource : %s",
- comp_log_get_error_string(ret));
+ device = _check_device_in_mot_enb_dev_list(uuid);
+ if (device) {
+ cmd->host = _addr2host(device->addr, device->secure_port);
+ ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to send data : %s",
+ comp_log_get_error_string(ret));
+ }
+ } else {
+ ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to discover resource : %s",
+ comp_log_get_error_string(ret));
+ }
}
return ret;
int comp_group_request_delete_group(char *uuid, char *group_name)
{
int ret = COMP_ERROR_NONE;
+ comp_mot_device_t *device;
LOG_ERR("[Request Delete Group] %s", group_name);
LOG_ERR("[Request Delete Group] to %s", uuid);
cmd->uuid = g_strdup(uuid);
cmd->arg1 = g_strdup(group_name);
- ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
- if (ret != COMP_ERROR_NONE) {
- LOG_ERR("Failed to discover resource : %s",
- comp_log_get_error_string(ret));
+ device = _check_device_in_mot_enb_dev_list(uuid);
+ if (device) {
+ cmd->host = _addr2host(device->addr, device->secure_port);
+ ret = comp_iot_send_data(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to send data : %s",
+ comp_log_get_error_string(ret));
+ }
+ } else {
+ ret = comp_iot_discovery_resource(COMP_RESOURCE_TYPE_DATA, 5, cmd);
+ if (ret != COMP_ERROR_NONE) {
+ LOG_ERR("Failed to discover resource : %s",
+ comp_log_get_error_string(ret));
+ }
}
return ret;
void *user_data)
{
int ret;
+ comp_command_t *cmd = (comp_command_t *)user_data;
+
try {
if (eCode == OC_STACK_OK || eCode == OC_STACK_RESOURCE_CREATED ||
eCode == OC_STACK_RESOURCE_CHANGED) {
}
notify_send_data_finish("RESP_DATA", last_get_result);
+
_clear_user_data(user_data);
}
}
OCRepresentation rep;
- rep.setValue("CMD", COMP_REQ_SEND_DATA);
- rep.setValue("data", std::string(cmd->arg1));
+ rep.setValue("CMD", cmd->command);
+ switch (cmd->command) {
+ case COMP_REQ_SEND_DATA:
+ rep.setValue("data", std::string(cmd->arg1));
+ break;
+ case COMP_REQ_CREATE_GROUP:
+ case COMP_REQ_DELETE_GROUP:
+ rep.setValue("name", std::string(cmd->arg1));
+ break;
+ case COMP_REQ_INVITE_DEVICE:
+ rep.setValue("name", std::string(cmd->arg1));
+ rep.setValue("id", std::string(cmd->arg2));
+ rep.setValue("PIN", std::string(cmd->arg3));
+ break;
+ case COMP_REQ_EJECT_DEVICE:
+ rep.setValue("name", std::string(cmd->arg1));
+ rep.setValue("id", std::string(cmd->arg2));
+ break;
+ default:
+ LOG_ERR("Invalid command %d", cmd->command);
+ return -1;
+ }
ret = resource->post(rep, QueryParamsMap(),
std::bind (&_on_post, std::placeholders::_1,