-
Notifications
You must be signed in to change notification settings - Fork 69
/
DummyWifiNetworkCommissioningServer.ts
160 lines (133 loc) · 6.13 KB
/
DummyWifiNetworkCommissioningServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @license
* Copyright 2022-2024 Matter.js Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { Bytes } from "@matter/main";
import { GeneralCommissioningBehavior } from "@matter/main/behaviors/general-commissioning";
import { NetworkCommissioningBehavior } from "@matter/main/behaviors/network-commissioning";
import { NetworkCommissioning } from "@matter/main/clusters/network-commissioning";
import { DeviceAdvertiser, DeviceCommissioner } from "@matter/main/protocol";
const firstNetworkId = new Uint8Array(32);
/**
* This represents a Dummy version of a Wifi Network Commissioning Cluster Server without real Wifi related logic, beside
* returning some values provided as CLI parameters. This dummy implementation is only there for tests/as showcase for BLE
* commissioning of a device.
*/
export class DummyWifiNetworkCommissioningServer extends NetworkCommissioningBehavior.with(
NetworkCommissioning.Feature.WiFiNetworkInterface,
) {
override scanNetworks({ ssid, breadcrumb }: NetworkCommissioning.ScanNetworksRequest) {
console.log(
`---> scanNetworks called on NetworkCommissioning cluster: ${ssid ? Bytes.toHex(ssid) : undefined} ${breadcrumb}`,
);
// Simulate successful scan
if (breadcrumb !== undefined) {
const generalCommissioningCluster = this.agent.get(GeneralCommissioningBehavior);
generalCommissioningCluster.state.breadcrumb = breadcrumb;
}
const networkingStatus = NetworkCommissioning.NetworkCommissioningStatus.Success;
this.state.lastNetworkingStatus = networkingStatus;
return {
networkingStatus,
wiFiScanResults: [
{
security: {
unencrypted: false,
wep: false,
wpaPersonal: false,
wpa2Personal: true,
wpa3Personal: true,
},
ssid: ssid || Bytes.fromString(this.env.vars.get("ble.wifi.scanSsid") ?? "TestSSID"), // Set a valid existing local Wi-Fi SSID here
bssid: Bytes.fromString(this.env.vars.get("ble.wifi.scanBssid") ?? "00:00:00:00:00:00"),
channel: 1,
},
],
};
}
override addOrUpdateWiFiNetwork({
ssid,
credentials,
breadcrumb,
}: NetworkCommissioning.AddOrUpdateWiFiNetworkRequest) {
console.log(
`---> addOrUpdateWiFiNetwork called on NetworkCommissioning cluster: ${Bytes.toHex(ssid)} ${Bytes.toHex(credentials)} ${breadcrumb}`,
);
this.env
.get(DeviceCommissioner)
.assertFailsafeArmed("Failsafe timer needs to be armed to add or update networks.");
// Simulate successful add or update
if (breadcrumb !== undefined) {
const generalCommissioningCluster = this.agent.get(GeneralCommissioningBehavior);
generalCommissioningCluster.state.breadcrumb = breadcrumb;
}
const networkingStatus = NetworkCommissioning.NetworkCommissioningStatus.Success;
this.state.lastNetworkingStatus = networkingStatus;
this.state.lastNetworkId = firstNetworkId;
return {
networkingStatus,
networkIndex: 0,
};
}
override removeNetwork({ networkId, breadcrumb }: NetworkCommissioning.RemoveNetworkRequest) {
console.log(
`---> removeNetwork called on NetworkCommissioning cluster: ${Bytes.toHex(networkId)} ${breadcrumb}`,
);
this.env
.get(DeviceCommissioner)
.assertFailsafeArmed("Failsafe timer needs to be armed to add or update networks.");
// Simulate successful add or update
if (breadcrumb !== undefined) {
const generalCommissioningCluster = this.agent.get(GeneralCommissioningBehavior);
generalCommissioningCluster.state.breadcrumb = breadcrumb;
}
const networkingStatus = NetworkCommissioning.NetworkCommissioningStatus.Success;
this.state.lastNetworkingStatus = networkingStatus;
this.state.lastNetworkId = firstNetworkId;
return {
networkingStatus,
networkIndex: 0,
};
}
override async connectNetwork({ networkId, breadcrumb }: NetworkCommissioning.ConnectNetworkRequest) {
console.log(
`---> connectNetwork called on NetworkCommissioning cluster: ${Bytes.toHex(networkId)} ${breadcrumb}`,
);
this.env
.get(DeviceCommissioner)
.assertFailsafeArmed("Failsafe timer needs to be armed to add or update networks.");
// Simulate successful connection
if (breadcrumb !== undefined) {
const generalCommissioningCluster = this.agent.get(GeneralCommissioningBehavior);
generalCommissioningCluster.state.breadcrumb = breadcrumb;
}
this.state.networks[0].connected = true;
const networkingStatus = NetworkCommissioning.NetworkCommissioningStatus.Success;
this.state.lastNetworkingStatus = networkingStatus;
this.state.lastNetworkId = firstNetworkId;
this.state.lastConnectErrorValue = null;
// Announce operational in IP network
await this.env.get(DeviceAdvertiser).startAdvertising();
return {
networkingStatus,
errorValue: null,
};
}
override reorderNetwork({ networkId, networkIndex, breadcrumb }: NetworkCommissioning.ReorderNetworkRequest) {
console.log(
`---> reorderNetwork called on NetworkCommissioning cluster: ${Bytes.toHex(networkId)} ${networkIndex} ${breadcrumb}`,
);
// Simulate successful connection
if (breadcrumb !== undefined) {
const generalCommissioningCluster = this.agent.get(GeneralCommissioningBehavior);
generalCommissioningCluster.state.breadcrumb = breadcrumb;
}
const networkingStatus = NetworkCommissioning.NetworkCommissioningStatus.Success;
this.state.lastNetworkingStatus = networkingStatus;
return {
networkingStatus,
networkIndex: 0,
};
}
}