Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers emac_test_memory.cpp Source File

emac_test_memory.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 #include "mbed.h"
00019 #include "greentea-client/test_env.h"
00020 #include "unity.h"
00021 #include "utest.h"
00022 
00023 #if MBED_CONF_APP_TEST_WIFI || MBED_CONF_APP_TEST_ETHERNET
00024 
00025 #include "emac_tests.h"
00026 #include "emac_util.h"
00027 #include "emac_ctp.h"
00028 
00029 using namespace utest::v1;
00030 
00031 void test_emac_memory_cb(int opt)
00032 {
00033     static bool send_request = true;
00034     static bool memory = true;
00035     static int no_response_cnt = 0;
00036     static int retries = 0;
00037     static int msg_len = 0;
00038     static int test_step = 0;
00039     static bool next_step = true;
00040     static int options = CTP_OPT_NON_ALIGNED;
00041 
00042     // Defines test step
00043     if (next_step) {
00044         next_step = false;
00045         switch (test_step) {
00046             case 0:
00047                 printf("STEP 0: memory available\r\n\r\n");
00048                 emac_if_set_output_memory(true);
00049                 emac_if_set_input_memory(true);
00050                 memory = true;
00051                 break;
00052 
00053             case 1:
00054                 printf("STEP 1: no input memory buffer memory available\r\n\r\n");
00055                 emac_if_set_output_memory(true);
00056                 emac_if_set_input_memory(false);
00057                 memory = false;
00058                 break;
00059 
00060             case 2:
00061                 printf("STEP 2: memory available\r\n\r\n");
00062                 emac_if_set_output_memory(true);
00063                 emac_if_set_input_memory(true);
00064                 memory = true;
00065                 break;
00066 
00067             case 3:
00068                 printf("STEP 3: no output memory buffer memory available\r\n\r\n");
00069                 emac_if_set_output_memory(false);
00070                 emac_if_set_input_memory(true);
00071                 memory = false;
00072                 break;
00073 
00074             case 4:
00075                 printf("STEP 4: memory available\r\n\r\n");
00076                 emac_if_set_output_memory(true);
00077                 emac_if_set_input_memory(true);
00078                 memory = true;
00079                 break;
00080 
00081             case 5:
00082                 printf("STEP 5: no output or input memory buffer memory available\r\n\r\n");
00083                 emac_if_set_output_memory(false);
00084                 emac_if_set_input_memory(false);
00085                 memory = false;
00086                 break;
00087 
00088             case 6:
00089                  printf("STEP 6: memory available\r\n\r\n");
00090                  emac_if_set_output_memory(true);
00091                  emac_if_set_input_memory(true);
00092                  memory = true;
00093                  break;
00094 
00095             case 7:
00096                  printf("STEP 7: memory available, alloc from heap\r\n\r\n");
00097                  emac_if_set_output_memory(true);
00098                  emac_if_set_input_memory(true);
00099                  options |= CTP_OPT_HEAP;
00100                  memory = true;
00101                  break;
00102 
00103             case 8:
00104                 // Test ended
00105                 END_TEST_LOOP;
00106         }
00107     }
00108 
00109     bool next_len = false;
00110 
00111     // Timeout
00112     if (opt == TIMEOUT && send_request) {
00113         CTP_MSG_SEND(msg_len, emac_if_get_echo_server_addr(0), emac_if_get_own_addr(), emac_if_get_own_addr(), options);
00114         send_request = false;
00115         no_response_cnt = 0;
00116     } else if (opt == TIMEOUT) {
00117         if (++no_response_cnt > 5) {
00118             if (++retries > 3) {
00119                 // If echo replies should be received fails the test case
00120                 if (memory) {
00121                     printf("too many retries\r\n\r\n");
00122                     SET_ERROR_FLAGS(TEST_FAILED);
00123                     END_TEST_LOOP;
00124                 // Otherwise continues test
00125                 } else {
00126                     RESET_OUTGOING_MSG_DATA;
00127                     next_len = true;
00128                 }
00129             } else {
00130                 printf("retry count %i\r\n\r\n", retries);
00131                 send_request = true;
00132             }
00133         }
00134     }
00135 
00136     // Echo response received or retry count exceeded for memory buffers are not available
00137     if (opt == INPUT || next_len) {
00138         if (msg_len == ETH_MAX_LEN) {
00139             msg_len = 0;
00140             test_step++;
00141             next_step = true;
00142         } else if (msg_len + 50 >= ETH_MAX_LEN) {
00143             msg_len = ETH_MAX_LEN;
00144         } else {
00145             msg_len += 50;
00146         }
00147         printf("message length %i\r\n\r\n", msg_len);
00148         retries = 0;
00149         send_request = true;
00150     }
00151 }
00152 
00153 void test_emac_memory()
00154 {
00155     RESET_ALL_ERROR_FLAGS;
00156     SET_TRACE_LEVEL(TRACE_SEND | TRACE_SUCCESS | TRACE_FAILURE);
00157 
00158     if (ECHO_SERVER_ADDRESS_KNOWN) {
00159         START_TEST_LOOP(test_emac_memory_cb, 100);
00160     }
00161 
00162     PRINT_ERROR_FLAGS;
00163     TEST_ASSERT_FALSE(ERROR_FLAGS);
00164     RESET_OUTGOING_MSG_DATA;
00165 }
00166 
00167 #endif