8000 Improve reliability on connection loss by aentinger · Pull Request #233 · arduino-libraries/ArduinoIoTCloud · GitHub
[go: up one dir, main page]

Skip to content

Improve reliability on connection loss #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
{
if (!_mqttClient.connected())
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
_mqttClient.stop();
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
return State::ConnectPhy;
}

if (!_mqttClient.subscribe(_dataTopicIn))
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str());
Expand Down Expand Up @@ -360,6 +368,14 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
{
if (!_mqttClient.connected())
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__);
_mqttClient.stop();
execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT);
return State::ConnectPhy;
}

/* Check whether or not we need to send a new request. */
unsigned long const now = millis();
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
Expand Down
16 changes: 16 additions & 0 deletions src/tls/BearSSLClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

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);


bool BearSSLClient::_sslio_closing = false;


BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs, GetTimeCallbackFunc func) :
_client(client),
_TAs(myTAs),
Expand Down Expand Up @@ -156,6 +160,7 @@ void BearSSLClient::stop()
{
if (_client->connected()) {
if ((br_ssl_engine_current_state(&_sc.eng) & BR_SSL_CLOSED) == 0) {
BearSSLClient::_sslio_closing = true;
br_sslio_close(&_ioc);
}

Expand Down Expand Up @@ -258,6 +263,9 @@ int BearSSLClient::errorCode()

int BearSSLClient::connectSSL(const char* host)
{
/* Ensure this flag is cleared so we don't terminate a just starting connection. */
_sslio_closing = false;

// initialize client context with all necessary algorithms and hardcoded trust anchors.
aiotc_client_profile_init(&_sc, &_xc, _TAs, _numTAs);

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

int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
{
if (BearSSLClient::_sslio_closing) {
return -1;
}

Client* c = (Client*)ctx;

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

int BearSSLClient::clientWrite(void *ctx, const unsigned char *buf, size_t len)
{
if (BearSSLClient::_sslio_closing) {
return -1;
}

Client* c = (Client*)ctx;

#ifdef DEBUGSERIAL
Expand Down
1 change: 1 addition & 0 deletions src/tls/BearSSLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class BearSSLClient : public Client {
br_x509_certificate _ecCert;
bool _ecCertDynamic;

static bool _sslio_closing;
br_ssl_client_context _sc;
br_x509_minimal_context _xc;
unsigned char _ibuf[BEAR_SSL_CLIENT_IBUF_SIZE];
Expand Down
0