Skip to content

Commit 4974635

Browse files
committed
Add hook for OTA confirmation
1 parent c8eac59 commit 4974635

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-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

+26
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,27 @@ 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+
87+
// The callback is triggered when the OTA is initiated
88+
// Should return true when the OTA can be applied, false otherwise
89+
90+
/*
91+
static first_run = true;
92+
bool sample_ota_confirmation() {
93+
if (first_run) {
94+
HMI.show(confirmationModal)
95+
first_run = false;
96+
}
97+
return HMI.getConfirmation();
98+
}
99+
*/
100+
101+
void onOTARequestCb(otaConfirmationStatus cb) {
102+
_get_ota_confirmation = cb;
103+
_automatic_ota = false;
104+
}
105+
#endif
83106

84107
private:
85108
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
@@ -130,6 +153,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
130153
String _ota_img_sha256;
131154
String _ota_url;
132155
bool _ota_req;
156+
bool _automatic_ota = true;
133157
#endif /* OTA_ENABLED */
134158

135159
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
@@ -153,6 +177,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
153177
#if OTA_ENABLED
154178
void onOTARequest();
155179
#endif
180+
181+
otaConfirmationStatus _get_ota_confirmation = {nullptr};
156182
};
157183

158184
/******************************************************************************

0 commit comments

Comments
 (0)