Easily add all supported connectivity methods to your mbed OS project
esp8266-driver/TESTS/net/tcp_echo/main.cpp@0:615f90842ce8, 2017-07-12 (annotated)
- Committer:
- MACRUM
- Date:
- Wed Jul 12 10:52:58 2017 +0000
- Revision:
- 0:615f90842ce8
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:615f90842ce8 | 1 | #include "mbed.h" |
MACRUM | 0:615f90842ce8 | 2 | #include "ESP8266Interface.h" |
MACRUM | 0:615f90842ce8 | 3 | #include "TCPSocket.h" |
MACRUM | 0:615f90842ce8 | 4 | #include "greentea-client/test_env.h" |
MACRUM | 0:615f90842ce8 | 5 | #include "unity/unity.h" |
MACRUM | 0:615f90842ce8 | 6 | #include "utest.h" |
MACRUM | 0:615f90842ce8 | 7 | |
MACRUM | 0:615f90842ce8 | 8 | using namespace utest::v1; |
MACRUM | 0:615f90842ce8 | 9 | |
MACRUM | 0:615f90842ce8 | 10 | |
MACRUM | 0:615f90842ce8 | 11 | #ifndef MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE |
MACRUM | 0:615f90842ce8 | 12 | #define MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 256 |
MACRUM | 0:615f90842ce8 | 13 | #endif |
MACRUM | 0:615f90842ce8 | 14 | |
MACRUM | 0:615f90842ce8 | 15 | #ifndef MBED_CFG_ESP8266_TX |
MACRUM | 0:615f90842ce8 | 16 | #define MBED_CFG_ESP8266_TX D1 |
MACRUM | 0:615f90842ce8 | 17 | #endif |
MACRUM | 0:615f90842ce8 | 18 | |
MACRUM | 0:615f90842ce8 | 19 | #ifndef MBED_CFG_ESP8266_RX |
MACRUM | 0:615f90842ce8 | 20 | #define MBED_CFG_ESP8266_RX D0 |
MACRUM | 0:615f90842ce8 | 21 | #endif |
MACRUM | 0:615f90842ce8 | 22 | |
MACRUM | 0:615f90842ce8 | 23 | #ifndef MBED_CFG_ESP8266_DEBUG |
MACRUM | 0:615f90842ce8 | 24 | #define MBED_CFG_ESP8266_DEBUG false |
MACRUM | 0:615f90842ce8 | 25 | #endif |
MACRUM | 0:615f90842ce8 | 26 | |
MACRUM | 0:615f90842ce8 | 27 | namespace { |
MACRUM | 0:615f90842ce8 | 28 | char tx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; |
MACRUM | 0:615f90842ce8 | 29 | char rx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; |
MACRUM | 0:615f90842ce8 | 30 | const char ASCII_MAX = '~' - ' '; |
MACRUM | 0:615f90842ce8 | 31 | } |
MACRUM | 0:615f90842ce8 | 32 | |
MACRUM | 0:615f90842ce8 | 33 | void prep_buffer(char *tx_buffer, size_t tx_size) { |
MACRUM | 0:615f90842ce8 | 34 | for (size_t i=0; i<tx_size; ++i) { |
MACRUM | 0:615f90842ce8 | 35 | tx_buffer[i] = (rand() % 10) + '0'; |
MACRUM | 0:615f90842ce8 | 36 | } |
MACRUM | 0:615f90842ce8 | 37 | } |
MACRUM | 0:615f90842ce8 | 38 | |
MACRUM | 0:615f90842ce8 | 39 | void test_tcp_echo() { |
MACRUM | 0:615f90842ce8 | 40 | ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG); |
MACRUM | 0:615f90842ce8 | 41 | int err = net.connect(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS); |
MACRUM | 0:615f90842ce8 | 42 | |
MACRUM | 0:615f90842ce8 | 43 | if (err) { |
MACRUM | 0:615f90842ce8 | 44 | printf("MBED: failed to connect with an error of %d\r\n", err); |
MACRUM | 0:615f90842ce8 | 45 | TEST_ASSERT_EQUAL(0, err); |
MACRUM | 0:615f90842ce8 | 46 | } |
MACRUM | 0:615f90842ce8 | 47 | |
MACRUM | 0:615f90842ce8 | 48 | printf("MBED: TCPClient IP address is '%s'\n", net.get_ip_address()); |
MACRUM | 0:615f90842ce8 | 49 | printf("MBED: TCPClient waiting for server IP and port...\n"); |
MACRUM | 0:615f90842ce8 | 50 | |
MACRUM | 0:615f90842ce8 | 51 | greentea_send_kv("target_ip", net.get_ip_address()); |
MACRUM | 0:615f90842ce8 | 52 | |
MACRUM | 0:615f90842ce8 | 53 | bool result = false; |
MACRUM | 0:615f90842ce8 | 54 | |
MACRUM | 0:615f90842ce8 | 55 | char recv_key[] = "host_port"; |
MACRUM | 0:615f90842ce8 | 56 | char ipbuf[60] = {0}; |
MACRUM | 0:615f90842ce8 | 57 | char portbuf[16] = {0}; |
MACRUM | 0:615f90842ce8 | 58 | unsigned int port = 0; |
MACRUM | 0:615f90842ce8 | 59 | |
MACRUM | 0:615f90842ce8 | 60 | greentea_send_kv("host_ip", " "); |
MACRUM | 0:615f90842ce8 | 61 | greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); |
MACRUM | 0:615f90842ce8 | 62 | |
MACRUM | 0:615f90842ce8 | 63 | greentea_send_kv("host_port", " "); |
MACRUM | 0:615f90842ce8 | 64 | greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); |
MACRUM | 0:615f90842ce8 | 65 | sscanf(portbuf, "%u", &port); |
MACRUM | 0:615f90842ce8 | 66 | |
MACRUM | 0:615f90842ce8 | 67 | printf("MBED: Server IP address received: %s:%d \n", ipbuf, port); |
MACRUM | 0:615f90842ce8 | 68 | |
MACRUM | 0:615f90842ce8 | 69 | TCPSocket sock(&net); |
MACRUM | 0:615f90842ce8 | 70 | SocketAddress tcp_addr(ipbuf, port); |
MACRUM | 0:615f90842ce8 | 71 | if (sock.connect(tcp_addr) == 0) { |
MACRUM | 0:615f90842ce8 | 72 | printf("HTTP: Connected to %s:%d\r\n", ipbuf, port); |
MACRUM | 0:615f90842ce8 | 73 | printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer)); |
MACRUM | 0:615f90842ce8 | 74 | printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer)); |
MACRUM | 0:615f90842ce8 | 75 | |
MACRUM | 0:615f90842ce8 | 76 | prep_buffer(tx_buffer, sizeof(tx_buffer)); |
MACRUM | 0:615f90842ce8 | 77 | sock.send(tx_buffer, sizeof(tx_buffer)); |
MACRUM | 0:615f90842ce8 | 78 | printf("MBED: Finished sending\r\n"); |
MACRUM | 0:615f90842ce8 | 79 | // Server will respond with HTTP GET's success code |
MACRUM | 0:615f90842ce8 | 80 | const int ret = sock.recv(rx_buffer, sizeof(rx_buffer)); |
MACRUM | 0:615f90842ce8 | 81 | printf("MBED: Finished receiving\r\n"); |
MACRUM | 0:615f90842ce8 | 82 | |
MACRUM | 0:615f90842ce8 | 83 | result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer)); |
MACRUM | 0:615f90842ce8 | 84 | TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer)); |
MACRUM | 0:615f90842ce8 | 85 | TEST_ASSERT_EQUAL(true, result); |
MACRUM | 0:615f90842ce8 | 86 | } |
MACRUM | 0:615f90842ce8 | 87 | |
MACRUM | 0:615f90842ce8 | 88 | sock.close(); |
MACRUM | 0:615f90842ce8 | 89 | net.disconnect(); |
MACRUM | 0:615f90842ce8 | 90 | TEST_ASSERT_EQUAL(true, result); |
MACRUM | 0:615f90842ce8 | 91 | } |
MACRUM | 0:615f90842ce8 | 92 | |
MACRUM | 0:615f90842ce8 | 93 | |
MACRUM | 0:615f90842ce8 | 94 | // Test setup |
MACRUM | 0:615f90842ce8 | 95 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
MACRUM | 0:615f90842ce8 | 96 | char uuid[48] = {0}; |
MACRUM | 0:615f90842ce8 | 97 | GREENTEA_SETUP_UUID(120, "tcp_echo", uuid, 48); |
MACRUM | 0:615f90842ce8 | 98 | |
MACRUM | 0:615f90842ce8 | 99 | // create mac address based on uuid |
MACRUM | 0:615f90842ce8 | 100 | uint64_t mac = 0; |
MACRUM | 0:615f90842ce8 | 101 | for (int i = 0; i < sizeof(uuid); i++) { |
MACRUM | 0:615f90842ce8 | 102 | mac += uuid[i]; |
MACRUM | 0:615f90842ce8 | 103 | } |
MACRUM | 0:615f90842ce8 | 104 | mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1); |
MACRUM | 0:615f90842ce8 | 105 | |
MACRUM | 0:615f90842ce8 | 106 | return verbose_test_setup_handler(number_of_cases); |
MACRUM | 0:615f90842ce8 | 107 | } |
MACRUM | 0:615f90842ce8 | 108 | |
MACRUM | 0:615f90842ce8 | 109 | Case cases[] = { |
MACRUM | 0:615f90842ce8 | 110 | Case("TCP echo", test_tcp_echo), |
MACRUM | 0:615f90842ce8 | 111 | }; |
MACRUM | 0:615f90842ce8 | 112 | |
MACRUM | 0:615f90842ce8 | 113 | Specification specification(test_setup, cases); |
MACRUM | 0:615f90842ce8 | 114 | |
MACRUM | 0:615f90842ce8 | 115 | int main() { |
MACRUM | 0:615f90842ce8 | 116 | return !Harness::run(specification); |
MACRUM | 0:615f90842ce8 | 117 | } |
MACRUM | 0:615f90842ce8 | 118 |