leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2018 ARM Limited
00004  *
00005  * SPDX-License-Identifier: Apache-2.0
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 /*
00021  * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
00022 */
00023 
00024 #include "mbed.h"
00025 #include "utest/utest.h"
00026 #include "unity/unity.h"
00027 #include "greentea-client/test_env.h"
00028 #include "common_defines_test.h"
00029 #include "download_test.h"
00030 #include <string>
00031 
00032 #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME
00033   #include MBED_CONF_APP_BASICS_TEST_FILENAME
00034 #else
00035   #include "alice.h"
00036 #endif
00037 
00038 #ifndef MBED_CONF_APP_TESTS_FS_SIZE
00039   #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024)
00040 #endif
00041 
00042 using namespace utest::v1;
00043 
00044 #if !defined(MBED_CONF_APP_NO_LED)
00045 DigitalOut led1(LED1);
00046 DigitalOut led2(LED2);
00047 void led_thread() {
00048     led1 = !led1;
00049     led2 = !led1;
00050 }
00051 #endif
00052 
00053 #define MAX_RETRIES 3
00054 NetworkInterface* net = NULL;
00055 static control_t setup_network(const size_t call_count) {
00056     net = NetworkInterface::get_default_instance();
00057     TEST_ASSERT_NOT_NULL_MESSAGE(net, "failed to initialize network");
00058 
00059     nsapi_error_t err = -1;
00060     for (int tries = 0; tries < MAX_RETRIES; tries++) {
00061         err = net->connect();
00062         if (err == NSAPI_ERROR_OK) {
00063             break;
00064         } else {
00065             printf("[ERROR] Connecting to network. Retrying %d of %d.\r\n", tries, MAX_RETRIES);
00066         }
00067     }
00068     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
00069     printf("[NET] IP address is '%s'\n", net->get_ip_address());
00070     printf("[NET] MAC address is '%s'\n", net->get_mac_address());
00071     return CaseNext;
00072 }
00073 
00074 static uint32_t thread_counter = 0;
00075 void download_fn() {
00076     uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
00077     download_test(net, story, sizeof(story), 1024, thread_id);
00078 }
00079 
00080 static control_t download_1_thread(const size_t call_count) {
00081     thread_counter = 0;
00082  
00083     Thread t1;
00084     t1.start(download_fn);
00085     t1.join();
00086  
00087     return CaseNext;
00088 }
00089 
00090 static control_t download_2_threads(const size_t call_count) {
00091     thread_counter = 0;
00092 
00093     Thread t1;
00094     Thread t2;
00095     t1.start(download_fn);
00096     wait(0.5);
00097     t2.start(download_fn);
00098     t2.join();
00099     t1.join();
00100  
00101     return CaseNext;
00102 }
00103 
00104 static control_t download_3_threads(const size_t call_count) {
00105     thread_counter = 0;
00106 
00107     Thread t1;
00108     Thread t2;
00109     Thread t3;
00110     t1.start(download_fn);
00111     t2.start(download_fn);
00112     t3.start(download_fn);
00113     t1.join();
00114     t2.join();
00115     t3.join();
00116  
00117     return CaseNext;
00118 }
00119 
00120 static control_t download_4_threads(const size_t call_count) {
00121     thread_counter = 0;
00122 
00123     Thread t1;
00124     Thread t2;
00125     Thread t3;
00126     Thread t4;
00127     t1.start(download_fn);
00128     t2.start(download_fn);
00129     t3.start(download_fn);
00130     t4.start(download_fn);
00131     t1.join();
00132     t2.join();
00133     t3.join();
00134     t4.join();
00135  
00136     return CaseNext;
00137 }
00138 
00139 utest::v1::status_t greentea_setup(const size_t number_of_cases) {
00140     GREENTEA_SETUP(10*60, "default_auto");
00141     return greentea_test_setup_handler(number_of_cases);
00142 }
00143 
00144 Case cases[] = {
00145     Case(TEST_NETWORK_TYPE " network setup", setup_network),
00146     Case(TEST_NETWORK_TYPE " 1 thread", download_1_thread),
00147 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR
00148     Case(TEST_NETWORK_TYPE " 2 threads", download_2_threads),
00149 #endif
00150     //Case(TEST_NETWORK_TYPE " 3 threads", download_3_threads),
00151     // 4 threads may fail due to http host limits
00152     //Case(TEST_NETWORK_TYPE " 4 threads", download_4_threads),
00153 };
00154 
00155 Specification specification(greentea_setup, cases);
00156 
00157 int main() {
00158     //Create a thread to blink an LED and signal that the device is alive
00159 #if !defined(MBED_CONF_APP_NO_LED)
00160     Ticker t;
00161     t.attach(led_thread, 0.5);
00162 #endif
00163 
00164     return !Harness::run(specification);
00165 }