|
| 1 | +/* |
| 2 | + This file is part of ArduinoIoTCloud. |
| 3 | +
|
| 4 | + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 5 | +
|
| 6 | + This software is released under the GNU General Public License version 3, |
| 7 | + which covers the main part of arduino-cli. |
| 8 | + The terms of this license can be found at: |
| 9 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | +
|
| 11 | + You can be released from the requirements of the above licenses by purchasing |
| 12 | + a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + otherwise use the software for commercial activities involving the Arduino |
| 14 | + software without disclosing the source code of your own applications. To purchase |
| 15 | + a commercial license, send an email to license@arduino.cc. |
| 16 | +*/ |
| 17 | + |
| 18 | +/****************************************************************************** |
| 19 | + * INCLUDE |
| 20 | + ******************************************************************************/ |
| 21 | + |
| 22 | +#include <AIoTC_Config.h> |
| 23 | + |
| 24 | +#if defined ARDUINO_ARCH_ESP32 && OTA_ENABLED |
| 25 | +#include "OTA.h" |
| 26 | +#include <Arduino_DebugUtils.h> |
| 27 | +#include <Arduino_ESP_OTA.h> |
| 28 | + |
| 29 | +/****************************************************************************** |
| 30 | + * FUNCTION DEFINITION |
| 31 | + ******************************************************************************/ |
| 32 | + |
| 33 | +int esp32_onOTARequest(char const * ota_url) |
| 34 | +{ |
| 35 | + Arduino_ESP32_OTA::Error ota_err = Arduino_ESP32_OTA::Error::None; |
| 36 | + Arduino_ESP32_OTA ota; |
| 37 | + |
| 38 | + /* Initialize the board for OTA handling. */ |
| 39 | + if ((ota_err = ota.begin()) != Arduino_ESP32_OTA::Error::None) |
| 40 | + { |
| 41 | + DEBUG_ERROR("Arduino_ESP32_OTA::begin() failed with %d", static_cast<int>(ota_err)); |
| 42 | + return static_cast<int>(ota_err); |
| 43 | + } |
| 44 | + |
| 45 | + /* Download the OTA file from the web storage location. */ |
| 46 | + int const ota_download = ota.download(ota_url); |
| 47 | + if (ota_download <= 0) |
| 48 | + { |
| 49 | + DEBUG_ERROR("Arduino_ESP_OTA::download() failed with %d", ota_download); |
| 50 | + return ota_download; |
| 51 | + } |
| 52 | + DEBUG_VERBOSE("Arduino_ESP_OTA::download() %d bytes downloaded", static_cast<int>(ota_download)); |
| 53 | + |
| 54 | + /* Verify update integrity and apply */ |
| 55 | + if ((ota_err = ota.update()) != Arduino_ESP32_OTA::Error::None) |
| 56 | + { |
| 57 | + DEBUG_ERROR("Arduino_ESP_OTA::update() failed with %d", static_cast<int>(ota_err)); |
| 58 | + return static_cast<int>(ota_err); |
| 59 | + } |
| 60 | + |
| 61 | + /* Perform the reset to reboot */ |
| 62 | + ota.reset(); |
| 63 | + |
| 64 | + return static_cast<int>(OTAError::None); |
| 65 | +} |
| 66 | + |
| 67 | +#endif /* ARDUINO_ARCH_ESP32 */ |
0 commit comments