Skip to contentSkip to conversation

Bluetooth: Legacy name discovery support for mgmt

PATCH In review [email protected] 2 participants 22 messages Jan 5, 2012 – Jan 8, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Bluetooth: Legacy name discovery support for mgmt

Hi,

These patches implement initial legacy name resolving support as it is currently specified in the mgmt API. This is one of the last bigger pieces missing in order to be able to abandon hciops for good. There's still possibly going to be some API changes to this (e.g. we've been discussing of getting rid of mgmt_ev_remote_name) and some optimizations (e.g. the "resolve" list should really be sorted by RSSI) but these patches at least enable basic support of legacy resolving together with the latest user-space git.

Johan Hedberg (9):
Bluetooth: Fix persistency of the HCI_MGMT flag
Bluetooth: Convert inquiry cache to use standard list types
Bluetooth: Move Extended Inquiry Response defines to hci.h
Bluetooth: Add initial mgmt_confirm_name support
Bluetooth: Return updated name state with hci_inquiry_cache_update
Bluetooth: Flush inquiry cache when starting mgmt triggered inquiry
Bluetooth: Rename hdev->inq_cache to hdev->discovery
Bluetooth: Add discovery state tracking
Bluetooth: Add name resolving support for mgmt based discovery

 include/net/bluetooth/hci.h      |   13 +++
 include/net/bluetooth/hci_core.h |   54 ++++++++++---
 net/bluetooth/hci_core.c         |  155 +++++++++++++++++++++++++++++++-------
 net/bluetooth/hci_event.c        |  141 +++++++++++++++++++++++++++++++---
 net/bluetooth/hci_sysfs.c        |    4 +-
 net/bluetooth/mgmt.c             |  112 +++++++++++++++++++++++----
 6 files changed, 409 insertions(+), 70 deletions(-)
#1 lore raw

PATCH 1/9 Bluetooth: Fix persistency of the HCI_MGMT flag

From: Johan Hedberg <[email protected]>

The HCI_MGMT flag needs to remain set even after closing an HCI device.

---
 net/bluetooth/hci_core.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +2 -2
index 6d38d80..52edc11 100644
@@ -670,8 +670,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	mgmt_powered(hdev, 0);
 	hci_dev_unlock(hdev);
 
-	/* Clear flags */
-	hdev->flags = 0;
+	/* Clear flags, except persistent ones like HCI_MGMT */
+	hdev->flags &= BIT(HCI_MGMT);
 
 	hci_req_unlock(hdev);
 
Signature · 1 line
1.7.7.3
maintainer
#2 lore raw

Re: [PATCH 1/9] Bluetooth: Fix persistency of the HCI_MGMT flag

Hi Johan,

Quoted 12 lines

The HCI_MGMT flag needs to remain set even after closing an HCI device.

---
 net/bluetooth/hci_core.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +0 -0
index 6d38d80..52edc11 100644

net/bluetooth/hci_core.c static int hci_dev_do_close(struct hci_dev *hdev) · line 677

Quoted 10 lines
net/bluetooth/hci_core.c +2 -2
@@ -670,8 +670,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	mgmt_powered(hdev, 0);
 	hci_dev_unlock(hdev);
 
-	/* Clear flags */
-	hdev->flags = 0;
+	/* Clear flags, except persistent ones like HCI_MGMT */
+	hdev->flags &= BIT(HCI_MGMT);
 
 	hci_req_unlock(hdev);
 

can we just move this away from hdev->flags into a hdev->dev_flags.

Regards

Marcel

#3 lore raw

Re: [PATCH 1/9] Bluetooth: Fix persistency of the HCI_MGMT flag

Hi Marcel,

Quoted 20 lines · Marcel Holtmann
Quoted 20 lines

The HCI_MGMT flag needs to remain set even after closing an HCI device.

---
 net/bluetooth/hci_core.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +2 -2
index 6d38d80..52edc11 100644
@@ -670,8 +670,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	mgmt_powered(hdev, 0);
 	hci_dev_unlock(hdev);
 
-	/* Clear flags */
-	hdev->flags = 0;
+	/* Clear flags, except persistent ones like HCI_MGMT */
+	hdev->flags &= BIT(HCI_MGMT);
 
Quoted 4 lines

net/bluetooth/hci_core.c static int hci_dev_do_close(struct hci_dev *hdev) · line 677

Quoted 2 lines
net/bluetooth/hci_core.c +0 -0
 	hci_req_unlock(hdev);
 

can we just move this away from hdev->flags into a hdev->dev_flags.

Yes, that should be done (and not just for HCI_MGMT but also for other related flags) but it should imo be in a separate patch. Simply moving to dev_flags won't fix this particular bug since there's a similar "hdev->dev_flags = 0;" inside hci_cc_reset() in hci_event.c.

Johan

maintainer
#4 lore raw

Re: [PATCH 1/9] Bluetooth: Fix persistency of the HCI_MGMT flag

Hi Johan,

Quoted 25 lines
Quoted 20 lines
Quoted 20 lines

The HCI_MGMT flag needs to remain set even after closing an HCI device.

---
 net/bluetooth/hci_core.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
net/bluetooth/hci_core.c +2 -2
index 6d38d80..52edc11 100644
@@ -670,8 +670,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	mgmt_powered(hdev, 0);
 	hci_dev_unlock(hdev);
 
-	/* Clear flags */
-	hdev->flags = 0;
+	/* Clear flags, except persistent ones like HCI_MGMT */
+	hdev->flags &= BIT(HCI_MGMT);
 
Quoted 4 lines

net/bluetooth/hci_core.c static int hci_dev_do_close(struct hci_dev *hdev) · line 677

Quoted 2 lines
net/bluetooth/hci_core.c +0 -0
 	hci_req_unlock(hdev);
 

can we just move this away from hdev->flags into a hdev->dev_flags.

