33#include < stdbool.h>
44#if CONFIG_ESP_WIFI_REMOTE_ENABLED
55#include " Arduino.h"
6- #include " esp32-hal-hosted.h" // ESP-Hosted specific functions
7- #include " Network.h" // Network connectivity management
8- #include " HTTPClient.h" // HTTP client for downloading updates
9- #include " NetworkClientSecure.h" // Secure network client for HTTPS
6+ #include " esp32-hal-hosted.h" // ESP-Hosted specific functions
7+ #include " Network.h" // Network connectivity management
8+ #include " HTTPClient.h" // HTTP client for downloading updates
9+ #include " NetworkClientSecure.h" // Secure network client for HTTPS
1010#endif
1111
1212/* *
1717bool updateEspHostedSlave () {
1818#if CONFIG_ESP_WIFI_REMOTE_ENABLED
1919 bool updateSuccess = false ;
20-
20+
2121 // Step 1: Verify ESP-Hosted is properly initialized
2222 if (!hostedIsInitialized ()) {
2323 Serial.println (" ERROR: esp-hosted is not initialized. Did you call WiFi.STA.begin()?" );
2424 return updateSuccess;
2525 }
26-
26+
2727 // Step 2: Check if an update is actually available
2828 if (!hostedHasUpdate ()) {
2929 // esp-hosted is already the latest version - no update needed
3030 return updateSuccess;
3131 }
32-
32+
3333 // Step 3: Ensure network connectivity is available
3434 if (!Network.isOnline ()) {
3535 Serial.println (" ERROR: Network is not online! Did you call WiFi.STA.connect(ssid, password)?" );
3636 return updateSuccess;
3737 }
38-
38+
3939 // Step 4: Begin the update process - display update URL
4040 Serial.print (" Updating esp-hosted co-processor from " );
4141 Serial.println (hostedGetUpdateURL ());
42-
42+
4343 // Step 5: Create a secure network client for HTTPS communication
4444 NetworkClientSecure *client = new NetworkClientSecure ();
4545 if (!client) {
4646 Serial.println (" ERROR: Could not allocate client!" );
4747 return updateSuccess;
4848 }
49-
49+
5050 // Step 6: Configure client to skip certificate verification (insecure mode)
5151 client->setInsecure ();
52-
52+
5353 // Step 7: Initialize HTTP client and attempt to connect to update server
5454 HTTPClient https;
5555 int httpCode = 0 ;
5656 if (!https.begin (*client, hostedGetUpdateURL ())) {
5757 Serial.println (" ERROR: HTTP begin failed!" );
5858 goto finish_ota;
5959 }
60-
60+
6161 // Step 8: Send HTTP GET request to download the firmware
6262 httpCode = https.GET ();
6363 if (httpCode == HTTP_CODE_OK) {
@@ -68,68 +68,68 @@ bool updateEspHostedSlave() {
6868 https.end ();
6969 goto finish_ota;
7070 }
71-
71+
7272 // Step 10: Get stream pointer for reading firmware data
7373 NetworkClient *stream = https.getStreamPtr ();
74-
74+
7575 // Step 11: Initialize the ESP-Hosted update process
7676 if (!hostedBeginUpdate ()) {
7777 Serial.println (" ERROR: esp-hosted update start failed!" );
7878 https.end ();
7979 goto finish_ota;
8080 }
81-
82- // Step 12: Allocate buffer for firmware data transfer (2KB chunks)
83- #define HOSTED_OTA_BUF_SIZE 2048
84- uint8_t * buff = (uint8_t *)malloc (HOSTED_OTA_BUF_SIZE);
81+
82+ // Step 12: Allocate buffer for firmware data transfer (2KB chunks)
83+ #define HOSTED_OTA_BUF_SIZE 2048
84+ uint8_t *buff = (uint8_t *)malloc (HOSTED_OTA_BUF_SIZE);
8585 if (!buff) {
8686 Serial.println (" ERROR: Could not allocate OTA buffer!" );
8787 https.end ();
8888 goto finish_ota;
8989 }
90-
90+
9191 // Step 13: Download and write firmware data in chunks
9292 while (https.connected () && len > 0 ) {
9393 size_t size = stream->available ();
9494 if (size > 0 ) {
9595 // Show progress indicator
9696 Serial.print (" ." );
97-
97+
9898 // Limit chunk size to buffer capacity
9999 if (size > HOSTED_OTA_BUF_SIZE) {
100100 size = HOSTED_OTA_BUF_SIZE;
101101 }
102-
102+
103103 // Prevent reading more data than expected
104104 if (size > len) {
105105 Serial.printf (" \n ERROR: Update received extra bytes: %u!" , size - len);
106106 break ;
107107 }
108-
108+
109109 // Read firmware data chunk into buffer
110110 int readLen = stream->readBytes (buff, size);
111111 len -= readLen;
112-
112+
113113 // Write the chunk to ESP-Hosted co-processor
114114 if (!hostedWriteUpdate (buff, readLen)) {
115115 Serial.println (" \n ERROR: esp-hosted update write failed!" );
116116 break ;
117117 }
118-
118+
119119 // Step 14: Check if entire firmware has been downloaded
120120 if (len == 0 ) {
121121 // Finalize the update process
122122 if (!hostedEndUpdate ()) {
123123 Serial.println (" \n ERROR: esp-hosted update end failed!" );
124124 break ;
125125 }
126-
126+
127127 // Activate the new firmware
128128 if (!hostedActivateUpdate ()) {
129129 Serial.println (" \n ERROR: esp-hosted update activate failed!" );
130130 break ;
131131 }
132-
132+
133133 // Update completed successfully
134134 updateSuccess = true ;
135135 Serial.println (" \n SUCCESS: esp-hosted co-processor updated!" );
@@ -139,15 +139,15 @@ bool updateEspHostedSlave() {
139139 // Small delay to prevent overwhelming the system
140140 delay (1 );
141141 }
142-
142+
143143 // Step 15: Clean up allocated buffer
144144 free (buff);
145145 Serial.println ();
146146 }
147147
148148 // Step 16: Close HTTP connection
149149 https.end ();
150-
150+
151151finish_ota:
152152 // Step 17: Clean up network client
153153 delete client;
0 commit comments