-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathArduinoCloud.cpp
203 lines (165 loc) · 5.64 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "ArduinoCloudV2.h"
#include <ArduinoECCX08.h>
#include <utility/ECCX08Cert.h>
#include "CloudSerial.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);
// Thing initialization
Thing.begin();
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";
_dataTopicIn = "/a/d/" + _id + "/e/i";
_dataTopicOut = "/a/d/" + _id + "/e/o";
// use onMessage as callback for received mqtt messages
_mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage);
_mqttClient.begin(server, 8883, net);
// Set will for MQTT client: {topic, qos, retain message}
const char lastMessage[] = "abcb";
_mqttClient.setWill(_dataTopicOut.c_str(), lastMessage, false, 1);
// Set MQTT connection options
_mqttClient.setOptions(mqttOpt.keepAlive, mqttOpt.cleanSession, mqttOpt.timeout);
}
int ArduinoCloudClass::connect()
{
//TODO MQTT brocker connection
// Username: device id
// Password: empty
if (!_mqttClient.connect(_id.c_str())) {
return 0;
}
_mqttClient.subscribe(_stdinTopic);
_mqttClient.subscribe(_dataTopicIn);
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;
// 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) {
// 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();
// delay eventually used for the nex re-connection try
delay(timeout);
}
// It was impossible to establish a connection, return
if (retries == maxRetries)
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();
uint8_t data[MQTT_BUFFER_SIZE];
int length = Thing.poll(data, sizeof(data));
// Are there some read properties that must be sent to the cloud ??
if (length > 0) {
// Check the connection is ok! if not reconnect
// If a thing has all read properties only try to reconnect when the have to be sent(based on their update policy)
writeProperties(data, length);
}
}
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::writeProperties(const byte data[], int length)
{
return _mqttClient.publish(_dataTopicOut.c_str(), (const char*)data, length);
}
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 (strcmp(_stdinTopic.c_str(), topic) == 0) {
CloudSerial.appendStdin((uint8_t*)bytes, length);
}
if (strcmp(_dataTopicIn.c_str(), topic) == 0) {
Thing.decode((uint8_t*)bytes, length);
}
}
ArduinoCloudClass ArduinoCloud;