Quoted 4 lines

Yes, that should be done (and not just for HCI_MGMT but also for other related flags) but it should imo be in a separate patch. Simply moving to dev_flags won't fix this particular bug since there's a similar "hdev->dev_flags = 0;" inside hci_cc_reset() in hci_event.c.

so I rather do the change to dev_flags first.

Regards

Marcel

#5 lore raw

Re: [PATCH 1/9] Bluetooth: Fix persistency of the HCI_MGMT flag

Hi Marcel,

Quoted 18 lines · Marcel Holtmann
Quoted 18 lines
Quoted 17 lines
Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
index 6d38d80..52edc11 100644

net/bluetooth/hci_core.c static int hci_dev_do_close(struct hci_dev *hdev) · line 677

Quoted 10 lines
net/bluetooth/hci_core.c +2 -2
@@ -670,8 +670,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	mgmt_powered(hdev, 0);
 	hci_dev_unlock(hdev);
 
-	/* Clear flags */
-	hdev->flags = 0;
+	/* Clear flags, except persistent ones like HCI_MGMT */
+	hdev->flags &= BIT(HCI_MGMT);
 
 	hci_req_unlock(hdev);
 

can we just move this away from hdev->flags into a hdev->dev_flags.

Quoted 6 lines
Quoted 4 lines

Yes, that should be done (and not just for HCI_MGMT but also for other related flags) but it should imo be in a separate patch. Simply moving to dev_flags won't fix this particular bug since there's a similar "hdev->dev_flags = 0;" inside hci_cc_reset() in hci_event.c.

so I rather do the change to dev_flags first.

Alright, I'll do it that way around. Meanwhile I've applied the other acked patches to my bluetooth-next tree.

Johan

#6 lore raw

PATCH 2/9 Bluetooth: Convert inquiry cache to use standard list types

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This makes it possible to use the convenience functions provided for standard kernel list types and it also makes it easier to extend the use of the cache for the management interface where e.g. name resolving control will be needed.

---
 include/net/bluetooth/hci_core.h |   10 ++++------
 net/bluetooth/hci_core.c         |   31 +++++++++++++++++--------------
 net/bluetooth/hci_sysfs.c        |    2 +-
 3 files changed, 22 insertions(+), 21 deletions(-)
include/net/bluetooth/hci_core.h +4 -6
index ea9231f..91d1baf 100644
@@ -44,14 +44,14 @@ struct inquiry_data {
 };
 
 struct inquiry_entry {
-	struct inquiry_entry	*next;
+	struct list_head	list;
 	__u32			timestamp;
 	struct inquiry_data	data;
 };
 
 struct inquiry_cache {
+	struct list_head	list;
 	__u32			timestamp;
-	struct inquiry_entry	*list;
 };
 
 struct hci_conn_hash {
@@ -350,14 +350,12 @@ extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
 
 static inline void inquiry_cache_init(struct hci_dev *hdev)
 {
-	struct inquiry_cache *c = &hdev->inq_cache;
-	c->list = NULL;
+	INIT_LIST_HEAD(&hdev->inq_cache.list);
 }
 
 static inline int inquiry_cache_empty(struct hci_dev *hdev)
 {
-	struct inquiry_cache *c = &hdev->inq_cache;
-	return c->list == NULL;
+	return list_empty(&hdev->inq_cache.list);
 }
 
 static inline long inquiry_cache_age(struct hci_dev *hdev)
net/bluetooth/hci_core.c +17 -14
index 52edc11..39214e1 100644
@@ -357,15 +357,11 @@ struct hci_dev *hci_dev_get(int index)
 /* ---- Inquiry support ---- */
 static void inquiry_cache_flush(struct hci_dev *hdev)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
-	struct inquiry_entry *next  = cache->list, *e;
-
-	BT_DBG("cache %p", cache);
+	struct inquiry_entry *p, *n;
 
-	cache->list = NULL;
-	while ((e = next)) {
-		next = e->next;
-		kfree(e);
+	list_for_each_entry_safe(p, n, &hdev->inq_cache.list, list) {
+		list_del(&p->list);
+		kfree(p);
 	}
 }
 
@@ -376,10 +372,12 @@ struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *b
 
 	BT_DBG("cache %p, %s", cache, batostr(bdaddr));
 
-	for (e = cache->list; e; e = e->next)
+	list_for_each_entry(e, &cache->list, list) {
 		if (!bacmp(&e->data.bdaddr, bdaddr))
-			break;
-	return e;
+			return e;
+	}
+
+	return NULL;
 }
 
 void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data)
@@ -396,8 +394,7 @@ void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data)
 		if (!ie)
 			return;
 
-		ie->next = cache->list;
-		cache->list = ie;
+		list_add(&ie->list, &cache->list);
 	}
 
 	memcpy(&ie->data, data, sizeof(*data));
@@ -412,15 +409,21 @@ static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
 	struct inquiry_entry *e;
 	int copied = 0;
 
