Mesh: Add framework event handlers
authorAnupam Roy <anupam.r@samsung.com>
Fri, 17 Jul 2020 17:54:37 +0000 (23:24 +0530)
committerAnupam Roy <anupam.r@samsung.com>
Fri, 17 Jul 2020 17:54:37 +0000 (23:24 +0530)
This patch handles following
- Catches events sent by bt-service
- Handles DBUS exception cases for Async requests

Change-Id: I2e74d2f177e76f12822da9aba30f96766268cc86
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
bt-api/bt-common.c
bt-api/bt-event-handler.c
bt-api/bt-request-sender.c
bt-api/include/bt-common.h

index 39b5480..53f13d5 100644 (file)
@@ -41,6 +41,8 @@
 #include "bluetooth-gatt-client-api.h"
 #endif
 
+#include "bluetooth-mesh-api.h"
+
 #include "bt-common.h"
 #include "bt-request-sender.h"
 #include "bt-event-handler.h"
@@ -319,6 +321,23 @@ void _bt_gatt_server_event_cb(int event, int result, void *param,
 }
 #endif
 
+void _bt_mesh_event_cb(int event, int result, void *param,
+               void *callback, void *user_data)
+{
+       BT_DBG("_bt_mesh_event_cb");
+       mesh_event_param_t bt_event = { 0, 0, NULL, NULL };
+       bt_event.event = event;
+       bt_event.result = result;
+       bt_event.param_data = param;
+       if (callback) {
+               BT_DBG("Mesh event callback is registered");
+               ((mesh_cb_func_ptr)callback)(bt_event.event, &bt_event,
+                       user_data);
+       } else {
+               BT_ERR("Mesh event callback is not registered!!!");
+       }
+}
+
 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
                                unsigned int cod)
 {
index 3696c85..2b754b5 100644 (file)
@@ -30,6 +30,8 @@
 #include "bluetooth-gatt-server-api.h"
 #endif
 
+#include "bluetooth-mesh-api.h"
+
 #include "bt-common.h"
 #include "bt-event-handler.h"
 #include "bt-gatt-client.h"
@@ -400,6 +402,325 @@ static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
        }
 }
 
