Linux bluetooth development
 help / color / mirror / Atom feed
From: Sebastian <seb.witt@googlemail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Sebastian <seb.witt@googlemail.com>,
	"Claude Opus 5.5" <noreply@anthropic.com>
Subject: [PATCH 1/2] shared/gatt-client: Add bt_gatt_client_read_long_value_max_len
Date: Mon, 30 Nov 2026 20:03:39 +0100	[thread overview]
Message-ID: <20261130190340.201189-2-seb.witt@googlemail.com> (raw)
In-Reply-To: <20261130190340.201189-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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 src/shared/gatt-client.c | 40 ++++++++++++++++++++++++++++++++++++----
 src/shared/gatt-client.h |  7 +++++++
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 31982bd29..f9806b0e3 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)
@@ -3186,6 +3188,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 +3230,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-11-30 19:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 19:03 [PATCH 0/2] Accept HoG Report Map longer than 512 octets Sebastian
2026-11-30 19:03 ` Sebastian [this message]
2026-11-30 19:03 ` [PATCH 2/2] shared/hog: Accept " Sebastian

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=20261130190340.201189-2-seb.witt@googlemail.com \
    --to=seb.witt@googlemail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=noreply@anthropic.com \
    /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