Skip to content

Commit c5ec9a3

Browse files
committed
cleanup and retro-compatibility
1 parent 2ed9788 commit c5ec9a3

File tree

4 files changed

+38
-49
lines changed

4 files changed

+38
-49
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int ArduinoIoTCloudClass::begin(ConnectionManager *c, String brokerAddress)
4646
int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
4747
{
4848

49+
_net = &net;
4950
// store the broker address as class member
5051
_brokerAddress = brokerAddress;
5152

@@ -80,15 +81,19 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
8081
if(connection != NULL){
8182
_bearSslClient = new BearSSLClient(connection->getClient());
8283
}else{
83-
_bearSslClient = new BearSSLClient(net);
84+
_bearSslClient = new BearSSLClient(*_net);
8485
}
8586

8687
_bearSslClient->setEccSlot(keySlot, ECCX08Cert.bytes(), ECCX08Cert.length());
8788
_mqttClient = new MqttClient(*_bearSslClient);
8889

8990
// Bind ArduinoBearSSL callback using static "non-method" function
90-
getTimeConnection = connection;
91-
ArduinoBearSSL.onGetTime(getTime);
91+
if(connection != NULL){
92+
getTimeConnection = connection;
93+
ArduinoBearSSL.onGetTime(getTime);
94+
}
95+
96+
9297
// TODO: Find a better way to allow callback into object method
9398

9499
// Begin function for the MQTTClient
@@ -99,6 +104,11 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
99104
return 1;
100105
}
101106

107+
void ArduinoIoTCloudClass::onGetTime(unsigned long(*callback)(void))
108+
{
109+
ArduinoBearSSL.onGetTime(callback);
110+
}
111+
102112
// private class method used to initialize mqttClient class member. (called in the begin class method)
103113
void ArduinoIoTCloudClass::mqttClientBegin()
104114
{
@@ -197,7 +207,7 @@ void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const re
197207
}
198208
}
199209

