|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, ARM Limited, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "mbed.h" |
| 19 | +#include "greentea-client/test_env.h" |
| 20 | +#include "networkinterface_tests.h" |
| 21 | +#include "unity/unity.h" |
| 22 | +#include "utest.h" |
| 23 | + |
| 24 | +using namespace utest::v1; |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | + NetworkInterface* net; |
| 29 | + rtos::Semaphore status_semaphore; |
| 30 | + int status_write_counter = 0; |
| 31 | + int status_read_counter = 0; |
| 32 | + const int repeats = 5; |
| 33 | + const int status_buffer_size = 100; |
| 34 | + nsapi_connection_status_t current_status = NSAPI_STATUS_ERROR_UNSUPPORTED; |
| 35 | + nsapi_connection_status_t statuses[status_buffer_size]; |
| 36 | +} |
| 37 | + |
| 38 | +void status_cb(nsapi_event_t event, intptr_t value) |
| 39 | +{ |
| 40 | + TEST_ASSERT_EQUAL(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, event); |
| 41 | + |
| 42 | + statuses[status_write_counter] = static_cast<nsapi_connection_status_t>(value); |
| 43 | + status_write_counter++; |
| 44 | + if (status_write_counter >= status_buffer_size) { |
| 45 | + TEST_ASSERT(0); |
| 46 | + } |
| 47 | + |
| 48 | + status_semaphore.release(); |
| 49 | +} |
| 50 | + |
| 51 | +nsapi_connection_status_t wait_status_callback() |
| 52 | +{ |
| 53 | + nsapi_connection_status_t status; |
| 54 | + |
| 55 | + while (true) { |
| 56 | + status_semaphore.wait(); |
| 57 | + |
| 58 | + status = statuses[status_read_counter]; |
| 59 | + status_read_counter++; |
| 60 | + |
| 61 | + if (status != current_status) { |
| 62 | + current_status = status; |
| 63 | + return status; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void NETWORKINTERFACE_STATUS() |
| 69 | +{ |
| 70 | + nsapi_connection_status_t status; |
| 71 | + |
| 72 | + status_write_counter = 0; |
| 73 | + status_read_counter = 0; |
| 74 | + current_status = NSAPI_STATUS_ERROR_UNSUPPORTED; |
| 75 | + |
| 76 | + net = NetworkInterface::get_default_instance(); |
| 77 | + net->attach(status_cb); |
| 78 | + net->set_blocking(true); |
| 79 | + |
| 80 | + for (int i = 0; i < repeats; i++) { |
| 81 | + nsapi_error_t err = net->connect(); |
| 82 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
| 83 | + |
| 84 | + status = wait_status_callback(); |
| 85 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING, status); |
| 86 | + |
| 87 | + status = wait_status_callback(); |
| 88 | + if (status == NSAPI_STATUS_LOCAL_UP) { |
| 89 | + status = wait_status_callback(); |
| 90 | + } |
| 91 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP, status); |
| 92 | + |
| 93 | + net->disconnect(); |
| 94 | + |
| 95 | + status = wait_status_callback(); |
| 96 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, status); |
| 97 | + } |
| 98 | + |
| 99 | + net->attach(NULL); |
| 100 | +} |
| 101 | + |
| 102 | +void NETWORKINTERFACE_STATUS_NONBLOCK() |
| 103 | +{ |
| 104 | + nsapi_connection_status_t status; |
| 105 | + |
| 106 | + status_write_counter = 0; |
| 107 | + status_read_counter = 0; |
| 108 | + current_status = NSAPI_STATUS_ERROR_UNSUPPORTED; |
| 109 | + |
| 110 | + net = NetworkInterface::get_default_instance(); |
| 111 | + net->attach(status_cb); |
| 112 | + net->set_blocking(false); |
| 113 | + |
| 114 | + for (int i = 0; i < repeats; i++) { |
| 115 | + nsapi_error_t err = net->connect(); |
| 116 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
| 117 | + |
| 118 | + status = wait_status_callback(); |
| 119 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_CONNECTING, status); |
| 120 | + |
| 121 | + status = wait_status_callback(); |
| 122 | + if (status == NSAPI_STATUS_LOCAL_UP) { |
| 123 | + status = wait_status_callback(); |
| 124 | + } |
| 125 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_GLOBAL_UP, status); |
| 126 | + |
| 127 | + net->disconnect(); |
| 128 | + |
| 129 | + status = wait_status_callback(); |
| 130 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, status); |
| 131 | + } |
| 132 | + |
| 133 | + net->attach(NULL); |
| 134 | + net->set_blocking(true); |
| 135 | +} |
| 136 | + |
| 137 | +void NETWORKINTERFACE_STATUS_GET() |
| 138 | +{ |
| 139 | + nsapi_connection_status_t status; |
| 140 | + |
| 141 | + net = NetworkInterface::get_default_instance(); |
| 142 | + net->set_blocking(true); |
| 143 | + |
| 144 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, net->get_connection_status()); |
| 145 | + |
| 146 | + for (int i = 0; i < repeats; i++) { |
| 147 | + nsapi_error_t err = net->connect(); |
| 148 | + TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
| 149 | + |
| 150 | + while (net->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) { |
| 151 | + wait(0.5); |
| 152 | + } |
| 153 | + |
| 154 | + net->disconnect(); |
| 155 | + |
| 156 | + TEST_ASSERT_EQUAL(NSAPI_STATUS_DISCONNECTED, net->get_connection_status()); |
| 157 | + } |
| 158 | + |
| 159 | + net->attach(NULL); |
| 160 | +} |
0 commit comments