examples/protocols/http(s)_server: use common network component

This commit is contained in:
Ivan Grokhotkov
2018-11-21 00:42:37 +08:00
committed by bot
parent 6548afcf49
commit a5b0f5d6ed
27 changed files with 272 additions and 424 deletions

View File

@@ -2,7 +2,9 @@
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(tests)
target_include_directories(tests.elf PRIVATE main/include)

View File

@@ -5,5 +5,7 @@
PROJECT_NAME := tests
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
include $(IDF_PATH)/make/project.mk

View File

@@ -66,7 +66,7 @@ def test_examples_protocol_http_server_advanced(env, extra_data):
# Parse IP address of STA
Utility.console_log("Waiting to connect with AP")
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=30)[0]
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Started HTTP server on port: '(\d+)'"), timeout=15)[0]
result = dut1.expect(re.compile(r"(?:[\s\S]*)Max URI handlers: '(\d+)'(?:[\s\S]*)Max Open Sessions: " # noqa: W605

View File

@@ -1,16 +0,0 @@
menu "Example Configuration"
config WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
Can be left blank if the network has no security set.
endmenu

View File

@@ -1,81 +1,72 @@
/* HTTP Server Tests
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include "esp_eth.h"
#include "protocol_examples_common.h"
#include "tests.h"
/* The examples use simple WiFi configuration that you can set via
'make menuconfig'.
static const char *TAG = "example";
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
static const char *TAG="TEST_WIFI";
static esp_err_t event_handler(void *ctx, system_event_t *event)
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
httpd_handle_t *hd = (httpd_handle_t *) ctx;
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
ESP_ERROR_CHECK(esp_wifi_connect());
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
ESP_LOGI(TAG, "Got IP: '%s'",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
// Start webserver tests
if (*hd == NULL) {
*hd = start_tests();
}
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
ESP_ERROR_CHECK(esp_wifi_connect());
// Stop webserver tests
if (*hd) {
stop_tests(*hd);
*hd = NULL;
}
break;
default:
break;
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server) {
ESP_LOGI(TAG, "Stopping webserver");
stop_tests(*server);
*server = NULL;
}
return ESP_OK;
}
static void initialise_wifi(void)
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
tcpip_adapter_init();
static httpd_handle_t hd = NULL;
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, &hd));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
ESP_LOGI(TAG, "Starting webserver");
*server = start_tests();
}
}
void app_main()
{
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init());
initialise_wifi();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
* and re-start it upon connection.
*/
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
#ifdef CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
/* Start the server for the first time */
server = start_tests();
}

View File

@@ -7,10 +7,9 @@
#include "tests.h"
static const char *TAG="TESTS";
static const char *TAG = "TESTS";
int pre_start_mem, post_stop_mem, post_stop_min_mem;
bool basic_sanity = true;
static int pre_start_mem, post_stop_mem;
struct async_resp_arg {
httpd_handle_t hd;
@@ -19,7 +18,7 @@ struct async_resp_arg {
/********************* Basic Handlers Start *******************/
esp_err_t hello_get_handler(httpd_req_t *req)
static esp_err_t hello_get_handler(httpd_req_t *req)
{
#define STR "Hello World!"
ESP_LOGI(TAG, "Free Stack for server task: '%d'", uxTaskGetStackHighWaterMark(NULL));
@@ -28,7 +27,7 @@ esp_err_t hello_get_handler(httpd_req_t *req)
#undef STR
}
esp_err_t hello_type_get_handler(httpd_req_t *req)
static esp_err_t hello_type_get_handler(httpd_req_t *req)
{
#define STR "Hello World!"
httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
@@ -37,7 +36,7 @@ esp_err_t hello_type_get_handler(httpd_req_t *req)
#undef STR
}
esp_err_t hello_status_get_handler(httpd_req_t *req)
static esp_err_t hello_status_get_handler(httpd_req_t *req)
{
#define STR "Hello World!"
httpd_resp_set_status(req, HTTPD_500);
@@ -46,7 +45,7 @@ esp_err_t hello_status_get_handler(httpd_req_t *req)
#undef STR
}
esp_err_t echo_post_handler(httpd_req_t *req)
static esp_err_t echo_post_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "/echo handler read content length %d", req->content_len);
@@ -101,7 +100,7 @@ esp_err_t echo_post_handler(httpd_req_t *req)
return ESP_OK;
}
void adder_free_func(void *ctx)
static void adder_free_func(void *ctx)
{
ESP_LOGI(TAG, "Custom Free Context function called");
free(ctx);
@@ -110,7 +109,7 @@ void adder_free_func(void *ctx)
/* Create a context, keep incrementing value in the context, by whatever was
* received. Return the result
*/
esp_err_t adder_post_handler(httpd_req_t *req)
static esp_err_t adder_post_handler(httpd_req_t *req)
{
char buf[10];
char outbuf[50];
@@ -143,7 +142,7 @@ esp_err_t adder_post_handler(httpd_req_t *req)
return ESP_OK;
}
esp_err_t leftover_data_post_handler(httpd_req_t *req)
static esp_err_t leftover_data_post_handler(httpd_req_t *req)
{
/* Only echo the first 10 bytes of the request, leaving the rest of the
* request data as is.
@@ -166,8 +165,9 @@ esp_err_t leftover_data_post_handler(httpd_req_t *req)
return ESP_OK;
}
int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, unsigned buf_len, int flags);
void generate_async_resp(void *arg)
extern int httpd_default_send(httpd_handle_t hd, int sockfd, const char *buf, unsigned buf_len, int flags);
static void generate_async_resp(void *arg)
{
char buf[250];
struct async_resp_arg *resp_arg = (struct async_resp_arg *)arg;
@@ -190,7 +190,7 @@ void generate_async_resp(void *arg)
free(arg);
}
esp_err_t async_get_handler(httpd_req_t *req)
static esp_err_t async_get_handler(httpd_req_t *req)
{
#define STR "Hello World!"
httpd_resp_send(req, STR, strlen(STR));
@@ -211,7 +211,7 @@ esp_err_t async_get_handler(httpd_req_t *req)
}
httpd_uri_t basic_handlers[] = {
static const httpd_uri_t basic_handlers[] = {
{ .uri = "/hello/type_html",
.method = HTTP_GET,
.handler = hello_type_get_handler,
@@ -254,8 +254,9 @@ httpd_uri_t basic_handlers[] = {
}
};
int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
void register_basic_handlers(httpd_handle_t hd)
static const int basic_handlers_no = sizeof(basic_handlers)/sizeof(httpd_uri_t);
static void register_basic_handlers(httpd_handle_t hd)
{
int i;
ESP_LOGI(TAG, "Registering basic handlers");
@@ -269,7 +270,7 @@ void register_basic_handlers(httpd_handle_t hd)
ESP_LOGI(TAG, "Success");
}
httpd_handle_t test_httpd_start()
static httpd_handle_t test_httpd_start()
{
pre_start_mem = esp_get_free_heap_size();
httpd_handle_t hd;
@@ -291,7 +292,7 @@ httpd_handle_t test_httpd_start()
return NULL;
}
void test_httpd_stop(httpd_handle_t hd)
static void test_httpd_stop(httpd_handle_t hd)
{
httpd_stop(hd);
post_stop_mem = esp_get_free_heap_size();