|
36 | 36 | #include <esp_delta_ota.h>
|
37 | 37 | #endif // CONFIG_ENABLE_DELTA_OTA
|
38 | 38 |
|
39 |
| -#if defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
40 |
| -#include "esp_br_firmware.h" |
41 |
| -#include "esp_check.h" |
42 |
| -#include "esp_rcp_update.h" |
43 |
| -#endif |
44 |
| - |
45 | 39 | #define TAG "OTAImageProcessor"
|
46 | 40 |
|
47 | 41 | #ifdef CONFIG_ENABLE_DELTA_OTA
|
48 | 42 | #define PATCH_HEADER_SIZE 64
|
49 | 43 | #define DIGEST_SIZE 32
|
50 | 44 | #endif // CONFIG_ENABLE_DELTA_OTA
|
51 | 45 |
|
52 |
| -#if defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
53 |
| -#define MAX_WRITE_RETRY_COUNT 5 |
54 |
| -#define OTA_MAX_WRITE_SIZE 16 |
55 |
| -#endif // defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
56 |
| - |
57 | 46 | using namespace chip::System;
|
58 | 47 | using namespace ::chip::DeviceLayer::Internal;
|
59 | 48 |
|
@@ -292,178 +281,33 @@ esp_err_t OTAImageProcessorImpl::DeltaOTAWriteCallback(const uint8_t * buf, size
|
292 | 281 | #endif // CONFIG_ENABLE_DELTA_OTA
|
293 | 282 |
|
294 | 283 | #if defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER)
|
295 |
| -typedef enum rcpImageState |
| 284 | +esp_err_t OTAImageProcessorImpl::ProcessRcpImage(intptr_t context, const uint8_t * buffer, uint32_t bufLen) |
296 | 285 | {
|
297 |
| - BR_OTA_INIT = 0, |
298 |
| - RCP_DOWNLOAD, |
299 |
| - BR_FW_DOWNLOAD, |
300 |
| -} RcpImageState_t; |
| 286 | + esp_err_t err = ESP_OK; |
| 287 | + auto * imageProcessor = reinterpret_cast<OTAImageProcessorImpl *>(context); |
301 | 288 |
|
302 |
| -static int WriteFileForLength(FILE * fp, const void * buf, size_t size) |
303 |
| -{ |
304 |
| - int retryCount = 0; |
305 |
| - int offset = 0; |
306 |
| - const uint8_t * data = (const uint8_t *) buf; |
307 |
| - while (offset < size) |
| 289 | + if (!imageProcessor->mRcpDone) |
308 | 290 | {
|
309 |
| - int ret = fwrite(data + offset, 1, ((size - offset) < OTA_MAX_WRITE_SIZE ? (size - offset) : OTA_MAX_WRITE_SIZE), fp); |
310 |
| - if (ret < 0) |
311 |
| - { |
312 |
| - return ret; |
313 |
| - } |
314 |
| - if (ret == 0) |
315 |
| - { |
316 |
| - retryCount++; |
317 |
| - } |
318 |
| - else |
319 |
| - { |
320 |
| - offset += ret; |
321 |
| - retryCount = 0; |
322 |
| - } |
323 |
| - |
324 |
| - if (retryCount > MAX_WRITE_RETRY_COUNT) |
325 |
| - { |
326 |
| - return -1; |
327 |
| - } |
328 |
| - } |
329 |
| - return size; |
330 |
| -} |
331 |
| - |
332 |
| -esp_err_t OTAImageProcessorImpl::ProcessRcpImage(esp_ota_handle_t handle, const uint8_t * buffer, uint32_t bufLengthen) |
333 |
| -{ |
334 |
| - static char rcpTargetPath[RCP_FILENAME_MAX_SIZE]; |
335 |
| - static RcpImageState_t state = BR_OTA_INIT; |
336 |
| - static const char * rcpFirmwareDir = esp_rcp_get_firmware_dir(); |
337 |
| - static int8_t rcpUpdateSeq = esp_rcp_get_next_update_seq(); |
338 |
| - static uint32_t headerSize = 0; |
339 |
| - static uint32_t totalRevSize = 0; |
340 |
| - static uint32_t brFirmwareOffset = 0; |
341 |
| - static uint32_t brFirmwareSize = 0; |
342 |
| - static FILE * fp = NULL; |
343 |
| - |
344 |
| - switch (state) |
345 |
| - { |
346 |
| - case BR_OTA_INIT: { |
347 |
| - sprintf(rcpTargetPath, "%s_%d/" ESP_BR_RCP_IMAGE_FILENAME, rcpFirmwareDir, rcpUpdateSeq); |
348 |
| - ESP_LOGI(TAG, "Downloading RCP target file %s", rcpTargetPath); |
349 |
| - |
350 |
| - fp = fopen(rcpTargetPath, "w"); |
351 |
| - if (!fp) |
352 |
| - { |
353 |
| - ESP_LOGE(TAG, "Fail to open %s, will delete it", rcpTargetPath); |
354 |
| - remove(rcpTargetPath); |
355 |
| - return ESP_FAIL; |
356 |
| - } |
357 |
| - |
358 |
| - esp_br_subfile_info_t subFileInfo[7]; |
359 |
| - if (sizeof(subFileInfo) > bufLengthen) |
360 |
| - { |
361 |
| - ESP_LOGE(TAG, "Packet len is less than the RCP header len"); |
362 |
| - if (fp != NULL) |
363 |
| - { |
364 |
| - fclose(fp); |
365 |
| - } |
366 |
| - return ESP_FAIL; |
367 |
| - } |
368 |
| - |
369 |
| - memcpy((char *) subFileInfo, buffer, sizeof(subFileInfo)); |
370 |
| - for (int i = 0; i < 7; i++) |
371 |
| - { |
372 |
| - ESP_LOGD(TAG, "subFileInfo[%d].tag:0x%lx--offset:0x%lx---size:0x%lx\n", i, subFileInfo[i].tag, subFileInfo[i].offset, |
373 |
| - subFileInfo[i].size); |
374 |
| - if (subFileInfo[i].tag == FILETAG_IMAGE_HEADER) |
375 |
| - { |
376 |
| - headerSize = subFileInfo[i].size; |
377 |
| - ESP_LOGI(TAG, "RCP image header-----headerSize:%ld\n", headerSize); |
378 |
| - } |
379 |
| - else if (subFileInfo[i].tag == FILETAG_BR_FIRMWARE) |
380 |
| - { |
381 |
| - brFirmwareOffset = subFileInfo[i].offset; |
382 |
| - brFirmwareSize = subFileInfo[i].size; |
383 |
| - ESP_LOGI(TAG, "Border Router image--offset:%ld--size:%ld\n", brFirmwareOffset, brFirmwareSize); |
384 |
| - } |
385 |
| - } |
386 |
| - |
387 |
| - if ((headerSize != sizeof(subFileInfo)) || (brFirmwareOffset == 0) || (brFirmwareSize == 0)) |
388 |
| - { |
389 |
| - ESP_LOGE(TAG, "RCP header error"); |
390 |
| - fclose(fp); |
391 |
| - return ESP_FAIL; |
392 |
| - } |
| 291 | + size_t rcpOtaReceivedLen = 0; |
| 292 | + err = esp_rcp_ota_receive(imageProcessor->mRcpOtaHandle, buffer, bufLen, &rcpOtaReceivedLen); |
393 | 293 |
|
394 |
| - if (WriteFileForLength(fp, buffer, bufLengthen) != bufLengthen) |
| 294 | + if (esp_rcp_ota_get_state(imageProcessor->mRcpOtaHandle) == ESP_RCP_OTA_STATE_FINISHED) |
395 | 295 | {
|
396 |
| - ESP_LOGE(TAG, "Failed to write data"); |
397 |
| - fclose(fp); |
398 |
| - return ESP_FAIL; |
| 296 | + imageProcessor->mBrFirmwareSize = esp_rcp_ota_get_subfile_size(imageProcessor->mRcpOtaHandle, FILETAG_HOST_FIRMWARE); |
| 297 | + err = esp_ota_write(imageProcessor->mOTAUpdateHandle, buffer + rcpOtaReceivedLen, bufLen - rcpOtaReceivedLen); |
| 298 | + imageProcessor->mRcpDone = true; |
399 | 299 | }
|
400 |
| - |
401 |
| - state = RCP_DOWNLOAD; |
402 |
| - totalRevSize = bufLengthen; |
403 |
| - break; |
404 |
| - } |
405 |
| - case RCP_DOWNLOAD: { |
406 |
| - if (totalRevSize + bufLengthen >= brFirmwareOffset) |
407 |
| - { |
408 |
| - uint32_t len = brFirmwareOffset - totalRevSize; |
409 |
| - if (WriteFileForLength(fp, buffer, len) == len) |
410 |
| - { |
411 |
| - ESP_LOGI(TAG, "RCP receive done, total size %ld bytes", brFirmwareOffset); |
412 |
| - state = BR_FW_DOWNLOAD; |
413 |
| - fclose(fp); |
414 |
| - if (esp_rcp_submit_new_image() != ESP_OK) |
415 |
| - { |
416 |
| - ESP_LOGI(TAG, "Failed to submit RCP image"); |
417 |
| - state = BR_OTA_INIT; |
418 |
| - return ESP_FAIL; |
419 |
| - } |
420 |
| - } |
421 |
| - else |
422 |
| - { |
423 |
| - ESP_LOGE(TAG, "Failed to write data"); |
424 |
| - fclose(fp); |
425 |
| - state = BR_OTA_INIT; |
426 |
| - return ESP_FAIL; |
427 |
| - } |
428 |
| - |
429 |
| - if (esp_ota_write(handle, buffer + len, bufLengthen - len) != ESP_OK) |
430 |
| - { |
431 |
| - ESP_LOGE(TAG, "OTA write failed"); |
432 |
| - state = BR_OTA_INIT; |
433 |
| - return ESP_FAIL; |
434 |
| - } |
435 |
| - |
436 |
| - totalRevSize = bufLengthen - len; |
437 |
| - } |
438 |
| - else |
439 |
| - { |
440 |
| - if (WriteFileForLength(fp, buffer, bufLengthen) != bufLengthen) |
441 |
| - { |
442 |
| - ESP_LOGE(TAG, "Failed to write data"); |
443 |
| - fclose(fp); |
444 |
| - state = BR_OTA_INIT; |
445 |
| - return ESP_FAIL; |
446 |
| - } |
447 |
| - totalRevSize += bufLengthen; |
448 |
| - } |
449 |
| - break; |
450 |
| - } |
451 |
| - case BR_FW_DOWNLOAD: { |
452 |
| - if (esp_ota_write(handle, buffer, bufLengthen) != ESP_OK) |
453 |
| - { |
454 |
| - ESP_LOGE(TAG, "OTA write failed"); |
455 |
| - state = BR_OTA_INIT; |
456 |
| - return ESP_FAIL; |
457 |
| - ESP_LOGI(TAG, "Thread BR image receive done"); |
458 |
| - } |
459 |
| - break; |
460 | 300 | }
|
461 |
| - default: { |
462 |
| - return ESP_FAIL; |
| 301 | + else if (imageProcessor->mBrFirmwareSize > 0) |
| 302 | + { |
| 303 | + err = esp_ota_write(imageProcessor->mOTAUpdateHandle, buffer, bufLen); |
463 | 304 | }
|
| 305 | + else |
| 306 | + { |
| 307 | + err = ESP_FAIL; |
464 | 308 | }
|
465 | 309 |
|
466 |
| - return ESP_OK; |
| 310 | + return err; |
467 | 311 | }
|
468 | 312 | #endif
|
469 | 313 |
|
@@ -524,6 +368,16 @@ void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context)
|
524 | 368 | }
|
525 | 369 | #endif // CONFIG_ENABLE_ENCRYPTED_OTA
|
526 | 370 |
|
| 371 | +#if defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
| 372 | + imageProcessor->mRcpOtaHandle = 0; |
| 373 | + imageProcessor->mBrFirmwareSize = 0; |
| 374 | + imageProcessor->mRcpDone = false; |
| 375 | + if (esp_rcp_ota_begin(&imageProcessor->mRcpOtaHandle) != ESP_OK) |
| 376 | + { |
| 377 | + return; |
| 378 | + } |
| 379 | +#endif |
| 380 | + |
527 | 381 | imageProcessor->mHeaderParser.Init();
|
528 | 382 | imageProcessor->mDownloader->OnPreparedForDownload(CHIP_NO_ERROR);
|
529 | 383 | PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadInProgress);
|
@@ -567,6 +421,12 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
|
567 | 421 |
|
568 | 422 | err = esp_ota_end(imageProcessor->mOTAUpdateHandle);
|
569 | 423 | DeltaOTACleanUp(reinterpret_cast<intptr_t>(imageProcessor));
|
| 424 | +#elif defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
| 425 | + esp_err_t err = esp_rcp_ota_end(imageProcessor->mRcpOtaHandle); |
| 426 | + err |= esp_ota_end(imageProcessor->mOTAUpdateHandle); |
| 427 | + imageProcessor->mRcpOtaHandle = 0; |
| 428 | + imageProcessor->mBrFirmwareSize = 0; |
| 429 | + imageProcessor->mRcpDone = false; |
570 | 430 | #else
|
571 | 431 | esp_err_t err = esp_ota_end(imageProcessor->mOTAUpdateHandle);
|
572 | 432 | #endif // CONFIG_ENABLE_DELTA_OTA
|
@@ -608,6 +468,16 @@ void OTAImageProcessorImpl::HandleAbort(intptr_t context)
|
608 | 468 | DeltaOTACleanUp(reinterpret_cast<intptr_t>(imageProcessor));
|
609 | 469 | #endif // CONFIG_ENABLE_DELTA_OTA
|
610 | 470 |
|
| 471 | +#if defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER) |
| 472 | + if (esp_rcp_ota_abort(imageProcessor->mRcpOtaHandle) != ESP_OK) |
| 473 | + { |
| 474 | + ESP_LOGE(TAG, "ESP RCP OTA abort failed"); |
| 475 | + } |
| 476 | + imageProcessor->mRcpOtaHandle = 0; |
| 477 | + imageProcessor->mBrFirmwareSize = 0; |
| 478 | + imageProcessor->mRcpDone = false; |
| 479 | +#endif |
| 480 | + |
611 | 481 | if (esp_ota_abort(imageProcessor->mOTAUpdateHandle) != ESP_OK)
|
612 | 482 | {
|
613 | 483 | ESP_LOGE(TAG, "ESP OTA abort failed");
|
@@ -671,7 +541,7 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
|
671 | 541 | // Apply the patch and writes that data to the passive partition.
|
672 | 542 | err = esp_delta_ota_feed_patch(imageProcessor->mDeltaOTAUpdateHandle, blockToWrite.data() + index, blockToWrite.size() - index);
|
673 | 543 | #elif defined(CONFIG_AUTO_UPDATE_RCP) && defined(CONFIG_OPENTHREAD_BORDER_ROUTER)
|
674 |
| - err = imageProcessor->ProcessRcpImage(imageProcessor->mOTAUpdateHandle, blockToWrite.data(), blockToWrite.size()); |
| 544 | + err = imageProcessor->ProcessRcpImage(context, blockToWrite.data(), blockToWrite.size()); |
675 | 545 | #else
|
676 | 546 | err = esp_ota_write(imageProcessor->mOTAUpdateHandle, blockToWrite.data(), blockToWrite.size());
|
677 | 547 | #endif // CONFIG_ENABLE_DELTA_OTA
|
|
0 commit comments