Skip to content

Commit 1d4f51b

Browse files
implemented samd ota class
1 parent 08067d5 commit 1d4f51b

File tree

2 files changed

+132
-8
lines changed

2 files changed

+132
-8
lines changed

src/ota/implementation/OTASamd.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,97 @@
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+
113
#if defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED
214
#include "OTASamd.h"
315

16+
#include <Arduino_DebugUtils.h>
17+
#if OTA_STORAGE_SNU
18+
# include <SNU.h>
19+
# include <WiFiNINA.h> /* WiFiStorage */
20+
#endif
21+
22+
SAMDOTACloudProcess::SAMDOTACloudProcess(MessageStream *ms)
23+
: OTACloudProcessInterface(ms){
24+
25+
}
26+
27+
OTACloudProcessInterface::State SAMDOTACloudProcess::resume(Message* msg) {
28+
return OtaBegin;
29+
}
30+
31+
OTACloudProcessInterface::State SAMDOTACloudProcess::startOTA() {
32+
reset();
33+
return Fetch;
34+
}
35+
36+
OTACloudProcessInterface::State SAMDOTACloudProcess::fetch() {
37+
#if OTA_STORAGE_SNU
38+
uint8_t nina_ota_err_code = 0;
39+
if (!WiFiStorage.downloadOTA(this->context->url, &nina_ota_err_code)) {
40+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code);
41+
switch(static_cast<ninaOTAError>(nina_ota_err_code)) {
42+
case ninaOTAError::Open:
43+
return ErrorOpenUpdateFileFail;
44+
case ninaOTAError::Length:
45+
return OtaDownloadFail;
46+
case ninaOTAError::CRC:
47+
return OtaHeaderCrcFail;
48+
case ninaOTAError::Rename:
49+
return ErrorRenameFail;
50+
default:
51+
return OtaDownloadFail;
52+
}
53+
}
54+
#endif // OTA_STORAGE_SNU
55+
56+
return FlashOTA;
57+
}
58+
59+
OTACloudProcessInterface::State SAMDOTACloudProcess::flashOTA() {
60+
return Reboot;
61+
}
62+
63+
OTACloudProcessInterface::State SAMDOTACloudProcess::reboot() {
64+
NVIC_SystemReset();
65+
}
66+
67+
void SAMDOTACloudProcess::reset() {
68+
#if OTA_STORAGE_SNU
69+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS");
70+
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
71+
#endif // OTA_STORAGE_SNU
72+
}
73+
74+
bool SAMDOTACloudProcess::isOtaCapable() {
75+
#if OTA_STORAGE_SNU
76+
if (strcmp(WiFi.firmwareVersion(), "1.4.1") < 0) {
77+
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
78+
return false;
79+
} else {
80+
return true;
81+
}
82+
#endif
83+
return false;
84+
}
85+
86+
extern void* __text_start__;
87+
extern void* __etext;
88+
extern void* __data_end__;
89+
extern void* __data_start__;
90+
91+
void* SAMDOTACloudProcess::appStartAddress() { return &__text_start__; }
92+
93+
uint32_t SAMDOTACloudProcess::appSize() {
94+
return ((&__etext - &__text_start__) + (&__data_end__ - &__data_start__))*sizeof(void*);
95+
}
96+
497
#endif // defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED

src/ota/implementation/OTASamd.h

+39-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
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+
111
#pragma once
212

3-
#include "src/ota/interface/OTAInterface.h"
13+
#include "ota/interface/OTAInterface.h"
414
#include <Arduino_DebugUtils.h>
515

616
class SAMDOTACloudProcess: public OTACloudProcessInterface {
717
public:
8-
STM32H7OTACloudProcess();
18+
SAMDOTACloudProcess(MessageStream *ms);
19+
20+
virtual bool isOtaCapable() override;
921
protected:
10-
// we start the download and decompress process
11-
virtual State fetch(Message* msg=nullptr);
22+
virtual OTACloudProcessInterface::State resume(Message* msg=nullptr) override;
1223

13-
// when the download is completed we verify for integrity and correctness of the downloaded binary
14-
// virtual State verifyOTA(Message* msg=nullptr); // TODO this may be performed inside download
24+
// we are overriding the method of startOTA in order to download ota file on ESP32
25+
virtual OTACloudProcessInterface::State startOTA() override;
26+
27+
// we start the download and decompress process
28+
virtual OTACloudProcessInterface::State fetch() override;
1529

1630
// whene the download is correctly finished we set the mcu to use the newly downloaded binary
17-
virtual State flashOTA(Message* msg=nullptr);
31+
virtual OTACloudProcessInterface::State flashOTA();
1832

1933
// we reboot the device
20-
virtual State reboot(Message* msg=nullptr);
34+
virtual OTACloudProcessInterface::State reboot();
35+
36+
virtual void reset() override;
37+
38+
void* appStartAddress();
39+
uint32_t appSize();
40+
41+
bool appFlashOpen() { return true; }
42+
bool appFlashClose() { return true; }
43+
44+
private:
45+
enum class ninaOTAError : int {
46+
None = 0,
47+
Open = 1,
48+
Length = 2,
49+
CRC = 3,
50+
Rename = 4,
51+
};
2152
};

0 commit comments

Comments
 (0)