From 8d1b54d0df28760d058b586459a94b3d73090e9f Mon Sep 17 00:00:00 2001 From: Seungyoun Ju Date: Thu, 7 Sep 2017 20:28:33 +0900 Subject: [PATCH] Fix : GATT client services are not updated If GATT client handle is created during service discovery, some services could not be imported. But there is no logic to refresh services once discovery is done. This patch set a flag of services_discovered to false if importing one of services is failed. So that, when service discovery is done, it imports again services. Change-Id: I366c09ed208e11b47423b034d01e288e09c1c0a7 Signed-off-by: Seungyoun Ju --- src/bluetooth-common.c | 10 +++------- src/bluetooth-gatt.c | 32 +++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/bluetooth-common.c b/src/bluetooth-common.c index 94250f0..31ff603 100644 --- a/src/bluetooth-common.c +++ b/src/bluetooth-common.c @@ -1830,14 +1830,10 @@ static void __bt_event_proxy(int event, bluetooth_event_param_t *param, void *us _bt_convert_address_to_string(&device_addr, (bluetooth_device_address_t *)(param->param_data)); client_s = (bt_gatt_client_s *)_bt_gatt_get_client(device_addr); - if (client_s && !client_s->services_discovered) { - if (_bt_gatt_client_update_services(client_s) != BT_ERROR_NONE) - BT_ERR("_bt_gatt_client_update_services failed"); - else - client_s->services_discovered = true; - } - if (client_s) + if (client_s) { client_s->connected = true; + _bt_gatt_client_update_services(client_s); + } if (event_index >= 0) cb = bt_event_slot_container[event_index].callback; if (cb) diff --git a/src/bluetooth-gatt.c b/src/bluetooth-gatt.c index 199b973..04d508e 100644 --- a/src/bluetooth-gatt.c +++ b/src/bluetooth-gatt.c @@ -183,6 +183,16 @@ int _bt_gatt_client_update_services(bt_gatt_client_h client) int ret; int i; + if (!client_s->connected) { + BT_INFO("Not connected"); + return BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED; + } + + if (client_s->services_discovered) { + BT_INFO("Already discovered"); + return BT_ERROR_ALREADY_DONE; + } + _bt_convert_address_to_hex(&addr_hex, client_s->remote_address); ret = bluetooth_gatt_get_primary_services(&addr_hex, &prim_svc); @@ -192,7 +202,8 @@ int _bt_gatt_client_update_services(bt_gatt_client_h client) return ret; } - g_slist_free_full(client_s->services, __bt_gatt_free_service); + if (client_s->services) + g_slist_free_full(client_s->services, __bt_gatt_free_service); client_s->services = NULL; if (prim_svc.count == 0) { @@ -200,10 +211,13 @@ int _bt_gatt_client_update_services(bt_gatt_client_h client) return BT_ERROR_NONE; } + client_s->services_discovered = true; for (i = 0; i < prim_svc.count; i++) { - if (!_bt_gatt_client_add_service(client, prim_svc.handle[i])) + if (!_bt_gatt_client_add_service(client, prim_svc.handle[i])) { BT_ERR("_bt_gatt_client_add_service is failed [%s]", prim_svc.handle[i]); + client_s->services_discovered = false; + } } g_strfreev(prim_svc.handle); @@ -2781,17 +2795,21 @@ int bt_gatt_client_create(const char *remote_address, bt_gatt_client_h *client) return ret; } - *client = (bt_gatt_client_h)client_s; - gatt_client_list = g_slist_append(gatt_client_list, client_s); - if (bt_device_is_profile_connected(remote_address, BT_PROFILE_GATT, &connected) != BT_ERROR_NONE) BT_ERR("bt_device_is_profile_connected is failed"); else client_s->connected = connected; - if (_bt_gatt_client_update_services(*client) == BT_ERROR_NONE) - client_s->services_discovered = true; + ret = _bt_gatt_client_update_services(client_s); + if (ret != BT_ERROR_NONE) { + BT_INFO("_bt_gatt_client_update_services returns 0x%X. " + "It could be updated when service is available.", ret); + ret = BT_ERROR_NONE; + } + + *client = (bt_gatt_client_h)client_s; + gatt_client_list = g_slist_append(gatt_client_list, client_s); BT_INFO("GATT Client Handle is created"); -- 2.7.4