Skip to content

Commit

Permalink
SuitListResponse
Browse files Browse the repository at this point in the history
For SuitManager's list, we now return a subtype of McuMgrResponse. This keeps the API in line with all the others. As for why this was not the case originally, it's because this API is not simple. We need to send one call, and build the response out of other responses. So there's no 'one McuMgrResponse' that we can return, so we build it instead. Hopefully we built it well.
  • Loading branch information
dinesharjani committed Jul 9, 2024
1 parent 30a701c commit cd4d7bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Source/Managers/SuitManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class SuitManager: McuManager {
private var state: SuitManagerState = .none
private weak var uploadDelegate: SuitManagerDelegate?

private var callback: ManifestCallback?
private var callback: McuMgrCallback<SuitListResponse>?
private var roleIndex: Int?
private var roles: [McuMgrManifestListResponse.Manifest.Role] = []
private var responses: [McuMgrManifestStateResponse] = []
Expand All @@ -77,16 +77,16 @@ public class SuitManager: McuManager {

// MARK: List

public typealias ManifestCallback = ([McuMgrManifestStateResponse], Error?) -> Void
/**
Command allows to get information about roles of manifests supported by the device.
*/
public func listManifest(callback: @escaping ManifestCallback) {
public func listManifest(callback: @escaping McuMgrCallback<SuitListResponse>) {
self.callback = callback
roleIndex = 0
roles = []
responses = []
send(op: .read, commandId: SuitID.manifestList, payload: nil, callback: listManifestCallback)
send(op: .read, commandId: SuitID.manifestList, payload: nil,
callback: listManifestCallback)
}

private func validateNext() {
Expand All @@ -97,7 +97,9 @@ public class SuitManager: McuManager {
atLevel: .verbose)
getManifestState(for: role, callback: roleStateCallback)
} else {
callback?(responses, nil)
let suitResponse = try? SuitListResponse(cbor: nil)
suitResponse?.states = responses
callback?(suitResponse, nil)
}
}

Expand All @@ -108,7 +110,7 @@ public class SuitManager: McuManager {

guard error == nil, let response, response.rc != 8 else {
self.logDelegate?.log("List Manifest Callback not Supported.", ofCategory: .suit, atLevel: .error)
self.callback?([], error)
self.callback?(nil, error)
return
}

Expand Down
17 changes: 17 additions & 0 deletions Source/McuMgrResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,23 @@ public final class McuMgrManifestStateResponse: McuMgrResponse {
}
}

// MARK: - SuitListResponse

/**
This response type is not a part of the McuMgr/SUIT Protocol. We've added it to make the library API side match what most other APIs do, which is to return a ``McuMgrResponse`` for List in ``SuitManager``.
*/
public final class SuitListResponse: McuMgrResponse {

public var states: [McuMgrManifestStateResponse]?

public required init(cbor: CBOR?) throws {
try super.init(cbor: cbor)
if case let CBOR.array(states)? = cbor?["states"] {
self.states = try CBOR.toObjectArray(array: states) ?? []
}
}
}

// MARK: - McuMgrPollResponse

public final class McuMgrPollResponse: McuMgrResponse {
Expand Down

0 comments on commit cd4d7bb

Please sign in to comment.