-	for (e = cache->list; e && copied < num; e = e->next, copied++) {
+	list_for_each_entry(e, &cache->list, list) {
 		struct inquiry_data *data = &e->data;
+
+		if (copied >= num)
+			break;
+
 		bacpy(&info->bdaddr, &data->bdaddr);
 		info->pscan_rep_mode	= data->pscan_rep_mode;
 		info->pscan_period_mode	= data->pscan_period_mode;
 		info->pscan_mode	= data->pscan_mode;
 		memcpy(info->dev_class, data->dev_class, 3);
 		info->clock_offset	= data->clock_offset;
+
 		info++;
+		copied++;
 	}
 
 	BT_DBG("cache %p, copied %d", cache, copied);
net/bluetooth/hci_sysfs.c +1 -1
index 5210956..ed9ccee 100644
@@ -388,7 +388,7 @@ static int inquiry_cache_show(struct seq_file *f, void *p)
 
 	hci_dev_lock(hdev);
 
-	for (e = cache->list; e; e = e->next) {
+	list_for_each_entry(e, &cache->list, list) {
 		struct inquiry_data *data = &e->data;
 		seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
 			   batostr(&data->bdaddr),
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 2/9
#7 lore raw

Re: [PATCH 2/9] Bluetooth: Convert inquiry cache to use standard list types

Hi Johan,

Quoted 11 lines

This makes it possible to use the convenience functions provided for standard kernel list types and it also makes it easier to extend the use of the cache for the management interface where e.g. name resolving control will be needed.

---
 include/net/bluetooth/hci_core.h |   10 ++++------
 net/bluetooth/hci_core.c         |   31 +++++++++++++++++--------------
 net/bluetooth/hci_sysfs.c        |    2 +-
 3 files changed, 22 insertions(+), 21 deletions(-)

Regards

Marcel

#8 lore raw

PATCH 3/9 Bluetooth: Move Extended Inquiry Response defines to hci.h

In reply to [email protected]

From: Johan Hedberg <[email protected]>

The EIR defines are needed also outside of mgmt.c (e.g. in hci_event.c to check if EIR data has the complete name) so it's better to have them in a single public place, i.e. hci.h.

---
 include/net/bluetooth/hci.h |   13 +++++++++++++
 net/bluetooth/mgmt.c        |   12 ------------
 2 files changed, 13 insertions(+), 12 deletions(-)
include/net/bluetooth/hci.h +13 -0
index 5b2fed5..ce5133b 100644
@@ -284,6 +284,19 @@ enum {
 #define HCI_FLOW_CTL_MODE_PACKET_BASED	0x00
 #define HCI_FLOW_CTL_MODE_BLOCK_BASED	0x01
 
+/* Extended Inquiry Response field types */
+#define EIR_FLAGS		0x01 /* flags */
+#define EIR_UUID16_SOME		0x02 /* 16-bit UUID, more available */
+#define EIR_UUID16_ALL		0x03 /* 16-bit UUID, all listed */
+#define EIR_UUID32_SOME		0x04 /* 32-bit UUID, more available */
+#define EIR_UUID32_ALL		0x05 /* 32-bit UUID, all listed */
+#define EIR_UUID128_SOME	0x06 /* 128-bit UUID, more available */
+#define EIR_UUID128_ALL		0x07 /* 128-bit UUID, all listed */
+#define EIR_NAME_SHORT		0x08 /* shortened local name */
+#define EIR_NAME_COMPLETE	0x09 /* complete local name */
+#define EIR_TX_POWER		0x0A /* transmit power level */
+#define EIR_DEVICE_ID		0x10 /* device ID */
+
 /* -----  HCI Commands ---- */
 #define HCI_OP_NOP			0x0000
 
net/bluetooth/mgmt.c +0 -12
index bc8e59d..851cb19 100644
@@ -303,18 +303,6 @@ static u32 get_current_settings(struct hci_dev *hdev)
 	return settings;
 }
 
-#define EIR_FLAGS		0x01 /* flags */
-#define EIR_UUID16_SOME		0x02 /* 16-bit UUID, more available */
-#define EIR_UUID16_ALL		0x03 /* 16-bit UUID, all listed */
-#define EIR_UUID32_SOME		0x04 /* 32-bit UUID, more available */
-#define EIR_UUID32_ALL		0x05 /* 32-bit UUID, all listed */
-#define EIR_UUID128_SOME	0x06 /* 128-bit UUID, more available */
-#define EIR_UUID128_ALL		0x07 /* 128-bit UUID, all listed */
-#define EIR_NAME_SHORT		0x08 /* shortened local name */
-#define EIR_NAME_COMPLETE	0x09 /* complete local name */
-#define EIR_TX_POWER		0x0A /* transmit power level */
-#define EIR_DEVICE_ID		0x10 /* device ID */
-
 #define PNP_INFO_SVCLASS_ID		0x1200
 
 static u8 bluetooth_base_uuid[] = {
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 3/9
#9 lore raw

Re: [PATCH 3/9] Bluetooth: Move Extended Inquiry Response defines to hci.h

Hi Johan,

Quoted 9 lines

The EIR defines are needed also outside of mgmt.c (e.g. in hci_event.c to check if EIR data has the complete name) so it's better to have them in a single public place, i.e. hci.h.

---
 include/net/bluetooth/hci.h |   13 +++++++++++++
 net/bluetooth/mgmt.c        |   12 ------------
 2 files changed, 13 insertions(+), 12 deletions(-)

Regards

Marcel

#10 lore raw

PATCH 4/9 Bluetooth: Add initial mgmt_confirm_name support

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds initial support for mgmt_confirm_name. It adds the necessary tracking of the name state by extending the inquiry cache. The actual name resolving operation (to be done once inquiry is finished) is not yet part of this patch.

---
 include/net/bluetooth/hci_core.h |   29 ++++++++++++++----
 net/bluetooth/hci_core.c         |   58 ++++++++++++++++++++++++++++++-------
 net/bluetooth/hci_event.c        |   51 ++++++++++++++++++++++++++++-----
 net/bluetooth/hci_sysfs.c        |    2 +-
 net/bluetooth/mgmt.c             |   51 ++++++++++++++++++++++++++++++++-
 5 files changed, 163 insertions(+), 28 deletions(-)
include/net/bluetooth/hci_core.h +22 -7
index 91d1baf..2999b6e 100644
@@ -44,14 +44,23 @@ struct inquiry_data {
 };
 
 struct inquiry_entry {
-	struct list_head	list;
+	struct list_head	all;		/* inq_cache.all */
+	struct list_head	list;		/* unknown or resolve */
+	enum {
+		NAME_NOT_KNOWN,
+		NAME_NEEDED,
+		NAME_PENDING,
+		NAME_KNOWN,
+	} name_state;
 	__u32			timestamp;
 	struct inquiry_data	data;
 };
 
 struct inquiry_cache {
-	struct list_head	list;
-	__u32			timestamp;
+	struct list_head all;		/* All devices found during inquiry */
+	struct list_head unknown;	/* Name state not known */
+	struct list_head resolve;	/* Name needs to be resolved */
+	__u32		timestamp;
 };
 
 struct hci_conn_hash {
@@ -350,12 +359,14 @@ extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
 
 static inline void inquiry_cache_init(struct hci_dev *hdev)
 {
-	INIT_LIST_HEAD(&hdev->inq_cache.list);
+	INIT_LIST_HEAD(&hdev->inq_cache.all);
+	INIT_LIST_HEAD(&hdev->inq_cache.unknown);
+	INIT_LIST_HEAD(&hdev->inq_cache.resolve);
 }
 
 static inline int inquiry_cache_empty(struct hci_dev *hdev)
 {
-	return list_empty(&hdev->inq_cache.list);
+	return list_empty(&hdev->inq_cache.all);
 }
 
 static inline long inquiry_cache_age(struct hci_dev *hdev)
@@ -371,7 +382,10 @@ static inline long inquiry_entry_age(struct inquiry_entry *e)
 
 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
 							bdaddr_t *bdaddr);
-void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data);
+struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
+							bdaddr_t *bdaddr);
+void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
+							bool name_known);
 
 /* ----- HCI Connections ----- */
 enum {
@@ -913,7 +927,8 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
 int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
 						u8 *randomizer, u8 status);
 int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
-				u8 addr_type, u8 *dev_class, s8 rssi, u8 *eir);
+					u8 addr_type, u8 *dev_class, s8 rssi,
+					u8 cfm_name, u8 *eir);
 int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *name);
 int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status);
 int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status);
