All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sebastian <seb.witt@googlemail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Sebastian <seb.witt@googlemail.com>
Subject: [PATCH v3 1/6] shared/gatt-client: Add bt_gatt_client_read_long_value_max_len
Date: Thu,  3 Dec 2026 16:34:03 +0100	[thread overview]
Message-ID: <20261203153408.31769-2-seb.witt@googlemail.com> (raw)
In-Reply-To: <20261203153408.31769-1-seb.witt@googlemail.com>

Long reads are truncated to BT_ATT_MAX_VALUE_LEN (512 octets), the
maximum length of an attribute value. Some non-compliant peripherals
expose longer values that other stacks read in full, so add a variant of
bt_gatt_client_read_long_value that lets the caller pick the maximum
length to accept.

bt_gatt_client_read_long_value keeps truncating to BT_ATT_MAX_VALUE_LEN.

Reads starting beyond the maximum length are refused, as the length of
the data appended to the value would otherwise underflow in
append_chunk, which also applies to bt_gatt_client_read_long_value.
---
 src/shared/gatt-client.c | 46 ++++++++++++++++++++++++++++++++++++----
 src/shared/gatt-client.h |  7 ++++++
 2 files changed, 49 insertions(+), 4 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 31982bd29..2870d5445 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -3064,6 +3064,7 @@ struct read_long_op {
 	int ref_count;
 	uint16_t value_handle;
 	uint16_t offset;
+	uint16_t max_len;
 	struct iovec iov;
 	bt_gatt_client_read_callback_t callback;
 	void *user_data;
@@ -3085,8 +3086,8 @@ static bool append_chunk(struct read_long_op *op, const uint8_t *data,
 								uint16_t len)
 {
 	/* Truncate if the data would exceed maximum length */
-	if (op->offset + len > BT_ATT_MAX_VALUE_LEN)
-		len = BT_ATT_MAX_VALUE_LEN - op->offset;
+	if (op->offset + len > op->max_len)
+		len = op->max_len - op->offset;
 
 	if (!util_iov_append(&op->iov, data, len))
 		return false;
@@ -3125,7 +3126,7 @@ static void read_long_cb(uint8_t opcode, const void *pdu,
 		goto done;
 	}
 
-	if (op->offset >= BT_ATT_MAX_VALUE_LEN)
+	if (op->offset >= op->max_len)
 		goto success;
 
 	if (length >= bt_att_get_mtu(op->client->att) - 1) {
@@ -3160,8 +3161,9 @@ done:
 						op->iov.iov_len, op->user_data);
 }
 
-unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
+static unsigned int read_long_value(struct bt_gatt_client *client,
 					uint16_t value_handle, uint16_t offset,
+					uint16_t max_len,
 					bt_gatt_client_read_callback_t callback,
 					void *user_data,
 					bt_gatt_client_destroy_func_t destroy)
@@ -3175,6 +3177,12 @@ unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
 	if (!client)
 		return 0;
 
+	/* Reading can't start beyond the maximum length of the value, the
+	 * length of the data appended to it would otherwise underflow.
+	 */
+	if (offset > max_len)
+		return 0;
+
 	op = new0(struct read_long_op, 1);
 
 	req = request_create(client);
@@ -3186,6 +3194,7 @@ unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
 	op->client = client;
 	op->value_handle = value_handle;
 	op->offset = offset;
+	op->max_len = max_len;
 	op->callback = callback;
 	op->user_data = user_data;
 	op->destroy = destroy;
@@ -3227,6 +3236,35 @@ unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
 	return req->id;
 }
 
+unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
+					uint16_t value_handle, uint16_t offset,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy)
+{
+	return read_long_value(client, value_handle, offset,
+					BT_ATT_MAX_VALUE_LEN, callback,
+					user_data, destroy);
+}
+
+/*
+ * Same as bt_gatt_client_read_long_value but allows the caller to accept
+ * values longer than the 512 octets permitted by the specification
+ * (Core v5.4, Vol 3, Part F, Section 3.2.9), as sent by some non-compliant
+ * peripherals. Reading stops once max_len octets have been received.
+ */
+unsigned int bt_gatt_client_read_long_value_max_len(
+					struct bt_gatt_client *client,
+					uint16_t value_handle, uint16_t offset,
+					uint16_t max_len,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy)
+{
+	return read_long_value(client, value_handle, offset, max_len,
+					callback, user_data, destroy);
+}
+
 unsigned int bt_gatt_client_write_without_response(
 					struct bt_gatt_client *client,
 					uint16_t value_handle,
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index 0c5d118c7..65266e11a 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -80,6 +80,13 @@ unsigned int bt_gatt_client_read_long_value(struct bt_gatt_client *client,
 					bt_gatt_client_read_callback_t callback,
 					void *user_data,
 					bt_gatt_client_destroy_func_t destroy);
+unsigned int bt_gatt_client_read_long_value_max_len(
+					struct bt_gatt_client *client,
+					uint16_t value_handle, uint16_t offset,
+					uint16_t max_len,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy);
 unsigned int bt_gatt_client_read_multiple(struct bt_gatt_client *client,
 					uint16_t *handles, uint8_t num_handles,
 					bt_gatt_client_read_callback_t callback,
-- 
2.56.1


  reply	other threads:[~2026-12-03 15:34 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CABBYNZ+GNO-eVNC5B_4SqCdHOE+OHmXUaW0NwStd_BsbUzx35A@mail.gmail.com>
2026-11-30 20:45 ` [PATCH v2 0/4] Accept HoG Report Map longer than 512 octets Sebastian
2026-11-30 20:45   ` [PATCH v2 1/4] shared/gatt-client: Add bt_gatt_client_read_long_value_max_len Sebastian
2026-12-01  0:02     ` Accept HoG Report Map longer than 512 octets bluez.test.bot
2026-12-02 16:13       ` Luiz Augusto von Dentz
2026-12-03 18:16         ` Sebastian Witt
2026-11-30 20:45   ` [PATCH v2 2/4] main.conf: Add LongReportMap option to [Input] Sebastian
2026-11-30 20:45   ` [PATCH v2 3/4] shared/hog: Add bt_hog_set_long_report_map Sebastian
2026-11-30 20:45   ` [PATCH v2 4/4] input/hog: Accept Report Map longer than 512 octets Sebastian
2026-12-03 15:34 ` [PATCH v3 0/6] Accept HoG " Sebastian
2026-12-03 15:34   ` Sebastian [this message]
2026-12-03 17:56     ` bluez.test.bot
2026-12-03 15:34   ` [PATCH v3 2/6] main.conf: Add LongReportMap option to [Input] Sebastian
2026-12-03 15:34   ` [PATCH v3 3/6] shared/hog: Add bt_hog_set_long_report_map Sebastian
2026-12-03 15:34   ` [PATCH v3 4/6] input/hog: Accept Report Map longer than 512 octets Sebastian
2026-12-03 15:34   ` [PATCH v3 5/6] unit/test-gatt: Add tests for long reads beyond " Sebastian
2026-12-03 15:34   ` [PATCH v3 6/6] unit/test-hog: Add tests for long Report Map Sebastian
2026-12-03 20:30   ` [PATCH v3 0/6] Accept HoG Report Map longer than 512 octets patchwork-bot+bluetooth

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=20261203153408.31769-2-seb.witt@googlemail.com \
    --to=seb.witt@googlemail.com \
    --cc=linux-bluetooth@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.