Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
marcozecchini 0:9fca2b23d0ba 3 * SPDX-License-Identifier: Apache-2.0
marcozecchini 0:9fca2b23d0ba 4 *
marcozecchini 0:9fca2b23d0ba 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
marcozecchini 0:9fca2b23d0ba 6 * not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 7 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 8 *
marcozecchini 0:9fca2b23d0ba 9 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
marcozecchini 0:9fca2b23d0ba 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 14 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 15 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 16 */
marcozecchini 0:9fca2b23d0ba 17
marcozecchini 0:9fca2b23d0ba 18 #ifndef MBED_CONF_APP_CONNECT_STATEMENT
marcozecchini 0:9fca2b23d0ba 19 #error [NOT_SUPPORTED] No network configuration found for this target.
marcozecchini 0:9fca2b23d0ba 20 #endif
marcozecchini 0:9fca2b23d0ba 21
marcozecchini 0:9fca2b23d0ba 22 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 23 #include MBED_CONF_APP_HEADER_FILE
marcozecchini 0:9fca2b23d0ba 24 #include "UDPSocket.h"
marcozecchini 0:9fca2b23d0ba 25 #include "greentea-client/test_env.h"
marcozecchini 0:9fca2b23d0ba 26 #include "unity/unity.h"
marcozecchini 0:9fca2b23d0ba 27 #include "utest.h"
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 using namespace utest::v1;
marcozecchini 0:9fca2b23d0ba 30
marcozecchini 0:9fca2b23d0ba 31
marcozecchini 0:9fca2b23d0ba 32 #ifndef MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE
marcozecchini 0:9fca2b23d0ba 33 #define MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE 64
marcozecchini 0:9fca2b23d0ba 34 #endif
marcozecchini 0:9fca2b23d0ba 35
marcozecchini 0:9fca2b23d0ba 36 #ifndef MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT
marcozecchini 0:9fca2b23d0ba 37 #define MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT 500
marcozecchini 0:9fca2b23d0ba 38 #endif
marcozecchini 0:9fca2b23d0ba 39
marcozecchini 0:9fca2b23d0ba 40 namespace {
marcozecchini 0:9fca2b23d0ba 41 char tx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
marcozecchini 0:9fca2b23d0ba 42 char rx_buffer[MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE] = {0};
marcozecchini 0:9fca2b23d0ba 43 const char ASCII_MAX = '~' - ' ';
marcozecchini 0:9fca2b23d0ba 44 const int ECHO_LOOPS = 16;
marcozecchini 0:9fca2b23d0ba 45 char uuid[48] = {0};
marcozecchini 0:9fca2b23d0ba 46 }
marcozecchini 0:9fca2b23d0ba 47
marcozecchini 0:9fca2b23d0ba 48 void prep_buffer(char *uuid, char *tx_buffer, size_t tx_size) {
marcozecchini 0:9fca2b23d0ba 49 size_t i = 0;
marcozecchini 0:9fca2b23d0ba 50
marcozecchini 0:9fca2b23d0ba 51 memcpy(tx_buffer, uuid, strlen(uuid));
marcozecchini 0:9fca2b23d0ba 52 i += strlen(uuid);
marcozecchini 0:9fca2b23d0ba 53
marcozecchini 0:9fca2b23d0ba 54 tx_buffer[i++] = ' ';
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 for (; i<tx_size; ++i) {
marcozecchini 0:9fca2b23d0ba 57 tx_buffer[i] = (rand() % 10) + '0';
marcozecchini 0:9fca2b23d0ba 58 }
marcozecchini 0:9fca2b23d0ba 59 }
marcozecchini 0:9fca2b23d0ba 60
marcozecchini 0:9fca2b23d0ba 61 void test_udp_echo() {
marcozecchini 0:9fca2b23d0ba 62
marcozecchini 0:9fca2b23d0ba 63 NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
marcozecchini 0:9fca2b23d0ba 64 int err = MBED_CONF_APP_CONNECT_STATEMENT;
marcozecchini 0:9fca2b23d0ba 65
marcozecchini 0:9fca2b23d0ba 66 TEST_ASSERT_EQUAL(0, err);
marcozecchini 0:9fca2b23d0ba 67
marcozecchini 0:9fca2b23d0ba 68 if (err) {
marcozecchini 0:9fca2b23d0ba 69 printf("MBED: failed to connect with an error of %d\r\n", err);
marcozecchini 0:9fca2b23d0ba 70 TEST_ASSERT_EQUAL(0, err);
marcozecchini 0:9fca2b23d0ba 71 }
marcozecchini 0:9fca2b23d0ba 72
marcozecchini 0:9fca2b23d0ba 73 printf("UDP client IP Address is %s\n", net->get_ip_address());
marcozecchini 0:9fca2b23d0ba 74
marcozecchini 0:9fca2b23d0ba 75 UDPSocket sock;
marcozecchini 0:9fca2b23d0ba 76 sock.open(net);
marcozecchini 0:9fca2b23d0ba 77 sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 SocketAddress udp_addr(MBED_CONF_APP_ECHO_SERVER_ADDR, MBED_CONF_APP_ECHO_SERVER_PORT);
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 int success = 0;
marcozecchini 0:9fca2b23d0ba 82 for (int i = 0; success < ECHO_LOOPS; i++) {
marcozecchini 0:9fca2b23d0ba 83 prep_buffer(uuid, tx_buffer, sizeof(tx_buffer));
marcozecchini 0:9fca2b23d0ba 84 const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
marcozecchini 0:9fca2b23d0ba 85 if (ret >= 0) {
marcozecchini 0:9fca2b23d0ba 86 printf("[%02d] sent %d bytes - %.*s \n", i, ret, ret, tx_buffer);
marcozecchini 0:9fca2b23d0ba 87 } else {
marcozecchini 0:9fca2b23d0ba 88 printf("[%02d] Network error %d\n", i, ret);
marcozecchini 0:9fca2b23d0ba 89 continue;
marcozecchini 0:9fca2b23d0ba 90 }
marcozecchini 0:9fca2b23d0ba 91
marcozecchini 0:9fca2b23d0ba 92 SocketAddress temp_addr;
marcozecchini 0:9fca2b23d0ba 93 const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
marcozecchini 0:9fca2b23d0ba 94 if (n >= 0) {
marcozecchini 0:9fca2b23d0ba 95 printf("[%02d] recv %d bytes - %.*s \n", i, n, n, rx_buffer);
marcozecchini 0:9fca2b23d0ba 96 } else {
marcozecchini 0:9fca2b23d0ba 97 printf("[%02d] Network error %d\n", i, n);
marcozecchini 0:9fca2b23d0ba 98 continue;
marcozecchini 0:9fca2b23d0ba 99 }
marcozecchini 0:9fca2b23d0ba 100
marcozecchini 0:9fca2b23d0ba 101 if ((temp_addr == udp_addr &&
marcozecchini 0:9fca2b23d0ba 102 n == sizeof(tx_buffer) &&
marcozecchini 0:9fca2b23d0ba 103 memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
marcozecchini 0:9fca2b23d0ba 104 success += 1;
marcozecchini 0:9fca2b23d0ba 105
marcozecchini 0:9fca2b23d0ba 106 printf("[%02d] success #%d\n", i, success);
marcozecchini 0:9fca2b23d0ba 107 continue;
marcozecchini 0:9fca2b23d0ba 108 }
marcozecchini 0:9fca2b23d0ba 109
marcozecchini 0:9fca2b23d0ba 110 // failed, clean out any remaining bad packets
marcozecchini 0:9fca2b23d0ba 111 sock.set_timeout(0);
marcozecchini 0:9fca2b23d0ba 112 while (true) {
marcozecchini 0:9fca2b23d0ba 113 err = sock.recvfrom(NULL, NULL, 0);
marcozecchini 0:9fca2b23d0ba 114 if (err == NSAPI_ERROR_WOULD_BLOCK) {
marcozecchini 0:9fca2b23d0ba 115 break;
marcozecchini 0:9fca2b23d0ba 116 }
marcozecchini 0:9fca2b23d0ba 117 }
marcozecchini 0:9fca2b23d0ba 118 sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
marcozecchini 0:9fca2b23d0ba 119 }
marcozecchini 0:9fca2b23d0ba 120
marcozecchini 0:9fca2b23d0ba 121 sock.close();
marcozecchini 0:9fca2b23d0ba 122 net->disconnect();
marcozecchini 0:9fca2b23d0ba 123 TEST_ASSERT_EQUAL(ECHO_LOOPS, success);
marcozecchini 0:9fca2b23d0ba 124 }
marcozecchini 0:9fca2b23d0ba 125
marcozecchini 0:9fca2b23d0ba 126
marcozecchini 0:9fca2b23d0ba 127 // Test setup
marcozecchini 0:9fca2b23d0ba 128 utest::v1::status_t test_setup(const size_t number_of_cases) {
marcozecchini 0:9fca2b23d0ba 129 GREENTEA_SETUP(240, "udp_echo");
marcozecchini 0:9fca2b23d0ba 130
marcozecchini 0:9fca2b23d0ba 131 // create mac address based on uuid
marcozecchini 0:9fca2b23d0ba 132 uint64_t mac = 0;
marcozecchini 0:9fca2b23d0ba 133 for (int i = 0; i < sizeof(uuid); i++) {
marcozecchini 0:9fca2b23d0ba 134 mac += uuid[i];
marcozecchini 0:9fca2b23d0ba 135 }
marcozecchini 0:9fca2b23d0ba 136 //mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1);
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 return verbose_test_setup_handler(number_of_cases);
marcozecchini 0:9fca2b23d0ba 139 }
marcozecchini 0:9fca2b23d0ba 140
marcozecchini 0:9fca2b23d0ba 141 Case cases[] = {
marcozecchini 0:9fca2b23d0ba 142 Case("UDP echo", test_udp_echo),
marcozecchini 0:9fca2b23d0ba 143 };
marcozecchini 0:9fca2b23d0ba 144
marcozecchini 0:9fca2b23d0ba 145 Specification specification(test_setup, cases);
marcozecchini 0:9fca2b23d0ba 146
marcozecchini 0:9fca2b23d0ba 147 int main() {
marcozecchini 0:9fca2b23d0ba 148 return !Harness::run(specification);
marcozecchini 0:9fca2b23d0ba 149 }