Skip to content

Commit

Permalink
Changed to single hostname for device. Small changes to http-server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Anforowicz committed Sep 9, 2024
1 parent 9ca74ba commit babee43
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 181 deletions.
17 changes: 11 additions & 6 deletions main/driver_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ esp_err_t driver_setup_ethernet(const esp_netif_ip_info_t *ip_info,
esp_eth_netif_glue_handle_t eth_netif_glue =
esp_eth_new_netif_glue(eth_handle);

if (eth_netif_glue == NULL) {
ESP_LOGE(TAG, "Couldn't create eth netif glue");
return ESP_FAIL;
}

err = esp_netif_attach(esp_netif, eth_netif_glue);
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't attach ethernet to ESP netif.");

Expand All @@ -134,7 +139,7 @@ esp_err_t driver_setup_ethernet(const esp_netif_ip_info_t *ip_info,
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't start ethernet.");

/// Wait for internet to actually work ////
if (xSemaphoreTake(internet_ready, pdMS_TO_TICKS(10000)) == pdFALSE) {
if (xSemaphoreTake(internet_ready, pdMS_TO_TICKS(10000)) != pdTRUE) {
vSemaphoreDelete(internet_ready);
ESP_LOGE(TAG, "Couldn't start ethernet driver in 10 seconds.");
return ESP_FAIL;
Expand Down Expand Up @@ -212,7 +217,7 @@ esp_err_t driver_setup_wifi(const esp_netif_ip_info_t *ip_info,
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't connect to Wi-Fi.");

//// Wait for internet to actually work ////
if (xSemaphoreTake(internet_ready, pdMS_TO_TICKS(10000)) == pdFALSE) {
if (xSemaphoreTake(internet_ready, pdMS_TO_TICKS(10000)) != pdTRUE) {
vSemaphoreDelete(internet_ready);
ESP_LOGE(TAG, "Couldn't start WIFI driver in 10 seconds.");
return ESP_FAIL;
Expand All @@ -236,8 +241,8 @@ esp_err_t driver_setup_can(const twai_timing_config_t *timing_config) {
// Change this line to enable/disable logging:
g_config.alerts_enabled = TWAI_ALERT_AND_LOG | TWAI_ALERT_ABOVE_ERR_WARN |
TWAI_ALERT_BUS_OFF | TWAI_ALERT_BUS_RECOVERED;
g_config.tx_queue_len = 64;
g_config.rx_queue_len = 64;
g_config.tx_queue_len = 32;
g_config.rx_queue_len = 32;
twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL();

// Install TWAI driver
Expand Down Expand Up @@ -273,7 +278,7 @@ static void ethernet_event_handler(void *arg, esp_event_base_t event_base,
break;
case ETHERNET_EVENT_START:
ESP_LOGD(TAG, "Ethernet Started");
xSemaphoreGive(internet_ready);
assert(xSemaphoreGive(internet_ready) == pdTRUE);
break;
case ETHERNET_EVENT_STOP:
ESP_LOGE(TAG, "Ethernet Stopped");
Expand All @@ -290,7 +295,7 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base,
case WIFI_EVENT_STA_START:
ESP_LOGD(TAG, "WIFI station started. Connecting...");
esp_wifi_connect();
xSemaphoreGive(internet_ready);
assert(xSemaphoreGive(internet_ready) == pdTRUE);
break;
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGD(TAG, "WIFI station connected.");
Expand Down
137 changes: 63 additions & 74 deletions main/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ static const httpd_uri_t get_alpine_js_handler = {
.method = HTTP_GET,
.user_ctx = NULL};

// GET /api/status
static esp_err_t serve_get_api_status(httpd_req_t *req);
static const httpd_uri_t get_api_status_handler = {
.uri = "/api/status",
.handler = serve_get_api_status,
.method = HTTP_GET,
.user_ctx = NULL};

// GET /api/config
static esp_err_t serve_get_api_config(httpd_req_t *req) {
esp_err_t err = httpd_resp_set_type(req, "application/json");
Expand All @@ -81,6 +73,14 @@ static const httpd_uri_t get_api_config_handler = {
.method = HTTP_GET,
.user_ctx = NULL};

// GET /api/status
static esp_err_t serve_get_api_status(httpd_req_t *req);
static const httpd_uri_t get_api_status_handler = {
.uri = "/api/status",
.handler = serve_get_api_status,
.method = HTTP_GET,
.user_ctx = NULL};

// POST /api/config
static esp_err_t serve_post_api_config(httpd_req_t *req);
static const httpd_uri_t post_api_config_handler = {
Expand All @@ -89,16 +89,14 @@ static const httpd_uri_t post_api_config_handler = {
.method = HTTP_POST,
.user_ctx = NULL};

// Updates `persistent_settings` from `json`.
// `persistent_settings` must be the current settings,
// and they are changed to updated settings based on `json`.
// tmp_arg_buf must point to at least 256 bytes of memory that this
// function can temporarily use.
// On success, restarts the microcontroller.
// Updates `settings_to_update` with any updated values from `json`.
// `tmp_arg_buf` is a buffer temporarily used by this function.
// An error may be returned if `arg_buf_size` is under 256 bytes.
// On success, `settings_to_update` will hold the updated settings.
// On failure, returns an error.
static esp_err_t update_persistent_settings_from_json(
const char *json, char *tmp_arg_buf,
persistent_settings_t *persistent_settings);
const char *json, char *tmp_arg_buf, size_t arg_buf_size,
persistent_settings_t *settings_to_update);

esp_err_t start_http_server(void) {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
Expand All @@ -120,10 +118,10 @@ esp_err_t start_http_server(void) {
err = httpd_register_uri_handler(server, &get_alpine_js_handler);
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't register HTTP URI handler.");

err = httpd_register_uri_handler(server, &get_api_status_handler);
err = httpd_register_uri_handler(server, &get_api_config_handler);
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't register HTTP URI handler.");

err = httpd_register_uri_handler(server, &get_api_config_handler);
err = httpd_register_uri_handler(server, &get_api_status_handler);
ESP_RETURN_ON_ERROR(err, TAG, "Couldn't register HTTP URI handler.");

err = httpd_register_uri_handler(server, &post_api_config_handler);
Expand Down Expand Up @@ -158,94 +156,95 @@ static esp_err_t serve_get_api_status(httpd_req_t *req) {

// Temporarily stores the body of
// the request in `serve_post_api_config()`.
static char post_buf[2048];
static char tmp_arg_buf[256];
static char shared_post_buf[2048];
static char shared_tmp_arg_buf[256];
SemaphoreHandle_t post_buf_mutex = NULL;
StaticSemaphore_t post_buf_mutex_mem;

static esp_err_t serve_post_api_config(httpd_req_t *req) {
// If POST request is too large
if (req->content_len >= sizeof(post_buf)) {
httpd_resp_send_err(req, 500, "POST post_buf too long.");
ESP_LOGE(TAG, "POST post_buf too long.");
if (req->content_len >= sizeof(shared_post_buf)) {
httpd_resp_send_err(req, 500, "POST request payload is too long.");
ESP_LOGE(TAG, "POST request payload was too long.");
return ESP_FAIL;
}

// Ensure we're the only ones using `post_buf`.
if (post_buf_mutex == NULL) {
post_buf_mutex = xSemaphoreCreateMutexStatic(&post_buf_mutex_mem);
}
xSemaphoreTake(post_buf_mutex, portMAX_DELAY);
assert(xSemaphoreTake(post_buf_mutex, portMAX_DELAY) == pdTRUE);

// Read the post request post_buf
int ret = httpd_req_recv(req, post_buf, req->content_len);
int ret = httpd_req_recv(req, shared_post_buf, req->content_len);

// If unexpected post_buf length
if (ret != req->content_len) {
xSemaphoreGive(post_buf_mutex);
assert(xSemaphoreGive(post_buf_mutex) == pdTRUE);
httpd_resp_send_err(req, 500, "Couldn't read POST post_buf");
ESP_LOGE(TAG, "Couldn't read POST post_buf.");
return ret;
}

post_buf[req->content_len] = '\0';
// null-terminate as a precaution
shared_post_buf[req->content_len] = '\0';

// Parse the posted JSON
persistent_settings_t new_persistent_settings = *persistent_settings;
esp_err_t err = update_persistent_settings_from_json(
post_buf, tmp_arg_buf, &new_persistent_settings);
shared_post_buf, shared_tmp_arg_buf, sizeof(shared_tmp_arg_buf),
&new_persistent_settings);

xSemaphoreGive(post_buf_mutex);
assert(xSemaphoreGive(post_buf_mutex) == pdTRUE);

// If couldn't parse the posted JSON
if (err != ESP_OK) {
ESP_LOGE(TAG, "Error parsing POSTed persistent settings JSON: %s",
esp_err_to_name(err));
httpd_resp_send_err(req, 400,
"Couldn't parse the given settings. Make sure they're "
"Received invalid settings. Make sure they're "
"formatted correctly!");
return ESP_FAIL;
}

// Save the new configuration

httpd_resp_send(req, "Updating settings and restarting adapter...",
HTTPD_RESP_USE_STRLEN);

err = persistent_settings_save(&new_persistent_settings);

if (err != ESP_OK) {
if (err == ESP_OK) {
err = httpd_resp_send(req, "Updating settings and restarting adapter...",
HTTPD_RESP_USE_STRLEN);
ESP_LOGI(TAG, "Restarting ESP32 to enact updated settings.");
esp_restart();
return err;

} else {
ESP_LOGE(TAG, "Couldn't save persistent settings.");
return ESP_FAIL;
err = httpd_resp_send_err(
req, 500, "Internal error: Couldn't save persistent settings.");
return err;
}

return ESP_OK;
}

static esp_err_t update_persistent_settings_from_json(
const char *json, char *tmp_arg_buf,
persistent_settings_t *persistent_settings) {
const char *json, char *tmp_arg_buf, size_t arg_buf_size,
persistent_settings_t *settings_to_update) {
// rebind for brevity
persistent_settings_t *cnf = persistent_settings;
persistent_settings_t *cnf = settings_to_update;
char *arg_buf = tmp_arg_buf;
esp_err_t err;

// `tmp_arg_buf` must be at least this size
// according to this function's doc comment.
size_t arg_buf_size = 256;

// read eth_hostname field
memset(arg_buf, 0, sizeof(cnf->eth_hostname));
err = httpd_query_key_value(post_buf, "eth_hostname", arg_buf,
sizeof(cnf->eth_hostname));
// read hostname field
memset(arg_buf, 0, sizeof(cnf->hostname));
err = httpd_query_key_value(json, "hostname", arg_buf, sizeof(cnf->hostname));
if (err == ESP_OK) {
memcpy(cnf->eth_hostname, arg_buf, sizeof(cnf->eth_hostname));
memcpy(cnf->hostname, arg_buf, sizeof(cnf->hostname));
} else if (err != ESP_ERR_NOT_FOUND) {
return err;
}

// read eth_use_dhcp field
err = httpd_query_key_value(post_buf, "eth_use_dhcp", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "eth_use_dhcp", arg_buf, arg_buf_size);
if (err == ESP_OK) {
if (strncasecmp(arg_buf, "true", 4) == 0)
cnf->eth_use_dhcp = true;
Expand All @@ -256,7 +255,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read eth_ip field
err = httpd_query_key_value(post_buf, "eth_ip", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "eth_ip", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->eth_ip_info.ip);
if (err != ESP_OK) {
Expand All @@ -267,7 +266,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read eth_netmask field
err = httpd_query_key_value(post_buf, "eth_netmask", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "eth_netmask", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->eth_ip_info.netmask);
if (err != ESP_OK) {
Expand All @@ -278,7 +277,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read eth_gw field
err = httpd_query_key_value(post_buf, "eth_gw", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "eth_gw", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->eth_ip_info.gw);
if (err != ESP_OK) {
Expand All @@ -289,7 +288,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read wifi_enabled field
err = httpd_query_key_value(post_buf, "wifi_enabled", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "wifi_enabled", arg_buf, arg_buf_size);
if (err == ESP_OK) {
if (strncasecmp(arg_buf, "true", 4) == 0)
cnf->wifi_enabled = true;
Expand All @@ -301,8 +300,8 @@ static esp_err_t update_persistent_settings_from_json(

// read wifi_ssid field
memset(arg_buf, 0, sizeof(cnf->wifi_ssid));
err = httpd_query_key_value(post_buf, "wifi_ssid", arg_buf,
sizeof(cnf->wifi_ssid));
err =
httpd_query_key_value(json, "wifi_ssid", arg_buf, sizeof(cnf->wifi_ssid));
if (err == ESP_OK) {
memcpy(cnf->wifi_ssid, arg_buf, sizeof(cnf->wifi_ssid));
} else if (err != ESP_ERR_NOT_FOUND) {
Expand All @@ -311,26 +310,16 @@ static esp_err_t update_persistent_settings_from_json(

// read wifi_pass field
memset(arg_buf, 0, sizeof(cnf->wifi_pass));
err = httpd_query_key_value(post_buf, "wifi_pass", arg_buf,
sizeof(cnf->wifi_pass));
err =
httpd_query_key_value(json, "wifi_pass", arg_buf, sizeof(cnf->wifi_pass));
if (err == ESP_OK) {
memcpy(cnf->wifi_pass, arg_buf, sizeof(cnf->wifi_pass));
} else if (err != ESP_ERR_NOT_FOUND) {
return err;
}

// read wifi_hostname field
memset(arg_buf, 0, sizeof(cnf->wifi_hostname));
err = httpd_query_key_value(post_buf, "wifi_hostname", arg_buf,
sizeof(cnf->wifi_hostname));
if (err == ESP_OK) {
memcpy(cnf->wifi_hostname, arg_buf, sizeof(cnf->wifi_hostname));
} else if (err != ESP_ERR_NOT_FOUND) {
return err;
}

// read wifi_use_dhcp field
err = httpd_query_key_value(post_buf, "wifi_use_dhcp", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "wifi_use_dhcp", arg_buf, arg_buf_size);
if (err == ESP_OK) {
if (strncasecmp(arg_buf, "true", 4) == 0)
cnf->wifi_use_dhcp = true;
Expand All @@ -341,7 +330,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read wifi_ip field
err = httpd_query_key_value(post_buf, "wifi_ip", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "wifi_ip", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->wifi_ip_info.ip);
if (err != ESP_OK) {
Expand All @@ -352,7 +341,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read wifi_netmask field
err = httpd_query_key_value(post_buf, "wifi_netmask", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "wifi_netmask", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->wifi_ip_info.netmask);
if (err != ESP_OK) {
Expand All @@ -363,7 +352,7 @@ static esp_err_t update_persistent_settings_from_json(
}

// read wifi_gw field
err = httpd_query_key_value(post_buf, "wifi_gw", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "wifi_gw", arg_buf, arg_buf_size);
if (err == ESP_OK) {
err = esp_netif_str_to_ip4(arg_buf, &cnf->wifi_ip_info.gw);
if (err != ESP_OK) {
Expand All @@ -374,9 +363,9 @@ static esp_err_t update_persistent_settings_from_json(
}

// read can_bitrate field
err = httpd_query_key_value(post_buf, "can_bitrate", arg_buf, arg_buf_size);
err = httpd_query_key_value(json, "can_bitrate", arg_buf, arg_buf_size);
if (err == ESP_OK) {
long num = strtol(arg_buf, NULL, 10);
uint32_t num = strtol(arg_buf, NULL, 10);

enum can_bitrate_setting bitrate = (enum can_bitrate_setting)num;

Expand Down
2 changes: 1 addition & 1 deletion main/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#include "esp_http_server.h"

// Starts an HTTP server on port 80.
// Servers an info screen with config options.
// Serves an info screen with config options.
// May only be called once.
esp_err_t start_http_server(void);
Loading

0 comments on commit babee43

Please sign in to comment.