Skip to content

Commit 5a06750

Browse files
committed
Detect empty SPIFFS /data partition
Signed-off-by: John Walicki <[email protected]>
1 parent 4107298 commit 5a06750

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

WatsonIoT/src/main.cpp

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -380,43 +380,49 @@ bool FirmwareVersionCheck( char *firmware_latest, String firmware_ota_url ) {
380380
// Launch an OTA upgrade
381381
NeoPixelStatus( LED_FIRMWARE_OTA ); // blink magenta
382382

383-
if( SPIFFS.begin(true) ) {
383+
if( SPIFFS.begin(false) ) {
384384
Serial.printf("Opening Server PEM Chain : %s\r\n", DOWNLOAD_CERT_PEM_FILE);
385-
File pemfile = SPIFFS.open( DOWNLOAD_CERT_PEM_FILE );
386-
if( pemfile ) {
387-
char *DownloadServerPemChain = nullptr;
388-
size_t pemSize = pemfile.size();
389-
DownloadServerPemChain = (char *)malloc(pemSize);
390-
391-
if( pemSize != pemfile.readBytes(DownloadServerPemChain, pemSize) ) {
392-
Serial.printf("Reading %s pem server certificate chain failed.\r\n",DOWNLOAD_CERT_PEM_FILE);
393-
} else {
394-
Serial.printf("Read %s pem server certificate chain from SPIFFS\r\n",DOWNLOAD_CERT_PEM_FILE);
395-
//Serial.write((const unsigned char*)DownloadServerPemChain,pemSize);
396-
397-
// Increase the watchdog timer before starting the firmware upgrade
398-
// The download and write can trip the watchdog timer and the old firmware
399-
// will abort / reset before the new firmware is complete.
400-
esp_task_wdt_init(15,0);
401-
402-
Serial.println("Starting OpenEEW OTA firmware upgrade...");
403-
esp_http_client_config_t config = {0};
404-
config.url = firmware_ota_url.c_str() ;
405-
config.cert_pem = DownloadServerPemChain ;
406-
esp_err_t ret = esp_https_ota(&config);
407-
if (ret == ESP_OK) {
408-
Serial.println("OTA upgrade downloaded. Restarting...");
409-
esp_restart();
410-
} else {
411-
esp_task_wdt_init(5,0);
412-
Serial.println("The OpenEEW OTA firmware upgrade failed : ESP_FAIL");
385+
if( SPIFFS.exists( DOWNLOAD_CERT_PEM_FILE )) {
386+
File pemfile = SPIFFS.open( DOWNLOAD_CERT_PEM_FILE );
387+
if( pemfile ) {
388+
char *DownloadServerPemChain = nullptr;
389+
size_t pemSize = pemfile.size();
390+
if( pemSize == 0 ) {
391+
DownloadServerPemChain = (char *)malloc(pemSize);
392+
if( pemSize != pemfile.readBytes(DownloadServerPemChain, pemSize) ) {
393+
Serial.printf("Reading %s pem server certificate chain failed.\r\n",DOWNLOAD_CERT_PEM_FILE);
394+
} else {
395+
Serial.printf("Read %s pem server certificate chain from SPIFFS\r\n",DOWNLOAD_CERT_PEM_FILE);
396+
Serial.write((const unsigned char*)DownloadServerPemChain,pemSize);
397+
398+
// Increase the watchdog timer before starting the firmware upgrade
399+
// The download and write can trip the watchdog timer and the old firmware
400+
// will abort / reset before the new firmware is complete.
401+
esp_task_wdt_init(15,0);
402+
403+
Serial.println("Starting OpenEEW OTA firmware upgrade...");
404+
esp_http_client_config_t config = {0};
405+
config.url = firmware_ota_url.c_str() ;
406+
config.cert_pem = DownloadServerPemChain ;
407+
esp_err_t ret = esp_https_ota(&config);
408+
if (ret == ESP_OK) {
409+
Serial.println("OTA upgrade downloaded. Restarting...");
410+
esp_restart();
411+
} else {
412+
esp_task_wdt_init(5,0);
413+
Serial.println("The OpenEEW OTA firmware upgrade failed : ESP_FAIL");
414+
}
415+
}
416+
free( DownloadServerPemChain );
413417
}
418+
} else {
419+
Serial.println("Failed to open server pem chain.");
414420
}
415-
free( DownloadServerPemChain );
421+
pemfile.close();
416422
} else {
417-
Serial.println("Failed to open server pem chain.");
423+
Serial.printf("The %s pem server certificate file does not exist.\r\n",DOWNLOAD_CERT_PEM_FILE);
424+
Serial.println("The SPIFFS filesystem might be empty.");
418425
}
419-
pemfile.close();
420426
} else {
421427
Serial.println("An error has occurred while mounting SPIFFS");
422428
}
@@ -526,6 +532,8 @@ template<typename L> void loadFromFile(const char* fname, L&& load) {
526532
File f = SPIFFS.open(fname);
527533
load(f, f.size());
528534
f.close();
535+
} else {
536+
Serial.printf("The certificate does not exist: %s\r\n",fname);
529537
}
530538
}
531539

0 commit comments

Comments
 (0)