-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathArduinoIoTCloudLPWAN.cpp
205 lines (165 loc) · 5.67 KB
/
ArduinoIoTCloudLPWAN.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
204
205
/*
This file is part of ArduinoIoTCloud.
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
#include "ArduinoIoTCloud_Defines.h"
#ifdef HAS_LORA
#include<ArduinoIoTCloudLPWAN.h>
#ifdef ARDUINO_ARCH_SAMD
#include <RTCZero.h>
RTCZero rtc;
#endif
ArduinoIoTCloudLPWAN::ArduinoIoTCloudLPWAN() :
_connection(NULL) {}
ArduinoIoTCloudLPWAN::~ArduinoIoTCloudLPWAN() {
}
int ArduinoIoTCloudLPWAN::connect() {
_connection->connect();
const int state = _connection->getStatus() == NetworkConnectionState::INIT ? 1 : 0;
return state;
}
bool ArduinoIoTCloudLPWAN::disconnect() {
_connection->disconnect();
return true;
}
int ArduinoIoTCloudLPWAN::connected() {
int state = _connection->getStatus() == NetworkConnectionState::INIT ? 1 : 0;
return state;
}
int ArduinoIoTCloudLPWAN::begin(LPWANConnectionHandler& connection, bool retry) {
_connection = &connection;
_connection->init();
_retryEnable = retry;
_maxNumRetry = 5;
_intervalRetry = 1000;
Thing.begin();
return 1;
}
void ArduinoIoTCloudLPWAN::update() {
// Check if a primitive property wrapper is locally changed
Thing.updateTimestampOnLocallyChangedProperties();
connectionCheck();
if (_iotStatus != ArduinoIoTConnectionStatus::CONNECTED) {
return;
}
if (_connection->available()) {
uint8_t msgBuf[DEFAULT_CBOR_LORA_MSG_SIZE];
uint8_t i = 0;
while (_connection->available()) {
msgBuf[i++] = _connection->read();
}
Thing.decode(msgBuf, sizeof(msgBuf));
}
sendPropertiesToCloud();
execCloudEventCallback(_on_sync_event_callback, 0 /* callback_arg */);
}
void ArduinoIoTCloudLPWAN::connectionCheck() {
if (_connection != NULL) {
_connection->check();
if (_connection->getStatus() != NetworkConnectionState::CONNECTED) {
if (_iotStatus == ArduinoIoTConnectionStatus::CONNECTED) {
_iotStatus = ArduinoIoTConnectionStatus::DISCONNECTED;
printConnectionStatus(_iotStatus);
}
return;
}
}
switch (_iotStatus) {
case ArduinoIoTConnectionStatus::IDLE: {
_iotStatus = ArduinoIoTConnectionStatus::CONNECTING;
printConnectionStatus(_iotStatus);
}
break;
case ArduinoIoTConnectionStatus::ERROR: {
_iotStatus = ArduinoIoTConnectionStatus::RECONNECTING;
printConnectionStatus(_iotStatus);
}
break;
case ArduinoIoTConnectionStatus::CONNECTED: {
if (_connection->getStatus() != NetworkConnectionState::CONNECTED) {
_iotStatus = ArduinoIoTConnectionStatus::DISCONNECTED;
printConnectionStatus(_iotStatus);
execCloudEventCallback(_on_disconnect_event_callback, 0 /* callback_arg - e.g. could be error code casted to void * */);
}
}
break;
case ArduinoIoTConnectionStatus::DISCONNECTED: {
_iotStatus = ArduinoIoTConnectionStatus::RECONNECTING;
printConnectionStatus(_iotStatus);
}
break;
case ArduinoIoTConnectionStatus::RECONNECTING: {
int const ret_code = connect();
Debug.print(DBG_INFO, "ArduinoCloud.reconnect()");
if (ret_code == 1) {
_iotStatus = ArduinoIoTConnectionStatus::IDLE;
} else {
_iotStatus = ArduinoIoTConnectionStatus::ERROR;
}
}
break;
case ArduinoIoTConnectionStatus::CONNECTING: {
NetworkConnectionState net_status = _connection->getStatus();
Debug.print(DBG_VERBOSE, "ArduinoCloud.connect(): %d", net_status);
if (net_status == NetworkConnectionState::CONNECTED) {
_iotStatus = ArduinoIoTConnectionStatus::CONNECTED;
printConnectionStatus(_iotStatus);
execCloudEventCallback(_on_connect_event_callback, 0 /* callback_arg */);
}
}
break;
}
}
void ArduinoIoTCloudLPWAN::printDebugInfo() {
Debug.print(DBG_INFO, "***** Arduino IoT Cloud LPWAN- configuration info *****");
Debug.print(DBG_INFO, "Device ID: %s", getDeviceId().c_str());
Debug.print(DBG_INFO, "Thing ID: %s", getThingId().c_str());
}
int ArduinoIoTCloudLPWAN::writeProperties(const byte data[], int length) {
int retcode = _connection->write(data, length);
int i = 0;
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
delay(_intervalRetry);
retcode = _connection->write(data, length);
i++;
}
return 1;
}
int ArduinoIoTCloudLPWAN::writeStdout(const byte data[], int length) {
int retcode = _connection->write(data, length);
int i = 0;
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
delay(_intervalRetry);
retcode = _connection->write(data, length);
i++;
}
return 1;
}
int ArduinoIoTCloudLPWAN::writeShadowOut(const byte data[], int length) {
int retcode = _connection->write(data, length);
int i = 0;
while (_retryEnable && retcode < 0 && i < _maxNumRetry) {
delay(_intervalRetry);
retcode = _connection->write(data, length);
i++;
}
return 1;
}
void ArduinoIoTCloudLPWAN::sendPropertiesToCloud() {
uint8_t data[DEFAULT_CBOR_LORA_MSG_SIZE];
int const length = Thing.encode(data, sizeof(data), true);
if (length > 0) {
writeProperties(data, length);
}
}
ArduinoIoTCloudLPWAN ArduinoCloud;
#endif