31
31
import CoreBluetooth
32
32
33
33
internal enum SecureDFUOpCode : UInt8 {
34
+ case getProtocolVersion = 0x0 // not supported by this library
34
35
case createObject = 0x01
35
36
case setPRNValue = 0x02
36
37
case calculateChecksum = 0x03
37
38
case execute = 0x04
39
+ // case no-such-op-code = 0x05
38
40
case readObjectInfo = 0x06
41
+ case getMtu = 0x07 // not supported by this library
42
+ case write = 0x08 // not supported by this library
43
+ case ping = 0x09 // not supported by this library
44
+ case getHwVersion = 0x0A // not supported by this library
45
+ case getFwVersion = 0x0B // not supported by this library
46
+ case abort = 0x0C
39
47
case responseCode = 0x60
40
48
41
49
var code : UInt8 {
@@ -98,17 +106,40 @@ internal enum SecureDFUProcedureType : UInt8 {
98
106
}
99
107
}
100
108
109
+ internal enum SecureDFUImageType : UInt8 {
110
+ case softdevice = 0x00
111
+ case application = 0x01
112
+ case bootloader = 0x02
113
+
114
+ var description : String {
115
+ switch self {
116
+ case . softdevice: return " Soft Device "
117
+ case . application: return " Application "
118
+ case . bootloader: return " Bootloader "
119
+ }
120
+ }
121
+ }
122
+
101
123
internal enum SecureDFURequest {
102
- case createCommandObject( withSize : UInt32 )
103
- case createDataObject( withSize : UInt32 )
124
+ case getProtocolVersion
125
+ case createCommandObject( withSize: UInt32 )
126
+ case createDataObject( withSize: UInt32 )
104
127
case readCommandObjectInfo
105
128
case readDataObjectInfo
106
- case setPacketReceiptNotification( value : UInt16 )
129
+ case setPacketReceiptNotification( value: UInt16 )
107
130
case calculateChecksumCommand
108
131
case executeCommand
132
+ case getMtu
133
+ case write( bytes: Data )
134
+ case ping( id: UInt8 )
135
+ case getHwVersion
136
+ case getFwVersion( image: SecureDFUImageType )
137
+ case abort
109
138
110
139
var data : Data {
111
140
switch self {
141
+ case . getProtocolVersion:
142
+ return Data ( [ SecureDFUOpCode . getProtocolVersion. code] )
112
143
case . createDataObject( let aSize) :
113
144
var data = Data ( [ SecureDFUOpCode . createObject. code, SecureDFUProcedureType . data. rawValue] )
114
145
data += aSize. littleEndian
@@ -117,10 +148,6 @@ internal enum SecureDFURequest {
117
148
var data = Data ( [ SecureDFUOpCode . createObject. code, SecureDFUProcedureType . command. rawValue] )
118
149
data += aSize. littleEndian
119
150
return data
120
- case . readCommandObjectInfo:
121
- return Data ( [ SecureDFUOpCode . readObjectInfo. code, SecureDFUProcedureType . command. rawValue] )
122
- case . readDataObjectInfo:
123
- return Data ( [ SecureDFUOpCode . readObjectInfo. code, SecureDFUProcedureType . data. rawValue] )
124
151
case . setPacketReceiptNotification( let aSize) :
125
152
var data = Data ( [ SecureDFUOpCode . setPRNValue. code] )
126
153
data += aSize. littleEndian
@@ -129,19 +156,45 @@ internal enum SecureDFURequest {
129
156
return Data ( [ SecureDFUOpCode . calculateChecksum. code] )
130
157
case . executeCommand:
131
158
return Data ( [ SecureDFUOpCode . execute. code] )
159
+ case . readCommandObjectInfo:
160
+ return Data ( [ SecureDFUOpCode . readObjectInfo. code, SecureDFUProcedureType . command. rawValue] )
161
+ case . readDataObjectInfo:
162
+ return Data ( [ SecureDFUOpCode . readObjectInfo. code, SecureDFUProcedureType . data. rawValue] )
163
+ case . getMtu:
164
+ return Data ( [ SecureDFUOpCode . getMtu. code] )
165
+ case . write( let bytes) :
166
+ var data = Data ( [ SecureDFUOpCode . write. code] )
167
+ data += bytes
168
+ data += UInt16 ( bytes. count) . littleEndian
169
+ return data
170
+ case . ping( let id) :
171
+ return Data ( [ SecureDFUOpCode . ping. code, id] )
172
+ case . getHwVersion:
173
+ return Data ( [ SecureDFUOpCode . getHwVersion. code] )
174
+ case . getFwVersion( let image) :
175
+ return Data ( [ SecureDFUOpCode . getFwVersion. code, image. rawValue] )
176
+ case . abort:
177
+ return Data ( [ SecureDFUOpCode . abort. code] )
132
178
}
133
179
}
134
180
135
181
var description : String {
136
182
switch self {
183
+ case . getProtocolVersion: return " Get Protocol Version (Op Code = 0) "
137
184
case . createCommandObject( let size) : return " Create Command Object (Op Code = 1, Type = 1, Size: \( size) b) "
138
185
case . createDataObject( let size) : return " Create Data Object (Op Code = 1, Type = 2, Size: \( size) b) "
139
- case . readCommandObjectInfo: return " Read Command Object Info (Op Code = 6, Type = 1) "
140
- case . readDataObjectInfo: return " Read Data Object Info (Op Code = 6, Type = 2) "
141
186
case . setPacketReceiptNotification( let number) :
142
187
return " Packet Receipt Notif Req (Op Code = 2, Value = \( number) ) "
143
188
case . calculateChecksumCommand: return " Calculate Checksum (Op Code = 3) "
144
189
case . executeCommand: return " Execute Object (Op Code = 4) "
190
+ case . readCommandObjectInfo: return " Read Command Object Info (Op Code = 6, Type = 1) "
191
+ case . readDataObjectInfo: return " Read Data Object Info (Op Code = 6, Type = 2) "
192
+ case . getMtu: return " Get MTU (Op Code = 7) "
193
+ case . write( let bytes) : return " Write (Op Code = 8, Data = 0x \( bytes. hexString) , Length = \( bytes. count) ) "
194
+ case . ping( let id) : return " Ping (Op Code = 9, ID = \( id) ) "
195
+ case . getHwVersion: return " Get HW Version (Op Code = 10) "
196
+ case . getFwVersion( let image) : return " Get FW Version (Op Code = 11, Type = \( image. rawValue) ) "
197
+ case . abort: return " Abort (Op Code = 12) "
145
198
}
146
199
}
147
200
}
@@ -155,7 +208,7 @@ internal enum SecureDFUResultCode : UInt8 {
155
208
case invalidObject = 0x05
156
209
case signatureMismatch = 0x06
157
210
case unsupportedType = 0x07
158
- case operationNotpermitted = 0x08
211
+ case operationNotPermitted = 0x08
159
212
case operationFailed = 0x0A
160
213
case extendedError = 0x0B
161
214
@@ -176,7 +229,7 @@ internal enum SecureDFUResultCode : UInt8 {
176
229
case . insufficientResources: return " Insufficient resources "
177
230
case . invalidObject: return " Invalid object "
178
231
case . signatureMismatch: return " Signature mismatch "
179
- case . operationNotpermitted : return " Operation not permitted "
232
+ case . operationNotPermitted : return " Operation not permitted "
180
233
case . unsupportedType: return " Unsupported type "
181
234
case . operationFailed: return " Operation failed "
182
235
case . extendedError: return " Extended error "
0 commit comments