200-
int ArduinoIoTCloudClass::reconnect()
210+
int ArduinoIoTCloudClass::reconnect(Client& /* net */)
201211
{
202212
if (_mqttClient->connected()) {
203213
_mqttClient->stop();
@@ -285,13 +295,20 @@ void ArduinoIoTCloudClass::connectionCheck() {
285295

286296
switch (iotStatus) {
287297
case IOT_STATUS_IDLE:
288-
if (!begin(connection)) {
289-
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
290-
iotStatus = IOT_STATUS_CLOUD_ERROR;
291-
return;
298+
if(connection == NULL){
299+
if(!begin(*_net)){
300+
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
301+
iotStatus = IOT_STATUS_CLOUD_ERROR;
302+
return;
303+
}
304+
}else{
305+
if (!begin(connection)) {
306+
debugMessage("Error Starting Arduino Cloud\nTrying again in a few seconds", 0);
307+
iotStatus = IOT_STATUS_CLOUD_ERROR;
308+
return;
309+
}
292310
}
293-
// TODO: Controlla bene che non serva
294-
//ArduinoCloud.onGetTime(getTime);
311+
295312
iotStatus = IOT_STATUS_CLOUD_CONNECTING;
296313
break;
297314
case IOT_STATUS_CLOUD_ERROR:
@@ -306,7 +323,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
306323
case IOT_STATUS_CLOUD_RECONNECTING:
307324
debugMessage("IoT Cloud reconnecting...", 1);
308325
//wifiClient.stop();
309-
arduinoIoTConnectionAttempt = reconnect();
326+
arduinoIoTConnectionAttempt = reconnect(*_net);
310327
*msgBuffer = 0;
311328
sprintf(msgBuffer, "ArduinoCloud.reconnect(): %d", arduinoIoTConnectionAttempt);
312329
debugMessage(msgBuffer, 1);

src/ArduinoIoTCloud.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ class ArduinoIoTCloudClass {
4141
~ArduinoIoTCloudClass();
4242

4343
int begin(ConnectionManager *connection = ArduinoIoTPreferredConnection, String brokerAddress = "mqtts-sa.iot.arduino.cc");
44-
int begin(Client& net, String brokerAddress);
44+
int begin(Client& net, String brokerAddress = "mqtts-sa.iot.arduino.cc");
4545
// Class constant declaration
4646
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
4747
static const int MAX_RETRIES = 5;
4848
static const int RECONNECTION_TIMEOUT = 2000;
4949

50+
51+
void onGetTime(unsigned long(*callback)(void));
52+
5053
int connect ();
5154
bool disconnect();
5255

@@ -58,7 +61,7 @@ class ArduinoIoTCloudClass {
5861

5962
int connected();
6063
// Clean up existing Mqtt connection, create a new one and initialize it
61-
int reconnect();
64+
int reconnect(Client& /* net */);
6265

6366
inline void setThingId(String const thing_id) { _thing_id = thing_id; };
6467

@@ -121,6 +124,7 @@ class ArduinoIoTCloudClass {
121124
String _dataTopicOut;
122125
String _dataTopicIn;
123126
String _otaTopic;
127+
Client *_net;
124128
};
125129

126130
extern ArduinoIoTCloudClass ArduinoCloud;

src/ConnectionManager.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,6 @@ class ConnectionManager {
3333
NetworkConnectionState netConnectionState = CONNECTION_STATE_IDLE;
3434
};
3535

36-
// Network Connection Status
37-
//int networkStatus = NETWORK_IDLE_STATUS;
38-
39-
// ********* NETWORK LAYER **********
40-
// max network layer connection retries
41-
#define NETWORK_LAYER_CONNECTION_RETRIES 6
42-
// ms to wait between each retry
43-
#define NETWORK_LAYER_CONNECTION_TIMEOUT 10000
44-
// ms to wait between each retry
45-
#define NETWORK_LAYER_RECONNECTION_TIMEOUT 2000
46-
47-
// TODO: remove references to ArduinoIoTCloud
48-
// ********** CLOUD LAYER ***********
49-
// max arduino cloud connection retries
50-
#define ARDUINO_IOT_CLOUD_CONNECTION_RETRIES 30
51-
// max wifi connection retries
52-
#define ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT 3000
53-
54-
// === NETWORK CONNECTION MANAGEMENT ===
55-
// last time when the Network Connection was checked
56-
//unsigned long lastNetworkCheck = 0;
57-
// time interval to check the Network Connection
58-
//static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
59-
// timeout between each network connection retry
60-
//static const unsigned long NETWORK_CONNECTION_TIMEOUT = 2000;
61-
6236
#ifdef ARDUINO_SAMD_MKR1000
6337
#include <WiFi101.h>
6438
#define BOARD_HAS_WIFI
@@ -83,9 +57,6 @@ class ConnectionManager {
8357
#define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY
8458
#endif
8559

86-
// TODO: remove references to ArduinoIoTCloud
87-
#include <ArduinoIoTCloud.h>
88-
8960
inline void debugMessage(char *_msg, uint8_t _debugLevel) {
9061
if (_debugLevel <= ARDUINO_CLOUD_DEBUG_LEVEL) {
9162
char prepend[20];

src/WiFiConnectionManager.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ class WiFiConnectionManager : public ConnectionManager {
99
virtual void init();
1010
virtual void check();
1111
virtual Client &getClient() { return wifiClient; };
12-
// TODO: remove references to ArduinoIoTCloud
13-
virtual ArduinoIoTConnectionStatus status() { return IOT_STATUS_IDLE; };
12+
1413

1514
private:
16-
// TODO: remove references to ArduinoIoTCloud
17-
void iotConnectionCheck();
15+
1816
void changeConnectionState(NetworkConnectionState _newState);
1917

2018
const int CHECK_INTERVAL_IDLE = 100;
@@ -65,8 +63,7 @@ void WiFiConnectionManager::changeConnectionState(NetworkConnectionState _newSta
6563
break;
6664
case CONNECTION_STATE_DISCONNECTED:
6765
newInterval = CHECK_INTERVAL_DISCONNECTED;
68-
// TODO: Riportare in ArduinoIoTCloud
69-
//iotStatus = IOT_STATUS_CLOUD_DISCONNECTED;
66+
7067
break;
7168
}
7269
connectionTickTimeInterval = newInterval;
@@ -96,7 +93,7 @@ void WiFiConnectionManager::check() {
9693
}
9794
*msgBuffer = 0;
9895
sprintf(msgBuffer, "WiFi Firmware v. %s", WiFi.firmwareVersion());
99-
debugMessage(msgBuffer, 3);
96+
debugMessage(msgBuffer, 0);
10097
changeConnectionState(CONNECTION_STATE_CONNECTING);
10198
break;
10299
case CONNECTION_STATE_CONNECTING:

0 commit comments

Comments
 (0)