Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FOTA Service mock example #370

Merged
merged 23 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BLE_GattServer_FOTAService/.mbed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ROOT=.
1 change: 1 addition & 0 deletions BLE_GattServer_FOTAService/.mbedignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scripts/*
46 changes: 46 additions & 0 deletions BLE_GattServer_FOTAService/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)

set(MBED_PATH ${CMAKE_CURRENT_SOURCE_DIR}/mbed-os CACHE INTERNAL "")
set(MBED_CONFIG_PATH ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "")
set(APP_TARGET BLE_GattServer_FOTAService)

include(${MBED_PATH}/tools/cmake/app.cmake)

add_subdirectory(${MBED_PATH})

add_subdirectory(mbed-os-experimental-ble-services/services/FOTA)

include_directories(mbed-os-experimental-ble-services)

add_executable(${APP_TARGET})

project(${APP_TARGET})

target_sources(${APP_TARGET}
PRIVATE
source/main.cpp
source/BlockDeviceFOTAEventHandler.h
source/BlockDeviceFOTAEventHandler.cpp
source/PeriodicBlockDeviceEraser.h
source/PeriodicBlockDeviceEraser.cpp
)

target_link_libraries(${APP_TARGET}
PRIVATE
mbed-os
mbed-events
mbed-ble
mbed-storage
mbed-mbedtls
ble-service-fota
)

mbed_set_post_build(${APP_TARGET})

option(VERBOSE_BUILD "Have a verbose build process")
if(VERBOSE_BUILD)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()
72 changes: 72 additions & 0 deletions BLE_GattServer_FOTAService/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# FOTA Service Mock Example

The FOTA service, as defined in its [specification](https://github.com/ARMmbed/mbed-os-experimental-ble-services/blob/fota-service-github-ci/services/FOTA/docs/README.md) document, facilitates the transfer of firmware updates over BLE.

In this demo, the FOTA service is used to transfer a binary from the host PC into the flash of the target.
A basic _FOTA client_, implemented in Python using [bleak](https://pypi.org/project/bleak/), is used to read/write the binary stream, control and status characteristics.
Please refer to [Section 1](https://github.com/ARMmbed/mbed-os-experimental-ble-services/tree/fota-service-github-ci/services/FOTA/docs#fota-service-structure) of the spec to learn more about these characteristics.

To verify the success of the transfer, the application computes the [SHA-256](https://en.wikipedia.org/wiki/SHA-2) of the binary and prints it to the serial port.
The SHA-256 is also computed on the host using [sha256sum](https://man7.org/linux/man-pages/man1/sha256sum.1.html).
The transfer is regarded as a success if the hashes are equal.

noonfom marked this conversation as resolved.
Show resolved Hide resolved
## Usage

### Hardware Requirements

This application requires either the [DISCO_L475VG_IOT01A](https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/) or [NRF52840_DK](https://os.mbed.com/platforms/Nordic-nRF52-DK/) platforms.

To use the Python client, the host PC must have Bluetooth capabilities, either through an inbuilt chipset or an external USB adapter.

### Building

The application can be built and flashed onto the target using Mbed CLI 2:

```shell
mbed-tools deploy
mbed-tools compile -t <toolchain> -m <target> -f
```

A `bin` file is required for the demo.
For nRF, this must be created manually from the `elf` as Mbed Tools outputs a `hex` file by default:

```shell
arm-none-eabi-objcopy -O binary cmake_build/NRF52840_DK/develop/<toolchain>/BLE_GattServer_FOTAService.elf cmake_build/NRF52840_DK/develop/<toolchain>/BLE_GattServer_FOTAService.bin
```

Lastly, create a Python virtual environment inside the `scripts` folder and install bleak:

```shell
cd scripts && mkdir venv && virtualenv venv && source venv/bin/activate
python -m pip install --upgrade pip && pip install bleak
```

### Demonstration

1. Open a serial terminal on the host PC to receive serial prints from the _FOTA target_:

```shell
mbed term -b 115200
```

2. In a separate window, run the test script:

```
python test_fota.py
```

It scans for a device named 'FOTA' and attempts to connect to it.
Once connected, it asks the user to enter the path to the binary.
Use the binary running on the target:

```
Enter the path to the binary: ../cmake_build/<target>/develop/<toolchain>/BLE_GattServer_FOTAService.bin
```

3. The client initiates the transfer once the FOTA session begins and commits the update once the entire binary has been sent.
Subsequently, the target computes the SHA-256 of the binary and prints it to the serial.
Verify the `<hash>` with sha256sum:

```shell
echo "<hash> ../cmake_build/<target>/develop/<toolchain>/BLE_GattServer_FOTAService.bin" | sha256sum --check
noonfom marked this conversation as resolved.
Show resolved Hide resolved
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/ARMmbed/mbed-os-experimental-ble-services/#7f7ed190a4d5da02a1340aeff3ed2e7bc9ee5b64
1 change: 1 addition & 0 deletions BLE_GattServer_FOTAService/mbed-os.lib
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/noonfom/mbed-os/#27160dbe0460ed5c898effbaf8881c39117ba505
23 changes: 23 additions & 0 deletions BLE_GattServer_FOTAService/mbed_app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"target_overrides": {
"*": {
"platform.stdio-convert-newlines": true,
"platform.stdio-baud-rate": 115200,
"mbed-trace.enable": 1,
"mbed-trace.max-level": "TRACE_LEVEL_INFO",
"ble-api-implementation.max-characteristic-authorisation-count": 100
},
"DISCO_L475VG_IOT01A": {
"target.features_add": ["BLE"],
"cordio.desired-att-mtu": 200,
"cordio.rx-acl-buffer-size": 204
},
"NRF52840_DK": {
"target.features_add": ["BLE"],
"cordio.desired-att-mtu": 200,
"cordio.rx-acl-buffer-size": 204,
"cordio-nordic-ll.wsf-pool-buffer-size": 8192,
"cordio-ll.max-acl-size": 204
}
}
}
28 changes: 28 additions & 0 deletions BLE_GattServer_FOTAService/scripts/logging.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[loggers]
keys=root,logger

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=consoleHandler

[logger_logger]
level=INFO
handlers=consoleHandler
qualname=logger
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
Loading