|
|
|
@@ -6,7 +6,6 @@
|
|
|
|
|
#include <board.h>
|
|
|
|
|
#include "periph_sdcard.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sdcard_scan.h>
|
|
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include "esp_system.h"
|
|
|
|
@@ -32,15 +31,8 @@ void WebInterface::start() {
|
|
|
|
|
server = start_webserver();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sdcard_url_save_cb(void *user_data, char *url)
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Found file: %s", url);
|
|
|
|
|
/* playlist_operator_handle_t sdcard_handle = (playlist_operator_handle_t)user_data;
|
|
|
|
|
esp_err_t ret = sdcard_list_save(sdcard_handle, url);
|
|
|
|
|
|
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
|
ESP_LOGE(TAG, "Fail to save sdcard url to sdcard playlist");
|
|
|
|
|
} */
|
|
|
|
|
void WebInterface::finishResourceRegistrations() {
|
|
|
|
|
registerFileServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebInterface::initSD() {
|
|
|
|
@@ -53,85 +45,6 @@ void WebInterface::initSD() {
|
|
|
|
|
} else {
|
|
|
|
|
ESP_LOGI(TAG, "SDCard mounted");
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
const char* fileExtensions[1] = {"html"};
|
|
|
|
|
*/
|
|
|
|
|
sdcard_scan(sdcard_url_save_cb, "/sdcard", 5, NULL, 0, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* An HTTP GET handler */
|
|
|
|
|
esp_err_t hello_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 = (char*)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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf_len = httpd_req_get_hdr_value_len(req, "Test-Header-2") + 1;
|
|
|
|
|
if (buf_len > 1) {
|
|
|
|
|
buf = (char*)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 = (char*)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 = (char*)malloc(buf_len);
|
|
|
|
|
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK) {
|
|
|
|
|
ESP_LOGI(TAG, "Found URL query => %s", buf);
|
|
|
|
|
char param[32];
|
|
|
|
|
/* Get value of expected key from query string */
|
|
|
|
|
if (httpd_query_key_value(buf, "query1", param, sizeof(param)) == ESP_OK) {
|
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query1=%s", param);
|
|
|
|
|
}
|
|
|
|
|
if (httpd_query_key_value(buf, "query3", param, sizeof(param)) == ESP_OK) {
|
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query3=%s", param);
|
|
|
|
|
}
|
|
|
|
|
if (httpd_query_key_value(buf, "query2", param, sizeof(param)) == ESP_OK) {
|
|
|
|
|
ESP_LOGI(TAG, "Found URL query parameter => query2=%s", param);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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, "ello orld", 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set HTTP response content type according to file extension */
|
|
|
|
@@ -231,28 +144,31 @@ httpd_handle_t WebInterface::start_webserver(void) {
|
|
|
|
|
config.lru_purge_enable = true;
|
|
|
|
|
config.uri_match_fn = httpd_uri_match_wildcard;
|
|
|
|
|
|
|
|
|
|
httpd_uri_t hello = {
|
|
|
|
|
.uri = "/hello",
|
|
|
|
|
.method = HTTP_GET,
|
|
|
|
|
.handler = hello_get_handler,
|
|
|
|
|
.user_ctx = (void*)"asdff"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Start the httpd server
|
|
|
|
|
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
|
|
|
|
if (httpd_start(&server, &config) == ESP_OK) {
|
|
|
|
|
// Set URI handlers
|
|
|
|
|
ESP_LOGI(TAG, "Registering URI handlers");
|
|
|
|
|
httpd_register_uri_handler(server, &hello);
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Error starting server!");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebInterface::registerResource(httpd_uri_t* config) {
|
|
|
|
|
ESP_LOGI(TAG, "Register resource on path %s", config->uri);
|
|
|
|
|
httpd_register_uri_handler(server, config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebInterface::registerFileServer() {
|
|
|
|
|
rest_server_context_t *fileContext = (rest_server_context_t*)calloc(1, sizeof(rest_server_context_t));
|
|
|
|
|
|
|
|
|
|
if (! fileContext) {
|
|
|
|
|
ESP_LOGE(TAG, "No memory for rest context");
|
|
|
|
|
return NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
strlcpy(fileContext->base_path, "/sdcard", sizeof(fileContext->base_path));
|
|
|
|
|
|
|
|
|
|
strlcpy(fileContext->base_path, "/sdcard/www", sizeof(fileContext->base_path));
|
|
|
|
|
|
|
|
|
|
/* URI handler for getting web server files */
|
|
|
|
|
httpd_uri_t common_get_uri = {
|
|
|
|
@@ -261,12 +177,6 @@ httpd_handle_t WebInterface::start_webserver(void) {
|
|
|
|
|
.handler = rest_common_get_handler,
|
|
|
|
|
.user_ctx = fileContext
|
|
|
|
|
};
|
|
|
|
|
httpd_register_uri_handler(server, &common_get_uri);
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Error starting server!");
|
|
|
|
|
return NULL;
|
|
|
|
|
registerResource(&common_get_uri);
|
|
|
|
|
}
|