20
20
21
21
#include < ArduinoIoTCloud_Defines.h>
22
22
23
- #ifdef BOARD_HAS_ECCX08
24
- #include < ArduinoBearSSL.h>
25
- #elif defined(BOARD_ESP)
26
- #include < WiFiClientSecure.h>
27
- #endif
23
+
28
24
29
25
#include < ArduinoCloudThing.h>
30
- # include < ArduinoMqttClient.h >
26
+
31
27
#include < Arduino_DebugUtils.h>
32
- #include < Arduino_ConnectionHandler.h>
33
- #include " types/CloudWrapperBool.h"
28
+ #include " types/CloudWrapperBool.h"
34
29
#include " types/CloudWrapperFloat.h"
35
30
#include " types/CloudWrapperInt.h"
36
31
#include " types/CloudWrapperString.h"
37
- # include " utility/NTPUtils.h "
32
+
38
33
39
34
#include " CloudSerial.h"
40
35
41
- static char const DEFAULT_BROKER_ADDRESS_SECURE_AUTH[] = " mqtts-sa.iot.arduino.cc" ;
42
- static uint16_t const DEFAULT_BROKER_PORT_SECURE_AUTH = 8883 ;
43
- static char const DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH[] = " mqtts-up.iot.arduino.cc" ;
44
- static uint16_t const DEFAULT_BROKER_PORT_USER_PASS_AUTH = 8884 ;
45
36
46
37
typedef enum {
47
38
READ = 0x01 ,
48
39
WRITE = 0x02 ,
49
40
READWRITE = READ | WRITE
50
41
} permissionType;
51
42
52
- // Declaration of the struct for the mqtt connection options
53
- typedef struct {
54
- int keepAlive;
55
- bool cleanSession;
56
- int timeout;
57
- } mqttConnectionOptions;
58
43
59
44
enum class ArduinoIoTConnectionStatus {
60
45
IDLE,
@@ -78,59 +63,40 @@ enum class ArduinoIoTCloudEvent {
78
63
typedef void (*CallbackFunc)(void );
79
64
typedef void (*OnCloudEventCallback)(void * /* arg */ );
80
65
66
+ /* ************************************************
67
+ Pure Virtual Class Definition
68
+ **************************************************/
81
69
class ArduinoIoTCloudClass {
82
70
83
71
public:
84
- ArduinoIoTCloudClass ();
85
- ~ArduinoIoTCloudClass ();
86
-
87
- #ifdef BOARD_HAS_ECCX08
88
- int begin (ConnectionHandler &connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
89
- #else
90
- int begin (ConnectionHandler &connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
91
- #endif
92
- int begin (Client &net, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
93
- // Class constant declaration
94
- static const int MQTT_TRANSMIT_BUFFER_SIZE = 256 ;
95
- static const int TIMEOUT_FOR_LASTVALUES_SYNC = 10000 ;
96
-
97
- int connect ();
98
- bool disconnect ();
99
-
100
- inline void update () {
101
- update (NULL );
102
- }
103
- inline void update (int const reconnectionMaxRetries, int const reconnectionTimeoutMs) __attribute__((deprecated)) {
104
- update (NULL );
105
- }
106
- void update (CallbackFunc onSyncCompleteCallback) __attribute__((deprecated)); /* Attention: Function is deprecated - use 'addCallback(ArduinoIoTCloudConnectionEvent::SYNC, &onSync)' for adding a onSyncCallback instead */
72
+ static const int TIMEOUT_FOR_LASTVALUES_SYNC = 10000 ;
73
+ /* Public Virtual Functions*/
74
+ virtual int connect () = 0;
75
+ virtual bool disconnect () = 0;
107
76
108
- int connected ();
109
- // Clean up existing Mqtt connection, create a new one and initialize it
110
- int reconnect (Client& /* net */ );
77
+ virtual void update () = 0;
78
+ virtual void update (int const reconnectionMaxRetries, int const reconnectionTimeoutMs) __attribute__((deprecated)) = 0;
79
+ virtual void update (CallbackFunc onSyncCompleteCallback) __attribute__((deprecated)) = 0; /* Attention: Function is deprecated - use 'addCallback(ArduinoIoTCloudConnectionEvent::SYNC, &onSync)' for adding a onSyncCallback instead */
80
+
81
+ virtual int connected () = 0;
82
+
83
+ virtual void connectionCheck () = 0;
84
+
85
+ virtual void printDebugInfo () = 0;
111
86
112
87
inline void setThingId (String const thing_id) {
113
88
_thing_id = thing_id;
114
89
};
115
- #ifdef BOARD_ESP
116
- inline void setBoardId (String const device_id) {
117
- _device_id = device_id;
118
- }
119
- inline void setSecretDeviceKey (String const password) {
120
- _password = password;
121
- }
122
- #endif
90
+
123
91
inline String getThingId () const {
124
92
return _thing_id;
125
93
};
126
- inline String getDeviceId () const {
127
- return _device_id;
128
- };
129
- inline ConnectionHandler * getConnection () {
130
- return _connection;
131
- }
132
94
133
- #define addProperty ( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
95
+ inline String getDeviceId () const {
96
+ return _device_id;
97
+ };
98
+
99
+ #define addProperty ( v, ...) addPropertyReal(v, #v, __VA_ARGS__)
134
100
135
101
static unsigned long const DEFAULT_MIN_TIME_BETWEEN_UPDATES_MILLIS = 500 ; /* Data rate throttled to 2 Hz */
136
102
@@ -183,76 +149,68 @@ class ArduinoIoTCloudClass {
183
149
return Thing.addPropertyReal (*p, name, permission);
184
150
}
185
151
186
- void connectionCheck ();
187
- String getBrokerAddress () {
188
- return _brokerAddress;
189
- }
190
- uint16_t getBrokerPort () {
191
- return _brokerPort;
192
- }
193
- void printDebugInfo ();
194
- void addCallback (ArduinoIoTCloudEvent const event, OnCloudEventCallback callback);
152
+ void addCallback (ArduinoIoTCloudEvent const event, OnCloudEventCallback callback) {
153
+ switch (event) {
154
+ case ArduinoIoTCloudEvent::SYNC: _on_sync_event_callback = callback; break ;
155
+ case ArduinoIoTCloudEvent::CONNECT: _on_connect_event_callback = callback; break ;
156
+ case ArduinoIoTCloudEvent::DISCONNECT: _on_disconnect_event_callback = callback; break ;
157
+ }
158
+ };
195
159
196
160
protected:
197
- friend class CloudSerialClass ;
198
- int writeStdout (const byte data[], int length);
199
- int writeProperties (const byte data[], int length);
200
- int writeShadowOut (const byte data[], int length);
201
-
202
- // Used to initialize MQTTClient
203
- void mqttClientBegin ();
204
- // Function in charge of perform MQTT reconnection, basing on class parameters(retries,and timeout)
205
- bool mqttReconnect (int const maxRetries, int const timeout);
206
- // Used to retrieve last values from _shadowTopicIn
207
- void requestLastValue ();
161
+
162
+ virtual int writeStdout (const byte data[], int length) = 0;
163
+ virtual int writeProperties (const byte data[], int length) = 0;
164
+ virtual int writeShadowOut (const byte data[], int length) = 0;
165
+
166
+
167
+
208
168
209
169
ArduinoIoTConnectionStatus getIoTStatus () {
210
170
return _iotStatus;
211
171
}
212
172
213
- private:
214
173
ArduinoIoTConnectionStatus _iotStatus = ArduinoIoTConnectionStatus::IDLE;
215
- ConnectionHandler * _connection;
216
- static void onMessage (int length);
217
- void handleMessage (int length);
218
- ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED;
174
+
219
175
220
- void sendPropertiesToCloud ();
176
+
177
+ ArduinoIoTSynchronizationStatus _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED;
221
178
222
- String _device_id, _thing_id, _brokerAddress;
223
- uint16_t _brokerPort;
179
+
180
+ String _device_id, _thing_id;
181
+
224
182
225
183
ArduinoCloudThing Thing;
226
-
227
- #ifdef BOARD_HAS_ECCX08
228
- BearSSLClient *_sslClient;
229
- #elif defined(BOARD_ESP)
230
- WiFiClientSecure *_sslClient;
231
- String _password;
232
- #endif
233
-
234
- MqttClient *_mqttClient;
184
+
235
185
int _lastSyncRequestTickTime;
236
186
237
-
238
- // Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload
239
- String _stdinTopic;
240
- String _stdoutTopic;
241
- String _shadowTopicOut;
242
- String _shadowTopicIn;
243
- String _dataTopicOut;
244
- String _dataTopicIn;
245
- String _otaTopic;
246
- Client *_net;
247
-
248
187
OnCloudEventCallback _on_sync_event_callback,
249
188
_on_connect_event_callback,
250
189
_on_disconnect_event_callback;
251
190
252
- static void execCloudEventCallback (OnCloudEventCallback & callback, void * callback_arg);
253
- static void printConnectionStatus (ArduinoIoTConnectionStatus status);
191
+ static void execCloudEventCallback (OnCloudEventCallback & callback, void * callback_arg) {
192
+ if (callback) {
193
+ (*callback)(callback_arg);
194
+ }
195
+ }
196
+ static void printConnectionStatus (ArduinoIoTConnectionStatus status) {
197
+ switch (status) {
198
+ case ArduinoIoTConnectionStatus::IDLE: Debug.print (DBG_INFO, " Arduino IoT Cloud Connection status: IDLE" ); break ;
199
+ case ArduinoIoTConnectionStatus::ERROR: Debug.print (DBG_ERROR, " Arduino IoT Cloud Connection status: ERROR" ); break ;
200
+ case ArduinoIoTConnectionStatus::CONNECTING: Debug.print (DBG_INFO, " Arduino IoT Cloud Connection status: CONNECTING" ); break ;
201
+ case ArduinoIoTConnectionStatus::RECONNECTING: Debug.print (DBG_INFO, " Arduino IoT Cloud Connection status: RECONNECTING" ); break ;
202
+ case ArduinoIoTConnectionStatus::CONNECTED: Debug.print (DBG_INFO, " Arduino IoT Cloud Connection status: CONNECTED" ); break ;
203
+ case ArduinoIoTConnectionStatus::DISCONNECTED: Debug.print (DBG_ERROR, " Arduino IoT Cloud Connection status: DISCONNECTED" ); break ;
204
+ }
205
+ }
254
206
};
255
-
256
- extern ArduinoIoTCloudClass ArduinoCloud;
207
+ #if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWIFI1010) || \
208
+ defined (ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT)
209
+ #include " ArduinoIoTCloudTCP.h"
210
+ extern ArduinoIoTCloudTCP ArduinoCloud;
211
+ #elif defined(ARDUINO_SAMD_MKR1300) || defined(ARDUINO_SAMD_MKR1310)
212
+ // #include "ArduinoIoTCloudLPWAN.h"
213
+ // extern ArduinoIoTCloudLPWAN ArduinoCloud;
214
+ #endif
257
215
258
216
#endif
0 commit comments