Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef MBED_CONF_APP_CONNECT_STATEMENT
00019     #error [NOT_SUPPORTED] No network configuration found for this target.
00020 #endif
00021 
00022 #include "mbed.h"
00023 #include MBED_CONF_APP_HEADER_FILE
00024 #include "UDPSocket.h"
00025 #include "greentea-client/test_env.h"
00026 #include "unity/unity.h"
00027 #include "utest.h"
00028 
00029 using namespace utest::v1;
00030 
00031 
00032 #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE
00033 #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 64
00034 #endif
00035 
00036 #ifndef MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT
00037 #define MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT 500
00038 #endif
00039 
00040 namespace {
00041     char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
00042     char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
00043     const char ASCII_MAX = '~' - ' ';
00044     const int ECHO_LOOPS = 16;
00045     char uuid[48] = {0};
00046 }
00047 
00048 void prep_buffer(char *uuid, char *tx_buffer, size_t tx_size) {
00049     size_t i = 0;
00050 
00051     memcpy(tx_buffer, uuid, strlen(uuid));
00052     i += strlen(uuid);
00053 
00054     tx_buffer[i++] = ' ';
00055 
00056     for (; i<tx_size; ++i) {
00057         tx_buffer[i] = (rand() % 10) + '0';
00058     }
00059 }
00060 
00061 void test_udp_echo() {
00062 
00063     NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00064     int err =  MBED_CONF_APP_CONNECT_STATEMENT;
00065 
00066     TEST_ASSERT_EQUAL(0, err);
00067 
00068     if (err) {
00069         printf("MBED: failed to connect with an error of %d\r\n", err);
00070         TEST_ASSERT_EQUAL(0, err);
00071     }
00072 
00073     printf("UDP client IP Address is %s\n", net->get_ip_address());
00074 
00075     UDPSocket sock;
00076     sock.open(net);
00077     sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
00078 
00079     SocketAddress udp_addr(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT);
00080 
00081     int success = 0;
00082     for (int i = 0; success < ECHO_LOOPS; i++) {
00083         prep_buffer(uuid, tx_buffer, sizeof(tx_buffer));
00084         const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
00085         if (ret >= 0) {
00086             printf("[%02d] sent %d bytes - %.*s  \n", i, ret, ret, tx_buffer);
00087         } else {
00088             printf("[%02d] Network error %d\n", i, ret);
00089             continue;
00090         }
00091 
00092         SocketAddress temp_addr;
00093         const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
00094         if (n >= 0) {
00095             printf("[%02d] recv %d bytes - %.*s  \n", i, n, n, rx_buffer);
00096         } else {
00097             printf("[%02d] Network error %d\n", i, n);
00098             continue;
00099         }
00100 
00101         if ((temp_addr == udp_addr &&
00102              n == sizeof(tx_buffer) &&
00103              memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
00104             success += 1;
00105 
00106             printf("[%02d] success #%d\n", i, success);
00107             continue;
00108         }
00109 
00110         // failed, clean out any remaining bad packets
00111         sock.set_timeout(0);
00112         while (true) {
00113             err = sock.recvfrom(NULL, NULL, 0);
00114             if (err == NSAPI_ERROR_WOULD_BLOCK ) {
00115                 break;
00116             }
00117         }
00118         sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
00119     }
00120 
00121     sock.close();
00122     net->disconnect();
00123     TEST_ASSERT_EQUAL(ECHO_LOOPS, success);
00124 }
00125 
00126 
00127 // Test setup
00128 utest::v1::status_t test_setup(const size_t number_of_cases) {
00129     GREENTEA_SETUP(240, "udp_echo");
00130 
00131     // create mac address based on uuid
00132     uint64_t mac = 0;
00133     for (int i = 0; i < sizeof(uuid); i++) {
00134         mac += uuid[i];
00135     }
00136     //mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
00137 
00138     return verbose_test_setup_handler(number_of_cases);
00139 }
00140 
00141 Case cases[] = {
00142     Case("UDP echo", test_udp_echo),
00143 };
00144 
00145 Specification specification(test_setup, cases);
00146 
00147 int main() {
00148     return !Harness::run(specification);
00149 }