From 423172653af6f89e6ab5a4f8954da112725c85c1 Mon Sep 17 00:00:00 2001 From: "h.sandeep" Date: Mon, 23 May 2016 18:08:54 +0530 Subject: [PATCH] GATT-Server: Enable Read/Write request Callbacks for Descriptor. Change-Id: I1162f64f10ca669ffc24a98e25d8b7851ff6e82c Signed-off-by: h.sandeep --- include/bluetooth_private.h | 6 ++++++ src/bluetooth-common.c | 39 ++++++++++++++++++++++++++----------- src/bluetooth-gatt.c | 35 +++++++++++++++++++++++++-------- test/bt_unit_test.c | 7 +++++++ 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/include/bluetooth_private.h b/include/bluetooth_private.h index 81cf98c..e064c24 100644 --- a/include/bluetooth_private.h +++ b/include/bluetooth_private.h @@ -316,6 +316,12 @@ typedef struct { int permissions; + bt_gatt_server_value_changed_cb server_value_changed_cb; + void *server_value_changed_user_data; + + bt_gatt_server_read_value_requested_cb read_requested_cb; + void *read_requested_user_data; + int value_length; char *value; } bt_gatt_descriptor_s; diff --git a/src/bluetooth-common.c b/src/bluetooth-common.c index ada528f..43aa1e3 100644 --- a/src/bluetooth-common.c +++ b/src/bluetooth-common.c @@ -486,12 +486,12 @@ static int __bt_get_bt_device_connection_info_s(bt_device_connection_info_s **de } static bt_gatt_server_read_value_requested_cb __bt_gatt_attribute_get_read_cb( - bt_gatt_h service, bt_gatt_h characteristic, void **user_data) + bt_gatt_h service, bt_gatt_h attribute, void **user_data) { gchar *svc_path = (gchar *)service; - gchar *chr_path = (gchar *)characteristic; + gchar *att_path = (gchar *)attribute; const GSList *gatt_server_list = NULL; - const GSList *l1, *l2, *l3; + const GSList *l1, *l2, *l3, *l4; gatt_server_list = _bt_gatt_get_server_list(); @@ -508,12 +508,26 @@ static bt_gatt_server_read_value_requested_cb __bt_gatt_attribute_get_read_cb( for (l3 = svc->characteristics; l3 != NULL; l3 = l3->next) { bt_gatt_characteristic_s *chr = l3->data; - if (chr && g_strcmp0(chr->path, chr_path) == 0) { - if (chr->read_requested_cb) { - *user_data = chr->read_requested_user_data; - return chr->read_requested_cb; - } else - return NULL; + if (chr) { + if (g_strcmp0(chr->path, att_path) == 0) { + if (chr->read_requested_cb) { + *user_data = chr->read_requested_user_data; + return chr->read_requested_cb; + } else + return NULL; + } else { + for (l4 = chr->descriptors; l4 != NULL; l4 = l4->next) { + bt_gatt_descriptor_s *desc = l4->data; + + if (desc && g_strcmp0(desc->path, att_path) == 0) { + if (desc->read_requested_cb) { + *user_data = desc->read_requested_user_data; + return desc->read_requested_cb; + } else + return NULL; + } + } + } } } } @@ -557,8 +571,11 @@ static bt_gatt_server_value_changed_cb __bt_gatt_attribute_get_value_change_cb( bt_gatt_descriptor_s *desc = l4->data; if (desc && g_strcmp0(desc->path, att_path) == 0) { - /* TODO: Call value changed callback registerd for the descriptor */ - return NULL; + if (desc->read_requested_cb) { + *user_data = desc->server_value_changed_user_data; + return desc->server_value_changed_cb; + } else + return NULL; } } } diff --git a/src/bluetooth-gatt.c b/src/bluetooth-gatt.c index fb6003a..e175aef 100644 --- a/src/bluetooth-gatt.c +++ b/src/bluetooth-gatt.c @@ -2110,15 +2110,25 @@ int bt_gatt_server_set_read_value_requested_cb(bt_gatt_h gatt_handle, bt_gatt_server_read_value_requested_cb callback, void *user_data) { + bt_gatt_common_s *handle = (bt_gatt_common_s *)gatt_handle; bt_gatt_characteristic_s *chr = (bt_gatt_characteristic_s *)gatt_handle; + bt_gatt_descriptor_s *desc = (bt_gatt_descriptor_s*)gatt_handle; BT_CHECK_INIT_STATUS(); BT_CHECK_GATT_SERVER_INIT_STATUS(); BT_CHECK_INPUT_PARAMETER(gatt_handle); BT_CHECK_INPUT_PARAMETER(callback); - chr->read_requested_cb = callback; - chr->read_requested_user_data = user_data; + if (handle->type == BT_GATT_TYPE_CHARACTERISTIC) { + chr->read_requested_cb = callback; + chr->read_requested_user_data = user_data; + } else if (handle->type == BT_GATT_TYPE_DESCRIPTOR) { + desc->read_requested_cb = callback; + desc->read_requested_user_data = user_data; + } else { + BT_ERR("Type is invalid(type:%d)", handle->type); + return BT_ERROR_INVALID_PARAMETER; + } _bt_set_cb(BT_EVENT_GATT_SERVER_READ_REQUESTED, callback, user_data); @@ -2363,20 +2373,29 @@ int bt_gatt_server_notify(bt_gatt_h characteristic, bool need_confirm, return ret; } -int bt_gatt_server_set_value_changed_cb(bt_gatt_h characteristic, +int bt_gatt_server_set_value_changed_cb(bt_gatt_h gatt_handle, bt_gatt_server_value_changed_cb callback, void *user_data) { - bt_gatt_characteristic_s *chr = (bt_gatt_characteristic_s *)characteristic; + bt_gatt_common_s *handle = (bt_gatt_common_s *)gatt_handle; + bt_gatt_characteristic_s *chr = (bt_gatt_characteristic_s *)gatt_handle; + bt_gatt_descriptor_s *desc = (bt_gatt_descriptor_s*)gatt_handle; BT_CHECK_INIT_STATUS(); BT_CHECK_GATT_SERVER_INIT_STATUS(); - - BT_CHECK_INPUT_PARAMETER(characteristic); + BT_CHECK_INPUT_PARAMETER(gatt_handle); BT_CHECK_INPUT_PARAMETER(callback); - chr->server_value_changed_cb = callback; - chr->server_value_changed_user_data = user_data; + if (handle->type == BT_GATT_TYPE_CHARACTERISTIC) { + chr->server_value_changed_cb = callback; + chr->server_value_changed_user_data = user_data; + } else if (handle->type == BT_GATT_TYPE_DESCRIPTOR) { + desc->server_value_changed_cb = callback; + desc->server_value_changed_user_data = user_data; + } else { + BT_ERR("Type is invalid(type:%d)", handle->type); + return BT_ERROR_INVALID_PARAMETER; + } return BT_ERROR_NONE; } diff --git a/test/bt_unit_test.c b/test/bt_unit_test.c index dbc747f..a4ebc72 100644 --- a/test/bt_unit_test.c +++ b/test/bt_unit_test.c @@ -6197,6 +6197,13 @@ int test_input_callback(void *data) desc_value, value_length, &descriptor); TC_PRT("bt_gatt_descriptor_create : %s\n", __bt_get_error_message(ret)); + bt_gatt_server_set_read_value_requested_cb(descriptor, + __bt_gatt_server_read_value_requested_cb, NULL); + + ret = bt_gatt_server_set_value_changed_cb(descriptor, + __bt_gatt_server_value_changed_cb, + NULL); + ret = bt_gatt_characteristic_add_descriptor(characteristic, descriptor); TC_PRT("bt_gatt_characteristic_add_descriptor : %s\n", __bt_get_error_message(ret)); -- 2.34.1