Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commita2ecddfd0351b6c53b3cb9d5f8d88743924d9e01 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-05-28 15:50:35 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-05-28 15:50:35 +0300
Files0e5525188882a0052a4bf578d2a3973dc645a16f
uni-projects-a2ecddfd0351b6c53b3cb9d5f8d88743924d9e01.tar
uni-projects-a2ecddfd0351b6c53b3cb9d5f8d88743924d9e01.tar.gz
uni-projects-a2ecddfd0351b6c53b3cb9d5f8d88743924d9e01.zip
Parent6270409b28e06b34a9e2e911843d7feca2e68b79 (diff)
[iot/goam] Moved configuration forms from having an address for each to a global /config address
diff --git a/Year 3/Internet of things/goam/main/main.c b/Year 3/Internet of things/goam/main/main.c
index b785efe..3f2cf6a 100644
--- a/Year 3/Internet of things/goam/main/main.c
+++ b/Year 3/Internet of things/goam/main/main.c
@@ -76,7 +76,7 @@ static void display_imagesave() {
CONFIG_SAVEIMAGE_COLUMN);
}
-void main_store_saveimage(int32_t value) {
+void main_store_imagesave(int32_t value) {
if (value < 0 && value > BOTH) {
ESP_LOGW(TAG, "Invalid value for saveimage was passed, not assigning");
return;
diff --git a/Year 3/Internet of things/goam/main/main.h b/Year 3/Internet of things/goam/main/main.h
index ec8f1af..765d1b5 100644
--- a/Year 3/Internet of things/goam/main/main.h
+++ b/Year 3/Internet of things/goam/main/main.h
@@ -1,6 +1,6 @@
#pragma once
-void main_store_saveimage(int32_t value);
+void main_store_imagesave(int32_t value);
void main_store_resolution(int32_t value);
void main_store_invertion(int32_t value);
diff --git a/Year 3/Internet of things/goam/main/webserver.c b/Year 3/Internet of things/goam/main/webserver.c
index 1c23867..fe6861d 100644
--- a/Year 3/Internet of things/goam/main/webserver.c
+++ b/Year 3/Internet of things/goam/main/webserver.c
@@ -23,6 +23,22 @@ static const char *TAG = "webserver";
static httpd_handle_t server = NULL;
/*
+ * Helpers
+ */
+
+static char* get_value(char* buffer, const char* key, size_t key_size) {
+ // Doesn't handle if key can be found in a value, but thats fine for our cases
+ char* start = strstr(buffer, key);
+ if (start == NULL) return NULL;
+
+ start += key_size + 1;
+ char* end = strchr(start, '&');
+ if (end == NULL) return start;
+ *end = '\1';
+ return start;
+}
+
+/*
* Page handlers
*/
@@ -37,6 +53,17 @@ static void logi_get(httpd_req_t *req) {
}
}
+static void logi_post(httpd_req_t *req) {
+ /* 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, "Processed %s", req->uri);
+ }
+ else {
+ ESP_LOGE(TAG, "Couldn't process %s", req->uri);
+ }
+}
+
// Index page is embedded in binary
static const unsigned char* index_page = NULL;
static size_t index_page_len = 0;
@@ -90,150 +117,84 @@ static const httpd_uri_t get_image = {
.method = HTTP_GET,
.handler = get_image_handler,
};
+;
-static const char timelapse_started[] = "Timelapse started!";
-
-static esp_err_t get_start_timelapse_handler(httpd_req_t *req)
-{
- main_start_timelapse();
- httpd_resp_send(req, timelapse_started, sizeof(timelapse_started));
+const char ssid_param[] = "ssid";
+const char pass_param[] = "pass";
+const char imagesave_param[] = "imagesave";
+const char resolution_param[] = "resolution";
+const char invertion_param[] = "invertion";
+const char timelapse_period_param[] = "timelapse_period";
+const char timelapse_change_param[] = "timelapse_change";
- logi_get(req);
- return ESP_OK;
+static void main_store_request(const char* value, void (*main_store)(int32_t)) {
+ if (*value == '%') value++;
+ main_store((int32_t)strtol(value, NULL, 16));
}
-static const httpd_uri_t get_start_timelapse = {
- .uri = "/start_timelapse",
- .method = HTTP_GET,
- .handler = get_start_timelapse_handler,
-};
-
-static const char timelapse_stopped[] = "Timelapse stopped!";
-static esp_err_t get_stop_timelapse_handler(httpd_req_t *req)
-{
- main_stop_timelapse();
- httpd_resp_send(req, timelapse_stopped, sizeof(timelapse_stopped));
-
- logi_get(req);
- return ESP_OK;
-}
-static const httpd_uri_t get_stop_timelapse = {
- .uri = "/stop_timelapse",
- .method = HTTP_GET,
- .handler = get_stop_timelapse_handler,
-};
-
-static void logi_post(httpd_req_t *req) {
- /* 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, "Processed %s", req->uri);
- }
- else {
- ESP_LOGE(TAG, "Couldn't process %s", req->uri);
- }
-}
-
-const char ssid_post_param[] = "ssid";
-const char pass_post_param[] = "pass";
-
-static esp_err_t post_network_handler(httpd_req_t *req) {
+static esp_err_t post_config_handler(httpd_req_t *req) {
+ // Set buffer
char* buffer = malloc(req->content_len + 1);
httpd_req_recv(req, buffer, req->content_len);
buffer[req->content_len] = '\0'; // just in case
+ // Fix spaces
for (char* iter = buffer; *iter != '\0'; iter++) {
if (*iter == '+') *iter = ' ';
}
- char* ssid_start = strstr(buffer, ssid_post_param) + sizeof(ssid_post_param);
- char* pass_start = strstr(buffer, pass_post_param) + sizeof(pass_post_param);
- char* separator = strchr(buffer, '&');
- *separator = '\0';
-
- wifi_store_network(ssid_start, pass_start);
-
- httpd_resp_send(req, "", 0);
- free(buffer);
-
- logi_post(req);
- return ESP_OK;
-}
-static const httpd_uri_t post_network = {
- .uri = "/network",
- .method = HTTP_POST,
- .handler = post_network_handler,
-};
+ // Get values
+ char* ssid = get_value(buffer, ssid_param, sizeof(ssid_param)-1);
+ char* pass = get_value(buffer, pass_param, sizeof(pass_param)-1);
-static void main_store_request(httpd_req_t *req, void (*main_store)(int32_t)) {
- char* buffer = malloc(req->content_len + 1);
- httpd_req_recv(req, buffer, req->content_len);
+ char* imagesave = get_value(buffer, imagesave_param, sizeof(imagesave_param)-1);
+ char* resolution = get_value(buffer, resolution_param, sizeof(resolution_param)-1);
+ char* invertion = get_value(buffer, invertion_param, sizeof(invertion_param)-1);
- char* numstart = strchr(buffer, '=') + 1;
- if (*numstart == '%') numstart++;
- main_store((int32_t)strtol(numstart, NULL, 16));
-
- free(buffer);
-}
-
-static esp_err_t post_imagesave_handler(httpd_req_t *req) {
- main_store_request(req, main_store_saveimage);
-
- httpd_resp_send(req, "", 0);
+ char* timelapse_period = get_value(buffer, timelapse_period_param, sizeof(timelapse_period_param)-1);
+ char* timelapse_change = get_value(buffer, timelapse_change_param, sizeof(timelapse_change_param)-1);
- logi_post(req);
- return ESP_OK;
-}
-static const httpd_uri_t post_imagesave = {
- .uri = "/imagesave",
- .method = HTTP_POST,
- .handler = post_imagesave_handler,
-};
-
-static esp_err_t post_resolution_handler(httpd_req_t *req) {
- main_store_request(req, main_store_resolution);
-
- httpd_resp_send(req, "", 0);
-
- logi_post(req);
- return ESP_OK;
-}
-static const httpd_uri_t post_resolution = {
- .uri = "/resolution",
- .method = HTTP_POST,
- .handler = post_resolution_handler,
-};
-
-static esp_err_t post_invertion_handler(httpd_req_t *req) {
- main_store_request(req, main_store_invertion);
-
- httpd_resp_send(req, "", 0);
-
- logi_post(req);
- return ESP_OK;
-}
-static const httpd_uri_t post_invertion = {
- .uri = "/invertion",
- .method = HTTP_POST,
- .handler = post_invertion_handler,
-};
+ // Fix terminating zero
+ for (char* iter = buffer; *iter != '\0'; iter++) {
+ if (*iter == '\1') *iter = '\0';
+ }
-static esp_err_t post_timelapse_period_handler(httpd_req_t *req) {
- char* buffer = malloc(req->content_len + 1);
- httpd_req_recv(req, buffer, req->content_len);
+ // Value logic
+ if (ssid != NULL && pass != NULL) {
+ wifi_store_network(ssid, pass);
+ }
+ if (imagesave != NULL) {
+ main_store_request(imagesave, main_store_imagesave);
+ }
+ if (resolution != NULL) {
+ main_store_request(resolution, main_store_resolution);
+ }
+ if (invertion != NULL) {
+ main_store_request(invertion, main_store_invertion);
+ }
+ if (timelapse_period != NULL) {
+ main_store_timelapse_period((int32_t)atoi(timelapse_period));
+ }
+ if (timelapse_change != NULL) {
+ if (*timelapse_change == '0') {
+ main_stop_timelapse();
+ }
+ else if (*timelapse_change == '1') {
+ main_start_timelapse();
+ }
+ }
- main_store_timelapse_period((int32_t)atoi(strchr(buffer, '=') + 1));
+ // Final
free(buffer);
-
httpd_resp_send(req, "", 0);
logi_post(req);
return ESP_OK;
}
-static const httpd_uri_t post_timelapse_period = {
- .uri = "/timelapse_period",
+static const httpd_uri_t post_config = {
+ .uri = "/config",
.method = HTTP_POST,
- .handler = post_timelapse_period_handler,
+ .handler = post_config_handler,
};
static esp_err_t ws_camera_handler(httpd_req_t *req) {
@@ -304,7 +265,6 @@ void webserver_start()
config.httpd.lru_purge_enable = true;
config.httpd.uri_match_fn = httpd_uri_match_wildcard;
config.httpd.max_open_sockets = MAX_CLIENTS;
- config.httpd.max_uri_handlers = 16;
SET_EMBEDDED_PTR(config.servercert, config.servercert_len, cacert, "cacert_pem");
SET_EMBEDDED_PTR(config.prvtkey_pem, config.prvtkey_len, prvtkey, "prvtkey_pem");
@@ -318,13 +278,7 @@ void webserver_start()
httpd_register_uri_handler(server, &get_index);
httpd_register_uri_handler(server, &get_favicon);
httpd_register_uri_handler(server, &get_image);
- httpd_register_uri_handler(server, &get_start_timelapse);
- httpd_register_uri_handler(server, &get_stop_timelapse);
- httpd_register_uri_handler(server, &post_network);
- httpd_register_uri_handler(server, &post_imagesave);
- httpd_register_uri_handler(server, &post_resolution);
- httpd_register_uri_handler(server, &post_invertion);
- httpd_register_uri_handler(server, &post_timelapse_period);
+ httpd_register_uri_handler(server, &post_config);
httpd_register_uri_handler(server, &ws_camera);
ESP_LOGI(TAG, "Started server on port %d", config.httpd.server_port);
diff --git a/Year 3/Internet of things/goam/webserver/index.html b/Year 3/Internet of things/goam/webserver/index.html
index c43bdac..afce35c 100644
--- a/Year 3/Internet of things/goam/webserver/index.html
+++ b/Year 3/Internet of things/goam/webserver/index.html
@@ -63,29 +63,31 @@ iframe {
<h1>Configure static network</h1>
<p>Upon boot up, the device first tries to connect to the (static) network with stored name/password. If that fails, then it tries connecting to an open network. If that fails too, after 2 seconds connection logic is restarted.</p>
- <form action="/network" target="dummyframe" method="POST">
- <input id="network-ssid" name="ssid" type="text" placeholder="Name (SSID)">
- <input id="network-pass" name="pass" type="text" placeholder="Password">
+ <form action="/config" target="dummyframe" method="POST">
+ <input name="ssid" type="text" placeholder="Name (SSID)">
+ <input name="pass" type="text" placeholder="Password">
<input class="button" type="submit" value="Save">
</form>
<h1>Configure timelapse</h1>
- <form action="/timelapse_period" target="dummyframe" method="POST">
+ <form action="/config" target="dummyframe" method="POST">
<input name="timelapse_period" type="number" placeholder="Timelapse period" min="10">
<input class="button" type="submit" value="Save">
</form>
- <form action="/start_timelapse" target="dummyframe" method="GET">
+ <form action="/config" target="dummyframe" method="POST">
+ <input name="timelapse_change" type="hidden" value="1">
<input class="button" type="submit" value="Start timelapse">
</form>
- <form action="/stop_timelapse" target="dummyframe" method="GET">
+ <form action="/config" target="dummyframe" method="POST">
+ <input name="timelapse_change" type="hidden" value="0">
<input class="button" type="submit" value="Stop timelapse">
</form>
<h1>Configure behaviour</h1>
<p><i>Beware, these values may not represent what the device is doing! To be certain, always check the display!</i></p>
- <form action="/imagesave" target="dummyframe" method="POST">
+ <form action="/config" target="dummyframe" method="POST">
<select name="imagesave">
<option value="0">Only save to camera</option>
<option value="&#x01;">Only send to <code>/camera</code> listeners</option>
@@ -94,7 +96,7 @@ iframe {
<input class="button" type="submit" value="Apply">
</form>
<br>
- <form action="/resolution" target="dummyframe" method="POST">
+ <form action="/config" target="dummyframe" method="POST">
<select name="resolution">
<option value="0" selected>640x480</option>
<option value="&#x11;">320x240</option>
@@ -103,7 +105,7 @@ iframe {
<input class="button" type="submit" value="Apply">
</form>
<br>
- <form action="/invertion" target="dummyframe" method="POST">
+ <form action="/config" target="dummyframe" method="POST">
<select name="invertion">
<option value="&#xA6;" selected>Normal</option>
<option value="&#xA7;">Inverted</option>