From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796312064; b=ej2YVNr6IyJfEFtipWs2qqsjFRVVmglTUPlqzmWbWDFvIQ/r/n1dlA8PDazuqEl119XT GjycdeheEXBwKYNJtzbqSTPzjUuA9dkDVnUejyZkBwB6dY4RGUVNEvra6QobV2WTSPhkm 1HNQHLUItlSZV/1tJIDDecu8uFPINWI3oyFh6CkrVJy6axwqxLNUdvwS2KBNOnvG/HE+6 Ae8OLfYOnMDUYqMvyKbp8nTZtZLam5tAYpAxPjS4wrA0Q8cN+jDjDzacBRnErdNVDrVYa qkb9/G6hGoF/rpzDD8fs/DrevcpsM1PdM1WDSKF4atYdN7vFshynJzy20MXg3Ua3B8g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796312064; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=zu/QEJWqs43B2uFLVsSTJ3aF9ZDBOtHnNDiWVgCOvfg=; b=ATCq0f+MHQ+cSxBSG9uVHg8AmFVD33ObMm1E1YMNayZb06Hvss57ogVEbmvQsDDgApCW /AyUsqYQ4rGvQQsqMhGxy4GaqfwEf0Y4jtW/i0gDYfMoYUCo8KJnmJKY0MZL9+zSlDsb2 Ph5n0xcOg0KRIhTTMDz+rJrGFbF5GGpv1qIVaaarmPfiAgfLsE2i90hdKndjri7EEo43+ dJQA0GMhTFnuOcJVYrVHqYVoqEW+QadInqSeIr1Kp+DTYBF48v1z8EZhhG4LV1nXw8NRh Cnh4yVIZGzyRnaWnm4rBq1zS9HCqkpfOdEI2ecp6a6e/hLGN3x88ZNeNMtSyXjxNRzA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=googlemail.com; spf=pass smtp.mailfrom=googlemail.com; dkim=pass header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20260707 header.b=e+LNitlf; arc=none smtp.remote-ip=209.85.221.51 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCF2A606A334 for ; Thu, 3 Dec 2026 15:34:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CCF2A606A334 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=googlemail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=googlemail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=googlemail.com header.i=@googlemail.com header.a=rsa-sha256 header.s=20260707 header.b=e+LNitlf Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4ac08f4a0b2so10268338f8f.3 for ; Thu, 03 Dec 2026 07:34:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20260707; t=1796312062; x=1796916862; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zu/QEJWqs43B2uFLVsSTJ3aF9ZDBOtHnNDiWVgCOvfg=; b=e+LNitlfippRKWCD+L9AUAcirxdCQFPoB9gKdLc3RY/KUG6g0eIL5arU3p0dyTDeMQ NYbvPHsklmTB60xET5oPhcXF/nUdeQyvx1PruTnHK3u5SGTLpl9GRR3d4alGSsiaXYvq 6Zfcjgh2q1h+f4Np1L6tcVXPAMeYh7baiOBgF2SrGYiJQ51xAM2JL1bf3DkrmPz6QsI0 69VCyVo/RZkBOa32AphFOxcbXDeMGyaWzEFjxUrbsezH6GUYxIXT6a6iDDrU2xhBolUr ploIFi4EcBtxHYhu3GRlIIdLa8s14SDs6HU2QlL2o7kDurU5bgTdthi+1O438aR8aBWD oOcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796312062; x=1796916862; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zu/QEJWqs43B2uFLVsSTJ3aF9ZDBOtHnNDiWVgCOvfg=; b=qBR4jB5QUeLuZsMCwDfNapjBnMzh58A6T0GqSenf5fNpwJ2ZRiAFeWScRPePi4KfJC DLt1eZMSveF+jUPijRSn5mcHLbZknolmUDh4vLyOxfeBUTd23+7k8ygi7MYm/L07tH/F IHYhAqm6P2U/rtBVadoArPuBbnKVBSRqsKdikzseAVAm4rEXK14dSXlrExi7tEnhCkos aLzmJ3RsttgJDApfI+XaK9y12R3CAav0H9C+bGKqJ1uLL1N6rH8agA/FBhpDA7JzC0NR iZxF61ieTSCZuisapkl8212+GoeJHlczD+spjpePIyWGIrqbtyTkW/dZxuUCW5KxHNua /jlg== X-Gm-Message-State: APuU4Z+Jocb8ze5MefKoH9zOyc+TvtQgqJXav4tgT+Zl7biRRel3yp+W CrnENiTeVWK8EBNH7AzgA+x2T56oGvaLCsuj5/OWUtKcKacRistrfc9OWdI0M3Dh X-Gm-Gg: AZFdTZf1BkEdnrGwveotcjGNuvFsoYwtgDAkQT1eydsuuxdsjQAsQAxLhkpBkt9ldZy Ou8khHRTmWUhJixnlEyOYmASDWmiNcATdmaYD6WbOf6TYK3JcSrncuQlF0Jst9OSQpldjdn6DM5 X1jXU6BImoVbj1N5EXatr+u6X4Rx+/4BAgrjCksEOE3gZGd32JYsZdFmEl2Dpk+3FvULJxE+ks9 eTzIkZ1Sg6yc0gGSV758gFbTzVt4hIjh2MfDsqbKlUeVJmJZs7YG8XZLODfcnKhWtAJhlkVyC// +8oZrlogPs0T8KTm+zfhzpCttF94XMGLDgKgQeWtF5zkioUBpDNuPwyqvsERw3qmsNqHedNQOJ/ 7GdJw8J9oHCO47TNDo2gg+sdPdWVQjBVXMWbjhMLXHBhZBobIhbxgk7al2CTzQizi0LTYCpTy5c mh/Bmv2/I1t0xbj0gVTzIiB5a/yDXUkdbjku0bG/5vS2Mw+UCE8c+EA0zCNIlc3vJc2lwfHYoas dKgAF0RGdvPo4JQKHe+vs/4/v6Er+UG6oCWeqeLVO6UTKM6UwgaCeHw+5m9cD//vHN8D+hZrTrU sMJqQ/ZKOKiiflEPnC3qPDlXQ0pXhVmINBOLY3oBdP+pgEr8Pu9/QbxWaw== X-Received: by 2002:a05:6000:18aa:b0:4a5:33ad:8721 with SMTP id ffacd0b85a97d-4aba532017dmr80922663f8f.0.1796312062147; Thu, 03 Dec 2026 07:34:22 -0800 (PST) Received: from sp8 (213-146-237-228.ptr.skytron.de. [213.146.237.228]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab65cb42absm83352896f8f.1.2026.12.03.07.34.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 07:34:21 -0800 (PST) From: Sebastian To: linux-bluetooth@vger.kernel.org Cc: Sebastian 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 Message-ID: <20261203153408.31769-2-seb.witt@googlemail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203153408.31769-1-seb.witt@googlemail.com> References: <20261203153408.31769-1-seb.witt@googlemail.com> X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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