mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-24 11:46:34 +00:00
Allow HTTPS sites with or without redirects enabled #300
This commit is contained in:
parent
af336b190b
commit
00919d7f05
@ -303,7 +303,7 @@ static WiFiSpiClass WiFi;
|
||||
#include "sys/svc/hasp_mdns.h"
|
||||
#endif
|
||||
|
||||
#if HASP_USE_OTA > 0
|
||||
#if HASP_USE_OTA > 0 || HASP_USE_HTTP_UPDATE > 0
|
||||
#include "sys/svc/hasp_ota.h"
|
||||
#endif
|
||||
|
||||
|
@ -535,6 +535,14 @@ void dispatch_config(const char* topic, const char* payload, uint8_t source)
|
||||
httpGetConfig(settings);
|
||||
}
|
||||
#endif
|
||||
#if HASP_USE_OTA > 0 || HASP_USE_HTTP_UPDATE > 0
|
||||
else if(strcasecmp_P(topic, PSTR("ota")) == 0) {
|
||||
if(update)
|
||||
otaSetConfig(settings);
|
||||
else
|
||||
otaGetConfig(settings);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Send output
|
||||
@ -1238,6 +1246,11 @@ bool dispatch_factory_reset()
|
||||
{
|
||||
bool formated = true;
|
||||
bool erased = true;
|
||||
bool cleared = true;
|
||||
|
||||
#if ESP32
|
||||
erased = nvs_clear_user_config();
|
||||
#endif
|
||||
|
||||
#if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0
|
||||
formated = HASP_FS.format();
|
||||
@ -1248,7 +1261,7 @@ bool dispatch_factory_reset()
|
||||
erased = configClearEeprom();
|
||||
#endif
|
||||
|
||||
return formated && erased;
|
||||
return formated && erased && cleared;
|
||||
}
|
||||
|
||||
void dispatch_calibrate(const char*, const char*, uint8_t source)
|
||||
|
@ -3,6 +3,20 @@
|
||||
#include "hasplib.h"
|
||||
#include "hasp_nvs.h"
|
||||
|
||||
bool nvs_clear_user_config()
|
||||
{
|
||||
const char* name[8] = {"time", "ota"};
|
||||
Preferences preferences;
|
||||
|
||||
for(int i = 0; i < 2; i++) {
|
||||
preferences.begin(name[i], false);
|
||||
preferences.clear();
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool nvsUpdateString(Preferences& preferences, const char* key, JsonVariant value)
|
||||
{
|
||||
bool changed = false;
|
||||
@ -15,7 +29,26 @@ bool nvsUpdateString(Preferences& preferences, const char* key, JsonVariant valu
|
||||
changed = true; // Nvs key doesnot exist, create it
|
||||
if(changed) {
|
||||
size_t len = preferences.putString(key, val);
|
||||
LOG_VERBOSE(TAG_TIME, F(D_BULLET "Wrote %s => %s (%d bytes)"), key, val, len);
|
||||
LOG_DEBUG(TAG_TIME, F(D_BULLET "Wrote %s => %s (%d bytes)"), key, val, len);
|
||||
}
|
||||
}
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool nvsUpdateUInt(Preferences& preferences, const char* key, JsonVariant value)
|
||||
{
|
||||
bool changed = false;
|
||||
uint32_t val = value.as<uint32_t>();
|
||||
|
||||
if(!value.isNull()) { // Json key exists
|
||||
if(preferences.isKey(key)) { // Nvs key exists
|
||||
changed = preferences.getUInt(key, 0) != val; // Value changed
|
||||
} else
|
||||
changed = true; // Nvs key doesnot exist, create it
|
||||
if(changed) {
|
||||
size_t len = preferences.putUInt(key, val);
|
||||
LOG_DEBUG(TAG_TIME, F(D_BULLET "Wrote %s => %d"), key, val);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,9 @@
|
||||
#include "hasplib.h"
|
||||
#include <Preferences.h>
|
||||
|
||||
bool nvs_clear_user_config();
|
||||
bool nvsUpdateString(Preferences& preferences, const char* key, JsonVariant value);
|
||||
bool nvsUpdateUInt(Preferences& preferences, const char* key, JsonVariant value);
|
||||
|
||||
#endif // ESP32
|
||||
|
||||
|
@ -109,7 +109,7 @@
|
||||
#define D_JSONL_FAILED "JSONL parsing failed at line %u"
|
||||
#define D_JSONL_SUCCEEDED "Jsonl fully parsed"
|
||||
|
||||
#define D_OTA_CHECK_UPDATE "Checking updates URL: %s"
|
||||
#define D_OTA_CHECK_UPDATE "Trying update URL: %s"
|
||||
#define D_OTA_CHECK_COMPLETE "Update check complete"
|
||||
#define D_OTA_CHECK_FAILED "Update check failed: %s"
|
||||
#define D_OTA_UPDATE_FIRMWARE "OTA Firmware Update"
|
||||
|
@ -97,27 +97,27 @@
|
||||
#define D_ATTRIBUTE_COLOR_INVALID "Invalid color property: %s" // new
|
||||
#define D_ATTRIBUTE_LONG_MODE_INVALID "Invalid long mode: %s" // new
|
||||
|
||||
#define D_OOBE_SSID_VALIDATED "SSID %s validated"
|
||||
#define D_OOBE_AUTO_CALIBRATE "Auto calibrate enabled"
|
||||
#define D_OOBE_CALIBRATED "Already calibrated"
|
||||
#define D_OOBE_SSID_VALIDATED "SSID %s validated" // new
|
||||
#define D_OOBE_AUTO_CALIBRATE "Auto calibrate enabled" // new
|
||||
#define D_OOBE_CALIBRATED "Already calibrated" // new
|
||||
|
||||
#define D_DISPATCH_COMMAND_NOT_FOUND "Command '%s' not found"
|
||||
#define D_DISPATCH_INVALID_PAGE "Invalid page %s"
|
||||
#define D_DISPATCH_REBOOT "Rebooting the MCU now!"
|
||||
#define D_DISPATCH_COMMAND_NOT_FOUND "Command '%s' not found" // new
|
||||
#define D_DISPATCH_INVALID_PAGE "Invalid page %s" // new
|
||||
#define D_DISPATCH_REBOOT "Rebooting the MCU now!" // new
|
||||
|
||||
#define D_JSON_FAILED "JSON parsing failed:"
|
||||
#define D_JSONL_FAILED "JSONL parsing failed at line %u"
|
||||
#define D_JSONL_SUCCEEDED "Jsonl fully parsed"
|
||||
#define D_JSON_FAILED "JSON parsing failed:" // new
|
||||
#define D_JSONL_FAILED "JSONL parsing failed at line %u" // new
|
||||
#define D_JSONL_SUCCEEDED "Jsonl fully parsed" // new
|
||||
|
||||
#define D_OTA_CHECK_UPDATE "Checking updates URL: %s"
|
||||
#define D_OTA_CHECK_COMPLETE "Update check complete"
|
||||
#define D_OTA_CHECK_FAILED "Update check failed: %s"
|
||||
#define D_OTA_UPDATE_FIRMWARE "OTA Firmware Update"
|
||||
#define D_OTA_UPDATE_COMPLETE "OTA Update complete"
|
||||
#define D_OTA_UPDATE_APPLY "Applying Firmware & Reboot"
|
||||
#define D_OTA_UPDATE_FAILED "OTA Update failed"
|
||||
#define D_OTA_UPDATING_FIRMWARE "Updating firmware..."
|
||||
#define D_OTA_UPDATING_FILESYSTEM "Updating filesystem..."
|
||||
#define D_OTA_CHECK_UPDATE "Checking update URL: %s" // new
|
||||
#define D_OTA_CHECK_COMPLETE "Update check complete" // new
|
||||
#define D_OTA_CHECK_FAILED "Update check failed: %s" // new
|
||||
#define D_OTA_UPDATE_FIRMWARE "OTA Firmware Update" // new
|
||||
#define D_OTA_UPDATE_COMPLETE "OTA Update complete" // new
|
||||
#define D_OTA_UPDATE_APPLY "Applying Firmware & Reboot" // new
|
||||
#define D_OTA_UPDATE_FAILED "OTA Update failed" // new
|
||||
#define D_OTA_UPDATING_FIRMWARE "Updating firmware..." // new
|
||||
#define D_OTA_UPDATING_FILESYSTEM "Updating filesystem..." // new
|
||||
|
||||
#define D_HTTP_HASP_DESIGN "Conception HASP"
|
||||
#define D_HTTP_INFORMATION "Information"
|
||||
|
@ -110,8 +110,8 @@
|
||||
#define D_JSONL_SUCCEEDED "Jsonl volledig verwerkt"
|
||||
|
||||
#define D_OTA_CHECK_UPDATE "Controle update URL: %s"
|
||||
#define D_OTA_CHECK_COMPLETE "Update controle klaar"
|
||||
#define D_OTA_CHECK_FAILED "Update check failed: %s"
|
||||
#define D_OTA_CHECK_COMPLETE "Update gecontroleerd"
|
||||
#define D_OTA_CHECK_FAILED "Update controle mislukt: %s"
|
||||
#define D_OTA_UPDATE_FIRMWARE "OTA Firmware bijwerken"
|
||||
#define D_OTA_UPDATE_COMPLETE "OTA Firmware bijgewerkt"
|
||||
#define D_OTA_UPDATE_APPLY "Firmware Schrijven & Herstart"
|
||||
|
@ -86,7 +86,7 @@ void setup()
|
||||
mdnsSetup();
|
||||
#endif
|
||||
|
||||
#if HASP_USE_OTA > 0
|
||||
#if HASP_USE_OTA > 0 || HASP_USE_HTTP_UPDATE > 0
|
||||
otaSetup();
|
||||
#endif
|
||||
|
||||
|
@ -535,6 +535,11 @@ static void webHandleApiConfig()
|
||||
if(!strcasecmp_P(endpoint_key, PSTR("http"))) {
|
||||
httpSetConfig(settings);
|
||||
} else
|
||||
#endif
|
||||
#if HASP_USE_OTA > 0 || HASP_USE_HTTP_UPDATE > 0
|
||||
if(!strcasecmp_P(endpoint_key, PSTR("ota"))) {
|
||||
otaSetConfig(settings);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
LOG_WARNING(TAG_HTTP, F("Invalid module %s"), endpoint_key);
|
||||
@ -566,6 +571,11 @@ static void webHandleApiConfig()
|
||||
if(!strcasecmp_P(endpoint_key, PSTR("http"))) {
|
||||
httpGetConfig(settings);
|
||||
} else
|
||||
#endif
|
||||
#if HASP_USE_OTA > 0 || HASP_USE_HTTP_UPDATE > 0
|
||||
if(!strcasecmp_P(endpoint_key, PSTR("ota"))) {
|
||||
otaGetConfig(settings);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
webServer.send(400, contentType, "Bad Request");
|
||||
@ -573,25 +583,25 @@ static void webHandleApiConfig()
|
||||
}
|
||||
configOutput(settings, TAG_HTTP); // Log current JSON config
|
||||
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
// Mask non-blank passwords
|
||||
if(!settings[FPSTR(FP_CONFIG_PASS)].isNull() && settings[FPSTR(FP_CONFIG_PASS)].as<String>().length() != 0) {
|
||||
settings[FPSTR(FP_CONFIG_PASS)] = D_PASSWORD_MASK;
|
||||
}
|
||||
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
doc.shrinkToFit();
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
const size_t size = measureJson(doc) + 1;
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
char jsondata[size];
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
memset(jsondata, 0, size);
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
serializeJson(doc, jsondata, size);
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
webServer.send(200, contentType, jsondata);
|
||||
LOG_WARNING(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
LOG_DEBUG(TAG_HTTP, "%s - %d", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@ -2019,6 +2029,10 @@ static void webHandleFirmware()
|
||||
if(!httpIsAuthenticated(F("firmware"))) return;
|
||||
|
||||
if(webServer.method() == HTTP_POST && webServer.hasArg(PSTR("url"))) {
|
||||
StaticJsonDocument<512> settings;
|
||||
for(int i = 0; i < webServer.args(); i++) settings[webServer.argName(i)] = webServer.arg(i);
|
||||
bool updated = otaSetConfig(settings.as<JsonObject>());
|
||||
|
||||
String url = webServer.arg(PSTR("url"));
|
||||
{
|
||||
String httpMessage((char*)0);
|
||||
@ -2027,14 +2041,17 @@ static void webHandleFirmware()
|
||||
httpMessage += haspDevice.get_hostname();
|
||||
httpMessage += F("</h1><hr>");
|
||||
|
||||
httpMessage += F("<p><b>ESP update</b></p>Updating ESP firmware from: ");
|
||||
httpMessage += F("<h2>" D_HTTP_FIRMWARE_UPGRADE "</h2>");
|
||||
httpMessage += F("<p>Updating firmware from: ");
|
||||
httpMessage += url;
|
||||
httpMessage += F("</p><p>Please wait...</p>");
|
||||
|
||||
webSendHeader(haspDevice.get_hostname(), httpMessage.length(), 30);
|
||||
httpMessage += FPSTR(MAIN_MENU_BUTTON);
|
||||
|
||||
webSendHeader(haspDevice.get_hostname(), httpMessage.length(), 20);
|
||||
webServer.sendContent(httpMessage);
|
||||
}
|
||||
|
||||
LOG_TRACE(TAG_HTTP, F("Updating ESP firmware from: %s"), url.c_str());
|
||||
dispatch_web_update(NULL, url.c_str(), TAG_HTTP);
|
||||
} else {
|
||||
// Send Firmware page
|
||||
@ -2050,7 +2067,7 @@ static void webHandleFirmware()
|
||||
|
||||
// File
|
||||
httpMessage +=
|
||||
F("<div class='row'><div class='col-25'><label class='required' for='filename'>From File</label></div>");
|
||||
F("<div class='row'><div class='col-25'><label class='required' for='filename'>OTA File</label></div>");
|
||||
httpMessage += F("<div class='col-75'><input required type='file' name='filename' accept='.bin'></div></div>");
|
||||
|
||||
// Destination
|
||||
@ -2069,14 +2086,23 @@ static void webHandleFirmware()
|
||||
|
||||
// URL
|
||||
httpMessage +=
|
||||
F("<div class='row'><div class='col-25'><label class='required' for='url'>From URL</label></div>");
|
||||
F("<div class='row'><div class='col-25'><label class='required' for='url'>OTA URL</label></div>");
|
||||
httpMessage += F("<div class='col-75'><input required id='url' name='url' value=''></div></div>");
|
||||
|
||||
// Redirect
|
||||
httpMessage += F("<div class='row'><div class='col-25'><label for='redirect'>Follow Redirects</label></div>");
|
||||
httpMessage += F("<div class='col-75'><select id='redirect' name='redirect'>");
|
||||
httpMessage += getOption(0, F("Disabled"), -1);
|
||||
httpMessage += getOption(1, F("Strict"), -1);
|
||||
httpMessage += getOption(2, F("Always"), -1);
|
||||
httpMessage += F("</select></div></div>");
|
||||
|
||||
// Submit & End Form
|
||||
httpMessage += F("<button type='submit' name='save' value='debug'>Update from URL</button>");
|
||||
httpMessage += F("<button type='submit' name='save' value='debug'>" D_HTTP_UPDATE_FIRMWARE "</button>");
|
||||
httpMessage += F("</form></div>");
|
||||
|
||||
httpMessage += FPSTR(MAIN_MENU_BUTTON);
|
||||
httpMessage += "<script>filler(\"GET\", \"/api/config/ota/\")</script>";
|
||||
|
||||
webSendHeader(haspDevice.get_hostname(), httpMessage.length(), 0);
|
||||
webServer.sendContent(httpMessage);
|
||||
|
@ -6,6 +6,7 @@
|
||||
#include "hasplib.h"
|
||||
|
||||
#include "hasp_debug.h"
|
||||
#include "hasp_config.h"
|
||||
#include "hasp_ota.h"
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266)
|
||||
@ -13,8 +14,8 @@
|
||||
#include <ESP8266httpUpdate.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#ifndef HASP_OTA_PORT
|
||||
#define HASP_OTA_PORT 8266
|
||||
#ifndef HASP_ARDUINOOTA_PORT
|
||||
#define HASP_ARDUINOOTA_PORT 8266
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@ -23,8 +24,12 @@
|
||||
#include <HTTPUpdate.h>
|
||||
#include <WiFi.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#ifndef HASP_OTA_PORT
|
||||
#define HASP_OTA_PORT 3232
|
||||
#ifndef HASP_ARDUINOOTA_PORT
|
||||
#define HASP_ARDUINOOTA_PORT 3232
|
||||
#endif
|
||||
|
||||
#ifndef HASP_OTA_URL
|
||||
#define HASP_OTA_URL ""
|
||||
#endif
|
||||
|
||||
/**
|
||||
@ -63,11 +68,59 @@ const char* rootCACertificate = "-----BEGIN CERTIFICATE-----\n"
|
||||
extern const uint8_t rootca_crt_bundle_start[] asm("_binary_data_cert_x509_crt_bundle_bin_start");
|
||||
#endif // ARDUINO_ARCH_ESP32
|
||||
|
||||
static WiFiClient otaClient;
|
||||
std::string otaUrl = "http://ota.netwize.be";
|
||||
int16_t otaPort = HASP_OTA_PORT;
|
||||
static WiFiClientSecure secureClient;
|
||||
std::string otaUrl = "http://ota.netwize.be";
|
||||
|
||||
int16_t arduinoOtaPort = HASP_ARDUINOOTA_PORT;
|
||||
int8_t otaPrecentageComplete = -1;
|
||||
|
||||
void otaSetup(void)
|
||||
{
|
||||
#if ESP_ARDUINO_VERSION_MAJOR >= 2
|
||||
/* This method is similar to the single root certificate verfication, but it uses a standard set of root
|
||||
* certificates from Mozilla to authenticate against. This allows the client to connect to all public SSL
|
||||
* servers. */
|
||||
secureClient.setCACertBundle(rootca_crt_bundle_start);
|
||||
#endif
|
||||
// Reading data over SSL may be slow, use an adequate timeout
|
||||
secureClient.setTimeout(12); // timeout argument is defined in seconds
|
||||
|
||||
#if HASP_USE_OTA > 0
|
||||
if(strlen(otaUrl.c_str())) {
|
||||
LOG_INFO(TAG_OTA, otaUrl.c_str());
|
||||
}
|
||||
|
||||
if(arduinoOtaPort > 0) {
|
||||
ArduinoOTA.onStart(ota_on_start);
|
||||
ArduinoOTA.onEnd(ota_on_end);
|
||||
ArduinoOTA.onProgress(ota_on_progress);
|
||||
ArduinoOTA.onError(ota_on_error);
|
||||
|
||||
ArduinoOTA.setHostname(haspDevice.get_hostname());
|
||||
// ArduinoOTA.setPassword(configPassword); // See OTA_PASSWORD
|
||||
ArduinoOTA.setPort(arduinoOtaPort);
|
||||
|
||||
#if ESP32
|
||||
#if HASP_USE_MDNS > 0
|
||||
ArduinoOTA.setMdnsEnabled(false); // it's already started
|
||||
MDNS.enableArduino(_port, (_password.length() > 0)); // Add the Arduino SVC
|
||||
#endif
|
||||
// ArduinoOTA.setTimeout(1000); // default
|
||||
#endif
|
||||
ArduinoOTA.setRebootOnSuccess(false); // We do that ourselves
|
||||
|
||||
#ifdef OTA_PASSWORD
|
||||
ArduinoOTA.setPassword(OTA_PASSWORD); // TODO
|
||||
#endif
|
||||
|
||||
ArduinoOTA.begin();
|
||||
LOG_INFO(TAG_OTA, F(D_SERVICE_STARTED));
|
||||
} else {
|
||||
LOG_WARNING(TAG_OTA, F(D_SERVICE_DISABLED));
|
||||
}
|
||||
#endif // HASP_USE_OTA
|
||||
}
|
||||
|
||||
bool otaUpdateCheck()
|
||||
{ // firmware update check
|
||||
WiFiClientSecure wifiUpdateClientSecure;
|
||||
@ -174,42 +227,6 @@ static void ota_on_error(ota_error_t error)
|
||||
// delay(5000);
|
||||
}
|
||||
|
||||
void otaSetup(void)
|
||||
{
|
||||
if(strlen(otaUrl.c_str())) {
|
||||
LOG_INFO(TAG_OTA, otaUrl.c_str());
|
||||
}
|
||||
|
||||
if(otaPort > 0) {
|
||||
ArduinoOTA.onStart(ota_on_start);
|
||||
ArduinoOTA.onEnd(ota_on_end);
|
||||
ArduinoOTA.onProgress(ota_on_progress);
|
||||
ArduinoOTA.onError(ota_on_error);
|
||||
|
||||
ArduinoOTA.setHostname(haspDevice.get_hostname());
|
||||
// ArduinoOTA.setPassword(configPassword); // See OTA_PASSWORD
|
||||
ArduinoOTA.setPort(otaPort);
|
||||
|
||||
#if ESP32
|
||||
#if HASP_USE_MDNS > 0
|
||||
ArduinoOTA.setMdnsEnabled(false); // it's already started
|
||||
MDNS.enableArduino(_port, (_password.length() > 0)); // Add the Arduino SVC
|
||||
#endif
|
||||
// ArduinoOTA.setTimeout(1000); // default
|
||||
#endif
|
||||
ArduinoOTA.setRebootOnSuccess(false); // We do that ourselves
|
||||
|
||||
#ifdef OTA_PASSWORD
|
||||
ArduinoOTA.setPassword(OTA_PASSWORD); // TODO
|
||||
#endif
|
||||
|
||||
ArduinoOTA.begin();
|
||||
LOG_INFO(TAG_OTA, F(D_SERVICE_STARTED));
|
||||
} else {
|
||||
LOG_WARNING(TAG_OTA, F(D_SERVICE_DISABLED));
|
||||
}
|
||||
}
|
||||
|
||||
IRAM_ATTR void otaLoop(void)
|
||||
{
|
||||
ArduinoOTA.handle();
|
||||
@ -251,9 +268,37 @@ static void ota_on_http_error(int error)
|
||||
haspProgressMsg(F(D_OTA_UPDATE_FAILED));
|
||||
}
|
||||
|
||||
void ota_http_update(const char* espOtaUrl)
|
||||
void ota_http_update(const char* url)
|
||||
{ // Update ESP firmware from HTTP
|
||||
|
||||
t_httpUpdate_return returnCode;
|
||||
followRedirects_t redirectCode;
|
||||
|
||||
if(secureClient.connected() && url == strstr_P(url, PSTR("https://"))) { // not start with https
|
||||
LOG_ERROR(TAG_OTA, F("HTTP_UPDATE_FAILED client is already connected"));
|
||||
return;
|
||||
} else {
|
||||
StaticJsonDocument<512> doc; // update update url, get redirect setting
|
||||
JsonObject settings;
|
||||
settings = doc.to<JsonObject>(); // force creation of an empty JsonObject
|
||||
otaGetConfig(settings);
|
||||
settings["url"] = url;
|
||||
switch(settings["redirect"].as<uint8_t>()) {
|
||||
case 1:
|
||||
redirectCode = HTTPC_STRICT_FOLLOW_REDIRECTS;
|
||||
LOG_VERBOSE(TAG_OTA, F("Strict follow redirects"));
|
||||
break;
|
||||
case 2:
|
||||
redirectCode = HTTPC_FORCE_FOLLOW_REDIRECTS;
|
||||
LOG_VERBOSE(TAG_OTA, F("Force follow redirects"));
|
||||
break;
|
||||
default:
|
||||
redirectCode = HTTPC_DISABLE_FOLLOW_REDIRECTS;
|
||||
LOG_VERBOSE(TAG_OTA, F("Disable follow redirects"));
|
||||
settings["redirect"] = 0;
|
||||
}
|
||||
otaSetConfig(settings);
|
||||
}
|
||||
|
||||
#if HASP_USE_MDNS > 0
|
||||
mdnsStop(); // Keep mDNS responder from breaking things
|
||||
@ -266,7 +311,7 @@ void ota_http_update(const char* espOtaUrl)
|
||||
// ESPhttpUpdate.onError(update_error);
|
||||
ESP8266HTTPUpdate httpUpdate;
|
||||
httpUpdate.rebootOnUpdate(false); // We do that ourselves
|
||||
returnCode = httpUpdate.update(otaClient, espOtaUrl);
|
||||
returnCode = httpUpdate.update(otaClient, url);
|
||||
|
||||
#else
|
||||
HTTPUpdate httpUpdate;
|
||||
@ -276,38 +321,29 @@ void ota_http_update(const char* espOtaUrl)
|
||||
httpUpdate.onProgress(ota_on_http_progress);
|
||||
httpUpdate.onError(ota_on_http_error);
|
||||
httpUpdate.rebootOnUpdate(false); // We do that ourselves
|
||||
httpUpdate.setFollowRedirects(redirectCode);
|
||||
|
||||
if(espOtaUrl != strstr_P(espOtaUrl, PSTR("https://"))) { // not start with https
|
||||
returnCode = httpUpdate.update(otaClient, espOtaUrl);
|
||||
if(url != strstr_P(url, PSTR("https://"))) { // not start with https
|
||||
WiFiClient otaClient;
|
||||
returnCode = httpUpdate.update(otaClient, url);
|
||||
} else {
|
||||
WiFiClientSecure secureClient;
|
||||
// Reading data over SSL may be slow, use an adequate timeout
|
||||
secureClient.setTimeout(12); // timeout argument is defined in seconds
|
||||
|
||||
#if ESP_ARDUINO_VERSION_MAJOR >= 2
|
||||
/* This method is similar to the single root certificate verfication, but it uses a standard set of root
|
||||
* certificates from Mozilla to authenticate against. This allows the client to connect to all public SSL
|
||||
* servers. */
|
||||
secureClient.setCACertBundle(rootca_crt_bundle_start);
|
||||
#endif
|
||||
|
||||
returnCode = httpUpdate.update(secureClient, espOtaUrl, haspDevice.get_version());
|
||||
returnCode = httpUpdate.update(secureClient, url, haspDevice.get_version());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
switch(returnCode) {
|
||||
case HTTP_UPDATE_FAILED:
|
||||
LOG_ERROR(TAG_FWUP, F("HTTP_UPDATE_FAILED error %i %s"), httpUpdate.getLastError(),
|
||||
LOG_ERROR(TAG_OTA, F("HTTP_UPDATE_FAILED error %i %s"), httpUpdate.getLastError(),
|
||||
httpUpdate.getLastErrorString().c_str());
|
||||
break;
|
||||
|
||||
case HTTP_UPDATE_NO_UPDATES:
|
||||
LOG_TRACE(TAG_FWUP, F("HTTP_UPDATE_NO_UPDATES"));
|
||||
LOG_TRACE(TAG_OTA, F("HTTP_UPDATE_NO_UPDATES"));
|
||||
break;
|
||||
|
||||
case HTTP_UPDATE_OK:
|
||||
LOG_TRACE(TAG_FWUP, F("HTTP_UPDATE_OK"));
|
||||
LOG_TRACE(TAG_OTA, F("HTTP_UPDATE_OK"));
|
||||
dispatch_reboot(true);
|
||||
delay(5000);
|
||||
}
|
||||
@ -318,4 +354,46 @@ void ota_http_update(const char* espOtaUrl)
|
||||
}
|
||||
#endif // HASP_USE_HTTP_UPDATE
|
||||
|
||||
/* ===== Read/Write Configuration ===== */
|
||||
#if HASP_USE_CONFIG > 0
|
||||
bool otaGetConfig(const JsonObject& settings)
|
||||
{
|
||||
Preferences preferences;
|
||||
bool changed = false;
|
||||
|
||||
preferences.begin("ota", true);
|
||||
settings["url"] = preferences.getString("url", HASP_OTA_URL);
|
||||
settings["redirect"] = preferences.getUInt("redirect", 0);
|
||||
preferences.end();
|
||||
|
||||
// #if ESP_ARDUINO_VERSION_MAJOR >= 2
|
||||
// nvs_iterator_t it = nvs_entry_find("nvs", "ota", NVS_TYPE_ANY);
|
||||
// while(it != NULL) {
|
||||
// nvs_entry_info_t info;
|
||||
// nvs_entry_info(it, &info);
|
||||
// it = nvs_entry_next(it);
|
||||
// printf("key '%s', type '%d' \n", info.key, info.type);
|
||||
// };
|
||||
// #endif
|
||||
|
||||
if(changed) configOutput(settings, TAG_TIME);
|
||||
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool otaSetConfig(const JsonObject& settings)
|
||||
{
|
||||
Preferences preferences;
|
||||
preferences.begin("ota", false);
|
||||
|
||||
configOutput(settings, TAG_OTA);
|
||||
bool changed = false;
|
||||
changed |= nvsUpdateString(preferences, "url", settings["url"]);
|
||||
changed |= nvsUpdateUInt(preferences, "redirect", settings["redirect"]);
|
||||
|
||||
preferences.end();
|
||||
return changed;
|
||||
}
|
||||
#endif // HASP_USE_CONFIG
|
||||
|
||||
#endif // ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32
|
@ -1,19 +1,19 @@
|
||||
/* MIT License - Copyright (c) 2019-2022 Francis Van Roie
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#ifndef HASP_OTA_H
|
||||
#define HASP_OTA_H
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#include "hasp_conf.h"
|
||||
|
||||
#include "ArduinoJson.h"
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
/* ===== Default Event Processors ===== */
|
||||
#if HASP_USE_OTA > 0
|
||||
void otaSetup(void);
|
||||
#if HASP_USE_OTA > 0
|
||||
IRAM_ATTR void otaLoop(void);
|
||||
void otaEverySecond(void);
|
||||
#endif // HASP_USE_OTA
|
||||
@ -23,5 +23,13 @@ void otaEverySecond(void);
|
||||
void ota_http_update(const char* espOtaUrl);
|
||||
#endif // HASP_USE_HTTP_UPDATE
|
||||
|
||||
/* ===== Getter and Setter Functions ===== */
|
||||
|
||||
/* ===== Read/Write Configuration ===== */
|
||||
#if HASP_USE_CONFIG > 0
|
||||
bool otaGetConfig(const JsonObject& settings);
|
||||
bool otaSetConfig(const JsonObject& settings);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user