Skip to content

Commit 6664b20

Browse files
committed
improved setconfig, obsoleted checkURL and useDeviceID
1 parent 7de9467 commit 6664b20

File tree

2 files changed

+65
-32
lines changed

2 files changed

+65
-32
lines changed

src/esp32FOTA.cpp

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,22 @@ esp32FOTA::~esp32FOTA(){}
120120

121121
esp32FOTA::esp32FOTA( FOTAConfig_t cfg )
122122
{
123-
setConfig( cfg );
123+
setConfig( cfg );
124124
}
125125

126126

127+
void esp32FOTA::setString( const char *dest, const char* src )
128+
{
129+
if( !src ) return;
130+
dest = (const char*)calloc( strlen(src)+1, sizeof(char));
131+
strcpy( (char*)dest, src );
132+
}
133+
134+
135+
127136
esp32FOTA::esp32FOTA(const char* firmwareType, int firmwareVersion, bool validate, bool allow_insecure_https)
128137
{
138+
setString( _cfg.name, firmwareType );
129139
_cfg.name = firmwareType;
130140
_cfg.sem = SemverClass( firmwareVersion );
131141
_cfg.check_sig = validate;
@@ -138,7 +148,7 @@ esp32FOTA::esp32FOTA(const char* firmwareType, int firmwareVersion, bool validat
138148

139149
esp32FOTA::esp32FOTA(const char* firmwareType, const char* firmwareSemanticVersion, bool validate, bool allow_insecure_https)
140150
{
141-
_cfg.name = firmwareType;
151+
setString( _cfg.name, firmwareType );
142152
_cfg.check_sig = validate;
143153
_cfg.unsafe = allow_insecure_https;
144154
_cfg.sem = SemverClass( firmwareSemanticVersion );
@@ -149,6 +159,21 @@ esp32FOTA::esp32FOTA(const char* firmwareType, const char* firmwareSemanticVersi
149159

150160

151161

162+
void esp32FOTA::setConfig( FOTAConfig_t cfg )
163+
{
164+
setString( _cfg.name, cfg.name );
165+
setString( _cfg.manifest_url, cfg.manifest_url );
166+
167+
_cfg.sem = cfg.sem;
168+
_cfg.check_sig = cfg.check_sig;
169+
_cfg.unsafe = cfg.unsafe;
170+
_cfg.use_device_id = cfg.use_device_id;
171+
_cfg.root_ca = cfg.root_ca;
172+
_cfg.pub_key = cfg.pub_key;
173+
}
174+
175+
176+
152177

153178
void esp32FOTA::setCertFileSystem( fs::FS *cert_filesystem )
154179
{
@@ -419,15 +444,17 @@ bool esp32FOTA::execOTA()
419444
bool esp32FOTA::execOTA( int partition, bool restart_after )
420445
{
421446
// health checks
447+
if( partition != U_SPIFFS && partition != U_FLASH ) {
448+
Serial.printf("Bad partition number: %i or empty URL, aborting\n", partition);
449+
return false;
450+
}
422451
if( partition == U_SPIFFS && _flashFileSystemUrl.isEmpty() ) {
423-
log_i("[SKIP] No spiffs/littlefs/fatfs partition was specified");
424-
return true; // data partition is optional, so not an error
425-
} else if ( partition == U_FLASH && _firmwareUrl.isEmpty() ) {
426-
log_e("No app partition was specified");
427-
return false; // app partition is mandatory
428-
} else if( partition != U_SPIFFS && partition != U_FLASH ) {
429-
log_e("Bad partition number: %i or empty URL", partition);
430-
return false;
452+
log_i("[SKIP] No spiffs/littlefs/fatfs partition was specified");
453+
return true; // data partition is optional, so not an error
454+
}
455+
if ( partition == U_FLASH && _firmwareUrl.isEmpty() ) {
456+
Serial.printf("No firmware URL, aborting\n");
457+
return false; // app partition is mandatory
431458
}
432459

433460
// call getHTTPStream
@@ -455,15 +482,15 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
455482
log_d("compression: %s", mode_z ? "enabled" : "disabled" );
456483

457484
if( _cfg.check_sig ) {
458-
if( mode_z ) {
459-
Serial.println("[ERROR] Compressed && signed image is not (yet) supported");
460-
return false;
461-
}
462-
if( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= FW_SIGNATURE_LENGTH ) {
463-
Serial.println("[ERROR] Malformed signature+fw combo");
464-
return false;
465-
}
466-
updateSize -= FW_SIGNATURE_LENGTH;
485+
if( mode_z ) {
486+
Serial.println("[ERROR] Compressed && signed image is not (yet) supported");
487+
return false;
488+
}
489+
if( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= FW_SIGNATURE_LENGTH ) {
490+
Serial.println("[ERROR] Malformed signature+fw combo");
491+
return false;
492+
}
493+
updateSize -= FW_SIGNATURE_LENGTH;
467494
}
468495

469496
// If using compression, the size is implicitely unknown
@@ -695,17 +722,22 @@ bool esp32FOTA::execHTTPcheck()
695722
{
696723
String useURL = String( _cfg.manifest_url );
697724

698-
// being deprecated, soon unsupported!
699-
if( useURL.isEmpty() && !checkURL.isEmpty() ) {
700-
Serial.println("checkURL will soon be unsupported, use FOTAConfig_t::manifest_url instead!!");
701-
useURL = checkURL;
725+
if( useURL.isEmpty() ) {
726+
Serial.println("No manifest_url provided in config, aborting!");
727+
return false;
702728
}
703729

704730
// being deprecated, soon unsupported!
705-
if( useDeviceID ) {
706-
Serial.println("useDeviceID will soon be unsupported, use FOTAConfig_t::use_device_id instead!!");
707-
_cfg.use_device_id = useDeviceID;
708-
}
731+
// if( useURL.isEmpty() && !checkURL.isEmpty() ) {
732+
// Serial.println("checkURL will soon be unsupported, use FOTAConfig_t::manifest_url instead!!");
733+
// useURL = checkURL;
734+
// }
735+
736+
// // being deprecated, soon unsupported!
737+
// if( useDeviceID ) {
738+
// Serial.println("useDeviceID will soon be unsupported, use FOTAConfig_t::use_device_id instead!!");
739+
// _cfg.use_device_id = useDeviceID;
740+
// }
709741

710742
if (_cfg.use_device_id) {
711743
// URL may already have GET values
@@ -792,7 +824,7 @@ void esp32FOTA::forceUpdate(const char* firmwareURL, bool validate )
792824

793825
void esp32FOTA::forceUpdate(const char* firmwareHost, uint16_t firmwarePort, const char* firmwarePath, bool validate )
794826
{
795-
String firmwareURL("http");
827+
static String firmwareURL("http");
796828
if ( firmwarePort == 443 || firmwarePort == 4433 ) firmwareURL += "s";
797829
firmwareURL += String(firmwareHost);
798830
firmwareURL += ":";

src/esp32FOTA.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ class esp32FOTA
241241
void useDeviceId( bool use=true ) { _cfg.use_device_id = use; }
242242

243243
// config setter
244-
void setConfig( FOTAConfig_t cfg ) { _cfg = cfg; }
244+
void setConfig( FOTAConfig_t cfg );
245245

246246
// Manually specify the manifest url, this is provided as a transition between legagy and new config system
247-
void setManifestURL( const String &manifest_url ) { _cfg.manifest_url = manifest_url.c_str(); }
247+
void setManifestURL( const String &manifest_url ) { setString( _cfg.manifest_url, manifest_url.c_str() ); }
248248

249249
// use this to set "Authorization: Basic" or other specific headers to be sent with the queries
250250
void setExtraHTTPHeader( String name, String value ) { extraHTTPHeaders[name] = value; }
@@ -311,8 +311,8 @@ class esp32FOTA
311311
bool setupHTTP( const char* url );
312312
void setFotaStream( Stream* stream ) { _stream = stream; }
313313

314-
[[deprecated("Use setManifestURL( String ) or cfg.manifest_url with setConfig( FOTAConfig_t )")]] String checkURL = "";
315-
[[deprecated("Use cfg.use_device_id with setConfig( FOTAConfig_t )")]] bool useDeviceID = false;
314+
//[[deprecated("Use setManifestURL( String ) or cfg.manifest_url with setConfig( FOTAConfig_t )")]] String checkURL = "";
315+
//[[deprecated("Use cfg.use_device_id with setConfig( FOTAConfig_t )")]] bool useDeviceID = false;
316316

317317

318318
private:
@@ -329,6 +329,7 @@ class esp32FOTA
329329

330330
void setupStream();
331331
void stopStream();
332+
void setString( const char *dest, const char* src ); // mem allocator
332333

333334
FOTAConfig_t _cfg;
334335

0 commit comments

Comments
 (0)