1515#include < NimBLEDevice.h>
1616#include < BLE_Custom_Characteristic.h>
1717
18+ #define BLE_OTA_LOG_TAG " BLE_OTA"
19+
1820/* ------------------------------------------------------------------------------
1921 BLE instances & variables
2022 ----------------------------------------------------------------------------*/
@@ -40,7 +42,7 @@ bool downloadFlag = false;
4042 ----------------------------------------------------------------------------*/
4143
4244class otaCallback : public BLECharacteristicCallbacks {
43- void onWrite (BLECharacteristic *pCharacteristic, ble_gap_conn_desc *connDesc) {
45+ void onWrite (NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo) override {
4446 std::string rxData = pCharacteristic->getValue ();
4547 bufferCount++;
4648
@@ -52,33 +54,36 @@ class otaCallback : public BLECharacteristicCallbacks {
5254 // update the connection interval so that it provides enough time for the long writes
5355
5456 Serial.printf (" 1. BeginOTA" );
55- BLEDevice::getServer ()->updateConnParams (connDesc-> conn_handle , 12 , 12 , 0 , 1000 );
57+ BLEDevice::getServer ()->updateConnParams (connInfo. getConnHandle () , 12 , 12 , 0 , 1000 );
5658 const esp_partition_t *configured = esp_ota_get_boot_partition ();
5759 const esp_partition_t *running = esp_ota_get_running_partition ();
5860
5961 if (configured != running) {
60- Serial. printf ( " ERROR: Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x" , configured->address , running->address );
61- Serial. printf ( " (This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)" );
62+ SS2K_LOG ( BLE_OTA_LOG_TAG , " ERROR: Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x" , configured->address , running->address );
63+ SS2K_LOG ( BLE_OTA_LOG_TAG , " (This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)" );
6264 downloadFlag = false ;
6365 esp_ota_end (otaHandler);
6466 } else {
65- Serial. printf ( " 2. Running partition type %d subtype %d (offset 0x%08x) \n " , running->type , running->subtype , running->address );
67+ SS2K_LOG ( BLE_OTA_LOG_TAG , " 2. Running partition type %d subtype %d (offset 0x%08x) \n " , running->type , running->subtype , running->address );
6668 }
6769
6870 update_partition = esp_ota_get_next_update_partition (NULL );
69- assert (update_partition != NULL );
71+ if (update_partition == NULL ) {
72+ SS2K_LOG (BLE_OTA_LOG_TAG , " ERROR: No valid partition found" );
73+ downloadFlag = false ;
74+ ss2k->rebootFlag = true ;
75+ return ;
76+ }
7077
71- Serial. printf ( " 3. Writing to partition subtype %d at offset 0x%x \n " , update_partition->subtype , update_partition->address );
78+ SS2K_LOG ( BLE_OTA_LOG_TAG , " 3. Writing to partition subtype %d at offset 0x%x \n " , update_partition->subtype , update_partition->address );
7279
7380 // ------------------------------------------------------------------------------------------
7481 // esp_ota_begin can take a while to complete as it erase the flash partition (3-5 seconds)
7582 // so make sure there's no timeout on the client side (iOS) that triggers before that.
7683 // ------------------------------------------------------------------------------------------
77- esp_task_wdt_config_t wdt_config = {
78- .timeout_ms = 10000 , // 10 seconds
79- .idle_core_mask = 0 ,
80- .trigger_panic = false
81- };
84+ esp_task_wdt_config_t wdt_config = {.timeout_ms = 20000 , // 20 seconds
85+ .idle_core_mask = 0 ,
86+ .trigger_panic = false };
8287 esp_task_wdt_init (&wdt_config);
8388
8489 // if (BLECommunicationTask != NULL) {
@@ -97,26 +102,27 @@ class otaCallback : public BLECharacteristicCallbacks {
97102 // vTaskDelay(5);
98103
99104 if (esp_ota_begin (update_partition, OTA_SIZE_UNKNOWN , &otaHandler) != ESP_OK ) {
100- downloadFlag = false ;
101- ss2k->isUpdating = false ;
105+ downloadFlag = false ;
106+ ss2k->isUpdating = false ;
107+ SS2K_LOG (BLE_OTA_LOG_TAG , " OTA begin failed" );
102108 return ;
103109 }
104110 downloadFlag = true ;
105111 }
106112
107113 if (bufferCount >= 1 || rxData.length () > 0 ) {
108114 if (esp_ota_write (otaHandler, (uint8_t *)rxData.c_str (), rxData.length ()) != ESP_OK ) {
109- Serial. printf ( " Error: write to flash failed" );
115+ SS2K_LOG ( BLE_OTA_LOG_TAG , " Error: write to flash failed" );
110116 downloadFlag = false ;
111117 pTxCharacteristic->notify (0x04 , 1 );
118+ ss2k->rebootFlag = true ;
112119 return ;
113120 } else {
114121 bufferCount = 1 ;
115122 // Serial.printf("%d bytes", rxData.length());
116123 // Notify the iOS app so next batch can be sent
117124 Serial.printf (" ." );
118- // pTxCharacteristic->setValue(0x02, sizeof(uint8_t));
119- // pTxCharacteristic->notify();
125+ pTxCharacteristic->notify (0x02 , sizeof (uint8_t ));
120126 }
121127
122128 // -------------------------------------------------------------------
@@ -126,50 +132,54 @@ class otaCallback : public BLECharacteristicCallbacks {
126132 // -------------------------------------------------------------------
127133 if (rxData.length () < 512 ) // TODO Asumes at least 511 data bytes (@BLE 4.2).
128134 {
129- Serial. printf ( " 4. Final byte arrived" );
135+ SS2K_LOG ( BLE_OTA_LOG_TAG , " 4. Final byte arrived" );
130136 // -----------------------------------------------------------------
131137 // Final chunk arrived. Now check that
132138 // the length of total file is correct
133139 // -----------------------------------------------------------------
134140 if (esp_ota_end (otaHandler) != ESP_OK ) {
135- Serial. printf ( " OTA end failed " );
141+ SS2K_LOG ( BLE_OTA_LOG_TAG , " OTA end failed " );
136142 downloadFlag = false ;
137143 pTxCharacteristic->notify (0x04 , sizeof (uint8_t ));
144+ ss2k->rebootFlag = true ;
138145 return ;
139146 }
140147 pTxCharacteristic->notify (0x05 , sizeof (uint8_t ));
141148 // -----------------------------------------------------------------
142149 // Clear download flag and restart the ESP32 if the firmware
143150 // update was successful
144151 // -----------------------------------------------------------------
145- Serial. printf ( " Set Boot partion " );
152+ SS2K_LOG ( BLE_OTA_LOG_TAG , " Set Boot partition " );
146153 if (ESP_OK == esp_ota_set_boot_partition (update_partition)) {
147154 esp_ota_end (otaHandler);
148155 downloadFlag = false ;
149- Serial. printf ( " Restarting..." );
156+ SS2K_LOG ( BLE_OTA_LOG_TAG , " Restarting..." );
150157 ss2k->rebootFlag = true ;
151158 return ;
152159 } else {
153160 // ------------------------------------------------------------
154161 // Something went wrong, the upload was not successful
155162 // ------------------------------------------------------------
156- Serial. printf ( " Upload Error" );
163+ SS2K_LOG ( BLE_OTA_LOG_TAG , " Upload Error" );
157164 pTxCharacteristic->notify (0x04 , sizeof (uint8_t ));
158165 downloadFlag = false ;
159166 esp_ota_end (otaHandler);
167+ ss2k->rebootFlag = true ;
160168 return ;
161169 }
162170 }
163171 } else {
164- ss2k->isUpdating = false ;
165- downloadFlag = false ;
172+ SS2K_LOG (BLE_OTA_LOG_TAG , " Data Length < 1" );
173+ ss2k->isUpdating = false ;
174+ downloadFlag = false ;
175+ ss2k->rebootFlag = true ;
166176 }
167177 }
168178};
169179
170- void BLEFirmwareSetup () {
180+ void BLEFirmwareSetup (NimBLEServer *pServer ) {
171181 // 3. Create BLE Service
172- NimBLEService *pService = spinBLEServer. pServer ->createService (FIRMWARE_SERVICE_UUID );
182+ NimBLEService *pService = pServer->createService (FIRMWARE_SERVICE_UUID );
173183
174184 // 4. Create BLE Characteristics inside the service(s)
175185 pTxCharacteristic = pService->createCharacteristic (FIRMWARE_CHARACTERISTIC_TX_UUID , NIMBLE_PROPERTY ::READ | NIMBLE_PROPERTY ::WRITE | NIMBLE_PROPERTY ::NOTIFY );
0 commit comments