-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathArduinoIoTCloudTCP.h
158 lines (118 loc) · 5.7 KB
/
ArduinoIoTCloudTCP.h
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
/*
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].
*/
#ifndef ARDUINO_IOT_CLOUD_TCP_H
#define ARDUINO_IOT_CLOUD_TCP_H
/******************************************************************************
* INCLUDE
******************************************************************************/
#include <ArduinoIoTCloud_Config.h>
#include <ArduinoIoTCloud.h>
#ifdef BOARD_HAS_ECCX08
#include "tls/BearSSLClient.h"
#include "tls/utility/ECCX08Cert.h"
#elif defined(BOARD_ESP)
#include <WiFiClientSecure.h>
#endif
#include <ArduinoMqttClient.h>
#if OTA_ENABLED
#include "utility/ota/OTALogic.h"
#include "utility/ota/OTAStorage.h"
#endif /* OTA_ENABLED */
/******************************************************************************
CONSTANTS
******************************************************************************/
static char const DEFAULT_BROKER_ADDRESS_SECURE_AUTH[] = "mqtts-sa.iot.arduino.cc";
static uint16_t const DEFAULT_BROKER_PORT_SECURE_AUTH = 8883;
static char const DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH[] = "mqtts-up.iot.arduino.cc";
static uint16_t const DEFAULT_BROKER_PORT_USER_PASS_AUTH = 8884;
/******************************************************************************
* CLASS DECLARATION
******************************************************************************/
class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
{
public:
ArduinoIoTCloudTCP();
virtual ~ArduinoIoTCloudTCP() { }
virtual void update () override;
virtual int connected () override;
virtual void printDebugInfo() override;
#ifdef BOARD_HAS_ECCX08
int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
#else
int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
#endif
int begin(String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
#ifdef BOARD_ESP
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
inline void setSecretDeviceKey(String const password) { _password = password; }
#endif
inline String getBrokerAddress() const { return _brokerAddress; }
inline uint16_t getBrokerPort () const { return _brokerPort; }
#if OTA_ENABLED
void setOTAStorage(OTAStorage & ota_storage);
#endif /* OTA_ENABLED */
// Clean up existing Mqtt connection, create a new one and initialize it
int reconnect();
friend class CloudSerialClass;
protected:
virtual int connect () override;
virtual void disconnect () override;
private:
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
int _lastSyncRequestTickTime;
String _brokerAddress;
uint16_t _brokerPort;
uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];
int _mqtt_data_len;
bool _mqtt_data_request_retransmit;
#ifdef BOARD_HAS_ECCX08
ECCX08CertClass _eccx08_cert;
BearSSLClient _sslClient;
#elif defined(BOARD_ESP)
WiFiClientSecure _sslClient;
String _password;
#endif
MqttClient _mqttClient;
ArduinoIoTSynchronizationStatus _syncStatus;
// Class attribute to define MTTQ topics 2 for stdIn/out and 2 for data, in order to avoid getting previous pupblished payload
String _stdinTopic;
String _stdoutTopic;
String _shadowTopicOut;
String _shadowTopicIn;
String _dataTopicOut;
String _dataTopicIn;
String _ota_topic_in;
#if OTA_ENABLED
OTALogic _ota_logic;
int _ota_error;
#endif /* OTA_ENABLED */
inline String getTopic_stdin () { return String("/a/d/" + getDeviceId() + "/s/i"); }
inline String getTopic_stdout () { return String("/a/d/" + getDeviceId() + "/s/o"); }
inline String getTopic_shadowout() { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/o"); }
inline String getTopic_shadowin () { return ( getThingId().length() == 0) ? String("") : String("/a/t/" + getThingId() + "/shadow/i"); }
inline String getTopic_dataout () { return ( getThingId().length() == 0) ? String("/a/d/" + getDeviceId() + "/e/o") : String("/a/t/" + getThingId() + "/e/o"); }
inline String getTopic_datain () { return ( getThingId().length() == 0) ? String("/a/d/" + getDeviceId() + "/e/i") : String("/a/t/" + getThingId() + "/e/i"); }
inline String getTopic_ota_in () { return String("/a/d/" + getDeviceId() + "/ota/i"); }
static void onMessage(int length);
void handleMessage(int length);
void sendPropertiesToCloud();
void requestLastValue();
ArduinoIoTConnectionStatus checkCloudConnection();
int write(String const topic, byte const data[], int const length);
};
/******************************************************************************
* EXTERN DECLARATION
******************************************************************************/
extern ArduinoIoTCloudTCP ArduinoCloud;
#endif