### Initialize and Start Firmware Upgrade Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Initializes the McuMgrTransport and FirmwareUpgradeManager, then creates a package from a URL and starts the firmware upgrade process. Ensure the package URL is valid and the transport is correctly initialized. ```swift import iOSMcuManagerLibrary do { // Initialize the BLE transport using a scanned peripheral let bleTransport = McuMgrBleTransport(cbPeripheral) // Initialize the FirmwareUpgradeManager using the transport and a delegate let dfuManager = FirmwareUpgradeManager(bleTransport, delegate) let packageURL = /* Obtain URL to the file user wants to Upload */ let package = try McuMgrPackage(from: packageURL) // Start the firmware upgrade with the given package dfuManager.start(package: package) } catch { // Package initialisation errors here. } ``` -------------------------------- ### Initialize and Configure Logging Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Initialize McuMgrBleTransport and DeviceManager, setting their log delegates to enable low-level logging. This setup is useful for debugging app and device interactions. ```swift import iOSMcuManagerLibrary // Initialize the BLE transport using a scanned peripheral let bleTransport = McuMgrBleTransport(cbPeripheral) bleTransport.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate // Initialize the DeviceManager using the transport and a delegate let deviceManager = DeviceManager(bleTransport, delegate) deviceManager.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate // Send echo deviceManger.echo("Hello World!", callback) ``` -------------------------------- ### Install iOSMcuManagerLibrary with CocoaPods Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Use this command to add the iOSMcuManagerLibrary to your project via CocoaPods. Ensure CocoaPods is set up in your project. ```bash pod 'iOSMcuManagerLibrary' ``` -------------------------------- ### Perform SUIT Upgrade using ImageManager.Image API Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Initializes the BLE transport and FirmwareUpgradeManager, parses a SUIT envelope, extracts the SHA256 hash, and starts the firmware upgrade process. Ensure the envelope's hash algorithm is supported by the library. ```swift import iOSMcuManagerLibrary do { // Initialize the BLE transport using a scanned peripheral let bleTransport = McuMgrBleTransport(cbPeripheral) // Initialize the FirmwareUpgradeManager using the transport and a delegate let dfuManager = FirmwareUpgradeManager(bleTransport, delegate) // Parse McuMgrSuitEnvelope from File URL let envelope = try McuMgrSuitEnvelope(from: dfuSuitEnvelopeUrl) // Look for valid Algorithm Hash guard let sha256Hash = envelope.digest.hash(for: .sha256) else { throw McuMgrSuitParseError.supportedAlgorithmNotFound } let suitImage = ImageManager.Image(image: 0, hash: sha256Hash, data: envelope.data) try dfuManager.start(images: [suitImage]) } catch { // Handle errors from McuMgrSuitEnvelope init, start() API call, etc. } ``` -------------------------------- ### Configure Firmware Upgrade with Custom Settings Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Demonstrates how to set up a `FirmwareUpgradeManager` and initiate a firmware upgrade using custom `FirmwareUpgradeConfiguration` objects. Use this for non-pipelined or pipelined upgrades. ```swift import iOSMcuManagerLibrary // Setup let bleTransport = McuMgrBleTransport(cbPeripheral) let dfuManager = FirmwareUpgradeManager(bleTransport, delegate) // Non-Pipelined Example let nonPipelinedConfiguration = FirmwareUpgradeConfiguration( estimatedSwapTime: 10.0, eraseAppSettings: false, pipelineDepth: 2, ) dfuManager.start(package: package, using: nonPipelinedConfiguration) // Pipelined Example let pipelinedConfiguration = FirmwareUpgradeConfiguration( estimatedSwapTime: 10.0, eraseAppSettings: true, pipelineDepth: 4, byteAlignment: .fourByte ) dfuManager.start(package: package, using: pipelinedConfiguration) ``` -------------------------------- ### Upload File using FileSystemManager (Non-Pipelined) Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Demonstrates the basic usage of uploading a file to an SMP Server device without using pipelining. Ensure the McuMgrBleTransport is set up and the necessary parameters like file name and data are provided. ```swift import iOSMcuManagerLibrary // Setup let bleTransport = McuMgrBleTransport(cbPeripheral) bleTransport.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate let fsManager = FileSystemManager(transport: bleTransport) // Parameters let fileData: Data = // File Contents let mountingPoint: String = "/lfs1" // Default. May vary depending on your specific setup. let fileName: String = mountingPoint + "/" + "example.txt" let uploadDelegate: FileUploadDelegate = // FileUploadDelegate // Non-Pipelined (Default Configuration) Example let didUploadStart = fsManager.upload(name: fileName, data: fileData, delegate: uploadDelegate) ``` -------------------------------- ### Upload File using FileSystemManager (Pipelined) Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Shows how to upload a file using pipelining for potentially improved performance. Configure the `FirmwareUpgradeConfiguration` with a desired `pipelineDepth` to enable this feature. ```swift import iOSMcuManagerLibrary // Setup let bleTransport = McuMgrBleTransport(cbPeripheral) bleTransport.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate let fsManager = FileSystemManager(transport: bleTransport) // Parameters let fileData: Data = // File Contents let mountingPoint: String = "/lfs1" // Default. May vary depending on your specific setup. let fileName: String = mountingPoint + "/" + "example.txt" let uploadDelegate: FileUploadDelegate = // FileUploadDelegate // Pipelined Example var pipelinedConfiguration = FirmwareUpgradeConfiguration() pipelinedConfiguration.pipelineDepth = 4 // Equivalent to CONFIG_MCUMGR_TRANSPORT_NETBUF_COUNT=4 in firmware KConfig files. let didUploadStart = fsManager.upload(name: fileName, data: fileData, using: pipelinedConfiguration, delegate: uploadDelegate) ``` -------------------------------- ### Download File using FileSystemManager Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Demonstrates how to download a file from an SMP Server device. The `McuMgrBleTransport` automatically handles reassembly of file packets. ```swift import iOSMcuManagerLibrary // Setup let bleTransport = McuMgrBleTransport(cbPeripheral) bleTransport.logDelegate = UIApplication.shared.delegate as? McuMgrLogDelegate let fsManager = FileSystemManager(transport: bleTransport) // Parameters let fileData: Data = // File Contents let mountingPoint: String = "/lfs1" // Default. May vary depending on your specific setup. let fileName: String = mountingPoint + "/" + "example.txt" let downloadDelegate: FileDownloadDelegate = // FileDownloadDelegate let downloadStarted = fsManager.download(name: destination, delegate: downloadDelegate) // FileDownloadDelegate { func download(of name: String, didFinish data: Data) { // Downloaded file's contents are in @data parameter } func downloadProgressDidChange(bytesDownloaded: Int, fileSize: Int, timestamp: Date) { /* ... */ } func downloadDidFail(with error: Error) { /* ... */ } func downloadDidCancel() { /* ... */ } } ``` -------------------------------- ### Perform Multi-Image DFU with iOSMcuManagerLibrary Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Initializes the BLE transport and FirmwareUpgradeManager to perform a multi-image DFU. Ensure the 'appCoreFileURL' and 'netCoreFileURL' are valid URLs to the firmware data. ```swift import iOSMcuManagerLibrary try { // Initialize the BLE transport using a scanned peripheral let bleTransport = McuMgrBleTransport(cbPeripheral) // Initialize the FirmwareUpgradeManager using the transport and a delegate let dfuManager = FirmwareUpgradeManager(bleTransport, delegate) // Build Multi-Image DFU parameters let appCoreData = try Data(contentsOf: appCoreFileURL) let appCoreDataHash = try McuMgrImage(data: appCoreData).hash let netCoreData = try Data(contentsOf: netCoreFileURL) let netCoreDataHash = try McuMgrImage(data: netCoreData).hash let images: [ImageManager.Image] = [ (image: 0, slot: 1, hash: appCoreDataHash, data: appCoreData), (image: 1, slot: 1, hash: netCoreDataHash, data: netCoreData) ] // Start Multi-Image DFU firmware upgrade dfuManager.start(images: images) } catch { // Errors here. } ``` -------------------------------- ### Implement SuitFirmwareUpgradeDelegate for Resource Requests Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Extends FirmwareUpgradeDelegate to handle resource requests during SUIT upgrades, typically by retrieving data for the requested resource from a package. This is crucial when the upgrade file is a .zip archive containing additional files. ```swift func uploadRequestsResource(_ resource: FirmwareUpgradeResource) { let image: ImageManager.Image! = package?.image(forResource: resource) firmwareUpgradeManager.uploadResource(resource, data: image.data) } ``` -------------------------------- ### Perform DirectXIP Firmware Upgrade Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Builds parameters for a DirectXIP firmware upgrade. This method is used when firmware supports booting from either slot, providing hashes for both slot 0 and slot 1. ```swift import iOSMcuManagerLibrary try { /* Initialise transport & manager as above. */ // Build DirectXIP parameters let appCoreSlotZeroData = try Data(contentsOf: appCoreSlotZeroURL) let appCoreSlotZeroHash = try McuMgrImage(data: appCoreSlotZeroData).hash let appCoreSlotOneData = try Data(contentsOf: appCoreSlotOneURL) let appCoreSlotOneHash = try McuMgrImage(data: appCoreSlotOneData).hash let directXIP: [ImageManager.Image] = [ (image: 0, slot: 0, hash: appCoreSlotZeroHash, data: appCoreSlotZeroData), (image: 0, slot: 1, hash: appCoreSlotOneHash, data: appCoreSlotOneData) ] // Start DirectXIP Firmware Upgrade dfuManager.start(images: directXIP) } catch { // Errors here. } ``` -------------------------------- ### Perform Firmware Upgrade using McuManager Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md After downloading an OTA update file, this snippet shows how to use iOSMcuManagerLibrary to perform the firmware upgrade. It requires a Bluetooth transport and a delegate for the upgrade process. ```swift import iOSOtaLibrary import iOSMcuManagerLibrary do { let bleTransport = McuMgrBleTransport(cbPeripheral) let dfuManager = FirmwareUpgradeManager(bleTransport, delegate) let fileURL: = // locally-downloaded update let package = try McuMgrPackage(from: packageURL) dfuManager.start(package: package) } catch { // Package initialisation errors here. } ``` -------------------------------- ### Perform OTA Update Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md This snippet demonstrates how to check for and download an OTA update for a Bluetooth LE device. It requires device information and a project key for authorization. ```swift import iOSOtaLibrary do { let identifier: UUID // UUID of the target Bluetooth Peripheral let deviceInfoManager = DeviceInfoManager(identifier) // Device Information such as current software version, target hardware, etc. let deviceInfo: DeviceInfoToken = try await deviceInfoManager.getDeviceInfoToken() // nRF Cloud powered by Memfault Authorisation Token let projectKey: ProjectKey = try await deviceInfoManager.getProjectKey() // (Both deviceInfo and projectKey can be obtained by other means. It is up to you.) // Is there an OTA update available? let otaManager = OTAManager() switch otaManager.getLatestReleaseInfo(deviceInfo: deviceInfo, projectKey: projectKey) { case .success(let resultInfo): // OTA Update available. // Download let artifact: ReleaseArtifact = // select artifact from previously obtained resultInfo switch try await otaManager.download(artifact: artifact) { case .success(let fileURL): // fileURL is locally-downloaded file containing update package case .failure(let error): // Handle Error } case .failure(let otaError): // Handle Error // OTAManagerError.deviceIsUpToDate is returned if device is up to date } } catch { // Process errors. } ``` -------------------------------- ### Connect to Device for Observability Stream Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md This snippet establishes a connection to a Bluetooth LE device to receive a continuous stream of telemetry data (Observability events). It handles potential network connectivity issues by storing data locally and retrying automatically. ```swift import iOSOtaLibrary let identifier: UUID // UUID of the target Bluetooth Peripheral let observabilityManager = ObservabilityManager() // Connects to the device and obtains an AsyncSequence of events. // You may not listen to its events / discard function's result. let observabilityStream: ObservabilityManager.AsyncObservabilityStream = observabilityManager.connectToDevice(observabilityIdentifier) do { for try await event in observabilityStream { // Process events } // Device connection closed } catch let obsError as ObservabilityError { // Errors covered by ObservabilityError, such as pairing to device requirement } catch let error { // Unexpected errors } ``` -------------------------------- ### Resume Pending Observability Uploads Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md This snippet provides an API to manually trigger the library to resume uploading pending data packets if network connectivity was interrupted. This is useful for providing users an actionable item to resume uploads. ```swift do { try observabilityManager.continuePendingUploads(for: observabilityIdentifier) } catch { // Process errors. } ``` -------------------------------- ### Define Image Structure for MCU Manager Source: https://github.com/nordicsemi/ios-nrf-connect-device-manager/blob/main/README.md Defines the structure for image data used in MCU Manager operations. The 'image' and 'slot' parameters are relevant for McuMgr/MCUboot but ignored for SUIT updates. ```swift public class ImageManager: McuManager { public struct Image { public let name: String? public let image: Int public let slot: Int public let content: McuMgrManifest.File.ContentType public let hash: Data public let data: Data /* ... */ } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.