55#ifndef ESP_CONFIG_PAGE_OTA_H
66#define ESP_CONFIG_PAGE_OTA_H
77
8+ #ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
9+ #include < esp_ota_ops.h>
10+ #include < esp_partition.h>
11+ #warning "Using ESP-IDF OTA API instead of Arduino's"
12+ #endif
13+
814namespace ESP_CONFIG_PAGE
915{
16+ #ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
17+ inline size_t writeOffset = 0 ;
18+ inline const esp_partition_t *otaPartition = nullptr ;
19+ inline esp_ota_handle_t otaHandle = 0 ;
20+ #endif
1021
22+ #ifndef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
1123#ifdef ESP32
1224#define GET_UPDATE_ERROR_STR Update.errorString()
1325#elif ESP8266
1426#define GET_UPDATE_ERROR_STR Update.getErrorString().c_str()
27+ #endif
1528#endif
1629
1730 inline void ota (bool isFilesystem)
@@ -45,6 +58,43 @@ namespace ESP_CONFIG_PAGE
4558 WiFiUDP::stopAll ();
4659#endif
4760
61+ #ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
62+ if (isFilesystem)
63+ {
64+ writeOffset = 0 ;
65+
66+ otaPartition = esp_partition_find_first (ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, " spiffs" );
67+ if (otaPartition == NULL ) {
68+ LOGN (" Error when starting update." );
69+ server->send (500 , " text/plain" , " Error starting update, filesystem partition is null" );
70+ return ;
71+ }
72+
73+ esp_err_t err = esp_partition_erase_range (otaPartition, 0 , otaPartition->size );
74+ if (err != ESP_OK) {
75+ LOGN (" Error when starting update." );
76+ server->send (500 , " text/plain" , " Error when erasing filesystem partition" );
77+ return ;
78+ }
79+ }
80+ else
81+ {
82+ otaPartition = esp_ota_get_next_update_partition (NULL );
83+ if (otaPartition == NULL )
84+ {
85+ LOGN (" Error when starting update." );
86+ server->send (500 , " text/plain" , " Start update error, next update partition is null" );
87+ return ;
88+ }
89+
90+ esp_err_t err = esp_ota_begin (otaPartition, OTA_SIZE_UNKNOWN, &otaHandle);
91+ if (err != ESP_OK) {
92+ LOGN (" Error when starting update." );
93+ server->send (500 , " text/plain" , " Error when setting up firmware ota update" );
94+ return ;
95+ }
96+ }
97+ #else
4898#ifdef ESP32
4999 uint32_t maxSpace = UPDATE_SIZE_UNKNOWN;
50100#elif ESP8266
@@ -64,19 +114,88 @@ namespace ESP_CONFIG_PAGE
64114 LOGN (" Error when starting update." );
65115 server->send (400 , " text/plain" , GET_UPDATE_ERROR_STR);
66116 }
117+ #endif
67118 }
68119 else if (upload.status == UPLOAD_FILE_WRITE)
69120 {
70121 LOGN (" Update write." );
122+
123+ #ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
124+ if (isFilesystem)
125+ {
126+ if (!otaPartition)
127+ {
128+ LOGN (" Error when writing to update." );
129+ server->send (500 , " text/plain" , " Error writing to partition, is null" );
130+ return ;
131+ }
132+
133+ esp_err_t err = esp_partition_write (otaPartition, writeOffset, upload.buf , upload.currentSize );
134+ if (err != ESP_OK) {
135+ LOGN (" Error when writing to update." );
136+ server->send (500 , " text/plain" , String (err));
137+ return ;
138+ }
139+
140+ writeOffset += upload.currentSize ;
141+ }
142+ else
143+ {
144+ if (otaHandle == 0 )
145+ {
146+ LOGN (" Error when writing to update." );
147+ server->send (500 , " text/plain" , " Ota handle is null" );
148+ return ;
149+ }
150+
151+ esp_ota_write (otaHandle, (const void *) upload.buf , upload.currentSize );
152+ }
153+ #else
71154 if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize )
72155 {
73156 LOGN (" Error when writing update." );
74157 server->send (400 , " text/plain" , GET_UPDATE_ERROR_STR);
75158 }
159+ #endif
76160 }
77161 else if (upload.status == UPLOAD_FILE_END)
78162 {
79163 LOGN (" Update ended." );
164+
165+ #ifdef ESP32_CONFIG_PAGE_USE_ESP_IDF_OTA
166+ if (isFilesystem)
167+ {
168+ server->send (200 , " text/plain" , " " );
169+ esp_restart ();
170+ }
171+ else {
172+ if (otaHandle == 0 )
173+ {
174+ LOGN (" Error when writing to update." );
175+ server->send (500 , " text/plain" , " Ota handle is null" );
176+ esp_restart ();
177+ return ;
178+ }
179+
180+ esp_err_t err = esp_ota_end (otaHandle);
181+ if (err != ESP_OK) {
182+ LOGF (" Ota update unsuccessful, err: %x\n " , err);
183+ server->send (500 , " text/plain" , String (" Ota update unsuccessful, err: " ) + err);
184+ esp_restart ();
185+ return ;
186+ }
187+
188+ err = esp_ota_set_boot_partition (otaPartition);
189+ if (err != ESP_OK) {
190+ LOGF (" Error setting ota partition as new boot partition: %x\n " , err);
191+ server->send (500 , " text/plain" , " Error setting ota partition as new boot partition." );
192+ esp_restart ();
193+ return ;
194+ }
195+
196+ LOGN (" Update successful" );
197+ }
198+ #else
80199 if (Update.end (true ))
81200 {
82201 server->send (200 );
@@ -86,14 +205,14 @@ namespace ESP_CONFIG_PAGE
86205 LOGN (" Error finishing update." );
87206 server->send (400 , " text/plain" , GET_UPDATE_ERROR_STR);
88207 }
208+ #endif
89209 }
90- yield ( );
210+ delay ( 10 );
91211 }
92212
93213 inline void otaEnd ()
94214 {
95- server->sendHeader (" Connection" , " close" );
96- server->send (200 , " text/plain" , (Update.hasError ()) ? " FAIL" : " OK" );
215+ server->send (200 , " text/plain" , " " );
97216 LOGN (" OTA update finished, restarting." );
98217 delay (100 );
99218 ESP.restart ();
@@ -114,7 +233,7 @@ namespace ESP_CONFIG_PAGE
114233 server->on (F (" /config/update/filesystem" ), HTTP_POST, []()
115234 {
116235 VALIDATE_AUTH ();
117- otaEnd ();
236+ // otaEnd();
118237 }, []()
119238 {
120239 VALIDATE_AUTH ();
0 commit comments