Skip to content

Commit

Permalink
Favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
elmot committed May 30, 2024
1 parent c5bae2d commit 2e5f1e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
idf_component_register(SRCS "main.c" "dns_server.c" "as5600.c" "sensors.c" "ip_web.c" "ota/ota.c" "sim.c"
INCLUDE_DIRS "include"
EMBED_FILES root.html setup.html ota/ota.html)
EMBED_FILES root.html setup.html ota/ota.html favicon.ico)
Binary file added main/favicon.ico
Binary file not shown.
20 changes: 18 additions & 2 deletions main/ip_web.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ extern const char root_end[] asm("_binary_root_html_end");
extern const char setup_start[] asm("_binary_setup_html_start");
extern const char setup_end[] asm("_binary_setup_html_end");

extern const char favicon_start[] asm("_binary_favicon_ico_start");
extern const char favicon_end[] asm("_binary_favicon_ico_end");

// HTTP GET Handlers
esp_err_t page_get_handler(httpd_req_t *req, const char *const start, const char *const end) {
const ssize_t root_len = end - start;
ESP_LOGD(TAG_WEB, "Serve url: %s", req->uri);
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, start, root_len);
httpd_resp_send(req, start, end - start);
return ESP_OK;
}

Expand All @@ -32,6 +34,13 @@ static esp_err_t setup_get_handler(httpd_req_t *req) {
return page_get_handler(req, setup_start, setup_end);
}

// ReSharper disable once CppDFAConstantFunctionResult
static esp_err_t favicon_get_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "image/vnd.microsoft.icon");
httpd_resp_send(req, favicon_start, favicon_end - favicon_start);
return ESP_OK;
}

static esp_err_t root_get_handler(httpd_req_t *req) {
httpd_resp_set_status(req, "302 Temporary Redirect");
httpd_resp_set_hdr(req, "Location", "http://yanus.local/wind");
Expand Down Expand Up @@ -120,6 +129,12 @@ static const httpd_uri_t root = {
.handler = root_get_handler
};

static const httpd_uri_t favicon = {
.uri = "/favicon.ico",
.method = HTTP_GET,
.handler = favicon_get_handler
};

static const httpd_uri_t setup = {
.uri = "/setup",
.method = HTTP_GET,
Expand Down Expand Up @@ -159,6 +174,7 @@ esp_err_t http_404_error_handler(httpd_req_t *req, __unused httpd_err_code_t err

void registerHttpHandlers(httpd_handle_t server) {
httpd_register_uri_handler(server, &root);
httpd_register_uri_handler(server, &favicon);
httpd_register_uri_handler(server, &wind);
httpd_register_uri_handler(server, &data);
httpd_register_uri_handler(server, &setup);
Expand Down

0 comments on commit 2e5f1e8

Please sign in to comment.