linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Archie Pusaka <apusaka@google.com>
To: linux-bluetooth <linux-bluetooth@vger.kernel.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Marcel Holtmann <marcel@holtmann.org>
Cc: CrosBT Upstreaming <chromeos-bluetooth-upstreaming@chromium.org>,
	Archie Pusaka <apusaka@chromium.org>,
	Miao-chen Chou <mcchou@chromium.org>
Subject: [Bluez PATCH v4 2/5] Listen and process remote name resolving failure
Date: Thu, 25 Nov 2021 15:06:24 +0800	[thread overview]
Message-ID: <20211125150558.Bluez.v4.2.I9fc087b25433a9347b2d8c8ff7a25fadf448ef49@changeid> (raw)
In-Reply-To: <20211125150558.Bluez.v4.1.I78857808e0b20c6e4dd934b174d3f1106fe3402d@changeid>

From: Archie Pusaka <apusaka@chromium.org>

When Remote Name Resolve ends with failure, record this occurrence and
prevent remote name resolving for the same device for some time.

Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
---

Changes in v4:
* Use CLOCK_MONOTONIC for timekeeping.
* Constant waiting time between retries instead of increasing.
* Fix conflict merge with adv_monitor.c

Changes in v3:
* Rename MGMT const to align with the doc.

Changes in v2:
* Stay silent instead of sending MGMT_OP_CONFIRM_NAME with DONT_CARE flag.

 src/adapter.c     | 16 +++++++++++++---
 src/adapter.h     |  1 +
 src/adv_monitor.c |  7 +++++--
 src/device.c      | 27 +++++++++++++++++++++++++++
 src/device.h      |  2 ++
 5 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 98616f17d2..c49f42cfbb 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -6979,6 +6979,7 @@ void btd_adapter_update_found_device(struct btd_adapter *adapter,
 					uint8_t bdaddr_type, int8_t rssi,
 					bool confirm, bool legacy,
 					bool not_connectable,
+					bool name_resolve_failed,
 					const uint8_t *data, uint8_t data_len,
 					bool monitoring)
 {
@@ -7076,6 +7077,9 @@ void btd_adapter_update_found_device(struct btd_adapter *adapter,
 
 	device_set_legacy(dev, legacy);
 
+	if (name_resolve_failed)
+		device_name_resolve_fail(dev);
+
 	if (adapter->filtered_discovery)
 		device_set_rssi_with_delta(dev, rssi, 0);
 	else
@@ -7146,7 +7150,10 @@ void btd_adapter_update_found_device(struct btd_adapter *adapter,
 	if (g_slist_find(adapter->discovery_found, dev))
 		return;
 
-	if (confirm)
+	/* If name is unknown but it's not allowed to resolve, don't send
+	 * MGMT_OP_CONFIRM_NAME.
+	 */
+	if (confirm && (name_known || device_is_name_resolve_allowed(dev)))
 		confirm_name(adapter, bdaddr, bdaddr_type, name_known);
 
 	adapter->discovery_found = g_slist_prepend(adapter->discovery_found,
@@ -7196,8 +7203,9 @@ static void device_found_callback(uint16_t index, uint16_t length,
 	uint32_t flags;
 	bool confirm_name;
 	bool legacy;
-	char addr[18];
 	bool not_connectable;
+	bool name_resolve_failed;
+	char addr[18];
 
 	if (length < sizeof(*ev)) {
 		btd_error(adapter->dev_id,
@@ -7227,10 +7235,12 @@ static void device_found_callback(uint16_t index, uint16_t length,
 	confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
 	legacy = (flags & MGMT_DEV_FOUND_LEGACY_PAIRING);
 	not_connectable = (flags & MGMT_DEV_FOUND_NOT_CONNECTABLE);
+	name_resolve_failed = (flags & MGMT_DEV_FOUND_NAME_REQUEST_FAILED);
 
 	btd_adapter_update_found_device(adapter, &ev->addr.bdaddr,
 					ev->addr.type, ev->rssi, confirm_name,
-					legacy, not_connectable, eir, eir_len,
+					legacy, not_connectable,
+					name_resolve_failed, eir, eir_len,
 					false);
 }
 
diff --git a/src/adapter.h b/src/adapter.h
index 2815d4613f..35deb1d117 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -92,6 +92,7 @@ void btd_adapter_update_found_device(struct btd_adapter *adapter,
 					uint8_t bdaddr_type, int8_t rssi,
 					bool confirm, bool legacy,
 					bool not_connectable,
+					bool name_resolve_failed,
 					const uint8_t *data, uint8_t data_len,
 					bool monitored);
 
diff --git a/src/adv_monitor.c b/src/adv_monitor.c
index bf7f2bed3b..602830e30b 100644
--- a/src/adv_monitor.c
+++ b/src/adv_monitor.c
@@ -1585,8 +1585,9 @@ static void adv_monitor_device_found_callback(uint16_t index, uint16_t length,
 	uint32_t flags;
 	bool confirm_name;
 	bool legacy;
-	char addr[18];
 	bool not_connectable;
+	bool name_resolve_failed;
+	char addr[18];
 
 	if (length < sizeof(*ev)) {
 		btd_error(adapter_id,
@@ -1613,10 +1614,12 @@ static void adv_monitor_device_found_callback(uint16_t index, uint16_t length,
 	confirm_name = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
 	legacy = (flags & MGMT_DEV_FOUND_LEGACY_PAIRING);
 	not_connectable = (flags & MGMT_DEV_FOUND_NOT_CONNECTABLE);
+	name_resolve_failed = (flags & MGMT_DEV_FOUND_NAME_REQUEST_FAILED);
 
 	btd_adapter_update_found_device(adapter, &ev->addr.bdaddr,
 					ev->addr.type, ev->rssi, confirm_name,
-					legacy, not_connectable, ad_data,
+					legacy, not_connectable,
+					name_resolve_failed, ad_data,
 					ad_data_len, true);
 
 	if (handle) {
diff --git a/src/device.c b/src/device.c
index 6b398bd396..a83cb61f8c 100644
--- a/src/device.c
+++ b/src/device.c
@@ -79,6 +79,8 @@
 #define GATT_INCLUDE_UUID_STR "2802"
 #define GATT_CHARAC_UUID_STR "2803"
 
+#define NAME_RESOLVE_RETRY_DELAY	300 /* seconds */
+
 static DBusConnection *dbus_conn = NULL;
 static unsigned service_state_cb_id;
 
@@ -272,6 +274,8 @@ struct btd_device {
 
 	GIOChannel	*att_io;
 	guint		store_id;
+
+	time_t		name_resolve_failed_time;
 };
 
 static const uint16_t uuid_list[] = {
@@ -4389,6 +4393,29 @@ bool device_name_known(struct btd_device *device)
 	return device->name[0] != '\0';
 }
 
+bool device_is_name_resolve_allowed(struct btd_device *device)
+{
+	struct timespec now;
+
+	if (!device)
+		return false;
+
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	return now.tv_sec >= device->name_resolve_failed_time +
+						NAME_RESOLVE_RETRY_DELAY;
+}
+
+void device_name_resolve_fail(struct btd_device *device)
+{
+	struct timespec now;
+
+	if (!device)
+		return;
+
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	device->name_resolve_failed_time = now.tv_sec;
+}
+
 void device_set_class(struct btd_device *device, uint32_t class)
 {
 	if (device->class == class)
diff --git a/src/device.h b/src/device.h
index b37a0a3d21..071576d6b3 100644
--- a/src/device.h
+++ b/src/device.h
@@ -25,6 +25,8 @@ void btd_device_device_set_name(struct btd_device *device, const char *name);
 void device_store_cached_name(struct btd_device *dev, const char *name);
 void device_get_name(struct btd_device *device, char *name, size_t len);
 bool device_name_known(struct btd_device *device);
+bool device_is_name_resolve_allowed(struct btd_device *device);
+void device_name_resolve_fail(struct btd_device *device);
 void device_set_class(struct btd_device *device, uint32_t class);
 void device_update_addr(struct btd_device *device, const bdaddr_t *bdaddr,
 							uint8_t bdaddr_type);
-- 
2.34.0.rc2.393.gf8c9666880-goog


  reply	other threads:[~2021-11-25  7:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-25  7:06 [Bluez PATCH v4 1/5] mgmt: Add NAME_REQUEST_FAILED flag for device_found event Archie Pusaka
2021-11-25  7:06 ` Archie Pusaka [this message]
2021-11-25  7:06 ` [Bluez PATCH v4 3/5] device: Save remote name request attempts into cache file Archie Pusaka
2021-11-25  7:06 ` [Bluez PATCH v4 4/5] main: add configurable RemoteNameRequestRetryDelay parameter Archie Pusaka
2021-11-25  7:06 ` [Bluez PATCH v4 5/5] doc: Add Name Request Fail flag in device found event Archie Pusaka
2021-11-25  7:37 ` [Bluez,v4,1/5] mgmt: Add NAME_REQUEST_FAILED flag for device_found event bluez.test.bot
2021-11-29 17:49   ` Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211125150558.Bluez.v4.2.I9fc087b25433a9347b2d8c8ff7a25fadf448ef49@changeid \
    --to=apusaka@google.com \
    --cc=apusaka@chromium.org \
    --cc=chromeos-bluetooth-upstreaming@chromium.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=mcchou@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).