Skip to content

Commit 69132fb

Browse files
committed
Added ConnectionManager (experimental WIP)
1 parent 2764c88 commit 69132fb

File tree

5 files changed

+167
-12
lines changed

5 files changed

+167
-12
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ const static int compressedCertSlot = 10;
88
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
99
const static int thingIdSlot = 12;
1010

11+
static ConnectionManager *getTimeConnection = NULL;
12+
13+
static unsigned long getTime() {
14+
if (!getTimeConnection) return 0;
15+
return getTimeConnection->getTime();
16+
}
17+
1118
ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
1219
_thing_id (""),
1320
_bearSslClient(NULL),
@@ -28,8 +35,10 @@ ArduinoIoTCloudClass::~ArduinoIoTCloudClass()
2835
}
2936
}
3037

31-
int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
38+
int ArduinoIoTCloudClass::begin(ConnectionManager *c, String brokerAddress)
3239
{
40+
connection = c;
41+
3342
// store the broker address as class member
3443
_brokerAddress = brokerAddress;
3544

@@ -61,10 +70,15 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
6170
if (_bearSslClient) {
6271
delete _bearSslClient;
6372
}
64-
_bearSslClient = new BearSSLClient(net);
73+
_bearSslClient = new BearSSLClient(connection->getClient());
6574
_bearSslClient->setEccSlot(keySlot, ECCX08Cert.bytes(), ECCX08Cert.length());
6675
_mqttClient = new MqttClient(*_bearSslClient);
6776

77+
// Bind ArduinoBearSSL callback using static "non-method" function
78+
getTimeConnection = connection;
79+
ArduinoBearSSL.onGetTime(getTime);
80+
// TODO: Find a better way to allow callback into object method
81+
6882
// Begin function for the MQTTClient
6983
mqttClientBegin();
7084

@@ -134,7 +148,7 @@ bool ArduinoIoTCloudClass::mqttReconnect(int const maxRetries, int const timeout
134148

135149
// Check for MQTT broker connection, of if maxReties limit is reached
136150
// if MQTTClient is connected , simply do nothing and retun true
137-
while(!_mqttClient->connected() && (retries++ < maxRetries) && (millis() - start < timeout)) {
151+
while (!_mqttClient->connected() && (retries++ < maxRetries) && (millis() - start < timeout)) {
138152
// int connectError = _mqttClient->connectError();
139153

140154
// try establish the MQTT broker connection
@@ -179,11 +193,6 @@ int ArduinoIoTCloudClass::reconnect(Client& /*net*/)
179193
return connect();
180194
}
181195

182-
void ArduinoIoTCloudClass::onGetTime(unsigned long(*callback)(void))
183-
{
184-
ArduinoBearSSL.onGetTime(callback);
185-
}
186-
187196
int ArduinoIoTCloudClass::connected()
188197
{
189198
return _mqttClient->connected();

src/ArduinoIoTCloud.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <ArduinoMqttClient.h>
55
#include <ArduinoIoTCloudBearSSL.h>
66
#include <ArduinoCloudThing.h>
7+
#include "ConnectionManager.h"
78

89
#include "CloudSerial.h"
910

@@ -20,13 +21,15 @@ typedef struct {
2021
int timeout;
2122
} mqttConnectionOptions;
2223

24+
extern ConnectionManager *ArduinoIoTPreferredConnection;
25+
2326
class ArduinoIoTCloudClass {
2427

2528
public:
2629
ArduinoIoTCloudClass();
2730
~ArduinoIoTCloudClass();
2831

29-
int begin(Client& net, String brokerAddress = "mqtts-sa.iot.arduino.cc");
32+
int begin(ConnectionManager *connection = ArduinoIoTPreferredConnection, String brokerAddress = "mqtts-sa.iot.arduino.cc");
3033

3134
// Class constant declaration
3235
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
@@ -41,9 +44,6 @@ class ArduinoIoTCloudClass {
4144

4245
// defined for users who want to specify max reconnections reties and timeout between them
4346
void update(int const reconnectionMaxRetries, int const reconnectionTimeoutMs);
44-
// It must be a user defined function, in order to avoid ArduinoCloud include specific WiFi file
45-
// in this case this library is independent from the WiFi one
46-
void onGetTime(unsigned long(*)(void));
4747

4848
int connected();
4949
// Clean up existing Mqtt connection, create a new one and initialize it
@@ -88,6 +88,7 @@ class ArduinoIoTCloudClass {
8888
bool mqttReconnect(int const maxRetries, int const timeout);
8989

9090
private:
91+
ConnectionManager *connection;
9192
static void onMessage(int length);
9293
void handleMessage(int length);
9394

src/ConnectionManager.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#ifndef CONNECTION_MANAGER_H_INCLUDED
2+
#define CONNECTION_MANAGER_H_INCLUDED
3+
4+
#include <Client.h>
5+
6+
/* NOT USED FOR NOW */
7+
enum ArduinoIoTConnectionStatus {
8+
IOT_STATUS_IDLE,/* only at start */
9+
IOT_STATUS_CLOUD_IDLE,
10+
IOT_STATUS_CLOUD_CONNECTING,
11+
IOT_STATUS_CLOUD_CONNECTED,
12+
IOT_STATUS_CLOUD_DISCONNECTED,
13+
IOT_STATUS_CLOUD_ERROR,
14+
IOT_STATUS_NETWORK_IDLE,
15+
IOT_STATUS_NETWORK_CONNECTED,
16+
IOT_STATUS_NETWORK_CONNECTING,
17+
IOT_STATUS_NETWORK_DISCONNECTED,
18+
IOT_STATUS_NETWORK_ERROR,
19+
IOT_STATUS_ERROR_GENERIC
20+
};
21+
22+
class ConnectionManager {
23+
public:
24+
virtual void init() = 0;
25+
virtual void check() = 0;
26+
virtual ArduinoIoTConnectionStatus status() = 0;
27+
virtual unsigned long getTime() = 0;
28+
virtual Client &getClient();
29+
};
30+
31+
// ********* NETWORK LAYER **********
32+
// max network layer connection retries
33+
#define NETWORK_LAYER_CONNECTION_RETRIES 6
34+
// ms to wait between each retry
35+
#define NETWORK_LAYER_CONNECTION_TIMEOUT 10000
36+
// ms to wait between each retry
37+
#define NETWORK_LAYER_RECONNECTION_TIMEOUT 2000
38+
39+
// ********** CLOUD LAYER ***********
40+
// max arduino cloud connection retries
41+
#define ARDUINO_IOT_CLOUD_CONNECTION_RETRIES 30
42+
// max wifi connection retries
43+
#define ARDUINO_IOT_CLOUD_CONNECTION_TIMEOUT 3000
44+
45+
// Network Connection Status
46+
//int iotStatus = IOT_STATUS_IDLE;
47+
//int networkStatus = NETWORK_IDLE_STATUS;
48+
49+
// === NETWORK CONNECTION MANAGEMENT ===
50+
// last time when the Network Connection was checked
51+
//unsigned long lastNetworkCheck = 0;
52+
// time interval to check the Network Connection
53+
//static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
54+
// timeout between each network connection retry
55+
//static const unsigned long NETWORK_CONNECTION_TIMEOUT = 2000;
56+
57+
#ifdef ARDUINO_SAMD_MKR1000
58+
#include <WiFi101.h>
59+
#include "WiFiConnectionManager.h"
60+
#define BOARD_HAS_WIFI
61+
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
62+
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
63+
#define NETWORK_CONNECTED WL_CONNECTED
64+
#endif
65+
66+
#ifdef ARDUINO_SAMD_MKRWIFI1010
67+
#include <WiFiNINA.h>
68+
#include "WiFiConnectionManager.h"
69+
#define BOARD_HAS_WIFI
70+
#define NETWORK_HARDWARE_ERROR WL_NO_MODULE
71+
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
72+
#define NETWORK_CONNECTED WL_CONNECTED
73+
#endif
74+
75+
#ifdef ARDUINO_SAMD_MKRGSM1400
76+
#include <MKRGSM.h>
77+
#define BOARD_HAS_GSM
78+
#define NETWORK_HARDWARE_ERROR GPRS_PING_ERROR
79+
#define NETWORK_IDLE_STATUS GSM3_NetworkStatus_t::IDLE
80+
#define NETWORK_CONNECTED GSM3_NetworkStatus_t::GPRS_READY
81+
#endif
82+
83+
#include <ArduinoIoTCloud.h>
84+
85+
#endif

src/GSMConnectionManager.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <MKRGSM.h>
2+
#include "ConnectionManager.h"
3+
4+
class GSMConnectionManager : public ConnectionManager {
5+
public:
6+
GSMConnectionManager(const char *pin, const char *apn, const char *login, const char *pass);
7+
8+
virtual unsigned long getTime();
9+
virtual void init();
10+
virtual void check();
11+
virtual Client &getClient() { return networkClient; };
12+
virtual ArduinoIoTConnectionStatus status() { return IOT_STATUS_IDLE; };
13+
14+
GSMClient networkClient;
15+
GSM gsmAccess;
16+
GPRS gprs;
17+
private:
18+
const char *pin, *apn, *login, *pass;
19+
unsigned long lastNetworkCheck, lastNetworkStep;
20+
};
21+
22+
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
23+
24+
GSMConnectionManager::GSMConnectionManager(const char *pin, const char *apn, const char *login, const char *pass) :
25+
pin(pin), apn(apn), login(login), pass(pass), lastNetworkCheck(millis()) {
26+
}
27+
28+
unsigned long GSMConnectionManager::getTime() {
29+
return gsmAccess.getTime();
30+
}
31+
32+
void GSMConnectionManager::init() {
33+
if (gsmAccess.begin(pin) == GSM_READY && gprs.attachGPRS(apn, login, pass) == GPRS_READY) {
34+
Serial.println("GPRS connected");
35+
int pingResult = gprs.ping("google.com");
36+
if (pingResult >= 0) {
37+
Serial.print("SUCCESS! RTT = ");
38+
Serial.print(pingResult);
39+
Serial.println(" ms");
40+
}
41+
} else {
42+
Serial.println("GPRS not connected");
43+
while(1);
44+
}
45+
}
46+
47+
void GSMConnectionManager::check() {
48+
if (millis() - lastNetworkCheck < NETWORK_CONNECTION_INTERVAL) {
49+
return;
50+
}
51+
52+
Serial.print("<<Network Status: ");
53+
if (gprs.attachGPRS(apn, login, pass) == GPRS_READY) {
54+
Serial.println("CONNECTED");
55+
lastNetworkCheck = millis();
56+
} else {
57+
Serial.println("NOT CONNECTED");
58+
}
59+
}

src/WiFiConnectionManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO

0 commit comments

Comments
 (0)