|
| 1 | +// Include necessary headers for SDK configuration, Arduino framework, and networking |
1 | 2 | #include "sdkconfig.h" |
2 | 3 | #include <stdbool.h> |
3 | 4 | #if CONFIG_ESP_WIFI_REMOTE_ENABLED |
4 | 5 | #include "Arduino.h" |
5 | | -#include "esp32-hal-hosted.h" |
6 | | -#include "Network.h" |
7 | | -#include "HTTPClient.h" |
8 | | -#include "NetworkClientSecure.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 |
9 | 10 | #endif |
10 | 11 |
|
| 12 | +/** |
| 13 | + * Updates the ESP-Hosted co-processor firmware over-the-air (OTA) |
| 14 | + * This function downloads and installs firmware updates for the ESP-Hosted slave device |
| 15 | + * @return true if update was successful, false otherwise |
| 16 | + */ |
11 | 17 | bool updateEspHostedSlave() { |
12 | 18 | #if CONFIG_ESP_WIFI_REMOTE_ENABLED |
13 | 19 | bool updateSuccess = false; |
| 20 | + |
| 21 | + // Step 1: Verify ESP-Hosted is properly initialized |
14 | 22 | if (!hostedIsInitialized()) { |
15 | 23 | Serial.println("ERROR: esp-hosted is not initialized. Did you call WiFi.STA.begin()?"); |
16 | 24 | return updateSuccess; |
17 | 25 | } |
| 26 | + |
| 27 | + // Step 2: Check if an update is actually available |
18 | 28 | if (!hostedHasUpdate()) { |
19 | | - // esp-hosted is already the latest version |
| 29 | + // esp-hosted is already the latest version - no update needed |
20 | 30 | return updateSuccess; |
21 | 31 | } |
| 32 | + |
| 33 | + // Step 3: Ensure network connectivity is available |
22 | 34 | if (!Network.isOnline()) { |
23 | 35 | Serial.println("ERROR: Network is not online! Did you call WiFi.STA.connect(ssid, password)?"); |
24 | 36 | return updateSuccess; |
25 | 37 | } |
| 38 | + |
| 39 | + // Step 4: Begin the update process - display update URL |
26 | 40 | Serial.print("Updating esp-hosted co-processor from "); |
27 | 41 | Serial.println(hostedGetUpdateURL()); |
| 42 | + |
| 43 | + // Step 5: Create a secure network client for HTTPS communication |
28 | 44 | NetworkClientSecure *client = new NetworkClientSecure(); |
29 | 45 | if (!client) { |
30 | 46 | Serial.println("ERROR: Could not allocate client!"); |
31 | 47 | return updateSuccess; |
32 | 48 | } |
| 49 | + |
| 50 | + // Step 6: Configure client to skip certificate verification (insecure mode) |
33 | 51 | client->setInsecure(); |
| 52 | + |
| 53 | + // Step 7: Initialize HTTP client and attempt to connect to update server |
34 | 54 | HTTPClient https; |
35 | 55 | int httpCode = 0; |
36 | 56 | if (!https.begin(*client, hostedGetUpdateURL())) { |
37 | 57 | Serial.println("ERROR: HTTP begin failed!"); |
38 | 58 | goto finish_ota; |
39 | 59 | } |
| 60 | + |
| 61 | + // Step 8: Send HTTP GET request to download the firmware |
40 | 62 | httpCode = https.GET(); |
41 | 63 | if (httpCode == HTTP_CODE_OK) { |
| 64 | + // Step 9: Get the size of the firmware file to download |
42 | 65 | int len = https.getSize(); |
43 | 66 | if (len < 0) { |
44 | 67 | Serial.println("ERROR: Update size not received!"); |
45 | 68 | https.end(); |
46 | 69 | goto finish_ota; |
47 | 70 | } |
| 71 | + |
| 72 | + // Step 10: Get stream pointer for reading firmware data |
48 | 73 | NetworkClient *stream = https.getStreamPtr(); |
| 74 | + |
| 75 | + // Step 11: Initialize the ESP-Hosted update process |
49 | 76 | if (!hostedBeginUpdate()) { |
50 | 77 | Serial.println("ERROR: esp-hosted update start failed!"); |
51 | 78 | https.end(); |
52 | 79 | goto finish_ota; |
53 | 80 | } |
| 81 | + |
| 82 | + // Step 12: Allocate buffer for firmware data transfer (2KB chunks) |
54 | 83 | #define HOSTED_OTA_BUF_SIZE 2048 |
55 | 84 | uint8_t * buff = (uint8_t*)malloc(HOSTED_OTA_BUF_SIZE); |
56 | 85 | if (!buff) { |
57 | 86 | Serial.println("ERROR: Could not allocate OTA buffer!"); |
58 | 87 | https.end(); |
59 | 88 | goto finish_ota; |
60 | 89 | } |
| 90 | + |
| 91 | + // Step 13: Download and write firmware data in chunks |
61 | 92 | while (https.connected() && len > 0) { |
62 | 93 | size_t size = stream->available(); |
63 | 94 | if (size > 0) { |
| 95 | + // Show progress indicator |
64 | 96 | Serial.print("."); |
| 97 | + |
| 98 | + // Limit chunk size to buffer capacity |
65 | 99 | if (size > HOSTED_OTA_BUF_SIZE) { |
66 | 100 | size = HOSTED_OTA_BUF_SIZE; |
67 | 101 | } |
| 102 | + |
| 103 | + // Prevent reading more data than expected |
68 | 104 | if (size > len) { |
69 | 105 | Serial.printf("\nERROR: Update received extra bytes: %u!", size - len); |
70 | 106 | break; |
71 | 107 | } |
| 108 | + |
| 109 | + // Read firmware data chunk into buffer |
72 | 110 | int readLen = stream->readBytes(buff, size); |
73 | 111 | len -= readLen; |
| 112 | + |
| 113 | + // Write the chunk to ESP-Hosted co-processor |
74 | 114 | if (!hostedWriteUpdate(buff, readLen)) { |
75 | 115 | Serial.println("\nERROR: esp-hosted update write failed!"); |
76 | 116 | break; |
77 | 117 | } |
| 118 | + |
| 119 | + // Step 14: Check if entire firmware has been downloaded |
78 | 120 | if (len == 0) { |
| 121 | + // Finalize the update process |
79 | 122 | if (!hostedEndUpdate()) { |
80 | 123 | Serial.println("\nERROR: esp-hosted update end failed!"); |
81 | 124 | break; |
82 | 125 | } |
| 126 | + |
| 127 | + // Activate the new firmware |
83 | 128 | if (!hostedActivateUpdate()) { |
84 | 129 | Serial.println("\nERROR: esp-hosted update activate failed!"); |
85 | 130 | break; |
86 | 131 | } |
| 132 | + |
| 133 | + // Update completed successfully |
87 | 134 | updateSuccess = true; |
88 | 135 | Serial.println("\nSUCCESS: esp-hosted co-processor updated!"); |
89 | 136 | break; |
90 | 137 | } |
91 | 138 | } |
| 139 | + // Small delay to prevent overwhelming the system |
92 | 140 | delay(1); |
93 | 141 | } |
| 142 | + |
| 143 | + // Step 15: Clean up allocated buffer |
94 | 144 | free(buff); |
95 | 145 | Serial.println(); |
96 | 146 | } |
97 | 147 |
|
| 148 | + // Step 16: Close HTTP connection |
98 | 149 | https.end(); |
| 150 | + |
99 | 151 | finish_ota: |
| 152 | + // Step 17: Clean up network client |
100 | 153 | delete client; |
101 | 154 | return updateSuccess; |
102 | 155 | #else |
| 156 | + // ESP-Hosted functionality is not enabled in SDK configuration |
103 | 157 | return false; |
104 | 158 | #endif |
105 | 159 | } |
0 commit comments