-
Notifications
You must be signed in to change notification settings - Fork 7
/
EspIdfProvisioning.swift
347 lines (290 loc) · 13.6 KB
/
EspIdfProvisioning.swift
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// EspIdfProvisioning.swift
// react-native-esp-idf-provisioning
//
// Created by Mateo Gianolio on 2023-09-18.
//
import Foundation
import ESPProvision
@objc(EspIdfProvisioning)
class EspIdfProvisioning: NSObject {
// Think we need to keep a dictionary of espDevices since we can't pass native
// classes to react-native
private var espDevices: [String : ESPDevice] = [:]
private var softAPPasswords: [String : String] = [:]
@objc(searchESPDevices:transport:security:resolve:reject:)
func searchESPDevices(devicePrefix: String, transport: String, security: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let transport = ESPTransport(rawValue: transport) ?? ESPTransport.ble
let security = ESPSecurity(rawValue: security)
self.espDevices.removeAll()
var invoked = false
ESPProvisionManager.shared.searchESPDevices(devicePrefix: devicePrefix, transport: transport, security: security) { espDevices, error in
// Prevent multiple callback invokation error
guard !invoked else { return }
if error != nil {
reject("error", error?.description, nil)
invoked = true
return
}
espDevices?.forEach {
self.espDevices[$0.name] = $0
}
resolve(espDevices!.map {[
"name": $0.name,
"transport": $0.transport.rawValue,
"security": $0.security.rawValue,
]})
invoked = true
}
}
@objc(stopESPDevicesSearch)
func stopESPDevicesSearch() {
ESPProvisionManager.shared.stopESPDevicesSearch()
}
@objc(createESPDevice:transport:security:proofOfPossession:softAPPassword:username:resolve:reject:)
func createESPDevice(deviceName: String, transport: String, security: Int, proofOfPossession: String? = nil, softAPPassword: String? = nil, username: String? = nil, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let transport = ESPTransport(rawValue: transport) ?? ESPTransport.ble
let security = ESPSecurity(rawValue: security)
var invoked = false
ESPProvisionManager.shared.createESPDevice(deviceName: deviceName, transport: transport, security: security, proofOfPossession: proofOfPossession, softAPPassword: softAPPassword, username: username) { espDevice, error in
// Prevent multiple callback invokation error
guard !invoked else { return }
if error != nil {
reject("error", error?.description, nil)
invoked = true
return
}
self.softAPPasswords[espDevice!.name] = softAPPassword
self.espDevices[espDevice!.name] = espDevice
resolve([
"name": espDevice!.name,
"transport": espDevice!.transport.rawValue,
"security": espDevice!.security.rawValue,
])
invoked = true
}
}
@objc(connect:resolve:reject:)
func connect(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
var invoked = false
espDevice.connect(completionHandler: { status in
// Prevent multiple callback invokation error
guard !invoked else { return }
switch status {
case .connected:
resolve([
"status": "connected"
])
invoked = true
case .failedToConnect(let error):
reject("error", error.description, nil)
invoked = true
case .disconnected:
reject("error", "Device disconnected.", nil)
invoked = true
}
})
}
@objc(sendData:path:data:resolve:reject:)
func sendData(deviceName: String, path: String, data: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
let data: Data = data.data(using: .utf8)!
if let data = Data(base64Encoded: data) {
var invoked = false
espDevice.sendData(path: path, data: data, completionHandler: { data, error in
// Prevent multiple callback invokation error
guard !invoked else { return }
if error != nil {
reject("error", error?.description, nil)
invoked = true
return
}
resolve(data!.base64EncodedString())
invoked = true
})
} else {
reject("error", "Data is not base64 encoded.", nil)
}
}
@objc(scanWifiList:resolve:reject:)
func scanWifiList(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
var invoked = false
espDevice.scanWifiList(completionHandler: { wifiList, error in
// Prevent multiple callback invokation error
guard !invoked else { return }
if error != nil {
// Ignore error as per https://github.com/orbital-systems/react-native-esp-idf-provisioning/issues/22
// and https://github.com/espressif/esp-idf-provisioning-ios/issues/74
return
}
resolve(wifiList!.map {[
"ssid": $0.ssid,
"bssid": $0.bssid.toHexString(),
"rssi": $0.rssi,
"auth": $0.auth.rawValue,
"channel": $0.channel
]})
invoked = true
})
}
@objc(disconnect:)
func disconnect(deviceName: String) {
self.espDevices[deviceName]?.disconnect()
}
@objc(provision:ssid:passphrase:resolve:reject:)
func provision(deviceName: String, ssid: String, passphrase: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
var invoked = false
espDevice.provision(ssid: ssid, passPhrase: passphrase, completionHandler: { status in
// Prevent multiple callback invokation error
guard !invoked else { return }
switch status {
case .success:
resolve([
"status": "success"
])
invoked = true
case .failure(let error):
reject("error", error.description, nil)
invoked = true
case .configApplied:
break
}
})
}
@objc(getProofOfPossession:resolve:reject:)
func getProofOfPossession(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
var invoked = false
espDevice.delegate?.getProofOfPossesion(forDevice: espDevice, completionHandler: { proofOfPossession in
// Prevent multiple callback invokation error
guard !invoked else { return }
resolve(proofOfPossession)
invoked = true
})
}
@objc(setProofOfPossession:proofOfPossession:resolve:reject:)
func setProofOfPossession(deviceName: String, proofOfPossession: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
// Need to recreate the device to set proof of possession
createESPDevice(deviceName: deviceName, transport: espDevice.transport.rawValue, security: espDevice.security.rawValue, proofOfPossession: proofOfPossession, softAPPassword: softAPPasswords[deviceName], username: espDevice.username, resolve: resolve, reject: reject)
}
@objc(getUsername:resolve:reject:)
func getUsername(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
var invoked = false
espDevice.delegate?.getUsername(forDevice: espDevice, completionHandler: { userName in
// Prevent multiple callback invokation error
guard !invoked else { return }
resolve(userName)
invoked = true
})
}
@objc(setUsername:username:resolve:reject:)
func setUsername(deviceName: String, username: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
espDevice.username = username
}
@objc(getDeviceName:resolve:reject:)
func getDeviceName(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
resolve(espDevice.name)
}
@objc(setDeviceName:newDeviceName:resolve:reject:)
func setDeviceName(deviceName: String, newDeviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
// No-op: Can't change device name on iOS
resolve(newDeviceName)
}
@objc(getPrimaryServiceUuid:resolve:reject:)
func getPrimaryServiceUuid(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
// No-op: primaryServiceUuid does not exist on iOS
resolve("")
}
@objc(setPrimaryServiceUuid:primaryServiceUuid:resolve:reject:)
func setPrimaryServiceUuid(deviceName: String, primaryServiceUuid: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
// No-op: primaryServiceUuid does not exist on iOS
resolve("")
}
@objc(getSecurityType:resolve:reject:)
func getSecurityType(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
resolve(espDevice.security.rawValue)
}
@objc(setSecurityType:security:resolve:reject:)
func setSecurityType(deviceName: String, security: Int, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
let security = ESPSecurity(rawValue: security)
espDevice.security = security
}
@objc(getTransportType:resolve:reject:)
func getTransportType(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
resolve(espDevice.transport.rawValue)
}
@objc(getVersionInfo:resolve:reject:)
func getVersionInfo(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
resolve(espDevice.versionInfo)
}
@objc(getDeviceCapabilities:resolve:reject:)
func getDeviceCapabilities(deviceName: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
guard let espDevice = self.espDevices[deviceName] else {
reject("error", "No ESP device found. Call createESPDevice first.", nil)
return
}
resolve(espDevice.capabilities)
}
}