Mesh: added message response on Node Role 27/257127/3 accepted/tizen/unified/20210427.085759 submit/tizen/20210426.061315
authoranuj.bhumiya <anuj.bhumiya@samsung.com>
Mon, 19 Apr 2021 10:09:45 +0000 (15:39 +0530)
committeranuj.bhumiya <anuj.bhumiya@samsung.com>
Fri, 23 Apr 2021 05:18:44 +0000 (10:48 +0530)
This patch implements the message response from node
side to Provisioner node when it receive any message
from provisioner.

Change-Id: I3b8f1d092985bd489a5732a685eecedc74212e6a
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
include/bluetooth_internal.h
include/bluetooth_private.h
src/bluetooth-common.c
src/bluetooth-mesh.c
tests/test/bt_mesh_unit_test.c

index 69681e1..c23f64e 100644 (file)
@@ -5221,6 +5221,22 @@ int bt_mesh_model_send_msg(bt_mesh_model_h model,
 /**
  * @internal
  * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
+ * @brief Send response of the message send by provisioner node.
+ *
+ * @param: in: model, model handle to which message to be sent
+ * @param: in: msg_params, opcode and the message to be sent
+ *
+ * @see bt_mesh_model_msg_params_s
+ *
+ * @since_tizen 6.5
+ * @privlevel platform
+ */
+int bt_mesh_node_model_send_response(bt_mesh_model_h model,
+       bt_mesh_model_msg_params_s *msg_params, void *user_data);
+
+/**
+ * @internal
+ * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
  * @brief Send Group message originated by a local mesh model.
  *
  * @param: in: group, group handle to which message to be sent
index 00ee146..af4c118 100644 (file)
@@ -183,6 +183,7 @@ typedef enum {
        BT_EVENT_MESH_NODE_TTL_EXECUTE_COMPLETED, /**< Mesh -  Node TTL Get/Set event */
        BT_EVENT_MESH_NODE_MODEL_BIND_APPKEY_COMPLETED, /**< Mesh -  Node Model Bind AppKey */
        BT_EVENT_MESH_NODE_MODEL_EXECUTE_MSG_COMPLETED, /**< Mesh -  Node Model Send/Receive msg */
+       BT_EVENT_MESH_NODE_MODEL_RECEIVED_MSG, /**< Mesh -  Node Model Receive msg */
        BT_EVENT_MESH_NODE_MODEL_APPKEY_LIST, /**< Mesh -  Model AppKey List  */
        BT_EVENT_MESH_NODE_MODEL_SUB_LIST, /**< Mesh -  Model Subscription List  */
        BT_EVENT_MESH_NODE_MODEL_GROUP_SUB, /**< Mesh -   Model Virtual Subscription - Add/Delete/overwrite/DeleteAll  */
@@ -1124,6 +1125,14 @@ bt_mesh_network_s * _bt_mesh_get_network_handle_info(char *net_uuid);
 /**
  * @internal
  * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
+ * @brief Gets Mesh node object from node UUID
+ * @since_tizen 6.0
+ */
+bt_mesh_node_s * _bt_mesh_get_node_handle_info(char *node_uuid);
+
+/**
+ * @internal
+ * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
  * @brief Sets node info and return node object.
  * @since_tizen 6.0
  */
index c43e721..cbed9bf 100644 (file)
@@ -295,6 +295,7 @@ static bt_event2index_table_t event2index[] = {
        { BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND, BT_EVENT_MESH_NODE_MODEL_BIND_APPKEY_COMPLETED },
        { BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST, BT_EVENT_MESH_NODE_MODEL_APPKEY_LIST },
        { BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED, BT_EVENT_MESH_NODE_MODEL_EXECUTE_MSG_COMPLETED },
+       { BLUETOOTH_EVENT_MESH_MODEL_MSG_RECEIVED, BT_EVENT_MESH_NODE_MODEL_RECEIVED_MSG },
        { BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST, BT_EVENT_MESH_NODE_MODEL_SUB_LIST },
        { BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED, BT_EVENT_MESH_NODE_MODEL_GROUP_SUB },
        { BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, BT_EVENT_MESH_NODE_MODEL_GROUP_VIR_SUB },
@@ -1468,6 +1469,7 @@ static bool __bt_need_to_handle(int event)
        case BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED:
        case BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS:
        case BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED:
+       case BLUETOOTH_EVENT_MESH_MODEL_MSG_RECEIVED:
        case BLUETOOTH_EVENT_MESH_JOIN_COMPLETED:
                return true;
        default:
@@ -4159,6 +4161,48 @@ static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *us
                        (bt_mesh_appkey_h) appkey_s, msg,
                        bt_event_slot_container[event_index].user_data);
                break;
+       } case BLUETOOTH_EVENT_MESH_MODEL_MSG_RECEIVED: {
+               BT_INFO("BLUETOOTH_EVENT_MESH_MODEL_MSG_RECEIVED");
+               bluetooth_mesh_model_msg_t *res = NULL;
+               bt_mesh_network_s *info = NULL;
+               bt_mesh_element_s *element_s = NULL;
+               bt_mesh_appkey_s *appkey_s = NULL;
+               bt_mesh_model_msg_params_s *msg = NULL;
+               res = (bluetooth_mesh_model_msg_t *)(param->param_data);
+
+               info = _bt_mesh_get_network_handle_info(res->net_uuid);
+               if (!info)
+                       break;
+
+               if (_bt_get_error_code(param->result) == BT_ERROR_NONE)
+                       BT_INFO("Mesh: Node Model Message execution: SUCCESS");
+               else
+                       BT_INFO("Mesh: Node Model Message execution: Failed");
+
+               /* Get Element info */
+               element_s = _bt_mesh_get_node_get_element_from_index(res->net_uuid,
+                               res->primary_unicast, res->elem_index);
+               if (!element_s) {
+                       /* In case of group message, element may not be found */
+                       BT_INFO("Mesh: Element not found in Network!!");
+               }
+
+               /* Get appkey info */
+               appkey_s = _bt_mesh_network_get_appkey_from_index(info, res->appkey_idx);
+
+               msg = g_malloc0(sizeof(bt_mesh_model_msg_params_s));
+               BT_DBG("Mesh: Msg opcode: 0x%4.4X", res->opcode);
+               msg->opcode = res->opcode;
+               if (res->msg_len > 0) {
+                       msg->data = g_memdup(res->msg, res->msg_len + 1);
+                       BT_DBG("Mesh: msg %s", msg->data);
+               }
+
+               ((bt_mesh_model_msg_cb)bt_event_slot_container[event_index].callback)
+                       (_bt_get_error_code(param->result), (bt_mesh_element_h) element_s,
+                               (bt_mesh_appkey_h) appkey_s, msg,
+                                       bt_event_slot_container[event_index].user_data);
+                       break;
        } case BLUETOOTH_EVENT_MESH_JOIN_COMPLETED: {
                BT_INFO("BLUETOOTH_EVENT_MESH_JOIN_FINISHED");
                bluetooth_mesh_node_t *node = NULL;
index d99f759..2a8fbac 100644 (file)
@@ -379,6 +379,13 @@ static gint __bt_mesh_compare_net_uuid(gpointer *a, gpointer *b)
 }
 
 
