From 1094dbb4d0156abf651797f5ebfc7689e928b8c2 Mon Sep 17 00:00:00 2001 From: Abhay Agarwal Date: Fri, 31 Jul 2020 08:51:49 +0530 Subject: [PATCH] Mesh: Implement bluetooth service interface for model message This patch implement bt-api and bt-service interface for mesh model message comminications. Change-Id: Ib7eed9cbce7c86ec204e87f64e495d08ca2026e9 Signed-off-by: Abhay Agarwal --- bt-api/bt-event-handler.c | 17 ++++++++++++++ bt-api/bt-mesh.c | 26 ++++++++++++++++++++++ bt-api/bt-request-sender.c | 7 ++++++ bt-service/services/bt-request-handler.c | 26 ++++++++++++++++++++++ bt-service/services/bt-service-event-receiver.c | 1 + bt-service/services/bt-service-event-sender.c | 3 +++ .../services/include/bt-service-mesh-model.h | 2 +- bt-service/services/include/bt-service-mesh-util.h | 6 +++++ bt-service/services/mesh/bt-service-mesh-main.c | 12 ++++++++++ bt-service/services/mesh/bt-service-mesh-model.c | 6 ++--- bt-service/services/mesh/bt-service-mesh-util.c | 3 ++- include/bluetooth-api.h | 1 + include/bluetooth-mesh-api.h | 18 +++++++++++++++ include/bt-internal-types.h | 2 ++ 14 files changed, 125 insertions(+), 5 deletions(-) diff --git a/bt-api/bt-event-handler.c b/bt-api/bt-event-handler.c index abdecd3..5b31961 100644 --- a/bt-api/bt-event-handler.c +++ b/bt-api/bt-event-handler.c @@ -742,6 +742,23 @@ static void __bt_mesh_event_filter(GDBusConnection *connection, _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS, result, info, event_info->cb, event_info->user_data); + } else if (strcasecmp(signal_name, BT_MESH_MODEL_MSG_EXECUTED) == 0) { + int result; + GVariant *param = NULL; + GArray *dbus_data = NULL; + bluetooth_mesh_model_msg_t *info = NULL; + + g_variant_get(parameters, "(iv)", &result, ¶m); + dbus_data = g_array_new(TRUE, TRUE, sizeof(gchar)); + + __bt_fill_garray_from_variant(param, dbus_data); + g_variant_unref(param); + + info = &g_array_index(dbus_data, bluetooth_mesh_model_msg_t, 0); + + _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED, + result, info, + event_info->cb, event_info->user_data); } BT_INFO("Mesh event handler Exit <<"); } diff --git a/bt-api/bt-mesh.c b/bt-api/bt-mesh.c index 8ac40ac..aac4f6b 100644 --- a/bt-api/bt-mesh.c +++ b/bt-api/bt-mesh.c @@ -651,6 +651,32 @@ BT_EXPORT_API int bluetooth_mesh_model_configure_appkey( return result; } +BT_EXPORT_API int bluetooth_mesh_model_send_msg( + bluetooth_mesh_model_msg_t *req) +{ + int result; + bt_user_info_t *user_info; + + BT_CHECK_PARAMETER(req, return); + + user_info = _bt_get_user_data(BT_MESH); + retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL); + + BT_INIT_PARAMS(); + BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_array_append_vals(in_param1, req, sizeof(bluetooth_mesh_model_msg_t)); + + BT_INFO("Mesh: Node Model Send Msg"); + result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_MESH_MODEL_EXECUTE_MSG, + in_param1, in_param2, in_param3, in_param4, + user_info->cb, user_info->user_data); + + BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + return result; +} + BT_EXPORT_API int bluetooth_mesh_node_configure_key( bluetooth_mesh_key_configure_t *req) { diff --git a/bt-api/bt-request-sender.c b/bt-api/bt-request-sender.c index c530c89..207dcda 100644 --- a/bt-api/bt-request-sender.c +++ b/bt-api/bt-request-sender.c @@ -373,6 +373,13 @@ static void __bt_get_event_info(int service_function, GArray *output, *param_data = &g_array_index(output, bluetooth_mesh_model_configure_t, 0); break; + case BT_MESH_MODEL_EXECUTE_MSG: + *event_type = BT_MESH_EVENT; + *event = BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED; + ret_if(output == NULL); + *param_data = &g_array_index(output, + bluetooth_mesh_model_msg_t, 0); + break; case BT_MESH_MODEL_CONFIG_GROUP_SUB: *event_type = BT_MESH_EVENT; *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED; diff --git a/bt-service/services/bt-request-handler.c b/bt-service/services/bt-request-handler.c index 91418a8..cfc467d 100644 --- a/bt-service/services/bt-request-handler.c +++ b/bt-service/services/bt-request-handler.c @@ -46,6 +46,7 @@ #include "bt-service-mesh-util.h" #include "bt-service-mesh-network.h" #include "bt-service-mesh-config-client.h" +#include "bt-service-mesh-model.h" #ifdef TIZEN_FEATURE_BT_DPM #include "bt-service-dpm.h" @@ -3901,6 +3902,30 @@ normal: } break; } + case BT_MESH_MODEL_EXECUTE_MSG: { + bluetooth_mesh_model_msg_t req; + + memset(&req, 0x00, sizeof(bluetooth_mesh_model_msg_t)); + + __bt_service_get_parameters(in_param1, + &req, sizeof(bluetooth_mesh_model_msg_t)); + + result = _bt_mesh_model_execute_msg( + requester_unique_creds, sender, &req); + + /* Save invocation */ + if (result != BLUETOOTH_ERROR_NONE) { + g_array_append_vals(*out_param1, &req, sizeof(bluetooth_mesh_model_msg_t)); + } else { + bluetooth_mesh_model_msg_t *param = \ + g_memdup(&req, sizeof(bluetooth_mesh_model_msg_t)); + + sender = (char*)g_dbus_method_invocation_get_sender(context); + _bt_save_invocation_context(context, result, sender, + function_name, (gpointer)param); + } + break; + } case BT_MESH_NODE_GET_NETKEYS: { bluetooth_mesh_node_discover_t node; memset(&node, 0x00, sizeof(bluetooth_mesh_node_discover_t)); @@ -5101,6 +5126,7 @@ gboolean __bt_service_check_privilege(int function_name, case BT_MESH_NODE_GET_APPKEYS: case BT_MESH_MODEL_CONFIGURE_APPKEY: case BT_MESH_MODEL_GET_APPKEY_LIST: + case BT_MESH_MODEL_EXECUTE_MSG: case BT_MESH_NETWORK_CREATE_GROUP: case BT_MESH_NETWORK_REMOVE_GROUP: case BT_MESH_MODEL_CONFIG_GROUP_SUB: diff --git a/bt-service/services/bt-service-event-receiver.c b/bt-service/services/bt-service-event-receiver.c index 81ba33a..7c52eca 100644 --- a/bt-service/services/bt-service-event-receiver.c +++ b/bt-service/services/bt-service-event-receiver.c @@ -420,6 +420,7 @@ static gboolean __bt_handle_oal_events(gpointer data) case OAL_EVENT_MESH_NETKEY_EXECUTE_EVENT: case OAL_EVENT_MESH_APPKEY_EXECUTE_EVENT: case OAL_EVENT_MESH_DEVKEY_MESSAGE_RECEIVED: + case OAL_EVENT_MESH_MESSAGE_RECEIVED: BT_INFO("Mesh: Event"); if (mesh_cb) mesh_cb(event_type, event_data); diff --git a/bt-service/services/bt-service-event-sender.c b/bt-service/services/bt-service-event-sender.c index 4168567..97aaf88 100644 --- a/bt-service/services/bt-service-event-sender.c +++ b/bt-service/services/bt-service-event-sender.c @@ -575,6 +575,9 @@ int _bt_send_event(int event_type, int event, GVariant *param) case BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND: signal = BT_MESH_MODEL_APPKEY_CONFIGURED; break; + case BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED: + signal = BT_MESH_MODEL_MSG_EXECUTED; + break; case BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST: signal = BT_MESH_MODEL_APPKEY_LIST; break; diff --git a/bt-service/services/include/bt-service-mesh-model.h b/bt-service/services/include/bt-service-mesh-model.h index 21da36d..3257f70 100644 --- a/bt-service/services/include/bt-service-mesh-model.h +++ b/bt-service/services/include/bt-service-mesh-model.h @@ -36,7 +36,7 @@ extern "C" { void _bt_mesh_msg_handler( event_mesh_message_t *event); -int _bt_mesh_node_model_execute_msg(const char *app_cred, +int _bt_mesh_model_execute_msg(const char *app_cred, const char *sender, bluetooth_mesh_model_msg_t *req); diff --git a/bt-service/services/include/bt-service-mesh-util.h b/bt-service/services/include/bt-service-mesh-util.h index 4f6418d..31af938 100644 --- a/bt-service/services/include/bt-service-mesh-util.h +++ b/bt-service/services/include/bt-service-mesh-util.h @@ -185,6 +185,12 @@ extern "C" { #define MESH_OPCODE_VENDOR_MODEL_APP_GET 0x804D #define MESH_OPCODE_VENDOR_MODEL_APP_LIST 0x804E +/*Model msg opcodes */ +#define MESH_OPCODE_MODEL_GENERIC_ONOFF_GET 0x8201 +#define MESH_OPCODE_MODEL_GENERIC_ONOFF_SET 0x8202 +#define MESH_OPCODE_MODEL_GENERIC_ONOFF_SET_UNACK 0x8203 +#define MESH_OPCODE_MODEL_GENERIC_ONOFF_STATUS 0x8204 +/*TO-DO Add more model msg opcodes */ uint32_t _bt_mesh_util_get_timestamp_secs(void); diff --git a/bt-service/services/mesh/bt-service-mesh-main.c b/bt-service/services/mesh/bt-service-mesh-main.c index 167b3ba..fb5b80d 100644 --- a/bt-service/services/mesh/bt-service-mesh-main.c +++ b/bt-service/services/mesh/bt-service-mesh-main.c @@ -42,6 +42,7 @@ #include "bt-service-mesh-network.h" #include "bt-service-mesh-nodes.h" #include "bt-service-mesh-config-client.h" +#include "bt-service-mesh-model.h" #include @@ -326,6 +327,12 @@ static void __handle_mesh_devkey_message_received_event( _bt_mesh_config_client_devkey_msg_handler(event); } +static void __handle_mesh_message_received_event( + event_mesh_message_t *event) +{ + _bt_mesh_msg_handler(event); +} + static void __handle_mesh_network_attached_event( event_mesh_network_attached_t *event) { @@ -664,6 +671,11 @@ static void __handle_mesh_events(int event_type, (event_mesh_devkey_message_t*)event_data); BT_PERMANENT_LOG("Mesh: DevKey Message Received event"); break; + case OAL_EVENT_MESH_MESSAGE_RECEIVED: + __handle_mesh_message_received_event( + (event_mesh_message_t*)event_data); + BT_PERMANENT_LOG("Mesh: Model Message Received event"); + break; default: break; } diff --git a/bt-service/services/mesh/bt-service-mesh-model.c b/bt-service/services/mesh/bt-service-mesh-model.c index 6eebdfd..3df36f1 100644 --- a/bt-service/services/mesh/bt-service-mesh-model.c +++ b/bt-service/services/mesh/bt-service-mesh-model.c @@ -215,7 +215,7 @@ static struct mesh_pending_request *__bt_mesh_get_request_by_response( return NULL; } -bool _bt_mesh_check_pending_request(uint32_t opcode, +bool _bt_mesh_check_pending_msg_request(uint32_t opcode, uint16_t dest, uint8_t net_uuid[]) { const struct mesh_msg_cmd *cmd; @@ -384,7 +384,7 @@ void _bt_mesh_msg_handler(event_mesh_message_t *event) } -int _bt_mesh_node_model_execute_msg(const char *app_cred, const char *sender, +int _bt_mesh_model_execute_msg(const char *app_cred, const char *sender, bluetooth_mesh_model_msg_t *req) { int ret = OAL_STATUS_SUCCESS; @@ -404,7 +404,7 @@ int _bt_mesh_node_model_execute_msg(const char *app_cred, const char *sender, /* Check pending request */ opcode = req->opcode; - if (_bt_mesh_check_pending_request(opcode, req->primary_unicast, + if (_bt_mesh_check_pending_msg_request(opcode, req->primary_unicast, net_uuid.uuid)) { BT_ERR("Device is buzy.."); return BLUETOOTH_ERROR_DEVICE_BUSY; diff --git a/bt-service/services/mesh/bt-service-mesh-util.c b/bt-service/services/mesh/bt-service-mesh-util.c index 12629e2..db3fd53 100644 --- a/bt-service/services/mesh/bt-service-mesh-util.c +++ b/bt-service/services/mesh/bt-service-mesh-util.c @@ -124,8 +124,9 @@ uint16_t _bt_mesh_util_opcode_set(uint32_t opcode, buf[0] = (opcode >> 16) & 0xff; l_put_be16(opcode, buf + 1); return 3; - } else + } else { return 0; + } } bool _bt_mesh_util_opcode_get(const uint8_t *buf, diff --git a/include/bluetooth-api.h b/include/bluetooth-api.h index 9b3da39..80988d1 100644 --- a/include/bluetooth-api.h +++ b/include/bluetooth-api.h @@ -962,6 +962,7 @@ typedef enum { BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED, BLUETOOTH_EVENT_MESH_MODEL_APPKEY_BIND, BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST, + BLUETOOTH_EVENT_MESH_MODEL_MSG_EXECUTED, BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST, BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED, BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED, diff --git a/include/bluetooth-mesh-api.h b/include/bluetooth-mesh-api.h index 5fd4255..777661b 100644 --- a/include/bluetooth-mesh-api.h +++ b/include/bluetooth-mesh-api.h @@ -38,6 +38,7 @@ extern "C" { #define BLUETOOTH_MESH_NETWORK_TOKEN_STRING_LENGTH 16 #define BLUETOOTH_MESH_APP_UNIQUE_CRED_STRING_LENGTH 50 #define BLUETOOTH_MESH_AUTH_VALUE_LENGTH_MAX 50 +#define BLUETOOTH_MESH_MODEL_MSG_LENGTH_MAX 32 typedef enum { BLUETOOTH_MESH_NODE_KEY_ADD, @@ -181,6 +182,17 @@ typedef struct { typedef struct { char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1]; + uint16_t primary_unicast; + int elem_index; + uint16_t model; + uint16_t appkey_idx; + uint16_t opcode; + uint16_t msg_len; + char msg[BLUETOOTH_MESH_MODEL_MSG_LENGTH_MAX + 1]; +} bluetooth_mesh_model_msg_t; + +typedef struct { + char net_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH + 1]; bool is_virtual; uint16_t group_addr; char label_uuid[BLUETOOTH_MESH_NETWORK_UUID_STRING_LENGTH +1]; @@ -376,6 +388,12 @@ int bluetooth_mesh_model_configure_appkey( bluetooth_mesh_model_configure_t *req); /** + * Send message to a model of a node + */ +int bluetooth_mesh_model_send_msg( + bluetooth_mesh_model_msg_t *req); + +/** * Get all application keys added to a model of a node */ int bluetooth_mesh_model_get_all_appkeys( diff --git a/include/bt-internal-types.h b/include/bt-internal-types.h index df61ab3..6dccd25 100644 --- a/include/bt-internal-types.h +++ b/include/bt-internal-types.h @@ -502,6 +502,7 @@ typedef enum { BT_MESH_NODE_GET_APPKEYS, BT_MESH_MODEL_CONFIGURE_APPKEY, BT_MESH_MODEL_GET_APPKEY_LIST, + BT_MESH_MODEL_EXECUTE_MSG, BT_MESH_MODEL_GET_SUBSCRIPTION_LIST, BT_MESH_MODEL_SET_PUBLICATION, BT_MESH_MODEL_GET_PUBLICATION, @@ -764,6 +765,7 @@ typedef struct { #define BT_MESH_NODE_KEY_CONFIGURED "MeshNodeKeyConfigured" #define BT_MESH_NODE_TTL_CONFIGURED "MeshNodeTTLConfigured" #define BT_MESH_MODEL_APPKEY_CONFIGURED "MeshModelAppKeyConfigured" +#define BT_MESH_MODEL_MSG_EXECUTED "MeshModelMsgExecuted" #define BT_MESH_MODEL_APPKEY_LIST "MeshModelAppKeyList" #define BT_MESH_MODEL_SUB_LIST "MeshModelSubscriptionList" #define BT_MESH_MODEL_SUB_CONF "MeshModelSubConfiguration" -- 2.7.4