Mesh: Fix & Refactor Mesh Model Subscription codes
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-config-client.c
index 211e46d..aa3b38e 100644 (file)
@@ -266,6 +266,25 @@ static void __bt_mesh_wait_response_timeout(
                                BT_MESH_NODE_TTL_EXECUTE, req->data,
                                        sizeof(bluetooth_mesh_node_ttl_info_t));
                break;
+       case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
+       case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE:
+       case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
+       case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
+               /* Send event with timeout */
+                __bt_mesh_handle_pending_dev_config_request_info(
+                       BLUETOOTH_ERROR_TIMEOUT,
+                               BT_MESH_MODEL_CONFIG_GROUP_SUB, req->data,
+                                       sizeof(bluetooth_mesh_model_configure_t));
+               break;
+       case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
+       case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
+       case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
+               /* Send event with timeout */
+                __bt_mesh_handle_pending_dev_config_request_info(
+                       BLUETOOTH_ERROR_TIMEOUT,
+                               BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, req->data,
+                                       sizeof(bluetooth_mesh_model_configure_t));
+               break;
        default:
                break;
        }
@@ -871,11 +890,17 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        event = (bluetooth_mesh_model_configure_t*) param;
                        req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
+                       BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
+                               event->net_uuid, req->net_uuid);
+
+                       BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
+                               event->primary_unicast, req->primary_unicast);
 
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
 
                                req->sub_addr = event->sub_addr;