+/* Mesh Event Handler */
+static void __bt_mesh_event_filter(GDBusConnection *connection,
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
+{
+       BT_INFO("Mesh event handler Entry >>");
+       bt_event_info_t *event_info;
+
+       event_info = (bt_event_info_t *)user_data;
+       ret_if(event_info == NULL);
+
+       if (strcasecmp(signal_name, BT_MESH_SCAN_STARTED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_network_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_network_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_SCAN_STARTED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_SCAN_FINISHED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_network_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_network_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_SCAN_FINISHED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_SCAN_RESULT) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_scan_result_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_scan_result_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_SCAN_RESULT,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_AUTHENTICATION_REQ) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_authentication_request_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_authentication_request_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_AUTHENTICATION_REQUEST,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_PROVISIONING_RESULT) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_provisioning_result_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_provisioning_result_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_NODE_BROWSED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_node_discover_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_node_discover_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_BROWSED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_NODE_VEND_FEATS) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_node_features_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_node_features_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_NODE_KEY_CONFIGURED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_key_configure_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_key_configure_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_APPKEY_LIST) == 0) {
+               int result;
+               GVariantIter *iter = NULL;
+               bluetooth_mesh_model_configure_t info;
+               uint16_t node_unicast;
+               int elem_idx;
+               uint32_t model;
+               const char *net_uuid = NULL;
+               int total = 0;
+               uint16_t appkey_idx;
+               int i;
+
+               g_variant_get(parameters, "(i&sqiui(ay))", &result, &net_uuid,
+                               &node_unicast, &elem_idx, &model, &total, &iter);
+
+               memset(&info, 0x00, sizeof(bluetooth_mesh_model_configure_t));
+               info.primary_unicast = node_unicast;
+               g_strlcpy(info.net_uuid, net_uuid, 33);
+               info.elem_index = elem_idx;
+               info.model = model;
+               info.appkeylist_count = total;
+
+               if (total) {
+                       info.appkey_list = (uint16_t**)g_malloc0(sizeof(uint16_t*) * total);
+
+                       for (i = 0; i < total && g_variant_iter_loop(iter, "q", &appkey_idx); ++i) {
+                               info.appkey_list[i] = g_malloc0(sizeof(uint16_t));
+                               *info.appkey_list[i] = appkey_idx;
+                       }
+               }
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST,
+                               result, &info,
+                               event_info->cb, event_info->user_data);
+
+               if (total) {
+                       for (i = 0; i < total; i++)
+                               g_free(info.appkey_list[i]);
+                       g_variant_iter_free(iter);
+               }
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_SUB_LIST) == 0) {
+               int result;
+               GVariantIter *iter = NULL;
+               bluetooth_mesh_model_configure_t info;
+               uint16_t node_unicast;
+               int elem_idx;
+               uint32_t model;
+               const char *net_uuid = NULL;
+               int total = 0;
+               uint16_t sub_addr;
+               int i;
+
+               g_variant_get(parameters, "(i&sqiui(ay))", &result, &net_uuid,
+                               &node_unicast, &elem_idx, &model, &total, &iter);
+
+               memset(&info, 0x00, sizeof(bluetooth_mesh_model_configure_t));
+               info.primary_unicast = node_unicast;
+               g_strlcpy(info.net_uuid, net_uuid, 33);
+               info.elem_index = elem_idx;
+               info.model = model;
+               info.sublist_count = total;
+
+               if (total) {
+                       info.sub_list = (uint16_t**)g_malloc0(sizeof(uint16_t*) * total);
+                       for (i = 0; i < total && g_variant_iter_loop(iter, "q", &sub_addr); ++i) {
+                               info.sub_list[i] = g_malloc0(sizeof(uint16_t));
+                               *info.sub_list[i] = sub_addr;
+                       }
+               }
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST,
+                               result, &info,
+                               event_info->cb, event_info->user_data);
+
+               if (total) {
+                       for (i = 0; i < total; i++)
+                               g_free(info.sub_list[i]);
+                       g_variant_iter_free(iter);
+               }
+       } else if (strcasecmp(signal_name, BT_MESH_NODE_TTL_CONFIGURED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_node_ttl_info_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_node_ttl_info_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_NODE_TTL_CONFIGURED) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_node_ttl_info_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_node_ttl_info_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_SUB_CONF) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_key_configure_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_key_configure_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_VIR_SUB_CONF) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_key_configure_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_key_configure_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       } else if (strcasecmp(signal_name, BT_MESH_MODEL_PUB_STATUS) == 0) {
+               int result;
+               GVariant *param = NULL;
+               GArray *dbus_data = NULL;
+               bluetooth_mesh_key_configure_t *info = NULL;
+
+               g_variant_get(parameters, "(iv)", &result, &param);
+               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_key_configure_t, 0);
+
+               _bt_mesh_event_cb(BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS,
+                               result, info,
+                               event_info->cb, event_info->user_data);
+       }
+       BT_INFO("Mesh event handler Exit <<");
+}
+
 void __bt_adapter_event_filter(GDBusConnection *connection,
                                                 const gchar *sender_name,
                                                 const gchar *object_path,
@@ -4178,6 +4499,10 @@ int _bt_register_event(int event_type, void *event_cb, void *user_data)
                path = BT_GATT_CLIENT_PATH;
                break;
 #endif
+       case BT_MESH_EVENT:
+               event_func = __bt_mesh_event_filter;
+               path = BT_MESH_PATH;
+               break;
        default:
                BT_ERR("Unknown event");
                return BLUETOOTH_ERROR_INTERNAL;
index c31ab18..a8ce6d9 100644 (file)
@@ -34,6 +34,8 @@
 #include "bluetooth-gatt-client-api.h"
 #endif
 
+#include "bluetooth-mesh-api.h"
+
 static GSList *sending_requests;
 
 static GDBusProxy *service_gproxy;
@@ -329,6 +331,90 @@ static void __bt_get_event_info(int service_function, GArray *output,
                *param_data = &g_array_index(output,
                                bt_hdp_disconnected_t, 0);
                break;
+       case BT_MESH_NETWORK_PROVISION_DEVICE:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_PROVISIONING_FINISHED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_provisioning_request_t, 0);
+               break;
+       case BT_MESH_NODE_BROWSE:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_NODE_BROWSED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_node_discover_t, 0);
+               break;
+       case BT_MESH_NODE_GET_VENDOR_FEATURES:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_NODE_VENDOR_FEATURES;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_node_features_t, 0);
+               break;
+       case BT_MESH_NODE_CONFIGURE_KEY:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_NODE_KEY_CONFIGURED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_key_configure_t, 0);
+               break;
+       case BT_MESH_NODE_TTL_EXECUTE:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_NODE_TTL_CONFIGURED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_node_ttl_info_t, 0);
+               break;
+       case BT_MESH_MODEL_CONFIGURE_APPKEY:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_NODE_MODEL_APPKEY_BIND;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_CONFIG_GROUP_SUB:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_CONFGURED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_GET_APPKEY_LIST:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_APPKEY_LIST;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_GET_SUBSCRIPTION_LIST:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_SUBSCRIPTION_LIST;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_CONFIG_VIRTUAL_GROUP_SUB:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_VIRTUAL_SUBSCRIPTION_CONFGURED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_SET_PUBLICATION:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
+       case BT_MESH_MODEL_GET_PUBLICATION:
+               *event_type = BT_MESH_EVENT;
+               *event = BLUETOOTH_EVENT_MESH_MODEL_PUBLICATION_STATUS;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                               bluetooth_mesh_model_configure_t, 0);
+               break;
        default:
                BT_ERR("Unknown function");
                return;
@@ -497,6 +583,9 @@ static void __send_request_cb(GDBusProxy *proxy,
                ((gatt_client_cb_func_ptr)cb_data->cb)(bt_event.event,
                                (gatt_client_event_param_t*)&bt_event, cb_data->user_data);
 #endif
+       } else if (event_type == BT_MESH_EVENT) {
+               ((mesh_cb_func_ptr)cb_data->cb)(bt_event.event,
+                               (mesh_event_param_t*)&bt_event, cb_data->user_data);
        } else {
                BT_INFO("Not handled event type : %d", event_type);
        }
index 6dc125d..e7bbd8d 100644 (file)
@@ -311,6 +311,10 @@ void _bt_gatt_client_event_cb(int event, int result, void *param,
                void *callback, void *user_data);
 #endif
 
+/*Mesh*/
+void _bt_mesh_event_cb(int event, int result, void *param,
+               void *callback, void *user_data);
+
 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
                                unsigned int cod);