Mesh: Add API for mesh message communication 14/240014/1
authorAbhay Agarwal <ay.agarwal@samsung.com>
Mon, 27 Jul 2020 07:16:10 +0000 (12:46 +0530)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 31 Jul 2020 10:39:46 +0000 (16:09 +0530)
This patch introduces an interface which can be used to
send/receive mesh messages originated/received by local node
which needs to be encypted/decrypted using application key.

Change-Id: I2013811ef191a88a6a385ca8ba6dcde2f1f7c8de
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
include/bluetooth_internal.h
include/bluetooth_private.h
include/bluetooth_type_internal.h
src/bluetooth-common.c
src/bluetooth-mesh.c

index 1d87d29..f255de5 100644 (file)
@@ -5038,6 +5038,27 @@ int bt_mesh_model_get_appkey_list(bt_mesh_model_h model,
 /**
  * @internal
  * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
+ * @brief Send message originated by a local mesh model.
+ *
+ * @param: in: model, model handle to which message to be sent
+ * @param: in: appkey, appkey to encrypt mesh message
+ * appkey must be valid for corresponding model, i.e
+ * appkey must be bound to the model
+ * @param: in: msg_params, opcode and the message to be sent
+ * @param: in: callback, callback to be called for msg response
+ *
+ * @see bt_mesh_model_msg_params_s
+ *
+ * @since_tizen 6.0
+ * @privlevel platform
+ */
+int bt_mesh_model_send_msg(bt_mesh_model_h model,
+       bt_mesh_appkey_h appkey, bt_mesh_model_msg_params_s *msg_params,
+       bt_mesh_model_msg_cb callback, void *user_data);
+
+/**
+ * @internal
+ * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
  * @brief Create a Mesh group with a given group address in the Local Network
  *
  * @since_tizen 6.0
index df3e9db..1b19e05 100644 (file)
@@ -181,6 +181,7 @@ typedef enum {
        BT_EVENT_MESH_NODE_KEY_CONFIGURATION_COMPLETED, /**< Mesh -  Node Key Configuration event */
        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_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  */
index 68383d7..1ba2757 100644 (file)
@@ -1601,6 +1601,17 @@ typedef struct {
 
 /**
  * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
+ * @brief  structure of Model Message parameters
+ * @since_tizen 6.0
+ */
+typedef struct {
+       uint16_t opcode;
+       char *data;
+} bt_mesh_model_msg_params_s;
+
+
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_MESH_MODULE
  * @brief The handle of a BLE Mesh provisioner or a Mesh Network instance.
  * @since_tizen 6.0
  */
@@ -1805,6 +1816,10 @@ typedef void (*bt_mesh_model_appkey_list_cb)
                        const GSList *appkeylist,
                                void *user_data);
 
+typedef void (*bt_mesh_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);
+
 typedef void (*bt_mesh_model_subscription_op_cb)
                (int result, bt_mesh_model_subscription_op_e op,
                        bt_mesh_model_h model, bt_mesh_group_h group,
index f9a73e9..ed3614e 100644 (file)
@@ -294,6 +294,7 @@ static bt_event2index_table_t event2index[] = {
        { BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED, BT_EVENT_MESH_NODE_TTL_EXECUTE_COMPLETED },
        { 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_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 },
@@ -1429,6 +1430,7 @@ static bool __bt_need_to_handle(int event)
        case BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED:
        case BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED:
        case BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS:
+       case BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED:
                return true;
        default:
                break;
@@ -4123,6 +4125,48 @@ static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *us
                                         total, sublist,
                                                bt_event_slot_container[event_index].user_data);
                break;
+       } case BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED : {
+               BT_INFO("BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED");
+               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) {
+                       BT_ERR("Mesh: Element not found in Network!! Unexpected!!");
+                       break;
+               }
+
+               /* 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;
        }
        default:
                BT_INFO("Unknown function");
index 243e206..b423dcc 100644 (file)
@@ -2833,6 +2833,76 @@ int bt_mesh_model_get_appkey_list(bt_mesh_model_h model,
        return error_code;
 }
 
+int bt_mesh_model_send_msg(bt_mesh_model_h model,
+       bt_mesh_appkey_h appkey, bt_mesh_model_msg_params_s *msg_params,
+       bt_mesh_model_msg_cb callback, 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;
+       bt_mesh_network_s *network_s;
+       bt_mesh_appkey_s *appkey_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(appkey);
+       BT_CHECK_INPUT_PARAMETER(msg_params);
+       BT_CHECK_INPUT_PARAMETER(callback);
+
+       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);
+
+       appkey_s = (bt_mesh_appkey_s*) appkey;
+       BT_MESH_VALIDATE_HANDLE(appkey_s, appkey_list);
+
+       network_s = (bt_mesh_network_s*) node_s->parent;
+       BT_MESH_VALIDATE_HANDLE(network_s, networks);
+
+       /* Return error, if node is not attached */
+       if (!node_s->is_attached)
+               return BT_ERROR_INVALID_PARAMETER;
+
+       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, network_s->uuid, 33);
+       req.primary_unicast = node_s->unicast;
+       req.elem_index = element_s->index;
+       req.model = model_s->id;
+       req.appkey_idx = appkey_s->appkey_index;
+       req.opcode = msg_params->opcode;
+       if (msg_params->data) {
+               BT_INFO("Mesh: model msg strlen %d message: %s",
+               strlen(msg_params->data), msg_params->data);
+
+               req.msg_len = strlen(msg_params->data);
+               g_strlcpy(req.msg, msg_params->data, sizeof(msg_params->data) + 1);
+       } else {
+               req.msg_len = 0;
+       }
+
+       error_code = _bt_get_error_code(bluetooth_mesh_model_send_msg(&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;
+       _bt_set_cb(BT_EVENT_MESH_NODE_MODEL_EXECUTE_MSG_COMPLETED, callback, user_data);
+       return BT_ERROR_NONE;
+}
+
 int bt_mesh_network_foreach_groups(bt_mesh_network_h network,
                bt_mesh_network_group_info_cb callback, void *user_data)
 {