From 8ef79b7185a2cd25a02247f150796c1a44529647 Mon Sep 17 00:00:00 2001 From: Ingo Fischer Date: Mon, 6 Nov 2023 20:09:24 +0100 Subject: [PATCH] Use Netinterface limitation als for scanner (#475) --- CHANGELOG.md | 3 +++ packages/matter-node-shell.js/src/MatterNode.ts | 7 +++++-- packages/matter-node-shell.js/src/app.ts | 16 +++++++++++----- packages/matter-node.js-examples/README.md | 2 +- .../src/examples/BridgedDevicesNode.ts | 4 ++-- .../src/examples/ComposedDeviceNode.ts | 4 ++-- .../src/examples/DeviceNode.ts | 4 ++-- .../src/examples/MultiDeviceNode.ts | 4 ++-- packages/matter.js/src/MatterServer.ts | 15 +++++++++++++-- 9 files changed, 41 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d70f712b..e641e605d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,12 +36,15 @@ The main work (all changes without a GitHub username in brackets in the below li * 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. + * Breaking: option "mdnsAnnounceInterface" was deprecated and replaced by "mdnsInterface" and now used to limit announcements and scanning to a specific interface * 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 * matter-node-shell.js * Feature: Completely refactored and enhances shell to support commissioning, identify and many more new commands. See Readme, try it +* matter-node.js-examples + * Breaking: Rename parameter -announceinterface to -netinterface and use for announcements and scanning ## 0.6.0 (2023-10-08) * Matter-Core functionality: diff --git a/packages/matter-node-shell.js/src/MatterNode.ts b/packages/matter-node-shell.js/src/MatterNode.ts index 81d9fb27de..946185ca07 100644 --- a/packages/matter-node-shell.js/src/MatterNode.ts +++ b/packages/matter-node-shell.js/src/MatterNode.ts @@ -26,7 +26,10 @@ export class MatterNode { commissioningController?: CommissioningController; private matterController?: MatterServer; - constructor(private nodeNum: number) {} + constructor( + private readonly nodeNum: number, + private readonly netInterface?: string, + ) {} async initialize(resetStorage: boolean) { /** @@ -88,7 +91,7 @@ export class MatterNode { * are called. */ - this.matterController = new MatterServer(this.storageManager); + this.matterController = new MatterServer(this.storageManager, { mdnsInterface: this.netInterface }); this.commissioningController = new CommissioningController({ autoConnect: false, }); diff --git a/packages/matter-node-shell.js/src/app.ts b/packages/matter-node-shell.js/src/app.ts index 42bcf3a698..a7cf1d8afa 100644 --- a/packages/matter-node-shell.js/src/app.ts +++ b/packages/matter-node-shell.js/src/app.ts @@ -63,20 +63,26 @@ async function main() { description: "Enable BLE support", type: "boolean", }, - "reset-storage": { - description: "Reset storage of this node", + "factory-reset": { + description: "Factory-Reset storage of this node", default: false, type: "boolean", }, + "net-interface": { + description: "Network interface to use for MDNS announcements and scanning", + type: "string", + default: undefined, + }, }); }, async argv => { if (argv.help) return; - const { nodeNum, ble, nodeType, resetStorage } = argv; + const { nodeNum, ble, nodeType, factoryReset, netInterface } = argv; + + theNode = new MatterNode(nodeNum, netInterface); + await theNode.initialize(factoryReset); - theNode = new MatterNode(nodeNum); - await theNode.initialize(resetStorage); const theShell = new Shell(theNode, PROMPT); if (ble) { diff --git a/packages/matter-node.js-examples/README.md b/packages/matter-node.js-examples/README.md index 9d0c09c206..4b0a708be8 100644 --- a/packages/matter-node.js-examples/README.md +++ b/packages/matter-node.js-examples/README.md @@ -95,7 +95,7 @@ The following parameters are available: * -productid: the product ID as number to use for pairing (default: 32768 (0x8000)) * -uniqueid: a unique ID for this device to be used in naming and to store structure information (default: ms time now) * -type: the device type to use for pairing (default: light, alternative value is "socket") -* -announceinterface: limit mdns announcements to the provided network interface, e.g. "en0" (default: all interfaces available) +* -netinterface: limit mdns announcements and scanning to the provided network interface, e.g. "en0" (default: all interfaces available) * -ble: enable BLE support (default: false) If this is enabled the device will announce itself _only_ via BLE if not commissioned and also presents a "Wifi only" device for commissioning to show this feature! * -ble-hci-id: Optionally, HCI ID to use (Linux only, default 0) * -port: the port to listen on for the device (default: 5540) diff --git a/packages/matter-node.js-examples/src/examples/BridgedDevicesNode.ts b/packages/matter-node.js-examples/src/examples/BridgedDevicesNode.ts index 2a619416a0..95a0292ce2 100644 --- a/packages/matter-node.js-examples/src/examples/BridgedDevicesNode.ts +++ b/packages/matter-node.js-examples/src/examples/BridgedDevicesNode.ts @@ -107,7 +107,7 @@ class BridgedDevice { const productName = `node-matter OnOff-Bridge`; const productId = getIntParameter("productid") ?? deviceStorage.get("productid", 0x8000); - const netAnnounceInterface = getParameter("announceinterface"); + const netInterface = getParameter("netinterface"); const port = getIntParameter("port") ?? 5540; const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs()); @@ -131,7 +131,7 @@ class BridgedDevice { * are called. */ - this.matterServer = new MatterServer(storageManager, { mdnsAnnounceInterface: netAnnounceInterface }); + this.matterServer = new MatterServer(storageManager, { mdnsInterface: netInterface }); const commissioningServer = new CommissioningServer({ port, diff --git a/packages/matter-node.js-examples/src/examples/ComposedDeviceNode.ts b/packages/matter-node.js-examples/src/examples/ComposedDeviceNode.ts index b37ad26fae..d5d26cf523 100644 --- a/packages/matter-node.js-examples/src/examples/ComposedDeviceNode.ts +++ b/packages/matter-node.js-examples/src/examples/ComposedDeviceNode.ts @@ -114,7 +114,7 @@ class ComposedDevice { const productName = `node-matter OnOff-Bridge`; const productId = getIntParameter("productid") ?? deviceStorage.get("productid", 0x8000); - const netAnnounceInterface = getParameter("announceinterface"); + const netInterface = getParameter("netinterface"); const port = getIntParameter("port") ?? 5540; const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs()); @@ -139,7 +139,7 @@ class ComposedDevice { * are called. */ - this.matterServer = new MatterServer(storageManager, { mdnsAnnounceInterface: netAnnounceInterface }); + this.matterServer = new MatterServer(storageManager, { mdnsInterface: netInterface }); const commissioningServer = new CommissioningServer({ port, diff --git a/packages/matter-node.js-examples/src/examples/DeviceNode.ts b/packages/matter-node.js-examples/src/examples/DeviceNode.ts index 7af74eb9b0..99e846c0ee 100644 --- a/packages/matter-node.js-examples/src/examples/DeviceNode.ts +++ b/packages/matter-node.js-examples/src/examples/DeviceNode.ts @@ -124,7 +124,7 @@ class Device { const productName = `node-matter OnOff ${isSocket ? "Socket" : "Light"}`; const productId = getIntParameter("productid") ?? deviceStorage.get("productid", 0x8000); - const netAnnounceInterface = getParameter("announceinterface"); + const netInterface = getParameter("netinterface"); const port = getIntParameter("port") ?? 5540; const uniqueId = getIntParameter("uniqueid") ?? deviceStorage.get("uniqueid", Time.nowMs()); @@ -168,7 +168,7 @@ class Device { * are called. */ - this.matterServer = new MatterServer(storageManager, { mdnsAnnounceInterface: netAnnounceInterface }); + this.matterServer = new MatterServer(storageManager, { mdnsInterface: netInterface }); const commissioningServer = new CommissioningServer({ port, diff --git a/packages/matter-node.js-examples/src/examples/MultiDeviceNode.ts b/packages/matter-node.js-examples/src/examples/MultiDeviceNode.ts index e0e55816e1..f9b03d718e 100644 --- a/packages/matter-node.js-examples/src/examples/MultiDeviceNode.ts +++ b/packages/matter-node.js-examples/src/examples/MultiDeviceNode.ts @@ -96,7 +96,7 @@ class Device { * and easy reuse. When you also do that be careful to not overlap with Matter-Server own contexts * (so maybe better not ;-)). */ - const netAnnounceInterface = getParameter("announceinterface"); + const netInterface = getParameter("netinterface"); const deviceStorage = storageManager.createContext("Device"); @@ -113,7 +113,7 @@ class Device { * are called. */ - this.matterServer = new MatterServer(storageManager, { mdnsAnnounceInterface: netAnnounceInterface }); + this.matterServer = new MatterServer(storageManager, { mdnsInterface: netInterface }); /** * Create Device instance and add needed Listener diff --git a/packages/matter.js/src/MatterServer.ts b/packages/matter.js/src/MatterServer.ts index 175058b433..5da03aaf6b 100644 --- a/packages/matter.js/src/MatterServer.ts +++ b/packages/matter.js/src/MatterServer.ts @@ -34,8 +34,16 @@ export type MatterServerOptions = { /** * Interface to use for MDNS announcements. If not provided announcements will be sent from all network interfaces + * TODO: Remove in later versions then 0.7 + * @deprecated */ mdnsAnnounceInterface?: string; + + /** + * Interface to use for MDNS announcements and scanning. If not provided announcements/scanning will be done on all + * network interfaces + */ + mdnsInterface?: string; }; /** @@ -180,11 +188,14 @@ export class MatterServer { if (this.mdnsBroadcaster === undefined) { this.mdnsBroadcaster = await MdnsBroadcaster.create({ enableIpv4: !this.ipv4Disabled, - multicastInterface: this.options?.mdnsAnnounceInterface, + multicastInterface: this.options?.mdnsInterface ?? this.options?.mdnsAnnounceInterface, }); } if (this.mdnsScanner === undefined) { - this.mdnsScanner = await MdnsScanner.create({ enableIpv4: !this.ipv4Disabled }); + this.mdnsScanner = await MdnsScanner.create({ + enableIpv4: !this.ipv4Disabled, + netInterface: this.options?.mdnsInterface, + }); } // TODO the mdns classes will later be in this class and assigned differently!! for (const node of this.nodes) {