+                               BT_INFO("Mesh: Send event for Model Subscription");
                                /* Send Event */
                                __bt_mesh_send_model_subscription_configure_event(
                                        BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
@@ -897,7 +922,11 @@ static void __bt_mesh_handle_pending_dev_config_request_info(int result,
 
                        event = (bluetooth_mesh_model_configure_t*) param;
                        req = (bluetooth_mesh_model_configure_t*)req_info->user_data;
+                       BT_INFO("Mesh: Event Network [%s] Req Network [%s]",
+                               event->net_uuid, req->net_uuid);
 
+                       BT_INFO("Mesh: Event Unicast [0x%4.4x] Req Unicast [0x%4.4x]",
+                               event->primary_unicast, req->primary_unicast);
                        if (!g_strcmp0(req->net_uuid, event->net_uuid) &&
                                req->primary_unicast == event->primary_unicast) {
                                /* Send Event */
@@ -1001,6 +1030,7 @@ static void __mesh_handle_model_subscription_event(int result,
                bluetooth_mesh_model_configure_t *param,
                        const struct mesh_config_cmd *cmd)
 {
+       BT_INFO("Mesh: Model Subscription Event: Request [%d]", cmd->opcode);
        switch (cmd->opcode) {
        /* Fall through */
        case MESH_OPCODE_CONFIG_MODEL_SUB_ADD:
@@ -1008,15 +1038,17 @@ static void __mesh_handle_model_subscription_event(int result,
        case MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE:
        case MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL:
                /* Model Bind/UnBind Event */
+       BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_GROUP_SUB");
        __bt_mesh_handle_pending_dev_config_request_info(result,
-                       BT_MESH_MODEL_CONFIG_GROUP_SUB, &param,
+                       BT_MESH_MODEL_CONFIG_GROUP_SUB, param,
                                sizeof(bluetooth_mesh_model_configure_t));
                break;
        case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD:
        case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE:
        case MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
+       BT_INFO("Mesh: Handle Event for Request: BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB");
        __bt_mesh_handle_pending_dev_config_request_info(result,
-                       BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, &param,
+                       BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB, param,
                                sizeof(bluetooth_mesh_model_configure_t));
                break;
        default:
@@ -1133,9 +1165,18 @@ void _bt_mesh_config_client_devkey_msg_handler(
                BT_INFO("\nNode %4.4x Subscription status %s\n",
                                event->source, __mesh_status_to_string(data[0]));
 
+               if (data[0] != MESH_STATUS_SUCCESS)
+                       result = BLUETOOTH_ERROR_INTERNAL;
+
+               if (!cmd) {
+                       BT_INFO("Mesh: Command not found!!");
+                       break;
+               }
+
                ele_addr = l_get_le16(data + 1);
                addr = l_get_le16(data + 3);
                BT_INFO("Element Addr\t%4.4x\n", ele_addr);
+               BT_INFO("Message Source Addr\t%4.4x\n", event->source);
 
                if (data_len == 9) {
                        /* vendor Model */
@@ -1153,7 +1194,11 @@ void _bt_mesh_config_client_devkey_msg_handler(
                param.sub_addr = addr;
                param.model = mod_id;
                BT_INFO("Subscr Addr\t%4.4x\n", addr);
-               __mesh_handle_model_subscription_event(result, &param, cmd);
+               BT_INFO("Model ID\t%4.4x\n", mod_id);
+
+               if (cmd)
+                       __mesh_handle_model_subscription_event(result, &param, cmd);
+
                break;
        }
        case MESH_OPCODE_CONFIG_DEFAULT_TTL_STATUS: {
@@ -1679,25 +1724,28 @@ int _bt_mesh_model_configure_group_subscription(const char *app_cred,
                return BLUETOOTH_ERROR_DEVICE_BUSY;
        }
 
+       BT_INFO("Mesh: group Subscription Req: [%d]", req->op);
        if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
+               BT_INFO("Mesh: group Subscription Addr [0x%2.2x]", req->sub_addr);
+
                /* Subscriptio address sanity check */
-               if ((!MESH_IS_GROUP(req->sub_addr) ||
-                       MESH_IS_ALL_NODES(req->sub_addr)) ||
+               if (!MESH_IS_GROUP(req->sub_addr) ||
                                MESH_IS_VIRTUAL(req->sub_addr)) {
-                       BT_ERR("Mesh: Bad subscription address %x\n",
+                       BT_ERR("Mesh: Bad subscription address [0x%2.2x\n]",
                                req->sub_addr);
                        return BLUETOOTH_ERROR_INVALID_PARAM;
                }
-       }
+       } else
+               BT_INFO("Mesh: group Subscription Req: Delete All");
 
+       _bt_mesh_util_convert_string_to_hex(req->net_uuid,
+               strlen(req->net_uuid), net_uuid.uuid, 16);
        /* Get Subnet index of the remote node for TX encryption */
        netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
                                req->primary_unicast);
        if (netkey_idx == MESH_NET_IDX_INVALID)
                return BLUETOOTH_ERROR_INTERNAL;
 
-       _bt_mesh_util_convert_string_to_hex(req->net_uuid,
-               strlen(req->net_uuid), net_uuid.uuid, 16);
 
        if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
                opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
@@ -1717,6 +1765,8 @@ int _bt_mesh_model_configure_group_subscription(const char *app_cred,
 
        data_len = _bt_mesh_util_opcode_set(opcode, buffer);
 
+       BT_INFO("Mesh: Group Subscription Primary unicast [0x%2.2x]",
+                       req->primary_unicast);
        /* Element Address */
        l_put_le16((req->primary_unicast + req->elem_index),
                        buffer + data_len);
@@ -1728,6 +1778,7 @@ int _bt_mesh_model_configure_group_subscription(const char *app_cred,
                data_len += 2;
        }
 
+       BT_INFO("Mesh: Group Subscription Model ID [0x%4.4x]", req->model);
        /* Insert Model ID */
         if (req->model >= 0xFFFF0000) {
                /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
@@ -1749,6 +1800,7 @@ int _bt_mesh_model_configure_group_subscription(const char *app_cred,
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+       BT_INFO("Mesh: Group Subscription Command sent successfully");
        /* Queue the request with timeout */
        __bt_mesh_add_request(opcode, req->primary_unicast,
                net_uuid.uuid,
@@ -1778,16 +1830,19 @@ int _bt_mesh_model_configure_virtual_group_subscription(
        _bt_mesh_util_convert_string_to_hex(req->net_uuid,
                strlen(req->net_uuid), net_uuid.uuid, 16);
 
+       BT_INFO("Mesh: Virtual Group Subscription Req: [%d] Group Addr [0x%2.2x]",
+                       req->op, req->sub_addr);
+
        if (req->op != BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL) {
                /* Subscription address sanity check */
-               if ((MESH_IS_GROUP(req->sub_addr) ||
-                       MESH_IS_ALL_NODES(req->sub_addr)) ||
+               if (MESH_IS_GROUP(req->sub_addr) ||
                                !(MESH_IS_VIRTUAL(req->sub_addr))) {
-                       BT_ERR("Mesh: Bad Virtual subscription address %x\n",
+                       BT_ERR("Mesh: Bad Virtual Group subscription address [0x%2.2x\n]",
                                req->sub_addr);
                        return BLUETOOTH_ERROR_INVALID_PARAM;
                }
-       }
+       } else
+               BT_INFO("Mesh: Virtual Group Subscription Req: Delete All");
 
        /* Get Subnet index of the remote node for TX encryption */
        netkey_idx = _bt_mesh_node_get_subnet_idx(net_uuid.uuid,
@@ -1796,13 +1851,11 @@ int _bt_mesh_model_configure_virtual_group_subscription(
                return BLUETOOTH_ERROR_INTERNAL;
 
        if (req->op == BLUETOOTH_MESH_MODEL_SUB_ADD)
-               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_ADD;
+               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_ADD;
        else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE)
-               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE;
-       else if (req->op == BLUETOOTH_MESH_MODEL_SUB_DELETE_ALL)
-               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_DELETE_ALL;
+               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_DELETE;
        else if (req->op == BLUETOOTH_MESH_MODEL_SUB_OVERWRITE)
-               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_OVERWRITE;
+               opcode = MESH_OPCODE_CONFIG_MODEL_SUB_VIRT_OVERWRITE;
 
        /* Check pending request */
        if (_bt_mesh_check_pending_request(opcode,
@@ -1829,6 +1882,7 @@ int _bt_mesh_model_configure_virtual_group_subscription(
                data_len += 16;
        }
 
+       BT_INFO("Mesh: Virtual Group Subscription Model ID [0x%4.4x]", req->model);
        /* Insert Model ID */
         if (req->model >= 0xFFFF0000) {
                /* 1st 2 octet Company ID is 0xFFFF means, it is BT SIG Model*/
@@ -1851,6 +1905,7 @@ int _bt_mesh_model_configure_virtual_group_subscription(
                return BLUETOOTH_ERROR_INTERNAL;
        }
 
+       BT_INFO("Mesh: Virtual Group Subscription Command sent successfully");
        /* Queue the request with timeout */
        __bt_mesh_add_request(opcode, req->primary_unicast,
                net_uuid.uuid,