takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2018, 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 #define WIFI 2
00019 #if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
00020     (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
00021 #error [NOT_SUPPORTED] No network configuration found for this target.
00022 #endif
00023 #ifndef MBED_CONF_APP_ECHO_SERVER_ADDR
00024 #error [NOT_SUPPORTED] Requires parameters from mbed_app.json
00025 #endif
00026 
00027 #include "mbed.h"
00028 #include "greentea-client/test_env.h"
00029 #include "unity/unity.h"
00030 #include "utest.h"
00031 #include "utest/utest_stack_trace.h"
00032 #include "udp_tests.h"
00033 
00034 using namespace utest::v1;
00035 
00036 namespace {
00037 NetworkInterface *net;
00038 }
00039 
00040 NetworkInterface *get_interface()
00041 {
00042     return net;
00043 }
00044 
00045 static void _ifup()
00046 {
00047     net = NetworkInterface::get_default_instance();
00048     nsapi_error_t err = net->connect();
00049     TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err);
00050     printf("MBED: UDPClient IP address is '%s'\n", net->get_ip_address());
00051 }
00052 
00053 static void _ifdown()
00054 {
00055     net->disconnect();
00056     printf("MBED: ifdown\n");
00057 }
00058 
00059 void drop_bad_packets(UDPSocket &sock, int orig_timeout)
00060 {
00061     nsapi_error_t err;
00062     sock.set_timeout(0);
00063     while (true) {
00064         err = sock.recvfrom(NULL, 0, 0);
00065         if (err == NSAPI_ERROR_WOULD_BLOCK ) {
00066             break;
00067         }
00068     }
00069     sock.set_timeout(orig_timeout);
00070 }
00071 
00072 void fill_tx_buffer_ascii(char *buff, size_t len)
00073 {
00074     for (size_t i = 0; i < len; ++i) {
00075         buff[i] = (rand() % 43) + '0';
00076     }
00077 }
00078 
00079 // Test setup
00080 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00081 {
00082     GREENTEA_SETUP(480, "default_auto");
00083     _ifup();
00084     return greentea_test_setup_handler(number_of_cases);
00085 }
00086 
00087 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00088 {
00089     _ifdown();
00090     return greentea_test_teardown_handler(passed, failed, failure);
00091 }
00092 
00093 Case cases[] = {
00094     Case("UDPSOCKET_ECHOTEST_NONBLOCK", UDPSOCKET_ECHOTEST_NONBLOCK),
00095     Case("UDPSOCKET_OPEN_CLOSE_REPEAT", UDPSOCKET_OPEN_CLOSE_REPEAT),
00096     Case("UDPSOCKET_OPEN_LIMIT", UDPSOCKET_OPEN_LIMIT),
00097     Case("UDPSOCKET_SENDTO_TIMEOUT", UDPSOCKET_SENDTO_TIMEOUT),
00098 #ifdef MBED_EXTENDED_TESTS
00099     Case("UDPSOCKET_SENDTO_INVALID", UDPSOCKET_SENDTO_INVALID),
00100     Case("UDPSOCKET_ECHOTEST", UDPSOCKET_ECHOTEST),
00101     Case("UDPSOCKET_ECHOTEST_BURST", UDPSOCKET_ECHOTEST_BURST),
00102     Case("UDPSOCKET_ECHOTEST_BURST_NONBLOCK", UDPSOCKET_ECHOTEST_BURST_NONBLOCK),
00103     Case("UDPSOCKET_SENDTO_REPEAT", UDPSOCKET_SENDTO_REPEAT),
00104 #endif
00105 };
00106 
00107 Specification specification(greentea_setup, cases, greentea_teardown);
00108 
00109 int main()
00110 {
00111     return !Harness::run(specification);
00112 }