2018-11-20 16:42:37 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2018-07-03 10:57:41 +00:00
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_event_loop.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
2018-11-20 16:42:37 +00:00
|
|
|
#include "tcpip_adapter.h"
|
|
|
|
#include "esp_eth.h"
|
|
|
|
#include "protocol_examples_common.h"
|
2018-07-03 10:57:41 +00:00
|
|
|
|
|
|
|
#include "tests.h"
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static const char *TAG = "example";
|
2018-07-03 10:57:41 +00:00
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static void disconnect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
2018-11-20 16:42:37 +00:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server) {
|
|
|
|
ESP_LOGI(TAG, "Stopping webserver");
|
|
|
|
stop_tests(*server);
|
|
|
|
*server = NULL;
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:42:37 +00:00
|
|
|
static void connect_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
2018-11-20 16:42:37 +00:00
|
|
|
httpd_handle_t* server = (httpd_handle_t*) arg;
|
|
|
|
if (*server == NULL) {
|
|
|
|
ESP_LOGI(TAG, "Starting webserver");
|
|
|
|
*server = start_tests();
|
|
|
|
}
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2018-07-03 10:57:41 +00:00
|
|
|
{
|
2018-11-20 16:42:37 +00:00
|
|
|
static httpd_handle_t server = NULL;
|
|
|
|
|
2018-07-03 10:57:41 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2018-11-20 16:42:37 +00:00
|
|
|
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();
|
2018-07-03 10:57:41 +00:00
|
|
|
}
|