-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoCloud.cpp
178 lines (140 loc) · 4.65 KB
/
ArduinoCloud.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ArduinoECCX08.h>
#include "utility/ECCX08Cert.h"
#include "CloudSerial.h"
#include "ArduinoCloudV2.h"
const static char server[] = "mqtts-sa.iot.oniudra.cc";
const static int keySlot = 0;
const static int compressedCertSlot = 10;
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
const static int thingIdSlot = 12;
ArduinoCloudClass::ArduinoCloudClass() :
_bearSslClient(NULL),
// Size of the receive buffer
_mqttClient(MQTT_BUFFER_SIZE)
{
}
ArduinoCloudClass::~ArduinoCloudClass()
{
if (_bearSslClient) {
delete _bearSslClient;
}
}
int ArduinoCloudClass::begin(Client& net)
{
byte thingIdBytes[72];
if (!ECCX08.begin()) {
return 0;
}
if (!ECCX08.readSlot(thingIdSlot, thingIdBytes, sizeof(thingIdBytes))) {
return 0;
}
_id = (char*)thingIdBytes;
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
return 0;
}
ECCX08Cert.setSubjectCommonName(_id);
ECCX08Cert.setIssuerCountryName("US");
ECCX08Cert.setIssuerOrganizationName("Arduino LLC US");
ECCX08Cert.setIssuerOrganizationalUnitName("IT");
ECCX08Cert.setIssuerCommonName("Arduino");
if (!ECCX08Cert.endReconstruction()) {
return 0;
}
if (_bearSslClient) {
delete _bearSslClient;
}
_bearSslClient = new BearSSLClient(net);
_bearSslClient->setEccSlot(keySlot, ECCX08Cert.bytes(), ECCX08Cert.length());
// Begin function for the MQTTClient
mqttClientBegin(*_bearSslClient);
return 1;
}
// private class method used to initialize mqttClient class member. (called in the begin class method)
void ArduinoCloudClass::mqttClientBegin(Client& net)
{
// MQTT topics definition
_stdoutTopic = "/a/d/" + _id + "/s/o";
_stdinTopic = "/a/d/" + _id + "/s/i";
// use onMessage as callback for received mqtt messages
_mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage);
_mqttClient.begin(server, 8883, net);
// Set MQTT connection options
_mqttClient.setOptions(mqttOpt.keepAlive, mqttOpt.cleanSession, mqttOpt.timeout);
}
int ArduinoCloudClass::connect()
{
// Username: device id
// Password: empty
if (!_mqttClient.connect(_id.c_str())) {
return 0;
}
_mqttClient.subscribe(_stdinTopic);
return 1;
}
bool ArduinoCloudClass::disconnect()
{
return _mqttClient.disconnect();
}
void ArduinoCloudClass::poll()
{
// If user call poll() without parameters use the default ones
poll(MAX_RETRIES, RECONNECTION_TIMEOUT);
}
bool ArduinoCloudClass::mqttReconnect(int maxRetries, int timeout)
{
// Counter for reconnection retries
int retries = 0;
unsigned long start = millis();
// Check for MQTT broker connection, of if maxReties limit is reached
// if MQTTClient is connected , simply do nothing and retun true
while(!_mqttClient.connected() && (retries++ < maxRetries) && (millis() - start < timeout)) {
// Get last MTTQClient error, (a common error may be a buffer overflow)
lwmqtt_err_t err = _mqttClient.lastError();
// try establish the MQTT broker connection
connect();
}
// It was impossible to establish a connection, return
if ((retries == maxRetries) || (millis() - start >= timeout))
return false;
return true;
}
void ArduinoCloudClass::poll(int reconnectionMaxRetries, int reconnectionTimeoutMs)
{
// Method's argument controls
int maxRetries = (reconnectionMaxRetries > 0) ? reconnectionMaxRetries : MAX_RETRIES;
int timeout = (reconnectionTimeoutMs > 0) ? reconnectionTimeoutMs : RECONNECTION_TIMEOUT;
// If the reconnect() culd not establish the connection, return the control to the user sketch
if (!mqttReconnect(maxRetries, timeout))
return;
// MTTQClient connected!, poll() used to retrieve data from MQTT broker
_mqttClient.loop();
}
void ArduinoCloudClass::reconnect(Client& net)
{
// Initialize again the MQTTClient, otherwise it would not be able to receive messages through its callback
mqttClientBegin(net);
connect();
}
void ArduinoCloudClass::onGetTime(unsigned long(*callback)(void))
{
ArduinoBearSSL.onGetTime(callback);
}
int ArduinoCloudClass::connected()
{
return _mqttClient.connected();
}
int ArduinoCloudClass::writeStdout(const byte data[], int length)
{
return _mqttClient.publish(_stdoutTopic.c_str(), (const char*)data, length);
}
void ArduinoCloudClass::onMessage(MQTTClient* /*client*/, char topic[], char bytes[], int length)
{
ArduinoCloud.handleMessage(topic, bytes, length);
}
void ArduinoCloudClass::handleMessage(char topic[], char bytes[], int length)
{
if (_stdinTopic == topic) {
CloudSerial.appendStdin((uint8_t*)bytes, length);
}
}
ArduinoCloudClass ArduinoCloud;