Skip to content

Commit

Permalink
feat: cleanup after adapter power on->off->on cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
stoprocent committed Feb 26, 2025
1 parent ba037b6 commit 276e8e7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
8 changes: 8 additions & 0 deletions lib/hci-socket/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ NobleBindings.prototype.onStateChange = function (state) {
if (this._state === state) {
return;
}

// If we are powered on and we are powered off, disconnect all connections
if (this._state === 'poweredOn' && state === 'poweredOff') {
for (const handle in this._handles) {
this.onDisconnComplete(handle, 0x03); // Hardward Failure
}
}

this._state = state;

if (state === 'unauthorized') {
Expand Down
1 change: 0 additions & 1 deletion lib/hci-socket/gatt.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ Gatt.prototype.onAclStreamEnd = function () {

Gatt.prototype.writeAtt = function (data) {
debug(`${this._address}: write: ${data.toString('hex')}`);

this._aclStream.write(ATT_CID, data);
};

Expand Down
51 changes: 26 additions & 25 deletions lib/hci-socket/hci.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,19 @@ const Hci = function (options) {
this._isExtended = 'extended' in options && options.extended;
this._state = null;
this._bindParams = 'bindParams' in options ? options.bindParams : undefined;

this._handleBuffers = {};

this._aclBuffers = undefined;
this._resolveAclBuffers = undefined;

const aclBuffersPromise = new Promise((resolve) => {
this._resolveAclBuffers = resolve;
});

this.getAclBuffers = async function () {
if (this._aclBuffers) return this._aclBuffers;
return await aclBuffersPromise;
}.bind(this);

this.setAclBuffers = function (length, num) {
if (this._aclBuffers) {
this._aclBuffers.length = length;
Expand All @@ -130,7 +131,6 @@ const Hci = function (options) {
}.bind(this);

this._aclConnections = new Map();

this._aclQueue = [];

this._deviceId = options.deviceId != null
Expand Down Expand Up @@ -926,32 +926,28 @@ Hci.prototype.processCmdCompleteEvent = function (cmd, status, result) {
this.setLeEventMask();
this.readLocalVersion();
this.readBdAddr();
} else if (cmd === LE_READ_LOCAL_SUPPORTED_FEATURES) {
if (status === 0) {
// Set _isExtended based on leExtendedAdvertising bit (12)
this._isExtended = !!(result.readUInt32LE(0) & (1 << 12));
} else if (cmd === LE_READ_LOCAL_SUPPORTED_FEATURES && status === 0) {
// Set _isExtended based on leExtendedAdvertising bit (12)
this._isExtended = !!(result.readUInt32LE(0) & (1 << 12));

this.emit('leFeatures', result);
this.emit('leFeatures', result);

if (this._isExtended) {
this.setCodedPhySupport();
}
this.setEventMask();
this.setLeEventMask();
this.readLocalVersion();
this.writeLeHostSupported();
this.readLeHostSupported();
this.readLeBufferSize();
this.readBdAddr();
if (this._isExtended) {
this.setCodedPhySupport();
}
} else if (cmd === READ_LE_HOST_SUPPORTED_CMD) {
if (status === 0) {
const le = result.readUInt8(0);
const simul = result.readUInt8(1);
this.setEventMask();
this.setLeEventMask();
this.readLocalVersion();
this.writeLeHostSupported();
this.readLeHostSupported();
this.readLeBufferSize();
this.readBdAddr();
} else if (cmd === READ_LE_HOST_SUPPORTED_CMD && status === 0) {
const le = result.readUInt8(0);
const simul = result.readUInt8(1);

debug(`\t\t\tle = ${le}`);
debug(`\t\t\tsimul = ${simul}`);
}
debug(`\t\t\tle = ${le}`);
debug(`\t\t\tsimul = ${simul}`);
} else if (cmd === READ_LOCAL_VERSION_CMD) {
const hciVer = result.readUInt8(0);
const hciRev = result.readUInt16LE(1);
Expand Down Expand Up @@ -1289,6 +1285,11 @@ Hci.prototype.processCmdStatusEvent = function (cmd, status) {
};

Hci.prototype.onStateChange = function (state) {
// If we are powered on and we are powered off, clean up
if (this._state === 'poweredOn' && state === 'poweredOff') {
this._aclConnections.clear();
this._aclQueue = [];
}
this._state = state;
};

Expand Down
23 changes: 15 additions & 8 deletions lib/noble.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ function Noble (bindings) {
this.address = 'unknown';
this._state = 'unknown';
this._bindings = bindings;
this._peripherals = {};
this._services = {};
this._characteristics = {};
this._descriptors = {};
this._discoveredPeripheralUUids = {};

this._cleanupPeriperals();

this._bindings.on('stateChange', this.onStateChange.bind(this));
this._bindings.on('addressChange', this.onAddressChange.bind(this));
Expand Down Expand Up @@ -56,7 +53,6 @@ function Noble (bindings) {
this.on('newListener', (event) => {
if (event === 'stateChange' && !this.initialized) {
this.initialized = true;

process.nextTick(() => {
try {
this._bindings.init();
Expand All @@ -73,7 +69,6 @@ function Noble (bindings) {
get: function () {
if (!this.initialized) {
this.initialized = true;

this._bindings.init();
}
return this._state;
Expand All @@ -84,11 +79,23 @@ function Noble (bindings) {

util.inherits(Noble, events.EventEmitter);

Noble.prototype._cleanupPeriperals = function () {
this._peripherals = {};
this._services = {};
this._characteristics = {};
this._descriptors = {};
this._discoveredPeripheralUUids = {};
}

Check failure on line 88 in lib/noble.js

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Noble.prototype.onStateChange = function (state) {
debug(`stateChange ${state}`);

this._state = state;
// If the state is poweredOff and the previous state was poweredOn, clean up the peripherals
if (state === 'poweredOff' && this._state === 'poweredOn') {
this._cleanupPeriperals();
}

this._state = state;
this.emit('stateChange', state);
};

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"node-gyp-build": "^4.8.1"
},
"optionalDependencies": {
"@stoprocent/bluetooth-hci-socket": "^1.4.5"
"@stoprocent/bluetooth-hci-socket": "^1.5.1"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
Expand Down

0 comments on commit 276e8e7

Please sign in to comment.