Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanner, Discovery and Controller enhancements #473

Merged
merged 45 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3f124e8
Make sure Cache remembers former keys
Apollon77 Nov 2, 2023
b15b6b6
Return ttl to when parsing IP Records
Apollon77 Nov 2, 2023
a9798e6
Add ttl handling in MDnS Scanner
Apollon77 Nov 2, 2023
d0d154d
Allow to discover only new records and continuously
Apollon77 Nov 2, 2023
3101939
Prevent message processing and also querying once closed
Apollon77 Nov 2, 2023
3a48161
allow to cancel "continuous" queries
Apollon77 Nov 2, 2023
072035a
Add deviceIdentifier to Commission device data
Apollon77 Nov 2, 2023
a5f3bb6
Add needed support to BLE Scanner too
Apollon77 Nov 2, 2023
5eab144
Enhance abstract Scanner definition
Apollon77 Nov 2, 2023
393e3c3
Move "promises.any" polyfill to generic class
Apollon77 Nov 2, 2023
c0b4a0e
Enhance ControllerDiscovery
Apollon77 Nov 2, 2023
76fb5ea
Enhance test to check scanner expiring records
Apollon77 Nov 2, 2023
12c69dc
Adjust commissioning to be more generic and parallel (IP/BLE)
Apollon77 Nov 2, 2023
e467a60
Add continuous and polling discovery to resume logic
Apollon77 Nov 2, 2023
ca942a5
Add 60s resume Timeout for Interaction client
Apollon77 Nov 2, 2023
d740a35
Cleanup and new imports
Apollon77 Nov 2, 2023
f895c0d
Allow controller to discover commissionable devices
Apollon77 Nov 2, 2023
070638a
Forward config/callbacks
Apollon77 Nov 2, 2023
d212c86
Use discovered device object for commissioning
Apollon77 Nov 2, 2023
e588028
Split disconnect from removal of a node
Apollon77 Nov 2, 2023
be7d9ff
Introduce Node state
Apollon77 Nov 2, 2023
8562378
Add connection state logic
Apollon77 Nov 2, 2023
f780f7a
Allow Endpoint structure to update
Apollon77 Nov 2, 2023
7997a35
Allow Endpoint logging to pass through options
Apollon77 Nov 2, 2023
8d94cde
Add nodeid to attribute/event callbacks
Apollon77 Nov 2, 2023
0c4561a
Add all callbacks to controller example
Apollon77 Nov 2, 2023
286010f
Update shell License infos
Apollon77 Nov 2, 2023
bac488c
Shell: commission - allow commission by instanceId
Apollon77 Nov 2, 2023
f9e7872
Shell: add possible callbacks and log to console for now what they re…
Apollon77 Nov 2, 2023
1a89430
Shell: add unpair command
Apollon77 Nov 2, 2023
eedcee8
Shell: discover commands
Apollon77 Nov 2, 2023
6749edd
Shell: Add connect/disconnect to a node
Apollon77 Nov 2, 2023
3c8dc32
Shell: Improve done/exit handling
Apollon77 Nov 2, 2023
9ae6443
Shell: Renaming for consistency
Apollon77 Nov 2, 2023
a57f4ee
Stabilize tests a bit more
Apollon77 Nov 2, 2023
12fc8d1
Leftover
Apollon77 Nov 2, 2023
cf29de6
Last fixes
Apollon77 Nov 2, 2023
dbbee15
[execute-chiptests-long] Changelog
Apollon77 Nov 2, 2023
ecd7e89
Merge branch 'main' into scanner-expiry
Apollon77 Nov 3, 2023
5029405
[execute-chiptests-long] Changelog
Apollon77 Nov 3, 2023
1589f09
Add some state changed tests
Apollon77 Nov 3, 2023
63551bf
Merge branch 'main' into scanner-expiry
Apollon77 Nov 3, 2023
91b6746
Merge branch 'main' into scanner-expiry
Apollon77 Nov 3, 2023
041960f
Merge branch 'main' into scanner-expiry
Apollon77 Nov 6, 2023
d3423c8
Address review feedback
Apollon77 Nov 6, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ The main work (all changes without a GitHub username in brackets in the below li
* Feature: Added generation method for random passcodes to PaseClient
* Feature: Generalized Discovery logic and allow discoveries via different methods (BLE+IP) in parallel
* Feature: Added functionality to clear session contexts including data in sub-contexts or not
* Feature: Enhance discovery methods to allow continuous discovery for operational devices
* matter.js API:
* Breaking: Rename resetStorage() on CommissioningServer to factoryReset() and add logic to restart the device if currently running
* Breaking: Restructure the CommissioningController to allow pairing with multiple nodes
* Adjusts some property and structure namings to be more consistent
* Introducing class PairedNode with the High level API for a paired Node
* Restructured CommissioningController to handle multiple nodes and offer new high level API
* Changed name of the unique storage id for servers or controllers added to MatterServer to "uniqueStorageKey"
* Adjusted subscription callbacks to also provide the nodeId of the affected device reporting the changes to allow callbacks to be used generically when connecting to all nodes
* Introduces a node state information callback to inform about the connection status but also when the node structure changed (for bridges) or such.
* Feature: Enhanced CommissioningServer API and CommissioningController for improved practical usage
* Feature: Makes Port for CommissioningServer optional and add automatic port handling in MatterServer
* Feature: Allows removal of Controller or Server instances from Matter server, optionally with deleting the storage
* Enhance: Makes passcode and discriminator for CommissioningServer optional and randomly generate them if not provided
Expand Down
65 changes: 57 additions & 8 deletions packages/matter-node-ble.js/src/ble/BleScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ type CommissionableDeviceData = CommissionableDevice & {
};

export class BleScanner implements Scanner {
private readonly recordWaiters = new Map<string, { resolver: () => void; timer: Timer }>();
private readonly recordWaiters = new Map<
string,
{ resolver: () => void; timer: Timer; resolveOnUpdatedRecords: boolean }
Apollon77 marked this conversation as resolved.
Show resolved Hide resolved
>();
private readonly discoveredMatterDevices = new Map<string, DiscoveredBleDevice>();

constructor(private readonly nobleClient: NobleBleClient) {
Expand All @@ -53,22 +56,27 @@ export class BleScanner implements Scanner {
* Registers a deferred promise for a specific queryId together with a timeout and return the promise.
* The promise will be resolved when the timer runs out latest.
*/
private async registerWaiterPromise(queryId: string, timeoutSeconds: number) {
private async registerWaiterPromise(queryId: string, timeoutSeconds: number, resolveOnUpdatedRecords = true) {
lauckhart marked this conversation as resolved.
Show resolved Hide resolved
const { promise, resolver } = createPromise<void>();
const timer = Time.getTimer(timeoutSeconds * 1000, () => this.finishWaiter(queryId, true)).start();
this.recordWaiters.set(queryId, { resolver, timer });
logger.debug(`Registered waiter for query ${queryId} with timeout ${timeoutSeconds} seconds`);
this.recordWaiters.set(queryId, { resolver, timer, resolveOnUpdatedRecords });
logger.debug(
`Registered waiter for query ${queryId} with timeout ${timeoutSeconds} seconds${
resolveOnUpdatedRecords ? "" : " (not resolving on updated records)"
}`,
);
return { promise };
}

/**
* Remove a waiter promise for a specific queryId and stop the connected timer. If required also resolve the
* promise.
*/
private finishWaiter(queryId: string, resolvePromise = false) {
private finishWaiter(queryId: string, resolvePromise: boolean, isUpdatedRecord = false) {
const waiter = this.recordWaiters.get(queryId);
if (waiter === undefined) return;
const { timer, resolver } = waiter;
const { timer, resolver, resolveOnUpdatedRecords } = waiter;
if (isUpdatedRecord && !resolveOnUpdatedRecords) return;
logger.debug(`Finishing waiter for query ${queryId}, resolving: ${resolvePromise}`);
timer.stop();
if (resolvePromise) {
Expand All @@ -77,6 +85,11 @@ export class BleScanner implements Scanner {
this.recordWaiters.delete(queryId);
}

cancelCommissionableDeviceDiscovery(identifier: CommissionableDeviceIdentifiers) {
const queryKey = this.buildCommissionableQueryIdentifier(identifier);
this.finishWaiter(queryKey, true);
}

private handleDiscoveredDevice(peripheral: Peripheral, manufacturerServiceData: ByteArray) {
logger.debug(`Discovered device ${peripheral.address} ${manufacturerServiceData?.toHex()}`);

Expand All @@ -85,6 +98,7 @@ export class BleScanner implements Scanner {
BtpCodec.decodeBleAdvertisementServiceData(manufacturerServiceData);

const commissionableDevice: CommissionableDeviceData = {
deviceIdentifier: peripheral.address,
D: discriminator,
SD: (discriminator >> 8) & 0x0f,
VP: `${vendorId}+${productId}`,
Expand All @@ -93,6 +107,8 @@ export class BleScanner implements Scanner {
};
logger.debug(`Discovered device ${peripheral.address} data: ${JSON.stringify(commissionableDevice)}`);

const deviceExisting = this.discoveredMatterDevices.has(peripheral.address);

this.discoveredMatterDevices.set(peripheral.address, {
deviceData: commissionableDevice,
peripheral: peripheral,
Expand All @@ -101,7 +117,7 @@ export class BleScanner implements Scanner {

const queryKey = this.findCommissionableQueryIdentifier(commissionableDevice);
if (queryKey !== undefined) {
this.finishWaiter(queryKey, true);
this.finishWaiter(queryKey, true, deviceExisting);
}
} catch (error) {
logger.debug(`Seems not to be a valid Matter device: Failed to decode device data: ${error}`);
Expand Down Expand Up @@ -217,12 +233,45 @@ export class BleScanner implements Scanner {
return storedRecords.map(({ deviceData }) => deviceData);
}

async findCommissionableDevicesContinuously(
identifier: CommissionableDeviceIdentifiers,
callback: (device: CommissionableDevice) => void,
timeoutSeconds = 60,
): Promise<CommissionableDevice[]> {
const discoveredDevices = new Set<string>();

const discoveryEndTime = Time.nowMs() + timeoutSeconds * 1000;
const queryKey = this.buildCommissionableQueryIdentifier(identifier);
await this.nobleClient.startScanning();

while (true) {
this.getCommissionableDevices(identifier).forEach(({ deviceData }) => {
const { deviceIdentifier } = deviceData;
if (!discoveredDevices.has(deviceIdentifier)) {
discoveredDevices.add(deviceIdentifier);
callback(deviceData);
}
});

const remainingTime = Math.ceil((discoveryEndTime - Time.nowMs()) / 1000);
if (remainingTime <= 0) {
break;
}
const { promise } = await this.registerWaiterPromise(queryKey, remainingTime, false);
Apollon77 marked this conversation as resolved.
Show resolved Hide resolved
await promise;
}
await this.nobleClient.stopScanning();
return this.getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
}

getDiscoveredCommissionableDevices(identifier: CommissionableDeviceIdentifiers): CommissionableDevice[] {
return this.getCommissionableDevices(identifier).map(({ deviceData }) => deviceData);
}

close(): void {
void this.nobleClient.stopScanning();
[...this.recordWaiters.keys()].forEach(queryId => this.finishWaiter(queryId, true));
[...this.recordWaiters.keys()].forEach(queryId =>
this.finishWaiter(queryId, !!this.recordWaiters.get(queryId)?.timer),
);
}
}
17 changes: 10 additions & 7 deletions packages/matter-node-shell.js/src/MatterNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
* Import needed modules from @project-chip/matter-node.js
* @license
* Copyright 2022-2023 Project CHIP Authors
* SPDX-License-Identifier: Apache-2.0
*/

// Include this first to auto-register Crypto, Network and Time Node.js implementations
import { CommissioningController, MatterServer } from "@project-chip/matter-node.js";

Expand All @@ -21,7 +24,7 @@ export class MatterNode {
private storageContext?: StorageContext;

commissioningController?: CommissioningController;
private matterDevice?: MatterServer;
private matterController?: MatterServer;

constructor(private nodeNum: number) {}

Expand Down Expand Up @@ -52,7 +55,7 @@ export class MatterNode {
}

async close() {
await this.matterDevice?.close();
await this.matterController?.close();
this.closeStorage();
}

Expand All @@ -67,7 +70,7 @@ export class MatterNode {
if (this.storageManager === undefined) {
throw new Error("StorageManager not initialized"); // Should never happen
}
if (this.matterDevice !== undefined) {
if (this.matterController !== undefined) {
return;
}
logger.info(`matter.js shell controller started for node ${this.nodeNum}`);
Expand All @@ -85,11 +88,11 @@ export class MatterNode {
* are called.
*/

this.matterDevice = new MatterServer(this.storageManager);
this.matterController = new MatterServer(this.storageManager);
this.commissioningController = new CommissioningController({
autoConnect: false,
});
this.matterDevice.addCommissioningController(this.commissioningController);
this.matterController.addCommissioningController(this.commissioningController);

/**
* Start the Matter Server
Expand All @@ -98,7 +101,7 @@ export class MatterNode {
* CommissioningServer node then this command also starts the announcement of the device into the network.
*/

await this.matterDevice.start();
await this.matterController.start();
}

async connectAndGetNodes(nodeIdStr?: string) {
Expand Down
18 changes: 4 additions & 14 deletions packages/matter-node-shell.js/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/**
* Copyright 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @license
* Copyright 2022-2023 Project CHIP Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { BleNode } from "@project-chip/matter-node-ble.js/ble";
Expand Down Expand Up @@ -85,7 +75,7 @@ async function main() {

const { nodeNum, ble, nodeType, resetStorage } = argv;

const theNode = new MatterNode(nodeNum);
theNode = new MatterNode(nodeNum);
await theNode.initialize(resetStorage);
const theShell = new Shell(theNode, PROMPT);

Expand Down
29 changes: 13 additions & 16 deletions packages/matter-node-shell.js/src/shell/Shell.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/**
* Copyright 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @license
* Copyright 2022-2023 Project CHIP Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { MatterError } from "@project-chip/matter-node.js/common";
Expand All @@ -22,6 +12,7 @@ import { MatterNode } from "../MatterNode.js";
import { exit } from "../app";
import cmdCommission from "./cmd_commission.js";
import cmdConfig from "./cmd_config.js";
import cmdDiscover from "./cmd_discover.js";
import cmdIdentify from "./cmd_identify.js";
import cmdLock from "./cmd_lock.js";
import cmdNodes from "./cmd_nodes.js";
Expand Down Expand Up @@ -55,7 +46,6 @@ export class Shell {
*
* @param {MatterNode} theNode MatterNode object to use for all commands.
* @param {string} prompt Prompt string to use for each command line.
* @param {Array} commandList Array of JSON commands dispatch structures.
*/
constructor(
public theNode: MatterNode,
Expand All @@ -77,8 +67,12 @@ export class Shell {
});
})
.on("close", () => {
process.stdout.write("goodbye\n");
process.exit(0);
exit()
.then(() => process.exit(0))
.catch(e => {
process.stderr.write(`Close error: ${e}\n`);
process.exit(1);
});
});

this.readline.prompt();
Expand All @@ -102,6 +96,7 @@ export class Shell {
cmdOnOff(this.theNode),
cmdSubscribe(this.theNode),
cmdIdentify(this.theNode),
cmdDiscover(this.theNode),
exitCommand(),
])
.command({
Expand All @@ -124,6 +119,8 @@ export class Shell {
if (argv.unhandled) {
process.stderr.write(`Unknown command: ${line}\n`);
yargsInstance.showHelp();
} else {
console.log("Done.");
}
} catch (error) {
process.stderr.write(`Error happened during command: ${error}\n`);
Expand Down
Loading