diff --git a/tests/basic_wifi/ap/ap.c b/tests/basic_wifi/ap/ap.c index 24768c0..22ee732 100644 --- a/tests/basic_wifi/ap/ap.c +++ b/tests/basic_wifi/ap/ap.c @@ -1,11 +1,8 @@ -/* Very basic example showing usage of access point mode and the DHCP server. - - The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that - outputs some status information if you connect to it, then closes - the connection. - - This example code is in the public domain. -*/ +/** + * This test creates a WiFi access point and listens on port 23 for incomming + * connection. When incomming connection occurs it sends a string and waits for + * a response. + */ #include #include @@ -19,12 +16,37 @@ #include "test/test.h" -#define AP_SSID "esp-open-rtos-ap" -#define AP_PSK "esp-open-rtos" +#define AP_SSID "esp-open-rtos-ap" +#define AP_PSK "esp-open-rtos" +#define SERVER_PORT 23 -#define TELNET_PORT 23 -static void telnetTask(void *pvParameters); +static void server_task(void *pvParameters) +{ + struct netconn *nc = netconn_new(NETCONN_TCP); + TEST_ASSERT(nc, "failed to allocate socket.") + + netconn_bind(nc, IP_ADDR_ANY, SERVER_PORT); + netconn_listen(nc); + + struct netconn *client = NULL; + err_t err = netconn_accept(nc, &client); + TEST_ASSERT(err == ERR_OK, "Error accepting connection"); + + ip_addr_t client_addr; + uint16_t port_ignore; + netconn_peer(client, &client_addr, &port_ignore); + + char buf[80]; + snprintf(buf, sizeof(buf), "test ping\r\n"); + printf("writing data to socket: %s\n", buf); + netconn_write(client, buf, strlen(buf), NETCONN_COPY); + vTaskDelay(1000 / portTICK_RATE_MS); + + netconn_delete(client); + + TEST_FINISH(); +} void user_init(void) { @@ -55,42 +77,5 @@ void user_init(void) IP4_ADDR(&first_client_ip, 172, 16, 0, 2); dhcpserver_start(&first_client_ip, 4); - xTaskCreate(telnetTask, (signed char *)"telnetTask", 512, NULL, 2, NULL); -} - -/* Telnet task listens on port 23, returns some status information and then closes - the connection if you connect to it. -*/ -static void telnetTask(void *pvParameters) -{ - struct netconn *nc = netconn_new (NETCONN_TCP); - if(!nc) { - printf("failed to allocate socket.\n"); - return; - } - netconn_bind(nc, IP_ADDR_ANY, TELNET_PORT); - netconn_listen(nc); - - struct netconn *client = NULL; - err_t err = netconn_accept(nc, &client); - - if ( err != ERR_OK ) { - if(client) - netconn_delete(client); - TEST_FAIL("Error accepting connection"); - } - - ip_addr_t client_addr; - uint16_t port_ignore; - netconn_peer(client, &client_addr, &port_ignore); - - char buf[80]; - snprintf(buf, sizeof(buf), "test ping\r\n"); - printf("writing data to socket: %s\n", buf); - netconn_write(client, buf, strlen(buf), NETCONN_COPY); - vTaskDelay(1000 / portTICK_RATE_MS); - - netconn_delete(client); - - TEST_FINISH(); + xTaskCreate(server_task, (signed char *)"setver_task", 512, NULL, 2, NULL); } diff --git a/tests/basic_wifi/station/station.c b/tests/basic_wifi/station/station.c index fd53801..2629de5 100644 --- a/tests/basic_wifi/station/station.c +++ b/tests/basic_wifi/station/station.c @@ -1,8 +1,6 @@ -/* http_get - Retrieves a web page over HTTP GET. - * - * See http_get_ssl for a TLS-enabled version. - * - * This sample code is in the public domain., +/** + * This test connects to an access point and connects to a server on port 23. + * It waits for a server data and responds with another data. */ #include "espressif/esp_common.h" #include "esp/uart.h" @@ -18,14 +16,12 @@ #include "lwip/netdb.h" #include "lwip/dns.h" -#include "ssid_config.h" - #include "test/test.h" -#define AP_SSID "esp-open-rtos-ap" -#define AP_PSK "esp-open-rtos" -#define SERVER "172.16.0.1" -#define PORT 23 +#define AP_SSID "esp-open-rtos-ap" +#define AP_PSK "esp-open-rtos" +#define SERVER "172.16.0.1" +#define PORT 23 void connect_task(void *pvParameters) { @@ -35,29 +31,25 @@ void connect_task(void *pvParameters) struct sockaddr_in serv_addr; uint8_t buf[256]; - int s = socket(AF_INET, SOCK_STREAM, 0); - if(s < 0) { - printf("failed to allocate socket.\n"); - vTaskDelay(1000 / portTICK_RATE_MS); - continue; + // wait for wifi connection + while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) { + vTaskDelay(100 / portTICK_RATE_MS); } + int s = socket(AF_INET, SOCK_STREAM, 0); + TEST_ASSERT(s > 0, "failed to allocate socket."); + bzero(&serv_addr, sizeof(serv_addr)); - serv_addr.sin_port = htons(23); + serv_addr.sin_port = htons(PORT); serv_addr.sin_family = AF_INET; - if (inet_aton(SERVER, &serv_addr.sin_addr.s_addr) == 0) { - printf("failed to set IP address\n"); - continue; - } + + TEST_ASSERT(inet_aton(SERVER, &serv_addr.sin_addr.s_addr), + "failed to set IP address\n"); printf("allocated socket\n"); - if(connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) != 0) { - close(s); - printf("socket connect failed.\n"); - vTaskDelay(4000 / portTICK_RATE_MS); - continue; - } + TEST_ASSERT(connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr)), + "socket connect failed"); printf("connected\n"); @@ -71,10 +63,9 @@ void connect_task(void *pvParameters) } vTaskDelay(1000 / portTICK_RATE_MS); } - printf("data: %s\n", buf); TEST_CHECK(r > 0, "no data received"); - TEST_CHECK(strcmp((const char*)buf, "test ping\r\n") == 0, + TEST_CHECK(strcmp((const char*)buf, "test ping\r\n") == 0, "received wrong data"); close(s);