Skip to content

Commit eed2a8c

Browse files
committed
Establish connection to OTA images server when _ota_req is set to 'true' and download OTA image from there.
1 parent 169509a commit eed2a8c

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/ArduinoIoTCloudTCP.cpp

+61-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
9797
, _ota_error{static_cast<int>(OTAError::None)}
9898
, _ota_img_sha256{"Inv."}
9999
, _ota_url{""}
100-
, _otq_request{false}
100+
, _ota_req{false}
101101
#endif /* OTA_ENABLED */
102102
{
103103

@@ -249,7 +249,7 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
249249
addPropertyReal(_ota_error, "OTA_ERROR", Permission::Read);
250250
addPropertyReal(_ota_img_sha256, "OTA_SHA256", Permission::Read);
251251
addPropertyReal(_ota_url, "OTA_URL", Permission::ReadWrite).onSync(DEVICE_WINS);
252-
addPropertyReal(_otq_request, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS);
252+
addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS).onUpdate(ArduinoIoTCloudTCP::on_OTA_REQ_Update);
253253
_ota_logic.setOTAStorage(ota_storage);
254254
}
255255
#endif /* OTA_ENABLED */
@@ -417,6 +417,65 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
417417
return 0;
418418
}
419419

420+
#if OTA_ENABLED
421+
void ArduinoIoTCloudTCP::on_OTA_REQ_Update()
422+
{
423+
ArduinoCloud.onOTARequest();
424+
}
425+
426+
void ArduinoIoTCloudTCP::onOTARequest()
427+
{
428+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_req = %s", __FUNCTION__, _ota_req ? "true" : "false");
429+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
430+
431+
if (_ota_req)
432+
{
433+
WiFiSSLClient ota_client;
434+
if (!ota_client.connect("www.107-systems.org", 443)) {
435+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s ota_client.connect failed", __FUNCTION__);
436+
return;
437+
}
438+
439+
/* Request binary via http-get */
440+
char get_msg[128];
441+
snprintf(get_msg, 128, "GET /ota/%s HTTP/1.1", _ota_url.c_str());
442+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s \"%s\"", __FUNCTION__, get_msg);
443+
444+
ota_client.println(get_msg);
445+
ota_client.println("Host: www.107-systems.org");
446+
ota_client.println("Connection: close");
447+
ota_client.println();
448+
449+
/* Read and parse the received data. */
450+
bool is_header_complete = false;
451+
size_t bytes_recv = 0;
452+
String http_header;
453+
454+
while (ota_client.available())
455+
{
456+
char const c = ota_client.read();
457+
Serial.print(c);
458+
459+
/* Check if header is complete. */
460+
if(!is_header_complete)
461+
{
462+
http_header += c;
463+
is_header_complete = http_header.endsWith("\r\n\r\n");
464+
break;
465+
}
466+
467+
/* If we reach this point then the HTTP header has
468+
* been received and we can feed the incoming binary
469+
* data into the OTA state machine.
470+
*/
471+
_ota_logic.onOTADataReceived(reinterpret_cast<uint8_t const *>(&c), 1);
472+
_ota_error = static_cast<int>(_ota_logic.update());
473+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s %d bytes received", __FUNCTION__, ++bytes_recv);
474+
}
475+
}
476+
}
477+
#endif
478+
420479
/******************************************************************************
421480
* EXTERN DEFINITION
422481
******************************************************************************/

src/ArduinoIoTCloudTCP.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
129129
int _ota_error;
130130
String _ota_img_sha256;
131131
String _ota_url;
132-
bool _otq_request;
132+
bool _ota_req;
133133
#endif /* OTA_ENABLED */
134134

135135
inline String getTopic_stdin () { return String("/a/d/" + getDeviceId() + "/s/i"); }
@@ -147,6 +147,10 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
147147
ArduinoIoTConnectionStatus checkCloudConnection();
148148
int write(String const topic, byte const data[], int const length);
149149

150+
#if OTA_ENABLED
151+
static void on_OTA_REQ_Update();
152+
void onOTARequest();
153+
#endif
150154
};
151155

152156
/******************************************************************************

0 commit comments

Comments
 (0)