Skip to content

Commit

Permalink
OTA Upgrade is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRvO committed Jul 4, 2017
1 parent 7c2fee9 commit dc9ff51
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 65 deletions.
4 changes: 2 additions & 2 deletions software/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ include $(IDF_PATH)/make/project.mk
include sdkconfig
include ${PWD}/components/libwebsockets/scripts/esp32.mk
CFLAGS +=-I$(PROJECT_PATH)/components/libwebsockets/plugins


CXXFLAGS +=-I$(PROJECT_PATH)/components/libwebsockets/plugins
GENCERTS = 0

romfs_prepare:
echo "Resolving server side includes"
Expand Down
165 changes: 105 additions & 60 deletions software/data_files/html/ota.html
Original file line number Diff line number Diff line change
@@ -1,69 +1,114 @@
-<!--#include file="ssi/header.html"-->
<style>
.fileUpload {
position: relative;
overflow: hidden;
margin: 10px;
}

.fileUpload input.upload {
position: absolute;
top: 0;
right: 0;
margin: 0;
padding: 0;
font-size: 20px;
cursor: pointer;
opacity: 0;
filter: alpha(opacity=0);
}
<!--#include file="ssi/header.html"-->
<style type="text/css">
.progr {
width: 100%;
height: 32px;
background-color: rgba(240, 240, 24, 0.6);
color: #c0c0c0;
}

.progr-ok {
width: 100%;
height: 32px;
background-color: rgba(240, 240, 24, 1.0);
color: #e0e060;
}

.progr-fail {
width: 100%;
height: 32px;
background-color: rgba(240, 240, 24, 1.0);
color: #c00000;
}

.progr-done {
width: 100%;
height: 32px;
background-color: rgba(240, 240, 24, 1.0);
color: #00c000;
}

</style>


<div id="main">
<div class="header">
<h1>OTA Upgrade!</h1>
<h2>Upload new firmware to the device and perform an upgrade Over The Air!</h2>
</div>
<div class="content">
<h2 class="content-subhead">Upload firmware</h2>
<p>
Select a firmware file you want flashed to the device.
Currently, we only supports flashing a new firmware. <br>Flashing the filesystem may be supported at a later time.
</p>

<form method="POST" id="FormOTA" enctype="multipart/form-data">
<input id="ota_upload_file" placeholder="Choose File" disabled="disabled" />
<div class="fileUpload pure-button pure-button-primary">
<span>Select File</span>
<input id="ota_upload" type="file" name="file" class="upload" />
</div>
<br>
<br>
<input type="submit" value="Upload" class="pure-button button-warning">
</form>
</div>
<div class="header">
<h1>OTA Upgrade!</h1>
<h2>Upload new firmware to the device and perform an upgrade Over The Air!</h2>
</div>
<div class="content">
<h2 class="content-subhead">Upload firmware</h2>
<p>
Select a firmware file you want flashed to the device.

<form name=multipart action="/api/v1/ota/" method="post" enctype="multipart/form-data" onsubmit="do_upload(this); return false;">
<table>
<tr>
<td>
<input type="file" name="ota" id="ota" size="20" accept=".bin" onchange="file_change();" style="font-size: 12pt">
</td>
<td colspan=2>
<span id=file_info style="font-size:12pt;"></span></td>
</tr>
<tr>
<td colspan=3>
<input type="submit" id="update" name="upload" disabled value="Upload" style="font-size: 18pt">
</td>
</tr>
<tr>
<td colspan=3>
<progress id=progr value=0 max=100 class=progr>Upload Progress</progress>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</div>
</div>

</div>

<script src="js/ui.js"></script>
<script src="js/jquery.min.js"></script>
<script>
document.getElementById("ota_upload").onchange = function() {
document.getElementById("ota_upload_file").value = this.value;
};

$( document ).ready(function() {
// POST firmware
$('#FormOTA').submit(function(e){
e.preventDefault();
$.ajax({
url:'/api/v1/post/ota_upload',
type:'POST',
data:$('#FormOTA').serialize(),
success:function(){
console.log("Success");
}
});
});
});
-<!--#include file="ssi/footer.html"-->

