feat(html) add wifi connection and scan function
This commit is contained in:
parent
c5f46accc3
commit
70b026228a
|
@ -3,10 +3,12 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <esp_compiler.h>
|
#include <esp_compiler.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
|
||||||
|
#define TAG __FILENAME__
|
||||||
|
|
||||||
int api_json_router_init()
|
int api_json_router_init()
|
||||||
{
|
{
|
||||||
api_json_module_dump();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +33,7 @@ int api_json_route(api_json_req_t *req, api_json_module_async_t *rsp)
|
||||||
cmd = cmd_json->valueint;
|
cmd = cmd_json->valueint;
|
||||||
module_id = module_json->valueint;
|
module_id = module_json->valueint;
|
||||||
|
|
||||||
printf("cmd %d received\n", cmd);
|
ESP_LOGI(TAG, "cmd %d received\n", cmd);
|
||||||
|
|
||||||
return api_json_module_call(module_id, cmd, req, rsp);
|
return api_json_module_call(module_id, cmd, req, rsp);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
file(GLOB HTML_FILES
|
||||||
|
*.gz
|
||||||
|
)
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
EMBED_FILES ${HTML_FILES}
|
||||||
|
)
|
Binary file not shown.
Binary file not shown.
|
@ -10,7 +10,7 @@ idf_component_register(
|
||||||
SRCS ${SOURCES}
|
SRCS ${SOURCES}
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
REQUIRES esp_http_server
|
REQUIRES esp_http_server
|
||||||
PRIV_REQUIRES request_runner api_router json static_buffer utils
|
PRIV_REQUIRES request_runner api_router json static_buffer utils html
|
||||||
)
|
)
|
||||||
|
|
||||||
idf_component_set_property(${COMPONENT_NAME} WHOLE_ARCHIVE ON)
|
idf_component_set_property(${COMPONENT_NAME} WHOLE_ARCHIVE ON)
|
||||||
|
|
|
@ -5,68 +5,39 @@
|
||||||
|
|
||||||
#define TAG __FILE_NAME__
|
#define TAG __FILE_NAME__
|
||||||
|
|
||||||
static esp_err_t hello_get_handler(httpd_req_t *req)
|
#define URI_ROOT 0x0000002F /* / */
|
||||||
|
#define URI_WS_DOT 0x2E73772F /* /ws. */
|
||||||
|
|
||||||
|
#define HTML_INDEX "index_html_gz"
|
||||||
|
#define JS_WS_SHARED "ws_sharedworker_js_gz"
|
||||||
|
|
||||||
|
#define SEND_FILE(req, filename) do { \
|
||||||
|
extern const unsigned char filename##_start[] asm("_binary_" filename "_start"); \
|
||||||
|
extern const unsigned char filename##_end[] asm("_binary_" filename "_end"); \
|
||||||
|
const ssize_t file_size = filename##_end - filename##_start; \
|
||||||
|
httpd_resp_send(req, (const char *)filename##_start, file_size); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
static esp_err_t html_base_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t buf_len;
|
size_t buf_len;
|
||||||
|
|
||||||
/* Get header value string length and allocate memory for length + 1,
|
/* this "hash" actually use the first 4 chars as an int32_t "hash" */
|
||||||
* extra byte for null termination */
|
const int *URI_HASH = (const int *)req->uri;
|
||||||
buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1;
|
|
||||||
if (buf_len > 1) {
|
httpd_resp_set_hdr(req, "Connection", "close");
|
||||||
buf = malloc(buf_len);
|
|
||||||
/* Copy null terminated value string into buffer */
|
if (*URI_HASH == URI_WS_DOT) {
|
||||||
if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) {
|
httpd_resp_set_type(req, "text/javascript");
|
||||||
ESP_LOGI(TAG, "Found header => Host: %s", buf);
|
httpd_resp_set_hdr(req, "Content-encoding", "gzip");
|
||||||
}
|
SEND_FILE(req, JS_WS_SHARED);
|
||||||
free(buf);
|
} else {
|
||||||
|
httpd_resp_set_type(req, "text/html");
|
||||||
|
httpd_resp_set_hdr(req, "Content-encoding", "gzip");
|
||||||
|
SEND_FILE(req, HTML_INDEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
|
|
||||||
if (buf_len > 1) {
|
|
||||||
buf = malloc(buf_len);
|
|
||||||
if (httpd_req_get_hdr_value_str(req, "Test-Header-2", buf, buf_len) == ESP_OK) {
|
|
||||||
ESP_LOGI(TAG, "Found header => Test-Header-2: %s", buf);
|
|
||||||
}
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-1") + 1;
|
|
||||||
if (buf_len > 1) {
|
|
||||||
buf = malloc(buf_len);
|
|
||||||
if (httpd_req_get_hdr_value_str(req, "Test-Header-1", buf, buf_len) == ESP_OK) {
|
|
||||||
ESP_LOGI(TAG, "Found header => Test-Header-1: %s", buf);
|
|
||||||
}
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read URL query string length and allocate memory for length + 1,
|
|
||||||
* extra byte for null termination */
|
|
||||||
buf_len = httpd_req_get_url_query_len(req) + 1;
|
|
||||||
if (buf_len > 1) {
|
|
||||||
buf = malloc(buf_len);
|
|
||||||
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
|
|
||||||
ESP_LOGI(TAG, "Found URL query => %s", buf);
|
|
||||||
/* Get value of expected key from query string */
|
|
||||||
ESP_LOGI(TAG, "query: %s", buf);
|
|
||||||
}
|
|
||||||
free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set some custom headers */
|
|
||||||
httpd_resp_set_hdr(req, "Custom-Header-1", "Custom-Value-1");
|
|
||||||
httpd_resp_set_hdr(req, "Custom-Header-2", "Custom-Value-2");
|
|
||||||
|
|
||||||
/* Send response with custom headers and body set as the
|
|
||||||
* string passed in user context*/
|
|
||||||
const char *resp_str = (const char *) req->user_ctx;
|
|
||||||
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);
|
|
||||||
|
|
||||||
/* After sending the HTTP response the old HTTP request
|
|
||||||
* headers are lost. Check if HTTP request headers can be read now. */
|
|
||||||
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
|
|
||||||
ESP_LOGI(TAG, "Request headers lost");
|
|
||||||
}
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,12 +46,9 @@ static esp_err_t hello_get_handler(httpd_req_t *req)
|
||||||
* */
|
* */
|
||||||
|
|
||||||
static const httpd_uri_t hello = {
|
static const httpd_uri_t hello = {
|
||||||
.uri = "/hello",
|
.uri = "/",
|
||||||
.method = HTTP_GET,
|
.method = HTTP_GET,
|
||||||
.handler = hello_get_handler,
|
.handler = html_base_get_handler,
|
||||||
/* Let's pass response string in user
|
|
||||||
* context to demonstrate it's usage */
|
|
||||||
.user_ctx = "Hello World!"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int URI_HTML_BASE_INIT(const httpd_uri_t **uri_conf) {
|
static int URI_HTML_BASE_INIT(const httpd_uri_t **uri_conf) {
|
||||||
|
|
|
@ -44,7 +44,7 @@ static bool uri_match(const char *reference_uri, const char *uri_to_match, size_
|
||||||
/* ref should be shorter than target */
|
/* ref should be shorter than target */
|
||||||
ref_length = strlen(reference_uri);
|
ref_length = strlen(reference_uri);
|
||||||
if (ref_length > match_upto) {
|
if (ref_length > match_upto) {
|
||||||
ESP_LOGI(TAG, "no match length");
|
ESP_LOGI(TAG, "no match length ref %s t: %s", reference_uri, uri_to_match);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ static bool uri_match(const char *reference_uri, const char *uri_to_match, size_
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "fall back false");
|
ESP_LOGI(TAG, "fall back false %s t: %s", reference_uri, uri_to_match);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,8 +107,12 @@ int wifi_api_json_connect(api_json_req_t *req)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "trigger connect\n");
|
int err = wifi_api_connect(ssid, password);
|
||||||
return wifi_api_connect(ssid, password);
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wifi_api_json_sta_get_ap_info(req);
|
||||||
};
|
};
|
||||||
|
|
||||||
int wifi_api_json_disconnect(api_json_req_t *req)
|
int wifi_api_json_disconnect(api_json_req_t *req)
|
||||||
|
|
Loading…
Reference in New Issue