Skip to content

Commit 70efc00

Browse files
authored
Merge pull request #233 from arduino-libraries/fix-reliability-on-connection-loss
Improve reliability on connection loss
2 parents 68f4b51 + 9852cd6 commit 70efc00

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
328328

329329
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
330330
{
331+
if (!_mqttClient.connected())
332+
{
333+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
334+
_mqttClient.stop();
335+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
336+
return State::ConnectPhy;
337+
}
338+
331339
if (!_mqttClient.subscribe(_dataTopicIn))
332340
{
333341
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str());
@@ -360,6 +368,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
360368

361369
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
362370
{
371+
if (!_mqttClient.connected())
372+
{
373+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
374+
_mqttClient.stop();
375+
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
376+
return State::ConnectPhy;
377+
}
378+
363379
/* Check whether or not we need to send a new request. */
364380
unsigned long const now = millis();
365381
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)

src/tls/BearSSLClient.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
extern "C" void aiotc_client_profile_init(br_ssl_client_context *cc, br_x509_minimal_context *xc, const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
3838

39+
40+
bool BearSSLClient::_sslio_closing = false;
41+
42+
3943
BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs, GetTimeCallbackFunc func) :
4044
_client(client),
4145
_TAs(myTAs),
@@ -156,6 +160,7 @@ void BearSSLClient::stop()
156160
{
157161
if (_client->connected()) {
158162
if ((br_ssl_engine_current_state(&_sc.eng) & BR_SSL_CLOSED) == 0) {
163+
BearSSLClient::_sslio_closing = true;
159164
br_sslio_close(&_ioc);
160165
}
161166

@@ -258,6 +263,9 @@ int BearSSLClient::errorCode()
258263

259264
int BearSSLClient::connectSSL(const char* host)
260265
{
266+
/* Ensure this flag is cleared so we don't terminate a just starting connection. */
267+
_sslio_closing = false;
268+
261269
// initialize client context with all necessary algorithms and hardcoded trust anchors.
262270
aiotc_client_profile_init(&_sc, &_xc, _TAs, _numTAs);
263271

@@ -315,6 +323,10 @@ int BearSSLClient::connectSSL(const char* host)
315323

316324
int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
317325
{
326+
if (BearSSLClient::_sslio_closing) {
327+
return -1;
328+
}
329+
318330
Client* c = (Client*)ctx;
319331

320332
if (!c->connected()) {
@@ -346,6 +358,10 @@ int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
346358

347359
int BearSSLClient::clientWrite(void *ctx, const unsigned char *buf, size_t len)
348360
{
361+
if (BearSSLClient::_sslio_closing) {
362+
return -1;
363+
}
364+
349365
Client* c = (Client*)ctx;
350366

351367
#ifdef DEBUGSERIAL

src/tls/BearSSLClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class BearSSLClient : public Client {
9898
br_x509_certificate _ecCert;
9999
bool _ecCertDynamic;
100100

101+
static bool _sslio_closing;
101102
br_ssl_client_context _sc;
102103
br_x509_minimal_context _xc;
103104
unsigned char _ibuf[BEAR_SSL_CLIENT_IBUF_SIZE];

0 commit comments

Comments
 (0)