Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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 "FATFileSystem.h"
00026 #include "LittleFileSystem.h"
00027 #include "utest/utest.h"
00028 #include "unity/unity.h"
00029 #include "greentea-client/test_env.h"
00030 #include "common_defines_test.h"
00031 #include "download_test.h"
00032 #include "file_test.h"
00033 #include <string>
00034 
00035 #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME
00036   #include MBED_CONF_APP_BASICS_TEST_FILENAME
00037 #else
00038   #include "alice.h"
00039 #endif
00040 
00041 #ifndef MBED_CONF_APP_TESTS_FS_SIZE
00042   #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024)
00043 #endif
00044 
00045 using namespace utest::v1;
00046 
00047 #if !defined(MBED_CONF_APP_NO_LED)
00048 DigitalOut led1(LED1);
00049 DigitalOut led2(LED2);
00050 void led_thread() {
00051     led1 = !led1;
00052     led2 = !led1;
00053 }
00054 #endif
00055 
00056 #define MAX_RETRIES 3
00057 NetworkInterface* interface = NULL;
00058 
00059 static control_t setup_network(const size_t call_count) {
00060     interface = NetworkInterface::get_default_instance();
00061     TEST_ASSERT_NOT_NULL_MESSAGE(interface, "failed to initialize network");
00062 
00063     nsapi_error_t err = -1;
00064     for (int tries = 0; tries < MAX_RETRIES; tries++) {
00065         err = interface->connect();
00066         if (err == NSAPI_ERROR_OK) {
00067             break;
00068         } else {
00069             printf("[ERROR] Connecting to network. Retrying %d of %d...\r\n", tries, MAX_RETRIES);
00070         }
00071     }
00072     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
00073     printf("[NET] IP address is '%s'\n", interface->get_ip_address());
00074     printf("[NET] MAC address is '%s'\n", interface->get_mac_address());
00075     return CaseNext;
00076 }
00077 
00078 BlockDevice* bd = BlockDevice::get_default_instance();
00079 SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
00080 #if TEST_USE_FILESYSTEM == FS_FAT
00081 FATFileSystem fs("sd");
00082 #else
00083 LittleFileSystem fs("sd");
00084 #endif
00085 
00086 static control_t test_format(const size_t call_count) {
00087     int format_err = fs.format(&sd);
00088     TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
00089 
00090     int mount_err = fs.mount(&sd);
00091     TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
00092 
00093     return CaseNext;
00094 }
00095 
00096 static uint32_t thread_counter = 0;
00097 
00098 void download_fn() {
00099     uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
00100     download_test(interface, story, sizeof(story), 256, thread_id);
00101 }
00102 void file_fn(size_t buffer) {
00103     uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
00104     char filename[255] = { 0 };
00105     snprintf(filename, 255, "mbed-file-test-%d.txt", thread_id);
00106     file_test_write(filename, 0, story, sizeof(story), buffer);
00107     file_test_read(filename, 0, story, sizeof(story), buffer);
00108 }
00109 void file_1b_fn() { return file_fn(1); }
00110 void file_4b_fn() { return file_fn(4); }
00111 void file_256b_fn() { return file_fn(256); }
00112 void file_1kb_fn() { return file_fn(1024); }
00113 
00114 static control_t stress_1_thread(const size_t call_count) {
00115     thread_counter = 0;
00116  
00117     Thread t1;
00118     t1.start(download_fn);
00119     t1.join();
00120     t1.start(file_1kb_fn);
00121     t1.join();
00122  
00123     return CaseNext;
00124 }
00125 
00126 static control_t stress_2_threads(const size_t call_count) {
00127     thread_counter = 0;
00128 
00129     Thread t1;
00130     Thread t2;
00131     t1.start(file_1kb_fn);
00132     wait(1);
00133     t2.start(download_fn);
00134     t2.join();
00135     t1.join();
00136  
00137     return CaseNext;
00138 }
00139 
00140 static control_t stress_3_threads(const size_t call_count) {
00141     thread_counter = 0;
00142 
00143     Thread t1;
00144     Thread t2;
00145     Thread t3;
00146     t1.start(file_256b_fn);
00147     t2.start(file_1kb_fn);
00148     wait(1);
00149     t3.start(download_fn);
00150     t3.join();
00151     t2.join();
00152     t1.join();
00153  
00154     return CaseNext;
00155 }
00156 
00157 static control_t stress_4_threads(const size_t call_count) {
00158     thread_counter = 0;
00159 
00160     Thread t1;
00161     Thread t2;
00162     Thread t3;
00163     Thread t4;
00164     t1.start(file_256b_fn);
00165     t2.start(file_256b_fn);
00166     t3.start(file_256b_fn);
00167     wait(1);
00168     t4.start(download_fn);
00169     t4.join();
00170     t3.join();
00171     t2.join();
00172     t1.join();
00173  
00174     return CaseNext;
00175 }
00176 
00177 template <uint32_t size>
00178 void test_malloc(){
00179  
00180     void *bufferTest = NULL;
00181     TEST_ASSERT_MESSAGE(size > 0, "Size must not be zero for test");
00182     printf("Allocating %d bytes",(int)size);
00183     bufferTest = malloc(size);
00184     TEST_ASSERT(bufferTest !=NULL);
00185     free(bufferTest);
00186 }
00187 
00188 utest::v1::status_t greentea_setup(const size_t number_of_cases) {
00189     GREENTEA_SETUP(10*60, "default_auto");
00190     return greentea_test_setup_handler(number_of_cases);
00191 }
00192 
00193 Case cases[] = {
00194     Case(TEST_NETWORK_TYPE " network setup", setup_network),
00195     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " format", test_format),
00196     Case("Test memory allocation of 10 K bytes", test_malloc<TEST_MEMORY_SIZE_10K>),
00197     Case("Test memory allocation of 20 K bytes", test_malloc<TEST_MEMORY_SIZE_20K>),
00198     Case("Test memory allocation of 40 K bytes", test_malloc<TEST_MEMORY_SIZE_40K>),
00199     Case("Test memory allocation of 60 K bytes", test_malloc<TEST_MEMORY_SIZE_60K>),
00200 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR
00201     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 1 thread, dl, file seq.", stress_1_thread),
00202     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 2 threads, dl, 1kb", stress_2_threads),
00203 #endif
00204     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 3 threads, dl, 256b, 1kb", stress_3_threads),
00205     //Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 4 threads, dl, 256b, 256b, 256b", stress_4_threads),
00206 };
00207 
00208 Specification specification(greentea_setup, cases);
00209 
00210 int main() {
00211     //Create a thread to blink an LED and signal that the device is alive
00212 #if !defined(MBED_CONF_APP_NO_LED)
00213     Ticker t;
00214     t.attach(led_thread, 0.5);
00215 #endif
00216 
00217     return !Harness::run(specification);
00218 }