takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers emac_test_broadcast.cpp Source File

emac_test_broadcast.cpp

00001 /*
00002  * Copyright (c) 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 #include "mbed.h"
00019 #include "greentea-client/test_env.h"
00020 #include "unity.h"
00021 #include "utest.h"
00022 
00023 #include "emac_tests.h"
00024 #include "emac_util.h"
00025 #include "emac_ctp.h"
00026 
00027 using namespace utest::v1;
00028 
00029 void test_emac_broadcast_cb(int opt)
00030 {
00031     static bool send_request = true;
00032     static int no_response_cnt = 0;
00033     static int retries = 0;
00034     static int test_step = 0;
00035     static int msg_len = 100;
00036     static int wait = 0;
00037 
00038     if (wait) {
00039         --wait;
00040         return;
00041     }
00042 
00043 #if MBED_CONF_APP_ECHO_SERVER
00044     static bool echo_server_started = false;
00045     if (!echo_server_started) {
00046 #if MBED_CONF_APP_ECHO_SERVER_TRACE == 0
00047         SET_TRACE_LEVEL(TRACE_NONE);
00048 #endif
00049         printf("echo server started successfully\r\n\r\n");
00050         echo_server_started = true;
00051     } else {
00052         // Sends broadcast every 60 seconds
00053         CTP_MSG_SEND(msg_len, eth_mac_broadcast_addr, emac_if_get_own_addr(), emac_if_get_own_addr(), 0);
00054         wait = 60;
00055     }
00056     return;
00057 #endif
00058 
00059     // Timeout
00060     if (opt == TIMEOUT && send_request) {
00061         CTP_MSG_SEND(msg_len, eth_mac_broadcast_addr, emac_if_get_own_addr(), emac_if_get_own_addr(), 0);
00062         send_request = false;
00063         no_response_cnt = 0;
00064     } else if (opt == TIMEOUT) {
00065         if (++no_response_cnt > 3) {
00066             if (++retries > 6) {
00067                 printf("too many retries\r\n\r\n");
00068                 END_TEST_LOOP;
00069             } else {
00070                 printf("retry count %i\r\n\r\n", retries);
00071                 send_request = true;
00072             }
00073         }
00074     }
00075 
00076     // Echo response received
00077     if (opt == INPUT) {
00078         ++test_step;
00079         if (test_step == 3) {
00080             msg_len = 60;
00081             wait = 3;
00082         } else if (test_step == 6) {
00083             END_TEST_LOOP;
00084         }
00085         retries = 0;
00086         send_request = true;
00087     }
00088 }
00089 
00090 void test_emac_broadcast(void)
00091 {
00092     RESET_ALL_ERROR_FLAGS;
00093     SET_TRACE_LEVEL(TRACE_SEND | TRACE_ETH_FRAMES | TRACE_SUCCESS | TRACE_FAILURE);
00094 
00095     START_TEST_LOOP(test_emac_broadcast_cb, 1 * SECOND_TO_MS);
00096 
00097     PRINT_ERROR_FLAGS;
00098     TEST_ASSERT_FALSE(ERROR_FLAGS);
00099     RESET_OUTGOING_MSG_DATA;
00100 }
00101