Skip to content

Commit cadbad8

Browse files
Add partition label argument to Update and ArduinoOTA classThe UpdateClass in the Updater component has the ability to update data toa SPIFFS partition. It selects the first available partition using theESP-IDF esp_partition_find_first() function.That behaviour is problematic if one has multiple SPIFFS partitions.This change allows a user to pass the label argument (defaults to NULL)to UpdateClass::begin() so a specific SPIFFS partition can be updated.Additionally, ArduinoOTA can set this partition label using thenew method ArduinoOTAClass::setPartitionLabel which is optional.This change does not break compatibility. (#4442)
The UpdateClass in the Updater component has the ability to update data to a SPIFFS partition. It selects the first available partition using the ESP-IDF esp_partition_find_first() function. That behaviour is problematic if one has multiple SPIFFS partitions. This change allows a user to pass the label argument (defaults to NULL) to UpdateClass::begin() so a specific SPIFFS partition can be updated. Additionally, ArduinoOTA can set this partition label using the new method ArduinoOTAClass::setPartitionLabel which is optional. This change does not break compatibility.
1 parent 3cbfa2f commit cadbad8

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Diff for: libraries/ArduinoOTA/src/ArduinoOTA.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ ArduinoOTAClass& ArduinoOTAClass::setPasswordHash(const char * password) {
8888
return *this;
8989
}
9090

91+
ArduinoOTAClass& ArduinoOTAClass::setPartitionLabel(const char * partition_label) {
92+
if (!_initialized && !_partition_label.length() && partition_label) {
93+
_partition_label = partition_label;
94+
}
95+
return *this;
96+
}
97+
98+
String ArduinoOTAClass::getPartitionLabel() {
99+
return _partition_label;
100+
}
101+
91102
ArduinoOTAClass& ArduinoOTAClass::setRebootOnSuccess(bool reboot){
92103
_rebootOnSuccess = reboot;
93104
return *this;
@@ -235,7 +246,8 @@ void ArduinoOTAClass::_onRx(){
235246
}
236247

237248
void ArduinoOTAClass::_runUpdate() {
238-
if (!Update.begin(_size, _cmd)) {
249+
const char *partition_label = _partition_label.length() ? _partition_label.c_str() : NULL;
250+
if (!Update.begin(_size, _cmd, -1, LOW, partition_label)) {
239251

240252
log_e("Begin ERROR: %s", Update.errorString());
241253

@@ -358,7 +370,7 @@ void ArduinoOTAClass::end() {
358370

359371
void ArduinoOTAClass::handle() {
360372
if (!_initialized) {
361-
return;
373+
return;
362374
}
363375
if (_state == OTA_RUNUPDATE) {
364376
_runUpdate();

Diff for: libraries/ArduinoOTA/src/ArduinoOTA.h

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class ArduinoOTAClass
4444
//Sets the password as above but in the form MD5(password). Default NULL
4545
ArduinoOTAClass& setPasswordHash(const char *password);
4646

47+
//Sets the partition label to write to when updating SPIFFS. Default NULL
48+
ArduinoOTAClass &setPartitionLabel(const char *partition_label);
49+
String getPartitionLabel();
50+
4751
//Sets if the device should be rebooted after successful update. Default true
4852
ArduinoOTAClass& setRebootOnSuccess(bool reboot);
4953

@@ -80,6 +84,7 @@ class ArduinoOTAClass
8084
int _port;
8185
String _password;
8286
String _hostname;
87+
String _partition_label;
8388
String _nonce;
8489
WiFiUDP _udp_ota;
8590
bool _initialized;

Diff for: libraries/Update/src/Update.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class UpdateClass {
4343
Call this to check the space needed for the update
4444
Will return false if there is not enough space
4545
*/
46-
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW);
46+
bool begin(size_t size=UPDATE_SIZE_UNKNOWN, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW, const char *label = NULL);
4747

4848
/*
4949
Writes a buffer to the flash and increments the address

Diff for: libraries/Update/src/Updater.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool UpdateClass::rollBack(){
104104
return _partitionIsBootable(partition) && !esp_ota_set_boot_partition(partition);
105105
}
106106

107-
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
107+
bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn, const char *label) {
108108
if(_size > 0){
109109
log_w("already running");
110110
return false;
@@ -132,7 +132,7 @@ bool UpdateClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
132132
log_d("OTA Partition: %s", _partition->label);
133133
}
134134
else if (command == U_SPIFFS) {
135-
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, NULL);
135+
_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_SPIFFS, label);
136136
if(!_partition){
137137
_error = UPDATE_ERROR_NO_PARTITION;
138138
return false;

0 commit comments

Comments
 (0)