net/bluetooth/hci_core.c +47 -11

93 lines not on this page · raw message

net/bluetooth/hci_event.c +43 -8

93 lines not on this page · raw message

net/bluetooth/hci_sysfs.c +1 -1
index ed9ccee..3600d78 100644
@@ -388,7 +388,7 @@ static int inquiry_cache_show(struct seq_file *f, void *p)
 
 	hci_dev_lock(hdev);
 
-	list_for_each_entry(e, &cache->list, list) {
+	list_for_each_entry(e, &cache->all, all) {
 		struct inquiry_data *data = &e->data;
 		seq_printf(f, "%s %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
 			   batostr(&data->bdaddr),
net/bluetooth/mgmt.c +50 -1

75 lines not on this page · raw message

Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 4/9
#11 lore raw

Re: [PATCH 4/9] Bluetooth: Add initial mgmt_confirm_name support

Hi Johan,

Quoted 5 lines

This patch adds initial support for mgmt_confirm_name. It adds the necessary tracking of the name state by extending the inquiry cache. The actual name resolving operation (to be done once inquiry is finished) is not yet part of this patch.

Quoted 8 lines
---
 include/net/bluetooth/hci_core.h |   29 ++++++++++++++----
 net/bluetooth/hci_core.c         |   58 ++++++++++++++++++++++++++++++-------
 net/bluetooth/hci_event.c        |   51 ++++++++++++++++++++++++++++-----
 net/bluetooth/hci_sysfs.c        |    2 +-
 net/bluetooth/mgmt.c             |   51 ++++++++++++++++++++++++++++++++-
 5 files changed, 163 insertions(+), 28 deletions(-)

Regards

Marcel

#12 lore raw

PATCH 5/9 Bluetooth: Return updated name state with hci_inquiry_cache_update

In reply to [email protected]

From: Johan Hedberg <[email protected]>

If user-space has already confirmed the name for a remote device we shouldn't request confirmation again. The simplest way to do this is to return the name state from hci_inquiry_cache_update (if it is anything else than unknown then we do not need confirmation from user-space).

---
 include/net/bluetooth/hci_core.h |    2 +-
 net/bluetooth/hci_core.c         |    9 +++++++--
 net/bluetooth/hci_event.c        |   21 ++++++++++++++-------
 3 files changed, 22 insertions(+), 10 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
index 2999b6e..236f7f0 100644
@@ -384,7 +384,7 @@ struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
 							bdaddr_t *bdaddr);
 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 							bdaddr_t *bdaddr);
-void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
+bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 							bool name_known);
 
 /* ----- HCI Connections ----- */
net/bluetooth/hci_core.c +7 -2
index 9e36fb1..67b0bd6 100644
@@ -400,7 +400,7 @@ struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 	return NULL;
 }
 
-void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
+bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 							bool name_known)
 {
 	struct inquiry_cache *cache = &hdev->inq_cache;
@@ -415,7 +415,7 @@ void hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 	/* Entry not in the cache. Add new one. */
 	ie = kzalloc(sizeof(struct inquiry_entry), GFP_ATOMIC);
 	if (!ie)
-		return;
+		return false;
 
 	list_add(&ie->all, &cache->all);
 
@@ -436,6 +436,11 @@ update:
 	memcpy(&ie->data, data, sizeof(*data));
 	ie->timestamp = jiffies;
 	cache->timestamp = jiffies;
+
+	if (ie->name_state == NAME_NOT_KNOWN)
+		return false;
+
+	return true;
 }
 
 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
net/bluetooth/hci_event.c +14 -7
index abcdadf..828ff14 100644
@@ -1525,6 +1525,8 @@ static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *
 	hci_dev_lock(hdev);
 
 	for (; num_rsp; num_rsp--, info++) {
+		bool name_known;
+
 		bacpy(&data.bdaddr, &info->bdaddr);
 		data.pscan_rep_mode	= info->pscan_rep_mode;
 		data.pscan_period_mode	= info->pscan_period_mode;
@@ -1533,9 +1535,10 @@ static inline void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *
 		data.clock_offset	= info->clock_offset;
 		data.rssi		= 0x00;
 		data.ssp_mode		= 0x00;
-		hci_inquiry_cache_update(hdev, &data, false);
+
+		name_known = hci_inquiry_cache_update(hdev, &data, false);
 		mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
-						info->dev_class, 0, 1, NULL);
+					info->dev_class, 0, !name_known, NULL);
 	}
 
 	hci_dev_unlock(hdev);
