Skip to content

Commit

Permalink
Merge pull request #225 from NordicSemiconductor/mcumgrpackage-suit-zip
Browse files Browse the repository at this point in the history
McuMgrPackage now parses a SUIT Manifest .ZIP
  • Loading branch information
dinesharjani authored Jun 27, 2024
2 parents b1b762b + 886d11a commit 961e6f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
19 changes: 14 additions & 5 deletions Example/Example/Util/McuMgrPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,22 @@ fileprivate extension McuMgrPackage {
throw McuMgrPackage.Error.manifestFileNotFound
}
let manifest = try McuMgrManifest(from: dfuManifestURL)
let images = try manifest.files.compactMap { manifestFile -> ImageManager.Image in
guard let imageURL = unzippedURLs.first(where: { $0.absoluteString.contains(manifestFile.file) }) else {
let images: [ImageManager.Image]
if let envelopeFile = manifest.envelopeFile() {
guard let envelopeURL = unzippedURLs.first(where: { $0.absoluteString.contains(envelopeFile.file) }) else {
throw McuMgrPackage.Error.manifestImageNotFound
}
let imageData = try Data(contentsOf: imageURL)
let imageHash = try McuMgrImage(data: imageData).hash
return ImageManager.Image(manifestFile, hash: imageHash, data: imageData)
let suitEnvelope = try McuMgrSuitEnvelope(from: envelopeURL)
return [suitEnvelope.image()].compactMap({ $0 })
} else {
images = try manifest.files.compactMap { manifestFile -> ImageManager.Image in
guard let imageURL = unzippedURLs.first(where: { $0.absoluteString.contains(manifestFile.file) }) else {
throw McuMgrPackage.Error.manifestImageNotFound
}
let imageData = try Data(contentsOf: imageURL)
let imageHash = try McuMgrImage(data: imageData).hash
return ImageManager.Image(manifestFile, hash: imageHash, data: imageData)
}
}
try unzippedURLs.forEach { url in
try fileManager.removeItem(at: url)
Expand Down
10 changes: 10 additions & 0 deletions Source/McuMgrManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Foundation

public struct McuMgrManifest: Codable {

// MARK: Public Properties

public let formatVersion: Int
public let time: Int
public let files: [File]
Expand All @@ -25,6 +27,8 @@ public struct McuMgrManifest: Codable {
static let LoadAddressRegEx: NSRegularExpression! =
try? NSRegularExpression(pattern: #"\"load_address\":0x[0-9a-z]+,"#, options: [.caseInsensitive])

// MARK: Init

public init(from url: URL) throws {
guard let data = try? Data(contentsOf: url),
let stringData = String(data: data, encoding: .utf8) else {
Expand All @@ -42,6 +46,12 @@ public struct McuMgrManifest: Codable {
throw Error.unableToDecodeJSON
}
}

// MARK: API

public func envelopeFile() -> File? {
files.first(where: { $0.content == .suitEnvelope })
}
}

// MARK: - McuMgrManifest.File
Expand Down
6 changes: 6 additions & 0 deletions Source/McuMgrSuitEnvelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public struct McuMgrSuitEnvelope {

// MARK: API

public func image() -> ImageManager.Image? {
// Currently only supported Hash Digest Algorithm is SHA256.
guard let hash = digest.hash(for: .sha256) else { return nil }
return ImageManager.Image(image: 0, hash: hash, data: data)
}

public func sizeString() -> String {
return "\(data.count) bytes"
}
Expand Down
4 changes: 0 additions & 4 deletions Source/McuMgrSuitManifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,9 @@ class McuMgrSuitCommonStructure: CBORMappable {

class McuMgrSuitSharedSequence: CBORMappable {

public var conditionVendorIdentifier: UInt64?
public var conditionClassIdentifier: UInt64?

public required init(cbor: CBOR?) throws {
if case let CBOR.unsignedInt(vendorIdentifier)? = cbor?[1] {
self.conditionVendorIdentifier = vendorIdentifier
}
if case let CBOR.unsignedInt(classIdentifier)? = cbor?[2] {
self.conditionClassIdentifier = classIdentifier
}
Expand Down

0 comments on commit 961e6f9

Please sign in to comment.