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