@@ -2551,6 +2554,7 @@ static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct
 {
 	struct inquiry_data data;
 	int num_rsp = *((__u8 *) skb->data);
+	bool name_known;
 
 	BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
 
@@ -2572,10 +2576,12 @@ static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct
 			data.clock_offset	= info->clock_offset;
 			data.rssi		= info->rssi;
 			data.ssp_mode		= 0x00;
-			hci_inquiry_cache_update(hdev, &data, false);
+
+			name_known = hci_inquiry_cache_update(hdev, &data,
+								false);
 			mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
 						info->dev_class, info->rssi,
-						1, NULL);
+						!name_known, NULL);
 		}
 	} else {
 		struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
@@ -2589,10 +2595,11 @@ static inline void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, struct
 			data.clock_offset	= info->clock_offset;
 			data.rssi		= info->rssi;
 			data.ssp_mode		= 0x00;
-			hci_inquiry_cache_update(hdev, &data, false);
+			name_known = hci_inquiry_cache_update(hdev, &data,
+								false);
 			mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
 						info->dev_class, info->rssi,
-						1, NULL);
+						!name_known, NULL);
 		}
 	}
 
@@ -2766,7 +2773,7 @@ static inline void hci_extended_inquiry_result_evt(struct hci_dev *hdev, struct
 		else
 			name_known = true;
 
-		hci_inquiry_cache_update(hdev, &data, name_known);
+		name_known = hci_inquiry_cache_update(hdev, &data, name_known);
 		mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
 						info->dev_class, info->rssi,
 						!name_known, info->data);
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 5/9
#13 lore raw

Re: [PATCH 5/9] Bluetooth: Return updated name state with hci_inquiry_cache_update

Hi Johan,

Quoted 11 lines

If user-space has already confirmed the name for a remote device we shouldn't request confirmation again. The simplest way to do this is to return the name state from hci_inquiry_cache_update (if it is anything else than unknown then we do not need confirmation from user-space).

---
 include/net/bluetooth/hci_core.h |    2 +-
 net/bluetooth/hci_core.c         |    9 +++++++--
 net/bluetooth/hci_event.c        |   21 ++++++++++++++-------
 3 files changed, 22 insertions(+), 10 deletions(-)

Regards

Marcel

#14 lore raw

PATCH 6/9 Bluetooth: Flush inquiry cache when starting mgmt triggered inquiry

In reply to [email protected]

From: Johan Hedberg <[email protected]>

For the remote name state tracking for the management interface to work the cache needs to be flushed whenever inquiry is started. The hci_do_inquiry function is only used by the management interface so by having the flushing done from it ensures that old ioctl based functionality isn't affected.

---
 net/bluetooth/hci_core.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
net/bluetooth/hci_core.c +2 -0
index 67b0bd6..5c0a1e7 100644
@@ -2611,6 +2611,8 @@ int hci_do_inquiry(struct hci_dev *hdev, u8 length)
 	if (test_bit(HCI_INQUIRY, &hdev->flags))
 		return -EINPROGRESS;
 
+	inquiry_cache_flush(hdev);
+
 	memset(&cp, 0, sizeof(cp));
 	memcpy(&cp.lap, lap, sizeof(cp.lap));
 	cp.length  = length;
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 6/9
#15 lore raw

Re: [PATCH 6/9] Bluetooth: Flush inquiry cache when starting mgmt triggered inquiry

Hi Johan,

Quoted 10 lines

For the remote name state tracking for the management interface to work the cache needs to be flushed whenever inquiry is started. The hci_do_inquiry function is only used by the management interface so by having the flushing done from it ensures that old ioctl based functionality isn't affected.

---
 net/bluetooth/hci_core.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

Regards

Marcel

#16 lore raw

PATCH 7/9 Bluetooth: Rename hdev->inq_cache to hdev->discovery

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This struct is used for not just inquiry caching but also for general device discovery state tracking so it's better to rename it to something more appropriate.

---
 include/net/bluetooth/hci_core.h |   16 ++++++++--------
 net/bluetooth/hci_core.c         |   12 ++++++------
 net/bluetooth/hci_sysfs.c        |    2 +-
 net/bluetooth/mgmt.c             |    2 +-
 4 files changed, 16 insertions(+), 16 deletions(-)
include/net/bluetooth/hci_core.h +8 -8
index 236f7f0..5a566fd 100644
@@ -56,7 +56,7 @@ struct inquiry_entry {
 	struct inquiry_data	data;
 };
 
