takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers asynchronous_dns_cancel.cpp Source File

asynchronous_dns_cancel.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 #include "dns_tests.h"
00023 
00024 using namespace utest::v1;
00025 
00026 void ASYNCHRONOUS_DNS_CANCEL()
00027 {
00028     rtos::Semaphore semaphore;
00029     dns_application_data *data = new dns_application_data[MBED_CONF_APP_DNS_TEST_HOSTS_NUM];
00030 
00031     int count = 0;
00032 
00033     for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
00034         data[i].value_set = false;
00035         data[i].semaphore = &semaphore;
00036         data[i].req_result = get_interface()->gethostbyname_async(dns_test_hosts[i],
00037                                                                   mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i]));
00038         TEST_ASSERT(data[i].req_result >= 0 || data[i].req_result == NSAPI_ERROR_NO_MEMORY );
00039 
00040         if (data[i].req_result >= 0) {
00041             // Callback will be called
00042             count++;
00043         } else {
00044             // No memory to initiate DNS query, callback will not be called
00045             data[i].result = NSAPI_ERROR_NO_MEMORY ;
00046             data[i].value_set = true;
00047         }
00048     }
00049 
00050     for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
00051         if (data[i].req_result > 0) {
00052             if (get_interface()->gethostbyname_async_cancel(data[i].req_result) == NSAPI_ERROR_OK ) {
00053                 count--;
00054             }
00055         }
00056     }
00057 
00058     // Wait for callback(s) to complete
00059     for (int i = 0; i < count; i++) {
00060         semaphore.wait();
00061     }
00062 
00063     for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
00064         if (!data[i].value_set) {
00065             printf("DNS: query \"%s\" => cancel\n", dns_test_hosts[i]);
00066             continue;
00067         }
00068         TEST_ASSERT(data[i].result == NSAPI_ERROR_OK  || data[i].result == NSAPI_ERROR_NO_MEMORY  || data[i].result == NSAPI_ERROR_DNS_FAILURE  || data[i].result == NSAPI_ERROR_TIMEOUT );
00069         if (data[i].result == NSAPI_ERROR_OK ) {
00070             printf("DNS: query \"%s\" => \"%s\"\n",
00071                    dns_test_hosts[i], data[i].addr.get_ip_address());
00072         } else if (data[i].result == NSAPI_ERROR_DNS_FAILURE ) {
00073             printf("DNS: query \"%s\" => DNS failure\n", dns_test_hosts[i]);
00074         } else if (data[i].result == NSAPI_ERROR_TIMEOUT ) {
00075             printf("DNS: query \"%s\" => timeout\n", dns_test_hosts[i]);
00076         } else if (data[i].result == NSAPI_ERROR_NO_MEMORY ) {
00077             printf("DNS: query \"%s\" => no memory\n", dns_test_hosts[i]);
00078         }
00079     }
00080 
00081     delete[] data;
00082 
00083     wait(5.0);
00084 }