This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).
Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn
The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/
pal/Test/Unitest/pal_socket_test.c@2:b894b3508057, 2017-09-05 (annotated)
- Committer:
- Ren Boting
- Date:
- Tue Sep 05 11:56:13 2017 +0900
- Revision:
- 2:b894b3508057
- Parent:
- 0:29983394c6b6
Update all libraries and reform main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
edamame22 | 0:29983394c6b6 | 1 | /* |
edamame22 | 0:29983394c6b6 | 2 | * Copyright (c) 2016 ARM Limited. All rights reserved. |
edamame22 | 0:29983394c6b6 | 3 | * SPDX-License-Identifier: Apache-2.0 |
edamame22 | 0:29983394c6b6 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
edamame22 | 0:29983394c6b6 | 5 | * not use this file except in compliance with the License. |
edamame22 | 0:29983394c6b6 | 6 | * You may obtain a copy of the License at |
edamame22 | 0:29983394c6b6 | 7 | * |
edamame22 | 0:29983394c6b6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
edamame22 | 0:29983394c6b6 | 9 | * |
edamame22 | 0:29983394c6b6 | 10 | * Unless required by applicable law or agreed to in writing, software |
edamame22 | 0:29983394c6b6 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
edamame22 | 0:29983394c6b6 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
edamame22 | 0:29983394c6b6 | 13 | * See the License for the specific language governing permissions and |
edamame22 | 0:29983394c6b6 | 14 | * limitations under the License. |
edamame22 | 0:29983394c6b6 | 15 | */ |
edamame22 | 0:29983394c6b6 | 16 | |
edamame22 | 0:29983394c6b6 | 17 | #include "pal.h" |
edamame22 | 0:29983394c6b6 | 18 | #include "pal_network.h" |
edamame22 | 0:29983394c6b6 | 19 | #include "pal_plat_network.h" |
edamame22 | 0:29983394c6b6 | 20 | #include "unity.h" |
edamame22 | 0:29983394c6b6 | 21 | #include "unity_fixture.h" |
edamame22 | 0:29983394c6b6 | 22 | #include "pal_test_utils.h" |
edamame22 | 0:29983394c6b6 | 23 | #include "pal_socket_test_utils.h" |
edamame22 | 0:29983394c6b6 | 24 | #include "string.h" |
edamame22 | 0:29983394c6b6 | 25 | |
edamame22 | 0:29983394c6b6 | 26 | |
edamame22 | 0:29983394c6b6 | 27 | |
edamame22 | 0:29983394c6b6 | 28 | TEST_GROUP(pal_socket); |
edamame22 | 0:29983394c6b6 | 29 | |
edamame22 | 0:29983394c6b6 | 30 | //sometimes you may want to get at local data in a module. |
edamame22 | 0:29983394c6b6 | 31 | //for example: If you plan to pass by reference, this could be useful |
edamame22 | 0:29983394c6b6 | 32 | //however, it should often be avoided |
edamame22 | 0:29983394c6b6 | 33 | //extern int Counter; |
edamame22 | 0:29983394c6b6 | 34 | |
edamame22 | 0:29983394c6b6 | 35 | #define PAL_NET_SUPPORT_LWIP 1 |
edamame22 | 0:29983394c6b6 | 36 | #define PAL_NET_TEST_SERVER_NAME "e109180-lin.kfn.arm.com" |
edamame22 | 0:29983394c6b6 | 37 | #define PAL_NET_TEST_SERVER_IP {10,45,48,190} |
edamame22 | 0:29983394c6b6 | 38 | #define PAL_NET_TEST_SERVER_IP_STRING "10.45.48.190" |
edamame22 | 0:29983394c6b6 | 39 | #define PAL_NET_TEST_SERVER_HTTP_PORT 8686 |
edamame22 | 0:29983394c6b6 | 40 | #define PAL_NET_TEST_SERVER_UDP_PORT 8383 |
edamame22 | 0:29983394c6b6 | 41 | #define PAL_NET_TEST_INCOMING_PORT 8000 |
edamame22 | 0:29983394c6b6 | 42 | |
edamame22 | 0:29983394c6b6 | 43 | void * g_networkInterface = NULL; |
edamame22 | 0:29983394c6b6 | 44 | |
edamame22 | 0:29983394c6b6 | 45 | |
edamame22 | 0:29983394c6b6 | 46 | static uint32_t s_callbackcounter = 0; |
edamame22 | 0:29983394c6b6 | 47 | |
edamame22 | 0:29983394c6b6 | 48 | void socketCallback() |
edamame22 | 0:29983394c6b6 | 49 | { |
edamame22 | 0:29983394c6b6 | 50 | s_callbackcounter++; |
edamame22 | 0:29983394c6b6 | 51 | } |
edamame22 | 0:29983394c6b6 | 52 | |
edamame22 | 0:29983394c6b6 | 53 | TEST_SETUP(pal_socket) |
edamame22 | 0:29983394c6b6 | 54 | { |
edamame22 | 0:29983394c6b6 | 55 | uint32_t index = 0; |
edamame22 | 0:29983394c6b6 | 56 | palStatus_t status = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 57 | static void * interfaceCTX = NULL; |
edamame22 | 0:29983394c6b6 | 58 | //This is run before EACH TEST |
edamame22 | 0:29983394c6b6 | 59 | if (!interfaceCTX) |
edamame22 | 0:29983394c6b6 | 60 | { |
edamame22 | 0:29983394c6b6 | 61 | status = pal_init(); |
edamame22 | 0:29983394c6b6 | 62 | if (PAL_SUCCESS == status) |
edamame22 | 0:29983394c6b6 | 63 | { |
edamame22 | 0:29983394c6b6 | 64 | interfaceCTX = palTestGetNetWorkInterfaceContext(); |
edamame22 | 0:29983394c6b6 | 65 | pal_registerNetworkInterface(interfaceCTX , &index); |
edamame22 | 0:29983394c6b6 | 66 | g_networkInterface = interfaceCTX; |
edamame22 | 0:29983394c6b6 | 67 | } |
edamame22 | 0:29983394c6b6 | 68 | } |
edamame22 | 0:29983394c6b6 | 69 | } |
edamame22 | 0:29983394c6b6 | 70 | |
edamame22 | 0:29983394c6b6 | 71 | TEST_TEAR_DOWN(pal_socket) |
edamame22 | 0:29983394c6b6 | 72 | { |
edamame22 | 0:29983394c6b6 | 73 | } |
edamame22 | 0:29983394c6b6 | 74 | |
edamame22 | 0:29983394c6b6 | 75 | #define PAL_TEST_BUFFER_SIZE 50 |
edamame22 | 0:29983394c6b6 | 76 | |
edamame22 | 0:29983394c6b6 | 77 | TEST(pal_socket, socketUDPCreationOptionsTest) |
edamame22 | 0:29983394c6b6 | 78 | { |
edamame22 | 0:29983394c6b6 | 79 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 80 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 81 | palSocket_t sock2 = 0; |
edamame22 | 0:29983394c6b6 | 82 | palSocket_t sock3 = 0; |
edamame22 | 0:29983394c6b6 | 83 | palSocket_t sock5 = 0; |
edamame22 | 0:29983394c6b6 | 84 | uint32_t numInterface = 0; |
edamame22 | 0:29983394c6b6 | 85 | palNetInterfaceInfo_t interfaceInfo; |
edamame22 | 0:29983394c6b6 | 86 | uint32_t interfaceIndex = 0; |
edamame22 | 0:29983394c6b6 | 87 | uint32_t sockOptVal = 5000; |
edamame22 | 0:29983394c6b6 | 88 | uint32_t sockOptLen = sizeof(sockOptVal); |
edamame22 | 0:29983394c6b6 | 89 | |
edamame22 | 0:29983394c6b6 | 90 | TEST_PRINTF("start socket test\r\n"); |
edamame22 | 0:29983394c6b6 | 91 | |
edamame22 | 0:29983394c6b6 | 92 | memset(&interfaceInfo,0,sizeof(interfaceInfo)); |
edamame22 | 0:29983394c6b6 | 93 | // check that re-addignt he network interface returns the same index |
edamame22 | 0:29983394c6b6 | 94 | pal_registerNetworkInterface(g_networkInterface, &interfaceIndex); |
edamame22 | 0:29983394c6b6 | 95 | TEST_ASSERT_EQUAL(interfaceIndex, 0); |
edamame22 | 0:29983394c6b6 | 96 | pal_registerNetworkInterface(g_networkInterface, &interfaceIndex); |
edamame22 | 0:29983394c6b6 | 97 | TEST_ASSERT_EQUAL(interfaceIndex, 0); |
edamame22 | 0:29983394c6b6 | 98 | |
edamame22 | 0:29983394c6b6 | 99 | TEST_PRINTF("create sockets\r\n"); |
edamame22 | 0:29983394c6b6 | 100 | |
edamame22 | 0:29983394c6b6 | 101 | |
edamame22 | 0:29983394c6b6 | 102 | //blocking |
edamame22 | 0:29983394c6b6 | 103 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, interfaceIndex, &sock); |
edamame22 | 0:29983394c6b6 | 104 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 105 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, interfaceIndex, &sock2); |
edamame22 | 0:29983394c6b6 | 106 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 107 | //non-blocking |
edamame22 | 0:29983394c6b6 | 108 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, true, interfaceIndex, &sock5); |
edamame22 | 0:29983394c6b6 | 109 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 110 | |
edamame22 | 0:29983394c6b6 | 111 | result = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, false, interfaceIndex, socketCallback, &sock3); |
edamame22 | 0:29983394c6b6 | 112 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 113 | |
edamame22 | 0:29983394c6b6 | 114 | |
edamame22 | 0:29983394c6b6 | 115 | result = pal_getNumberOfNetInterfaces(&numInterface); |
edamame22 | 0:29983394c6b6 | 116 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 117 | TEST_ASSERT_EQUAL(numInterface, 1); |
edamame22 | 0:29983394c6b6 | 118 | |
edamame22 | 0:29983394c6b6 | 119 | result = pal_getNetInterfaceInfo(0, &interfaceInfo); |
edamame22 | 0:29983394c6b6 | 120 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 121 | TEST_PRINTF("interface addr: %d %d %d %d \r\n", interfaceInfo.address.addressData[2], interfaceInfo.address.addressData[3], interfaceInfo.address.addressData[4], interfaceInfo.address.addressData[5]); |
edamame22 | 0:29983394c6b6 | 122 | |
edamame22 | 0:29983394c6b6 | 123 | |
edamame22 | 0:29983394c6b6 | 124 | result = pal_setSocketOptions(sock, PAL_SO_RCVTIMEO, &sockOptVal, sockOptLen); |
edamame22 | 0:29983394c6b6 | 125 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 126 | |
edamame22 | 0:29983394c6b6 | 127 | TEST_PRINTF("close sockets\r\n"); |
edamame22 | 0:29983394c6b6 | 128 | |
edamame22 | 0:29983394c6b6 | 129 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 130 | result = pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 131 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 132 | result = pal_close(&sock2); |
edamame22 | 0:29983394c6b6 | 133 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 134 | result = pal_close(&sock5); |
edamame22 | 0:29983394c6b6 | 135 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 136 | result = pal_close(&sock3); |
edamame22 | 0:29983394c6b6 | 137 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 138 | |
edamame22 | 0:29983394c6b6 | 139 | TEST_PRINTF("end\r\n"); |
edamame22 | 0:29983394c6b6 | 140 | |
edamame22 | 0:29983394c6b6 | 141 | } |
edamame22 | 0:29983394c6b6 | 142 | |
edamame22 | 0:29983394c6b6 | 143 | |
edamame22 | 0:29983394c6b6 | 144 | TEST(pal_socket, basicTCPclinetSendRecieve) |
edamame22 | 0:29983394c6b6 | 145 | { |
edamame22 | 0:29983394c6b6 | 146 | |
edamame22 | 0:29983394c6b6 | 147 | |
edamame22 | 0:29983394c6b6 | 148 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 149 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 150 | palSocketAddress_t address = { 0 }; |
edamame22 | 0:29983394c6b6 | 151 | const char* message = "GET / HTTP/1.0\r\n\r\n"; |
edamame22 | 0:29983394c6b6 | 152 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 153 | char buffer[100] = { 0 }; |
edamame22 | 0:29983394c6b6 | 154 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 155 | palSocketLength_t addrlen = 0; |
edamame22 | 0:29983394c6b6 | 156 | |
edamame22 | 0:29983394c6b6 | 157 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock); |
edamame22 | 0:29983394c6b6 | 158 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 159 | |
edamame22 | 0:29983394c6b6 | 160 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
edamame22 | 0:29983394c6b6 | 161 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 162 | |
edamame22 | 0:29983394c6b6 | 163 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_HTTP_PORT); |
edamame22 | 0:29983394c6b6 | 164 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 165 | |
edamame22 | 0:29983394c6b6 | 166 | result = pal_connect(sock, &address, 16); |
edamame22 | 0:29983394c6b6 | 167 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 168 | |
edamame22 | 0:29983394c6b6 | 169 | result = pal_send(sock, message, 45, &sent); |
edamame22 | 0:29983394c6b6 | 170 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 171 | |
edamame22 | 0:29983394c6b6 | 172 | result = pal_recv(sock, buffer, 99, &read); |
edamame22 | 0:29983394c6b6 | 173 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 174 | TEST_PRINTF(buffer); |
edamame22 | 0:29983394c6b6 | 175 | |
edamame22 | 0:29983394c6b6 | 176 | TEST_ASSERT(read >= 4); |
edamame22 | 0:29983394c6b6 | 177 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
edamame22 | 0:29983394c6b6 | 178 | pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 179 | |
edamame22 | 0:29983394c6b6 | 180 | TEST_PRINTF("test Done"); |
edamame22 | 0:29983394c6b6 | 181 | |
edamame22 | 0:29983394c6b6 | 182 | } |
edamame22 | 0:29983394c6b6 | 183 | |
edamame22 | 0:29983394c6b6 | 184 | TEST(pal_socket, basicUDPclinetSendRecieve) |
edamame22 | 0:29983394c6b6 | 185 | { |
edamame22 | 0:29983394c6b6 | 186 | |
edamame22 | 0:29983394c6b6 | 187 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 188 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 189 | palSocketAddress_t address = { 0 }; |
edamame22 | 0:29983394c6b6 | 190 | palSocketAddress_t address2 = { 0 }; |
edamame22 | 0:29983394c6b6 | 191 | uint8_t buffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; |
edamame22 | 0:29983394c6b6 | 192 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 193 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 194 | palSocketLength_t addrlen = 0; |
edamame22 | 0:29983394c6b6 | 195 | |
edamame22 | 0:29983394c6b6 | 196 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, 0, &sock); |
edamame22 | 0:29983394c6b6 | 197 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 198 | |
edamame22 | 0:29983394c6b6 | 199 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
edamame22 | 0:29983394c6b6 | 200 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 201 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_UDP_PORT); |
edamame22 | 0:29983394c6b6 | 202 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 203 | |
edamame22 | 0:29983394c6b6 | 204 | TEST_PRINTF("udp send \r\n"); |
edamame22 | 0:29983394c6b6 | 205 | result = pal_sendTo(sock, buffer, 10, &address, 16, &sent); |
edamame22 | 0:29983394c6b6 | 206 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 207 | TEST_ASSERT_EQUAL(sent, 10); |
edamame22 | 0:29983394c6b6 | 208 | result = pal_plat_receiveFrom(sock, buffer, 10, &address2, &addrlen, &read); |
edamame22 | 0:29983394c6b6 | 209 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 210 | TEST_ASSERT_EQUAL(read, 10); |
edamame22 | 0:29983394c6b6 | 211 | TEST_PRINTF("udp done \r\n"); |
edamame22 | 0:29983394c6b6 | 212 | pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 213 | } |
edamame22 | 0:29983394c6b6 | 214 | |
edamame22 | 0:29983394c6b6 | 215 | |
edamame22 | 0:29983394c6b6 | 216 | TEST(pal_socket, basicSocketScenario3) |
edamame22 | 0:29983394c6b6 | 217 | { |
edamame22 | 0:29983394c6b6 | 218 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 219 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 220 | palSocketAddress_t address = { 0 }; |
edamame22 | 0:29983394c6b6 | 221 | const char* message = "GET / HTTP/1.0\r\nHost:10.45.48.68:8000\r\n\r\n"; |
edamame22 | 0:29983394c6b6 | 222 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 223 | char buffer[100] = { 0 }; |
edamame22 | 0:29983394c6b6 | 224 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 225 | s_callbackcounter = 0; |
edamame22 | 0:29983394c6b6 | 226 | palSocketLength_t addrlen = 0; |
edamame22 | 0:29983394c6b6 | 227 | |
edamame22 | 0:29983394c6b6 | 228 | result = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, socketCallback, &sock); |
edamame22 | 0:29983394c6b6 | 229 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 230 | |
edamame22 | 0:29983394c6b6 | 231 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
edamame22 | 0:29983394c6b6 | 232 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 233 | |
edamame22 | 0:29983394c6b6 | 234 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_HTTP_PORT); |
edamame22 | 0:29983394c6b6 | 235 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 236 | |
edamame22 | 0:29983394c6b6 | 237 | result = pal_connect(sock, &address, 16); |
edamame22 | 0:29983394c6b6 | 238 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 239 | |
edamame22 | 0:29983394c6b6 | 240 | result = pal_send(sock, message, 45, &sent); |
edamame22 | 0:29983394c6b6 | 241 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 242 | |
edamame22 | 0:29983394c6b6 | 243 | result = pal_recv(sock, buffer, 99, &read); |
edamame22 | 0:29983394c6b6 | 244 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 245 | TEST_PRINTF(buffer); |
edamame22 | 0:29983394c6b6 | 246 | |
edamame22 | 0:29983394c6b6 | 247 | TEST_ASSERT(read >= 4); |
edamame22 | 0:29983394c6b6 | 248 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
edamame22 | 0:29983394c6b6 | 249 | TEST_ASSERT(s_callbackcounter > 0); |
edamame22 | 0:29983394c6b6 | 250 | TEST_PRINTF("callback counter %d \r\n", s_callbackcounter); |
edamame22 | 0:29983394c6b6 | 251 | pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 252 | |
edamame22 | 0:29983394c6b6 | 253 | TEST_PRINTF("test Done"); |
edamame22 | 0:29983394c6b6 | 254 | } |
edamame22 | 0:29983394c6b6 | 255 | |
edamame22 | 0:29983394c6b6 | 256 | TEST(pal_socket, basicSocketScenario4) |
edamame22 | 0:29983394c6b6 | 257 | { |
edamame22 | 0:29983394c6b6 | 258 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 259 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 260 | palSocket_t sock2 = 0; |
edamame22 | 0:29983394c6b6 | 261 | palSocketAddress_t address = { 0 }; |
edamame22 | 0:29983394c6b6 | 262 | const char* message = "GET / HTTP/1.0\r\n\r\n"; |
edamame22 | 0:29983394c6b6 | 263 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 264 | char buffer[100] = { 0 }; |
edamame22 | 0:29983394c6b6 | 265 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 266 | palSocketLength_t addlen = 0; |
edamame22 | 0:29983394c6b6 | 267 | uint32_t numSockets = 0; |
edamame22 | 0:29983394c6b6 | 268 | palSocket_t socketsToCheck[PAL_NET_SOCKET_SELECT_MAX_SOCKETS] = { 0 }; |
edamame22 | 0:29983394c6b6 | 269 | pal_timeVal_t tv = {0}; |
edamame22 | 0:29983394c6b6 | 270 | uint8_t palSocketStatus[PAL_NET_SOCKET_SELECT_MAX_SOCKETS] = { 0 }; |
edamame22 | 0:29983394c6b6 | 271 | |
edamame22 | 0:29983394c6b6 | 272 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock); |
edamame22 | 0:29983394c6b6 | 273 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 274 | |
edamame22 | 0:29983394c6b6 | 275 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock2); |
edamame22 | 0:29983394c6b6 | 276 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 277 | |
edamame22 | 0:29983394c6b6 | 278 | result = pal_getAddressInfo("www.w3.org", &address, &addlen); |
edamame22 | 0:29983394c6b6 | 279 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 280 | TEST_PRINTF("addr lookup: %d %d %d %d \r\n", address.addressData[2], address.addressData[3], address.addressData[4], address.addressData[5]); |
edamame22 | 0:29983394c6b6 | 281 | |
edamame22 | 0:29983394c6b6 | 282 | result = pal_setSockAddrPort(&address, 80); |
edamame22 | 0:29983394c6b6 | 283 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 284 | |
edamame22 | 0:29983394c6b6 | 285 | result = pal_connect(sock, &address, 16); |
edamame22 | 0:29983394c6b6 | 286 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 287 | |
edamame22 | 0:29983394c6b6 | 288 | result = pal_send(sock, message, 45, &sent); |
edamame22 | 0:29983394c6b6 | 289 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 290 | |
edamame22 | 0:29983394c6b6 | 291 | socketsToCheck[0] = sock; |
edamame22 | 0:29983394c6b6 | 292 | socketsToCheck[1] = sock2; |
edamame22 | 0:29983394c6b6 | 293 | tv.pal_tv_sec = 5; |
edamame22 | 0:29983394c6b6 | 294 | tv.pal_tv_usec = 1000; |
edamame22 | 0:29983394c6b6 | 295 | |
edamame22 | 0:29983394c6b6 | 296 | result = pal_plat_socketMiniSelect(socketsToCheck, 2, &tv, palSocketStatus, &numSockets); |
edamame22 | 0:29983394c6b6 | 297 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 298 | //TEST_ASSERT_EQUAL(numSockets, 1); |
edamame22 | 0:29983394c6b6 | 299 | //TEST_ASSERT(palSocketStatus[0] >= 0); |
edamame22 | 0:29983394c6b6 | 300 | |
edamame22 | 0:29983394c6b6 | 301 | result = pal_recv(sock, buffer, 99, &read); |
edamame22 | 0:29983394c6b6 | 302 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 303 | TEST_PRINTF(buffer); |
edamame22 | 0:29983394c6b6 | 304 | |
edamame22 | 0:29983394c6b6 | 305 | TEST_ASSERT(read >= 4); |
edamame22 | 0:29983394c6b6 | 306 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
edamame22 | 0:29983394c6b6 | 307 | |
edamame22 | 0:29983394c6b6 | 308 | socketsToCheck[0] = sock2; |
edamame22 | 0:29983394c6b6 | 309 | socketsToCheck[1] = 0; |
edamame22 | 0:29983394c6b6 | 310 | tv.pal_tv_sec = 0; |
edamame22 | 0:29983394c6b6 | 311 | tv.pal_tv_usec = 20000; |
edamame22 | 0:29983394c6b6 | 312 | result = pal_plat_socketMiniSelect(socketsToCheck, 1, &tv, palSocketStatus, &numSockets); |
edamame22 | 0:29983394c6b6 | 313 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 314 | TEST_ASSERT_EQUAL(numSockets, 0); |
edamame22 | 0:29983394c6b6 | 315 | TEST_ASSERT(palSocketStatus[0] == 0); |
edamame22 | 0:29983394c6b6 | 316 | |
edamame22 | 0:29983394c6b6 | 317 | pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 318 | pal_close(&sock2); |
edamame22 | 0:29983394c6b6 | 319 | |
edamame22 | 0:29983394c6b6 | 320 | TEST_PRINTF("test Done"); |
edamame22 | 0:29983394c6b6 | 321 | |
edamame22 | 0:29983394c6b6 | 322 | } |
edamame22 | 0:29983394c6b6 | 323 | |
edamame22 | 0:29983394c6b6 | 324 | TEST(pal_socket, basicSocketScenario5) |
edamame22 | 0:29983394c6b6 | 325 | { |
edamame22 | 0:29983394c6b6 | 326 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 327 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 328 | palSocket_t sock2 = 0; |
edamame22 | 0:29983394c6b6 | 329 | palSocket_t sock3 = 0; |
edamame22 | 0:29983394c6b6 | 330 | |
edamame22 | 0:29983394c6b6 | 331 | palSocketAddress_t address2 = { 0 }; |
edamame22 | 0:29983394c6b6 | 332 | |
edamame22 | 0:29983394c6b6 | 333 | char buffer[100] = { 0 }; |
edamame22 | 0:29983394c6b6 | 334 | const char* messageOut = "HTTP/1.0 200 OK"; |
edamame22 | 0:29983394c6b6 | 335 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 336 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 337 | palSocketLength_t addrlen = 16; |
edamame22 | 0:29983394c6b6 | 338 | palNetInterfaceInfo_t interfaceInfo; |
edamame22 | 0:29983394c6b6 | 339 | |
edamame22 | 0:29983394c6b6 | 340 | memset(&interfaceInfo,0,sizeof(interfaceInfo)); |
edamame22 | 0:29983394c6b6 | 341 | |
edamame22 | 0:29983394c6b6 | 342 | |
edamame22 | 0:29983394c6b6 | 343 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM_SERVER, false, 0, &sock); |
edamame22 | 0:29983394c6b6 | 344 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 345 | |
edamame22 | 0:29983394c6b6 | 346 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock2); |
edamame22 | 0:29983394c6b6 | 347 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 348 | |
edamame22 | 0:29983394c6b6 | 349 | result = pal_getNetInterfaceInfo(0, &interfaceInfo); |
edamame22 | 0:29983394c6b6 | 350 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 351 | |
edamame22 | 0:29983394c6b6 | 352 | result = pal_setSockAddrPort(&(interfaceInfo.address), PAL_NET_TEST_INCOMING_PORT); |
edamame22 | 0:29983394c6b6 | 353 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 354 | |
edamame22 | 0:29983394c6b6 | 355 | result = pal_bind(sock, &(interfaceInfo.address), interfaceInfo.addressSize); |
edamame22 | 0:29983394c6b6 | 356 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 357 | |
edamame22 | 0:29983394c6b6 | 358 | result = pal_listen(sock, 10); |
edamame22 | 0:29983394c6b6 | 359 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 360 | |
edamame22 | 0:29983394c6b6 | 361 | TEST_PRINTF("waiting for connection:\r\n"); |
edamame22 | 0:29983394c6b6 | 362 | result = pal_accept(sock, &address2, &addrlen, &sock2); |
edamame22 | 0:29983394c6b6 | 363 | TEST_PRINTF("after accept:\r\n"); |
edamame22 | 0:29983394c6b6 | 364 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 365 | |
edamame22 | 0:29983394c6b6 | 366 | |
edamame22 | 0:29983394c6b6 | 367 | result = pal_recv(sock2, buffer, 99, &read); |
edamame22 | 0:29983394c6b6 | 368 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 369 | TEST_PRINTF(buffer); |
edamame22 | 0:29983394c6b6 | 370 | |
edamame22 | 0:29983394c6b6 | 371 | result = pal_send(sock2, messageOut, 15, &sent); |
edamame22 | 0:29983394c6b6 | 372 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 373 | |
edamame22 | 0:29983394c6b6 | 374 | |
edamame22 | 0:29983394c6b6 | 375 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock3); |
edamame22 | 0:29983394c6b6 | 376 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 377 | |
edamame22 | 0:29983394c6b6 | 378 | |
edamame22 | 0:29983394c6b6 | 379 | |
edamame22 | 0:29983394c6b6 | 380 | pal_close(&sock2); |
edamame22 | 0:29983394c6b6 | 381 | pal_close(&sock3); |
edamame22 | 0:29983394c6b6 | 382 | pal_close(&sock); |
edamame22 | 0:29983394c6b6 | 383 | |
edamame22 | 0:29983394c6b6 | 384 | TEST_PRINTF("test Done"); |
edamame22 | 0:29983394c6b6 | 385 | } |
edamame22 | 0:29983394c6b6 | 386 | |
edamame22 | 0:29983394c6b6 | 387 | |
edamame22 | 0:29983394c6b6 | 388 | TEST(pal_socket, tProvUDPTest) |
edamame22 | 0:29983394c6b6 | 389 | { |
edamame22 | 0:29983394c6b6 | 390 | |
edamame22 | 0:29983394c6b6 | 391 | palStatus_t result = PAL_SUCCESS; |
edamame22 | 0:29983394c6b6 | 392 | palSocket_t sock = 0; |
edamame22 | 0:29983394c6b6 | 393 | palSocketAddress_t address = { 0 }; |
edamame22 | 0:29983394c6b6 | 394 | palSocketAddress_t address2 = { 0 }; |
edamame22 | 0:29983394c6b6 | 395 | char buffer[100] = { 0 }; |
edamame22 | 0:29983394c6b6 | 396 | const char* messageOut = "HTTP/1.0 200 OK"; |
edamame22 | 0:29983394c6b6 | 397 | size_t sent = 0; |
edamame22 | 0:29983394c6b6 | 398 | size_t read = 0; |
edamame22 | 0:29983394c6b6 | 399 | palSocketLength_t addrlen = 16; |
edamame22 | 0:29983394c6b6 | 400 | palSocketLength_t addrlen2 = 16; |
edamame22 | 0:29983394c6b6 | 401 | int timeout = 10000; |
edamame22 | 0:29983394c6b6 | 402 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, 0, &sock); |
edamame22 | 0:29983394c6b6 | 403 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 404 | |
edamame22 | 0:29983394c6b6 | 405 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_IP_STRING, &address, &addrlen); |
edamame22 | 0:29983394c6b6 | 406 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 407 | |
edamame22 | 0:29983394c6b6 | 408 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_UDP_PORT); |
edamame22 | 0:29983394c6b6 | 409 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 410 | |
edamame22 | 0:29983394c6b6 | 411 | result = pal_setSocketOptions(sock, PAL_SO_SNDTIMEO, &timeout, sizeof(timeout)); |
edamame22 | 0:29983394c6b6 | 412 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 413 | |
edamame22 | 0:29983394c6b6 | 414 | result = pal_sendTo(sock, messageOut, 16, &address, addrlen, &sent); |
edamame22 | 0:29983394c6b6 | 415 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 416 | TEST_ASSERT_EQUAL(sent, 16); |
edamame22 | 0:29983394c6b6 | 417 | |
edamame22 | 0:29983394c6b6 | 418 | result = pal_receiveFrom(sock, buffer, 100, NULL, NULL, &read); |
edamame22 | 0:29983394c6b6 | 419 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 420 | TEST_ASSERT_EQUAL(read, 16); |
edamame22 | 0:29983394c6b6 | 421 | |
edamame22 | 0:29983394c6b6 | 422 | timeout = 1; |
edamame22 | 0:29983394c6b6 | 423 | result = pal_setSocketOptions(sock, PAL_SO_RCVTIMEO, &timeout, sizeof(timeout)); |
edamame22 | 0:29983394c6b6 | 424 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
edamame22 | 0:29983394c6b6 | 425 | |
edamame22 | 0:29983394c6b6 | 426 | result = pal_receiveFrom(sock, buffer, 100, &address2, &addrlen2, &read); // should get timeout |
edamame22 | 0:29983394c6b6 | 427 | TEST_ASSERT_EQUAL(result, PAL_ERR_SOCKET_WOULD_BLOCK); |
edamame22 | 0:29983394c6b6 | 428 | } |