-struct inquiry_cache {
+struct discovery_state {
 	struct list_head all;		/* All devices found during inquiry */
 	struct list_head unknown;	/* Name state not known */
 	struct list_head resolve;	/* Name needs to be resolved */
@@ -226,7 +226,7 @@ struct hci_dev {
 
 	struct list_head	mgmt_pending;
 
-	struct inquiry_cache	inq_cache;
+	struct discovery_state	discovery;
 	struct hci_conn_hash	conn_hash;
 	struct list_head	blacklist;
 
@@ -357,21 +357,21 @@ extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
 
-static inline void inquiry_cache_init(struct hci_dev *hdev)
+static inline void discovery_init(struct hci_dev *hdev)
 {
-	INIT_LIST_HEAD(&hdev->inq_cache.all);
-	INIT_LIST_HEAD(&hdev->inq_cache.unknown);
-	INIT_LIST_HEAD(&hdev->inq_cache.resolve);
+	INIT_LIST_HEAD(&hdev->discovery.all);
+	INIT_LIST_HEAD(&hdev->discovery.unknown);
+	INIT_LIST_HEAD(&hdev->discovery.resolve);
 }
 
 static inline int inquiry_cache_empty(struct hci_dev *hdev)
 {
-	return list_empty(&hdev->inq_cache.all);
+	return list_empty(&hdev->discovery.all);
 }
 
 static inline long inquiry_cache_age(struct hci_dev *hdev)
 {
-	struct inquiry_cache *c = &hdev->inq_cache;
+	struct discovery_state *c = &hdev->discovery;
 	return jiffies - c->timestamp;
 }
 
net/bluetooth/hci_core.c +6 -6
index 5c0a1e7..82829c1 100644
@@ -357,7 +357,7 @@ struct hci_dev *hci_dev_get(int index)
 /* ---- Inquiry support ---- */
 static void inquiry_cache_flush(struct hci_dev *hdev)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_entry *p, *n;
 
 	list_for_each_entry_safe(p, n, &cache->all, all) {
@@ -371,7 +371,7 @@ static void inquiry_cache_flush(struct hci_dev *hdev)
 
 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_entry *e;
 
 	BT_DBG("cache %p, %s", cache, batostr(bdaddr));
@@ -387,7 +387,7 @@ struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *b
 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 							bdaddr_t *bdaddr)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_entry *e;
 
 	BT_DBG("cache %p, %s", cache, batostr(bdaddr));
@@ -403,7 +403,7 @@ struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 							bool name_known)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_entry *ie;
 
 	BT_DBG("cache %p, %s", cache, batostr(&data->bdaddr));
@@ -445,7 +445,7 @@ update:
 
 static int inquiry_cache_dump(struct hci_dev *hdev, int num, __u8 *buf)
 {
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_info *info = (struct inquiry_info *) buf;
 	struct inquiry_entry *e;
 	int copied = 0;
@@ -1546,7 +1546,7 @@ int hci_register_dev(struct hci_dev *hdev)
 	init_waitqueue_head(&hdev->req_wait_q);
 	mutex_init(&hdev->req_lock);
 
-	inquiry_cache_init(hdev);
+	discovery_init(hdev);
 
 	hci_conn_hash_init(hdev);
 
net/bluetooth/hci_sysfs.c +1 -1
index 3600d78..74b49e3 100644
@@ -383,7 +383,7 @@ static struct device_type bt_host = {
 static int inquiry_cache_show(struct seq_file *f, void *p)
 {
 	struct hci_dev *hdev = f->private;
-	struct inquiry_cache *cache = &hdev->inq_cache;
+	struct discovery_state *cache = &hdev->discovery;
 	struct inquiry_entry *e;
 
 	hci_dev_lock(hdev);
net/bluetooth/mgmt.c +1 -1
index 3977511..894f11b 100644
@@ -2000,7 +2000,7 @@ static int confirm_name(struct sock *sk, u16 index, unsigned char *data,
 		list_del(&e->list);
 	} else {
 		e->name_state = NAME_NEEDED;
-		list_move(&e->list, &hdev->inq_cache.resolve);
+		list_move(&e->list, &hdev->discovery.resolve);
 	}
 
 	err = 0;
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 7/9
#17 lore raw

Re: [PATCH 7/9] Bluetooth: Rename hdev->inq_cache to hdev->discovery

Hi Johan,

Quoted 11 lines

This struct is used for not just inquiry caching but also for general device discovery state tracking so it's better to rename it to something more appropriate.

---
 include/net/bluetooth/hci_core.h |   16 ++++++++--------
 net/bluetooth/hci_core.c         |   12 ++++++------
 net/bluetooth/hci_sysfs.c        |    2 +-
 net/bluetooth/mgmt.c             |    2 +-
 4 files changed, 16 insertions(+), 16 deletions(-)

Regards

Marcel

#18 lore raw

PATCH 8/9 Bluetooth: Add discovery state tracking

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds proper state tracking to the device discovery process. This makes it possible to return appropriate errors when trying to stop a non-active discovery or start discovery when it is already ongoing. Once name resolving is implemented this also makes it possible to know what the right action to do is when a remote name lookup is cancelled.

---
 include/net/bluetooth/hci_core.h |    9 +++++++++
 net/bluetooth/hci_core.c         |   25 +++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |    6 +++---
 net/bluetooth/mgmt.c             |   16 ++++++++++++++++
 4 files changed, 53 insertions(+), 3 deletions(-)
include/net/bluetooth/hci_core.h +9 -0
index 5a566fd..2f19de4 100644
@@ -57,6 +57,12 @@ struct inquiry_entry {
 };
 
 struct discovery_state {
+	enum {
+		DISCOVERY_STOPPED,
+		DISCOVERY_STARTING,
+		DISCOVERY_ACTIVE,
+		DISCOVERY_STOPPING,
+	} state;
 	struct list_head all;		/* All devices found during inquiry */
 	struct list_head unknown;	/* Name state not known */
 	struct list_head resolve;	/* Name needs to be resolved */
@@ -359,11 +365,14 @@ extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
 
 static inline void discovery_init(struct hci_dev *hdev)
 {
+	hdev->discovery.state = DISCOVERY_STOPPED;
 	INIT_LIST_HEAD(&hdev->discovery.all);
 	INIT_LIST_HEAD(&hdev->discovery.unknown);
 	INIT_LIST_HEAD(&hdev->discovery.resolve);
 }
 
+void hci_discovery_set_state(struct hci_dev *hdev, int state);
+
 static inline int inquiry_cache_empty(struct hci_dev *hdev)
 {
 	return list_empty(&hdev->discovery.all);
net/bluetooth/hci_core.c +25 -0
index 82829c1..f3aaa3c 100644
@@ -355,6 +355,30 @@ struct hci_dev *hci_dev_get(int index)
 }
 
 /* ---- Inquiry support ---- */
+
+void hci_discovery_set_state(struct hci_dev *hdev, int state)
+{
+	BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
+
+	if (hdev->discovery.state == state)
+		return;
+
+	switch (state) {
+	case DISCOVERY_STOPPED:
+		mgmt_discovering(hdev, 0);
+		break;
+	case DISCOVERY_STARTING:
+		break;
+	case DISCOVERY_ACTIVE:
+		mgmt_discovering(hdev, 1);
+		break;
+	case DISCOVERY_STOPPING:
+		break;
+	}
+
+	hdev->discovery.state = state;
+}
+
 static void inquiry_cache_flush(struct hci_dev *hdev)
 {
 	struct discovery_state *cache = &hdev->discovery;
@@ -367,6 +391,7 @@ static void inquiry_cache_flush(struct hci_dev *hdev)
 
 	INIT_LIST_HEAD(&cache->unknown);
 	INIT_LIST_HEAD(&cache->resolve);
+	cache->state = DISCOVERY_STOPPED;
 }
 
 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr)
net/bluetooth/hci_event.c +3 -3
index 828ff14..6213b5f 100644
@@ -65,7 +65,7 @@ static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
 	clear_bit(HCI_INQUIRY, &hdev->flags);
 
 	hci_dev_lock(hdev);
-	mgmt_discovering(hdev, 0);
+	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 	hci_dev_unlock(hdev);
 
 	hci_req_complete(hdev, HCI_OP_INQUIRY_CANCEL, status);
@@ -1119,7 +1119,7 @@ static inline void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
 	set_bit(HCI_INQUIRY, &hdev->flags);
 
 	hci_dev_lock(hdev);
-	mgmt_discovering(hdev, 1);
+	hci_discovery_set_state(hdev, DISCOVERY_ACTIVE);
 	hci_dev_unlock(hdev);
 }
 
@@ -1507,7 +1507,7 @@ static inline void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff
 		return;
 
 	hci_dev_lock(hdev);
-	mgmt_discovering(hdev, 0);
+	hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
 	hci_dev_unlock(hdev);
 }
 
net/bluetooth/mgmt.c +16 -0
index 894f11b..590966d 100644
@@ -1918,6 +1918,12 @@ static int start_discovery(struct sock *sk, u16 index,
 		goto failed;
 	}
 
+	if (hdev->discovery.state != DISCOVERY_STOPPED) {
+		err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY,
+						MGMT_STATUS_BUSY);
+		goto failed;
+	}
+
 	cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0);
 	if (!cmd) {
 		err = -ENOMEM;
@@ -1927,6 +1933,8 @@ static int start_discovery(struct sock *sk, u16 index,
 	err = hci_do_inquiry(hdev, INQUIRY_LEN_BREDR);
 	if (err < 0)
 		mgmt_pending_remove(cmd);
+	else
+		hci_discovery_set_state(hdev, DISCOVERY_STARTING);
 
 failed:
 	hci_dev_unlock(hdev);
@@ -1950,6 +1958,12 @@ static int stop_discovery(struct sock *sk, u16 index)
 
 	hci_dev_lock(hdev);
 
+	if (hdev->discovery.state != DISCOVERY_ACTIVE) {
+		err = cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY,
+						MGMT_STATUS_REJECTED);
+		goto failed;
+	}
+
 	cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
 	if (!cmd) {
 		err = -ENOMEM;
@@ -1959,6 +1973,8 @@ static int stop_discovery(struct sock *sk, u16 index)
 	err = hci_cancel_inquiry(hdev);
 	if (err < 0)
 		mgmt_pending_remove(cmd);
+	else
+		hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
 
 failed:
 	hci_dev_unlock(hdev);
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 8/9
#19 lore raw

Re: [PATCH 8/9] Bluetooth: Add discovery state tracking

Hi Johan,

Quoted 6 lines

This patch adds proper state tracking to the device discovery process. This makes it possible to return appropriate errors when trying to stop a non-active discovery or start discovery when it is already ongoing. Once name resolving is implemented this also makes it possible to know what the right action to do is when a remote name lookup is cancelled.

Quoted 7 lines
---
 include/net/bluetooth/hci_core.h |    9 +++++++++
 net/bluetooth/hci_core.c         |   25 +++++++++++++++++++++++++
 net/bluetooth/hci_event.c        |    6 +++---
 net/bluetooth/mgmt.c             |   16 ++++++++++++++++
 4 files changed, 53 insertions(+), 3 deletions(-)

Regards

Marcel

#20 lore raw

PATCH 9/9 Bluetooth: Add name resolving support for mgmt based discovery

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch adds the necessary logic to perform name lookups after inquiry completes. This is done by checking for entries in the resolve list after each inquiry complete and remote name complete HCI event.

---
 include/net/bluetooth/hci_core.h |    8 +++-
 net/bluetooth/hci_core.c         |   34 +++++++++++++++-
 net/bluetooth/hci_event.c        |   81 ++++++++++++++++++++++++++++++++++++--
 net/bluetooth/mgmt.c             |   37 +++++++++++++++--
 4 files changed, 149 insertions(+), 11 deletions(-)
include/net/bluetooth/hci_core.h +7 -1
index 2f19de4..a8680da 100644
@@ -60,7 +60,8 @@ struct discovery_state {
 	enum {
 		DISCOVERY_STOPPED,
 		DISCOVERY_STARTING,
-		DISCOVERY_ACTIVE,
+		DISCOVERY_INQUIRY,
+		DISCOVERY_RESOLVING,
 		DISCOVERY_STOPPING,
 	} state;
 	struct list_head all;		/* All devices found during inquiry */
@@ -371,6 +372,8 @@ static inline void discovery_init(struct hci_dev *hdev)
 	INIT_LIST_HEAD(&hdev->discovery.resolve);
 }
 
+bool hci_discovery_active(struct hci_dev *hdev);
+
 void hci_discovery_set_state(struct hci_dev *hdev, int state);
 
 static inline int inquiry_cache_empty(struct hci_dev *hdev)
@@ -393,6 +396,9 @@ struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
 							bdaddr_t *bdaddr);
 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 							bdaddr_t *bdaddr);
+struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
+							bdaddr_t *bdaddr,
+							int state);
 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 							bool name_known);
 
