forked from arduino-libraries/ArduinoIoTCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOTANanoRP2040.cpp
142 lines (110 loc) · 3.79 KB
/
OTANanoRP2040.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
This file is part of the ArduinoIoTCloud library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <AIoTC_Config.h>
#if defined(ARDUINO_NANO_RP2040_CONNECT) && OTA_ENABLED
#include <SFU.h>
#include "OTANanoRP2040.h"
#include <Arduino_DebugUtils.h>
#include "mbed.h"
#include "utility/watchdog/Watchdog.h"
#define SD_MOUNT_PATH "ota"
#define FULL_UPDATE_FILE_PATH "/ota/UPDATE.BIN"
const char NANO_RP2040OTACloudProcess::UPDATE_FILE_NAME[] = FULL_UPDATE_FILE_PATH;
NANO_RP2040OTACloudProcess::NANO_RP2040OTACloudProcess(MessageStream *ms, Client* client)
: OTADefaultCloudProcessInterface(ms, client)
, flash((uint32_t)appStartAddress() + 0xF00000, 0x100000) // TODO make this numbers a constant
, decompressed(nullptr)
, fs(nullptr) {
}
NANO_RP2040OTACloudProcess::~NANO_RP2040OTACloudProcess() {
close_fs();
}
OTACloudProcessInterface::State NANO_RP2040OTACloudProcess::resume(Message* msg) {
return OtaBegin;
}
int NANO_RP2040OTACloudProcess::writeFlash(uint8_t* const buffer, size_t len) {
if(decompressed == nullptr) {
DEBUG_VERBOSE("writing on a file that is not open"); // FIXME change log message
return 0;
}
return fwrite(buffer, sizeof(uint8_t), len, decompressed);
}
OTACloudProcessInterface::State NANO_RP2040OTACloudProcess::startOTA() {
int err = -1;
if ((err = flash.init()) < 0) {
DEBUG_VERBOSE("%s: flash.init() failed with %d", __FUNCTION__, err);
return OtaStorageInitFail;
}
flash.erase((uint32_t)appStartAddress() + 0xF00000, 0x100000);
fs = new mbed::FATFileSystem(SD_MOUNT_PATH); // FIXME can this be allocated in the stack?
if ((err = fs->reformat(&flash)) != 0) {
DEBUG_VERBOSE("%s: fs.reformat() failed with %d", __FUNCTION__, err);
return ErrorReformatFail;
}
decompressed = fopen(UPDATE_FILE_NAME, "wb"); // TODO make this a constant
if (!decompressed) {
DEBUG_VERBOSE("%s: fopen() failed", __FUNCTION__);
fclose(decompressed);
return ErrorOpenUpdateFileFail;
}
// we start the download here
return OTADefaultCloudProcessInterface::startOTA();;
}
OTACloudProcessInterface::State NANO_RP2040OTACloudProcess::flashOTA() {
int err = 0;
if((err = close_fs()) != 0) {
return ErrorUnmountFail;
}
return Reboot;
}
OTACloudProcessInterface::State NANO_RP2040OTACloudProcess::reboot() {
NVIC_SystemReset();
return Resume; // This won't ever be reached
}
void NANO_RP2040OTACloudProcess::reset() {
OTADefaultCloudProcessInterface::reset();
close_fs();
}
int NANO_RP2040OTACloudProcess::close_fs() {
int err = 0;
if(decompressed != nullptr) {
fclose(decompressed);
decompressed = nullptr;
}
if (fs != nullptr && (err = fs->unmount()) != 0) {
DEBUG_VERBOSE("%s: fs.unmount() failed with %d", __FUNCTION__, err);
} else {
delete fs;
fs = nullptr;
}
return err;
}
bool NANO_RP2040OTACloudProcess::isOtaCapable() {
return true;
}
extern uint32_t __flash_binary_start;
extern uint32_t __flash_binary_end;
#if defined(UNINITIALIZED_DATA_SECTION)
extern uint32_t __uninitialized_data_start__;
extern uint32_t __uninitialized_data_end__;
#endif
void* NANO_RP2040OTACloudProcess::appStartAddress() {
#if defined(UNINITIALIZED_DATA_SECTION)
return &__flash_binary_start;
#else
return (void*)XIP_BASE;
#endif
}
uint32_t NANO_RP2040OTACloudProcess::appSize() {
#if defined(UNINITIALIZED_DATA_SECTION)
return ((&__flash_binary_end - (uint32_t*)appStartAddress()) - (&__uninitialized_data_end__ - &__uninitialized_data_start__)) * sizeof(void*);
#else
return (&__flash_binary_end - (uint32_t*)appStartAddress()) * sizeof(void*);
#endif
}
#endif // defined(ARDUINO_NANO_RP2040_CONNECT) && OTA_ENABLED