Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions lib/hci-socket/gatt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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];
Expand Down