diff --git a/project_components/api_router/api_json_router.c b/project_components/api_router/api_json_router.c index f2132fb..f386bb9 100644 --- a/project_components/api_router/api_json_router.c +++ b/project_components/api_router/api_json_router.c @@ -3,10 +3,12 @@ #include #include #include +#include + +#define TAG __FILENAME__ int api_json_router_init() { - api_json_module_dump(); 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; 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); } diff --git a/project_components/html/CMakeLists.txt b/project_components/html/CMakeLists.txt new file mode 100644 index 0000000..cc52de5 --- /dev/null +++ b/project_components/html/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB HTML_FILES + *.gz + ) + +idf_component_register( + EMBED_FILES ${HTML_FILES} +) diff --git a/project_components/html/index.html.gz b/project_components/html/index.html.gz new file mode 100644 index 0000000..f2ba79e Binary files /dev/null and b/project_components/html/index.html.gz differ diff --git a/project_components/html/ws.sharedworker.js.gz b/project_components/html/ws.sharedworker.js.gz new file mode 100644 index 0000000..c53f1ab Binary files /dev/null and b/project_components/html/ws.sharedworker.js.gz differ diff --git a/project_components/web_server/CMakeLists.txt b/project_components/web_server/CMakeLists.txt index 0bee70e..08f8054 100644 --- a/project_components/web_server/CMakeLists.txt +++ b/project_components/web_server/CMakeLists.txt @@ -10,7 +10,7 @@ idf_component_register( SRCS ${SOURCES} INCLUDE_DIRS "." 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) diff --git a/project_components/web_server/uri_modules/uri_html_base.c b/project_components/web_server/uri_modules/uri_html_base.c index 5d8d02c..0c9bfe9 100644 --- a/project_components/web_server/uri_modules/uri_html_base.c +++ b/project_components/web_server/uri_modules/uri_html_base.c @@ -5,68 +5,39 @@ #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; size_t buf_len; - /* Get header value string length and allocate memory for length + 1, - * extra byte for null termination */ - buf_len = httpd_req_get_hdr_value_len(req, "Host") + 1; - if (buf_len > 1) { - buf = malloc(buf_len); - /* Copy null terminated value string into buffer */ - if (httpd_req_get_hdr_value_str(req, "Host", buf, buf_len) == ESP_OK) { - ESP_LOGI(TAG, "Found header => Host: %s", buf); - } - free(buf); + /* this "hash" actually use the first 4 chars as an int32_t "hash" */ + const int *URI_HASH = (const int *)req->uri; + + httpd_resp_set_hdr(req, "Connection", "close"); + + if (*URI_HASH == URI_WS_DOT) { + httpd_resp_set_type(req, "text/javascript"); + httpd_resp_set_hdr(req, "Content-encoding", "gzip"); + SEND_FILE(req, JS_WS_SHARED); + } 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; } @@ -75,12 +46,9 @@ static esp_err_t hello_get_handler(httpd_req_t *req) * */ static const httpd_uri_t hello = { - .uri = "/hello", + .uri = "/", .method = HTTP_GET, - .handler = hello_get_handler, - /* Let's pass response string in user - * context to demonstrate it's usage */ - .user_ctx = "Hello World!" + .handler = html_base_get_handler, }; static int URI_HTML_BASE_INIT(const httpd_uri_t **uri_conf) { diff --git a/project_components/web_server/web_server.c b/project_components/web_server/web_server.c index 1ba7723..a94afea 100644 --- a/project_components/web_server/web_server.c +++ b/project_components/web_server/web_server.c @@ -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_length = strlen(reference_uri); 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; } @@ -74,7 +74,7 @@ static bool uri_match(const char *reference_uri, const char *uri_to_match, size_ return true; } - ESP_LOGI(TAG, "fall back false"); + ESP_LOGI(TAG, "fall back false %s t: %s", reference_uri, uri_to_match); return false; } diff --git a/project_components/wifi_manager/wifi_api_json.c b/project_components/wifi_manager/wifi_api_json.c index b341021..522ccc7 100644 --- a/project_components/wifi_manager/wifi_api_json.c +++ b/project_components/wifi_manager/wifi_api_json.c @@ -107,8 +107,12 @@ int wifi_api_json_connect(api_json_req_t *req) return 1; } - ESP_LOGI(TAG, "trigger connect\n"); - return wifi_api_connect(ssid, password); + int err = 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)