@@ -54,6 +54,26 @@ import Foundation
54
54
case softdeviceBootloaderApplication = 7
55
55
}
56
56
57
+ /**
58
+ An error thrown when instantiating a `DFUFirmware` type from an invalid file.
59
+ */
60
+ public struct DFUFirmwareError : Error {
61
+ public enum FileType {
62
+ case zip
63
+ case binOrHex
64
+ case dat
65
+ }
66
+ public let type : FileType
67
+ }
68
+
69
+ extension DFUFirmwareError : LocalizedError {
70
+
71
+ public var errorDescription : String ? {
72
+ return NSLocalizedString ( " The \( type) file is invalid " , comment: " " )
73
+ }
74
+
75
+ }
76
+
57
77
/// The DFUFirmware object wraps the firmware file.
58
78
@objc public class DFUFirmware : NSObject , DFUStream {
59
79
internal let stream : DFUStream
@@ -101,10 +121,12 @@ import Foundation
101
121
- parameter urlToZipFile: URL to the Distribution packet (ZIP).
102
122
103
123
- returns: The DFU firmware object or `nil` in case of an error.
124
+ - throws: `DFUFirmwareError` if the file is invalid, or
125
+ `DFUStreamZipError` if creating a Zip stream failed.
104
126
*/
105
- @objc convenience public init ? ( urlToZipFile: URL ) {
106
- self . init ( urlToZipFile: urlToZipFile,
107
- type: DFUFirmwareType . softdeviceBootloaderApplication)
127
+ @objc convenience public init ( urlToZipFile: URL ) throws {
128
+ try self . init ( urlToZipFile: urlToZipFile,
129
+ type: DFUFirmwareType . softdeviceBootloaderApplication)
108
130
}
109
131
110
132
/**
@@ -117,24 +139,20 @@ import Foundation
117
139
- parameter type: The type of the firmware to use.
118
140
119
141
- returns: The DFU firmware object or `nil` in case of an error.
142
+ - throws: `DFUFirmwareError` if the file is invalid, or
143
+ `DFUStreamZipError` if creating a Zip stream failed.
120
144
*/
121
- @objc public init ? ( urlToZipFile: URL , type: DFUFirmwareType ) {
145
+ @objc public init ( urlToZipFile: URL , type: DFUFirmwareType ) throws {
122
146
fileUrl = urlToZipFile
123
147
fileName = urlToZipFile. lastPathComponent
124
148
125
149
// Quickly check if it's a ZIP file
126
150
let ext = urlToZipFile. pathExtension
127
151
if ext. caseInsensitiveCompare ( " zip " ) != . orderedSame {
128
- NSLog ( " \( fileName!) is not a ZIP file " )
129
- return nil
152
+ throw DFUFirmwareError ( type: . zip)
130
153
}
131
154
132
- do {
133
- stream = try DFUStreamZip ( urlToZipFile: urlToZipFile, type: type)
134
- } catch let error as NSError {
135
- NSLog ( " Error while creating ZIP stream: \( error. localizedDescription) " )
136
- return nil
137
- }
155
+ stream = try DFUStreamZip ( urlToZipFile: urlToZipFile, type: type)
138
156
super. init ( )
139
157
}
140
158
@@ -147,9 +165,13 @@ import Foundation
147
165
- parameter zipFile: The Distribution packet (ZIP) data.
148
166
149
167
- returns: The DFU firmware object or `nil` in case of an error.
168
+ - throws: `DFUFirmwareError` if the file is invalid,
169
+ `DFUStreamZipError` if creating a Zip stream failed,
170
+ or an error in the Cocoa domain, if the data cannot be written
171
+ to a temporary location.
150
172
*/
151
- @objc convenience public init ? ( zipFile: Data ) {
152
- self . init ( zipFile: zipFile, type: DFUFirmwareType . softdeviceBootloaderApplication)
173
+ @objc convenience public init ( zipFile: Data ) throws {
174
+ try self . init ( zipFile: zipFile, type: DFUFirmwareType . softdeviceBootloaderApplication)
153
175
}
154
176
155
177
/**
@@ -162,17 +184,15 @@ import Foundation
162
184
- parameter type: The type of the firmware to use.
163
185
164
186
- returns: The DFU firmware object or `nil` in case of an error.
187
+ - throws: `DFUFirmwareError` if the file is invalid,
188
+ `DFUStreamZipError` if creating a Zip stream failed,
189
+ or an error in the Cocoa domain, if the data cannot be written
190
+ to a temporary location.
165
191
*/
166
- @objc public init ? ( zipFile: Data , type: DFUFirmwareType ) {
192
+ @objc public init ( zipFile: Data , type: DFUFirmwareType ) throws {
167
193
fileUrl = nil
168
194
fileName = nil
169
-
170
- do {
171
- stream = try DFUStreamZip ( zipFile: zipFile, type: type)
172
- } catch let error as NSError {
173
- NSLog ( " Error while creating ZIP stream: \( error. localizedDescription) " )
174
- return nil
175
- }
195
+ stream = try DFUStreamZip ( zipFile: zipFile, type: type)
176
196
super. init ( )
177
197
}
178
198
@@ -186,8 +206,11 @@ import Foundation
186
206
- parameter type: The type of the firmware.
187
207
188
208
- returns: The DFU firmware object or `nil` in case of an error.
209
+ - throws: `DFUFirmwareError` if the file is invalid,
210
+ `DFUStreamHexError` if the hex file is invalid,
211
+ or an error in the Cocoa domain, if `url` cannot be read.
189
212
*/
190
- @objc public init ? ( urlToBinOrHexFile: URL , urlToDatFile: URL ? , type: DFUFirmwareType ) {
213
+ @objc public init ( urlToBinOrHexFile: URL , urlToDatFile: URL ? , type: DFUFirmwareType ) throws {
191
214
fileUrl = urlToBinOrHexFile
192
215
fileName = urlToBinOrHexFile. lastPathComponent
193
216
@@ -196,27 +219,22 @@ import Foundation
196
219
let bin = ext. caseInsensitiveCompare ( " bin " ) == . orderedSame
197
220
let hex = ext. caseInsensitiveCompare ( " hex " ) == . orderedSame
198
221
guard bin || hex else {
199
- NSLog ( " \( fileName!) is not a BIN or HEX file " )
200
- return nil
222
+ throw DFUFirmwareError ( type: . binOrHex)
201
223
}
202
224
203
225
if let datUrl = urlToDatFile {
204
226
let datExt = datUrl. pathExtension
205
227
guard datExt. caseInsensitiveCompare ( " dat " ) == . orderedSame else {
206
- NSLog ( " \( fileName!) is not a DAT file " )
207
- return nil
228
+ throw DFUFirmwareError ( type: . dat)
208
229
}
209
230
}
210
231
211
232
if bin {
212
- stream = DFUStreamBin ( urlToBinFile: urlToBinOrHexFile,
213
- urlToDatFile: urlToDatFile, type: type)
233
+ stream = try DFUStreamBin ( urlToBinFile: urlToBinOrHexFile,
234
+ urlToDatFile: urlToDatFile, type: type)
214
235
} else {
215
- guard let s = DFUStreamHex ( urlToHexFile: urlToBinOrHexFile,
216
- urlToDatFile: urlToDatFile, type: type) else {
217
- return nil
218
- }
219
- stream = s
236
+ stream = try DFUStreamHex ( urlToHexFile: urlToBinOrHexFile,
237
+ urlToDatFile: urlToDatFile, type: type)
220
238
}
221
239
super. init ( )
222
240
}
@@ -232,10 +250,9 @@ import Foundation
232
250
233
251
- returns: The DFU firmware object or `nil` in case of an error.
234
252
*/
235
- @objc public init ? ( binFile: Data , datFile: Data ? , type: DFUFirmwareType ) {
253
+ @objc public init ( binFile: Data , datFile: Data ? , type: DFUFirmwareType ) {
236
254
fileUrl = nil
237
255
fileName = nil
238
-
239
256
stream = DFUStreamBin ( binFile: binFile, datFile: datFile, type: type)
240
257
super. init ( )
241
258
}
@@ -250,15 +267,12 @@ import Foundation
250
267
- parameter type: The type of the firmware.
251
268
252
269
- returns: The DFU firmware object or `nil` in case of an error.
270
+ - throws: `DFUStreamHexError` if the hex file is invalid.
253
271
*/
254
- @objc public init ? ( hexFile: Data , datFile: Data ? , type: DFUFirmwareType ) {
272
+ @objc public init ( hexFile: Data , datFile: Data ? , type: DFUFirmwareType ) throws {
255
273
fileUrl = nil
256
274
fileName = nil
257
-
258
- guard let s = DFUStreamHex ( hexFile: hexFile, datFile: datFile, type: type) else {
259
- return nil
260
- }
261
- stream = s
275
+ stream = try DFUStreamHex ( hexFile: hexFile, datFile: datFile, type: type)
262
276
super. init ( )
263
277
}
264
278
0 commit comments