-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoIoTCloudLPWAN.cpp
156 lines (120 loc) · 4.98 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
/*
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
******************************************************************************/
#include <ArduinoIoTCloud_Config.h>
#ifdef HAS_LORA
#include<ArduinoIoTCloudLPWAN.h>
#include "cbor/CBOREncoder.h"
/******************************************************************************
CONSTANTS
******************************************************************************/
static size_t const CBOR_LORA_MSG_MAX_SIZE = 255;
/******************************************************************************
CTOR/DTOR
******************************************************************************/
ArduinoIoTCloudLPWAN::ArduinoIoTCloudLPWAN()
: _retryEnable{false}
, _maxNumRetry{5}
, _intervalRetry{1000}
{
}
/******************************************************************************
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/
int ArduinoIoTCloudLPWAN::connected()
{
return (_connection->getStatus() == NetworkConnectionState::CONNECTED) ? 1 : 0;
}
int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry)
{
_connection = &connection;
_retryEnable = retry;
return 1;
}
void ArduinoIoTCloudLPWAN::update()
{
// Check if a primitive property wrapper is locally changed
updateTimestampOnLocallyChangedProperties(_property_container);
ArduinoIoTConnectionStatus next_iot_status = _iot_status;
/* Since we do not have a direct connection to the Arduino IoT Cloud servers
* there is no such thing is a 'cloud connection state' since the LoRa
* board connection state to the gateway is all the information we have.
*/
NetworkConnectionState const net_con_state = checkPhyConnection();
if (net_con_state == NetworkConnectionState::CONNECTED) { next_iot_status = ArduinoIoTConnectionStatus::CONNECTED; execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT); }
else if(net_con_state == NetworkConnectionState::CONNECTING) { next_iot_status = ArduinoIoTConnectionStatus::CONNECTING; }
else if(net_con_state == NetworkConnectionState::DISCONNECTED) { next_iot_status = ArduinoIoTConnectionStatus::DISCONNECTED; execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT); }
if(next_iot_status != _iot_status)
{
printConnectionStatus(next_iot_status);
_iot_status = next_iot_status;
}
if(net_con_state != NetworkConnectionState::CONNECTED) return;
if (_connection->available()) {
uint8_t msgBuf[CBOR_LORA_MSG_MAX_SIZE];
uint8_t i = 0;
while (_connection->available()) {
msgBuf[i++] = _connection->read();
}
CBORDecoder::decode(_property_container, msgBuf, sizeof(msgBuf));
}
sendPropertiesToCloud();
execCloudEventCallback(ArduinoIoTCloudEvent::SYNC);
}
void ArduinoIoTCloudLPWAN::printDebugInfo()
{
Debug.print(DBG_INFO, "***** Arduino IoT Cloud LPWAN - configuration info *****");
Debug.print(DBG_INFO, "Thing ID: %s", getThingId().c_str());
}
/******************************************************************************
* PROTECTED MEMBER FUNCTIONS
******************************************************************************/
int ArduinoIoTCloudLPWAN::connect()
{
_connection->connect();
return 1;
}
void ArduinoIoTCloudLPWAN::disconnect()
{
_connection->disconnect();
}
/******************************************************************************
* PRIVATE MEMBER FUNCTIONS
******************************************************************************/
void ArduinoIoTCloudLPWAN::sendPropertiesToCloud()
{
int bytes_encoded = 0;
uint8_t data[CBOR_LORA_MSG_MAX_SIZE];
if (CBOREncoder::encode(_property_container, data, sizeof(data), bytes_encoded, true) == CborNoError)
if (bytes_encoded > 0)
writeProperties(data, bytes_encoded);
}
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;
}
/******************************************************************************
* EXTERN DEFINITION
******************************************************************************/
ArduinoIoTCloudLPWAN ArduinoCloud;
#endif