shared/gatt-client: Allow registering with NULL callback
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Thu, 5 Jan 2023 21:54:50 +0000 (13:54 -0800)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 5 Jan 2024 10:11:34 +0000 (15:41 +0530)
This makes bt_gatt_client_register_notify allow registering with NULL
callback which is interpreted as the CCC write has already been
performed therefore it won't be written again.

src/shared/gatt-client.c

index 8c9b262..aa694a0 100644 (file)
@@ -1929,8 +1929,11 @@ static unsigned int register_notify(struct bt_gatt_client *client,
                 * descriptor.
                 */
                chrc = notify_chrc_create(client, handle);
-               if (!chrc)
+               if (!chrc) {
+                       DBG(client, "Unable to locate characteristic at 0x%04x",
+                                                       handle);
                        return 0;
+               }
        }
 
        /* Fail if we've hit the maximum allowed notify sessions */
@@ -1968,9 +1971,10 @@ static unsigned int register_notify(struct bt_gatt_client *client,
        }
 
        /*
-        * If the ref count > 1, then notifications are already enabled.
+        * If the ref count > 1, ccc handle cannot be found or registration
+        * callback is not set consider notifications are already enabled.
         */
-       if (chrc->notify_count > 1 || !chrc->ccc_handle) {
+       if (chrc->notify_count > 1 || !chrc->ccc_handle || !callback) {
                complete_notify_request(notify_data);
                return notify_data->id;
        }
@@ -2485,6 +2489,9 @@ static void notify_cb(struct bt_att_chan *chan, uint8_t opcode,
        }
 #endif
 
+       if (queue_isempty(client->notify_list))
+               return;
+
        bt_gatt_client_ref(client);
 
        memset(&data, 0, sizeof(data));
@@ -4098,7 +4105,8 @@ unsigned int bt_gatt_client_register_notify(struct bt_gatt_client *client,
                                void *user_data,
                                bt_gatt_client_destroy_func_t destroy)
 {
-       if (!client || !client->db || !chrc_value_handle || !callback)
+       if (!client || !client->db || !chrc_value_handle ||
+                               (!callback && !notify))
                return 0;
 
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY