15
15
a commercial license, send an email to [email protected] .
16
16
*/
17
17
18
- #include < ArduinoECCX08.h>
18
+ #include < ArduinoIoTCloud.h>
19
+
20
+ #ifdef BOARD_HAS_ECCX08
19
21
#include " utility/ECCX08Cert.h"
20
- #include " CloudSerial.h"
21
- #include " ArduinoIoTCloud.h"
22
- #include < Arduino_DebugUtils.h>
22
+ #include < ArduinoECCX08.h>
23
+ #elif defined(BOARD_ESP)
24
+ #include " utility/ca_cert.h"
25
+ #endif
23
26
24
27
#ifdef ARDUINO_ARCH_SAMD
25
28
#include < RTCZero.h>
26
29
RTCZero rtc;
27
30
#endif
28
31
32
+ #ifdef BOARD_HAS_ECCX08
29
33
const static int keySlot = 0 ;
30
34
const static int compressedCertSlot = 10 ;
31
35
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11 ;
32
36
const static int deviceIdSlot = 12 ;
37
+ #endif
33
38
34
39
const static int CONNECT_SUCCESS = 1 ;
35
40
const static int CONNECT_FAILURE = 0 ;
@@ -56,7 +61,10 @@ static unsigned long getTime() {
56
61
ArduinoIoTCloudClass::ArduinoIoTCloudClass () :
57
62
_connection(NULL ),
58
63
_thing_id(" " ),
59
- _bearSslClient(NULL ),
64
+ _sslClient(NULL ),
65
+ #ifdef BOARD_ESP
66
+ _certificate (MQTTS_UP_ARDUINO_CC_CERTIFICATE),
67
+ #endif
60
68
_mqttClient (NULL ),
61
69
_lastSyncRequestTickTime(0 ),
62
70
_stdinTopic(" " ),
@@ -68,31 +76,41 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
68
76
_otaTopic(" " ),
69
77
_on_sync_event_callback(NULL ),
70
78
_on_connect_event_callback(NULL ),
71
- _on_disconnect_event_callback(NULL ) {
72
-
73
- }
79
+ _on_disconnect_event_callback(NULL ),
80
+ _device_id( " " ),
81
+ _password( " " ) { }
74
82
75
83
ArduinoIoTCloudClass::~ArduinoIoTCloudClass () {
76
84
if (_mqttClient) {
77
85
delete _mqttClient;
78
86
_mqttClient = NULL ;
79
87
}
80
88
81
- if (_bearSslClient ) {
82
- delete _bearSslClient ;
83
- _bearSslClient = NULL ;
89
+ if (_sslClient ) {
90
+ delete _sslClient ;
91
+ _sslClient = NULL ;
84
92
}
85
93
}
86
94
87
- int ArduinoIoTCloudClass::begin (ConnectionHandler& c, String brokerAddress, uint16_t brokerPort) {
88
- _connection = &c;
89
- Client &connectionClient = _connection->getClient ();
95
+ int ArduinoIoTCloudClass::begin (ConnectionHandler & connection,
96
+ String device_id,
97
+ String password,
98
+ String brokerAddress,
99
+ uint16_t brokerPort) {
100
+ _connection = &connection;
101
+ _device_id = device_id;
102
+ _password = password;
103
+ return begin (_connection->getClient (), brokerAddress, brokerPort);
104
+ }
105
+
106
+ int ArduinoIoTCloudClass::begin (ConnectionHandler & connection, String brokerAddress, uint16_t brokerPort) {
107
+ _connection = &connection;
90
108
_brokerAddress = brokerAddress;
91
109
_brokerPort = brokerPort;
92
110
#ifdef ARDUINO_ARCH_SAMD
93
111
rtc.begin ();
94
112
#endif
95
- return begin (connectionClient , _brokerAddress, _brokerPort);
113
+ return begin (_connection-> getClient () , _brokerAddress, _brokerPort);
96
114
}
97
115
98
116
int ArduinoIoTCloudClass::begin (Client& net, String brokerAddress, uint16_t brokerPort) {
@@ -101,8 +119,9 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
101
119
// store the broker address as class member
102
120
_brokerAddress = brokerAddress;
103
121
_brokerPort = brokerPort;
104
- byte deviceIdBytes[72 ];
105
122
123
+ #ifdef BOARD_HAS_ECCX08
124
+ byte deviceIdBytes[72 ];
106
125
if (!ECCX08.begin ()) {
107
126
Debug.print (DBG_ERROR, " Cryptography processor failure. Make sure you have a compatible board." );
108
127
return 0 ;
@@ -112,7 +131,7 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
112
131
Debug.print (DBG_ERROR, " Cryptography processor read failure." );
113
132
return 0 ;
114
133
}
115
- _device_id = (char *)deviceIdBytes;
134
+ _device_id = (char *)deviceIdBytes;
116
135
117
136
if (!ECCX08Cert.beginReconstruction (keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
118
137
Debug.print (DBG_ERROR, " Cryptography certificate reconstruction failure." );
@@ -129,26 +148,44 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
129
148
Debug.print (DBG_ERROR, " Cryptography certificate reconstruction failure." );
130
149
return 0 ;
131
150
}
151
+ #endif /* BOARD_HAS_ECCX08 */
132
152
133
- if (_bearSslClient) {
134
- delete _bearSslClient;
153
+ if (_sslClient) {
154
+ delete _sslClient;
155
+ _sslClient = NULL ;
135
156
}
157
+
158
+ #ifdef BOARD_HAS_ECCX08
136
159
if (_connection != NULL ) {
137
- _bearSslClient = new BearSSLClient (_connection->getClient ());
160
+ _sslClient = new BearSSLClient (_connection->getClient ());
138
161
} else {
139
- _bearSslClient = new BearSSLClient (*_net);
162
+ _sslClient = new BearSSLClient (*_net);
140
163
}
164
+ #elif defined(BOARD_ESP)
165
+ _sslClient = new WiFiClientSecure ();
166
+ Debug.print (DBG_VERBOSE, " new WiFiClientSecure()" );
167
+ #endif
168
+
169
+ #ifdef BOARD_HAS_ECCX08
170
+ _sslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
171
+ #elif defined(BOARD_ESP)
172
+ _sslClient->setTrustAnchors (&_certificate);
173
+ #endif
174
+
175
+ _mqttClient = new MqttClient (*_sslClient);
141
176
142
- _bearSslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
143
- _mqttClient = new MqttClient (*_bearSslClient);
177
+ #ifdef BOARD_ESP
178
+ _mqttClient->setUsernamePassword (_device_id, _password);
179
+ #endif
144
180
145
181
// Bind ArduinoBearSSL callback using static "non-method" function
146
182
if (_connection != NULL ) {
147
183
getTimeConnection = _connection;
184
+ #ifdef BOARD_HAS_ECCX08
148
185
ArduinoBearSSL.onGetTime (getTime);
186
+ #endif
149
187
}
150
188
151
-
152
189
// TODO: Find a better way to allow callback into object method
153
190
// Begin function for the MQTTClient
154
191
mqttClientBegin ();
@@ -157,8 +194,10 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
157
194
return 1 ;
158
195
}
159
196
160
- void ArduinoIoTCloudClass::onGetTime (unsigned long (*callback)(void )) {
197
+ void ArduinoIoTCloudClass::onGetTime (unsigned long (*callback)(void )) {
198
+ #ifdef BOARD_HAS_ECCX08
161
199
ArduinoBearSSL.onGetTime (callback);
200
+ #endif /* BOARD_HAS_ECCX08 */
162
201
}
163
202
164
203
// private class method used to initialize mqttClient class member. (called in the begin class method)
@@ -184,8 +223,7 @@ void ArduinoIoTCloudClass::mqttClientBegin() {
184
223
}
185
224
186
225
int ArduinoIoTCloudClass::connect () {
187
- // Username: device id
188
- // Password: empty
226
+
189
227
if (!_mqttClient->connect (_brokerAddress.c_str (), _brokerPort)) {
190
228
return CONNECT_FAILURE;
191
229
}
@@ -419,7 +457,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
419
457
void ArduinoIoTCloudClass::setIoTConnectionState (ArduinoIoTConnectionStatus newState) {
420
458
iotStatus = newState;
421
459
switch (iotStatus) {
422
- case ArduinoIoTConnectionStatus::IDLE: break ;
460
+ case ArduinoIoTConnectionStatus::IDLE: break ;
423
461
case ArduinoIoTConnectionStatus::ERROR: Debug.print (DBG_ERROR, " Arduino, we have a problem." ); break ;
424
462
case ArduinoIoTConnectionStatus::CONNECTING: Debug.print (DBG_ERROR, " Connecting to Arduino IoT Cloud..." ); break ;
425
463
case ArduinoIoTConnectionStatus::RECONNECTING: Debug.print (DBG_ERROR, " Reconnecting to Arduino IoT Cloud..." ); break ;
0 commit comments