Skip to content

Commit 9bc09d8

Browse files
committed
Establish connection to OTA images server when _ota_req is set to 'true' and download OTA image from there.
1 parent 0d8c582 commit 9bc09d8

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
@@ -90,7 +90,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
9090
, _ota_error{static_cast<int>(OTAError::None)}
9191
, _ota_img_sha256{"Inv."}
9292
, _ota_url{""}
93-
, _otq_request{false}
93+
, _ota_req{false}
9494
#endif /* OTA_ENABLED */
9595
{
9696

@@ -214,7 +214,7 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
214214
addPropertyReal(_ota_error, "OTA_ERROR", Permission::Read);
215215
addPropertyReal(_ota_img_sha256, "OTA_SHA256", Permission::Read);
216216
addPropertyReal(_ota_url, "OTA_URL", Permission::ReadWrite).onSync(DEVICE_WINS);
217-
addPropertyReal(_otq_request, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS);
217+
addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS).onUpdate(ArduinoIoTCloudTCP::on_OTA_REQ_Update);
218218
_ota_logic.setOTAStorage(ota_storage);
219219
}
220220
#endif /* OTA_ENABLED */
@@ -419,6 +419,65 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
419419
return 0;
420420
}
421421

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

src/ArduinoIoTCloudTCP.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
131131
int _ota_error;
132132
String _ota_img_sha256;
133133
String _ota_url;
134-
bool _otq_request;
134+
bool _ota_req;
135135
#endif /* OTA_ENABLED */
136136

137137
inline String getTopic_stdin () { return String("/a/d/" + getDeviceId() + "/s/i"); }
@@ -155,6 +155,10 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
155155
void requestLastValue();
156156
int write(String const topic, byte const data[], int const length);
157157

158+
#if OTA_ENABLED
159+
static void on_OTA_REQ_Update();
160+
void onOTARequest();
161+
#endif
158162
};
159163

160164
/******************************************************************************

0 commit comments

Comments
 (0)