|
| 1 | +/* |
| 2 | + This file is part of the ArduinoIoTCloud library. |
| 3 | +
|
| 4 | + Copyright (c) 2024 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <AIoTC_Config.h> |
| 12 | + |
1 | 13 | #if defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED
|
2 | 14 | #include "OTAUnoR4.h"
|
3 | 15 |
|
| 16 | +#include <Arduino_DebugUtils.h> |
| 17 | +#include "tls/utility/SHA256.h" |
| 18 | +#include "fsp_common_api.h" |
| 19 | +#include "r_flash_lp.h" |
| 20 | +#include "WiFi.h" |
| 21 | + |
| 22 | +/****************************************************************************** |
| 23 | + * DEFINES |
| 24 | + ******************************************************************************/ |
| 25 | + |
| 26 | +const char UNOR4OTACloudProcess::UPDATE_FILE_NAME[] = "/update.bin"; |
| 27 | + |
| 28 | +static OTACloudProcessInterface::State convertUnor4ErrorToState(int error_code); |
| 29 | + |
| 30 | +UNOR4OTACloudProcess::UNOR4OTACloudProcess(MessageStream *ms) |
| 31 | +: OTACloudProcessInterface(ms){ |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +OTACloudProcessInterface::State UNOR4OTACloudProcess::resume(Message* msg) { |
| 36 | + return OtaBegin; |
| 37 | +} |
| 38 | + |
| 39 | +OTACloudProcessInterface::State UNOR4OTACloudProcess::startOTA() { |
| 40 | + int ota_err = OTAUpdate::OTA_ERROR_NONE; |
| 41 | + |
| 42 | + // Open fs for ota |
| 43 | + if((ota_err = ota.begin(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { |
| 44 | + DEBUG_VERBOSE("OTAUpdate::begin() failed with %d", ota_err); |
| 45 | + return convertUnor4ErrorToState(ota_err); |
| 46 | + } |
| 47 | + |
| 48 | + return Fetch; |
| 49 | +} |
| 50 | + |
| 51 | +OTACloudProcessInterface::State UNOR4OTACloudProcess::fetch() { |
| 52 | + int ota_err = OTAUpdate::OTA_ERROR_NONE; |
| 53 | + |
| 54 | + int const ota_download = ota.download(this->context->url,UPDATE_FILE_NAME); |
| 55 | + if (ota_download <= 0) { |
| 56 | + DEBUG_VERBOSE("OTAUpdate::download() failed with %d", ota_download); |
| 57 | + return convertUnor4ErrorToState(ota_download); |
| 58 | + } |
| 59 | + DEBUG_VERBOSE("OTAUpdate::download() %d bytes downloaded", ota_download); |
| 60 | + |
| 61 | + if ((ota_err = ota.verify()) != OTAUpdate::OTA_ERROR_NONE) { |
| 62 | + DEBUG_VERBOSE("OTAUpdate::verify() failed with %d", ota_err); |
| 63 | + return convertUnor4ErrorToState(ota_err); |
| 64 | + } |
| 65 | + |
| 66 | + return FlashOTA; |
| 67 | +} |
| 68 | + |
| 69 | +OTACloudProcessInterface::State UNOR4OTACloudProcess::flashOTA() { |
| 70 | + int ota_err = OTAUpdate::OTA_ERROR_NONE; |
| 71 | + |
| 72 | + /* Flash new firmware */ |
| 73 | + if ((ota_err = ota.update(UPDATE_FILE_NAME)) != OTAUpdate::OTA_ERROR_NONE) { // This reboots the MCU |
| 74 | + DEBUG_VERBOSE("OTAUpdate::update() failed with %d", ota_err); |
| 75 | + return convertUnor4ErrorToState(ota_err); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +OTACloudProcessInterface::State UNOR4OTACloudProcess::reboot() { |
| 80 | +} |
| 81 | + |
| 82 | +void UNOR4OTACloudProcess::reset() { |
| 83 | +} |
| 84 | + |
| 85 | +bool UNOR4OTACloudProcess::isOtaCapable() { |
| 86 | + String const fv = WiFi.firmwareVersion(); |
| 87 | + if (fv < String("0.3.0")) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +extern void* __ROM_Start; |
| 94 | +extern void* __etext; |
| 95 | +extern void* __data_end__; |
| 96 | +extern void* __data_start__; |
| 97 | + |
| 98 | +constexpr void* UNOR4OTACloudProcess::appStartAddress() { return &__ROM_Start; } |
| 99 | +uint32_t UNOR4OTACloudProcess::appSize() { |
| 100 | + return ((&__etext - &__ROM_Start) + (&__data_end__ - &__data_start__))*sizeof(void*); |
| 101 | +} |
| 102 | + |
| 103 | +bool UNOR4OTACloudProcess::appFlashOpen() { |
| 104 | + cfg.data_flash_bgo = false; |
| 105 | + cfg.p_callback = nullptr; |
| 106 | + cfg.p_context = nullptr; |
| 107 | + cfg.p_extend = nullptr; |
| 108 | + cfg.ipl = (BSP_IRQ_DISABLED); |
| 109 | + cfg.irq = FSP_INVALID_VECTOR; |
| 110 | + cfg.err_ipl = (BSP_IRQ_DISABLED); |
| 111 | + cfg.err_irq = FSP_INVALID_VECTOR; |
| 112 | + |
| 113 | + fsp_err_t rv = FSP_ERR_UNSUPPORTED; |
| 114 | + |
| 115 | + rv = R_FLASH_LP_Open(&ctrl,&cfg); |
| 116 | + DEBUG_VERBOSE("Flash open %X", rv); |
| 117 | + |
| 118 | + return rv == FSP_SUCCESS; |
| 119 | +} |
| 120 | + |
| 121 | +bool UNOR4OTACloudProcess::appFlashClose() { |
| 122 | + fsp_err_t rv = FSP_ERR_UNSUPPORTED; |
| 123 | + rv = R_FLASH_LP_Close(&ctrl); |
| 124 | + DEBUG_VERBOSE("Flash close %X", rv); |
| 125 | + |
| 126 | + return rv == FSP_SUCCESS; |
| 127 | +} |
| 128 | + |
| 129 | +static OTACloudProcessInterface::State convertUnor4ErrorToState(int error_code) { |
| 130 | + switch(error_code) { |
| 131 | + case -2: |
| 132 | + return OTACloudProcessInterface::NoOtaStorageFail; |
| 133 | + case -3: |
| 134 | + return OTACloudProcessInterface::OtaStorageInitFail; |
| 135 | + case -4: |
| 136 | + return OTACloudProcessInterface::OtaStorageEndFail; |
| 137 | + case -5: |
| 138 | + return OTACloudProcessInterface::UrlParseErrorFail; |
| 139 | + case -6: |
| 140 | + return OTACloudProcessInterface::ServerConnectErrorFail; |
| 141 | + case -7: |
| 142 | + return OTACloudProcessInterface::HttpHeaderErrorFail; |
| 143 | + case -8: |
| 144 | + return OTACloudProcessInterface::ParseHttpHeaderFail; |
| 145 | + case -9: |
| 146 | + return OTACloudProcessInterface::OtaHeaderLengthFail; |
| 147 | + case -10: |
| 148 | + return OTACloudProcessInterface::OtaHeaderCrcFail; |
| 149 | + case -11: |
| 150 | + return OTACloudProcessInterface::OtaHeaterMagicNumberFail; |
| 151 | + case -12: |
| 152 | + return OTACloudProcessInterface::OtaDownloadFail; |
| 153 | + case -13: |
| 154 | + return OTACloudProcessInterface::OtaHeaderTimeoutFail; |
| 155 | + case -14: |
| 156 | + return OTACloudProcessInterface::HttpResponseFail; |
| 157 | + case -25: |
| 158 | + return OTACloudProcessInterface::LibraryFail; |
| 159 | + case -26: |
| 160 | + return OTACloudProcessInterface::ModemFail; |
| 161 | + default: |
| 162 | + DEBUG_VERBOSE("Unrecognized error code %d", error_code); |
| 163 | + return OTACloudProcessInterface::Fail; |
| 164 | + } |
| 165 | +} |
| 166 | + |
4 | 167 | #endif // defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED
|
0 commit comments