Skip to content

Commit 0b7a91d

Browse files
facchinmpennam
authored andcommitted
Add hook for OTA confirmation
1 parent 5e19f33 commit 0b7a91d

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/ArduinoIoTCloudTCP.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,16 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
511511

512512
if (_ota_req)
513513
{
514-
/* Clear the error flag. */
515-
_ota_error = static_cast<int>(OTAError::None);
516-
/* Transmit the cleared error flag to the cloud. */
517-
sendPropertiesToCloud();
518-
/* Clear the request flag. */
519-
_ota_req = false;
520-
/* Call member function to handle OTA request. */
521-
onOTARequest();
514+
if (_automatic_ota || (_get_ota_confirmation != nullptr && _get_ota_confirmation())) {
515+
/* Clear the error flag. */
516+
_ota_error = static_cast<int>(OTAError::None);
517+
/* Transmit the cleared error flag to the cloud. */
518+
sendPropertiesToCloud();
519+
/* Clear the request flag. */
520+
_ota_req = false;
521+
/* Call member function to handle OTA request. */
522+
onOTARequest();
523+
}
522524
}
523525
#endif /* OTA_ENABLED */
524526

src/ArduinoIoTCloudTCP.h

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static uint16_t const DEFAULT_BROKER_PORT_SECURE_AUTH = 8883;
4949
static char const DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH[] = "mqtts-up.iot.arduino.cc";
5050
static uint16_t const DEFAULT_BROKER_PORT_USER_PASS_AUTH = 8884;
5151

52+
typedef bool (*otaConfirmationStatus)(void);
53+
5254
/******************************************************************************
5355
* CLASS DECLARATION
5456
******************************************************************************/
@@ -80,6 +82,15 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
8082
inline String getBrokerAddress() const { return _brokerAddress; }
8183
inline uint16_t getBrokerPort () const { return _brokerPort; }
8284

85+
#if OTA_ENABLED
86+
/* The callback is triggered when the OTA is initiated and it gets executed until _ota_req flag is cleared.
87+
* It should return true when the OTA can be applied or false otherwise.
88+
*/
89+
void onOTARequestCb(otaConfirmationStatus cb) {
90+
_get_ota_confirmation = cb;
91+
_automatic_ota = false;
92+
}
93+
#endif
8394

8495
private:
8596
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
@@ -130,6 +141,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
130141
String _ota_img_sha256;
131142
String _ota_url;
132143
bool _ota_req;
144+
bool _automatic_ota = true;
133145
#endif /* OTA_ENABLED */
134146

135147
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
@@ -153,6 +165,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
153165
#if OTA_ENABLED
154166
void onOTARequest();
155167
#endif
168+
169+
otaConfirmationStatus _get_ota_confirmation = {nullptr};
156170
};
157171

158172
/******************************************************************************

0 commit comments

Comments
 (0)