Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
simple-mbed-cloud-client/TESTS/dev_mgmt/update/main.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /* |
leothedragon | 0:8f0bb79ddd48 | 2 | * mbed Microcontroller Library |
leothedragon | 0:8f0bb79ddd48 | 3 | * Copyright (c) 2006-2018 ARM Limited |
leothedragon | 0:8f0bb79ddd48 | 4 | * |
leothedragon | 0:8f0bb79ddd48 | 5 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 6 | * |
leothedragon | 0:8f0bb79ddd48 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
leothedragon | 0:8f0bb79ddd48 | 8 | * you may not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 9 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 10 | * |
leothedragon | 0:8f0bb79ddd48 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 12 | * |
leothedragon | 0:8f0bb79ddd48 | 13 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
leothedragon | 0:8f0bb79ddd48 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 16 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 17 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 18 | */ |
leothedragon | 0:8f0bb79ddd48 | 19 | |
leothedragon | 0:8f0bb79ddd48 | 20 | #include "mbed.h" |
leothedragon | 0:8f0bb79ddd48 | 21 | #include "FATFileSystem.h" |
leothedragon | 0:8f0bb79ddd48 | 22 | #include "LittleFileSystem.h" |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "simple-mbed-cloud-client.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | #include "greentea-client/test_env.h" |
leothedragon | 0:8f0bb79ddd48 | 25 | #include "common_defines_test.h" |
leothedragon | 0:8f0bb79ddd48 | 26 | |
leothedragon | 0:8f0bb79ddd48 | 27 | #ifndef MBED_CONF_APP_TESTS_FS_SIZE |
leothedragon | 0:8f0bb79ddd48 | 28 | #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024) |
leothedragon | 0:8f0bb79ddd48 | 29 | #endif |
leothedragon | 0:8f0bb79ddd48 | 30 | |
leothedragon | 0:8f0bb79ddd48 | 31 | uint32_t test_timeout = 30*60; |
leothedragon | 0:8f0bb79ddd48 | 32 | |
leothedragon | 0:8f0bb79ddd48 | 33 | #if !defined(MBED_CONF_APP_NO_LED) |
leothedragon | 0:8f0bb79ddd48 | 34 | DigitalOut led1(LED1); |
leothedragon | 0:8f0bb79ddd48 | 35 | DigitalOut led2(LED2); |
leothedragon | 0:8f0bb79ddd48 | 36 | void led_thread() { |
leothedragon | 0:8f0bb79ddd48 | 37 | led1 = !led1; |
leothedragon | 0:8f0bb79ddd48 | 38 | led2 = !led1; |
leothedragon | 0:8f0bb79ddd48 | 39 | } |
leothedragon | 0:8f0bb79ddd48 | 40 | #endif |
leothedragon | 0:8f0bb79ddd48 | 41 | RawSerial pc(USBTX, USBRX); |
leothedragon | 0:8f0bb79ddd48 | 42 | |
leothedragon | 0:8f0bb79ddd48 | 43 | void wait_nb(uint16_t ms) { |
leothedragon | 0:8f0bb79ddd48 | 44 | wait_ms(ms); |
leothedragon | 0:8f0bb79ddd48 | 45 | } |
leothedragon | 0:8f0bb79ddd48 | 46 | |
leothedragon | 0:8f0bb79ddd48 | 47 | void logger(const char* message, const char* decor) { |
leothedragon | 0:8f0bb79ddd48 | 48 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 49 | pc.printf(message, decor); |
leothedragon | 0:8f0bb79ddd48 | 50 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 51 | } |
leothedragon | 0:8f0bb79ddd48 | 52 | void logger(const char* message) { |
leothedragon | 0:8f0bb79ddd48 | 53 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 54 | pc.printf(message); |
leothedragon | 0:8f0bb79ddd48 | 55 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 56 | } |
leothedragon | 0:8f0bb79ddd48 | 57 | void test_failed() { |
leothedragon | 0:8f0bb79ddd48 | 58 | greentea_send_kv("test_failed", 1); |
leothedragon | 0:8f0bb79ddd48 | 59 | } |
leothedragon | 0:8f0bb79ddd48 | 60 | void test_case_start(const char *name, size_t index) { |
leothedragon | 0:8f0bb79ddd48 | 61 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 62 | pc.printf("\r\n>>> Running case #%u: '%s'...\n", index, name); |
leothedragon | 0:8f0bb79ddd48 | 63 | GREENTEA_TESTCASE_START(name); |
leothedragon | 0:8f0bb79ddd48 | 64 | } |
leothedragon | 0:8f0bb79ddd48 | 65 | void test_case_finish(const char *name, size_t passed, size_t failed) { |
leothedragon | 0:8f0bb79ddd48 | 66 | GREENTEA_TESTCASE_FINISH(name, passed, failed); |
leothedragon | 0:8f0bb79ddd48 | 67 | wait_nb(10); |
leothedragon | 0:8f0bb79ddd48 | 68 | pc.printf(">>> '%s': %u passed, %u failed\r\n", name, passed, failed); |
leothedragon | 0:8f0bb79ddd48 | 69 | } |
leothedragon | 0:8f0bb79ddd48 | 70 | |
leothedragon | 0:8f0bb79ddd48 | 71 | uint32_t dl_last_rpercent = 0; |
leothedragon | 0:8f0bb79ddd48 | 72 | bool dl_started = false; |
leothedragon | 0:8f0bb79ddd48 | 73 | Timer dl_timer; |
leothedragon | 0:8f0bb79ddd48 | 74 | void update_progress(uint32_t progress, uint32_t total) { |
leothedragon | 0:8f0bb79ddd48 | 75 | if (!dl_started) { |
leothedragon | 0:8f0bb79ddd48 | 76 | dl_started = true; |
leothedragon | 0:8f0bb79ddd48 | 77 | dl_timer.start(); |
leothedragon | 0:8f0bb79ddd48 | 78 | pc.printf("[INFO] Firmware download started. Size: %.2fKB\r\n", float(total) / 1024); |
leothedragon | 0:8f0bb79ddd48 | 79 | } else { |
leothedragon | 0:8f0bb79ddd48 | 80 | float speed = float(progress) / dl_timer.read(); |
leothedragon | 0:8f0bb79ddd48 | 81 | float percent = float(progress) * 100 / float(total); |
leothedragon | 0:8f0bb79ddd48 | 82 | uint32_t time_left = (total - progress) / speed; |
leothedragon | 0:8f0bb79ddd48 | 83 | pc.printf("[INFO] Downloading: %.2f%% (%.2fKB/s, ETA: %02d:%02d:%02d)\r\n", percent, speed / 1024, |
leothedragon | 0:8f0bb79ddd48 | 84 | time_left / 3600, (time_left / 60) % 60, time_left % 60); |
leothedragon | 0:8f0bb79ddd48 | 85 | |
leothedragon | 0:8f0bb79ddd48 | 86 | // // this is disabled until htrun is extended to support dynamic change of the duration of tests |
leothedragon | 0:8f0bb79ddd48 | 87 | // // see https://github.com/ARMmbed/htrun/pull/228 |
leothedragon | 0:8f0bb79ddd48 | 88 | // // extend the timeout of the test based on current progress |
leothedragon | 0:8f0bb79ddd48 | 89 | // uint32_t round_percent = progress * 100 / total; |
leothedragon | 0:8f0bb79ddd48 | 90 | // if (round_percent != dl_last_rpercent) { |
leothedragon | 0:8f0bb79ddd48 | 91 | // dl_last_rpercent = round_percent; |
leothedragon | 0:8f0bb79ddd48 | 92 | // uint32_t new_timeout = test_timeout + int(dl_timer.read() * (round_percent + 1) / round_percent); |
leothedragon | 0:8f0bb79ddd48 | 93 | // greentea_send_kv("__timeout_set", new_timeout); |
leothedragon | 0:8f0bb79ddd48 | 94 | // } |
leothedragon | 0:8f0bb79ddd48 | 95 | } |
leothedragon | 0:8f0bb79ddd48 | 96 | |
leothedragon | 0:8f0bb79ddd48 | 97 | if (progress == total) { |
leothedragon | 0:8f0bb79ddd48 | 98 | dl_timer.stop(); |
leothedragon | 0:8f0bb79ddd48 | 99 | dl_started = false; |
leothedragon | 0:8f0bb79ddd48 | 100 | pc.printf("[INFO] Firmware download completed. %.2fKB in %.2f seconds (%.2fKB/s)\r\n", |
leothedragon | 0:8f0bb79ddd48 | 101 | float(total) / 1024, dl_timer.read(), float(total) / dl_timer.read() / 1024); |
leothedragon | 0:8f0bb79ddd48 | 102 | test_case_finish("Pelion Firmware Download", 1, 0); |
leothedragon | 0:8f0bb79ddd48 | 103 | test_case_start("Pelion Firmware Update", 9); |
leothedragon | 0:8f0bb79ddd48 | 104 | } |
leothedragon | 0:8f0bb79ddd48 | 105 | } |
leothedragon | 0:8f0bb79ddd48 | 106 | |
leothedragon | 0:8f0bb79ddd48 | 107 | static const ConnectorClientEndpointInfo* endpointInfo; |
leothedragon | 0:8f0bb79ddd48 | 108 | void registered(const ConnectorClientEndpointInfo *endpoint) { |
leothedragon | 0:8f0bb79ddd48 | 109 | logger("[INFO] Connected to Pelion Device Management. Device ID: %s\n", |
leothedragon | 0:8f0bb79ddd48 | 110 | endpoint->internal_endpoint_name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 111 | endpointInfo = endpoint; |
leothedragon | 0:8f0bb79ddd48 | 112 | } |
leothedragon | 0:8f0bb79ddd48 | 113 | |
leothedragon | 0:8f0bb79ddd48 | 114 | void spdmc_testsuite_update(void) { |
leothedragon | 0:8f0bb79ddd48 | 115 | int i = 0; |
leothedragon | 0:8f0bb79ddd48 | 116 | int iteration = 0; |
leothedragon | 0:8f0bb79ddd48 | 117 | char _key[20] = { }; |
leothedragon | 0:8f0bb79ddd48 | 118 | char _value[128] = { }; |
leothedragon | 0:8f0bb79ddd48 | 119 | |
leothedragon | 0:8f0bb79ddd48 | 120 | greentea_send_kv("spdmc_ready_chk", true); |
leothedragon | 0:8f0bb79ddd48 | 121 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 122 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
leothedragon | 0:8f0bb79ddd48 | 123 | |
leothedragon | 0:8f0bb79ddd48 | 124 | if (strcmp(_key, "iteration") == 0) { |
leothedragon | 0:8f0bb79ddd48 | 125 | iteration = atoi(_value); |
leothedragon | 0:8f0bb79ddd48 | 126 | break; |
leothedragon | 0:8f0bb79ddd48 | 127 | } |
leothedragon | 0:8f0bb79ddd48 | 128 | } |
leothedragon | 0:8f0bb79ddd48 | 129 | |
leothedragon | 0:8f0bb79ddd48 | 130 | // provide manifest to greentea so it can correct show skipped and failed tests |
leothedragon | 0:8f0bb79ddd48 | 131 | if (iteration == 0) { |
leothedragon | 0:8f0bb79ddd48 | 132 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_COUNT, 10); |
leothedragon | 0:8f0bb79ddd48 | 133 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE); |
leothedragon | 0:8f0bb79ddd48 | 134 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Connect to " TEST_NETWORK_TYPE); |
leothedragon | 0:8f0bb79ddd48 | 135 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Format " TEST_FILESYSTEM_TYPE); |
leothedragon | 0:8f0bb79ddd48 | 136 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Initialize Simple PDMC"); |
leothedragon | 0:8f0bb79ddd48 | 137 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Bootstrap & Reg."); |
leothedragon | 0:8f0bb79ddd48 | 138 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Directory"); |
leothedragon | 0:8f0bb79ddd48 | 139 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Firmware Prepare"); |
leothedragon | 0:8f0bb79ddd48 | 140 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Firmware Download"); |
leothedragon | 0:8f0bb79ddd48 | 141 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Firmware Update"); |
leothedragon | 0:8f0bb79ddd48 | 142 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Re-register"); |
leothedragon | 0:8f0bb79ddd48 | 143 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Post-update Erase"); |
leothedragon | 0:8f0bb79ddd48 | 144 | greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Post-update Identity"); |
leothedragon | 0:8f0bb79ddd48 | 145 | } else { |
leothedragon | 0:8f0bb79ddd48 | 146 | test_case_finish("Pelion Firmware Update", true, false); |
leothedragon | 0:8f0bb79ddd48 | 147 | } |
leothedragon | 0:8f0bb79ddd48 | 148 | |
leothedragon | 0:8f0bb79ddd48 | 149 | test_case_start("Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE, 1); |
leothedragon | 0:8f0bb79ddd48 | 150 | logger("[INFO] Attempting to initialize storage.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 151 | |
leothedragon | 0:8f0bb79ddd48 | 152 | // Default storage definition. |
leothedragon | 0:8f0bb79ddd48 | 153 | BlockDevice* bd = BlockDevice::get_default_instance(); |
leothedragon | 0:8f0bb79ddd48 | 154 | SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE); |
leothedragon | 0:8f0bb79ddd48 | 155 | #if TEST_USE_FILESYSTEM == FS_FAT |
leothedragon | 0:8f0bb79ddd48 | 156 | FATFileSystem fs("fs", &sd); |
leothedragon | 0:8f0bb79ddd48 | 157 | #else |
leothedragon | 0:8f0bb79ddd48 | 158 | LittleFileSystem fs("fs", &sd); |
leothedragon | 0:8f0bb79ddd48 | 159 | #endif |
leothedragon | 0:8f0bb79ddd48 | 160 | |
leothedragon | 0:8f0bb79ddd48 | 161 | test_case_finish("Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE, iteration + 1, 0); |
leothedragon | 0:8f0bb79ddd48 | 162 | |
leothedragon | 0:8f0bb79ddd48 | 163 | // Corrupt the image after successful firmware update to ensure that the bootloader won't try to apply it for other test runs |
leothedragon | 0:8f0bb79ddd48 | 164 | if (iteration) { |
leothedragon | 0:8f0bb79ddd48 | 165 | #if defined(MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS) && defined(MBED_CONF_UPDATE_CLIENT_STORAGE_SIZE) |
leothedragon | 0:8f0bb79ddd48 | 166 | test_case_start("Post-update Erase", 11); |
leothedragon | 0:8f0bb79ddd48 | 167 | |
leothedragon | 0:8f0bb79ddd48 | 168 | int erase_status; |
leothedragon | 0:8f0bb79ddd48 | 169 | if (bd->get_erase_value() >= 0) { |
leothedragon | 0:8f0bb79ddd48 | 170 | // Blockdevice supports a straight erase |
leothedragon | 0:8f0bb79ddd48 | 171 | erase_status = bd->erase(MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS, bd->get_erase_size(MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS)); |
leothedragon | 0:8f0bb79ddd48 | 172 | } else { |
leothedragon | 0:8f0bb79ddd48 | 173 | // Blockdevice supports an overwrite |
leothedragon | 0:8f0bb79ddd48 | 174 | uint32_t garbage[8]; |
leothedragon | 0:8f0bb79ddd48 | 175 | erase_status = bd->program(garbage, MBED_CONF_UPDATE_CLIENT_STORAGE_ADDRESS, bd->get_program_size()); |
leothedragon | 0:8f0bb79ddd48 | 176 | } |
leothedragon | 0:8f0bb79ddd48 | 177 | if (erase_status != 0) { |
leothedragon | 0:8f0bb79ddd48 | 178 | logger("[ERROR] Post-update image invalidation failed.\n"); |
leothedragon | 0:8f0bb79ddd48 | 179 | } |
leothedragon | 0:8f0bb79ddd48 | 180 | test_case_finish("Post-update Erase", (erase_status == 0), (erase_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 181 | #endif |
leothedragon | 0:8f0bb79ddd48 | 182 | } |
leothedragon | 0:8f0bb79ddd48 | 183 | |
leothedragon | 0:8f0bb79ddd48 | 184 | // Start network connection test. |
leothedragon | 0:8f0bb79ddd48 | 185 | test_case_start("Connect to " TEST_NETWORK_TYPE, 2); |
leothedragon | 0:8f0bb79ddd48 | 186 | logger("[INFO] Attempting to connect to network.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 187 | |
leothedragon | 0:8f0bb79ddd48 | 188 | // Connection definition. |
leothedragon | 0:8f0bb79ddd48 | 189 | NetworkInterface *net = NetworkInterface::get_default_instance(); |
leothedragon | 0:8f0bb79ddd48 | 190 | nsapi_error_t net_status = -1; |
leothedragon | 0:8f0bb79ddd48 | 191 | for (int tries = 0; tries < 3; tries++) { |
leothedragon | 0:8f0bb79ddd48 | 192 | net_status = net->connect(); |
leothedragon | 0:8f0bb79ddd48 | 193 | if (net_status == NSAPI_ERROR_OK) { |
leothedragon | 0:8f0bb79ddd48 | 194 | break; |
leothedragon | 0:8f0bb79ddd48 | 195 | } else { |
leothedragon | 0:8f0bb79ddd48 | 196 | logger("[WARN] Unable to connect to network. Retrying..."); |
leothedragon | 0:8f0bb79ddd48 | 197 | } |
leothedragon | 0:8f0bb79ddd48 | 198 | } |
leothedragon | 0:8f0bb79ddd48 | 199 | |
leothedragon | 0:8f0bb79ddd48 | 200 | // Report status to console. |
leothedragon | 0:8f0bb79ddd48 | 201 | if (net_status != 0) { |
leothedragon | 0:8f0bb79ddd48 | 202 | logger("[ERROR] Device failed to connect to Network.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 203 | test_failed(); |
leothedragon | 0:8f0bb79ddd48 | 204 | } else { |
leothedragon | 0:8f0bb79ddd48 | 205 | logger("[INFO] Connected to network successfully. IP address: %s\n", net->get_ip_address()); |
leothedragon | 0:8f0bb79ddd48 | 206 | } |
leothedragon | 0:8f0bb79ddd48 | 207 | |
leothedragon | 0:8f0bb79ddd48 | 208 | test_case_finish("Connect to " TEST_NETWORK_TYPE, iteration + (net_status == 0), (net_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 209 | |
leothedragon | 0:8f0bb79ddd48 | 210 | if (iteration == 0) { |
leothedragon | 0:8f0bb79ddd48 | 211 | test_case_start("Format " TEST_FILESYSTEM_TYPE, 3); |
leothedragon | 0:8f0bb79ddd48 | 212 | logger("[INFO] Resetting storage to a clean state for test.\n"); |
leothedragon | 0:8f0bb79ddd48 | 213 | |
leothedragon | 0:8f0bb79ddd48 | 214 | int storage_status = fs.reformat(&sd); |
leothedragon | 0:8f0bb79ddd48 | 215 | if (storage_status != 0) { |
leothedragon | 0:8f0bb79ddd48 | 216 | storage_status = sd.erase(0, sd.size()); |
leothedragon | 0:8f0bb79ddd48 | 217 | if (storage_status == 0) { |
leothedragon | 0:8f0bb79ddd48 | 218 | storage_status = fs.format(&sd); |
leothedragon | 0:8f0bb79ddd48 | 219 | if (storage_status != 0) { |
leothedragon | 0:8f0bb79ddd48 | 220 | logger("[ERROR] Filesystem init failed\n"); |
leothedragon | 0:8f0bb79ddd48 | 221 | } |
leothedragon | 0:8f0bb79ddd48 | 222 | } |
leothedragon | 0:8f0bb79ddd48 | 223 | } |
leothedragon | 0:8f0bb79ddd48 | 224 | |
leothedragon | 0:8f0bb79ddd48 | 225 | // Report status to console. |
leothedragon | 0:8f0bb79ddd48 | 226 | if (storage_status == 0) { |
leothedragon | 0:8f0bb79ddd48 | 227 | logger("[INFO] Storage format successful.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 228 | } else { |
leothedragon | 0:8f0bb79ddd48 | 229 | logger("[ERROR] Storage format failed.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 230 | test_failed(); |
leothedragon | 0:8f0bb79ddd48 | 231 | } |
leothedragon | 0:8f0bb79ddd48 | 232 | |
leothedragon | 0:8f0bb79ddd48 | 233 | test_case_finish("Format " TEST_FILESYSTEM_TYPE, (storage_status == 0), (storage_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 234 | } |
leothedragon | 0:8f0bb79ddd48 | 235 | |
leothedragon | 0:8f0bb79ddd48 | 236 | // SimpleMbedCloudClient initialization must be successful. |
leothedragon | 0:8f0bb79ddd48 | 237 | test_case_start("Initialize Simple PDMC", 4); |
leothedragon | 0:8f0bb79ddd48 | 238 | |
leothedragon | 0:8f0bb79ddd48 | 239 | SimpleMbedCloudClient client(net, &sd, &fs); |
leothedragon | 0:8f0bb79ddd48 | 240 | int client_status = client.init(); |
leothedragon | 0:8f0bb79ddd48 | 241 | |
leothedragon | 0:8f0bb79ddd48 | 242 | // Report status to console. |
leothedragon | 0:8f0bb79ddd48 | 243 | if (client_status == 0) { |
leothedragon | 0:8f0bb79ddd48 | 244 | logger("[INFO] Simple PDMC initialization successful.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 245 | } else { |
leothedragon | 0:8f0bb79ddd48 | 246 | logger("[ERROR] Simple PDMC failed to initialize.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 247 | // End the test early, cannot continue without successful cloud client initialization. |
leothedragon | 0:8f0bb79ddd48 | 248 | test_failed(); |
leothedragon | 0:8f0bb79ddd48 | 249 | } |
leothedragon | 0:8f0bb79ddd48 | 250 | |
leothedragon | 0:8f0bb79ddd48 | 251 | test_case_finish("Initialize Simple PDMC", iteration + (client_status == 0), (client_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 252 | |
leothedragon | 0:8f0bb79ddd48 | 253 | //Create LwM2M resources |
leothedragon | 0:8f0bb79ddd48 | 254 | MbedCloudClientResource *res_get_test; |
leothedragon | 0:8f0bb79ddd48 | 255 | res_get_test = client.create_resource("5000/0/1", "get_resource"); |
leothedragon | 0:8f0bb79ddd48 | 256 | res_get_test->observable(true); |
leothedragon | 0:8f0bb79ddd48 | 257 | res_get_test->methods(M2MMethod::GET); |
leothedragon | 0:8f0bb79ddd48 | 258 | res_get_test->set_value("test0"); |
leothedragon | 0:8f0bb79ddd48 | 259 | |
leothedragon | 0:8f0bb79ddd48 | 260 | // Register to Pelion Device Management. |
leothedragon | 0:8f0bb79ddd48 | 261 | if (iteration == 0) { |
leothedragon | 0:8f0bb79ddd48 | 262 | test_case_start("Pelion Bootstrap & Reg.", 5); |
leothedragon | 0:8f0bb79ddd48 | 263 | } else { |
leothedragon | 0:8f0bb79ddd48 | 264 | test_case_start("Pelion Re-register", 10); |
leothedragon | 0:8f0bb79ddd48 | 265 | } |
leothedragon | 0:8f0bb79ddd48 | 266 | // Set client callback to report endpoint name. |
leothedragon | 0:8f0bb79ddd48 | 267 | client.on_registered(®istered); |
leothedragon | 0:8f0bb79ddd48 | 268 | client.register_and_connect(); |
leothedragon | 0:8f0bb79ddd48 | 269 | |
leothedragon | 0:8f0bb79ddd48 | 270 | i = 600; // wait 60 seconds |
leothedragon | 0:8f0bb79ddd48 | 271 | while (i-- > 0 && !client.is_client_registered()) { |
leothedragon | 0:8f0bb79ddd48 | 272 | wait_ms(100); |
leothedragon | 0:8f0bb79ddd48 | 273 | } |
leothedragon | 0:8f0bb79ddd48 | 274 | |
leothedragon | 0:8f0bb79ddd48 | 275 | // Get registration status. |
leothedragon | 0:8f0bb79ddd48 | 276 | bool client_registered = client.is_client_registered(); |
leothedragon | 0:8f0bb79ddd48 | 277 | if (client_registered) { |
leothedragon | 0:8f0bb79ddd48 | 278 | client_status = 0; |
leothedragon | 0:8f0bb79ddd48 | 279 | wait_nb(100); |
leothedragon | 0:8f0bb79ddd48 | 280 | logger("[INFO] Device successfully registered to Pelion DM.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 281 | } else { |
leothedragon | 0:8f0bb79ddd48 | 282 | client_status = -1; |
leothedragon | 0:8f0bb79ddd48 | 283 | logger("[ERROR] Device failed to register.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 284 | test_failed(); |
leothedragon | 0:8f0bb79ddd48 | 285 | } |
leothedragon | 0:8f0bb79ddd48 | 286 | if (iteration == 0) { |
leothedragon | 0:8f0bb79ddd48 | 287 | test_case_finish("Pelion Bootstrap & Reg.", (client_status == 0), (client_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 288 | } else { |
leothedragon | 0:8f0bb79ddd48 | 289 | test_case_finish("Pelion Re-register", (client_status == 0), (client_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 290 | } |
leothedragon | 0:8f0bb79ddd48 | 291 | |
leothedragon | 0:8f0bb79ddd48 | 292 | if (iteration == 0) { |
leothedragon | 0:8f0bb79ddd48 | 293 | //Start registration status test |
leothedragon | 0:8f0bb79ddd48 | 294 | test_case_start("Pelion Directory", 6); |
leothedragon | 0:8f0bb79ddd48 | 295 | int reg_status; |
leothedragon | 0:8f0bb79ddd48 | 296 | |
leothedragon | 0:8f0bb79ddd48 | 297 | logger("[INFO] Wait up to 10 seconds for Device Directory to update after initial registration.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 298 | i = 100; |
leothedragon | 0:8f0bb79ddd48 | 299 | while (i-- > 0 and !endpointInfo) { |
leothedragon | 0:8f0bb79ddd48 | 300 | wait(100); |
leothedragon | 0:8f0bb79ddd48 | 301 | } |
leothedragon | 0:8f0bb79ddd48 | 302 | |
leothedragon | 0:8f0bb79ddd48 | 303 | // Start host tests with device id |
leothedragon | 0:8f0bb79ddd48 | 304 | logger("[INFO] Starting Pelion verification using Python SDK...\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 305 | greentea_send_kv("verify_registration", endpointInfo->internal_endpoint_name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 306 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 307 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
leothedragon | 0:8f0bb79ddd48 | 308 | |
leothedragon | 0:8f0bb79ddd48 | 309 | if (strcmp(_key, "registered") == 0) { |
leothedragon | 0:8f0bb79ddd48 | 310 | if (atoi(_value)) { |
leothedragon | 0:8f0bb79ddd48 | 311 | reg_status = 0; |
leothedragon | 0:8f0bb79ddd48 | 312 | logger("[INFO] Device is registered in the Device Directory.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 313 | } else { |
leothedragon | 0:8f0bb79ddd48 | 314 | reg_status = -1; |
leothedragon | 0:8f0bb79ddd48 | 315 | logger("[ERROR] Device could not be verified as registered in Device Directory.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 316 | test_failed(); |
leothedragon | 0:8f0bb79ddd48 | 317 | } |
leothedragon | 0:8f0bb79ddd48 | 318 | break; |
leothedragon | 0:8f0bb79ddd48 | 319 | } |
leothedragon | 0:8f0bb79ddd48 | 320 | } |
leothedragon | 0:8f0bb79ddd48 | 321 | |
leothedragon | 0:8f0bb79ddd48 | 322 | test_case_finish("Pelion Directory", (reg_status == 0), (reg_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 323 | |
leothedragon | 0:8f0bb79ddd48 | 324 | if (reg_status == 0) { |
leothedragon | 0:8f0bb79ddd48 | 325 | test_case_start("Pelion Firmware Prepare", 7); |
leothedragon | 0:8f0bb79ddd48 | 326 | wait_nb(500); |
leothedragon | 0:8f0bb79ddd48 | 327 | int fw_status; |
leothedragon | 0:8f0bb79ddd48 | 328 | greentea_send_kv("firmware_prepare", 1); |
leothedragon | 0:8f0bb79ddd48 | 329 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 330 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
leothedragon | 0:8f0bb79ddd48 | 331 | if (strcmp(_key, "firmware_sent") == 0) { |
leothedragon | 0:8f0bb79ddd48 | 332 | if (atoi(_value)) { |
leothedragon | 0:8f0bb79ddd48 | 333 | fw_status = 0; |
leothedragon | 0:8f0bb79ddd48 | 334 | } else { |
leothedragon | 0:8f0bb79ddd48 | 335 | fw_status = -1; |
leothedragon | 0:8f0bb79ddd48 | 336 | logger("[ERROR] While preparing firmware.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 337 | } |
leothedragon | 0:8f0bb79ddd48 | 338 | break; |
leothedragon | 0:8f0bb79ddd48 | 339 | } |
leothedragon | 0:8f0bb79ddd48 | 340 | } |
leothedragon | 0:8f0bb79ddd48 | 341 | test_case_finish("Pelion Firmware Prepare", (fw_status == 0), (fw_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 342 | |
leothedragon | 0:8f0bb79ddd48 | 343 | test_case_start("Pelion Firmware Download", 8); |
leothedragon | 0:8f0bb79ddd48 | 344 | logger("[INFO] Update campaign has started.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 345 | // The device should download firmware and reset at this stage |
leothedragon | 0:8f0bb79ddd48 | 346 | } |
leothedragon | 0:8f0bb79ddd48 | 347 | |
leothedragon | 0:8f0bb79ddd48 | 348 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 349 | wait_nb(1000); |
leothedragon | 0:8f0bb79ddd48 | 350 | } |
leothedragon | 0:8f0bb79ddd48 | 351 | } else { |
leothedragon | 0:8f0bb79ddd48 | 352 | //Start consistent identity test. |
leothedragon | 0:8f0bb79ddd48 | 353 | test_case_start("Post-update Identity", 12); |
leothedragon | 0:8f0bb79ddd48 | 354 | int identity_status; |
leothedragon | 0:8f0bb79ddd48 | 355 | |
leothedragon | 0:8f0bb79ddd48 | 356 | logger("[INFO] Wait up to 5 seconds for Device Directory to update after reboot.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 357 | i = 50; |
leothedragon | 0:8f0bb79ddd48 | 358 | while (i-- > 0 and !endpointInfo) { |
leothedragon | 0:8f0bb79ddd48 | 359 | wait(100); |
leothedragon | 0:8f0bb79ddd48 | 360 | } |
leothedragon | 0:8f0bb79ddd48 | 361 | |
leothedragon | 0:8f0bb79ddd48 | 362 | // Wait for Host Test to verify consistent device ID (blocking here) |
leothedragon | 0:8f0bb79ddd48 | 363 | logger("[INFO] Verifying consistent Device ID...\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 364 | greentea_send_kv("verify_identity", endpointInfo->internal_endpoint_name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 365 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 366 | greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); |
leothedragon | 0:8f0bb79ddd48 | 367 | |
leothedragon | 0:8f0bb79ddd48 | 368 | if (strcmp(_key, "verified") == 0) { |
leothedragon | 0:8f0bb79ddd48 | 369 | if (atoi(_value)) { |
leothedragon | 0:8f0bb79ddd48 | 370 | identity_status = 0; |
leothedragon | 0:8f0bb79ddd48 | 371 | logger("[INFO] Device ID consistent, SOTP and Secure Storage is preserved correctly.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 372 | } else { |
leothedragon | 0:8f0bb79ddd48 | 373 | identity_status = -1; |
leothedragon | 0:8f0bb79ddd48 | 374 | logger("[ERROR] Device ID is inconsistent. SOTP and Secure Storage was not preserved.\r\n"); |
leothedragon | 0:8f0bb79ddd48 | 375 | } |
leothedragon | 0:8f0bb79ddd48 | 376 | break; |
leothedragon | 0:8f0bb79ddd48 | 377 | } |
leothedragon | 0:8f0bb79ddd48 | 378 | } |
leothedragon | 0:8f0bb79ddd48 | 379 | |
leothedragon | 0:8f0bb79ddd48 | 380 | test_case_finish("Post-update Identity", (identity_status == 0), (identity_status != 0)); |
leothedragon | 0:8f0bb79ddd48 | 381 | |
leothedragon | 0:8f0bb79ddd48 | 382 | GREENTEA_TESTSUITE_RESULT(identity_status == 0); |
leothedragon | 0:8f0bb79ddd48 | 383 | |
leothedragon | 0:8f0bb79ddd48 | 384 | while (1) { |
leothedragon | 0:8f0bb79ddd48 | 385 | wait(100); |
leothedragon | 0:8f0bb79ddd48 | 386 | } |
leothedragon | 0:8f0bb79ddd48 | 387 | } |
leothedragon | 0:8f0bb79ddd48 | 388 | } |
leothedragon | 0:8f0bb79ddd48 | 389 | |
leothedragon | 0:8f0bb79ddd48 | 390 | int main(void) { |
leothedragon | 0:8f0bb79ddd48 | 391 | //Create a thread to blink an LED and signal that the device is alive |
leothedragon | 0:8f0bb79ddd48 | 392 | #if !defined(MBED_CONF_APP_NO_LED) |
leothedragon | 0:8f0bb79ddd48 | 393 | Ticker t; |
leothedragon | 0:8f0bb79ddd48 | 394 | t.attach(led_thread, 0.5); |
leothedragon | 0:8f0bb79ddd48 | 395 | #endif |
leothedragon | 0:8f0bb79ddd48 | 396 | |
leothedragon | 0:8f0bb79ddd48 | 397 | greentea_send_kv("device_booted", 1); |
leothedragon | 0:8f0bb79ddd48 | 398 | |
leothedragon | 0:8f0bb79ddd48 | 399 | GREENTEA_SETUP(test_timeout, "sdk_host_tests"); |
leothedragon | 0:8f0bb79ddd48 | 400 | spdmc_testsuite_update(); |
leothedragon | 0:8f0bb79ddd48 | 401 | |
leothedragon | 0:8f0bb79ddd48 | 402 | return 0; |
leothedragon | 0:8f0bb79ddd48 | 403 | } |