+static gint __bt_mesh_compare_node_uuid(gpointer *a, gpointer *b)
+{
+       bt_mesh_node_s *net = (bt_mesh_node_s *)a;
+       char *net_uuid = (char *)b;
+       return g_strcmp0(net->uuid, net_uuid);
+}
+
 static void __bt_mesh_insert_elements_in_node(
                bt_mesh_node_s *node, uint16_t unicast,
                        int num_elements, bool is_local)
@@ -614,6 +621,17 @@ bt_mesh_network_s * _bt_mesh_get_network_handle_info(char *net_uuid)
        return (bt_mesh_network_s*)l->data;
 }
 
+bt_mesh_node_s * _bt_mesh_get_node_handle_info(char *net_uuid)
+{
+       GSList *l;
+
+       l = g_slist_find_custom(node_list, net_uuid, (GCompareFunc)__bt_mesh_compare_node_uuid);
+       if (!l)
+               return NULL;
+
+       return (bt_mesh_node_s*)l->data;
+}
+
 /* LCOV_EXCL_START */
 char * _bt_mesh_get_auth_string_from_value(int auth)
 {
@@ -1481,6 +1499,7 @@ int bt_mesh_node_network_join(bt_mesh_node_h node_handle,
 
        bluetooth_mesh_node_t param_node;
        bluetooth_mesh_model_t **param_model = NULL;
+       bt_mesh_network_s *network_s;
 
        BT_CHECK_MESH_SUPPORT();
        BT_CHECK_MESH_INIT_STATUS();
@@ -1548,6 +1567,8 @@ int bt_mesh_node_network_join(bt_mesh_node_h node_handle,
        param_node.num_elements =  g_slist_length(node->elements);
        param_node.primary_unicast = 0x0001;
        _bt_get_random_bytes(param_node.uuid, 16);
+       __bt_mesh_util_convert_hex_to_string((uint8_t *)param_node.uuid,
+                       16, node->uuid, sizeof(node->uuid));
 
        BT_INFO("Mesh: Total Models [%d]", num_models);
        param_model = (bluetooth_mesh_model_t**)g_malloc0(num_models * sizeof(bluetooth_mesh_model_t*));
@@ -1563,6 +1584,15 @@ int bt_mesh_node_network_join(bt_mesh_node_h node_handle,
                offset += g_slist_length(e->models);
        }
 
+       /* Create Network object & fill data in network */
+       network_s = g_malloc0(sizeof(bt_mesh_network_s));
+       network_s->num_nodes = 1;
+       network_s->is_discovered = true;
+       __bt_mesh_util_convert_hex_to_string((uint8_t *)param_node.uuid, 16,
+                       network_s->uuid, sizeof(network_s->uuid));
+       network_s->nodes = g_slist_append(network_s->nodes, node);
+       networks = g_slist_append(networks, network_s);
+
        BT_INFO("Mesh: Send Join Network Request to FRWK");
        error_code = _bt_get_error_code(bluetooth_mesh_network_join(&param_node,
                                num_models, param_model));
@@ -1583,10 +1613,7 @@ int bt_mesh_node_network_join(bt_mesh_node_h node_handle,
        /* Fill Node Data */
        node->is_attached = false;
        node->is_provisioner = false;
-       __bt_mesh_util_convert_hex_to_string((uint8_t *)param_node.uuid,
-               16, node->uuid, sizeof(node->uuid));
 
-       BT_INFO("Mesh: Node UUID [%s]", node->uuid);
        /* Clean up memory */
        for (int i = 0; i < num_models; i++)
                g_free(param_model[i]);
@@ -1640,7 +1667,7 @@ int bt_mesh_node_model_set_msg_cb(bt_mesh_node_h node_handle,
        BT_CHECK_INPUT_PARAMETER(callback);
        BT_CHECK_INPUT_PARAMETER(node_handle);
 
-       _bt_set_cb(BT_EVENT_MESH_NODE_MODEL_EXECUTE_MSG_COMPLETED, callback, user_data);
+       _bt_set_cb(BT_EVENT_MESH_NODE_MODEL_RECEIVED_MSG, callback, user_data);
        FUNC_EXIT;
        return BT_ERROR_NONE;
 }
@@ -3366,6 +3393,54 @@ int bt_mesh_model_send_msg(bt_mesh_model_h model,
        return BT_ERROR_NONE;
 }
 
+int bt_mesh_node_model_send_response(bt_mesh_model_h model,
+       bt_mesh_model_msg_params_s *msg_params, void *user_data)
+{
+       int error_code = BT_ERROR_NONE;
+       bt_mesh_model_s *model_s;
+       bt_mesh_element_s *element_s;
+       bt_mesh_node_s *node_s;
+       bluetooth_mesh_model_msg_t req;
+
+       FUNC_ENTRY;
+       BT_CHECK_MESH_SUPPORT();
+       BT_CHECK_MESH_INIT_STATUS();
+       BT_CHECK_INPUT_PARAMETER(model);
+       BT_CHECK_INPUT_PARAMETER(msg_params);
+
+       model_s = (bt_mesh_model_s*) model;
+       BT_MESH_VALIDATE_HANDLE(model_s, model_list);
+       element_s = (bt_mesh_element_s*) model_s->parent;
+       BT_MESH_VALIDATE_HANDLE(element_s, element_list);
+       node_s = (bt_mesh_node_s*) element_s->parent;
+       BT_MESH_VALIDATE_HANDLE(node_s, node_list);
+
+       BT_INFO("Mesh: Model msg opcode: 0x%2.2X", msg_params->opcode);
+       memset(&req, 0x00, sizeof(bluetooth_mesh_model_msg_t));
+       g_strlcpy(req.net_uuid, node_s->uuid, 33);
+       req.primary_unicast = node_s->unicast;
+       req.elem_index = element_s->index;
+       req.model = model_s->id;
+       req.opcode = msg_params->opcode;
+       if (msg_params->data) {
+               BT_INFO("Mesh: model msg strlen %zd message: %s",
+                       strlen(msg_params->data), msg_params->data);
+               req.msg_len = strlen(msg_params->data);
+               g_strlcpy(req.msg, msg_params->data, req.msg_len + 1);
+       } else {
+               req.msg_len = 0;
+       }
+
+       error_code = _bt_get_error_code(bluetooth_mesh_model_send_response(&req));
+       if (error_code != BT_ERROR_NONE) {
+               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
+               return error_code;
+       }
+
+       FUNC_EXIT;
+       return BT_ERROR_NONE;
+}
+
 int bt_mesh_group_send_msg(bt_mesh_group_h group,
        bt_mesh_appkey_h appkey, bt_mesh_model_msg_params_s *msg_params,
        bt_mesh_model_msg_cb callback, void *user_data)
index 6122b14..7b38407 100644 (file)
@@ -896,6 +896,22 @@ int __bt_mesh_model_send_msg(bt_mesh_model_h model,
        return ret;
 }
 
+void __bt_mesh_node_model_msg_cb(int result, bt_mesh_element_h element, bt_mesh_appkey_h appkey,
+               bt_mesh_model_msg_params_s *msg, void *user_data)
+{
+       __bt_mesh_model_msg_cb(result, element, appkey, msg, user_data);
+       bt_mesh_model_h model_h = g_model_onoff_srv;
+       bt_mesh_model_msg_params_s msg_params;
+
+       int ret = BT_ERROR_NONE;
+       const char *msg_s = "00010000";
+       msg_params.opcode = 0x8202;
+       msg_params.data = g_strdup(msg_s);
+       ret = bt_mesh_node_model_send_response(model_h, &msg_params, NULL);
+       if (ret != BT_ERROR_NONE)
+               TC_PRT("return %s\n", __bt_get_error_message(ret));
+}
+
 int __bt_mesh_group_send_msg(bt_mesh_group_h group,
        bt_mesh_appkey_h appkey, bt_mesh_model_msg_params_s *msg_params,
        bt_mesh_model_msg_cb callback, void *user_data)
@@ -2906,6 +2922,7 @@ int test_input_callback(void *data)
                        ret = bt_mesh_element_create_model(element_sh, &mod_id3, &model_sh3);
                        if (ret != BT_ERROR_NONE)
                                TC_PRT("bt_mesh_element_create_model return %s\n", __bt_get_error_message(ret));
+                       g_model_onoff_srv = model_sh3;
 
                        memset(&mod_id4, 0x00, sizeof(bt_mesh_model_id_s));
                        mod_id4.model_id = BT_MESH_MODEL_ID_GEN_ONOFF_CLIENT;
@@ -2942,7 +2959,7 @@ int test_input_callback(void *data)
                                break;
                        }
                        node_h = GUINT_TO_POINTER(strtoul(g_test_param.params[0], NULL, 16));
-                       ret = bt_mesh_node_model_set_msg_cb(node_h, __bt_mesh_model_msg_cb, NULL);
+                       ret = bt_mesh_node_model_set_msg_cb(node_h, __bt_mesh_node_model_msg_cb, NULL);
                        TC_PRT("returns %s\n", __bt_get_error_message(ret));
                        break;
                }