@@ -92,59 +92,20 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() {
92
92
}
93
93
94
94
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch () {
95
- if (fetchMode == OtaFetchTime) {
96
- return fetchTime ();
97
- } else {
98
- return fetchChunk ();
99
- }
100
- }
101
-
102
- OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetchChunk () {
103
95
OTACloudProcessInterface::State res = Fetch;
104
- int http_res = 0 ;
105
- uint32_t start = millis ();
106
- char range[128 ] = {0 };
107
-
108
- /* stop connected client */
109
- http_client->stop ();
110
-
111
- /* request chunk */
112
- http_client->beginRequest ();
113
- http_res = http_client->get (context->parsed_url .path ());
114
96
115
- if (username != nullptr && password != nullptr ) {
116
- http_client-> sendBasicAuth (username, password );
97
+ if (fetchMode == OtaFetchChunk ) {
98
+ res = requestChunk ( );
117
99
}
118
100
119
- size_t rangeSize = context->downloadedSize + context->maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : context->maxChunkSize ;
120
- sprintf (range, " bytes=%d-%d" , context->downloadedSize , context->downloadedSize + rangeSize);
121
- DEBUG_VERBOSE (" OTA downloading range: %s" , range);
122
- http_client->sendHeader (" Range" , range);
123
- http_client->endRequest ();
124
-
125
- if (http_res == HTTP_ERROR_CONNECTION_FAILED) {
126
- DEBUG_VERBOSE (" OTA ERROR: http client error connecting to server \" %s:%d\" " ,
127
- context->parsed_url .host (), context->parsed_url .port ());
128
- return ServerConnectErrorFail;
129
- } else if (http_res == HTTP_ERROR_TIMED_OUT) {
130
- DEBUG_VERBOSE (" OTA ERROR: http client timeout \" %s\" " , OTACloudProcessInterface::context->url );
131
- return OtaHeaderTimeoutFail;
132
- } else if (http_res != HTTP_SUCCESS) {
133
- DEBUG_VERBOSE (" OTA ERROR: http client returned %d on get \" %s\" " , res, OTACloudProcessInterface::context->url );
134
- return OtaDownloadFail;
135
- }
136
-
137
- int statusCode = http_client->responseStatusCode ();
101
+ context->downloadedChunkSize = 0 ;
102
+ context->downloadedChunkStartTime = millis ();
138
103
139
- if (statusCode != 206 ) {
140
- DEBUG_VERBOSE (" OTA ERROR: get response on \" %s\" returned status %d" , OTACloudProcessInterface::context->url , statusCode);
141
- return HttpResponseFail;
104
+ if (res != Fetch) {
105
+ goto exit;
142
106
}
143
107
144
- http_client->skipResponseHeaders ();
145
-
146
- /* download chunk */
147
- context->downloadedChunkSize = 0 ;
108
+ /* download chunked or timed */
148
109
do {
149
110
if (!http_client->connected ()) {
150
111
res = OtaDownloadFail;
@@ -157,7 +118,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetchChunk() {
157
118
continue ;
158
119
}
159
120
160
- http_res = http_client->read (context->buffer , context->bufLen );
121
+ int http_res = http_client->read (context->buffer , context->bufLen );
161
122
162
123
if (http_res < 0 ) {
163
124
DEBUG_VERBOSE (" OTA ERROR: Download read error %d" , http_res);
@@ -175,8 +136,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetchChunk() {
175
136
176
137
context->downloadedChunkSize += http_res;
177
138
178
- } while ((context->downloadState == OtaDownloadFile || context->downloadState == OtaDownloadHeader) &&
179
- (context->downloadedChunkSize < rangeSize));
139
+ } while (context->downloadState < OtaDownloadCompleted && fetchMore ());
180
140
181
141
// TODO verify that the information present in the ota header match the info in context
182
142
if (context->downloadState == OtaDownloadCompleted) {
@@ -209,70 +169,58 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetchChunk() {
209
169
return res;
210
170
}
211
171
212
- OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetchTime () {
213
- OTACloudProcessInterface::State res = Fetch;
172
+ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk () {
214
173
int http_res = 0 ;
215
174
uint32_t start = millis ();
175
+ char range[128 ] = {0 };
216
176
217
- do {
218
- if (!http_client->connected ()) {
219
- res = OtaDownloadFail;
220
- goto exit;
221
- }
177
+ /* stop connected client */
178
+ http_client->stop ();
222
179
223
- if (http_client->available () == 0 ) {
224
- /* Avoid tight loop and allow yield */
225
- delay (1 );
226
- continue ;
227
- }
180
+ /* request chunk */
181
+ http_client->beginRequest ();
182
+ http_res = http_client->get (context->parsed_url .path ());
228
183
229
- http_res = http_client->read (context->buffer , context->bufLen );
184
+ if (username != nullptr && password != nullptr ) {
185
+ http_client->sendBasicAuth (username, password);
186
+ }
230
187
231
- if (http_res < 0 ) {
232
- DEBUG_VERBOSE ( " OTA ERROR: Download read error %d " , http_res );
233
- res = OtaDownloadFail ;
234
- goto exit ;
235
- }
188
+ size_t rangeSize = context-> downloadedSize + maxChunkSize > context-> contentLength ? context-> contentLength - context-> downloadedSize : maxChunkSize;
189
+ sprintf (range, " bytes=%d-%d " , context-> downloadedSize , context-> downloadedSize + rangeSize );
190
+ DEBUG_VERBOSE ( " OTA downloading range: %s " , range) ;
191
+ http_client-> sendHeader ( " Range " , range) ;
192
+ http_client-> endRequest ();
236
193
237
- parseOta (context->buffer , http_res);
194
+ if (http_res == HTTP_ERROR_CONNECTION_FAILED) {
195
+ DEBUG_VERBOSE (" OTA ERROR: http client error connecting to server \" %s:%d\" " ,
196
+ context->parsed_url .host (), context->parsed_url .port ());
197
+ return ServerConnectErrorFail;
198
+ } else if (http_res == HTTP_ERROR_TIMED_OUT) {
199
+ DEBUG_VERBOSE (" OTA ERROR: http client timeout \" %s\" " , OTACloudProcessInterface::context->url );
200
+ return OtaHeaderTimeoutFail;
201
+ } else if (http_res != HTTP_SUCCESS) {
202
+ DEBUG_VERBOSE (" OTA ERROR: http client returned %d on get \" %s\" " , http_res, OTACloudProcessInterface::context->url );
203
+ return OtaDownloadFail;
204
+ }
238
205
239
- if (context->writeError ) {
240
- DEBUG_VERBOSE (" OTA ERROR: File write error" );
241
- res = ErrorWriteUpdateFileFail;
242
- goto exit;
243
- }
244
- } while ((context->downloadState == OtaDownloadFile || context->downloadState == OtaDownloadHeader) &&
245
- millis () - start < downloadTime);
206
+ int statusCode = http_client->responseStatusCode ();
246
207
247
- // TODO verify that the information present in the ota header match the info in context
248
- if ( context->downloadState == OtaDownloadCompleted) {
249
- // Verify that the downloaded file size is matching the expected size ??
250
- // this could distinguish between consistency of the downloaded bytes and filesize
208
+ if (statusCode != 206 ) {
209
+ DEBUG_VERBOSE ( " OTA ERROR: get response on \" %s \" returned status %d " , OTACloudProcessInterface:: context->url , statusCode);
210
+ return HttpResponseFail;
211
+ }
251
212
252
- // validate CRC
253
- context->calculatedCrc32 ^= 0xFFFFFFFF ; // finalize CRC
254
- if (context->header .header .crc32 == context->calculatedCrc32 ) {
255
- DEBUG_VERBOSE (" Ota download completed successfully" );
256
- res = FlashOTA;
257
- } else {
258
- res = OtaHeaderCrcFail;
259
- }
260
- } else if (context->downloadState == OtaDownloadError) {
261
- DEBUG_VERBOSE (" OTA ERROR: OtaDownloadError" );
213
+ http_client->skipResponseHeaders ();
262
214
263
- res = OtaDownloadFail;
264
- } else if (context->downloadState == OtaDownloadMagicNumberMismatch) {
265
- DEBUG_VERBOSE (" OTA ERROR: Magic number mismatch" );
266
- res = OtaHeaderMagicNumberFail;
267
- }
215
+ return Fetch;
216
+ }
268
217
269
- exit:
270
- if (res != Fetch ) {
271
- http_client-> stop (); // close the connection
272
- delete http_client;
273
- http_client = nullptr ;
218
+ bool OTADefaultCloudProcessInterface::fetchMore () {
219
+ if (fetchMode == OtaFetchChunk ) {
220
+ return context-> downloadedChunkSize < maxChunkSize;
221
+ } else {
222
+ return ( millis () - context-> downloadedChunkStartTime ) < downloadTime ;
274
223
}
275
- return res;
276
224
}
277
225
278
226
void OTADefaultCloudProcessInterface::parseOta (uint8_t * buffer, size_t bufLen) {
0 commit comments