Skip to content

Commit d3ee146

Browse files
committed
Read OTA download server certificate from SPIFFS
Signed-off-by: John Walicki <[email protected]>
1 parent 5a06750 commit d3ee146

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

WatsonIoT/src/main.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -387,34 +387,32 @@ bool FirmwareVersionCheck( char *firmware_latest, String firmware_ota_url ) {
387387
if( pemfile ) {
388388
char *DownloadServerPemChain = nullptr;
389389
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);
390+
DownloadServerPemChain = (char *)malloc(pemSize);
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();
394410
} 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-
}
411+
esp_task_wdt_init(5,0);
412+
Serial.println("The OpenEEW OTA firmware upgrade failed : ESP_FAIL");
415413
}
416-
free( DownloadServerPemChain );
417414
}
415+
free( DownloadServerPemChain );
418416
} else {
419417
Serial.println("Failed to open server pem chain.");
420418
}

0 commit comments

Comments
 (0)