2022-03-30 22:10:02 +02:00

115 lines
3.2 KiB
C

#include <stdio.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_http_server.h"
#include "configuration.h"
#include "HTTPServe.h"
esp_err_t start_server(HTTP_serve_config_t config, configuration_data_t* data){
httpd_config_t serv_config = HTTPD_DEFAULT_CONFIG();
serv_config.uri_match_fn = httpd_uri_match_wildcard;
httpd_handle_t server = NULL;
HTTP_serve_config_t* config_copy = malloc(sizeof(HTTP_serve_config_t));
memcpy(config_copy, &config, sizeof(HTTP_serve_config_t));
serv_config.global_user_ctx = config_copy;
httpd_uri_t uri_api_get = {
.uri = config.apiUri,
.method = HTTP_GET,
.handler = api_get_handler,
.user_ctx = data,
};
httpd_uri_t uri_get = {
.uri = config.getUri,
.method = HTTP_GET,
.handler = get_handler,
.user_ctx = NULL,
};
httpd_uri_t uri_post = {
.uri = config.postUri,
.method = HTTP_POST,
.handler = post_handler,
.user_ctx = NULL,
};
esp_err_t status = httpd_start(&server, &serv_config);
if(status == ESP_OK) {
httpd_register_uri_handler(server, &uri_api_get);
httpd_register_uri_handler(server, &uri_get);
httpd_register_uri_handler(server, &uri_post);
}
return status;
}
esp_err_t get_handler(httpd_req_t *req){
HTTP_serve_config_t* config = (HTTP_serve_config_t*)httpd_get_global_user_ctx(req->handle);
char* prefix = config->mountpoint;
const char* uri = req->uri;
bool inRoot = !strcmp(uri,"/");
char filename[strlen(uri) + strlen(prefix) + 1 + (inRoot?10:0)];
strcpy(filename, prefix);
strcat(filename, uri);
if(inRoot)
strcat(filename, "index.html");
const char err404[] = "404 page not found";
ESP_LOGI("HTTP","req : %s %s", prefix, filename);
FILE* f;
if(!(f = fopen(filename,"r"))){
httpd_resp_send(req, err404, HTTPD_RESP_USE_STRLEN);
return ESP_FAIL;
}
if(strstr(uri, ".js") != NULL)
httpd_resp_set_type(req, "application/javascript");
if(strstr(uri, ".css") != NULL)
httpd_resp_set_type(req, "text/css");
char chunk[1024];
size_t chunksize;
do {
chunksize = fread(chunk, 1, sizeof(chunk), f);
if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) {
fclose(f);
return ESP_FAIL;
}
} while (chunksize != 0);
httpd_resp_send_chunk(req, NULL, 0);
fclose(f);
return ESP_OK;
}
esp_err_t post_handler(httpd_req_t *req){
return ESP_OK;
}
esp_err_t api_get_handler(httpd_req_t *req){
size_t instr_len = httpd_req_get_url_query_len(req) + 1;
if(instr_len == 1){
const char * resp_str = "Error no request, you didn't ask for anything so here it is";
httpd_resp_send(req, resp_str, strlen(resp_str));
return ESP_OK;
}
char* instr = malloc(instr_len);
if (!instr) {
ESP_LOGE("api handler", "Not enough memory");
return ESP_ERR_NO_MEM;
}
ESP_ERROR_CHECK(httpd_req_get_url_query_str(req, instr, instr_len));
ESP_ERROR_CHECK(httpd_query_key_value(instr, "req", instr, instr_len));
configuration_data_t* data = (configuration_data_t*)req->user_ctx;
ESP_LOGI("API handler", "req : %s %i", instr, data->measure->co2);
const char* uk_instr_str = "unkwown instruction";
if(!strcmp(instr,"co2")) httpd_resp_send(req, (char*)&data->measure->co2, sizeof((scd4x_data_t *)0)->co2);
else httpd_resp_send(req, uk_instr_str, strlen(uk_instr_str));
free(instr);
return ESP_OK;
}