takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers networkinterface_status.cpp Source File

networkinterface_status.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 "networkinterface_tests.h"
00021 #include "unity/unity.h"
00022 #include "utest.h"
00023 
00024 using namespace utest::v1;
00025 
00026 namespace
00027 {
00028     NetworkInterface* net;
00029     rtos::Semaphore status_semaphore;
00030     int status_write_counter = 0;
00031     int status_read_counter = 0;
00032     const int repeats = 5;
00033     const int status_buffer_size = 100;
00034     nsapi_connection_status_t current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
00035     nsapi_connection_status_t statuses[status_buffer_size];
00036 }
00037 
00038 void status_cb(nsapi_event_t event, intptr_t value)
00039 {
00040     TEST_ASSERT_EQUAL(NSAPI_EVENT_CONNECTION_STATUS_CHANGE , event);
00041 
00042     statuses[status_write_counter] = static_cast<nsapi_connection_status_t>(value);
00043     status_write_counter++;
00044     if (status_write_counter >= status_buffer_size) {
00045         TEST_ASSERT(0);
00046     }
00047 
00048     status_semaphore.release();
00049 }
00050 
00051 nsapi_connection_status_t wait_status_callback()
00052 {
00053     nsapi_connection_status_t status;
00054 
00055     while (true) {
00056         status_semaphore.wait();
00057 
00058         status = statuses[status_read_counter];
00059         status_read_counter++;
00060 
00061         if (status != current_status) {
00062             current_status = status;
00063             return status;
00064         }
00065     }
00066 }
00067 
00068 void NETWORKINTERFACE_STATUS()
00069 {
00070     nsapi_connection_status_t status;
00071 
00072     status_write_counter = 0;
00073     status_read_counter = 0;
00074     current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
00075 
00076     net = NetworkInterface::get_default_instance();
00077     net->attach(status_cb);
00078     net->set_blocking(true);
00079 
00080     for (int i = 0; i < repeats; i++) {
00081         nsapi_error_t err = net->connect();
00082         TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err);
00083 
00084         status = wait_status_callback();
00085         TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING , status);
00086 
00087         status = wait_status_callback();
00088         if (status == NSAPI_STATUS_LOCAL_UP ) {
00089             status = wait_status_callback();
00090         }
00091         TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP , status);
00092 
00093         net->disconnect();
00094 
00095         status = wait_status_callback();
00096         TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED , status);
00097     }
00098 
00099     net->attach(NULL);
00100 }
00101 
00102 void NETWORKINTERFACE_STATUS_NONBLOCK()
00103 {
00104     nsapi_connection_status_t status;
00105 
00106     status_write_counter = 0;
00107     status_read_counter = 0;
00108     current_status = NSAPI_STATUS_ERROR_UNSUPPORTED;
00109 
00110     net = NetworkInterface::get_default_instance();
00111     net->attach(status_cb);
00112     net->set_blocking(false);
00113 
00114     for (int i = 0; i < repeats; i++) {
00115         nsapi_error_t err = net->connect();
00116         TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err);
00117 
00118         status = wait_status_callback();
00119         TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING , status);
00120 
00121         status = wait_status_callback();
00122         if (status == NSAPI_STATUS_LOCAL_UP ) {
00123             status = wait_status_callback();
00124         }
00125         TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP , status);
00126 
00127         net->disconnect();
00128 
00129         status = wait_status_callback();
00130         TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED , status);
00131     }
00132 
00133     net->attach(NULL);
00134     net->set_blocking(true);
00135 }
00136 
00137 void NETWORKINTERFACE_STATUS_GET()
00138 {
00139     nsapi_connection_status_t status;
00140 
00141     net = NetworkInterface::get_default_instance();
00142     net->set_blocking(true);
00143 
00144     TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED , net->get_connection_status());
00145 
00146     for (int i = 0; i < repeats; i++) {
00147         nsapi_error_t err = net->connect();
00148         TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err);
00149 
00150         while (net->get_connection_status() != NSAPI_STATUS_GLOBAL_UP ) {
00151             wait(0.5);
00152         }
00153 
00154         net->disconnect();
00155 
00156         TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED , net->get_connection_status());
00157     }
00158 
00159     net->attach(NULL);
00160 }