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 /*
leothedragon 0:8f0bb79ddd48 21 * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
leothedragon 0:8f0bb79ddd48 22 */
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 #include "mbed.h"
leothedragon 0:8f0bb79ddd48 25 #include "FATFileSystem.h"
leothedragon 0:8f0bb79ddd48 26 #include "LittleFileSystem.h"
leothedragon 0:8f0bb79ddd48 27 #include "utest/utest.h"
leothedragon 0:8f0bb79ddd48 28 #include "unity/unity.h"
leothedragon 0:8f0bb79ddd48 29 #include "greentea-client/test_env.h"
leothedragon 0:8f0bb79ddd48 30 #include "common_defines_test.h"
leothedragon 0:8f0bb79ddd48 31 #include "download_test.h"
leothedragon 0:8f0bb79ddd48 32 #include "file_test.h"
leothedragon 0:8f0bb79ddd48 33 #include <string>
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME
leothedragon 0:8f0bb79ddd48 36 #include MBED_CONF_APP_BASICS_TEST_FILENAME
leothedragon 0:8f0bb79ddd48 37 #else
leothedragon 0:8f0bb79ddd48 38 #include "alice.h"
leothedragon 0:8f0bb79ddd48 39 #endif
leothedragon 0:8f0bb79ddd48 40
leothedragon 0:8f0bb79ddd48 41 #ifndef MBED_CONF_APP_TESTS_FS_SIZE
leothedragon 0:8f0bb79ddd48 42 #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024)
leothedragon 0:8f0bb79ddd48 43 #endif
leothedragon 0:8f0bb79ddd48 44
leothedragon 0:8f0bb79ddd48 45 using namespace utest::v1;
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 #if !defined(MBED_CONF_APP_NO_LED)
leothedragon 0:8f0bb79ddd48 48 DigitalOut led1(LED1);
leothedragon 0:8f0bb79ddd48 49 DigitalOut led2(LED2);
leothedragon 0:8f0bb79ddd48 50 void led_thread() {
leothedragon 0:8f0bb79ddd48 51 led1 = !led1;
leothedragon 0:8f0bb79ddd48 52 led2 = !led1;
leothedragon 0:8f0bb79ddd48 53 }
leothedragon 0:8f0bb79ddd48 54 #endif
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 #define MAX_RETRIES 3
leothedragon 0:8f0bb79ddd48 57 NetworkInterface* interface = NULL;
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 static control_t setup_network(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 60 interface = NetworkInterface::get_default_instance();
leothedragon 0:8f0bb79ddd48 61 TEST_ASSERT_NOT_NULL_MESSAGE(interface, "failed to initialize network");
leothedragon 0:8f0bb79ddd48 62
leothedragon 0:8f0bb79ddd48 63 nsapi_error_t err = -1;
leothedragon 0:8f0bb79ddd48 64 for (int tries = 0; tries < MAX_RETRIES; tries++) {
leothedragon 0:8f0bb79ddd48 65 err = interface->connect();
leothedragon 0:8f0bb79ddd48 66 if (err == NSAPI_ERROR_OK) {
leothedragon 0:8f0bb79ddd48 67 break;
leothedragon 0:8f0bb79ddd48 68 } else {
leothedragon 0:8f0bb79ddd48 69 printf("[ERROR] Connecting to network. Retrying %d of %d...\r\n", tries, MAX_RETRIES);
leothedragon 0:8f0bb79ddd48 70 }
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
leothedragon 0:8f0bb79ddd48 73 printf("[NET] IP address is '%s'\n", interface->get_ip_address());
leothedragon 0:8f0bb79ddd48 74 printf("[NET] MAC address is '%s'\n", interface->get_mac_address());
leothedragon 0:8f0bb79ddd48 75 return CaseNext;
leothedragon 0:8f0bb79ddd48 76 }
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 BlockDevice* bd = BlockDevice::get_default_instance();
leothedragon 0:8f0bb79ddd48 79 SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
leothedragon 0:8f0bb79ddd48 80 #if TEST_USE_FILESYSTEM == FS_FAT
leothedragon 0:8f0bb79ddd48 81 FATFileSystem fs("sd");
leothedragon 0:8f0bb79ddd48 82 #else
leothedragon 0:8f0bb79ddd48 83 LittleFileSystem fs("sd");
leothedragon 0:8f0bb79ddd48 84 #endif
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 static control_t test_format(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 87 int format_err = fs.format(&sd);
leothedragon 0:8f0bb79ddd48 88 TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
leothedragon 0:8f0bb79ddd48 89
leothedragon 0:8f0bb79ddd48 90 int mount_err = fs.mount(&sd);
leothedragon 0:8f0bb79ddd48 91 TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
leothedragon 0:8f0bb79ddd48 92
leothedragon 0:8f0bb79ddd48 93 return CaseNext;
leothedragon 0:8f0bb79ddd48 94 }
leothedragon 0:8f0bb79ddd48 95
leothedragon 0:8f0bb79ddd48 96 static uint32_t thread_counter = 0;
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98 void download_fn() {
leothedragon 0:8f0bb79ddd48 99 uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
leothedragon 0:8f0bb79ddd48 100 download_test(interface, story, sizeof(story), 256, thread_id);
leothedragon 0:8f0bb79ddd48 101 }
leothedragon 0:8f0bb79ddd48 102 void file_fn(size_t buffer) {
leothedragon 0:8f0bb79ddd48 103 uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
leothedragon 0:8f0bb79ddd48 104 char filename[255] = { 0 };
leothedragon 0:8f0bb79ddd48 105 snprintf(filename, 255, "mbed-file-test-%d.txt", thread_id);
leothedragon 0:8f0bb79ddd48 106 file_test_write(filename, 0, story, sizeof(story), buffer);
leothedragon 0:8f0bb79ddd48 107 file_test_read(filename, 0, story, sizeof(story), buffer);
leothedragon 0:8f0bb79ddd48 108 }
leothedragon 0:8f0bb79ddd48 109 void file_1b_fn() { return file_fn(1); }
leothedragon 0:8f0bb79ddd48 110 void file_4b_fn() { return file_fn(4); }
leothedragon 0:8f0bb79ddd48 111 void file_256b_fn() { return file_fn(256); }
leothedragon 0:8f0bb79ddd48 112 void file_1kb_fn() { return file_fn(1024); }
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 static control_t stress_1_thread(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 115 thread_counter = 0;
leothedragon 0:8f0bb79ddd48 116
leothedragon 0:8f0bb79ddd48 117 Thread t1;
leothedragon 0:8f0bb79ddd48 118 t1.start(download_fn);
leothedragon 0:8f0bb79ddd48 119 t1.join();
leothedragon 0:8f0bb79ddd48 120 t1.start(file_1kb_fn);
leothedragon 0:8f0bb79ddd48 121 t1.join();
leothedragon 0:8f0bb79ddd48 122
leothedragon 0:8f0bb79ddd48 123 return CaseNext;
leothedragon 0:8f0bb79ddd48 124 }
leothedragon 0:8f0bb79ddd48 125
leothedragon 0:8f0bb79ddd48 126 static control_t stress_2_threads(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 127 thread_counter = 0;
leothedragon 0:8f0bb79ddd48 128
leothedragon 0:8f0bb79ddd48 129 Thread t1;
leothedragon 0:8f0bb79ddd48 130 Thread t2;
leothedragon 0:8f0bb79ddd48 131 t1.start(file_1kb_fn);
leothedragon 0:8f0bb79ddd48 132 wait(1);
leothedragon 0:8f0bb79ddd48 133 t2.start(download_fn);
leothedragon 0:8f0bb79ddd48 134 t2.join();
leothedragon 0:8f0bb79ddd48 135 t1.join();
leothedragon 0:8f0bb79ddd48 136
leothedragon 0:8f0bb79ddd48 137 return CaseNext;
leothedragon 0:8f0bb79ddd48 138 }
leothedragon 0:8f0bb79ddd48 139
leothedragon 0:8f0bb79ddd48 140 static control_t stress_3_threads(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 141 thread_counter = 0;
leothedragon 0:8f0bb79ddd48 142
leothedragon 0:8f0bb79ddd48 143 Thread t1;
leothedragon 0:8f0bb79ddd48 144 Thread t2;
leothedragon 0:8f0bb79ddd48 145 Thread t3;
leothedragon 0:8f0bb79ddd48 146 t1.start(file_256b_fn);
leothedragon 0:8f0bb79ddd48 147 t2.start(file_1kb_fn);
leothedragon 0:8f0bb79ddd48 148 wait(1);
leothedragon 0:8f0bb79ddd48 149 t3.start(download_fn);
leothedragon 0:8f0bb79ddd48 150 t3.join();
leothedragon 0:8f0bb79ddd48 151 t2.join();
leothedragon 0:8f0bb79ddd48 152 t1.join();
leothedragon 0:8f0bb79ddd48 153
leothedragon 0:8f0bb79ddd48 154 return CaseNext;
leothedragon 0:8f0bb79ddd48 155 }
leothedragon 0:8f0bb79ddd48 156
leothedragon 0:8f0bb79ddd48 157 static control_t stress_4_threads(const size_t call_count) {
leothedragon 0:8f0bb79ddd48 158 thread_counter = 0;
leothedragon 0:8f0bb79ddd48 159
leothedragon 0:8f0bb79ddd48 160 Thread t1;
leothedragon 0:8f0bb79ddd48 161 Thread t2;
leothedragon 0:8f0bb79ddd48 162 Thread t3;
leothedragon 0:8f0bb79ddd48 163 Thread t4;
leothedragon 0:8f0bb79ddd48 164 t1.start(file_256b_fn);
leothedragon 0:8f0bb79ddd48 165 t2.start(file_256b_fn);
leothedragon 0:8f0bb79ddd48 166 t3.start(file_256b_fn);
leothedragon 0:8f0bb79ddd48 167 wait(1);
leothedragon 0:8f0bb79ddd48 168 t4.start(download_fn);
leothedragon 0:8f0bb79ddd48 169 t4.join();
leothedragon 0:8f0bb79ddd48 170 t3.join();
leothedragon 0:8f0bb79ddd48 171 t2.join();
leothedragon 0:8f0bb79ddd48 172 t1.join();
leothedragon 0:8f0bb79ddd48 173
leothedragon 0:8f0bb79ddd48 174 return CaseNext;
leothedragon 0:8f0bb79ddd48 175 }
leothedragon 0:8f0bb79ddd48 176
leothedragon 0:8f0bb79ddd48 177 template <uint32_t size>
leothedragon 0:8f0bb79ddd48 178 void test_malloc(){
leothedragon 0:8f0bb79ddd48 179
leothedragon 0:8f0bb79ddd48 180 void *bufferTest = NULL;
leothedragon 0:8f0bb79ddd48 181 TEST_ASSERT_MESSAGE(size > 0, "Size must not be zero for test");
leothedragon 0:8f0bb79ddd48 182 printf("Allocating %d bytes",(int)size);
leothedragon 0:8f0bb79ddd48 183 bufferTest = malloc(size);
leothedragon 0:8f0bb79ddd48 184 TEST_ASSERT(bufferTest !=NULL);
leothedragon 0:8f0bb79ddd48 185 free(bufferTest);
leothedragon 0:8f0bb79ddd48 186 }
leothedragon 0:8f0bb79ddd48 187
leothedragon 0:8f0bb79ddd48 188 utest::v1::status_t greentea_setup(const size_t number_of_cases) {
leothedragon 0:8f0bb79ddd48 189 GREENTEA_SETUP(10*60, "default_auto");
leothedragon 0:8f0bb79ddd48 190 return greentea_test_setup_handler(number_of_cases);
leothedragon 0:8f0bb79ddd48 191 }
leothedragon 0:8f0bb79ddd48 192
leothedragon 0:8f0bb79ddd48 193 Case cases[] = {
leothedragon 0:8f0bb79ddd48 194 Case(TEST_NETWORK_TYPE " network setup", setup_network),
leothedragon 0:8f0bb79ddd48 195 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " format", test_format),
leothedragon 0:8f0bb79ddd48 196 Case("Test memory allocation of 10 K bytes", test_malloc<TEST_MEMORY_SIZE_10K>),
leothedragon 0:8f0bb79ddd48 197 Case("Test memory allocation of 20 K bytes", test_malloc<TEST_MEMORY_SIZE_20K>),
leothedragon 0:8f0bb79ddd48 198 Case("Test memory allocation of 40 K bytes", test_malloc<TEST_MEMORY_SIZE_40K>),
leothedragon 0:8f0bb79ddd48 199 Case("Test memory allocation of 60 K bytes", test_malloc<TEST_MEMORY_SIZE_60K>),
leothedragon 0:8f0bb79ddd48 200 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR
leothedragon 0:8f0bb79ddd48 201 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 1 thread, dl, file seq.", stress_1_thread),
leothedragon 0:8f0bb79ddd48 202 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 2 threads, dl, 1kb", stress_2_threads),
leothedragon 0:8f0bb79ddd48 203 #endif
leothedragon 0:8f0bb79ddd48 204 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 3 threads, dl, 256b, 1kb", stress_3_threads),
leothedragon 0:8f0bb79ddd48 205 //Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 4 threads, dl, 256b, 256b, 256b", stress_4_threads),
leothedragon 0:8f0bb79ddd48 206 };
leothedragon 0:8f0bb79ddd48 207
leothedragon 0:8f0bb79ddd48 208 Specification specification(greentea_setup, cases);
leothedragon 0:8f0bb79ddd48 209
leothedragon 0:8f0bb79ddd48 210 int main() {
leothedragon 0:8f0bb79ddd48 211 //Create a thread to blink an LED and signal that the device is alive
leothedragon 0:8f0bb79ddd48 212 #if !defined(MBED_CONF_APP_NO_LED)
leothedragon 0:8f0bb79ddd48 213 Ticker t;
leothedragon 0:8f0bb79ddd48 214 t.attach(led_thread, 0.5);
leothedragon 0:8f0bb79ddd48 215 #endif
leothedragon 0:8f0bb79ddd48 216
leothedragon 0:8f0bb79ddd48 217 return !Harness::run(specification);
leothedragon 0:8f0bb79ddd48 218 }