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

Add discovery over MDNS for commission #15

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions src/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,22 @@ const CertificateDeclaration = Buffer.from("3082021906092a864886f70d010702a08202

class Main {
async start() {
(await MatterServer.create())
const deviceName = "Matter test device";
const deviceType = 257 /* Dimmable bulb */;
const vendorName = "node-matter";
const vendorId = 0xFFF1;
const productName = "Matter test device";
const productId = 0X8001;
const discriminator = 3840;
(await MatterServer.create(deviceName, deviceType, vendorId, productId, discriminator))
.addChannel(new UdpChannel(5540))
.addProtocolHandler(Protocol.SECURE_CHANNEL, new SecureChannelHandler(
new PasePairing(20202021, { iteration: 1000, salt: Crypto.getRandomData(32) }),
new CasePairing(),
))
.addProtocolHandler(Protocol.INTERACTION_MODEL, new InteractionProtocol(new Device([
new Endpoint(0x00, "MA-rootdevice", [
new BasicCluster({ vendorName: "node-matter", vendorId: 0xFFF1, productName: "Matter test device", productId: 0X8001 }),
new BasicCluster({ vendorName, vendorId, productName, productId }),
new GeneralCommissioningCluster(),
new OperationalCredentialsCluster({devicePrivateKey: DevicePrivateKey, deviceCertificate: DeviceCertificate, deviceIntermediateCertificate: ProductIntermediateCertificate, certificateDeclaration: CertificateDeclaration}),
]),
Expand Down
45 changes: 45 additions & 0 deletions src/mdns/MatterMdnsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/

import { ARecord, PtrRecord, SrvRecord, TxtRecord } from "../codec/DnsCodec";
import { Crypto } from "../crypto/Crypto";
import { Fabric } from "../fabric/Fabric";
import { MdnsServer } from "./MdnsServer";

const SERVICE_DISCOVERY_QNAME = "_services._dns-sd._udp.local";
const MATTER_COMMISSION_SERVICE_QNAME = "_matterc._udp.local";
const MATTER_SERVICE_QNAME = "_matter._tcp.local";

export class MatterMdnsServer {
Expand All @@ -21,6 +23,49 @@ export class MatterMdnsServer {
private readonly mdnsServer: MdnsServer,
) {}

addRecordsForCommission(deviceName: string, deviceType: number, vendorId: number, productId: number, discriminator: number) {
const shortDiscriminator = (discriminator >> 8) & 0x0F;
const instanceId = Crypto.getRandomData(8).toString("hex").toUpperCase();
const vendorQname = `_V${vendorId}._sub.${MATTER_COMMISSION_SERVICE_QNAME}`;
const deviceTypeQname = `_T${deviceType}._sub.${MATTER_COMMISSION_SERVICE_QNAME}`;
const shortDiscriminatorQname = `_S${shortDiscriminator}._sub.${MATTER_COMMISSION_SERVICE_QNAME}`;
const longDiscriminatorQname = `_L${discriminator}._sub.${MATTER_COMMISSION_SERVICE_QNAME}`;
const commissionModeQname = `_CM._sub.${MATTER_COMMISSION_SERVICE_QNAME}`;
const deviceQname = `${instanceId}.${MATTER_COMMISSION_SERVICE_QNAME}`;
this.mdnsServer.setRecordsGenerator((ip, mac) => {
const hostname = mac.replace(/:/g, "").toUpperCase() + "0000.local";
return [
PtrRecord(SERVICE_DISCOVERY_QNAME, MATTER_COMMISSION_SERVICE_QNAME),
PtrRecord(SERVICE_DISCOVERY_QNAME, vendorQname),
PtrRecord(SERVICE_DISCOVERY_QNAME, deviceTypeQname),
PtrRecord(SERVICE_DISCOVERY_QNAME, shortDiscriminatorQname),
PtrRecord(SERVICE_DISCOVERY_QNAME, longDiscriminatorQname),
PtrRecord(SERVICE_DISCOVERY_QNAME, commissionModeQname),
PtrRecord(MATTER_COMMISSION_SERVICE_QNAME, deviceQname),
PtrRecord(vendorQname, deviceQname),
PtrRecord(deviceTypeQname, deviceQname),
PtrRecord(shortDiscriminatorQname, deviceQname),
PtrRecord(longDiscriminatorQname, deviceQname),
PtrRecord(commissionModeQname, deviceQname),
SrvRecord(deviceQname, {priority: 0, weight: 0, port: 5540, target: hostname }),
ARecord(hostname, ip),
// TODO: support IPv6
TxtRecord(deviceQname, [
`VP=${vendorId}+${productId}`, /* Vendor / Product */
`DT=${deviceType}`, /* Device Type */
`DN=${deviceName}`, /* Device Name */
"SII=5000", /* Sleepy Idle Interval */
"SAI=300", /* Sleepy Active Interval */
"T=1", /* TCP supported */
`D=${discriminator}`, /* Discriminator */
"CM=1", /* Commission Mode */
"PH=33", /* Pairing Hint */
"PI=", /* Pairing Instruction */
]),
];
});
}

addRecordsForFabric(fabric: Fabric) {
const nodeIdBuffer = Buffer.alloc(8);
nodeIdBuffer.writeBigUInt64BE(BigInt(fabric.nodeId));
Expand Down
7 changes: 5 additions & 2 deletions src/server/MatterServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export interface ProtocolHandler {
}

export class MatterServer {
static async create() {
return new MatterServer(await MatterMdnsServer.create());
static async create(deviceName: string, deviceType: number, vendorId: number, productId: number, discriminator: number) {
const mdnsServer = await MatterMdnsServer.create();
mdnsServer.addRecordsForCommission(deviceName, deviceType, vendorId, productId, discriminator);
return new MatterServer(mdnsServer);
}

constructor(
Expand All @@ -59,6 +61,7 @@ export class MatterServer {

start() {
this.channels.forEach(channel => channel.bind((socket, data) => this.onMessage(socket, data)));
this.mdnsServer.announce();
}

getMdnsServer() {
Expand Down