-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi-device.js
241 lines (226 loc) · 6.21 KB
/
wifi-device.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const DeviceOSProtobuf = require('@particle/device-os-protobuf');
const { definitions: proto } = require('@particle/device-os-protobuf');
const { fromProtobufEnum } = require('./protobuf-util');
const { convertBufferToMacAddress } = require('./address-util');
/**
* Wi-Fi security types.
*
* @enum {String}
*/
const WiFiSecurity = fromProtobufEnum(proto.wifi.Security, {
NONE : 'NO_SECURITY',
WEP : 'WEP',
WPA_PSK : 'WPA_PSK',
WPA2_PSK : 'WPA2_PSK',
WPA_WPA2_PSK : 'WPA_WPA2_PSK',
WPA3_PSK : 'WPA3_PSK',
WPA2_WPA3_PSK : 'WPA2_WPA3_PSK',
});
/**
* Wi-Fi device.
*
* This class is not meant to be instantiated directly. Use {@link getDevices} and
* {@link openDeviceById} to create device instances.
*
* @mixin
*/
const WifiDevice = base => class extends base {
/**
* Perform WiFi scan for Gen 3+ devices
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {Object} options See sendControlRequest(), same options are here.
* @return {Promise[Object]} - Each object in array has these properties: ssid, bssid, security, channel, rssi. See Network protobuf message from https://github.com/particle-iot/device-os-protobuf for more details.
*/
async scanWifiNetworks(options) {
const result = await this.sendProtobufRequest(
'wifi.ScanNetworksRequest',
{},
options
);
return result.networks.map((network) => {
return {
ssid: network.ssid || null, // can be blank for hidden networks
bssid: convertBufferToMacAddress(network.bssid), // convert buffer to hex string
security: WiFiSecurity.fromProtobuf(network.security),
channel: network.channel,
rssi: network.rssi
};
});
}
/**
* Join a new WiFi network for Gen 3+ devices.
*
* Warning: May not work for hidden networks due to certain bugs in the device-os.
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {string} ssid - SSID of Wifi Network
* @param {string} security - Security of Wifi network
* @param {string} password - Password of Wifi network, if not set will not use security
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - empty response
*/
async joinNewWifiNetwork({ ssid, security, password = null, hidden }, options) {
let dataPayload;
if (password === null) {
dataPayload = {
ssid,
bssid: null,
security: 0 // Security.NO_SECURITY
};
} else {
dataPayload = {
ssid,
bssid: null,
hidden: hidden === true,
security: security ? WiFiSecurity.toProtobuf(security) : null,
credentials: {
type: 1, // CredentialsType.PASSWORD
password
},
};
}
return await this.sendProtobufRequest(
'wifi.JoinNewNetworkRequest',
dataPayload,
options
);
}
/**
* Join a known WiFi network for Gen 3+ devices.
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {string} ssid - SSID of Wifi Network
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - empty response
*/
async joinKnownWifiNetwork({ ssid }, options) {
return await this.sendProtobufRequest(
'wifi.JoinKnownNetworkRequest',
{ ssid },
options
);
}
/**
* Gets the list of networks for Gen 3+ devices.
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - An array of known networks (ssid, security, credentialsType). See GetKnownNetworksReply from https://github.com/particle-iot/device-os-protobuf/blob/main/control/wifi_new.proto
*/
async listWifiNetworks(options) {
return await this.sendProtobufRequest(
'wifi.GetKnownNetworksRequest',
options
);
}
/**
* Removes a Wi-Fi network
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {string} ssid - SSID of Wifi Network
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - empty response
*/
async removeWifiNetwork({ ssid }, options) {
const dataPayload = {
ssid
};
return await this.sendProtobufRequest(
'wifi.RemoveKnownNetworkRequest',
dataPayload,
options
);
}
/**
* Gets the currently connected Wi-Fi network for Gen 3+ devices.
*
* Supported platforms:
* - Gen 3
* - Gen 4
*
* @param {Object} options See sendControlRequest(), same options are here.
* @return {Promise[Object]} - ssid, bssid, channel, rssi. See GetCurrentNetworkReply from https://github.com/particle-iot/device-os-protobuf/blob/main/control/wifi_new.proto
*/
async getCurrentWifiNetwork(options) {
const res = await this.sendProtobufRequest(
'wifi.GetCurrentNetworkRequest',
{},
options
);
const bssidStr = convertBufferToMacAddress(res.bssid);
res.bssid = bssidStr;
return res;
}
/**
* Set a new WiFi network for Gen 3+ devices.
*
* Supported platforms:
* - Gen 4: Supported on P2 since Device OS 5.8.2
*
* @param {string} ssid - SSID of Wifi Network
* @param {string} security - Security of Wifi network
* @param {string} password - Password of Wifi network, if not set will not use security
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - empty response
*/
async setWifiCredentials({ ssid, security, password = null, hidden }, options) {
let dataPayload;
if (password === null) {
dataPayload = {
ssid,
bssid: null,
security: 0 // Security.NO_SECURITY
};
} else {
dataPayload = {
ssid,
bssid: null,
hidden: hidden === true,
security : security ? WiFiSecurity.toProtobuf(security) : null,
credentials: {
type: 1, // CredentialsType.PASSWORD
password
},
};
}
return await this.sendProtobufRequest(
'wifi.SetNetworkCredentialsRequest',
dataPayload,
options
);
}
/**
* Clear Wifi networks for Gen 3+ devices
*
* @param {Object} options See sendControlRequest(), same options are here.
* @return {ProtobufInteraction} - empty response
*/
async clearWifiNetworks(options) {
return await this.sendProtobufRequest(
'wifi.ClearKnownNetworksRequest',
{},
options
);
}
};
const WifiSecurityEnum = DeviceOSProtobuf.getDefinition('wifi.Security').message;
module.exports = {
WifiDevice,
WifiSecurityEnum
};