Skip to content

Commit 5789902

Browse files
committed
Provide additional callbacks to register functions for various 'on-connection' failure types:
- Can not connect to the network (WiFi, GSM, ...) - Can not connect to MQTT broker - Can not subscribe to MQTT topics.
1 parent 1919728 commit 5789902

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/ArduinoIoTCloud.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ enum class ArduinoIoTConnectionStatus
6666

6767
enum class ArduinoIoTCloudEvent : size_t
6868
{
69-
SYNC = 0, CONNECT = 1, DISCONNECT = 2
69+
SYNC = 0, CONNECT = 1, DISCONNECT = 2, MOTT_CONNECT_FAILURE = 3, MOTT_SUBSCRIBE_FAILURE = 4
7070
};
7171

7272
typedef void (*OnCloudEventCallback)(void);
@@ -100,6 +100,11 @@ class ArduinoIoTCloudClass
100100

101101
void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
102102

103+
inline void onPhyConnectionFailure (OnCloudEventCallback func) { if (_connection) _connection->addCallback(NetworkConnectionEvent::CONNECTION_FAILED, func); }
104+
inline void onMQTTConnectionFailure (OnCloudEventCallback func) { addCallback(ArduinoIoTCloudEvent::MOTT_CONNECT_FAILURE, func); }
105+
inline void onMQTTSubscriptionFailure(OnCloudEventCallback func) { addCallback(ArduinoIoTCloudEvent::MOTT_SUBSCRIBE_FAILURE, func); }
106+
107+
103108
#define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
104109

105110
/* The following methods are used for non-LoRa boards which can use the
@@ -148,7 +153,7 @@ class ArduinoIoTCloudClass
148153

149154
String _thing_id = "";
150155
String _device_id = "";
151-
OnCloudEventCallback _cloud_event_callback[3] = {nullptr};
156+
OnCloudEventCallback _cloud_event_callback[5] = {nullptr};
152157
};
153158

154159
#ifdef HAS_TCP

src/ArduinoIoTCloudTCP.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
354354
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))
355355
return State::SubscribeMqttTopics;
356356

357+
execCloudEventCallback(ArduinoIoTCloudEvent::MOTT_CONNECT_FAILURE);
358+
357359
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
358360
return State::ConnectPhy;
359361
}
@@ -374,6 +376,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
374376
#if !defined(__AVR__)
375377
DEBUG_ERROR("Check your thing configuration, and press the reset button on your board.");
376378
#endif
379+
execCloudEventCallback(ArduinoIoTCloudEvent::MOTT_SUBSCRIBE_FAILURE);
377380
return State::SubscribeMqttTopics;
378381
}
379382

@@ -385,6 +388,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
385388
#if !defined(__AVR__)
386389
DEBUG_ERROR("Check your thing configuration, and press the reset button on your board.");
387390
#endif
391+
execCloudEventCallback(ArduinoIoTCloudEvent::MOTT_SUBSCRIBE_FAILURE);
388392
return State::SubscribeMqttTopics;
389393
}
390394
}

0 commit comments

Comments
 (0)