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.
Fork of OmniWheels by
main.cpp
00001 /* 00002 * Copyright (c) 2013-2017, 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 #ifndef MBED_CONF_APP_CONNECT_STATEMENT 00019 #error [NOT_SUPPORTED] No network configuration found for this target. 00020 #endif 00021 00022 #include "mbed.h" 00023 #include MBED_CONF_APP_HEADER_FILE 00024 #include "UDPSocket.h" 00025 #include "greentea-client/test_env.h" 00026 #include "unity/unity.h" 00027 #include "utest.h" 00028 00029 using namespace utest::v1; 00030 00031 00032 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE 00033 #define MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE 512 00034 #endif 00035 00036 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES 00037 #define MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES 16 00038 #endif 00039 00040 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN 00041 #define MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN 112, 384, 200, 219, 25 00042 #endif 00043 00044 #ifndef MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT 00045 #define MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT 1500 00046 #endif 00047 00048 00049 uint8_t buffer[MBED_CFG_UDP_DTLS_HANDSHAKE_BUFFER_SIZE] = {0}; 00050 int udp_dtls_handshake_pattern[] = {MBED_CFG_UDP_DTLS_HANDSHAKE_PATTERN}; 00051 const int udp_dtls_handshake_count = sizeof(udp_dtls_handshake_pattern) / sizeof(int); 00052 00053 void test_udp_dtls_handshake() { 00054 NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION; 00055 int err = MBED_CONF_APP_CONNECT_STATEMENT; 00056 TEST_ASSERT_EQUAL(0, err); 00057 00058 printf("MBED: UDPClient IP address is '%s'\n", net->get_ip_address()); 00059 printf("MBED: UDPClient waiting for server IP and port...\n"); 00060 00061 greentea_send_kv("target_ip", net->get_ip_address()); 00062 00063 bool result = false; 00064 00065 char recv_key[] = "host_port"; 00066 char ipbuf[60] = {0}; 00067 char portbuf[16] = {0}; 00068 unsigned int port = 0; 00069 00070 greentea_send_kv("host_ip", " "); 00071 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); 00072 00073 greentea_send_kv("host_port", " "); 00074 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); 00075 sscanf(portbuf, "%u", &port); 00076 00077 printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port); 00078 00079 // align each size to 4-bits 00080 for (int i = 0; i < udp_dtls_handshake_count; i++) { 00081 udp_dtls_handshake_pattern[i] = (~0xf & udp_dtls_handshake_pattern[i]) + 0x10; 00082 } 00083 00084 printf("MBED: DTLS pattern ["); 00085 for (int i = 0; i < udp_dtls_handshake_count; i++) { 00086 printf("%d", udp_dtls_handshake_pattern[i]); 00087 if (i != udp_dtls_handshake_count-1) { 00088 printf(", "); 00089 } 00090 } 00091 printf("]\r\n"); 00092 00093 UDPSocket sock; 00094 SocketAddress udp_addr(ipbuf, port); 00095 sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT); 00096 00097 for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) { 00098 err = sock.open(net); 00099 TEST_ASSERT_EQUAL(0, err); 00100 00101 for (int i = 0; i < udp_dtls_handshake_count; i++) { 00102 buffer[i] = udp_dtls_handshake_pattern[i] >> 4; 00103 } 00104 00105 err = sock.sendto(udp_addr, buffer, udp_dtls_handshake_count); 00106 printf("UDP: tx -> %d\r\n", err); 00107 TEST_ASSERT_EQUAL(udp_dtls_handshake_count, err); 00108 00109 int step = 0; 00110 while (step < udp_dtls_handshake_count) { 00111 err = sock.recvfrom(NULL, buffer, sizeof(buffer)); 00112 printf("UDP: rx <- %d ", err); 00113 00114 // check length 00115 if (err != udp_dtls_handshake_pattern[step]) { 00116 printf("x (expected %d)\r\n", udp_dtls_handshake_pattern[step]); 00117 break; 00118 } 00119 00120 // check quick xor of packet 00121 uint8_t check = 0; 00122 for (int j = 0; j < udp_dtls_handshake_pattern[step]; j++) { 00123 check ^= buffer[j]; 00124 } 00125 00126 if (check != 0) { 00127 printf("x (checksum 0x%02x)\r\n", check); 00128 break; 00129 } 00130 00131 // successfully got a packet 00132 printf("\r\n"); 00133 step += 1; 00134 } 00135 00136 err = sock.close(); 00137 TEST_ASSERT_EQUAL(0, err); 00138 00139 // got through all steps, test passed 00140 if (step == udp_dtls_handshake_count) { 00141 result = true; 00142 break; 00143 } 00144 } 00145 00146 net->disconnect(); 00147 TEST_ASSERT_EQUAL(true, result); 00148 } 00149 00150 00151 // Test setup 00152 utest::v1::status_t test_setup(const size_t number_of_cases) { 00153 GREENTEA_SETUP(120, "udp_shotgun"); 00154 return verbose_test_setup_handler(number_of_cases); 00155 } 00156 00157 Case cases[] = { 00158 Case("UDP DTLS handshake", test_udp_dtls_handshake), 00159 }; 00160 00161 Specification specification(test_setup, cases); 00162 00163 int main() { 00164 return !Harness::run(specification); 00165 }
Generated on Fri Jul 22 2022 04:53:56 by
1.7.2
