forked from espressif/esp-idf
Update http_server/simple
example to build for Linux target
This commit is contained in:
@@ -2,9 +2,13 @@
|
|||||||
# in this exact order for cmake to work correctly
|
# in this exact order for cmake to work correctly
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
# (Not part of the boilerplate)
|
if(${IDF_TARGET} STREQUAL "linux")
|
||||||
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/"
|
||||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
"$ENV{IDF_PATH}/examples/protocols/linux_stubs/esp_stubs")
|
||||||
|
set(COMPONENTS main)
|
||||||
|
else()
|
||||||
|
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
endif()
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(simple)
|
project(simple)
|
||||||
|
@@ -1,2 +1,7 @@
|
|||||||
|
set(requires "")
|
||||||
|
if(${IDF_TARGET} STREQUAL "linux")
|
||||||
|
list(APPEND requires esp_stubs esp-tls esp_http_server protocol_examples_common)
|
||||||
|
endif()
|
||||||
idf_component_register(SRCS "main.c"
|
idf_component_register(SRCS "main.c"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS "."
|
||||||
|
REQUIRES ${requires})
|
||||||
|
@@ -7,19 +7,27 @@
|
|||||||
CONDITIONS OF ANY KIND, either express or implied.
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <esp_wifi.h>
|
#include <string.h>
|
||||||
#include <esp_event.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <esp_system.h>
|
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include "nvs_flash.h"
|
|
||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_eth.h"
|
|
||||||
#include "protocol_examples_common.h"
|
#include "protocol_examples_common.h"
|
||||||
#include "protocol_examples_utils.h"
|
#include "protocol_examples_utils.h"
|
||||||
#include "esp_tls_crypto.h"
|
#include "esp_tls_crypto.h"
|
||||||
#include <esp_http_server.h>
|
#include <esp_http_server.h>
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
|
#include "esp_tls.h"
|
||||||
|
|
||||||
|
#if !CONFIG_IDF_TARGET_LINUX
|
||||||
|
#include <esp_wifi.h>
|
||||||
|
#include <esp_system.h>
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_eth.h"
|
||||||
|
#endif // !CONFIG_IDF_TARGET_LINUX
|
||||||
|
|
||||||
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
|
#define EXAMPLE_HTTP_QUERY_KEY_MAX_LEN (64)
|
||||||
|
|
||||||
@@ -40,11 +48,16 @@ typedef struct {
|
|||||||
|
|
||||||
static char *http_auth_basic(const char *username, const char *password)
|
static char *http_auth_basic(const char *username, const char *password)
|
||||||
{
|
{
|
||||||
int out;
|
size_t out;
|
||||||
char *user_info = NULL;
|
char *user_info = NULL;
|
||||||
char *digest = NULL;
|
char *digest = NULL;
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
asprintf(&user_info, "%s:%s", username, password);
|
int rc = asprintf(&user_info, "%s:%s", username, password);
|
||||||
|
if (rc < 0) {
|
||||||
|
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (!user_info) {
|
if (!user_info) {
|
||||||
ESP_LOGE(TAG, "No enough memory for user information");
|
ESP_LOGE(TAG, "No enough memory for user information");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -58,7 +71,7 @@ static char *http_auth_basic(const char *username, const char *password)
|
|||||||
digest = calloc(1, 6 + n + 1);
|
digest = calloc(1, 6 + n + 1);
|
||||||
if (digest) {
|
if (digest) {
|
||||||
strcpy(digest, "Basic ");
|
strcpy(digest, "Basic ");
|
||||||
esp_crypto_base64_encode((unsigned char *)digest + 6, n, (size_t *)&out, (const unsigned char *)user_info, strlen(user_info));
|
esp_crypto_base64_encode((unsigned char *)digest + 6, n, &out, (const unsigned char *)user_info, strlen(user_info));
|
||||||
}
|
}
|
||||||
free(user_info);
|
free(user_info);
|
||||||
return digest;
|
return digest;
|
||||||
@@ -105,7 +118,12 @@ static esp_err_t basic_auth_get_handler(httpd_req_t *req)
|
|||||||
httpd_resp_set_status(req, HTTPD_200);
|
httpd_resp_set_status(req, HTTPD_200);
|
||||||
httpd_resp_set_type(req, "application/json");
|
httpd_resp_set_type(req, "application/json");
|
||||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||||
asprintf(&basic_auth_resp, "{\"authenticated\": true,\"user\": \"%s\"}", basic_auth_info->username);
|
int rc = asprintf(&basic_auth_resp, "{\"authenticated\": true,\"user\": \"%s\"}", basic_auth_info->username);
|
||||||
|
if (rc < 0) {
|
||||||
|
ESP_LOGE(TAG, "asprintf() returned: %d", rc);
|
||||||
|
free(auth_credentials);
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
if (!basic_auth_resp) {
|
if (!basic_auth_resp) {
|
||||||
ESP_LOGE(TAG, "No enough memory for basic authorization response");
|
ESP_LOGE(TAG, "No enough memory for basic authorization response");
|
||||||
free(auth_credentials);
|
free(auth_credentials);
|
||||||
@@ -351,6 +369,13 @@ static httpd_handle_t start_webserver(void)
|
|||||||
{
|
{
|
||||||
httpd_handle_t server = NULL;
|
httpd_handle_t server = NULL;
|
||||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||||
|
#if CONFIG_IDF_TARGET_LINUX
|
||||||
|
// Setting port as 8001 when building for Linux. Port 80 can be used only by a priviliged user in linux.
|
||||||
|
// So when a unpriviliged user tries to run the application, it throws bind error and the server is not started.
|
||||||
|
// Port 8001 can be used by an unpriviliged user as well. So the application will not throw bind error and the
|
||||||
|
// server will be started.
|
||||||
|
config.server_port = 8001;
|
||||||
|
#endif // !CONFIG_IDF_TARGET_LINUX
|
||||||
config.lru_purge_enable = true;
|
config.lru_purge_enable = true;
|
||||||
|
|
||||||
// Start the httpd server
|
// Start the httpd server
|
||||||
@@ -371,6 +396,7 @@ static httpd_handle_t start_webserver(void)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !CONFIG_IDF_TARGET_LINUX
|
||||||
static esp_err_t stop_webserver(httpd_handle_t server)
|
static esp_err_t stop_webserver(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
// Stop the httpd server
|
// Stop the httpd server
|
||||||
@@ -400,7 +426,7 @@ static void connect_handler(void* arg, esp_event_base_t event_base,
|
|||||||
*server = start_webserver();
|
*server = start_webserver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif // !CONFIG_IDF_TARGET_LINUX
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
@@ -419,6 +445,7 @@ void app_main(void)
|
|||||||
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
/* Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
|
||||||
* and re-start it upon connection.
|
* and re-start it upon connection.
|
||||||
*/
|
*/
|
||||||
|
#if !CONFIG_IDF_TARGET_LINUX
|
||||||
#ifdef CONFIG_EXAMPLE_CONNECT_WIFI
|
#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(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));
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
|
||||||
@@ -427,7 +454,12 @@ void app_main(void)
|
|||||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
|
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));
|
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
|
||||||
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
|
||||||
|
#endif // !CONFIG_IDF_TARGET_LINUX
|
||||||
|
|
||||||
/* Start the server for the first time */
|
/* Start the server for the first time */
|
||||||
server = start_webserver();
|
server = start_webserver();
|
||||||
|
|
||||||
|
while (server) {
|
||||||
|
sleep(5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user