Nordic iOS DFU Library

repository·main·Indexed 20 days ago

https://github.com/nordicsemi/ios-dfu-library

A library for performing Over-The-Air (OTA) Device Firmware Updates on Nordic Semiconductor nRF5x Series SoCs (nRF51 and nRF52) using an iPhone or iPad. It supports both Legacy DFU and Secure DFU, is compatible with nRF5 SDK versions 4.3 through 17.1, and can be installed via CocoaPods, Carthage, or Swift Package Manager.

Tokens
7.4K
Snippets
23
Records
36
Agent score
70%

What's inside ios-dfu-library

  1. Overview of NordicDFU library

    main

    NordicDFU is a Swift library designed to update the firmware of nRF51 and nRF52 devices over Bluetooth LE. It supports devices running nRF5 SDK (version 4.3 and later).

    Key Capabilities:

    • Update Application, SoftDevice, and/or Bootloader Over-the-Air (OTA).
    • Support for ZIP, HEX, or BIN file formats.
    • Handle bonded devices and buttonless updates.
    • Built-in recovery mechanisms for communication errors.

    Important Compatibility Note: This library is not compatible with the MCU Manager subsystem from nRF Connect SDK (NCS) or Zephyr. For those SDKs, use the nRF Connect Device Manager library instead.

  2. The importance of the Service Changed characteristic

    main

    For DFU to work correctly with iOS, the target device MUST have the Service Changed characteristic with the Indicate property in the Generic Attribute service.

    Without this characteristic, iOS will not invalidate its service cache when the device switches to DFU bootloader mode, which can prevent the update from succeeding.

    Behavioral details:

    • Paired devices: iOS automatically handles the indication to the Service Changed characteristic to perform service discovery. This is supported in Legacy DFU since SDK 8.0.
    • Non-trusted (unpaired) devices: iOS clears the service cache every time the device disconnects.
  3. Compatibility with Legacy and Secure DFU

    main

    The library is fully backwards compatible and supports both Legacy DFU and Secure DFU.

    • Legacy DFU: Supports application updates (SDK 4.3.0) and combined SoftDevice/Bootloader updates (SDK 6.1.0).
    • Secure DFU: Introduced in SDK 12.0.0.

    Note on Experimental Buttonless Service: The experimental buttonless service from SDK 12 is supported since library version 1.1.0. Because this service is considered unsafe, it is disabled by default. To enable it, you must set enableUnsafeExperimentalButtonlessServiceInSecureDfu to true. It is recommended to use the Buttonless service from SDK 13 (for non-bonded devices) or SDK 14 (for bonded devices) instead.

  4. Monitor DFU progress using DFUState

    main

    The DFUState enumeration represents the various stages of the Device Firmware Update process. You can use these states to update your application's UI, track progress, or respond to specific lifecycle events (such as success, failure, or cancellation).

    The available states are:

    • connecting: The library is attempting to establish a connection with the device.
    • starting: The DFU process is being initialized.
    • enablingDfuMode: The device is being transitioned into DFU mode.
    • uploading: Firmware data is currently being transferred to the device.
    • validating: The device is verifying the integrity of the uploaded firmware.
    • disconnecting: The library is cleaning up the connection.
    • completed: The update finished successfully.
    • aborted: The update process was cancelled or failed to start.
  5. Requirements for using the iOS DFU Library

    main

    The library is designed for performing Device Firmware Updates on nRF5x Series devices. Specifically, it requires:

    • nRF51 or nRF52 devices.
    • An S-Series SoftDevice flashed on the device.
    • A DFU Bootloader flashed on the device.
  6. Track and cancel ZIP operation progress

    main

    All Archive operations accept an optional progress parameter of type Progress.

    • Tracking: ZIP Foundation automatically manages totalUnitCount and updates completedUnitCount. You can observe the fractionCompleted property using a Key-Value Observer (KVO) to get updates.
    • Cancellation: Call cancel() on the Progress instance to terminate an ongoing operation. If cancelled, the operation will throw an ArchiveError.cancelledOperation error.
  7. Monitor DFU progress and logs

    main

    To build a responsive UI and debug the update process, you should implement the following delegates on your DFUServiceInitiator:

    • DFUProgressDelegate: Use this to receive updates on the current progress of the firmware upload.
    • LoggerDelegate: Use this to receive log information via the logger property. This is useful for tracking the internal state and troubleshooting communication issues.
    • DFUServiceDelegate: Use this to be informed about the overall DFU state changes and any errors that occur during the process.
  8. Setup the DFU Test App for automated testing

    main

    The Test App (DFU Tester) is designed for automated testing of Legacy and Secure DFU. To use it, you must program a compatible nRF5x Development Kit (DK) with the appropriate firmware and then connect to it via the app.

    1. Hardware Requirements

    Depending on the DFU type and SDK version you wish to test, use one of the following supported DKs:

    • nRF51 DK: For testing Legacy DFU (SDK 6 to 12.2).
    • nRF52832 DK: For testing Secure DFU (SDK 12.2 to 16).
    • nRF52840 DK: For testing Secure DFU (SDK 13 to 17.1).

    2. Installation Steps

    1. Program the DK: Locate the appropriate HEX file in the Sources/Firmwares/<DK> directory of this repository and flash it onto your development kit.
    2. Verify Advertising: Once programmed, the DK should begin advertising via Bluetooth.
    3. Run the App: Launch the DFU Tester app on your iOS device.
    4. Connect: Select a device from the scanner that has a name starting with DFU....
  9. Install the iOS DFU Library via Carthage

    main

    To install using Carthage, add the repository to your Cartfile. Build the dependencies using the carthage update command with the --use-xcframeworks flag. This will produce NordicDFU.framework and ZipFramework.framework in the Carthage/Build/ directory, which you should then copy into your project.

    github "NordicSemiconductor/IOS-DFU-Library" ~> x.y // Replace x.y with your required version
    carthage update --use-xcframeworks --platform iOS
  10. Install ZIPFoundation via Swift Package Manager

    main

    To add ZIPFoundation to your project using Swift Package Manager, add the dependency to your Package.swift file and include it in your target's dependencies. After updating the file, run swift package resolve to fetch the library.

    // swift-tools-version:5.0
    import PackageDescription
    let package = Package(
        name: "<Your Product Name>",
        dependencies: [
    		.package(url: "https://github.com/weichsel/ZIPFoundation.git", .upToNextMajor(from: "0.9.0"))
        ],
        targets: [
            .target(
    		name: "<Your Target Name>",
    		dependencies: ["ZIPFoundation"]),
        ]
    )
    $ swift package resolve
  11. How to test DFU using the DFU Tester application

    main

    To perform a full DFU test cycle using the provided test application, follow these steps:

    1. Prepare the Hardware: In the device folders, locate the .hex file. This file contains the complete firmware (SoftDevice, Application with buttonless service, DFU Bootloader, and bootloader settings).
    2. Flash the Device: Connect your nRF5 DK to your computer and flash the .hex file onto it (e.g., via Drag&Drop or nrfjprog).
    3. Launch the App: Open the DFU Tester application on your iOS device.
    4. Identify the Device: Look for a device advertising with the name format DFU<board-id><mode><version>.
      • Board ID: 1 = nRF51, 2 = nRF52832, 3 = nRF52840
      • Mode: A = Application, B = Bootloader
      • Version: SDK version without dots (e.g., 141 = SDK 14.1, 08 = SDK 8, 061 = SDK 6.1)
    5. Execute Test: Tap the selected device and press the Connect button. The test may take several minutes as it attempts to downgrade/upgrade the device through various SDK versions to test different scenarios.

    Important Notes:

    • nRF51: The test downgrades SD+BL+App from SDK 12.2 down to 6.0.
    • Other Targets: The test upgrades from the oldest supported version to the current one.
    • Secure vs Legacy DFU: Downgrading from SDK 12.x+ to 11 requires switching from Secure DFU to Legacy DFU. Because the iOS DFU Library selects the DFU method only once during the first connection, the app will first upload only the SD+BL, then start a new upload to upload the Application only.
    • Bonding: This test does not cover DFU on bonded devices. All test Apps and Bootloaders have the Service Changed service enabled to prevent iOS from caching.