1515 a commercial license, send an email to license@arduino.cc.
1616*/
1717
18- #include < ArduinoECCX08.h>
18+ #include < ArduinoIoTCloud.h>
19+
20+ #ifdef BOARD_HAS_ECCX08
1921#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
2326
2427#ifdef ARDUINO_ARCH_SAMD
2528 #include < RTCZero.h>
2629 RTCZero rtc;
2730#endif
2831
32+ #ifdef BOARD_HAS_ECCX08
2933const static int keySlot = 0 ;
3034const static int compressedCertSlot = 10 ;
3135const static int serialNumberAndAuthorityKeyIdentifierSlot = 11 ;
3236const static int deviceIdSlot = 12 ;
37+ #endif
3338
3439const static int CONNECT_SUCCESS = 1 ;
3540const static int CONNECT_FAILURE = 0 ;
@@ -56,7 +61,10 @@ static unsigned long getTime() {
5661ArduinoIoTCloudClass::ArduinoIoTCloudClass () :
5762 _connection(NULL ),
5863 _thing_id(" " ),
59- _bearSslClient(NULL ),
64+ _sslClient(NULL ),
65+ #ifdef BOARD_ESP
66+ _certificate (MQTTS_UP_ARDUINO_CC_CERTIFICATE),
67+ #endif
6068 _mqttClient (NULL ),
6169 _lastSyncRequestTickTime(0 ),
6270 _stdinTopic(" " ),
@@ -68,31 +76,41 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
6876 _otaTopic(" " ),
6977 _on_sync_event_callback(NULL ),
7078 _on_connect_event_callback(NULL ),
71- _on_disconnect_event_callback(NULL ) {
72-
73- }
79+ _on_disconnect_event_callback(NULL ),
80+ _device_id( " " ),
81+ _password( " " ) { }
7482
7583ArduinoIoTCloudClass::~ArduinoIoTCloudClass () {
7684 if (_mqttClient) {
7785 delete _mqttClient;
7886 _mqttClient = NULL ;
7987 }
8088
81- if (_bearSslClient ) {
82- delete _bearSslClient ;
83- _bearSslClient = NULL ;
89+ if (_sslClient ) {
90+ delete _sslClient ;
91+ _sslClient = NULL ;
8492 }
8593}
8694
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;
90108 _brokerAddress = brokerAddress;
91109 _brokerPort = brokerPort;
92110 #ifdef ARDUINO_ARCH_SAMD
93111 rtc.begin ();
94112 #endif
95- return begin (connectionClient , _brokerAddress, _brokerPort);
113+ return begin (_connection-> getClient () , _brokerAddress, _brokerPort);
96114}
97115
98116int ArduinoIoTCloudClass::begin (Client& net, String brokerAddress, uint16_t brokerPort) {
@@ -101,8 +119,9 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
101119 // store the broker address as class member
102120 _brokerAddress = brokerAddress;
103121 _brokerPort = brokerPort;
104- byte deviceIdBytes[72 ];
105122
123+ #ifdef BOARD_HAS_ECCX08
124+ byte deviceIdBytes[72 ];
106125 if (!ECCX08.begin ()) {
107126 Debug.print (DBG_ERROR, " Cryptography processor failure. Make sure you have a compatible board." );
108127 return 0 ;
@@ -112,7 +131,7 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
112131 Debug.print (DBG_ERROR, " Cryptography processor read failure." );
113132 return 0 ;
114133 }
115- _device_id = (char *)deviceIdBytes;
134+ _device_id = (char *)deviceIdBytes;
116135
117136 if (!ECCX08Cert.beginReconstruction (keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
118137 Debug.print (DBG_ERROR, " Cryptography certificate reconstruction failure." );
@@ -129,26 +148,44 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
129148 Debug.print (DBG_ERROR, " Cryptography certificate reconstruction failure." );
130149 return 0 ;
131150 }
151+ #endif /* BOARD_HAS_ECCX08 */
132152
133- if (_bearSslClient) {
134- delete _bearSslClient;
153+ if (_sslClient) {
154+ delete _sslClient;
155+ _sslClient = NULL ;
135156 }
157+
158+ #ifdef BOARD_HAS_ECCX08
136159 if (_connection != NULL ) {
137- _bearSslClient = new BearSSLClient (_connection->getClient ());
160+ _sslClient = new BearSSLClient (_connection->getClient ());
138161 } else {
139- _bearSslClient = new BearSSLClient (*_net);
162+ _sslClient = new BearSSLClient (*_net);
140163 }
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);
141176
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
144180
145181 // Bind ArduinoBearSSL callback using static "non-method" function
146182 if (_connection != NULL ) {
147183 getTimeConnection = _connection;
184+ #ifdef BOARD_HAS_ECCX08
148185 ArduinoBearSSL.onGetTime (getTime);
186+ #endif
149187 }
150188
151-
152189 // TODO: Find a better way to allow callback into object method
153190 // Begin function for the MQTTClient
154191 mqttClientBegin ();
@@ -157,8 +194,10 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress, uint16_t brok
157194 return 1 ;
158195}
159196
160- void ArduinoIoTCloudClass::onGetTime (unsigned long (*callback)(void )) {
197+ void ArduinoIoTCloudClass::onGetTime (unsigned long (*callback)(void )) {
198+ #ifdef BOARD_HAS_ECCX08
161199 ArduinoBearSSL.onGetTime (callback);
200+ #endif /* BOARD_HAS_ECCX08 */
162201}
163202
164203// private class method used to initialize mqttClient class member. (called in the begin class method)
@@ -184,8 +223,7 @@ void ArduinoIoTCloudClass::mqttClientBegin() {
184223}
185224
186225int ArduinoIoTCloudClass::connect () {
187- // Username: device id
188- // Password: empty
226+
189227 if (!_mqttClient->connect (_brokerAddress.c_str (), _brokerPort)) {
190228 return CONNECT_FAILURE;
191229 }
@@ -419,7 +457,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
419457void ArduinoIoTCloudClass::setIoTConnectionState (ArduinoIoTConnectionStatus newState) {
420458 iotStatus = newState;
421459 switch (iotStatus) {
422- case ArduinoIoTConnectionStatus::IDLE: break ;
460+ case ArduinoIoTConnectionStatus::IDLE: break ;
423461 case ArduinoIoTConnectionStatus::ERROR: Debug.print (DBG_ERROR, " Arduino, we have a problem." ); break ;
424462 case ArduinoIoTConnectionStatus::CONNECTING: Debug.print (DBG_ERROR, " Connecting to Arduino IoT Cloud..." ); break ;
425463 case ArduinoIoTConnectionStatus::RECONNECTING: Debug.print (DBG_ERROR, " Reconnecting to Arduino IoT Cloud..." ); break ;
0 commit comments