Skip to content

Commit 3b4265b

Browse files
committed
OTA: rename requestChunk to requestOta and allow reusing
1 parent 74b1f17 commit 3b4265b

File tree

2 files changed

+16
-41
lines changed

2 files changed

+16
-41
lines changed

src/ota/interface/OTAInterfaceDefault.cpp

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,33 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::startOTA() {
4242
}
4343
);
4444

45-
// make the http get request
45+
// check url
4646
if(strcmp(context->parsed_url.schema(), "https") == 0) {
4747
http_client = new HttpClient(*client, context->parsed_url.host(), context->parsed_url.port());
4848
} else {
4949
return UrlParseErrorFail;
5050
}
5151

52-
http_client->beginRequest();
53-
auto res = http_client->get(context->parsed_url.path());
54-
55-
if(username != nullptr && password != nullptr) {
56-
http_client->sendBasicAuth(username, password);
57-
}
58-
59-
http_client->endRequest();
60-
61-
if(res == HTTP_ERROR_CONNECTION_FAILED) {
62-
DEBUG_VERBOSE("OTA ERROR: http client error connecting to server \"%s:%d\"",
63-
context->parsed_url.host(), context->parsed_url.port());
64-
return ServerConnectErrorFail;
65-
} else if(res == HTTP_ERROR_TIMED_OUT) {
66-
DEBUG_VERBOSE("OTA ERROR: http client timeout \"%s\"", OTACloudProcessInterface::context->url);
67-
return OtaHeaderTimeoutFail;
68-
} else if(res != HTTP_SUCCESS) {
69-
DEBUG_VERBOSE("OTA ERROR: http client returned %d on get \"%s\"", res, OTACloudProcessInterface::context->url);
70-
return OtaDownloadFail;
71-
}
72-
73-
int statusCode = http_client->responseStatusCode();
74-
75-
if(statusCode != 200) {
76-
DEBUG_VERBOSE("OTA ERROR: get response on \"%s\" returned status %d", OTACloudProcessInterface::context->url, statusCode);
77-
return HttpResponseFail;
78-
}
79-
80-
context->contentLength = http_client->contentLength();
52+
// make the http get request
53+
requestOta(OtaFetchTime);
8154

8255
// The following call is required to save the header value , keep it
56+
context->contentLength = http_client->contentLength();
8357
if(context->contentLength == HttpClient::kNoContentLengthHeader) {
8458
DEBUG_VERBOSE("OTA ERROR: the response header doesn't contain \"ContentLength\" field");
8559
return HttpHeaderErrorFail;
8660
}
8761

8862
context->lastReportTime = millis();
8963
DEBUG_VERBOSE("OTA file length: %d", context->contentLength);
90-
9164
return Fetch;
9265
}
9366

9467
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
9568
OTACloudProcessInterface::State res = Fetch;
9669

9770
if(fetchMode == OtaFetchChunk) {
98-
res = requestChunk();
71+
res = requestOta(OtaFetchChunk);
9972
}
10073

10174
context->downloadedChunkSize = 0;
@@ -169,10 +142,8 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::fetch() {
169142
return res;
170143
}
171144

172-
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk() {
145+
OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestOta(OTAFetchMode mode) {
173146
int http_res = 0;
174-
uint32_t start = millis();
175-
char range[128] = {0};
176147

177148
/* stop connected client */
178149
http_client->stop();
@@ -185,10 +156,14 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk()
185156
http_client->sendBasicAuth(username, password);
186157
}
187158

188-
size_t rangeSize = context->downloadedSize + maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : maxChunkSize;
189-
sprintf(range, "bytes=%d-%d", context->downloadedSize, context->downloadedSize + rangeSize);
190-
DEBUG_VERBOSE("OTA downloading range: %s", range);
191-
http_client->sendHeader("Range", range);
159+
if(mode == OtaFetchChunk) {
160+
char range[128] = {0};
161+
size_t rangeSize = context->downloadedSize + maxChunkSize > context->contentLength ? context->contentLength - context->downloadedSize : maxChunkSize;
162+
sprintf(range, "bytes=%d-%d", context->downloadedSize, context->downloadedSize + rangeSize);
163+
DEBUG_VERBOSE("OTA downloading range: %s", range);
164+
http_client->sendHeader("Range", range);
165+
}
166+
192167
http_client->endRequest();
193168

194169
if(http_res == HTTP_ERROR_CONNECTION_FAILED) {
@@ -205,7 +180,7 @@ OTACloudProcessInterface::State OTADefaultCloudProcessInterface::requestChunk()
205180

206181
int statusCode = http_client->responseStatusCode();
207182

208-
if(statusCode != 206) {
183+
if(((mode == OtaFetchChunk) && (statusCode != 206)) || ((mode == OtaFetchTime) && (statusCode != 200))) {
209184
DEBUG_VERBOSE("OTA ERROR: get response on \"%s\" returned status %d", OTACloudProcessInterface::context->url, statusCode);
210185
return HttpResponseFail;
211186
}

src/ota/interface/OTAInterfaceDefault.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class OTADefaultCloudProcessInterface: public OTACloudProcessInterface {
5050

5151
private:
5252
void parseOta(uint8_t* buffer, size_t bufLen);
53-
State requestChunk();
53+
State requestOta(OTAFetchMode mode);
5454
bool fetchMore();
5555

5656
Client* client;

0 commit comments

Comments
 (0)