Fix : GATT client services are not updated 17/149417/1
authorSeungyoun Ju <sy39.ju@samsung.com>
Thu, 7 Sep 2017 11:28:33 +0000 (20:28 +0900)
committerSeungyoun Ju <sy39.ju@samsung.com>
Tue, 12 Sep 2017 08:08:36 +0000 (17:08 +0900)
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 <sy39.ju@samsung.com>
src/bluetooth-common.c
src/bluetooth-gatt.c

index 94250f0ebe42854fda80351f3c9eb8bed0512d31..31ff6031ce1a2f7dc7028bcbfe9cc815117abfb2 100644 (file)
@@ -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)
index 199b9739e9e5311fad7d71c85c250ba82908d9a9..04d508eea2c7b4bc305c30f877d646fa0e39ed49 100644 (file)
@@ -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");