function do_upload(f) {
var xhr = new XMLHttpRequest();

document.getElementById('update').disabled = 1;
document.getElementById("progr").class = "progr-ok";

xhr.upload.addEventListener("progress", function(e) {
document.getElementById("progr").value = parseInt(e.loaded / e.total * 100);

if (e.loaded == e.total) {
}

}, false);

xhr.onreadystatechange = function(e) {
console.log("rs" + xhr.readyState + " status " + xhr.status);
if (xhr.readyState == 4) {
document.getElementById("progr").class = "progr-ok";
setTimeout(function() {
window.location.href = location.origin + "/";
}, 1000);
}
};

xhr.open("POST", f.action, true);
xhr.send(new FormData(f));

return false;
}

function file_change() {
document.getElementById('update').disabled = 0;
}
</script>



<!--#include file="ssi/footer.html"-->
12 changes: 11 additions & 1 deletion software/main/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
#include "lws_server_structs.h"
#include "cJSON.h"
#include <arpa/inet.h>
extern "C"
{
#include "ota_protocol.h"
}

static const char *TAG = "HTTP_SERVER";
/*We define this here as we need to access cpp functions from it.
**Other relevant LWS structs are defined in lws_server_structs.c
*/


static const struct lws_protocols __protocols[] = {
{
"http-only",
Expand All @@ -30,7 +35,12 @@ static const struct lws_protocols __protocols[] = {
sizeof(post_api_session_data), /* per_session_data_size */
0, 1, NULL, 900
},

{ \
"ota", \
callback_esplws_ota, \
sizeof(struct per_session_data__esplws_ota), \
4096, 0, NULL, 900 \
},
{ NULL, NULL, 0, 0, 0, NULL, 0 } /* terminator */
};

Expand Down
2 changes: 1 addition & 1 deletion software/main/Makefile.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ALL_FLAGS += -g
#ALL_FLAGS += -DMBEDTLS_X509_CRT_PARSE_C
ALL_FLAGS += -ffunction-sections
ALL_FLAGS += -fdata-sections
CXXFLAGS += ${ALL_FLAGS}
CXXFLAGS += -fpermissive
CXXFLAGS += ${ALL_FLAGS}
CFLAGS += ${ALL_FLAGS}
LDFLAGS += -ffunction-sections
LDFLAGS += -fdata-sections
19 changes: 19 additions & 0 deletions software/main/include/ota_protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef OTA_PROTOCOL_H
#define OTA_PROTOCOL_H
#include <libwebsockets.h>

int callback_esplws_ota(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len);

struct per_session_data__esplws_ota {
struct lws_spa *spa;
char filename[32];
char result[LWS_PRE + 512];
int result_len;
int filename_length;
esp_ota_handle_t otahandle;
const esp_partition_t *part;
long file_length;
nvs_handle nvh;
};
#endif
20 changes: 19 additions & 1 deletion software/main/lws_server_structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ const struct lws_protocol_vhost_options __pvo_headers = {
"timeout=15, max=20",
};

const struct lws_protocol_vhost_options __post_pvo = {
const struct lws_protocol_vhost_options __ota_pvo = {
NULL,
NULL,
"esplws-ota",
""
};

const struct lws_protocol_vhost_options __post_pvo = {
&__ota_pvo,
NULL,
"post",
""
Expand All @@ -24,7 +31,18 @@ const struct lws_protocol_vhost_options __get_pvo = {
""
};

const struct lws_http_mount __ota_mount = {
.mount_next = NULL,
.mountpoint = "/api/v1/ota/",
.origin = "ota",
.origin_protocol = LWSMPRO_CALLBACK,
.mountpoint_len = 11,
.protocol = "ota",
};


const struct lws_http_mount __post_mount = {
.mount_next = &__ota_mount,
.mountpoint = "/api/v1/post/",
.origin = "post",
.origin_protocol = LWSMPRO_CALLBACK,
Expand Down
Loading

0 comments on commit dc9ff51

Please sign in to comment.