Toyomasa Watarai
/
Mbed-example-WS-W27
Mbed Cloud example program for workshop in W27 2018.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "ESP8266Interface.h" 00003 #include "TCPSocket.h" 00004 #include "greentea-client/test_env.h" 00005 #include "unity/unity.h" 00006 #include "utest.h" 00007 00008 using namespace utest::v1; 00009 00010 00011 #ifndef MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 00012 #define MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE 256 00013 #endif 00014 00015 #ifndef MBED_CFG_ESP8266_TX 00016 #define MBED_CFG_ESP8266_TX D1 00017 #endif 00018 00019 #ifndef MBED_CFG_ESP8266_RX 00020 #define MBED_CFG_ESP8266_RX D0 00021 #endif 00022 00023 #ifndef MBED_CFG_ESP8266_DEBUG 00024 #define MBED_CFG_ESP8266_DEBUG false 00025 #endif 00026 00027 #define STRINGIZE(x) STRINGIZE2(x) 00028 #define STRINGIZE2(x) #x 00029 00030 namespace { 00031 char tx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00032 char rx_buffer[MBED_CFG_TCP_CLIENT_ECHO_BUFFER_SIZE] = {0}; 00033 const char ASCII_MAX = '~' - ' '; 00034 } 00035 00036 void prep_buffer(char *tx_buffer, size_t tx_size) { 00037 for (size_t i=0; i<tx_size; ++i) { 00038 tx_buffer[i] = (rand() % 10) + '0'; 00039 } 00040 } 00041 00042 void test_tcp_echo() { 00043 ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG); 00044 int err = net.connect(STRINGIZE(MBED_CFG_ESP8266_SSID), STRINGIZE(MBED_CFG_ESP8266_PASS)); 00045 00046 if (err) { 00047 printf("MBED: failed to connect with an error of %d\r\n", err); 00048 TEST_ASSERT_EQUAL(0, err); 00049 } 00050 00051 printf("MBED: TCPClient IP address is '%s'\n", net.get_ip_address()); 00052 printf("MBED: TCPClient waiting for server IP and port...\n"); 00053 00054 greentea_send_kv("target_ip", net.get_ip_address()); 00055 00056 bool result = false; 00057 00058 char recv_key[] = "host_port"; 00059 char ipbuf[60] = {0}; 00060 char portbuf[16] = {0}; 00061 unsigned int port = 0; 00062 00063 greentea_send_kv("host_ip", " "); 00064 greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf)); 00065 00066 greentea_send_kv("host_port", " "); 00067 greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf)); 00068 sscanf(portbuf, "%u", &port); 00069 00070 printf("MBED: Server IP address received: %s:%d \n", ipbuf, port); 00071 00072 TCPSocket sock(&net); 00073 SocketAddress tcp_addr(ipbuf, port); 00074 if (sock.connect(tcp_addr) == 0) { 00075 printf("HTTP: Connected to %s:%d\r\n", ipbuf, port); 00076 printf("tx_buffer buffer size: %u\r\n", sizeof(tx_buffer)); 00077 printf("rx_buffer buffer size: %u\r\n", sizeof(rx_buffer)); 00078 00079 prep_buffer(tx_buffer, sizeof(tx_buffer)); 00080 sock.send(tx_buffer, sizeof(tx_buffer)); 00081 printf("MBED: Finished sending\r\n"); 00082 // Server will respond with HTTP GET's success code 00083 const int ret = sock.recv(rx_buffer, sizeof(rx_buffer)); 00084 printf("MBED: Finished receiving\r\n"); 00085 00086 result = !memcmp(tx_buffer, rx_buffer, sizeof(tx_buffer)); 00087 TEST_ASSERT_EQUAL(ret, sizeof(rx_buffer)); 00088 TEST_ASSERT_EQUAL(true, result); 00089 } 00090 00091 sock.close(); 00092 net.disconnect(); 00093 TEST_ASSERT_EQUAL(true, result); 00094 } 00095 00096 00097 // Test setup 00098 utest::v1::status_t test_setup(const size_t number_of_cases) { 00099 GREENTEA_SETUP(120, "tcp_echo"); 00100 return verbose_test_setup_handler(number_of_cases); 00101 } 00102 00103 Case cases[] = { 00104 Case("TCP echo", test_tcp_echo), 00105 }; 00106 00107 Specification specification(test_setup, cases); 00108 00109 int main() { 00110 return !Harness::run(specification); 00111 } 00112
Generated on Tue Jul 12 2022 16:22:07 by 1.7.2