Skip to content

Commit

Permalink
Version 3.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
alf45tar committed Feb 4, 2024
1 parent a206597 commit bfe3d59
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 8 deletions.
73 changes: 69 additions & 4 deletions src/OTAHTTPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ __________ .___ .__ .__ _____ .__ .__

#include "Version.h"

#define OTA_PARTITION_SIZE 0x1D0000 // 1900544 bytes
#if defined(ARDUINO_BPI_LEAF_S3) || defined(ARDUINO_LILYGO_T_DISPLAY_S3)
#define OTA_PARTITION_SIZE 0x360000 // 3538944 bytes
#define FIRMWARE_MAX_SIZE (8 * 1024 * 1024) // 8M
#else
#define OTA_PARTITION_SIZE 0x1D0000 // 1900544 bytes
#define FIRMWARE_MAX_SIZE (4 * 1024 * 1024) // 4M
#endif

String latestFirmwareVersion = VERSION;
String url;
Expand Down Expand Up @@ -112,7 +118,7 @@ String get_latest_firmware_version(void) {
DPRINT("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
payload = https.getString();
payload = https.getString();
payload.trim();
if (payload.equals(VERSION)) {
DPRINT("Device already on latest firmware version: %s\n", VERSION);
Expand All @@ -135,6 +141,65 @@ String get_latest_firmware_version(void) {
}


String get_firmware_md5(void) {

String payload = "";

if (!wifiEnabled) return payload;

WiFiClientSecure *client = new WiFiClientSecure;

if (client) {

client->setCACert(rootCACertificate);

{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;

url = F("https://raw.githubusercontent.com/alf45tar/PedalinoMini/");
url += F(VERSION);
url += F("/firmware/");
url += xstr(PLATFORMIO_ENV);
url += F("/firmware.bin.md5?");
url += String(rand());

if (https.begin(*client, url)) {

DPRINT("[HTTPS] GET... %s\n", url.c_str());
// start connection and send HTTP header
int httpCode = https.GET();

// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
DPRINT("[HTTPS] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
payload = https.getString();
payload.trim();
}
} else {
DPRINT("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
} else {
DPRINT("[HTTPS] Unable to connect\n");
}
}
delete client;
}

return payload;
}


bool official_firmware () {
String md5 = get_firmware_md5();
return md5.equals(sketchMD5);
}


void ota_https_update_event_handler(HttpEvent_t *event) {

switch(event->event_id) {
Expand Down Expand Up @@ -165,9 +230,9 @@ void ota_https_update_event_handler(HttpEvent_t *event) {
DPRINT("#");
otaProgress += event->data_len;
#if defined(ARDUINO_LILYGO_T_DISPLAY) || defined(ARDUINO_LILYGO_T_DISPLAY_S3)
display_progress_bar_update(otaProgress, OTA_PARTITION_SIZE);
display_progress_bar_update(otaProgress, FIRMWARE_MAX_SIZE);
#else
display.drawProgressBar(4, 32, 120, 8, otaProgress / (OTA_PARTITION_SIZE / 100) );
display.drawProgressBar(4, 32, 120, 8, otaProgress / (FIRMWARE_MAX_SIZE / 100) );
display.display();
#endif
break;
Expand Down
30 changes: 26 additions & 4 deletions src/WebConfigAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -4845,6 +4845,26 @@ void get_update_page(unsigned int start, unsigned int len) {
page += F(" Update</button>");
page += F("</div>");
page += F("</div>");
page += F("<div class='row'>");
page += F("<div class='col-4'>");
page += F("<small>");
page += sketchMD5;
page += F("</small>");
page += F("</div>");
page += F("<div class='col-4'>");
page += F("<small>");
page += F("<div id='latestFirmwareVersion' w3-include-html='https://raw.githubusercontent.com/alf45tar/PedalinoMini/master/firmware/");
page += xstr(PLATFORMIO_ENV);
page += F("/firmware.bin.md5?");
page += (rand() % (999999 - 100000 + 1) + 100000);
page += F("'></div>");
page += F("</small>");
page += F("</div>");
page += F("<div class='col-1'>");
page += F("</div>");
page += F("<div class='col-3'>");
page += F("</div>");
page += F("</div>");
page += F("</div>");
page += F("</div>");

Expand All @@ -4864,7 +4884,9 @@ void get_update_page(unsigned int start, unsigned int len) {
page += F("<input type='file' class='form-control' id='firmwareFile' name='upload'>");
page += F("</div>");
page += F("<small id='uploadHelpBlock' class='form-text text-muted'>");
page += F("Select firmware.bin or spiffs.bin and press Upload to upgrade firmware or file system image.");
page += F("Select firmware.bin or spiffs.bin and press Upload to upgrade firmware or file system image. Max file size is ");
page += FIRMWARE_MAX_SIZE;
page += F(" bytes.");
page += F("</small>");
page += F("</div>");
page += F("<div class='col-1'>");
Expand Down Expand Up @@ -5060,7 +5082,7 @@ void get_update_page(unsigned int start, unsigned int len) {
"const allowed_mime_types = [ 'application/octet-stream', 'application/macbinary' ];\n"

"const allowed_size = ");
page += OTA_PARTITION_SIZE;
page += FIRMWARE_MAX_SIZE;
page += F(";\n");

if (trim_page(start, len)) return;
Expand All @@ -5071,7 +5093,7 @@ void get_update_page(unsigned int start, unsigned int len) {
"}\n"

"if(file.size > allowed_size) {\n"
"alert('File size exceeed 2M');\n"
"alert('File size exceeed allowed size');\n"
"return;"
"}\n"

Expand Down Expand Up @@ -5447,7 +5469,7 @@ void http_handle_update(AsyncWebServerRequest *request) {

void http_handle_progress(AsyncWebServerRequest *request) {
if (firmwareUpdate == PED_UPDATE_CLOUD)
request->send(200, "text/plain", String(HttpsOTA.status() == HTTPS_OTA_UPDATING ? 100 * otaProgress / OTA_PARTITION_SIZE : 0));
request->send(200, "text/plain", String(HttpsOTA.status() == HTTPS_OTA_UPDATING ? 100 * otaProgress / FIRMWARE_MAX_SIZE : 0));
else
request->send(200, "text/plain", String(Update.isRunning() && Update.size() ? 100 * Update.progress() / Update.size() : 0));
}
Expand Down

0 comments on commit bfe3d59

Please sign in to comment.