void UDPSOCKET_SENDTO_TIMEOUT() { char tx_buffer[100]; fill_tx_buffer_ascii(tx_buffer, sizeof(tx_buffer)); UDPSocket sock; TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); SocketAddress udp_addr; get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr); udp_addr.set_port(9); Timer timer; timer.start(); int sent = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); timer.stop(); TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); TEST_ASSERT(timer.read_ms() <= 100); timer.reset(); timer.start(); sent = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); timer.stop(); TEST_ASSERT_EQUAL(sizeof(tx_buffer), sent); TEST_ASSERT(timer.read_ms() <= 100); printf("MBED: Time taken: %fs\n", timer.read()); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); }
void UDPSOCKET_SENDTO_INVALID() { UDPSocket sock; TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface())); TEST_ASSERT(sock.sendto(NULL, 9, NULL, 0) < 0); TEST_ASSERT(sock.sendto("", 9, NULL, 0) < 0); TEST_ASSERT(sock.sendto("", 0, NULL, 0) < 0); TEST_ASSERT_EQUAL(5, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 0, "hello", 5)); TEST_ASSERT_EQUAL(0, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, NULL, 0)); TEST_ASSERT_EQUAL(5, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, "hello", 5)); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); }
int main() { GREENTEA_SETUP(60, "default_auto"); bool result = false; const time_t TIME1970 = 2208988800L; int ntp_send_values[12] = {0}; int ntp_recv_values[12] = {0}; EthernetInterface eth; eth.connect(); printf("UDP client IP Address is %s\n", eth.get_ip_address()); UDPSocket sock; sock.open(ð); sock.set_timeout(15000); SocketAddress nist(ð, HTTP_SERVER_NAME, HTTP_SERVER_PORT); printf("UDP: NIST server %s address: %s on port %d\r\n", HTTP_SERVER_NAME, nist.get_ip_address(), nist.get_port()); memset(ntp_send_values, 0x00, sizeof(ntp_send_values)); ntp_send_values[0] = '\x1b'; while(1) { memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values)); int ret_send = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values)); printf("UDP: Sent %d Bytes to NTP server \n", ret_send); SocketAddress source; const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values)); printf("UDP: Recved from NTP server %d Bytes \n", n); if (n > 0 && strcmp(source.get_ip_address(), nist.get_ip_address()) == 0) { result = true; printf("UDP: Values returned by NTP server: \n"); for (size_t i=0; i < sizeof(ntp_recv_values) / sizeof(ntp_recv_values[0]); ++i) { printf("\t[%02d] 0x%X", i, ntohl(ntp_recv_values[i])); if (i == 10) { time_t timestamp = ntohl(ntp_recv_values[i]) - TIME1970; printf("\tNTP timestamp is %s", ctime(×tamp)); } else { printf("\n"); } } break; } printf("Failed to receive data, retrying in 5 seconds...\n"); wait(5); } sock.close(); eth.disconnect(); GREENTEA_TESTSUITE_RESULT(result); }
void UDPSOCKET_SENDTO_INVALID() { UDPSocket sock; TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); TEST_ASSERT(sock.sendto(NULL, 9, NULL, 0) < 0); TEST_ASSERT(sock.sendto("", 9, NULL, 0) < 0); TEST_ASSERT(sock.sendto("", 0, NULL, 0) < 0); nsapi_error_t result = sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, NULL, 0); if (result != NSAPI_ERROR_UNSUPPORTED) { TEST_ASSERT_EQUAL(0, result); } TEST_ASSERT_EQUAL(5, sock.sendto(MBED_CONF_APP_ECHO_SERVER_ADDR, 9, "hello", 5)); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); }
int main() { GREENTEA_SETUP(20, "udp_echo"); EthernetInterface eth; eth.connect(); printf("UDP client IP Address is %s\n", eth.get_ip_address()); greentea_send_kv("target_ip", eth.get_ip_address()); char recv_key[] = "host_port"; char ipbuf[60] = {0}; char portbuf[16] = {0}; unsigned int port = 0; UDPSocket sock; sock.open(ð); sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT); greentea_send_kv("host_ip", " "); greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); greentea_send_kv("host_port", " "); greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); sscanf(portbuf, "%u", &port); printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port); SocketAddress udp_addr(ipbuf, port); int success = 0; for (int i=0; i < ECHO_LOOPS; ++i) { prep_buffer(tx_buffer, sizeof(tx_buffer)); const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); printf("[%02d] sent...%d Bytes \n", i, ret); SocketAddress temp_addr; const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer)); printf("[%02d] recv...%d Bytes \n", i, n); if ((temp_addr == udp_addr && n == sizeof(tx_buffer) && memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) { success += 1; } } bool result = (success > 3*ECHO_LOOPS/4); sock.close(); eth.disconnect(); GREENTEA_TESTSUITE_RESULT(result); }
int main() { GREENTEA_SETUP(20, "udp_echo_client"); EthernetInterface eth; eth.connect(); printf("UDP client IP Address is %s\n", eth.get_ip_address()); greentea_send_kv("target_ip", eth.get_ip_address()); bool result = true; char recv_key[] = "host_port"; char ipbuf[60] = {0}; char portbuf[16] = {0}; unsigned int port = 0; UDPSocket sock; sock.open(ð); greentea_send_kv("host_ip", " "); greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); greentea_send_kv("host_port", " "); greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); sscanf(portbuf, "%u", &port); printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port); SocketAddress addr(ipbuf, port); for (int i=0; i < ECHO_LOOPS; ++i) { prep_buffer(tx_buffer, sizeof(tx_buffer)); const int ret = sock.sendto(addr, tx_buffer, sizeof(tx_buffer)); printf("[%02d] sent...%d Bytes \n", i, ret); const int n = sock.recvfrom(&addr, rx_buffer, sizeof(rx_buffer)); printf("[%02d] recv...%d Bytes \n", i, n); if (memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer))) { result = false; break; } } sock.close(); eth.disconnect(); GREENTEA_TESTSUITE_RESULT(result); }
void UDPSOCKET_RECV_TIMEOUT() { SocketAddress udp_addr; NetworkInterface::get_default_instance()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr); udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT); static const int DATA_LEN = 100; char buff[DATA_LEN] = {0}; UDPSocket sock; TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(NetworkInterface::get_default_instance())); sock.set_timeout(100); sock.sigio(callback(_sigio_handler, ThisThread::get_id())); int recvd; Timer timer; SocketAddress temp_addr; int pkt_success = 0; for (int i = 0; i < PKT_NUM; i++) { TEST_ASSERT_EQUAL(DATA_LEN, sock.sendto(udp_addr, buff, DATA_LEN)); timer.reset(); timer.start(); recvd = sock.recvfrom(&temp_addr, buff, sizeof(buff)); timer.stop(); if (recvd == NSAPI_ERROR_WOULD_BLOCK) { osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT); printf("MBED: recvfrom() took: %dms\n", timer.read_ms()); TEST_ASSERT_INT_WITHIN(51, 150, timer.read_ms()); continue; } else if (recvd < 0) { printf("[bt#%02d] network error %d\n", i, recvd); continue; } else if (temp_addr != udp_addr) { printf("[bt#%02d] packet from wrong address\n", i); continue; } TEST_ASSERT_EQUAL(DATA_LEN, recvd); pkt_success++; } printf("MBED: %d out of %d packets were received.\n", pkt_success, PKT_NUM); TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close()); }
void echo() { int success = 0; int err = sock.open(net); TEST_ASSERT_EQUAL(0, err); sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT); for (int i = 0; success < ECHO_LOOPS; i++) { prep_buffer(id, uuid, tx_buffer, sizeof(tx_buffer)); const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer)); if (ret >= 0) { iomutex.lock(); printf("[ID:%01d][%02d] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer); iomutex.unlock(); } else { iomutex.lock(); printf("[ID:%01d][%02d] Network error %d\n", id, i, ret); iomutex.unlock(); continue; } SocketAddress temp_addr; const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer)); if (n >= 0) { iomutex.lock(); printf("[ID:%01d][%02d] recv %d bytes - %.*s \n", id, i, n, n, tx_buffer); iomutex.unlock(); } else { iomutex.lock(); printf("[ID:%01d][%02d] Network error %d\n", id, i, n); iomutex.unlock(); continue; } if ((temp_addr == udp_addr && n == sizeof(tx_buffer) && memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) { success += 1; iomutex.lock(); printf("[ID:%01d][%02d] success #%d\n", id, i, success); iomutex.unlock(); continue; } // failed, clean out any remaining bad packets sock.set_timeout(0); while (true) { err = sock.recvfrom(NULL, NULL, 0); if (err == NSAPI_ERROR_WOULD_BLOCK) { break; } } sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT); } result = success == ECHO_LOOPS; err = sock.close(); TEST_ASSERT_EQUAL(0, err); if (err) { result = false; } }
void test_udp_dtls_handshake() { EthernetInterface eth; int err = eth.connect(); TEST_ASSERT_EQUAL(0, err); printf("MBED: UDPClient IP address is '%s'\n", eth.get_ip_address()); printf("MBED: UDPClient waiting for server IP and port...\n"); greentea_send_kv("target_ip", eth.get_ip_address()); bool result = false; char recv_key[] = "host_port"; char ipbuf[60] = {0}; char portbuf[16] = {0}; unsigned int port = 0; greentea_send_kv("host_ip", " "); greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); greentea_send_kv("host_port", " "); greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); sscanf(portbuf, "%u", &port); printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port); // align each size to 4-bits for (int i = 0; i < udp_dtls_handshake_count; i++) { udp_dtls_handshake_pattern[i] = (~0xf & udp_dtls_handshake_pattern[i]) + 0x10; } printf("MBED: DTLS pattern ["); for (int i = 0; i < udp_dtls_handshake_count; i++) { printf("%d", udp_dtls_handshake_pattern[i]); if (i != udp_dtls_handshake_count-1) { printf(", "); } } printf("]\r\n"); UDPSocket sock; SocketAddress udp_addr(ipbuf, port); sock.set_timeout(MBED_CFG_UDP_DTLS_HANDSHAKE_TIMEOUT); for (int attempt = 0; attempt < MBED_CFG_UDP_DTLS_HANDSHAKE_RETRIES; attempt++) { err = sock.open(ð); TEST_ASSERT_EQUAL(0, err); for (int i = 0; i < udp_dtls_handshake_count; i++) { buffer[i] = udp_dtls_handshake_pattern[i] >> 4; } err = sock.sendto(udp_addr, buffer, udp_dtls_handshake_count); printf("UDP: tx -> %d\r\n", err); TEST_ASSERT_EQUAL(udp_dtls_handshake_count, err); int step = 0; while (step < udp_dtls_handshake_count) { err = sock.recvfrom(NULL, buffer, sizeof(buffer)); printf("UDP: rx <- %d ", err); // check length if (err != udp_dtls_handshake_pattern[step]) { printf("x (expected %d)\r\n", udp_dtls_handshake_pattern[step]); break; } // check quick xor of packet uint8_t check = 0; for (int j = 0; j < udp_dtls_handshake_pattern[step]; j++) { check ^= buffer[j]; } if (check != 0) { printf("x (checksum 0x%02x)\r\n", check); break; } // successfully got a packet printf("\r\n"); step += 1; } err = sock.close(); TEST_ASSERT_EQUAL(0, err); // got through all steps, test passed if (step == udp_dtls_handshake_count) { result = true; break; } } eth.disconnect(); TEST_ASSERT(result); }