leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew 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 #if !defined(MBED_CONF_APP_NO_LED)
leothedragon 0:8f0bb79ddd48 32 DigitalOut led1(LED1);
leothedragon 0:8f0bb79ddd48 33 DigitalOut led2(LED2);
leothedragon 0:8f0bb79ddd48 34 void led_thread() {
leothedragon 0:8f0bb79ddd48 35 led1 = !led1;
leothedragon 0:8f0bb79ddd48 36 led2 = !led1;
leothedragon 0:8f0bb79ddd48 37 }
leothedragon 0:8f0bb79ddd48 38 #endif
leothedragon 0:8f0bb79ddd48 39
leothedragon 0:8f0bb79ddd48 40 RawSerial pc(USBTX, USBRX);
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 void wait_nb(uint16_t ms) {
leothedragon 0:8f0bb79ddd48 43 wait_ms(ms);
leothedragon 0:8f0bb79ddd48 44 }
leothedragon 0:8f0bb79ddd48 45
leothedragon 0:8f0bb79ddd48 46 void logger(const char* message, const char* decor) {
leothedragon 0:8f0bb79ddd48 47 wait_nb(10);
leothedragon 0:8f0bb79ddd48 48 pc.printf(message, decor);
leothedragon 0:8f0bb79ddd48 49 wait_nb(10);
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51 void logger(const char* message) {
leothedragon 0:8f0bb79ddd48 52 wait_nb(10);
leothedragon 0:8f0bb79ddd48 53 pc.printf(message);
leothedragon 0:8f0bb79ddd48 54 wait_nb(10);
leothedragon 0:8f0bb79ddd48 55 }
leothedragon 0:8f0bb79ddd48 56 void test_failed() {
leothedragon 0:8f0bb79ddd48 57 greentea_send_kv("test_failed", 1);
leothedragon 0:8f0bb79ddd48 58 }
leothedragon 0:8f0bb79ddd48 59 void test_case_start(const char *name, size_t index) {
leothedragon 0:8f0bb79ddd48 60 wait_nb(10);
leothedragon 0:8f0bb79ddd48 61 pc.printf("\r\n>>> Running case #%u: '%s'...\n", index, name);
leothedragon 0:8f0bb79ddd48 62 GREENTEA_TESTCASE_START(name);
leothedragon 0:8f0bb79ddd48 63 }
leothedragon 0:8f0bb79ddd48 64 void test_case_finish(const char *name, size_t passed, size_t failed) {
leothedragon 0:8f0bb79ddd48 65 GREENTEA_TESTCASE_FINISH(name, passed, failed);
leothedragon 0:8f0bb79ddd48 66 wait_nb(10);
leothedragon 0:8f0bb79ddd48 67 pc.printf(">>> '%s': %u passed, %u failed\r\n", name, passed, failed);
leothedragon 0:8f0bb79ddd48 68 }
leothedragon 0:8f0bb79ddd48 69
leothedragon 0:8f0bb79ddd48 70 static const ConnectorClientEndpointInfo* endpointInfo;
leothedragon 0:8f0bb79ddd48 71 void registered(const ConnectorClientEndpointInfo *endpoint) {
leothedragon 0:8f0bb79ddd48 72 logger("[INFO] Connected to Pelion Device Management. Device ID: %s\n",
leothedragon 0:8f0bb79ddd48 73 endpoint->internal_endpoint_name.c_str());
leothedragon 0:8f0bb79ddd48 74 endpointInfo = endpoint;
leothedragon 0:8f0bb79ddd48 75 }
leothedragon 0:8f0bb79ddd48 76
leothedragon 0:8f0bb79ddd48 77 void post_test_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
leothedragon 0:8f0bb79ddd48 78 logger("[INFO] POST test callback executed.\r\n");
leothedragon 0:8f0bb79ddd48 79 greentea_send_kv("verify_lwm2m_post_test_result", 0);
leothedragon 0:8f0bb79ddd48 80 }
leothedragon 0:8f0bb79ddd48 81
leothedragon 0:8f0bb79ddd48 82 void spdmc_testsuite_connect(void) {
leothedragon 0:8f0bb79ddd48 83 int i = 0;
leothedragon 0:8f0bb79ddd48 84 int iteration = 0;
leothedragon 0:8f0bb79ddd48 85 char _key[20] = { };
leothedragon 0:8f0bb79ddd48 86 char _value[128] = { };
leothedragon 0:8f0bb79ddd48 87
leothedragon 0:8f0bb79ddd48 88 greentea_send_kv("device_ready", true);
leothedragon 0:8f0bb79ddd48 89 while (1) {
leothedragon 0:8f0bb79ddd48 90 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 91
leothedragon 0:8f0bb79ddd48 92 if (strcmp(_key, "iteration") == 0) {
leothedragon 0:8f0bb79ddd48 93 iteration = atoi(_value);
leothedragon 0:8f0bb79ddd48 94 break;
leothedragon 0:8f0bb79ddd48 95 }
leothedragon 0:8f0bb79ddd48 96 }
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98 // provide manifest to greentea so it can correct show skipped and failed tests
leothedragon 0:8f0bb79ddd48 99 if (iteration == 0) {
leothedragon 0:8f0bb79ddd48 100 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_COUNT, 10);
leothedragon 0:8f0bb79ddd48 101 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Connect to " TEST_NETWORK_TYPE);
leothedragon 0:8f0bb79ddd48 102 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE);
leothedragon 0:8f0bb79ddd48 103 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Format " TEST_FILESYSTEM_TYPE);
leothedragon 0:8f0bb79ddd48 104 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Initialize Simple PDMC");
leothedragon 0:8f0bb79ddd48 105 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Bootstrap & Reg.");
leothedragon 0:8f0bb79ddd48 106 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Directory");
leothedragon 0:8f0bb79ddd48 107 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Pelion Re-register");
leothedragon 0:8f0bb79ddd48 108 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Post-reset Identity");
leothedragon 0:8f0bb79ddd48 109 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Resource LwM2M GET");
leothedragon 0:8f0bb79ddd48 110 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Resource LwM2M SET");
leothedragon 0:8f0bb79ddd48 111 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Resource LwM2M PUT");
leothedragon 0:8f0bb79ddd48 112 greentea_send_kv(GREENTEA_TEST_ENV_TESTCASE_NAME, "Resource LwM2M POST");
leothedragon 0:8f0bb79ddd48 113 }
leothedragon 0:8f0bb79ddd48 114
leothedragon 0:8f0bb79ddd48 115 // Start network connection test.
leothedragon 0:8f0bb79ddd48 116 test_case_start("Connect to " TEST_NETWORK_TYPE, 1);
leothedragon 0:8f0bb79ddd48 117 logger("[INFO] Attempting to connect to network.\r\n");
leothedragon 0:8f0bb79ddd48 118
leothedragon 0:8f0bb79ddd48 119 // Connection definition.
leothedragon 0:8f0bb79ddd48 120 NetworkInterface *net = NetworkInterface::get_default_instance();
leothedragon 0:8f0bb79ddd48 121 nsapi_error_t net_status = -1;
leothedragon 0:8f0bb79ddd48 122 for (int tries = 0; tries < 3; tries++) {
leothedragon 0:8f0bb79ddd48 123 net_status = net->connect();
leothedragon 0:8f0bb79ddd48 124 if (net_status == NSAPI_ERROR_OK) {
leothedragon 0:8f0bb79ddd48 125 break;
leothedragon 0:8f0bb79ddd48 126 } else {
leothedragon 0:8f0bb79ddd48 127 logger("[WARN] Unable to connect to network. Retrying...");
leothedragon 0:8f0bb79ddd48 128 }
leothedragon 0:8f0bb79ddd48 129 }
leothedragon 0:8f0bb79ddd48 130
leothedragon 0:8f0bb79ddd48 131 // Report status to console.
leothedragon 0:8f0bb79ddd48 132 if (net_status != 0) {
leothedragon 0:8f0bb79ddd48 133 logger("[ERROR] Device failed to connect to Network.\r\n");
leothedragon 0:8f0bb79ddd48 134 test_failed();
leothedragon 0:8f0bb79ddd48 135 } else {
leothedragon 0:8f0bb79ddd48 136 logger("[INFO] Connected to network successfully. IP address: %s\n", net->get_ip_address());
leothedragon 0:8f0bb79ddd48 137 }
leothedragon 0:8f0bb79ddd48 138
leothedragon 0:8f0bb79ddd48 139 test_case_finish("Connect to " TEST_NETWORK_TYPE, iteration + (net_status == 0), (net_status != 0));
leothedragon 0:8f0bb79ddd48 140
leothedragon 0:8f0bb79ddd48 141 test_case_start("Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE, 2);
leothedragon 0:8f0bb79ddd48 142 logger("[INFO] Attempting to initialize storage.\r\n");
leothedragon 0:8f0bb79ddd48 143
leothedragon 0:8f0bb79ddd48 144 // Default storage definition.
leothedragon 0:8f0bb79ddd48 145 BlockDevice* bd = BlockDevice::get_default_instance();
leothedragon 0:8f0bb79ddd48 146 SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
leothedragon 0:8f0bb79ddd48 147 #if TEST_USE_FILESYSTEM == FS_FAT
leothedragon 0:8f0bb79ddd48 148 FATFileSystem fs("fs", &sd);
leothedragon 0:8f0bb79ddd48 149 #else
leothedragon 0:8f0bb79ddd48 150 LittleFileSystem fs("fs", &sd);
leothedragon 0:8f0bb79ddd48 151 #endif
leothedragon 0:8f0bb79ddd48 152
leothedragon 0:8f0bb79ddd48 153 test_case_finish("Initialize " TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE, iteration + 1, 0);
leothedragon 0:8f0bb79ddd48 154
leothedragon 0:8f0bb79ddd48 155 if (iteration == 0) {
leothedragon 0:8f0bb79ddd48 156 test_case_start("Format " TEST_FILESYSTEM_TYPE, 3);
leothedragon 0:8f0bb79ddd48 157 logger("[INFO] Resetting storage to a clean state for test.\n");
leothedragon 0:8f0bb79ddd48 158
leothedragon 0:8f0bb79ddd48 159 int storage_status = fs.reformat(&sd);
leothedragon 0:8f0bb79ddd48 160 if (storage_status != 0) {
leothedragon 0:8f0bb79ddd48 161 storage_status = sd.erase(0, sd.size());
leothedragon 0:8f0bb79ddd48 162 if (storage_status == 0) {
leothedragon 0:8f0bb79ddd48 163 storage_status = fs.format(&sd);
leothedragon 0:8f0bb79ddd48 164 if (storage_status != 0) {
leothedragon 0:8f0bb79ddd48 165 logger("[ERROR] Filesystem init failed\n");
leothedragon 0:8f0bb79ddd48 166 }
leothedragon 0:8f0bb79ddd48 167 }
leothedragon 0:8f0bb79ddd48 168 }
leothedragon 0:8f0bb79ddd48 169
leothedragon 0:8f0bb79ddd48 170 // Report status to console.
leothedragon 0:8f0bb79ddd48 171 if (storage_status == 0) {
leothedragon 0:8f0bb79ddd48 172 logger("[INFO] Storage format successful.\r\n");
leothedragon 0:8f0bb79ddd48 173 } else {
leothedragon 0:8f0bb79ddd48 174 logger("[ERROR] Storage format failed.\r\n");
leothedragon 0:8f0bb79ddd48 175 test_failed();
leothedragon 0:8f0bb79ddd48 176 }
leothedragon 0:8f0bb79ddd48 177
leothedragon 0:8f0bb79ddd48 178 test_case_finish("Format " TEST_FILESYSTEM_TYPE, (storage_status == 0), (storage_status != 0));
leothedragon 0:8f0bb79ddd48 179 }
leothedragon 0:8f0bb79ddd48 180
leothedragon 0:8f0bb79ddd48 181 // SimpleMbedCloudClient initialization must be successful.
leothedragon 0:8f0bb79ddd48 182 test_case_start("Initialize Simple PDMC", 4);
leothedragon 0:8f0bb79ddd48 183
leothedragon 0:8f0bb79ddd48 184 SimpleMbedCloudClient client(net, &sd, &fs);
leothedragon 0:8f0bb79ddd48 185 int client_status = client.init();
leothedragon 0:8f0bb79ddd48 186
leothedragon 0:8f0bb79ddd48 187 // Report status to console.
leothedragon 0:8f0bb79ddd48 188 if (client_status == 0) {
leothedragon 0:8f0bb79ddd48 189 logger("[INFO] Simple PDMC initialization successful.\r\n");
leothedragon 0:8f0bb79ddd48 190 } else {
leothedragon 0:8f0bb79ddd48 191 logger("[ERROR] Simple PDMC failed to initialize.\r\n");
leothedragon 0:8f0bb79ddd48 192 // End the test early, cannot continue without successful cloud client initialization.
leothedragon 0:8f0bb79ddd48 193 test_failed();
leothedragon 0:8f0bb79ddd48 194 }
leothedragon 0:8f0bb79ddd48 195
leothedragon 0:8f0bb79ddd48 196 test_case_finish("Initialize Simple PDMC", iteration + (client_status == 0), (client_status != 0));
leothedragon 0:8f0bb79ddd48 197
leothedragon 0:8f0bb79ddd48 198 //Create LwM2M resources
leothedragon 0:8f0bb79ddd48 199 MbedCloudClientResource *res_get_test;
leothedragon 0:8f0bb79ddd48 200 res_get_test = client.create_resource("5000/0/1", "get_resource");
leothedragon 0:8f0bb79ddd48 201 res_get_test->observable(true);
leothedragon 0:8f0bb79ddd48 202 res_get_test->methods(M2MMethod::GET);
leothedragon 0:8f0bb79ddd48 203 res_get_test->set_value("test0");
leothedragon 0:8f0bb79ddd48 204
leothedragon 0:8f0bb79ddd48 205 MbedCloudClientResource *res_put_test;
leothedragon 0:8f0bb79ddd48 206 res_put_test = client.create_resource("5000/0/2", "put_resource");
leothedragon 0:8f0bb79ddd48 207 res_put_test->methods(M2MMethod::PUT | M2MMethod::GET);
leothedragon 0:8f0bb79ddd48 208 res_put_test->set_value(1);
leothedragon 0:8f0bb79ddd48 209
leothedragon 0:8f0bb79ddd48 210 MbedCloudClientResource *res_post_test;
leothedragon 0:8f0bb79ddd48 211 res_post_test = client.create_resource("5000/0/3", "post_resource");
leothedragon 0:8f0bb79ddd48 212 res_post_test->methods(M2MMethod::POST);
leothedragon 0:8f0bb79ddd48 213 res_post_test->attach_post_callback(post_test_callback);
leothedragon 0:8f0bb79ddd48 214
leothedragon 0:8f0bb79ddd48 215 // Register to Pelion Device Management.
leothedragon 0:8f0bb79ddd48 216 if (iteration == 0) {
leothedragon 0:8f0bb79ddd48 217 test_case_start("Pelion Bootstrap & Reg.", 5);
leothedragon 0:8f0bb79ddd48 218 } else {
leothedragon 0:8f0bb79ddd48 219 test_case_start("Pelion Re-register", 7);
leothedragon 0:8f0bb79ddd48 220 }
leothedragon 0:8f0bb79ddd48 221 // Set client callback to report endpoint name.
leothedragon 0:8f0bb79ddd48 222 client.on_registered(&registered);
leothedragon 0:8f0bb79ddd48 223 client.register_and_connect();
leothedragon 0:8f0bb79ddd48 224
leothedragon 0:8f0bb79ddd48 225 i = 600; // wait 60 seconds
leothedragon 0:8f0bb79ddd48 226 while (i-- > 0 && !client.is_client_registered()) {
leothedragon 0:8f0bb79ddd48 227 wait_ms(100);
leothedragon 0:8f0bb79ddd48 228 }
leothedragon 0:8f0bb79ddd48 229
leothedragon 0:8f0bb79ddd48 230 // Get registration status.
leothedragon 0:8f0bb79ddd48 231 bool client_registered = client.is_client_registered();
leothedragon 0:8f0bb79ddd48 232 if (client_registered) {
leothedragon 0:8f0bb79ddd48 233 client_status = 0;
leothedragon 0:8f0bb79ddd48 234 wait_nb(100);
leothedragon 0:8f0bb79ddd48 235 logger("[INFO] Device successfully registered to Pelion DM.\r\n");
leothedragon 0:8f0bb79ddd48 236 } else {
leothedragon 0:8f0bb79ddd48 237 client_status = -1;
leothedragon 0:8f0bb79ddd48 238 logger("[ERROR] Device failed to register.\r\n");
leothedragon 0:8f0bb79ddd48 239 test_failed();
leothedragon 0:8f0bb79ddd48 240 }
leothedragon 0:8f0bb79ddd48 241 if (iteration == 0) {
leothedragon 0:8f0bb79ddd48 242 test_case_finish("Pelion Bootstrap & Reg.", (client_status == 0), (client_status != 0));
leothedragon 0:8f0bb79ddd48 243 } else {
leothedragon 0:8f0bb79ddd48 244 test_case_finish("Pelion Re-register", (client_status == 0), (client_status != 0));
leothedragon 0:8f0bb79ddd48 245 }
leothedragon 0:8f0bb79ddd48 246
leothedragon 0:8f0bb79ddd48 247 if (iteration == 0) {
leothedragon 0:8f0bb79ddd48 248 //Start registration status test
leothedragon 0:8f0bb79ddd48 249 test_case_start("Pelion Directory", 6);
leothedragon 0:8f0bb79ddd48 250 int reg_status;
leothedragon 0:8f0bb79ddd48 251
leothedragon 0:8f0bb79ddd48 252 logger("[INFO] Wait up to 10 seconds for Device Directory to update after initial registration.\r\n");
leothedragon 0:8f0bb79ddd48 253 i = 100;
leothedragon 0:8f0bb79ddd48 254 while (i-- > 0 and !endpointInfo) {
leothedragon 0:8f0bb79ddd48 255 wait(100);
leothedragon 0:8f0bb79ddd48 256 }
leothedragon 0:8f0bb79ddd48 257
leothedragon 0:8f0bb79ddd48 258 // Start host tests with device id
leothedragon 0:8f0bb79ddd48 259 logger("[INFO] Starting Pelion verification using Python SDK...\r\n");
leothedragon 0:8f0bb79ddd48 260 greentea_send_kv("verify_registration", endpointInfo->internal_endpoint_name.c_str());
leothedragon 0:8f0bb79ddd48 261 while (1) {
leothedragon 0:8f0bb79ddd48 262 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 263
leothedragon 0:8f0bb79ddd48 264 if (strcmp(_key, "registered") == 0) {
leothedragon 0:8f0bb79ddd48 265 if (atoi(_value)) {
leothedragon 0:8f0bb79ddd48 266 reg_status = 0;
leothedragon 0:8f0bb79ddd48 267 logger("[INFO] Device is registered in the Device Directory.\r\n");
leothedragon 0:8f0bb79ddd48 268 } else {
leothedragon 0:8f0bb79ddd48 269 reg_status = -1;
leothedragon 0:8f0bb79ddd48 270 logger("[ERROR] Device could not be verified as registered in Device Directory.\r\n");
leothedragon 0:8f0bb79ddd48 271 test_failed();
leothedragon 0:8f0bb79ddd48 272 }
leothedragon 0:8f0bb79ddd48 273 break;
leothedragon 0:8f0bb79ddd48 274 }
leothedragon 0:8f0bb79ddd48 275 }
leothedragon 0:8f0bb79ddd48 276
leothedragon 0:8f0bb79ddd48 277 test_case_finish("Pelion Directory", (reg_status == 0), (reg_status != 0));
leothedragon 0:8f0bb79ddd48 278
leothedragon 0:8f0bb79ddd48 279 if (reg_status == 0) {
leothedragon 0:8f0bb79ddd48 280 logger("[INFO] Resetting device.\r\n");
leothedragon 0:8f0bb79ddd48 281 greentea_send_kv("test_advance", 0);
leothedragon 0:8f0bb79ddd48 282 while (1) {
leothedragon 0:8f0bb79ddd48 283 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 284
leothedragon 0:8f0bb79ddd48 285 if (strcmp(_key, "reset") == 0) {
leothedragon 0:8f0bb79ddd48 286 system_reset();
leothedragon 0:8f0bb79ddd48 287 break;
leothedragon 0:8f0bb79ddd48 288 }
leothedragon 0:8f0bb79ddd48 289 }
leothedragon 0:8f0bb79ddd48 290 }
leothedragon 0:8f0bb79ddd48 291 } else {
leothedragon 0:8f0bb79ddd48 292 //Start consistent identity test.
leothedragon 0:8f0bb79ddd48 293 test_case_start("Post-reset Identity", 8);
leothedragon 0:8f0bb79ddd48 294 int identity_status;
leothedragon 0:8f0bb79ddd48 295
leothedragon 0:8f0bb79ddd48 296 logger("[INFO] Wait up to 5 seconds for Device Directory to update after reboot.\r\n");
leothedragon 0:8f0bb79ddd48 297 i = 50;
leothedragon 0:8f0bb79ddd48 298 while (i-- > 0 and !endpointInfo) {
leothedragon 0:8f0bb79ddd48 299 wait(100);
leothedragon 0:8f0bb79ddd48 300 }
leothedragon 0:8f0bb79ddd48 301
leothedragon 0:8f0bb79ddd48 302 // Wait for Host Test to verify consistent device ID (blocking here)
leothedragon 0:8f0bb79ddd48 303 logger("[INFO] Verifying consistent Device ID...\r\n");
leothedragon 0:8f0bb79ddd48 304 greentea_send_kv("verify_identity", endpointInfo->internal_endpoint_name.c_str());
leothedragon 0:8f0bb79ddd48 305 while (1) {
leothedragon 0:8f0bb79ddd48 306 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 307
leothedragon 0:8f0bb79ddd48 308 if (strcmp(_key, "verified") == 0) {
leothedragon 0:8f0bb79ddd48 309 if (atoi(_value)) {
leothedragon 0:8f0bb79ddd48 310 identity_status = 0;
leothedragon 0:8f0bb79ddd48 311 logger("[INFO] Device ID consistent, SOTP and Secure Storage is preserved correctly.\r\n");
leothedragon 0:8f0bb79ddd48 312 } else {
leothedragon 0:8f0bb79ddd48 313 identity_status = -1;
leothedragon 0:8f0bb79ddd48 314 logger("[ERROR] Device ID is inconsistent. SOTP and Secure Storage was not preserved.\r\n");
leothedragon 0:8f0bb79ddd48 315 }
leothedragon 0:8f0bb79ddd48 316 break;
leothedragon 0:8f0bb79ddd48 317 }
leothedragon 0:8f0bb79ddd48 318 }
leothedragon 0:8f0bb79ddd48 319
leothedragon 0:8f0bb79ddd48 320 test_case_finish("Post-reset Identity", (identity_status == 0), (identity_status != 0));
leothedragon 0:8f0bb79ddd48 321
leothedragon 0:8f0bb79ddd48 322 // LwM2M tests
leothedragon 0:8f0bb79ddd48 323 logger("[INFO] Beginning LwM2M resource tests.\r\n");
leothedragon 0:8f0bb79ddd48 324
leothedragon 0:8f0bb79ddd48 325 wait_nb(1000);
leothedragon 0:8f0bb79ddd48 326
leothedragon 0:8f0bb79ddd48 327 // ---------------------------------------------
leothedragon 0:8f0bb79ddd48 328 // GET test
leothedragon 0:8f0bb79ddd48 329 test_case_start("Resource LwM2M GET", 9);
leothedragon 0:8f0bb79ddd48 330 int get_status;
leothedragon 0:8f0bb79ddd48 331 // Read original value of /5000/0/1 and wait for Host Test to verify it read the value and send it back.
leothedragon 0:8f0bb79ddd48 332 greentea_send_kv("verify_lwm2m_get_test", "/5000/0/1");
leothedragon 0:8f0bb79ddd48 333 while (1) {
leothedragon 0:8f0bb79ddd48 334 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 335
leothedragon 0:8f0bb79ddd48 336 if (strcmp(_key, "get_value") == 0) {
leothedragon 0:8f0bb79ddd48 337 if (strcmp(_value, "test0") == 0) {
leothedragon 0:8f0bb79ddd48 338 get_status = 0;
leothedragon 0:8f0bb79ddd48 339 logger("[INFO] Original value of LwM2M resource /5000/0/1 is read correctly\r\n");
leothedragon 0:8f0bb79ddd48 340 } else {
leothedragon 0:8f0bb79ddd48 341 get_status = -1;
leothedragon 0:8f0bb79ddd48 342 logger("[ERROR] Wrong value reported in Pelion DM.\r\n");
leothedragon 0:8f0bb79ddd48 343 }
leothedragon 0:8f0bb79ddd48 344 break;
leothedragon 0:8f0bb79ddd48 345 } else if (strcmp(_key, "timeout") == 0) {
leothedragon 0:8f0bb79ddd48 346 get_status = -1;
leothedragon 0:8f0bb79ddd48 347 logger("[ERROR] Observation of LwM2M resource /5000/0/1 timed out.\r\n");
leothedragon 0:8f0bb79ddd48 348 break;
leothedragon 0:8f0bb79ddd48 349 }
leothedragon 0:8f0bb79ddd48 350 }
leothedragon 0:8f0bb79ddd48 351 test_case_finish("Resource LwM2M GET", (get_status == 0), (get_status != 0));
leothedragon 0:8f0bb79ddd48 352
leothedragon 0:8f0bb79ddd48 353 wait_nb(500);
leothedragon 0:8f0bb79ddd48 354
leothedragon 0:8f0bb79ddd48 355 // ---------------------------------------------
leothedragon 0:8f0bb79ddd48 356 // SET test
leothedragon 0:8f0bb79ddd48 357 test_case_start("Resource LwM2M SET", 10);
leothedragon 0:8f0bb79ddd48 358 int set_status;
leothedragon 0:8f0bb79ddd48 359 // Update resource /5000/0/1 from client and observe value
leothedragon 0:8f0bb79ddd48 360 res_get_test->set_value("test1");
leothedragon 0:8f0bb79ddd48 361
leothedragon 0:8f0bb79ddd48 362 greentea_send_kv("verify_lwm2m_set_test", "/5000/0/1");
leothedragon 0:8f0bb79ddd48 363 while (1) {
leothedragon 0:8f0bb79ddd48 364 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 365
leothedragon 0:8f0bb79ddd48 366 if (strcmp(_key, "set_value") == 0) {
leothedragon 0:8f0bb79ddd48 367 if (strcmp(_value, "test1") == 0) {
leothedragon 0:8f0bb79ddd48 368 set_status = 0;
leothedragon 0:8f0bb79ddd48 369 logger("[INFO] Changed value of LwM2M resource /5000/0/1 is observed correctly\r\n");
leothedragon 0:8f0bb79ddd48 370 } else {
leothedragon 0:8f0bb79ddd48 371 set_status = -1;
leothedragon 0:8f0bb79ddd48 372 logger("[ERROR] Wrong value observed in Pelion DM.\r\n");
leothedragon 0:8f0bb79ddd48 373 }
leothedragon 0:8f0bb79ddd48 374 break;
leothedragon 0:8f0bb79ddd48 375 } else if (strcmp(_key, "timeout") == 0) {
leothedragon 0:8f0bb79ddd48 376 set_status = -1;
leothedragon 0:8f0bb79ddd48 377 logger("[ERROR] Observation of LwM2M resource /5000/0/1 timed out.\r\n");
leothedragon 0:8f0bb79ddd48 378 break;
leothedragon 0:8f0bb79ddd48 379 }
leothedragon 0:8f0bb79ddd48 380 }
leothedragon 0:8f0bb79ddd48 381 test_case_finish("Resource LwM2M SET", (set_status == 0), (set_status != 0));
leothedragon 0:8f0bb79ddd48 382
leothedragon 0:8f0bb79ddd48 383 wait_nb(500);
leothedragon 0:8f0bb79ddd48 384
leothedragon 0:8f0bb79ddd48 385 // ---------------------------------------------
leothedragon 0:8f0bb79ddd48 386 // PUT Test
leothedragon 0:8f0bb79ddd48 387 test_case_start("Resource LwM2M PUT", 11);
leothedragon 0:8f0bb79ddd48 388 int put_status;
leothedragon 0:8f0bb79ddd48 389 int current_res_value;
leothedragon 0:8f0bb79ddd48 390 int updated_res_value;
leothedragon 0:8f0bb79ddd48 391
leothedragon 0:8f0bb79ddd48 392 // Observe resource /5000/0/2 from cloud, add +5, and confirm value is correct on client
leothedragon 0:8f0bb79ddd48 393 greentea_send_kv("verify_lwm2m_put_test", "/5000/0/2");
leothedragon 0:8f0bb79ddd48 394 while (1) {
leothedragon 0:8f0bb79ddd48 395 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 396
leothedragon 0:8f0bb79ddd48 397 if (strcmp(_key, "res_set") == 0) {
leothedragon 0:8f0bb79ddd48 398 // Get updated value from host test.
leothedragon 0:8f0bb79ddd48 399 updated_res_value = atoi(_value);
leothedragon 0:8f0bb79ddd48 400 // Get current value from resource.
leothedragon 0:8f0bb79ddd48 401 current_res_value = res_put_test->get_value_int();
leothedragon 0:8f0bb79ddd48 402
leothedragon 0:8f0bb79ddd48 403 if (updated_res_value == current_res_value) {
leothedragon 0:8f0bb79ddd48 404 put_status = 0;
leothedragon 0:8f0bb79ddd48 405 logger("[INFO] Value of resource /5000/0/2 successfully changed from the cloud using PUT.\r\n");
leothedragon 0:8f0bb79ddd48 406 } else {
leothedragon 0:8f0bb79ddd48 407 put_status = -1;
leothedragon 0:8f0bb79ddd48 408 logger("[ERROR] Wrong value read from device after resource update.\r\n");
leothedragon 0:8f0bb79ddd48 409 }
leothedragon 0:8f0bb79ddd48 410 break;
leothedragon 0:8f0bb79ddd48 411 } else if (strcmp(_key, "timeout") == 0) {
leothedragon 0:8f0bb79ddd48 412 put_status = -1;
leothedragon 0:8f0bb79ddd48 413 logger("[ERROR] PUT of LwM2M resource /5000/0/2 timed out.\r\n");
leothedragon 0:8f0bb79ddd48 414 break;
leothedragon 0:8f0bb79ddd48 415 }
leothedragon 0:8f0bb79ddd48 416 }
leothedragon 0:8f0bb79ddd48 417
leothedragon 0:8f0bb79ddd48 418 test_case_finish("Resource LwM2M PUT", (put_status == 0), (put_status != 0));
leothedragon 0:8f0bb79ddd48 419
leothedragon 0:8f0bb79ddd48 420 wait_nb(500);
leothedragon 0:8f0bb79ddd48 421
leothedragon 0:8f0bb79ddd48 422 // ---------------------------------------------
leothedragon 0:8f0bb79ddd48 423 // POST test
leothedragon 0:8f0bb79ddd48 424 test_case_start("Resource LwM2M POST", 12);
leothedragon 0:8f0bb79ddd48 425 int post_status;
leothedragon 0:8f0bb79ddd48 426
leothedragon 0:8f0bb79ddd48 427 logger("[INFO] Executing POST on /5000/0/3 and waiting for callback function\r\n.");
leothedragon 0:8f0bb79ddd48 428 greentea_send_kv("verify_lwm2m_post_test", "/5000/0/3");
leothedragon 0:8f0bb79ddd48 429 while (1) {
leothedragon 0:8f0bb79ddd48 430 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
leothedragon 0:8f0bb79ddd48 431
leothedragon 0:8f0bb79ddd48 432 if (strcmp(_key, "post_test_executed") == 0) {
leothedragon 0:8f0bb79ddd48 433 int result = atoi(_value);
leothedragon 0:8f0bb79ddd48 434 if (result == 0) {
leothedragon 0:8f0bb79ddd48 435 post_status = 0;
leothedragon 0:8f0bb79ddd48 436 logger("[INFO] Callback on resource /5000/0/3 executed successfully.\r\n");
leothedragon 0:8f0bb79ddd48 437 } else {
leothedragon 0:8f0bb79ddd48 438 post_status = -1;
leothedragon 0:8f0bb79ddd48 439 logger("[ERROR] Callback on resource /5000/0/3 failed.\r\n");
leothedragon 0:8f0bb79ddd48 440 }
leothedragon 0:8f0bb79ddd48 441 break;
leothedragon 0:8f0bb79ddd48 442 } else if (strcmp(_key, "timeout") == 0) {
leothedragon 0:8f0bb79ddd48 443 post_status = -1;
leothedragon 0:8f0bb79ddd48 444 logger("[ERROR] POST of LwM2M resource /5000/0/3 timed out.\r\n");
leothedragon 0:8f0bb79ddd48 445 break;
leothedragon 0:8f0bb79ddd48 446 }
leothedragon 0:8f0bb79ddd48 447 }
leothedragon 0:8f0bb79ddd48 448
leothedragon 0:8f0bb79ddd48 449 test_case_finish("Resource LwM2M POST", (post_status == 0), (post_status != 0));
leothedragon 0:8f0bb79ddd48 450
leothedragon 0:8f0bb79ddd48 451 GREENTEA_TESTSUITE_RESULT((get_status == 0) && (set_status == 0) && (put_status == 0) && (post_status == 0));
leothedragon 0:8f0bb79ddd48 452
leothedragon 0:8f0bb79ddd48 453 while (1) {
leothedragon 0:8f0bb79ddd48 454 wait(100);
leothedragon 0:8f0bb79ddd48 455 }
leothedragon 0:8f0bb79ddd48 456 }
leothedragon 0:8f0bb79ddd48 457 }
leothedragon 0:8f0bb79ddd48 458
leothedragon 0:8f0bb79ddd48 459 int main(void) {
leothedragon 0:8f0bb79ddd48 460 //Create a thread to blink an LED and signal that the device is alive
leothedragon 0:8f0bb79ddd48 461 #if !defined(MBED_CONF_APP_NO_LED)
leothedragon 0:8f0bb79ddd48 462 Ticker t;
leothedragon 0:8f0bb79ddd48 463 t.attach(led_thread, 0.5);
leothedragon 0:8f0bb79ddd48 464 #endif
leothedragon 0:8f0bb79ddd48 465
leothedragon 0:8f0bb79ddd48 466 greentea_send_kv("device_booted", 1);
leothedragon 0:8f0bb79ddd48 467
leothedragon 0:8f0bb79ddd48 468 GREENTEA_SETUP(240, "sdk_host_tests");
leothedragon 0:8f0bb79ddd48 469 spdmc_testsuite_connect();
leothedragon 0:8f0bb79ddd48 470
leothedragon 0:8f0bb79ddd48 471 return 0;
leothedragon 0:8f0bb79ddd48 472 }