Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stack.cpp
00001 /* 00002 * Copyright (c) 2017, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may 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, 00013 * WITHOUT 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 #include "CellularTests.h" 00018 00019 #ifdef CELLULAR_DEVICE 00020 00021 using namespace mbed; 00022 00023 static UDPSocket socket; 00024 #define SERVER_IP_ADDR "52.215.34.155" 00025 #define SERVER_UDP_PORT 7 00026 static rtos::Semaphore sock_event; 00027 00028 void test_socket_open() 00029 { 00030 nsapi_error_t err = socket.open(network); 00031 TEST_ASSERT(err == NSAPI_ERROR_OK ); 00032 } 00033 00034 void test_socket_bind() 00035 { 00036 nsapi_error_t err = socket.bind(3030); 00037 TEST_ASSERT(!err); 00038 } 00039 00040 /* 00041 void test_socket_set_blocking() 00042 { 00043 //socket.set_blocking(true); 00044 socket.set_timeout(5000); 00045 } 00046 00047 void test_socket_send_receive_blocking() 00048 { 00049 char receive_buffer[CELLULAR_MTU] = {0}; 00050 char send_buffer[] = { 'H', 'e', 'l', 'l', 'u', 'l', 'a', 'r', '!' }; 00051 int send_count = 0; 00052 int send_tries = 1; 00053 int max_send_tries = 3; 00054 00055 // Send to echo server 00056 while (send_tries <= max_send_tries) { 00057 tr_info("ONE!!!"); 00058 send_count = socket.sendto(SERVER_IP_ADDR, SERVER_UDP_PORT, send_buffer, sizeof(send_buffer)); 00059 TEST_ASSERT_MESSAGE(send_count == sizeof(send_buffer), "Sent count doesnt match sent buffer!"); 00060 send_tries++; 00061 00062 // Read response 00063 SocketAddress address; 00064 int receive_count = 0; 00065 // 2 tries. First recv attempt should be blocked and wait for a max 5 seconds for socket read flag 00066 int recv_tries = 2; 00067 while (recv_tries >= 0) { 00068 tr_info("RECV!!!"); 00069 receive_count = socket.recvfrom(&address, receive_buffer, sizeof(receive_buffer)); 00070 if (receive_count > 0) { 00071 break; 00072 } 00073 recv_tries--; 00074 wait(1); 00075 } 00076 TEST_ASSERT_MESSAGE(receive_count == send_count, "Receive and Sent count dont match!"); 00077 TEST_ASSERT_MESSAGE(strncmp(send_buffer, receive_buffer, send_count) == 0, "Sent data doesn't match received data while in ECHO"); 00078 } 00079 } 00080 */ 00081 00082 static void socket_sigio_cb() 00083 { 00084 sock_event.release(); 00085 } 00086 00087 void test_socket_set_non_blocking() 00088 { 00089 socket.set_blocking(false); 00090 socket.sigio(socket_sigio_cb); 00091 } 00092 00093 void test_socket_send_receive_non_blocking() 00094 { 00095 char receive_buffer[1500] = {0}; 00096 char send_buffer[] = { 'H', 'e', 'l', 'l', 'u', 'l', 'a', 'r', '!' }; 00097 00098 // Send to echo server 00099 int send_count = socket.sendto(SERVER_IP_ADDR, SERVER_UDP_PORT, send_buffer, sizeof(send_buffer)); 00100 TEST_ASSERT(send_count == sizeof(send_buffer)); 00101 00102 int32_t event; 00103 event = sock_event.wait(10000); 00104 TEST_ASSERT_MESSAGE( event>=1, "No Socket event within 10 seconds"); 00105 00106 // Read response 00107 SocketAddress address; 00108 int receive_count = socket.recvfrom(&address, receive_buffer, sizeof(receive_buffer)); 00109 TEST_ASSERT_MESSAGE(receive_count == send_count, "Receive and Sent count dont match!"); 00110 TEST_ASSERT_MESSAGE(strncmp(send_buffer, receive_buffer, send_count) == 0, "Sent data doesn't match received data while in ECHO"); 00111 } 00112 00113 00114 void test_socket_close() 00115 { 00116 nsapi_error_t err = socket.close(); 00117 TEST_ASSERT(!err); 00118 } 00119 00120 #endif // CELLULAR_DEVICE
Generated on Tue Jul 12 2022 13:31:32 by
