From 249007c594f48d66573e662134fbb7a652b11c70 Mon Sep 17 00:00:00 2001 From: Alexandre Trovato Date: Wed, 3 Mar 2021 22:02:58 +0100 Subject: [PATCH] Diff left --- lib/hci-socket/gatt.js | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/hci-socket/gatt.js b/lib/hci-socket/gatt.js index 535487a4f..e81049c28 100644 --- a/lib/hci-socket/gatt.js +++ b/lib/hci-socket/gatt.js @@ -676,6 +676,17 @@ Gatt.prototype.notify = function (serviceUuid, characteristicUuid, notify) { }); }; +function reverse (src) { + const buffer = Buffer.alloc(src.length); + + for (let i = 0, j = src.length - 1; i <= j; ++i, --j) { + buffer[i] = src[j]; + buffer[j] = src[i]; + } + + return buffer; +} + Gatt.prototype.discoverDescriptors = function (serviceUuid, characteristicUuid) { const characteristic = this._characteristics[serviceUuid][characteristicUuid]; const descriptors = []; @@ -684,24 +695,35 @@ Gatt.prototype.discoverDescriptors = function (serviceUuid, characteristicUuid) const callback = data => { const opcode = data[0]; - let i = 0; if (opcode === ATT_OP_FIND_INFO_RESP) { const format = data[1]; - const elen = 2 + (format === 0x01 ? 2 : 16); - const num = (data.length - 2) / (elen); - for (i = 0; i < num; i++) { - const offset = 2 + (i * elen); - descriptors.push({ - handle: data.readUInt16LE(offset + 0), - uuid: (format === 0x01) ? data.readUInt16LE(offset + 2).toString(16) : data.slice(offset + 2).slice(0, 16).toString('hex').match(/.{1,2}/g).reverse().join('') - }); + let pos = 2; // skip first 2 bytes (opcode, format) + + while (data.length > pos) { + switch (format) { + case 1: + descriptors.push({ + handle: data.readUInt16LE(pos), + uuid: data.readUInt16LE(pos + 2).toString(16) + }); + pos = pos + 4; + break; + + case 2: + descriptors.push({ + handle: data.readUInt16LE(pos), + uuid: reverse(data.slice(pos + 2, pos + 2 + 16)).toString('hex') + }); + pos = pos + 18; + break; + } } } if (opcode !== ATT_OP_FIND_INFO_RESP || descriptors[descriptors.length - 1].handle === characteristic.endHandle) { const descriptorUuids = []; - for (i = 0; i < descriptors.length; i++) { + for (let i = 0; i < descriptors.length; i++) { descriptorUuids.push(descriptors[i].uuid); this._descriptors[serviceUuid][characteristicUuid][descriptors[i].uuid] = descriptors[i];