-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathDFUPeripheral.swift
More file actions
795 lines (679 loc) · 30.7 KB
/
Copy pathDFUPeripheral.swift
File metadata and controls
795 lines (679 loc) · 30.7 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
* Copyright (c) 2019, Nordic Semiconductor
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import CoreBluetooth
internal protocol BaseDFUPeripheralAPI: AnyObject, DFUController {
/**
This method starts DFU process for given peripheral.
If the peripheral is not connected it will call the `connect()` method,
if it is connected, but services were not discovered before, it will try to
discover services instead.
If services were already discovered the DFU process will be started.
*/
func start()
/**
This method reconnects to the same peripheral after it has disconnected.
*/
func reconnect()
/**
Disconnects the target device.
*/
func disconnect()
/**
This method breaks the cyclic reference and both DFUExecutor and
DFUPeripheral may be released.
*/
func destroy()
/**
This method should reset the device, preferably switching it to application
mode.
*/
func resetDevice()
}
internal class BaseDFUPeripheral<TD : BasePeripheralDelegate> : NSObject, BaseDFUPeripheralAPI, CBPeripheralDelegate, CBCentralManagerDelegate {
/// Bluetooth Central Manager used to scan for the peripheral.
internal let centralManager: CBCentralManager
internal let targetIdentifier: UUID
internal let queue: DispatchQueue
/// The DFU Target peripheral.
internal var peripheral: CBPeripheral?
/// The peripheral delegate.
internal var delegate: TD?
/// The optional logger delegate.
internal let logger: LoggerHelper
/// A list of services required to be found on the peripheral.
/// May return nil - then all services will be discovered.
internal var requiredServices: [CBUUID]? {
// We have to find all services, not only those related to DFU.
// This is required in case the target device was created using
// SDK 6.0 or 6.1, where there was no DFU Version characteristic.
// In that case, this DFU library determines whether to jump to
// bootloader, or proceed with DFU based on number of services found.
// We have to find all of them. It is not necessary for newer
// firmwares (SDK 7+) or for Secure DFU where the code below could work.
return nil
/*
// If the experimental feature was enabled
if experimentalButtonlessServiceInSecureDfuEnabled {
return [uuidHelper.legacyDFUService, uuidHelper.secureDFUService, uuidHelper.buttonlessExperimentalService]
}
// By default only standard Secure and Legacy DFU services will be
// discovered.
return [uuidHelper.legacyDFUService, uuidHelper.secureDFUService]
*/
}
/// A flag indicating whether the experimental Buttonless DFU Service in
/// Secure DFU is supported.
internal let experimentalButtonlessServiceInSecureDfuEnabled: Bool
/// Default error callback.
internal var defaultErrorCallback: ErrorCallback {
return { [weak self] error, message in
self?.delegate?.error(error, didOccurWithMessage: message)
}
}
/// UUIDs for Service/Characteristics.
internal var uuidHelper: DFUUuidHelper
/// A flag set when upload has been aborted.
fileprivate var aborted: Bool = false
/// Connection timer cancels connection attempt if the device doesn't
/// connect before the time runs out.
fileprivate var connectionTimer: DispatchSourceTimer?
/// Connection timeout.
/// - since: 4.8.0
fileprivate let connectionTimeout: TimeInterval
/// Whether new address is expected in Legacy DFU Bootloader.
/// - seeAlso: `DFUServiceInitiator.forceScanningForNewAddressInLegacyDfu`
/// - since: 4.9.0
fileprivate let forceScanningForNewAddressInLegacyDfu: Bool
init(_ initiator: DFUServiceInitiator, _ logger: LoggerHelper) {
self.centralManager = initiator.centralManager
self.targetIdentifier = initiator.targetIdentifier
self.queue = initiator.queue
self.logger = logger
self.experimentalButtonlessServiceInSecureDfuEnabled = initiator.enableUnsafeExperimentalButtonlessServiceInSecureDfu
self.uuidHelper = initiator.uuidHelper
self.connectionTimeout = initiator.connectionTimeout
self.forceScanningForNewAddressInLegacyDfu = initiator.forceScanningForNewAddressInLegacyDfu
super.init()
}
// MARK: - Base DFU Peripheral API
func start() {
aborted = false
centralManager.delegate = self
if centralManager.state != .poweredOn {
// Central manager not ready. Wait for poweredOn state.
return
}
// Set the initial peripheral. It may be changed later (flashing App fw
// after first flashing SD/BL).
guard let peripheral = centralManager
.retrievePeripherals(withIdentifiers: [targetIdentifier]).first else {
delegate?.error(.bluetoothDisabled, didOccurWithMessage:
"Could not obtain peripheral instance")
return
}
self.peripheral = peripheral
switch peripheral.state {
case .connected:
let name = peripheral.name ?? "Unknown device"
logger.i("Connected to \(name)")
delegate?.peripheralDidConnect()
if let dfuService = findDfuService(in: peripheral.services) {
// A DFU service was found, congratulations!
logger.i("Services discovered")
peripheralDidDiscoverDfuService(dfuService)
} else {
// DFU service has not been found, but it doesn't matter it's not
// there. Perhaps the user's application didn't discover it.
// Let's discover DFU services.
discoverServices()
}
default:
connect()
}
}
func reconnect() {
guard let peripheral = peripheral, peripheral.state != .connected else { return }
connect()
}
func disconnect() {
guard let peripheral = peripheral else { return }
if peripheral.state == .connected {
logger.v("Disconnecting...")
} else {
logger.v("Cancelling connection...")
}
logger.d("centralManager.cancelPeripheralConnection(peripheral)")
centralManager.cancelPeripheralConnection(peripheral)
}
func destroy() {
centralManager.delegate = nil
peripheral?.delegate = nil
// Peripheral can't be cleared here to make restart() possible.
// See https://github.com/NordicSemiconductor/IOS-Pods-DFU-Library/issues/269
// peripheral = nil
delegate = nil
}
func resetDevice() {
if let peripheral = peripheral, peripheral.state != .disconnected {
disconnect()
} else {
peripheralDidDisconnect()
}
}
// MARK: - DFU Controller API
func pause() -> Bool {
// BaseDFUPeripheral does not support pausing or resuming.
return false
}
func resume() -> Bool {
// BaseDFUPeripheral does not support pausing or resuming.
return false
}
func abort() -> Bool {
aborted = true
if peripheral?.state == .connecting || peripheral?.state == .connected {
disconnect()
}
return true
}
// MARK: - Central Manager methods
func centralManagerDidUpdateState(_ central: CBCentralManager) {
var stateAsString: String
switch (central.state) {
case .poweredOn:
stateAsString = "Powered ON"
case .poweredOff:
stateAsString = "Powered OFF"
case .resetting:
stateAsString = "Resetting"
case .unauthorized:
stateAsString = "Unauthorized"
case .unsupported:
stateAsString = "Unsupported"
default:
stateAsString = "Unknown"
}
logger.d("[Callback] Central Manager did update state to: \(stateAsString)")
switch central.state {
case .poweredOn:
// We are now ready to rumble!
start()
default:
// The device has been already disconnected if it was connected.
delegate?.error(.bluetoothDisabled, didOccurWithMessage: "Bluetooth adapter powered off")
destroy()
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
guard peripheral.isEqual(self.peripheral) else {
return
}
cleanUp()
logger.d("[Callback] Central Manager did connect peripheral")
let name = peripheral.name ?? "Unknown device"
logger.i("Connected to \(name)")
guard !aborted else {
resetDevice()
return
}
delegate?.peripheralDidConnect()
discoverServices()
}
func centralManager(_ central: CBCentralManager,
didFailToConnect peripheral: CBPeripheral, error: Error?) {
guard peripheral.isEqual(self.peripheral) else {
return
}
cleanUp()
if let error = error {
logger.d("[Callback] Central Manager did fail to connect peripheral")
logger.e(error)
} else {
logger.d("[Callback] Central Manager did fail to connect peripheral without error")
}
logger.e("Device failed to connect")
delegate?.peripheralDidFailToConnect()
}
func centralManager(_ central: CBCentralManager,
didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
guard peripheral.isEqual(self.peripheral) else {
return
}
let wasConnected = connectionTimer == nil
cleanUp() // This clears the connectionTimer.
if !wasConnected {
logger.e("[Callback] Central Manager failed to connect to peripheral (timeout)")
delegate?.peripheralDidFailToConnect()
} else if let error = error {
// We may expect an error with
// code = 7: "The specified device has disconnected from us." (graceful disconnect),
// or
// code = 6: "The connection has timed out unexpectedly." (in case it disconnected
// before sending the ACK).
if let cbError = error as? CBError,
cbError.code == CBError.connectionTimeout ||
cbError.code == CBError.peripheralDisconnected {
logger.d("[Callback] Central Manager did disconnect peripheral")
logger.i("Disconnected by the remote device")
peripheralDidDisconnect()
} else {
logger.e("[Callback] Central Manager did disconnect peripheral with error: \(error.localizedDescription)")
logger.i("Disconnected")
logger.e(error)
delegate?.peripheralDidDisconnect(withError: error)
}
} else {
logger.d("[Callback] Central Manager did disconnect peripheral")
logger.i("Disconnected")
peripheralDidDisconnect()
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any], rssi RSSI: NSNumber) {
// This empty method has to be here, otherwise the BaseCommonDFUPeripheral does
// not get this callback
// Don't use central manager while DFU is in progress!
print("DFU in progress, don't use this CentralManager instance!")
central.stopScan()
}
// MARK: - Peripheral Delegate methods
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let error = error {
logger.e("Services discovery failed")
logger.e(error)
delegate?.error(.serviceDiscoveryFailed, didOccurWithMessage: "Services discovery failed")
return
}
logger.i("Services discovered")
guard !aborted else {
resetDevice()
return
}
// Search for DFU service
guard let dfuService = findDfuService(in: peripheral.services) else {
logger.e("DFU Service not found")
// Log what was found in case of an error
if let services = peripheral.services, services.isEmpty == false {
logger.d("The following services were discovered:")
services.forEach { service in
logger.d(" - \(service.uuid.uuidString)")
}
} else {
logger.d("No services found")
}
logger.d("Did you connect to the correct target? It might be that the previous services were cached: toggle Bluetooth from iOS settings to clear cache. Also, ensure the device contains the Service Changed characteristic")
// The device does not support DFU, nor buttonless jump
delegate?.error(.deviceNotSupported, didOccurWithMessage: "DFU Service not found")
return
}
// A DFU service was found, congratulations!
peripheralDidDiscoverDfuService(dfuService)
}
// MARK: - Methods to be overridden in the final implementation
/**
Method called when a DFU service has been found.
- parameter service: The service that has been found.
*/
func peripheralDidDiscoverDfuService(_ service: CBService) {
fatalError("This method must be overridden")
}
/**
Method called when the device got disconnected.
*/
func peripheralDidDisconnect() {
guard !aborted else {
// The device has reset. Notify user.
logger.w("Upload aborted")
delegate?.peripheralDidDisconnectAfterAborting()
return
}
// Notify the delegate about the disconnection.
// Most probably an error occurred and will be reported to the user.
delegate?.peripheralDidDisconnect()
}
// MARK: - Private methods
/**
Looks for a DFU Service in given list of services.
- returns: A `DFUService` type if a DFU service has been found, or `nil` if
services are `nil` or the list does not contain any supported DFU Service.
*/
private func findDfuService(in services: [CBService]?) -> CBService? {
return services?.first { service in
// The experimental Buttonless DFU Service for Secure DFU has been found.
// Skip the experimental Buttonless DFU Service if this feature wasn't enabled.
(experimentalButtonlessServiceInSecureDfuEnabled &&
service.matches(uuid: uuidHelper.buttonlessExperimentalService)) ||
// Secure DFU Service has been found.
service.matches(uuid: uuidHelper.secureDFUService) ||
// Legacy DFU Service has been found.
service.matches(uuid: uuidHelper.legacyDFUService)
}
}
/**
Starts the service discovery.
*/
private func discoverServices() {
if let peripheral = peripheral {
// Discover DFU service on the device to determine the DFU implementation.
logger.v("Discovering services...")
if let services = requiredServices {
logger.d("peripheral.discoverServices(\(services))")
} else {
logger.d("peripheral.discoverServices(nil)")
}
peripheral.delegate = self
peripheral.discoverServices(requiredServices)
} else {
logger.e("Unable to discover services: peripheral is nil. Is Bluetooth enabled?")
delegate?.error(.serviceDiscoveryFailed, didOccurWithMessage: "Peripheral is nil")
resetDevice()
}
}
/**
Connects to the peripheral and performs service discovery.
*/
fileprivate func connect() {
connect(withTimeout: connectionTimeout)
}
/**
Connects to the peripheral and performs service discovery.
If the connection fails before the time runs out, the optional timeout
handler will be called.
- parameters:
- timeout: The connection timeout.
- timeoutHandler: An optional callback, which will be executed on timeout.
*/
fileprivate func connect(withTimeout timeout: TimeInterval, timeoutHandler: (() -> ())? = nil) {
guard let peripheral = peripheral else {
return
}
let name = peripheral.name ?? "Unknown device"
logger.v("Connecting to \(name)...")
// Set a connection timer.
connectionTimer = DispatchSource.makeTimerSource()
connectionTimer?.setEventHandler { [weak self] in
if let self = self, let peripheral = self.peripheral {
self.connectionTimer?.cancel()
self.logger.w("Connection timeout!")
self.logger.d("centralManager.cancelPeripheralConnection(peripheral)")
self.centralManager.cancelPeripheralConnection(peripheral)
timeoutHandler?()
}
}
connectionTimer?.schedule(deadline: .now() + timeout)
connectionTimer?.resume()
logger.d("centralManager.connect(peripheral, options: nil)")
centralManager.connect(peripheral, options: nil)
}
fileprivate func cleanUp() {
connectionTimer?.cancel()
connectionTimer = nil
}
}
internal protocol DFUPeripheralAPI : BaseDFUPeripheralAPI {
/**
Checks whether the target device is in application mode and must be switched
to the DFU mode.
- parameter forceDfu: Should the service assume the device is in DFU Bootloader
mode when DFU Version characteristic does not exist and at
least one other service has been found on the device.
- returns: `True` if device needs to perform buttonless jump to DFU Bootloader mode.
*/
func isInApplicationMode(_ forceDfu: Bool) -> Bool
/**
Scans for a next device to connect to. When device is found and selected, it
connects to it.
After updating the SoftDevice the device may start advertising with an address
incremented by 1. A BLE scan needs to be done to find this new peripheral
(it's the same device, but as it advertises with a new address, from iOS point
of view it completely different device).
*/
func switchToNewPeripheralAndConnect()
/**
Returns whether the Init Packet is required by the target DFU device.
Init packet is required since DFU Bootloader version 0.5 (SDK 7.0.0).
- returns: `True` if init packet is required, `false` if not.
*/
func isInitPacketRequired() -> Bool
/// A flag set when a command to jump to DFU Bootloader has been sent.
var jumpingToBootloader: Bool { get set }
/// A flag set when a command to activate the new firmware and reset the device
/// has been sent.
var activating: Bool { get set }
/// A flag set when the library should try again connecting to the device
/// (it may be then in a correct state).
var shouldReconnect: Bool { get set }
/// A unique name that the bootloader will use in advertisement packets
/// (used since SDK 14).
var bootloaderName: String? { get set }
}
internal protocol DFUPeripheral : DFUPeripheralAPI {
associatedtype DFUServiceType : DFUService
/// Selector used to find the advertising peripheral in DFU Bootloader mode.
var peripheralSelector: DFUPeripheralSelectorDelegate { get }
/// The DFU Service instance. Not nil when found on the peripheral.
var dfuService: DFUServiceType? { get set }
}
internal class BaseCommonDFUPeripheral<TD : DFUPeripheralDelegate, TS : DFUService> : BaseDFUPeripheral<TD>, DFUPeripheral {
/// The peripheral selector instance specified in the initiator.
internal let peripheralSelector: DFUPeripheralSelectorDelegate
internal typealias DFUServiceType = TS
internal var dfuService: DFUServiceType?
/// This flag must be set to true if switching to bootloader mode is expected
/// after executing the next operation.
/// The operation is expected to reset the device. After the disconnect event
/// is received the service will try to connect back to the device, or scan
/// for a new device matching specified selector, depending on
/// `newAddressExpected` flag value.
internal var jumpingToBootloader : Bool = false
/// This flag must be set to true when the firmware upload is complete and
/// device will restart and run the new firmware after executing the next operation.
internal var activating : Bool = false
/// This flag has the same behavior as `jumpingToBootloader`, but it's used
/// when Invalid state error was received and a reset command will be executed.
/// The service will reconnect to the same device.
internal var shouldReconnect : Bool = false
/// This flag must be set to true if the device will advertise with a new
/// device address after it resets. The service will scan and use specified
/// peripheral selector in order to connect to the new peripheral.
internal var newAddressExpected : Bool = false
/// Expected Bootloader advertised Local Name.
internal var bootloaderName : String?
override init(_ initiator: DFUServiceInitiator, _ logger: LoggerHelper) {
self.peripheralSelector = initiator.peripheralSelector
super.init(initiator, logger)
}
// MARK: - Base DFU Peripheral API
override func peripheralDidDiscoverDfuService(_ service: CBService) {
dfuService = DFUServiceType(service, logger, uuidHelper, queue)
dfuService!.targetPeripheral = self
dfuService!.discoverCharacteristics(
onSuccess: { self.delegate?.peripheralDidBecomeReady() },
onError: defaultErrorCallback
)
}
override func peripheralDidDisconnect() {
guard !aborted else {
// The device has reset. Notify user.
logger.w("Upload aborted")
delegate?.peripheralDidDisconnectAfterAborting()
return
}
if shouldReconnect {
shouldReconnect = false
// We need to reconnect to the device.
connect(withTimeout: connectionTimeout)
} else if jumpingToBootloader {
jumpingToBootloader = false
if newAddressExpected {
newAddressExpected = false
// If in Legacy DFU, and `forceScanningForNewAddressInLegacyDfu`
// is set to true, try first connecting to the same peripheral.
// Perhaps it has not been updated to use incremented address yet.
if forceScanningForNewAddressInLegacyDfu {
// Despite the fact, that a new address is expected,
// try to reconnect to the same device.
connectOrSwitchToNewPeripheral(after: 2.0)
} else {
switchToNewPeripheralAndConnect()
}
} else {
// Connect again, hoping for DFU mode this time.
connect(withTimeout: connectionTimeout)
}
} else if activating {
activating = false
// This part of firmware has been successfully sent.
// Check if there is another part to be sent.
if delegate?.peripheralDidDisconnectAfterFirmwarePartSent() == true {
// As we are already in bootloader mode, the peripheral set
// may be reused for sending a second part.
connectOrSwitchToNewPeripheral(after: 15.0)
} else {
// Upload is completed.
// Peripheral has been destroyed and state is now .completed.
// There is nothing to be done here.
}
} else {
super.peripheralDidDisconnect()
}
}
override func destroy() {
super.destroy()
cleanUp()
}
// MARK: - DFU Peripheral API
func isInApplicationMode(_ forceDfu: Bool) -> Bool {
// This method should be overridden if the final implementation supports
// buttonless jump.
return false
}
func isInitPacketRequired() -> Bool {
// This method should be overridden if the final implementation requires
// Init Packet in the DFUFirmware.
return false
}
func connectOrSwitchToNewPeripheral(after timeout: TimeInterval) {
// In Legacy DFU the DFU bootloader by default advertises with the
// same MAC address as the application. However, recent changes in iOS
// (see: https://github.com/NordicSemiconductor/IOS-Pods-DFU-Library/issues/368#issuecomment-619066196)
// made it necessary for the bootloader to change its address.
// This requires updating the bootloader or the app on the device, as
// explained in the above-mentioned issue.
// Then, this flag needs to be set to true in the `DFUServiceInitiator`.
// With that flag equal to true, the library will try to connect to the
// same device (with a short timeout), and if that fails, will try to
// scan for a new address using `DFUPeripheralSelector`.
connect(withTimeout: timeout) { [weak self] in
// Scan for a new device and connect to it.
self?.switchToNewPeripheralAndConnect()
}
}
func switchToNewPeripheralAndConnect() {
// Release the previous peripheral.
peripheral?.delegate = nil
peripheral = nil
cleanUp()
guard !aborted else {
resetDevice()
return
}
// Set a scanner timer.
connectionTimer = DispatchSource.makeTimerSource()
connectionTimer?.setEventHandler { [weak self] in
if let self = self {
self.connectionTimer?.cancel()
self.logger.w("Scanning timed out returning no matching peripherals!")
self.logger.d("centralManager.stopScan()")
self.centralManager.stopScan()
self.delegate?.error(.failedToConnect, didOccurWithMessage: "No DFU device found.")
}
}
connectionTimer?.schedule(deadline: .now() + connectionTimeout)
connectionTimer?.resume()
logger.v("Scanning for the DFU Bootloader...")
let requiredServices = peripheralSelector.filterBy(hint: DFUServiceType.serviceUuid(from: uuidHelper))
logger.d("centralManager.scanForPeripherals(withServices, \(requiredServices?.description ?? "nil"))")
centralManager.scanForPeripherals(withServices: requiredServices)
}
// MARK: - Peripheral Delegate methods
override func centralManager(_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any], rssi RSSI: NSNumber) {
// Is this a device we are looking for?
if peripheralSelector.select(peripheral, advertisementData: advertisementData as [String : AnyObject],
RSSI: RSSI, hint: bootloaderName) {
// Hurray!
connectionTimer?.cancel()
connectionTimer = nil
central.stopScan()
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
logger.i("DFU Bootloader found with name \(name)")
} else {
logger.i("DFU Bootloader found")
}
self.peripheral = peripheral
connect()
}
}
// MARK: - DFU Controller API
override func pause() -> Bool {
guard let dfuService = dfuService, !aborted else { return false }
return dfuService.pause()
}
override func resume() -> Bool {
guard let dfuService = dfuService, !aborted else { return false }
return dfuService.resume() == false // resume() returns the 'paused' value.
}
override func abort() -> Bool {
aborted = true
guard let dfuService = dfuService else {
// DFU service has not yet been found.
// Peripheral is `nil` when the
// `switchToNewPeripheralAndConnect(_ selector:DFUPeripheralSelector)`
// method was called and the second peripheral has not been found yet.
// Delegate is nil when peripheral was destroyed.
if let delegate = delegate, peripheral == nil {
logger.w("Upload aborted. Part 1 flashed successfully")
centralManager.stopScan()
delegate.peripheralDidDisconnectAfterAborting()
}
return true
}
logger.w("Aborting upload...")
return dfuService.abort()
}
// MARK: - Private methods
fileprivate override func cleanUp() {
super.cleanUp()
dfuService?.destroy()
dfuService = nil
}
}