net/bluetooth/hci_core.c +33 -1
index f3aaa3c..803655f 100644
@@ -356,6 +356,17 @@ struct hci_dev *hci_dev_get(int index)
 
 /* ---- Inquiry support ---- */
 
+bool hci_discovery_active(struct hci_dev *hdev)
+{
+	struct discovery_state *discov = &hdev->discovery;
+
+	if (discov->state == DISCOVERY_INQUIRY ||
+					discov->state == DISCOVERY_RESOLVING)
+		return true;
+
+	return false;
+}
+
 void hci_discovery_set_state(struct hci_dev *hdev, int state)
 {
 	BT_DBG("%s state %u -> %u", hdev->name, hdev->discovery.state, state);
@@ -369,9 +380,11 @@ void hci_discovery_set_state(struct hci_dev *hdev, int state)
 		break;
 	case DISCOVERY_STARTING:
 		break;
-	case DISCOVERY_ACTIVE:
+	case DISCOVERY_INQUIRY:
 		mgmt_discovering(hdev, 1);
 		break;
+	case DISCOVERY_RESOLVING:
+		break;
 	case DISCOVERY_STOPPING:
 		break;
 	}
@@ -425,6 +438,25 @@ struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
 	return NULL;
 }
 
+struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
+							bdaddr_t *bdaddr,
+							int state)
+{
+	struct discovery_state *cache = &hdev->discovery;
+	struct inquiry_entry *e;
+
+	BT_DBG("cache %p bdaddr %s state %d", cache, batostr(bdaddr), state);
+
+	list_for_each_entry(e, &cache->resolve, list) {
+		if (!bacmp(bdaddr, BDADDR_ANY) && e->name_state == state)
+			return e;
+		if (!bacmp(&e->data.bdaddr, bdaddr))
+			return e;
+	}
+
+	return NULL;
+}
+
 bool hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
 							bool name_known)
 {
net/bluetooth/hci_event.c +77 -4

118 lines not on this page · raw message

net/bluetooth/mgmt.c +32 -5
index 590966d..295cfc8 100644
@@ -1947,6 +1947,8 @@ static int stop_discovery(struct sock *sk, u16 index)
 {
 	struct hci_dev *hdev;
 	struct pending_cmd *cmd;
+	struct hci_cp_remote_name_req_cancel cp;
+	struct inquiry_entry *e;
 	int err;
 
 	BT_DBG("hci%u", index);
@@ -1958,25 +1960,44 @@ static int stop_discovery(struct sock *sk, u16 index)
 
 	hci_dev_lock(hdev);
 
-	if (hdev->discovery.state != DISCOVERY_ACTIVE) {
+	if (!hci_discovery_active(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_STOP_DISCOVERY,
 						MGMT_STATUS_REJECTED);
-		goto failed;
+		goto unlock;
 	}
 
 	cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0);
 	if (!cmd) {
 		err = -ENOMEM;
-		goto failed;
+		goto unlock;
+	}
+
+	if (hdev->discovery.state == DISCOVERY_INQUIRY) {
+		err = hci_cancel_inquiry(hdev);
+		if (err < 0)
+			mgmt_pending_remove(cmd);
+		else
+			hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
+		goto unlock;
+	}
+
+	e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_PENDING);
+	if (!e) {
+		mgmt_pending_remove(cmd);
+		err = cmd_complete(sk, index, MGMT_OP_STOP_DISCOVERY, NULL, 0);
+		hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
+		goto unlock;
 	}
 
-	err = hci_cancel_inquiry(hdev);
+	bacpy(&cp.bdaddr, &e->data.bdaddr);
+	err = hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ_CANCEL,
+							sizeof(cp), &cp);
 	if (err < 0)
 		mgmt_pending_remove(cmd);
 	else
 		hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
 
