device: Fix not handling initiator properly
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 11 Sep 2023 22:30:01 +0000 (15:30 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 5 Jan 2024 13:34:04 +0000 (19:04 +0530)
Previously initiator would be set whenever a central key was found
which turns out to be unreliable besides the MGMT New Connection event
does in fact inform if the connection was initiated locally or not.

Fixes: https://github.com/bluez/bluez/issues/598
src/adapter.c
src/device.c
src/device.h

index ed588cc012076ddb7914aea971afae533270abb3..a7a5fe9aa4b69eaac347d14ffee3e8de56cafae4 100644 (file)
@@ -10080,9 +10080,10 @@ static void adapter_remove_device(struct btd_adapter *adapter,
 
 static void adapter_add_connection(struct btd_adapter *adapter,
                                                struct btd_device *device,
-                                               uint8_t bdaddr_type)
+                                               uint8_t bdaddr_type,
+                                               uint32_t flags)
 {
-       device_add_connection(device, bdaddr_type);
+       device_add_connection(device, bdaddr_type, flags);
 
        if (g_slist_find(adapter->connections, device)) {
                btd_error(adapter->dev_id,
@@ -10135,7 +10136,7 @@ static void get_connections_complete(uint8_t status, uint16_t length,
                device = btd_adapter_get_device(adapter, &addr->bdaddr,
                                                                addr->type);
                if (device)
-                       adapter_add_connection(adapter, device, addr->type);
+                       adapter_add_connection(adapter, device, addr->type, 0);
        }
 }
 
@@ -15524,7 +15525,8 @@ static void connected_callback(uint16_t index, uint16_t length,
        if (eir_data.class != 0)
                device_set_class(device, eir_data.class);
 
-       adapter_add_connection(adapter, device, ev->addr.type);
+       adapter_add_connection(adapter, device, ev->addr.type,
+                                       le32_to_cpu(ev->flags));
 
        name_known = device_name_known(device);
 
index 41d2d3387c025bb748cf0c1517174ee2feb4dda2..270053d9ddefe799d176179f70a05f3cf98c56c9 100644 (file)
@@ -474,16 +474,9 @@ static struct bearer_state *get_state(struct btd_device *dev,
 
 bool btd_device_is_initiator(struct btd_device *dev)
 {
-       if (dev->le_state.connected) {
-               /* Mark as initiator if not set yet and auto-connect flag is
-                * set and LTK key is for a peripheral.
-                */
-               if (!dev->le_state.initiator && dev->auto_connect &&
-                                       dev->ltk && !dev->ltk->central)
-                       dev->le_state.initiator = true;
-
+       if (dev->le_state.connected)
                return dev->le_state.initiator;
-       else if (dev->bredr_state.connected)
+       else if (dev->bredr_state.connected)
                return dev->bredr_state.initiator;
 
        return dev->att_io ? true : false;
@@ -5651,7 +5644,8 @@ static void clear_temporary_timer(struct btd_device *dev)
        }
 }
 
-void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type)
+void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type,
+                                                       uint32_t flags)
 {
        struct bearer_state *state = get_state(dev, bdaddr_type);
 
@@ -5678,6 +5672,7 @@ void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type)
                device_set_le_support(dev, bdaddr_type);
 
        state->connected = true;
+       state->initiator = flags & BIT(3);
 
 #ifndef TIZEN_FEATURE_BLUEZ_MODIFY
        if (dev->le_state.connected && dev->bredr_state.connected)
index 7abd023fbd36d0f55c8580262c41fb164b38eff2..11a60d23ace09b500a2fd9507d5cd582b122a6be 100644 (file)
@@ -172,7 +172,8 @@ gboolean device_is_authenticating(struct btd_device *dev, uint8_t bdaddr_type);
 #else
 gboolean device_is_authenticating(struct btd_device *device);
 #endif
-void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type);
+void device_add_connection(struct btd_device *dev, uint8_t bdaddr_type,
+                                                       uint32_t flags);
 void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type,
                                                                bool *remove);
 void device_request_disconnect(struct btd_device *device, DBusMessage *msg);