@@ -97,7 +97,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
97
97
, _ota_error{static_cast <int >(OTAError::None)}
98
98
, _ota_img_sha256{" Inv." }
99
99
, _ota_url{" " }
100
- , _otq_request {false }
100
+ , _ota_req {false }
101
101
#endif /* OTA_ENABLED */
102
102
{
103
103
@@ -249,7 +249,7 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
249
249
addPropertyReal (_ota_error, " OTA_ERROR" , Permission::Read);
250
250
addPropertyReal (_ota_img_sha256, " OTA_SHA256" , Permission::Read);
251
251
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 );
253
253
_ota_logic.setOTAStorage (ota_storage);
254
254
}
255
255
#endif /* OTA_ENABLED */
@@ -417,6 +417,65 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
417
417
return 0 ;
418
418
}
419
419
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
+
420
479
/* *****************************************************************************
421
480
* EXTERN DEFINITION
422
481
******************************************************************************/
0 commit comments