Skip to content

Commit

Permalink
Diff left
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Mar 3, 2021
1 parent dd15462 commit f05c736
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
12 changes: 12 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
npmpublish:
image: node:8
script:
- npm install -g npm-cli-login
- npm-cli-login -u ${NPM_USER} -p ${NPM_PASS} -e [email protected] -r https://npm.essentim.cloud -s @essentim --quotes
- npm publish
only:
- production
- staging
- master
tags:
- amd64
6 changes: 6 additions & 0 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ NobleBindings.prototype.connect = function (peripheralUuid, parameters) {
}
};

NobleBindings.prototype.cancelConnect = function (peripheralUuid) {
// TODO: check if it was not in the queue and only then issue cancel on hci
this._connectionQueue = this._connectionQueue.filter(c => c.id !== peripheralUuid);
this._hci.cancelConnect(this._handles[peripheralUuid]);
};

NobleBindings.prototype.disconnect = function (peripheralUuid) {
this._hci.disconnect(this._handles[peripheralUuid]);
};
Expand Down
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
25 changes: 25 additions & 0 deletions lib/peripheral.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ Peripheral.prototype.connectAsync = function (options) {
return util.promisify(callback => this.connect(options, callback))();
};

const cancelConnect = function (options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
// if (callback) {
// this.once('cancelConnect', function(error) {
// callback(error);
// });
// }

if (this.state !== 'connecting') {
// this.emit('cancelConnect', new Error('Peripheral currently not connecting'));
} else {
// this.state = 'connecting';
this.emit('connect', new Error('connection canceled!'));
this._noble.cancelConnect(this.id, options);
}
};

Peripheral.prototype.cancelConnect = cancelConnect;
Peripheral.prototype.cancelConnectAsync = function (options) {
return util.promisify((callback) => this.cancleConnect(options, callback))();
};

const disconnect = function (callback) {
if (callback) {
this.once('disconnect', () => {
Expand Down

0 comments on commit f05c736

Please sign in to comment.