Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 # Device Management for Mbed OS
maygup01 0:11cc2b7889af 2
maygup01 0:11cc2b7889af 3 (This was previously called Simple Mbed Cloud Client.)
maygup01 0:11cc2b7889af 4
maygup01 0:11cc2b7889af 5 This provides a way to add device management capabilities to Mbed OS devices using the Arm Pelion Device Management IoT platform.
maygup01 0:11cc2b7889af 6
maygup01 0:11cc2b7889af 7 It:
maygup01 0:11cc2b7889af 8
maygup01 0:11cc2b7889af 9 - Enables applications to connect and perform firmware updates in a few lines of code.
maygup01 0:11cc2b7889af 10 - Runs separately from your main application; it does not take over your main loop.
maygup01 0:11cc2b7889af 11 - Provides LWM2M resources, variables that sync automatically through Pelion Device Management.
maygup01 0:11cc2b7889af 12 - Helps users avoid doing blocking network operations in interrupt contexts, by automatically defering actions to a separate thread.
maygup01 0:11cc2b7889af 13 - Provides end-to-end Greentea tests for Pelion Device Management.
maygup01 0:11cc2b7889af 14
maygup01 0:11cc2b7889af 15 This library makes it trivial to expose sensors, actuators and other variables to a cloud service. For a complete Pelion Device Management Client API, please see our [documentation](https://cloud.mbed.com/docs/current/mbed-cloud-client/index.html).
maygup01 0:11cc2b7889af 16
maygup01 0:11cc2b7889af 17 ## Device management for your Mbed OS application
maygup01 0:11cc2b7889af 18
maygup01 0:11cc2b7889af 19 Not every device (microcontroller, module or board) is capable of running device management features. Although you can add or extend some hardware capabilities, such as connectivity, storage and TRNG, others are impossible or inconvenient to extend (for example, RAM or flash).
maygup01 0:11cc2b7889af 20
maygup01 0:11cc2b7889af 21 The minimum requirements to add device management feature to your application are:
maygup01 0:11cc2b7889af 22
maygup01 0:11cc2b7889af 23 - RAM: 128K or more.
maygup01 0:11cc2b7889af 24 - Flash: 512K or more.
maygup01 0:11cc2b7889af 25 - Real Time Clock (RTC).
maygup01 0:11cc2b7889af 26 - (Optional but recommended) True Random Number Generator (TRNG).
maygup01 0:11cc2b7889af 27 - A storage device: SD card, SPI flash, QSPI flash or data flash.
maygup01 0:11cc2b7889af 28 - IP connectivity: Ethernet, Wi-Fi, cellular, 6LoWPAN or Thread.
maygup01 0:11cc2b7889af 29
maygup01 0:11cc2b7889af 30 Additionally, we recommend the latest version of Mbed OS support the device and any additional complementary hardware components, or that they have support for the APIs provided in the latest releases of Mbed OS.
maygup01 0:11cc2b7889af 31
maygup01 0:11cc2b7889af 32 Useful references:
maygup01 0:11cc2b7889af 33
maygup01 0:11cc2b7889af 34 - Check which Mbed OS platforms are supported in the [Pelion Device Management quick start guide](https://cloud.mbed.com/quick-start).
maygup01 0:11cc2b7889af 35 - Check the [storage options available](https://os.mbed.com/docs/latest/reference/storage.html).
maygup01 0:11cc2b7889af 36 - Check the [network options available](https://os.mbed.com/docs/latest/reference/network-socket.html).
maygup01 0:11cc2b7889af 37
maygup01 0:11cc2b7889af 38 ### Adding a device management feature to your application
maygup01 0:11cc2b7889af 39
maygup01 0:11cc2b7889af 40 1. Add this library to your Mbed OS project:
maygup01 0:11cc2b7889af 41
maygup01 0:11cc2b7889af 42 ```
maygup01 0:11cc2b7889af 43 $ mbed add https://github.com/ARMmbed/simple-mbed-cloud-client
maygup01 0:11cc2b7889af 44 ```
maygup01 0:11cc2b7889af 45
maygup01 0:11cc2b7889af 46 If you do not have an Mbed OS project to add, you can create one with `mbed new <your_application_name>` and then the `mbed add` step above.
maygup01 0:11cc2b7889af 47
maygup01 0:11cc2b7889af 48 1. Reference the library from the `main.cpp` file, and add network and storage drivers. Finally, initialize the SimpleMbedCloudClient library. This is the architecture of a device management application with Mbed OS:
maygup01 0:11cc2b7889af 49
maygup01 0:11cc2b7889af 50 ```cpp
maygup01 0:11cc2b7889af 51 #include "simple-mbed-cloud-client.h"
maygup01 0:11cc2b7889af 52 #include <Block device>
maygup01 0:11cc2b7889af 53 #include <Filesystem>
maygup01 0:11cc2b7889af 54 #include <Network>
maygup01 0:11cc2b7889af 55
maygup01 0:11cc2b7889af 56 int main() {
maygup01 0:11cc2b7889af 57
maygup01 0:11cc2b7889af 58 /* Initialize connectivity */
maygup01 0:11cc2b7889af 59 NetworkInterface *net = NetworkInterface::get_default_instance();
maygup01 0:11cc2b7889af 60 net->connect();
maygup01 0:11cc2b7889af 61
maygup01 0:11cc2b7889af 62 /* Initialize storage */
maygup01 0:11cc2b7889af 63 BlockDevice *bd = BlockDevice::get_default_instance();
maygup01 0:11cc2b7889af 64 <Filesystem> fs("fs", &bd);
maygup01 0:11cc2b7889af 65
maygup01 0:11cc2b7889af 66 /* Initialize SimpleMbedCloudClient */
maygup01 0:11cc2b7889af 67 SimpleMbedCloudClient client(net, &bd, &fs);
maygup01 0:11cc2b7889af 68 client.init();
maygup01 0:11cc2b7889af 69
maygup01 0:11cc2b7889af 70 /* Create resource */
maygup01 0:11cc2b7889af 71 MbedCloudClientResource *variable;
maygup01 0:11cc2b7889af 72 variable = client.create_resource("3201/0/5853", "variable");
maygup01 0:11cc2b7889af 73 variable->set_value("assign new value");
maygup01 0:11cc2b7889af 74 variable->methods(M2MMethod::GET | M2MMethod::PUT);
maygup01 0:11cc2b7889af 75
maygup01 0:11cc2b7889af 76 }
maygup01 0:11cc2b7889af 77 ```
maygup01 0:11cc2b7889af 78
maygup01 0:11cc2b7889af 79 1. Configure the API key for your Pelion Portal account.
maygup01 0:11cc2b7889af 80
maygup01 0:11cc2b7889af 81 If you don't have an API key available, then log in to the [Pelion IoT Platform portal](https://portal.mbedcloud.com/), navigate to `Access Management` and `API keys`, and create a new one. Then specify the API key as the global `mbed` configuration:
maygup01 0:11cc2b7889af 82
maygup01 0:11cc2b7889af 83 ```
maygup01 0:11cc2b7889af 84 $ mbed config -G CLOUD_SDK_API_KEY <your-api-key>
maygup01 0:11cc2b7889af 85 ```
maygup01 0:11cc2b7889af 86
maygup01 0:11cc2b7889af 87 1. Install the device management certificate:
maygup01 0:11cc2b7889af 88
maygup01 0:11cc2b7889af 89 ```
maygup01 0:11cc2b7889af 90 $ mbed dm init -d "<your company name.com>" --model-name "<product model identifier>"
maygup01 0:11cc2b7889af 91 ```
maygup01 0:11cc2b7889af 92
maygup01 0:11cc2b7889af 93 This creates your private and public key pair and also initialize various `.c` files with these credentials, so you can use Connect and (firmware) Update device management features.
maygup01 0:11cc2b7889af 94
maygup01 0:11cc2b7889af 95 ### Example applications
maygup01 0:11cc2b7889af 96
maygup01 0:11cc2b7889af 97 To help you start quickly, please refer to the following [application example](https://github.com/ARMmbed/pelion-ready-example). It demonstrates how to connect to the Pelion IoT Platform service, register resources and get ready to receive a firmware update.
maygup01 0:11cc2b7889af 98
maygup01 0:11cc2b7889af 99 Also, there are a number of board-specific applications that focus on providing more elaborate hardware features with Mbed OS and the Pelion IoT Platform. These are available in the Pelion [quick start](https://cloud.mbed.com/quick-start). Please see the reference table below, organized by vendor name, for details:
maygup01 0:11cc2b7889af 100
maygup01 0:11cc2b7889af 101 Platform | Connectivity | Storage | Example URL
maygup01 0:11cc2b7889af 102 --------------------------------| -------------------| --------- | --------------------
maygup01 0:11cc2b7889af 103 Nuvoton NUMAKER-IOT-M487 | Wi-Fi | SD Card | https://os.mbed.com/teams/Nuvoton/code/pelion-example-common/
maygup01 0:11cc2b7889af 104 Nuvoton NUMAKER-PFM-M487 | Ethernet | SD Card | https://os.mbed.com/teams/Nuvoton/code/pelion-example-common/
maygup01 0:11cc2b7889af 105 Nuvoton NUMAKER-PFM-NUC472 | Ethernet | SD Card |https://os.mbed.com/teams/Nuvoton/code/pelion-example-common/
maygup01 0:11cc2b7889af 106 NXP FRDM-K64F | Ethernet | SD Card | https://os.mbed.com/teams/NXP/code/mbed-cloud-connect-example-ethernet
maygup01 0:11cc2b7889af 107 NXP FRDM-K66F | Ethernet | SD Card | https://os.mbed.com/teams/NXP/code/mbed-cloud-connect-example-ethernet
maygup01 0:11cc2b7889af 108 Renesas GR-LCYHEE | Wi-Fi | SD Card | https://os.mbed.com/teams/Renesas/code/pelion-example-common/
maygup01 0:11cc2b7889af 109 Sigma Delta Technologies SDT64B | Ethernet | SD Card | https://os.mbed.com/teams/Sigma-Delta-Technologies/code/pelion-example-common
maygup01 0:11cc2b7889af 110 ST DISCO_L475E_IOT01A | Wi-Fi | QSPI | https://os.mbed.com/teams/ST/code/pelion-example-disco-iot01/
maygup01 0:11cc2b7889af 111 ST DISCO_F413H | Wi-Fi | QSPI | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 112 ST DISCO_F746NG | Ethernet | QSPI | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 113 ST NUCLEO_F429ZI | Ethernet | SD Card | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 114 ST NUCLEO_F767ZI | Ethernet | SD Card | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 115 ST NUCLEO_F746ZG | Ethernet | SD Card | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 116 ST NUCLEO_F207ZG | Ethernet | SD Card | https://os.mbed.com/teams/ST/code/pelion-example-common/
maygup01 0:11cc2b7889af 117 UBlox EVK ODIN W2 | Wi-Fi | SD Card | https://os.mbed.com/teams/ublox/code/pelion-example-common/
maygup01 0:11cc2b7889af 118 UBlox C030 U201 | Cellular | SD Card | https://os.mbed.com/teams/ublox/code/pelion-example-common/
maygup01 0:11cc2b7889af 119
maygup01 0:11cc2b7889af 120 ## Device management configuration
maygup01 0:11cc2b7889af 121
maygup01 0:11cc2b7889af 122 The device management configuration has five distinct areas:
maygup01 0:11cc2b7889af 123
maygup01 0:11cc2b7889af 124 - Connectivity - the transport type for the device to connect to the device management service.
maygup01 0:11cc2b7889af 125 - Storage - the storage type and writing used for both the credentials and the firmware storage.
maygup01 0:11cc2b7889af 126 - Flash geometry - the device flash "sandbox" for bootloader, application header, application image and two SOTP regions.
maygup01 0:11cc2b7889af 127 - SOTP - the address and size of the SOTP regions in flash used to store the device special key that decrypts the credentials storage.
maygup01 0:11cc2b7889af 128 - Bootloader - the bootloader image, application and header offset.
maygup01 0:11cc2b7889af 129
maygup01 0:11cc2b7889af 130 Except for connectivity, the majority of the configuration is shared between the application and bootloader, which ensures the bootloader can correctly find, verify, authorize and apply an update to the device application.
maygup01 0:11cc2b7889af 131
maygup01 0:11cc2b7889af 132 For full documentation about bootloaders and firmware update, please read the following documents:
maygup01 0:11cc2b7889af 133
maygup01 0:11cc2b7889af 134 - [Introduction to Mbed OS bootloaders](https://os.mbed.com/docs/latest/porting/bootloader.html).
maygup01 0:11cc2b7889af 135 - [Creating and using an Mbed OS bootloader](https://os.mbed.com/docs/latest/tutorials/bootloader.html).
maygup01 0:11cc2b7889af 136 - [Bootloader configuration in Mbed OS](https://os.mbed.com/docs/latest/tools/configuring-tools.html).
maygup01 0:11cc2b7889af 137 - [Mbed Bootloader for Pelion Device Management](https://github.com/ARMmbed/mbed-bootloader).
maygup01 0:11cc2b7889af 138 - [Updating devices with Mbed CLI](https://os.mbed.com/docs/latest/tools/cli-update.html).
maygup01 0:11cc2b7889af 139
maygup01 0:11cc2b7889af 140 To hasten this process, you can copy the configuration from the [application example](https://github.com/ARMmbed/pelion-ready-example/blob/master/mbed_app.json) as the basis for your application configuration.
maygup01 0:11cc2b7889af 141
maygup01 0:11cc2b7889af 142 ### 1. Application configuration
maygup01 0:11cc2b7889af 143
maygup01 0:11cc2b7889af 144 Edit the `mbed_app.json` file, and create a new entry under `target_overrides` with the target name for your device:
maygup01 0:11cc2b7889af 145
maygup01 0:11cc2b7889af 146 - **Connectivity** - Specify the default connectivity type for your target. It's essential with targets that lack default connectivity set in `targets.json` or for targets that support multiple connectivity options. For example:
maygup01 0:11cc2b7889af 147
maygup01 0:11cc2b7889af 148 ```
maygup01 0:11cc2b7889af 149 "target.network-default-interface-type" : "ETHERNET",
maygup01 0:11cc2b7889af 150 ```
maygup01 0:11cc2b7889af 151
maygup01 0:11cc2b7889af 152 The possible options are `ETHERNET`, `WIFI` and `CELLULAR`.
maygup01 0:11cc2b7889af 153
maygup01 0:11cc2b7889af 154 Depending on connectivity type, you might have to specify more config options. For an example, please see the defined `CELLULAR` targets in `mbed_app.json`.
maygup01 0:11cc2b7889af 155
maygup01 0:11cc2b7889af 156 - **Storage** - Specify the storage block device type, which dynamically adds the block device driver you specified at compile time. For example:
maygup01 0:11cc2b7889af 157
maygup01 0:11cc2b7889af 158 ```
maygup01 0:11cc2b7889af 159 "target.components_add" : ["SD"],
maygup01 0:11cc2b7889af 160 ```
maygup01 0:11cc2b7889af 161
maygup01 0:11cc2b7889af 162 Valid options are `SD`, `SPIF`, `QSPIF` and `FLASHIAP` (not recommended). For more available options, please see the [block device components](https://github.com/ARMmbed/mbed-os/tree/master/components/storage/blockdevice).
maygup01 0:11cc2b7889af 163
maygup01 0:11cc2b7889af 164 You also have to specify block device pin configuration, which may vary from one block device type to another. Here's an example for `SD`:
maygup01 0:11cc2b7889af 165
maygup01 0:11cc2b7889af 166 ```
maygup01 0:11cc2b7889af 167 "sd.SPI_MOSI" : "PE_6",
maygup01 0:11cc2b7889af 168 "sd.SPI_MISO" : "PE_5",
maygup01 0:11cc2b7889af 169 "sd.SPI_CLK" : "PE_2",
maygup01 0:11cc2b7889af 170 "sd.SPI_CS" : "PE_4",
maygup01 0:11cc2b7889af 171 ```
maygup01 0:11cc2b7889af 172
maygup01 0:11cc2b7889af 173 If you are using SPI/QSPI flash, please make sure you have specified the correct SPI frequency by configuring `spif-driver.SPI_FREQ`. If it is not configured, 40Mhz will be applied by default.
maygup01 0:11cc2b7889af 174
maygup01 0:11cc2b7889af 175 - **Flash** - Define the basics for the microcontroller flash. For example:
maygup01 0:11cc2b7889af 176
maygup01 0:11cc2b7889af 177 ```
maygup01 0:11cc2b7889af 178 "device-management.flash-start-address" : "0x08000000",
maygup01 0:11cc2b7889af 179 "device-management.flash-size" : "(2048*1024)",
maygup01 0:11cc2b7889af 180 ```
maygup01 0:11cc2b7889af 181
maygup01 0:11cc2b7889af 182 - **SOTP** - Define two SOTP or NVStore regions that Mbed OS Device Management will use to store its special keys, which encrypt the data stored. Use the last two Flash sectors (if possible) to ensure that they don't get overwritten when new firmware is applied. For example:
maygup01 0:11cc2b7889af 183
maygup01 0:11cc2b7889af 184 ```
maygup01 0:11cc2b7889af 185 "device-management.sotp-section-1-address" : "(MBED_CONF_APP_FLASH_START_ADDRESS + MBED_CONF_APP_FLASH_SIZE - 2*(128*1024))",
maygup01 0:11cc2b7889af 186 "device-management.sotp-section-1-size" : "(128*1024)",
maygup01 0:11cc2b7889af 187 "device-management.sotp-section-2-address" : "(MBED_CONF_APP_FLASH_START_ADDRESS + MBED_CONF_APP_FLASH_SIZE - 1*(128*1024))",
maygup01 0:11cc2b7889af 188 "device-management.sotp-section-2-size" : "(128*1024)",
maygup01 0:11cc2b7889af 189 "device-management.sotp-num-sections" : 2
maygup01 0:11cc2b7889af 190 ```
maygup01 0:11cc2b7889af 191
maygup01 0:11cc2b7889af 192 `*-address` defines the start of the Flash sector, and `*-size` defines the actual sector size. `sotp-num-sections` should always be set to `2`.
maygup01 0:11cc2b7889af 193
maygup01 0:11cc2b7889af 194 At this point, we recommend you run the "connect" test suite, which verifies that the device can successfully bootstrap, register and send and receive data from Pelion Device Management service with the provided configuration.
maygup01 0:11cc2b7889af 195
maygup01 0:11cc2b7889af 196 If you already configured your Pelion API key and initialized your credentials as described in the [previous section](#adding-device-management-feature-to-your-application), you can compile the "Connect" tests using:
maygup01 0:11cc2b7889af 197
maygup01 0:11cc2b7889af 198 ```
maygup01 0:11cc2b7889af 199 $ mbed test -t <TOOLCHAIN> -m <BOARD> -n simple*dev*connect -DMBED_TEST_MODE --compile
maygup01 0:11cc2b7889af 200 ```
maygup01 0:11cc2b7889af 201
maygup01 0:11cc2b7889af 202 To run the tests:
maygup01 0:11cc2b7889af 203
maygup01 0:11cc2b7889af 204 ```
maygup01 0:11cc2b7889af 205 $ mbed test -t <TOOLCHAIN> -m <BOARD> -n simple*dev*connect --run -v
maygup01 0:11cc2b7889af 206 ```
maygup01 0:11cc2b7889af 207
maygup01 0:11cc2b7889af 208 ### 2. Bootloader configuration
maygup01 0:11cc2b7889af 209
maygup01 0:11cc2b7889af 210 After you've successfully passed the "Connect" tests as described above, you can enable firmware update feature by adding a bootloader to your application.
maygup01 0:11cc2b7889af 211
maygup01 0:11cc2b7889af 212 1. Import as a new application the official [mbed-bootloader](https://github.com/ARMmbed/mbed-bootloader/) repository or the [mbed-bootloader-extended](https://github.com/ARMmbed/mbed-bootloader-extended/) repository that builds on top of `mbed-bootloader` and extends the support for file systems and storage drivers. You can do this with `mbed import mbed-bootloader-extended`.
maygup01 0:11cc2b7889af 213
maygup01 0:11cc2b7889af 214 1. Inside the imported bootloader application, and edit the application configuration, for example `mbed-bootloader-extended/mbed_app.json`. Add a new target entry similar to the step above, and specify:
maygup01 0:11cc2b7889af 215
maygup01 0:11cc2b7889af 216 - **Flash** - Define the basics for the microcontroller flash (the same as in `mbed_app.json`). For example:
maygup01 0:11cc2b7889af 217
maygup01 0:11cc2b7889af 218 ```
maygup01 0:11cc2b7889af 219 "flash-start-address" : "0x08000000",
maygup01 0:11cc2b7889af 220 "flash-size" : "(2048*1024)",
maygup01 0:11cc2b7889af 221 ```
maygup01 0:11cc2b7889af 222
maygup01 0:11cc2b7889af 223 - **SOTP** - Similar to the **SOTP** step above, specify the location of the SOTP key storage. In the bootloader, the variables are named differently. Try to use the last two Flash sectors (if possible) to ensure that they don't get overwritten when new firmware is applied. For example:
maygup01 0:11cc2b7889af 224
maygup01 0:11cc2b7889af 225 ```
maygup01 0:11cc2b7889af 226 "nvstore.area_1_address" : "(MBED_CONF_APP_FLASH_START_ADDRESS + MBED_CONF_APP_FLASH_SIZE - 2*(128*1024))",
maygup01 0:11cc2b7889af 227 "nvstore.area_1_size" : "(128*1024)",
maygup01 0:11cc2b7889af 228 "nvstore.area_2_address" : "(MBED_CONF_APP_FLASH_START_ADDRESS + MBED_CONF_APP_FLASH_SIZE - 1*(128*1024))", "nvstore.area_2_size" : "(128*1024)",
maygup01 0:11cc2b7889af 229 ```
maygup01 0:11cc2b7889af 230
maygup01 0:11cc2b7889af 231 - **Application offset** - Specify start address for the application, and also the update-client meta information. As these are automatically calculated, you can copy the ones below:
maygup01 0:11cc2b7889af 232
maygup01 0:11cc2b7889af 233 ```
maygup01 0:11cc2b7889af 234 "update-client.application-details": "(MBED_CONF_APP_FLASH_START_ADDRESS + 64*1024)",
maygup01 0:11cc2b7889af 235 "application-start-address" : "(MBED_CONF_APP_FLASH_START_ADDRESS + 65*1024)",
maygup01 0:11cc2b7889af 236 "max-application-size" : "DEFAULT_MAX_APPLICATION_SIZE",
maygup01 0:11cc2b7889af 237 ```
maygup01 0:11cc2b7889af 238
maygup01 0:11cc2b7889af 239 - **Storage** - Specify the block device pin configuration, exactly as you defined it in the `mbed_app.json` file. For example:
maygup01 0:11cc2b7889af 240
maygup01 0:11cc2b7889af 241 ```
maygup01 0:11cc2b7889af 242 "target.components_add" : ["SD"],
maygup01 0:11cc2b7889af 243 "sd.SPI_MOSI" : "PE_6",
maygup01 0:11cc2b7889af 244 "sd.SPI_MISO" : "PE_5",
maygup01 0:11cc2b7889af 245 "sd.SPI_CLK" : "PE_2",
maygup01 0:11cc2b7889af 246 "sd.SPI_CS" : "PE_4"
maygup01 0:11cc2b7889af 247 ```
maygup01 0:11cc2b7889af 248
maygup01 0:11cc2b7889af 249 If you are using SPI/QSPI flash, please make sure you have specified the correct SPI frequency by configuring `spif-driver.SPI_FREQ`. If it is not configured, 40Mhz will be applied by default.
maygup01 0:11cc2b7889af 250
maygup01 0:11cc2b7889af 251 1. Compile the bootloader using the `bootloader_app.json` configuration you just edited:
maygup01 0:11cc2b7889af 252
maygup01 0:11cc2b7889af 253 ```
maygup01 0:11cc2b7889af 254 $ mbed compile -t <TOOLCHAIN> -m <TARGET> --profile=tiny.json --app-config=<path to pelion-enablement/bootloader/bootloader_app.json>
maygup01 0:11cc2b7889af 255 ```
maygup01 0:11cc2b7889af 256
maygup01 0:11cc2b7889af 257 <span class="notes">**Note:** `mbed-bootloader` is primarily optimized for `GCC_ARM`, so you may want to compile it with that toolchain.
maygup01 0:11cc2b7889af 258 Before jumping to the next step, you should compile and flash the bootloader and then connect over the virtual comport to ensure the bootloader is running correctly. You can ignore errors related to checksum verification or falure to jump to application - these are expected at this stage.</span>
maygup01 0:11cc2b7889af 259
maygup01 0:11cc2b7889af 260 ### 3. Add the bootloader to your application
maygup01 0:11cc2b7889af 261
maygup01 0:11cc2b7889af 262 1. Copy the compiled bootloader from `mbed-bootloader-extended/BUILDS/<TARGET>/<TOOLCHAIN>-TINY/mbed-bootloader.bin` to `<your_application_name>/bootloader/mbed-bootloader-<TARGET>.bin`.
maygup01 0:11cc2b7889af 263
maygup01 0:11cc2b7889af 264 1. Edit `<your_application_name>/mbed_app.json`, and modify the target entry to include:
maygup01 0:11cc2b7889af 265
maygup01 0:11cc2b7889af 266 ```
maygup01 0:11cc2b7889af 267 "target.features_add" : ["BOOTLOADER"],
maygup01 0:11cc2b7889af 268 "target.bootloader_img" : "bootloader/mbed-bootloader-<TARGET>.bin",
maygup01 0:11cc2b7889af 269 "target.app_offset" : "0x10400",
maygup01 0:11cc2b7889af 270 "target.header_offset" : "0x10000",
maygup01 0:11cc2b7889af 271 "update-client.application-details": "(MBED_CONF_APP_FLASH_START_ADDRESS + 64*1024)",
maygup01 0:11cc2b7889af 272 ```
maygup01 0:11cc2b7889af 273
maygup01 0:11cc2b7889af 274 <span class="notes">**Note:**
maygup01 0:11cc2b7889af 275 - `update-client.application-details` should be identical in both `bootloader_app.json` and `mbed_app.json`.
maygup01 0:11cc2b7889af 276 - `target.app_offset` is relative offset to `flash-start-address` you specified in `mbed_app.json` and `bootloader_app.json`, and is the hex value of the offset specified by `application-start-address` in `bootloader_app.json`. For example, `(MBED_CONF_APP_FLASH_START_ADDRESS+65*1024)` dec equals `0x10400` hex.
maygup01 0:11cc2b7889af 277 - `target.header_offset` is also relative offset to the `flash-start-address` you specified in the `bootloader_app.json`, and is the hex value of the offset specified by `update-client.application-details`. For example, `(MBED_CONF_APP_FLASH_START_ADDRESS+64*1024)` dec equals `0x10000` hex.</span>
maygup01 0:11cc2b7889af 278
maygup01 0:11cc2b7889af 279 1. Finally, compile and rerun all tests, including the firmware update ones with:
maygup01 0:11cc2b7889af 280
maygup01 0:11cc2b7889af 281 ```
maygup01 0:11cc2b7889af 282 $ mbed test -t <TOOLCHAIN> -m <BOARD> -n simple-mbed-cloud-client-tests-* -DMBED_TEST_MODE --compile
maygup01 0:11cc2b7889af 283
maygup01 0:11cc2b7889af 284 $ mbed test -t <TOOLCHAIN> -m <BOARD> -n simple-mbed-cloud-client-tests-* --run -v
maygup01 0:11cc2b7889af 285 ```
maygup01 0:11cc2b7889af 286
maygup01 0:11cc2b7889af 287 Refer to the next section about what tests are being executed.
maygup01 0:11cc2b7889af 288
maygup01 0:11cc2b7889af 289 ## Validation and testing
maygup01 0:11cc2b7889af 290
maygup01 0:11cc2b7889af 291 Mbed Device Management provides built-in tests to help you when define your device management configuration. Before running these tests, we recommend you refer to the [testing setup](#testing-setup) section below.
maygup01 0:11cc2b7889af 292
maygup01 0:11cc2b7889af 293 ### Test suites
maygup01 0:11cc2b7889af 294
maygup01 0:11cc2b7889af 295 | **Test suite** | **Description** |
maygup01 0:11cc2b7889af 296 | ------------- | ------------- |
maygup01 0:11cc2b7889af 297 | `fs-single` | File system single-threaded tests with write buffer sizes - 1 byte, 4b, 16b, 64b, 256b, 1kb, 4kb, 16kb, 64kb. |
maygup01 0:11cc2b7889af 298 | `fs-multi` | File system multithreaded test with write buffer sizes - 1 byte, 4b, 16b, 64b, 256b, 1kb, 4kb, 16kb, 64kb. |
maygup01 0:11cc2b7889af 299 | `net-single` | Network single-threaded test with receive buffer sizes - 128 bytes, 256b, 1kb, 2kb, 4kb. |
maygup01 0:11cc2b7889af 300 | `net-multi` | Network multithreaded test for 1, 2 and 3 download threads with 1kb receive buffer size. |
maygup01 0:11cc2b7889af 301 | `stress-net-fs` | Network and file system single and multithreaded tests:<ul><li>memory allocation - 10k, 20k, 40k, 60k</li><li>1 thread (sequential) - 1 download (1kb buffer), 1 file thread (1kb buffer)</li><li>2 parallel threads - 1 download, 1 file thread (1kb buffer)</li><li>3 parallel threads - 1 download, 2 file (256 bytes, 1 kb buffer)</li><li>4 parallel threads - 1 download, 3 file (1 byte, 256 bytes, 1kb buffer)</li></ul> |
maygup01 0:11cc2b7889af 302
maygup01 0:11cc2b7889af 303 ### Test cases - connect
maygup01 0:11cc2b7889af 304
maygup01 0:11cc2b7889af 305 | **Test case** | **Description** |
maygup01 0:11cc2b7889af 306 | ------------- | ------------- |
maygup01 0:11cc2b7889af 307 | `Connect to <Network type>` | Tests the connection to the network through the network interface. |
maygup01 0:11cc2b7889af 308 | `Initialize <Blockdevice>+<Filesystem>` | Initializes the block device driver and file system on top. Usually, the test will be stuck at this point if there's a problem with the storage device. |
maygup01 0:11cc2b7889af 309 | `Format <Filesystem>` | Tests that you can successfully format the block device for the file system type. |
maygup01 0:11cc2b7889af 310 | `Initialize Simple PDMC ` | Verifies you can initialize Pelion Device Management with the given network, storage and file system configuration. This is where the FCU and KCM configuration is written to storage and the Root of Trust is written to SOTP.
maygup01 0:11cc2b7889af 311 | `Pelion DM Bootstrap & Register` | Bootstraps the device and registers it for first time with Pelion Device Management. |
maygup01 0:11cc2b7889af 312 | `Pelion DM Directory` | Verifies that a registered device appears in the Device Directory in Pelion Device Management. |
maygup01 0:11cc2b7889af 313 | `Pelion DM Re-register` | Resets the device and reregisters with Pelion Device Management with previously bootstrapped credentials. |
maygup01 0:11cc2b7889af 314 | `Post-reset Identity` | Verifies that the device identity is preserved over device reset, confirming that Root of Trust is stored in SOTP correctly. |
maygup01 0:11cc2b7889af 315 | `ResourceLwM2M GET` | Verifies that the device can perform a GET request on an LwM2M resource. |
maygup01 0:11cc2b7889af 316 | `ResourceLwM2M SET Test` | Sets or changes value from the device and verifies the Pelion Device Management API client can observe the value changing. |
maygup01 0:11cc2b7889af 317 | `ResourceLwM2M PUT Test` | Verifies the device can perform a PUT request on an LwM2M resource by setting a new value. |
maygup01 0:11cc2b7889af 318 | `Resource LwM2M POST Test` | Verifies the device can execute a POST on an LwM2M resource and the callback function on the device is called. |
maygup01 0:11cc2b7889af 319
maygup01 0:11cc2b7889af 320 ### Test cases - update
maygup01 0:11cc2b7889af 321
maygup01 0:11cc2b7889af 322 | **Test case** | **Description** |
maygup01 0:11cc2b7889af 323 | ------------- | ------------- |
maygup01 0:11cc2b7889af 324 | `Connect to <Network type>` | Tests the connection to the network using the network interface. |
maygup01 0:11cc2b7889af 325 | `Initialize <Blockdevice>+<Filesystem>` | Initializes block device driver and file system on top. Usually the test will be stuck at this point if there's problem with the storage device. |
maygup01 0:11cc2b7889af 326 | `Format <Filesystem>` | Tests that you can successfully format the block device for the file system type. |
maygup01 0:11cc2b7889af 327 | `Initialize Simple PDMC ` | Verifies you can initialize Pelion Device Management with the given network, storage and file system configuration. This is where the FCU and KCM configuration is written to storage and the Root of Trust is written to SOTP.
maygup01 0:11cc2b7889af 328 | `Pelion DM Bootstrap & Register` | Bootstraps the device and registers it for first time with Pelion Device Management. |
maygup01 0:11cc2b7889af 329 | `Pelion DM Directory` | Verifies a registered device appears in the Device Directory in Pelion Device Management. |
maygup01 0:11cc2b7889af 330 | `Firmware Prepare` | Prepares the firmware on the host side and calls `mbed dm` to initiate the Pelion Device Management update campaign. |
maygup01 0:11cc2b7889af 331 | `Firmware Download` | Downloads the firmware onto the device. |
maygup01 0:11cc2b7889af 332 | `Firmware Update` | Resets the device, verifies that the firmware has correct checksum, applies it and reverifies the applied firmware checksum. |
maygup01 0:11cc2b7889af 333 | `Pelion DM Re-register` | Reregisters the device with Pelion Device Management using the new firmware and previously bootstrapped credentials. |
maygup01 0:11cc2b7889af 334 | `Post-update Identity` | Verifies that the device identity is preserved over firmware update and device reset, confirming that Root of Trust is stored in SOTP correctly. |
maygup01 0:11cc2b7889af 335
maygup01 0:11cc2b7889af 336 ### Requirements
maygup01 0:11cc2b7889af 337
maygup01 0:11cc2b7889af 338 Mbed Device Management tests rely on the Python SDK to test the end-to-end solution. To install the Python SDK:
maygup01 0:11cc2b7889af 339
maygup01 0:11cc2b7889af 340 ```
maygup01 0:11cc2b7889af 341 $ pip install mbed-cloud-sdk
maygup01 0:11cc2b7889af 342 ```
maygup01 0:11cc2b7889af 343
maygup01 0:11cc2b7889af 344 <span class="notes">**Note:** The Python SDK requires Python 2.7.10+ or Python 3.4.3+, built with SSL support.</span>
maygup01 0:11cc2b7889af 345
maygup01 0:11cc2b7889af 346 ### Testing setup
maygup01 0:11cc2b7889af 347
maygup01 0:11cc2b7889af 348 1. Import an example application for Pelion Device Management that contains the corresponding configuration for your target.
maygup01 0:11cc2b7889af 349
maygup01 0:11cc2b7889af 350 Please refer to the following [application example](https://github.com/ARMmbed/pelion-ready-example). It demonstrates how to connect to the Pelion IoT Platform service, register resources and get ready to receive a firmware update.
maygup01 0:11cc2b7889af 351
maygup01 0:11cc2b7889af 352 Also, there are board-specific applications that focus on providing more elaborate hardware features with Mbed OS and the Pelion IoT Platform. These are available in the Pelion [quick start](https://cloud.mbed.com/quick-start).
maygup01 0:11cc2b7889af 353
maygup01 0:11cc2b7889af 354 1. Set a global `mbed config` variable `CLOUD_SDK_API_KEY` on the host machine valid for the account your device will connect to. For example:
maygup01 0:11cc2b7889af 355
maygup01 0:11cc2b7889af 356 ```
maygup01 0:11cc2b7889af 357 $ mbed config -G CLOUD_SDK_API_KEY <API_KEY>
maygup01 0:11cc2b7889af 358 ```
maygup01 0:11cc2b7889af 359
maygup01 0:11cc2b7889af 360 For instructions on how to generate an API key, please [see the documentation](https://cloud.mbed.com/docs/current/integrate-web-app/api-keys.html#generating-an-api-key).
maygup01 0:11cc2b7889af 361
maygup01 0:11cc2b7889af 362 1. Initialize your Pelion DM credentials (once per project):
maygup01 0:11cc2b7889af 363
maygup01 0:11cc2b7889af 364 ```
maygup01 0:11cc2b7889af 365 $ mbed dm init -d "<your company name.com>" --model-name "<product model identifier>"
maygup01 0:11cc2b7889af 366 ```
maygup01 0:11cc2b7889af 367
maygup01 0:11cc2b7889af 368 This creates your private and public key pair and also initializes various `.c` files with these credentials, so you can use Connect and (firmware) Update device management features.
maygup01 0:11cc2b7889af 369
maygup01 0:11cc2b7889af 370 1. Remove the `main.cpp` application from the project, or ensure the content of the file is wrapped with `#ifndef MBED_TEST_MODE`.
maygup01 0:11cc2b7889af 371
maygup01 0:11cc2b7889af 372 1. Compile the tests with the `MBED_TEST_MODE` compilation flag.
maygup01 0:11cc2b7889af 373
maygup01 0:11cc2b7889af 374 ```
maygup01 0:11cc2b7889af 375 $ mbed test -t <toolchain> -m <platform> -n simple-mbed-cloud-client-tests-* -DMBED_TEST_MODE --compile
maygup01 0:11cc2b7889af 376 ```
maygup01 0:11cc2b7889af 377
maygup01 0:11cc2b7889af 378 1. Run the tests from the application directory:
maygup01 0:11cc2b7889af 379
maygup01 0:11cc2b7889af 380 ```
maygup01 0:11cc2b7889af 381 $ mbed test -t <toolchain> -m <platform> -n simple-mbed-cloud-client-tests-* --run -v
maygup01 0:11cc2b7889af 382 ```
maygup01 0:11cc2b7889af 383
maygup01 0:11cc2b7889af 384 ### Troubleshooting
maygup01 0:11cc2b7889af 385
maygup01 0:11cc2b7889af 386 Below are common issues and fixes.
maygup01 0:11cc2b7889af 387
maygup01 0:11cc2b7889af 388 #### Autoformatting failed with error -5005
maygup01 0:11cc2b7889af 389
maygup01 0:11cc2b7889af 390 This is due to an issue with the storage block device. If using an SD card, ensure that the SD card is seated properly.
maygup01 0:11cc2b7889af 391
maygup01 0:11cc2b7889af 392 #### Storage initialization failed with error -4002
maygup01 0:11cc2b7889af 393
maygup01 0:11cc2b7889af 394 This is observed when the device is using legacy serial flash which does not support SFDP, or the SPI frequency is not configured properly.
maygup01 0:11cc2b7889af 395
maygup01 0:11cc2b7889af 396 #### SYNC_FAILED during testing
maygup01 0:11cc2b7889af 397
maygup01 0:11cc2b7889af 398 Occasionally, if the test failed during a previous attempt, the SMCC Greentea tests fail to sync. If this is the case, please replug your device to the host PC. Additionally, you may need to update your DAPLink or ST-Link interface firmware.
maygup01 0:11cc2b7889af 399
maygup01 0:11cc2b7889af 400 If the test fails with SYNC_FAILED all the time, please check if the UART flow control pins (UART_CTS, UART_RTS) are properly defined. If your device supports flow control over UART, fill them with the corresponding pins; if not, please specify NC.
maygup01 0:11cc2b7889af 401
maygup01 0:11cc2b7889af 402 #### Device identity is inconsistent
maygup01 0:11cc2b7889af 403
maygup01 0:11cc2b7889af 404 If your device ID in Pelion Device Management is inconsistent over a device reset, it could be because it is failing to open the credentials on the storage held in the Enhanced Secure File System. Typically, this is because the device cannot access the Root of Trust stored in SOTP.
maygup01 0:11cc2b7889af 405
maygup01 0:11cc2b7889af 406 One way to verify this is to see if the storage is reformatted after a device reset when `format-storage-layer-on-error` is set to `1` in `mbed_app.json`. It would appear on the serial terminal output from the device as:
maygup01 0:11cc2b7889af 407
maygup01 0:11cc2b7889af 408 ```
maygup01 0:11cc2b7889af 409 [SMCC] Initializing storage.
maygup01 0:11cc2b7889af 410 [SMCC] Autoformatting the storage.
maygup01 0:11cc2b7889af 411 [SMCC] Reset storage to an empty state.
maygup01 0:11cc2b7889af 412 [SMCC] Starting developer flow
maygup01 0:11cc2b7889af 413 ```
maygup01 0:11cc2b7889af 414
maygup01 0:11cc2b7889af 415 When this occurs, look at the SOTP sectors defined in `mbed_app.json`:
maygup01 0:11cc2b7889af 416
maygup01 0:11cc2b7889af 417 ```
maygup01 0:11cc2b7889af 418 "sotp-section-1-address" : "0xFE000",
maygup01 0:11cc2b7889af 419 "sotp-section-1-size" : "0x1000",
maygup01 0:11cc2b7889af 420 "sotp-section-2-address" : "0xFF000",
maygup01 0:11cc2b7889af 421 "sotp-section-2-size" : "0x1000",
maygup01 0:11cc2b7889af 422 ```
maygup01 0:11cc2b7889af 423
maygup01 0:11cc2b7889af 424 Ensure that the sectors are correct according to the flash layout of your device, and they are not being overwritten during the programming of the device. ST-Link devices overwrite these sectors when you use drag-and-drop of `.bin` files. Thus, moving the SOTP sectors to the end sectors of flash ensures they are not overwritten.
maygup01 0:11cc2b7889af 425
maygup01 0:11cc2b7889af 426 #### Stack overflow
maygup01 0:11cc2b7889af 427
maygup01 0:11cc2b7889af 428 If you receive a stack overflow error, increase the Mbed OS main stack size to at least 6000. You can do this by modifying the following parameter in `mbed_app.json`:
maygup01 0:11cc2b7889af 429
maygup01 0:11cc2b7889af 430 ```
maygup01 0:11cc2b7889af 431 "MBED_CONF_APP_MAIN_STACK_SIZE=6000",
maygup01 0:11cc2b7889af 432 ```
maygup01 0:11cc2b7889af 433
maygup01 0:11cc2b7889af 434 #### Device failed to register
maygup01 0:11cc2b7889af 435
maygup01 0:11cc2b7889af 436 Check the device allocation on your Pelion account to see if you are allowed additional devices to connect. You can delete development devices. After being deleted, they will not count toward your allocation.
maygup01 0:11cc2b7889af 437
maygup01 0:11cc2b7889af 438 #### In network test cases, tests over larger buffers passed, but tests over small buffers keeps failing
maygup01 0:11cc2b7889af 439 This could be observed with cellular modems driven by AT commands.
maygup01 0:11cc2b7889af 440 Suggestions:
maygup01 0:11cc2b7889af 441 1. Connect the modem to an serial interface which supports hardware flow control, and define MDMRTS and MDMCTS correspondingly.
maygup01 0:11cc2b7889af 442 2. Use the highest possible baud-rate of your modem, e.g. 115200bps
maygup01 0:11cc2b7889af 443 3. For the UART connected to your host PC, choose one which supports hardware flow control
maygup01 0:11cc2b7889af 444 4. Set the STDIO UART baud-rate to 230400bps by configuring `platform.stdio-baud-rate`.
maygup01 0:11cc2b7889af 445
maygup01 0:11cc2b7889af 446 #### With SPI/QSPI Flash, LittleFS got corrupted after firmware update test
maygup01 0:11cc2b7889af 447 If you observe logs such as `mbed assertation failed: block < lfs->cfg->block_count` or `invalid superblock`, etc., please check if partition mode is set to 0:
maygup01 0:11cc2b7889af 448 ```
maygup01 0:11cc2b7889af 449 "device-management.partition_mode" : 0
maygup01 0:11cc2b7889af 450 ```
maygup01 0:11cc2b7889af 451
maygup01 0:11cc2b7889af 452 #### Notification channel failures during LwM2M Resource test cases
maygup01 0:11cc2b7889af 453 This could be observed if a previously registered long-poll or webhook notification channel with the same API key existed.
maygup01 0:11cc2b7889af 454 You may either use another API key, or delete the notification channel with curl command:
maygup01 0:11cc2b7889af 455
maygup01 0:11cc2b7889af 456 to delete Long Poll:
maygup01 0:11cc2b7889af 457 ```
maygup01 0:11cc2b7889af 458 curl -H "Authorization: Bearer ${API_KEY}" -X DELETE ${API_URL}/v2/notification/pull
maygup01 0:11cc2b7889af 459 ```
maygup01 0:11cc2b7889af 460 to delete WebHook:
maygup01 0:11cc2b7889af 461 ```
maygup01 0:11cc2b7889af 462 curl -H "Authorization: Bearer ${API_KEY}" -X DELETE ${API_URL}/v2/notification/callback
maygup01 0:11cc2b7889af 463 ```
maygup01 0:11cc2b7889af 464 Please note that long-polling is now deprecated and will be likely be replace in the future. Use callback notification channel (WebHook) instead.
maygup01 0:11cc2b7889af 465
maygup01 0:11cc2b7889af 466 #### I had built the test suites but somehow I got my Python environment messed up; can I run the test binaries without GreenTea?
maygup01 0:11cc2b7889af 467 The test binaries are built under:
maygup01 0:11cc2b7889af 468 ```
maygup01 0:11cc2b7889af 469 <repo folder>/BUILD/tests/<Target Name>/<Toolchain>/simple-mbed-cloud-client/TESTS/<Test Suites>/<test case>/<test case name>.hex.
maygup01 0:11cc2b7889af 470 ```
maygup01 0:11cc2b7889af 471 You may copy the binary you want to manually test and flash it to the device, then when the device boots, paste the following line to the serial console to trigger the test:
maygup01 0:11cc2b7889af 472 ```
maygup01 0:11cc2b7889af 473 {{__sync;1}}
maygup01 0:11cc2b7889af 474 ```
maygup01 0:11cc2b7889af 475
maygup01 0:11cc2b7889af 476 ### Known issues
maygup01 0:11cc2b7889af 477
maygup01 0:11cc2b7889af 478 Check open issues on [GitHub](https://github.com/ARMmbed/simple-mbed-cloud-client/issues).