Cleanup WebInterface and make RFIDInterface reachable.

This commit is contained in:
Simon Weidacher
2021-12-26 14:00:30 +01:00
parent 1788c9e5aa
commit ad2499506a
5 changed files with 146 additions and 211 deletions

View File

@@ -1,6 +1,10 @@
#include "RFIDInterface.hpp"
#include <esp_log.h>
#include <esp_err.h>
#include <esp_http_server.h>
#include "WebInterface.hpp"
static const char* TAG = "rfid";
@@ -24,3 +28,16 @@ void RFIDInterface::start() {
rc522_start(startConfig);
}
void RFIDInterface::registerWebResources(WebInterface* interface) {
httpd_uri_t config = {
.uri = "/rfid",
.method = HTTP_GET,
.handler = [](httpd_req_t *req) {
httpd_resp_send(req, "it's all fine!", HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
};
interface->registerResource(&config);
}

View File

@@ -2,6 +2,7 @@
#define _RFID_INTERFACE_HPP
#include <rc522.h>
#include "WebInterface.hpp"
class RFIDInterface {
rc522_start_args_t startConfig;
@@ -9,6 +10,7 @@ class RFIDInterface {
public:
RFIDInterface();
void start();
void registerWebResources(WebInterface* interface);
};
#endif

View File

@@ -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);
}

View File

@@ -9,10 +9,13 @@ class WebInterface {
public:
WebInterface();
void start();
void finishResourceRegistrations();
void registerResource(httpd_uri_t* config);
private:
void initSD();
httpd_handle_t start_webserver();
void registerFileServer();
//esp_err_t hello_get_handler(httpd_req_t *req);
};

View File

@@ -22,4 +22,7 @@ void app_main(void) {
networking.connectWifi();
webInterface.start();
rfid.start();
rfid.registerWebResources(&webInterface);
webInterface.finishResourceRegistrations();
}