@@ -47,6 +47,17 @@ import CoreBluetooth
47
47
private let service : CBService
48
48
private var dfuPacketCharacteristic : SecureDFUPacket ?
49
49
private var dfuControlPointCharacteristic : SecureDFUControlPoint ?
50
+
51
+ /// This method returns true if DFU Control Point characteristc has been discovered.
52
+ /// A device without this characteristic is not supported and even can't be resetted
53
+ /// by sending a Reset command.
54
+ internal func supportsReset( ) -> Bool {
55
+ // The Abort (0x0C) command has been added to DFU bootloader in SDK 15.
56
+ // https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/lib_dfu_transport.html?cp=8_5_3_3_5_2
57
+ // For earlier SDKs there is no way to reset the bootloader other than
58
+ // disconnecting and waiting for it to time out after few minutes.
59
+ return dfuControlPointCharacteristic != nil
60
+ }
50
61
51
62
private var paused = false
52
63
private var aborted = false
@@ -55,7 +66,7 @@ import CoreBluetooth
55
66
private var success : Callback ?
56
67
/// A temporary callback used to report an operation error.
57
68
private var report : ErrorCallback ?
58
- /// A temporaty callback used to report progress status.
69
+ /// A temporary callback used to report progress status.
59
70
private var progressDelegate : DFUProgressDelegate ?
60
71
private var progressQueue : DispatchQueue ?
61
72
@@ -323,10 +334,19 @@ import CoreBluetooth
323
334
324
335
- parameter report: A callback called when writing characteristic failed.
325
336
*/
326
- private func sendReset( onError report: @escaping ErrorCallback ) {
337
+ func sendReset( onError report: @escaping ErrorCallback ) {
327
338
aborted = true
328
- // There is no command to reset a Secure DFU device. We can just disconnect.
329
- targetPeripheral? . disconnect ( )
339
+ // Upon sending the Abort request the device will immediately reboot in application
340
+ // mode. There will be no notification with status success returned.
341
+ dfuControlPointCharacteristic? . send ( . abort,
342
+ onSuccess: nil , // Device will disconnected immediately.
343
+ onError: { [ weak self] _, _ in
344
+ // Seems like the Abort request is not supported (indicating SDK 12-14).
345
+ // We can just disconnect. The bootloader should reset to app mode after
346
+ // a timeout.
347
+ self ? . targetPeripheral? . disconnect ( )
348
+ }
349
+ )
330
350
}
331
351
332
352
//MARK: - Packet commands
0 commit comments