Starting to write file upload code

parent ed65cfd2
PROJECT_NAME := nexboot
CFLAGS += -DMG_ENABLE_HTTP_STREAMING_MULTIPART
include $(IDF_PATH)/make/project.mk
clean:
./clean.sh
#!/bin/bash
echo -n " * creating html..."
p=$(dirname $(readlink -e $0))
rm -f main/html.h
cd web
for i in *.html ; do
xxd -i $i >> ../main/html.h
done
echo " OK!"
unsigned char index_html[] = {
0x3c, 0x21, 0x44, 0x4f, 0x43, 0x54, 0x59, 0x50, 0x45, 0x20, 0x68, 0x74,
0x6d, 0x6c, 0x3e, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a, 0x20,
0x20, 0x20, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x3c, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x6e, 0x61, 0x6d,
0x65, 0x3d, 0x22, 0x76, 0x69, 0x65, 0x77, 0x70, 0x6f, 0x72, 0x74, 0x22,
0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x3d, 0x22, 0x77, 0x69,
0x64, 0x74, 0x68, 0x3d, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2d, 0x77,
0x69, 0x64, 0x74, 0x68, 0x2c, 0x20, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x61,
0x6c, 0x2d, 0x73, 0x63, 0x61, 0x6c, 0x65, 0x3d, 0x31, 0x22, 0x3e, 0x0a,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x73, 0x74, 0x79, 0x6c, 0x65,
0x20, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x22, 0x74, 0x65, 0x78, 0x74, 0x2f,
0x63, 0x73, 0x73, 0x22, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x20, 0x62, 0x6f, 0x64,
0x79, 0x2c, 0x20, 0x69, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x20, 0x7b, 0x20,
0x6d, 0x61, 0x72, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x30, 0x3b, 0x20, 0x70,
0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x30, 0x3b, 0x20, 0x68,
0x65, 0x69, 0x67, 0x68, 0x74, 0x3a, 0x20, 0x31, 0x30, 0x30, 0x25, 0x3b,
0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x69, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x20, 0x7b, 0x20, 0x64, 0x69, 0x73,
0x70, 0x6c, 0x61, 0x79, 0x3a, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x3b,
0x20, 0x77, 0x69, 0x64, 0x74, 0x68, 0x3a, 0x20, 0x31, 0x30, 0x30, 0x25,
0x3b, 0x20, 0x62, 0x6f, 0x72, 0x64, 0x65, 0x72, 0x3a, 0x20, 0x6e, 0x6f,
0x6e, 0x65, 0x3b, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x3c, 0x2f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x3c, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x4e, 0x65,
0x78, 0x62, 0x6f, 0x6f, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72,
0x6b, 0x20, 0x42, 0x6f, 0x6f, 0x74, 0x6c, 0x6f, 0x61, 0x64, 0x65, 0x72,
0x20, 0x66, 0x6f, 0x72, 0x20, 0x45, 0x53, 0x50, 0x33, 0x32, 0x3c, 0x2f,
0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x3c, 0x2f,
0x68, 0x65, 0x61, 0x64, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x3c, 0x62, 0x6f,
0x64, 0x79, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x68,
0x31, 0x3e, 0x4e, 0x65, 0x78, 0x62, 0x6f, 0x6f, 0x74, 0x20, 0x4e, 0x65,
0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x42, 0x6f, 0x6f, 0x74, 0x6c, 0x6f,
0x61, 0x64, 0x65, 0x72, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x45, 0x53, 0x50,
0x33, 0x32, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x3c, 0x64, 0x69, 0x76, 0x3e, 0x53, 0x65, 0x6c, 0x65, 0x63,
0x74, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x66, 0x69, 0x72,
0x6d, 0x77, 0x61, 0x72, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x75, 0x70, 0x6c,
0x6f, 0x61, 0x64, 0x3c, 0x2f, 0x64, 0x69, 0x76, 0x3e, 0x0a, 0x09, 0x20,
0x20, 0x20, 0x3c, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6e, 0x61, 0x6d, 0x65,
0x3d, 0x22, 0x66, 0x77, 0x5f, 0x75, 0x70, 0x6c, 0x6f, 0x61, 0x64, 0x22,
0x20, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3d, 0x22, 0x2f, 0x22, 0x20,
0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x3d, 0x22, 0x50, 0x4f, 0x53, 0x54,
0x22, 0x20, 0x65, 0x6e, 0x63, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x22, 0x6d,
0x75, 0x6c, 0x74, 0x69, 0x70, 0x61, 0x72, 0x74, 0x2f, 0x66, 0x6f, 0x72,
0x6d, 0x2d, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x20,
0x20, 0x20, 0x3c, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x20, 0x74, 0x79, 0x70,
0x65, 0x3d, 0x22, 0x66, 0x69, 0x6c, 0x65, 0x22, 0x20, 0x6e, 0x61, 0x6d,
0x65, 0x3d, 0x22, 0x66, 0x77, 0x22, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x20,
0x74, 0x79, 0x70, 0x65, 0x3d, 0x22, 0x73, 0x75, 0x62, 0x6d, 0x69, 0x74,
0x22, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3d, 0x22, 0x75, 0x70, 0x6c,
0x6f, 0x61, 0x64, 0x22, 0x3e, 0x0a, 0x09, 0x09, 0x3c, 0x2f, 0x66, 0x6f,
0x72, 0x6d, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x62, 0x6f, 0x64,
0x79, 0x3e, 0x0a, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0a, 0x0a
};
unsigned int index_html_len = 660;
......@@ -6,6 +6,9 @@
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "esp_spi_flash.h"
#include "esp_ota_ops.h"
#include "nvs_flash.h"
#include "lwip/err.h"
......@@ -13,7 +16,7 @@
//#include "https_server.h"
#include "mongoose.h"
#include "nexboot.h"
#include "html.h"
/* You can set Wifi configuration via
'make menuconfig'.
......@@ -130,13 +133,25 @@ void wifi_init_sta()
#endif // if ESP_WIFI_MODE_AP
// Convert a mongoose string to a string
char *mgStrToStr(struct mg_str mgStr) {
char *retStr = (char *) malloc(mgStr.len + 1);
memcpy(retStr, mgStr.p, mgStr.len);
retStr[mgStr.len] = 0;
return retStr;
}
struct fwriter_data {
const esp_partition_t *update_partition;
char recv_buf[1024];
esp_ota_handle_t update_handle;
size_t bytes_written;
};
static void mg_ev_handler(struct mg_connection *nc, int ev, void *p) {
static const char *reply_fmt =
"HTTP/1.0 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"Hello %s\n";
struct fwriter_data *data = (struct fwriter_data *) nc->user_data;
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
switch (ev) {
case MG_EV_ACCEPT: {
......@@ -149,18 +164,57 @@ static void mg_ev_handler(struct mg_connection *nc, int ev, void *p) {
case MG_EV_HTTP_REQUEST: {
char addr[32];
struct http_message *hm = (struct http_message *) p;
char *uri = mgStrToStr(hm->uri);
char *method = mgStrToStr(hm->method);
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
ESP_LOGI(TAG, "HTTP request from %s: %.*s %.*s\n", addr, (int) hm->method.len,
hm->method.p, (int) hm->uri.len, hm->uri.p);
mg_printf(nc, reply_fmt, addr);
if(strcmp(uri, "/") == 0) {
if(!strcmp(method, "POST") == 0) {
mg_send_head(nc, 200, index_html_len, "Content-Type: text/html");
mg_send(nc, index_html, index_html_len);
}
} else {
mg_send_head(nc, 404, 0, "Content-Type: text/plain");
}
nc->flags |= MG_F_SEND_AND_CLOSE;
free(uri);
break;
}
case MG_EV_CLOSE: {
ESP_LOGI(TAG, "Connection %p closed\n", nc);
case MG_EV_HTTP_PART_BEGIN: {
ESP_LOGI(TAG, "Starting upload file from %p\n", nc);
if (data == NULL) {
data = calloc(1, sizeof(struct fwriter_data));
data->bytes_written = 0;
data->update_partition = NULL;
data->update_handle = 0;
}
nc->user_data = (void *) data;
break;
}
case MG_EV_HTTP_PART_DATA: {
data->bytes_written += mp->data.len;
ESP_LOGD(TAG, "MG_EV_HTTP_PART_DATA %p len %d\n", nc, mp->data.len);
break;
}
case MG_EV_HTTP_PART_END: {
mg_printf(nc,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n\r\n"
"Written POST data to OTA partition\n\n");
nc->flags |= MG_F_SEND_AND_CLOSE;
free(data);
nc->user_data = NULL;
break;
}
case MG_EV_CLOSE: {
ESP_LOGI(TAG, "Connection %p closed\n", nc);
break;
}
}
}
......@@ -175,7 +229,7 @@ void mongooseTask(void *data) {
ESP_LOGD(TAG, "Mongoose succesfully inited");
nc = mg_bind(&mgr, MG_LISTEN_ADDR, mg_ev_handler);
ESP_LOGI(TAG, "Webserver uccessfully bound on port %s\n", MG_LISTEN_ADDR);
ESP_LOGI(TAG, "Webserver successfully bound on port %s\n", MG_LISTEN_ADDR);
if (nc == NULL) {
ESP_LOGE(TAG, "No connection from the mg_bind()");
vTaskDelete(NULL);
......
const static char nexboot_index_html[] = "<!DOCTYPE html>"
"<html>\n"
"<head>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
" <style type=\"text/css\">\n"
" html, body, iframe { margin: 0; padding: 0; height: 100%; }\n"
" iframe { display: block; width: 100%; border: none; }\n"
" </style>\n"
"<title>Nexboot Network Bootloader for ESP32</title>\n"
"</head>\n"
"<body>\n"
"<h1>Nexboot Network Bootloader for ESP32</h1>\n"
"<div>Select binary firmware to upload</div>\n"
"<form name=\"fw_upload\" action=\"/\" method=\"POST\">\n"
"<input type=\"file\" name=\"fw\">\n"
"<input type=\"submit\" value=\"upload\">\n"
"</form>\n"
"</body>\n"
"</html>\n";
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
html, body, iframe { margin: 0; padding: 0; height: 100%; }
iframe { display: block; width: 100%; border: none; }
</style>
<title>Nexboot Network Bootloader for ESP32</title>
</head>
<body>
<h1>Nexboot Network Bootloader for ESP32</h1>
<div>Select binary firmware to upload</div>
<form name="fw_upload" action="/" method="POST" enctype="multipart/form-data">
<input type="file" name="fw">
<input type="submit" value="upload">
</form>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment