@@ -64,11 +64,13 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
64
64
, _mqtt_data_buf{0 }
65
65
, _mqtt_data_len{0 }
66
66
, _mqtt_data_request_retransmit{false }
67
- , _sslClient(NULL )
67
+ #ifdef BOARD_HAS_ECCX08
68
+ , _sslClient(nullptr , ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM)
69
+ #endif
68
70
#ifdef BOARD_ESP
69
71
, _password(" " )
70
72
#endif
71
- , _mqttClient( NULL )
73
+ , _mqttClient{ nullptr }
72
74
, _syncStatus{ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED}
73
75
, _stdinTopic(" " )
74
76
, _stdoutTopic(" " )
@@ -85,12 +87,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
85
87
86
88
}
87
89
88
- ArduinoIoTCloudTCP::~ArduinoIoTCloudTCP ()
89
- {
90
- delete _mqttClient; _mqttClient = NULL ;
91
- delete _sslClient; _sslClient = NULL ;
92
- }
93
-
94
90
/* *****************************************************************************
95
91
* PUBLIC MEMBER FUNCTIONS
96
92
******************************************************************************/
@@ -115,21 +111,20 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
115
111
if (!CryptoUtil::readDeviceId (ECCX08, getDeviceId (), ECCX08Slot::DeviceId)) { Debug.print (DBG_ERROR, " Cryptography processor read failure." ); return 0 ; }
116
112
if (!CryptoUtil::reconstructCertificate (_eccx08_cert, getDeviceId (), ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) { Debug.print (DBG_ERROR, " Cryptography certificate reconstruction failure." ); return 0 ; }
117
113
ArduinoBearSSL.onGetTime (getTime);
118
- _sslClient = new BearSSLClient (_connection->getClient (), ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM );
119
- _sslClient-> setEccSlot (static_cast <int >(ECCX08Slot::Key), _eccx08_cert.bytes (), _eccx08_cert.length ());
114
+ _sslClient. setClient (_connection->getClient ());
115
+ _sslClient. setEccSlot (static_cast <int >(ECCX08Slot::Key), _eccx08_cert.bytes (), _eccx08_cert.length ());
120
116
#elif defined(BOARD_ESP)
121
- _sslClient = new WiFiClientSecure ();
122
- _sslClient->setInsecure ();
117
+ _sslClient.setInsecure ();
123
118
#endif
124
119
125
- _mqttClient = new MqttClient (* _sslClient);
120
+ _mqttClient. setClient ( _sslClient);
126
121
#ifdef BOARD_ESP
127
- _mqttClient-> setUsernamePassword (getDeviceId (), _password);
122
+ _mqttClient. setUsernamePassword (getDeviceId (), _password);
128
123
#endif
129
- _mqttClient-> onMessage (ArduinoIoTCloudTCP::onMessage);
130
- _mqttClient-> setKeepAliveInterval (30 * 1000 );
131
- _mqttClient-> setConnectionTimeout (1500 );
132
- _mqttClient-> setId (getDeviceId ().c_str ());
124
+ _mqttClient. onMessage (ArduinoIoTCloudTCP::onMessage);
125
+ _mqttClient. setKeepAliveInterval (30 * 1000 );
126
+ _mqttClient. setConnectionTimeout (1500 );
127
+ _mqttClient. setId (getDeviceId ().c_str ());
133
128
134
129
_stdinTopic = getTopic_stdin ();
135
130
_stdoutTopic = getTopic_stdout ();
@@ -170,7 +165,7 @@ void ArduinoIoTCloudTCP::update()
170
165
}
171
166
172
167
// MTTQClient connected!, poll() used to retrieve data from MQTT broker
173
- _mqttClient-> poll ();
168
+ _mqttClient. poll ();
174
169
175
170
switch (_syncStatus)
176
171
{
@@ -197,7 +192,7 @@ void ArduinoIoTCloudTCP::update()
197
192
198
193
int ArduinoIoTCloudTCP::connected ()
199
194
{
200
- return _mqttClient-> connected ();
195
+ return _mqttClient. connected ();
201
196
}
202
197
203
198
void ArduinoIoTCloudTCP::printDebugInfo ()
@@ -220,8 +215,8 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
220
215
221
216
int ArduinoIoTCloudTCP::reconnect ()
222
217
{
223
- if (_mqttClient-> connected ()) {
224
- _mqttClient-> stop ();
218
+ if (_mqttClient. connected ()) {
219
+ _mqttClient. stop ();
225
220
}
226
221
return connect ();
227
222
}
@@ -232,14 +227,14 @@ int ArduinoIoTCloudTCP::reconnect()
232
227
233
228
int ArduinoIoTCloudTCP::connect ()
234
229
{
235
- if (!_mqttClient-> connect (_brokerAddress.c_str (), _brokerPort)) return CONNECT_FAILURE;
236
- if (_mqttClient-> subscribe (_stdinTopic) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
237
- if (_mqttClient-> subscribe (_dataTopicIn) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
238
- if (_mqttClient-> subscribe (_ota_topic_in) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
230
+ if (!_mqttClient. connect (_brokerAddress.c_str (), _brokerPort)) return CONNECT_FAILURE;
231
+ if (_mqttClient. subscribe (_stdinTopic) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
232
+ if (_mqttClient. subscribe (_dataTopicIn) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
233
+ if (_mqttClient. subscribe (_ota_topic_in) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
239
234
240
235
if (_shadowTopicIn != " " )
241
236
{
242
- if (_mqttClient-> subscribe (_shadowTopicIn) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
237
+ if (_mqttClient. subscribe (_shadowTopicIn) == 0 ) return CONNECT_FAILURE_SUBSCRIBE;
243
238
_syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES;
244
239
_lastSyncRequestTickTime = 0 ;
245
240
}
@@ -249,7 +244,7 @@ int ArduinoIoTCloudTCP::connect()
249
244
250
245
void ArduinoIoTCloudTCP::disconnect ()
251
246
{
252
- _mqttClient-> stop ();
247
+ _mqttClient. stop ();
253
248
}
254
249
255
250
/* *****************************************************************************
@@ -263,12 +258,12 @@ void ArduinoIoTCloudTCP::onMessage(int length)
263
258
264
259
void ArduinoIoTCloudTCP::handleMessage (int length)
265
260
{
266
- String topic = _mqttClient-> messageTopic ();
261
+ String topic = _mqttClient. messageTopic ();
267
262
268
263
byte bytes[length];
269
264
270
265
for (int i = 0 ; i < length; i++) {
271
- bytes[i] = _mqttClient-> read ();
266
+ bytes[i] = _mqttClient. read ();
272
267
}
273
268
274
269
if (_stdinTopic == topic) {
@@ -354,7 +349,7 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection()
354
349
355
350
case ArduinoIoTConnectionStatus::CONNECTED:
356
351
{
357
- if (!_mqttClient-> connected ())
352
+ if (!_mqttClient. connected ())
358
353
{
359
354
next_iot_status = ArduinoIoTConnectionStatus::DISCONNECTED;
360
355
_mqtt_data_request_retransmit = true ;
@@ -376,9 +371,9 @@ ArduinoIoTConnectionStatus ArduinoIoTCloudTCP::checkCloudConnection()
376
371
377
372
int ArduinoIoTCloudTCP::write (String const topic, byte const data[], int const length)
378
373
{
379
- if (_mqttClient-> beginMessage (topic, length, false , 0 )) {
380
- if (_mqttClient-> write (data, length)) {
381
- if (_mqttClient-> endMessage ()) {
374
+ if (_mqttClient. beginMessage (topic, length, false , 0 )) {
375
+ if (_mqttClient. write (data, length)) {
376
+ if (_mqttClient. endMessage ()) {
382
377
return 1 ;
383
378
}
384
379
}
0 commit comments