-failed:
+unlock:
 	hci_dev_unlock(hdev);
 	hci_dev_put(hdev);
 
@@ -2004,6 +2025,12 @@ static int confirm_name(struct sock *sk, u16 index, unsigned char *data,
 
 	hci_dev_lock(hdev);
 
+	if (!hci_discovery_active(hdev)) {
+		err = cmd_status(sk, index, MGMT_OP_CONFIRM_NAME,
+							MGMT_STATUS_FAILED);
+		goto failed;
+	}
+
 	e = hci_inquiry_cache_lookup_unknown(hdev, &cp->bdaddr);
 	if (!e) {
 		err = cmd_status (sk, index, MGMT_OP_CONFIRM_NAME,
Signature · 1 line
1.7.7.3
maintainer Acked-by on v1 9/9
#21 lore raw

Re: [PATCH 9/9] Bluetooth: Add name resolving support for mgmt based discovery

Hi Johan,

Quoted 11 lines

This patch adds the necessary logic to perform name lookups after inquiry completes. This is done by checking for entries in the resolve list after each inquiry complete and remote name complete HCI event.

---
 include/net/bluetooth/hci_core.h |    8 +++-
 net/bluetooth/hci_core.c         |   34 +++++++++++++++-
 net/bluetooth/hci_event.c        |   81 ++++++++++++++++++++++++++++++++++++--
 net/bluetooth/mgmt.c             |   37 +++++++++++++++--
 4 files changed, 149 insertions(+), 11 deletions(-)

Regards

Marcel