tests: Some clean-up in basic_wifi test
This commit is contained in:
parent
33d695ec83
commit
dfeb437f41
2 changed files with 55 additions and 79 deletions
|
|
@ -1,10 +1,7 @@
|
||||||
/* Very basic example showing usage of access point mode and the DHCP server.
|
/**
|
||||||
|
* This test creates a WiFi access point and listens on port 23 for incomming
|
||||||
The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that
|
* connection. When incomming connection occurs it sends a string and waits for
|
||||||
outputs some status information if you connect to it, then closes
|
* a response.
|
||||||
the connection.
|
|
||||||
|
|
||||||
This example code is in the public domain.
|
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
@ -21,10 +18,35 @@
|
||||||
|
|
||||||
#define AP_SSID "esp-open-rtos-ap"
|
#define AP_SSID "esp-open-rtos-ap"
|
||||||
#define AP_PSK "esp-open-rtos"
|
#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)
|
void user_init(void)
|
||||||
{
|
{
|
||||||
|
|
@ -55,42 +77,5 @@ void user_init(void)
|
||||||
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
||||||
dhcpserver_start(&first_client_ip, 4);
|
dhcpserver_start(&first_client_ip, 4);
|
||||||
|
|
||||||
xTaskCreate(telnetTask, (signed char *)"telnetTask", 512, NULL, 2, NULL);
|
xTaskCreate(server_task, (signed char *)"setver_task", 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();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
/* http_get - Retrieves a web page over HTTP GET.
|
/**
|
||||||
*
|
* This test connects to an access point and connects to a server on port 23.
|
||||||
* See http_get_ssl for a TLS-enabled version.
|
* It waits for a server data and responds with another data.
|
||||||
*
|
|
||||||
* This sample code is in the public domain.,
|
|
||||||
*/
|
*/
|
||||||
#include "espressif/esp_common.h"
|
#include "espressif/esp_common.h"
|
||||||
#include "esp/uart.h"
|
#include "esp/uart.h"
|
||||||
|
|
@ -18,8 +16,6 @@
|
||||||
#include "lwip/netdb.h"
|
#include "lwip/netdb.h"
|
||||||
#include "lwip/dns.h"
|
#include "lwip/dns.h"
|
||||||
|
|
||||||
#include "ssid_config.h"
|
|
||||||
|
|
||||||
#include "test/test.h"
|
#include "test/test.h"
|
||||||
|
|
||||||
#define AP_SSID "esp-open-rtos-ap"
|
#define AP_SSID "esp-open-rtos-ap"
|
||||||
|
|
@ -35,29 +31,25 @@ void connect_task(void *pvParameters)
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
uint8_t buf[256];
|
uint8_t buf[256];
|
||||||
|
|
||||||
int s = socket(AF_INET, SOCK_STREAM, 0);
|
// wait for wifi connection
|
||||||
if(s < 0) {
|
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
|
||||||
printf("failed to allocate socket.\n");
|
vTaskDelay(100 / portTICK_RATE_MS);
|
||||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int s = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
TEST_ASSERT(s > 0, "failed to allocate socket.");
|
||||||
|
|
||||||
bzero(&serv_addr, sizeof(serv_addr));
|
bzero(&serv_addr, sizeof(serv_addr));
|
||||||
serv_addr.sin_port = htons(23);
|
serv_addr.sin_port = htons(PORT);
|
||||||
serv_addr.sin_family = AF_INET;
|
serv_addr.sin_family = AF_INET;
|
||||||
if (inet_aton(SERVER, &serv_addr.sin_addr.s_addr) == 0) {
|
|
||||||
printf("failed to set IP address\n");
|
TEST_ASSERT(inet_aton(SERVER, &serv_addr.sin_addr.s_addr),
|
||||||
continue;
|
"failed to set IP address\n");
|
||||||
}
|
|
||||||
|
|
||||||
printf("allocated socket\n");
|
printf("allocated socket\n");
|
||||||
|
|
||||||
if(connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) != 0) {
|
TEST_ASSERT(connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr)),
|
||||||
close(s);
|
"socket connect failed");
|
||||||
printf("socket connect failed.\n");
|
|
||||||
vTaskDelay(4000 / portTICK_RATE_MS);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("connected\n");
|
printf("connected\n");
|
||||||
|
|
||||||
|
|
@ -71,7 +63,6 @@ void connect_task(void *pvParameters)
|
||||||
}
|
}
|
||||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||||
}
|
}
|
||||||
printf("data: %s\n", buf);
|
|
||||||
TEST_CHECK(r > 0, "no data received");
|
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,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue