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/basic/net-threaded/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 | /* |
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 "utest/utest.h" |
leothedragon | 0:8f0bb79ddd48 | 26 | #include "unity/unity.h" |
leothedragon | 0:8f0bb79ddd48 | 27 | #include "greentea-client/test_env.h" |
leothedragon | 0:8f0bb79ddd48 | 28 | #include "common_defines_test.h" |
leothedragon | 0:8f0bb79ddd48 | 29 | #include "download_test.h" |
leothedragon | 0:8f0bb79ddd48 | 30 | #include <string> |
leothedragon | 0:8f0bb79ddd48 | 31 | |
leothedragon | 0:8f0bb79ddd48 | 32 | #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME |
leothedragon | 0:8f0bb79ddd48 | 33 | #include MBED_CONF_APP_BASICS_TEST_FILENAME |
leothedragon | 0:8f0bb79ddd48 | 34 | #else |
leothedragon | 0:8f0bb79ddd48 | 35 | #include "alice.h" |
leothedragon | 0:8f0bb79ddd48 | 36 | #endif |
leothedragon | 0:8f0bb79ddd48 | 37 | |
leothedragon | 0:8f0bb79ddd48 | 38 | #ifndef MBED_CONF_APP_TESTS_FS_SIZE |
leothedragon | 0:8f0bb79ddd48 | 39 | #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024) |
leothedragon | 0:8f0bb79ddd48 | 40 | #endif |
leothedragon | 0:8f0bb79ddd48 | 41 | |
leothedragon | 0:8f0bb79ddd48 | 42 | using namespace utest::v1; |
leothedragon | 0:8f0bb79ddd48 | 43 | |
leothedragon | 0:8f0bb79ddd48 | 44 | #if !defined(MBED_CONF_APP_NO_LED) |
leothedragon | 0:8f0bb79ddd48 | 45 | DigitalOut led1(LED1); |
leothedragon | 0:8f0bb79ddd48 | 46 | DigitalOut led2(LED2); |
leothedragon | 0:8f0bb79ddd48 | 47 | void led_thread() { |
leothedragon | 0:8f0bb79ddd48 | 48 | led1 = !led1; |
leothedragon | 0:8f0bb79ddd48 | 49 | led2 = !led1; |
leothedragon | 0:8f0bb79ddd48 | 50 | } |
leothedragon | 0:8f0bb79ddd48 | 51 | #endif |
leothedragon | 0:8f0bb79ddd48 | 52 | |
leothedragon | 0:8f0bb79ddd48 | 53 | #define MAX_RETRIES 3 |
leothedragon | 0:8f0bb79ddd48 | 54 | NetworkInterface* net = NULL; |
leothedragon | 0:8f0bb79ddd48 | 55 | static control_t setup_network(const size_t call_count) { |
leothedragon | 0:8f0bb79ddd48 | 56 | net = NetworkInterface::get_default_instance(); |
leothedragon | 0:8f0bb79ddd48 | 57 | TEST_ASSERT_NOT_NULL_MESSAGE(net, "failed to initialize network"); |
leothedragon | 0:8f0bb79ddd48 | 58 | |
leothedragon | 0:8f0bb79ddd48 | 59 | nsapi_error_t err = -1; |
leothedragon | 0:8f0bb79ddd48 | 60 | for (int tries = 0; tries < MAX_RETRIES; tries++) { |
leothedragon | 0:8f0bb79ddd48 | 61 | err = net->connect(); |
leothedragon | 0:8f0bb79ddd48 | 62 | if (err == NSAPI_ERROR_OK) { |
leothedragon | 0:8f0bb79ddd48 | 63 | break; |
leothedragon | 0:8f0bb79ddd48 | 64 | } else { |
leothedragon | 0:8f0bb79ddd48 | 65 | printf("[ERROR] Connecting to network. Retrying %d of %d.\r\n", tries, MAX_RETRIES); |
leothedragon | 0:8f0bb79ddd48 | 66 | } |
leothedragon | 0:8f0bb79ddd48 | 67 | } |
leothedragon | 0:8f0bb79ddd48 | 68 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
leothedragon | 0:8f0bb79ddd48 | 69 | printf("[NET] IP address is '%s'\n", net->get_ip_address()); |
leothedragon | 0:8f0bb79ddd48 | 70 | printf("[NET] MAC address is '%s'\n", net->get_mac_address()); |
leothedragon | 0:8f0bb79ddd48 | 71 | return CaseNext; |
leothedragon | 0:8f0bb79ddd48 | 72 | } |
leothedragon | 0:8f0bb79ddd48 | 73 | |
leothedragon | 0:8f0bb79ddd48 | 74 | static uint32_t thread_counter = 0; |
leothedragon | 0:8f0bb79ddd48 | 75 | void download_fn() { |
leothedragon | 0:8f0bb79ddd48 | 76 | uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1); |
leothedragon | 0:8f0bb79ddd48 | 77 | download_test(net, story, sizeof(story), 1024, thread_id); |
leothedragon | 0:8f0bb79ddd48 | 78 | } |
leothedragon | 0:8f0bb79ddd48 | 79 | |
leothedragon | 0:8f0bb79ddd48 | 80 | static control_t download_1_thread(const size_t call_count) { |
leothedragon | 0:8f0bb79ddd48 | 81 | thread_counter = 0; |
leothedragon | 0:8f0bb79ddd48 | 82 | |
leothedragon | 0:8f0bb79ddd48 | 83 | Thread t1; |
leothedragon | 0:8f0bb79ddd48 | 84 | t1.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 85 | t1.join(); |
leothedragon | 0:8f0bb79ddd48 | 86 | |
leothedragon | 0:8f0bb79ddd48 | 87 | return CaseNext; |
leothedragon | 0:8f0bb79ddd48 | 88 | } |
leothedragon | 0:8f0bb79ddd48 | 89 | |
leothedragon | 0:8f0bb79ddd48 | 90 | static control_t download_2_threads(const size_t call_count) { |
leothedragon | 0:8f0bb79ddd48 | 91 | thread_counter = 0; |
leothedragon | 0:8f0bb79ddd48 | 92 | |
leothedragon | 0:8f0bb79ddd48 | 93 | Thread t1; |
leothedragon | 0:8f0bb79ddd48 | 94 | Thread t2; |
leothedragon | 0:8f0bb79ddd48 | 95 | t1.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 96 | wait(0.5); |
leothedragon | 0:8f0bb79ddd48 | 97 | t2.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 98 | t2.join(); |
leothedragon | 0:8f0bb79ddd48 | 99 | t1.join(); |
leothedragon | 0:8f0bb79ddd48 | 100 | |
leothedragon | 0:8f0bb79ddd48 | 101 | return CaseNext; |
leothedragon | 0:8f0bb79ddd48 | 102 | } |
leothedragon | 0:8f0bb79ddd48 | 103 | |
leothedragon | 0:8f0bb79ddd48 | 104 | static control_t download_3_threads(const size_t call_count) { |
leothedragon | 0:8f0bb79ddd48 | 105 | thread_counter = 0; |
leothedragon | 0:8f0bb79ddd48 | 106 | |
leothedragon | 0:8f0bb79ddd48 | 107 | Thread t1; |
leothedragon | 0:8f0bb79ddd48 | 108 | Thread t2; |
leothedragon | 0:8f0bb79ddd48 | 109 | Thread t3; |
leothedragon | 0:8f0bb79ddd48 | 110 | t1.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 111 | t2.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 112 | t3.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 113 | t1.join(); |
leothedragon | 0:8f0bb79ddd48 | 114 | t2.join(); |
leothedragon | 0:8f0bb79ddd48 | 115 | t3.join(); |
leothedragon | 0:8f0bb79ddd48 | 116 | |
leothedragon | 0:8f0bb79ddd48 | 117 | return CaseNext; |
leothedragon | 0:8f0bb79ddd48 | 118 | } |
leothedragon | 0:8f0bb79ddd48 | 119 | |
leothedragon | 0:8f0bb79ddd48 | 120 | static control_t download_4_threads(const size_t call_count) { |
leothedragon | 0:8f0bb79ddd48 | 121 | thread_counter = 0; |
leothedragon | 0:8f0bb79ddd48 | 122 | |
leothedragon | 0:8f0bb79ddd48 | 123 | Thread t1; |
leothedragon | 0:8f0bb79ddd48 | 124 | Thread t2; |
leothedragon | 0:8f0bb79ddd48 | 125 | Thread t3; |
leothedragon | 0:8f0bb79ddd48 | 126 | Thread t4; |
leothedragon | 0:8f0bb79ddd48 | 127 | t1.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 128 | t2.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 129 | t3.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 130 | t4.start(download_fn); |
leothedragon | 0:8f0bb79ddd48 | 131 | t1.join(); |
leothedragon | 0:8f0bb79ddd48 | 132 | t2.join(); |
leothedragon | 0:8f0bb79ddd48 | 133 | t3.join(); |
leothedragon | 0:8f0bb79ddd48 | 134 | t4.join(); |
leothedragon | 0:8f0bb79ddd48 | 135 | |
leothedragon | 0:8f0bb79ddd48 | 136 | return CaseNext; |
leothedragon | 0:8f0bb79ddd48 | 137 | } |
leothedragon | 0:8f0bb79ddd48 | 138 | |
leothedragon | 0:8f0bb79ddd48 | 139 | utest::v1::status_t greentea_setup(const size_t number_of_cases) { |
leothedragon | 0:8f0bb79ddd48 | 140 | GREENTEA_SETUP(10*60, "default_auto"); |
leothedragon | 0:8f0bb79ddd48 | 141 | return greentea_test_setup_handler(number_of_cases); |
leothedragon | 0:8f0bb79ddd48 | 142 | } |
leothedragon | 0:8f0bb79ddd48 | 143 | |
leothedragon | 0:8f0bb79ddd48 | 144 | Case cases[] = { |
leothedragon | 0:8f0bb79ddd48 | 145 | Case(TEST_NETWORK_TYPE " network setup", setup_network), |
leothedragon | 0:8f0bb79ddd48 | 146 | Case(TEST_NETWORK_TYPE " 1 thread", download_1_thread), |
leothedragon | 0:8f0bb79ddd48 | 147 | #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR |
leothedragon | 0:8f0bb79ddd48 | 148 | Case(TEST_NETWORK_TYPE " 2 threads", download_2_threads), |
leothedragon | 0:8f0bb79ddd48 | 149 | #endif |
leothedragon | 0:8f0bb79ddd48 | 150 | //Case(TEST_NETWORK_TYPE " 3 threads", download_3_threads), |
leothedragon | 0:8f0bb79ddd48 | 151 | // 4 threads may fail due to http host limits |
leothedragon | 0:8f0bb79ddd48 | 152 | //Case(TEST_NETWORK_TYPE " 4 threads", download_4_threads), |
leothedragon | 0:8f0bb79ddd48 | 153 | }; |
leothedragon | 0:8f0bb79ddd48 | 154 | |
leothedragon | 0:8f0bb79ddd48 | 155 | Specification specification(greentea_setup, cases); |
leothedragon | 0:8f0bb79ddd48 | 156 | |
leothedragon | 0:8f0bb79ddd48 | 157 | int main() { |
leothedragon | 0:8f0bb79ddd48 | 158 | //Create a thread to blink an LED and signal that the device is alive |
leothedragon | 0:8f0bb79ddd48 | 159 | #if !defined(MBED_CONF_APP_NO_LED) |
leothedragon | 0:8f0bb79ddd48 | 160 | Ticker t; |
leothedragon | 0:8f0bb79ddd48 | 161 | t.attach(led_thread, 0.5); |
leothedragon | 0:8f0bb79ddd48 | 162 | #endif |
leothedragon | 0:8f0bb79ddd48 | 163 | |
leothedragon | 0:8f0bb79ddd48 | 164 | return !Harness::run(specification); |
leothedragon | 0:8f0bb79ddd48 | 165 | } |