Skip to content

Commit ef73a65

Browse files
committed
OTA: use OtaFlags to enable Chunked download mode
1 parent a5015e9 commit ef73a65

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
105105
/* Slower but more reliable in some corner cases */
106106
void setOTAChunkMode(bool enable = true) {
107107
if(enable) {
108-
_ota.setFetchMode(OTADefaultCloudProcessInterface::OtaFetchChunk);
108+
_ota.enableOtaPolicy(OTADefaultCloudProcessInterface::ChunkDownload);
109109
} else {
110-
_ota.setFetchMode(OTADefaultCloudProcessInterface::OtaFetchTime);
110+
_ota.disableOtaPolicy(OTADefaultCloudProcessInterface::ChunkDownload);
111111
}
112112
}
113113
#endif

src/ota/interface/OTAInterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class OTACloudProcessInterface: public CloudProcess {
8080
enum OtaFlags: uint16_t {
8181
None = 0,
8282
ApprovalRequired = 1,
83-
Approved = 1<<1
83+
Approved = 1<<1,
84+
ChunkDownload = 1<<2
8485
};
8586

8687
virtual void handleMessage(Message*);

src/ota/interface/OTAInterfaceDefault.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ OTADefaultCloudProcessInterface::OTADefaultCloudProcessInterface(MessageStream *
2020
, client(client)
2121
, http_client(nullptr)
2222
, username(nullptr), password(nullptr)
23-
, fetchMode(OtaFetchTime)
2423
, context(nullptr) {
2524
}
2625

@@ -50,7 +49,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() {
5049
}
5150

5251
// make the http get request
53-
OTACloudProcessInterface::State res = requestOta(OtaFetchTime);
52+
OTACloudProcessInterface::State res = requestOta();
5453
if(res != Fetch) {
5554
return res;
5655
}
@@ -70,8 +69,8 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() {
7069
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
7170
OTACloudProcessInterface::State res = Fetch;
7271

73-
if(fetchMode == OtaFetchChunk) {
74-
res = requestOta(OtaFetchChunk);
72+
if(getOtaPolicy(ChunkDownload)) {
73+
res = requestOta(ChunkDownload);
7574
}
7675

7776
context->downloadedChunkSize = 0;
@@ -145,7 +144,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
145144
return res;
146145
}
147146

148-
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta(OTAFetchMode mode) {
147+
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta(OtaFlags mode) {
149148
int http_res = 0;
150149

151150
/* stop connected client */
@@ -159,7 +158,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta(OTAF
159158
http_client->sendBasicAuth(username, password);
160159
}
161160

162-
if(mode == OtaFetchChunk) {
161+
if((mode & ChunkDownload) == ChunkDownload) {
163162
char range[128] = {0};
164163
size_t rangeSize = context->downloadedSize + maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : maxChunkSize;
165164
sprintf(range, "bytes=%d-%d", context->downloadedSize, context->downloadedSize + rangeSize);
@@ -183,18 +182,18 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta(OTAF
183182

184183
int statusCode = http_client->responseStatusCode();
185184

186-
if(((mode == OtaFetchChunk) && (statusCode != 206)) || ((mode == OtaFetchTime) && (statusCode != 200))) {
185+
if((((mode & ChunkDownload) == ChunkDownload) && (statusCode != 206)) ||
186+
(((mode & ChunkDownload) != ChunkDownload) && (statusCode != 200))) {
187187
DEBUG_VERBOSE("OTA ERROR: get response on \"%s\" returned status %d", OTACloudProcessInterface::context->url, statusCode);
188188
return HttpResponseFail;
189189
}
190190

191191
http_client->skipResponseHeaders();
192-
193192
return Fetch;
194193
}
195194

196195
bool OTADefaultCloudProcessInterface::fetchMore() {
197-
if (fetchMode == OtaFetchChunk) {
196+
if (getOtaPolicy(ChunkDownload)) {
198197
return context->downloadedChunkSize < maxChunkSize;
199198
} else {
200199
return (millis() - context->downloadedChunkStartTime) < downloadTime;

src/ota/interface/OTAInterfaceDefault.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ class OTADefaultCloudProcessInterface: public OTACloudProcessInterface {
3535
this->password = password;
3636
}
3737

38-
enum OTAFetchMode: uint8_t {
39-
OtaFetchTime,
40-
OtaFetchChunk
41-
};
42-
43-
inline virtual void setFetchMode(OTAFetchMode mode) { this->fetchMode = mode; }
44-
4538
protected:
4639
State startOTA();
4740
State fetch();
@@ -50,19 +43,20 @@ class OTADefaultCloudProcessInterface: public OTACloudProcessInterface {
5043

5144
private:
5245
void parseOta(uint8_t* buffer, size_t bufLen);
53-
State requestOta(OTAFetchMode mode);
46+
State requestOta(OtaFlags mode = None);
5447
bool fetchMore();
5548

5649
Client* client;
5750
HttpClient* http_client;
5851

5952
const char *username, *password;
60-
OTAFetchMode fetchMode;
6153

6254
// The amount of time that each iteration of Fetch has to take at least
6355
// This mitigate the issues arising from tasks run in main loop that are using all the computing time
6456
static constexpr uint32_t downloadTime = 2000;
6557

58+
// The amount of data that each iteration of Fetch has to take at least
59+
// This should be enabled setting ChunkDownload OtaFlag to 1 and mitigate some Ota corner cases
6660
static constexpr size_t maxChunkSize = 1024 * 10;
6761

6862
enum OTADownloadState: uint8_t {

0 commit comments

Comments
 (0)