Skip to content

Commit 5ee11cf

Browse files
committed
Implementing username/password login for ESP8266
1 parent 9fc3de2 commit 5ee11cf

File tree

9 files changed

+351
-42
lines changed

9 files changed

+351
-42
lines changed

src/ArduinoIoTCloud.cpp

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@
1515
a commercial license, send an email to [email protected].
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
2933
const static int keySlot = 0;
3034
const static int compressedCertSlot = 10;
3135
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
3236
const static int deviceIdSlot = 12;
37+
#endif
3338

3439
const static int CONNECT_SUCCESS = 1;
3540
const static int CONNECT_FAILURE = 0;
@@ -56,7 +61,10 @@ static unsigned long getTime() {
5661
ArduinoIoTCloudClass::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

7583
ArduinoIoTCloudClass::~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

98116
int 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

186225
int 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() {
419457
void 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;

src/ArduinoIoTCloud.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@
1818
#ifndef ARDUINO_IOT_CLOUD_H
1919
#define ARDUINO_IOT_CLOUD_H
2020

21-
#include <ArduinoMqttClient.h>
22-
#include <ArduinoIoTCloudBearSSL.h>
21+
#include <ArduinoIoTCloud_Defines.h>
22+
23+
#ifdef BOARD_HAS_ECCX08
24+
#include <ArduinoIoTCloudBearSSL.h>
25+
#elif defined(BOARD_ESP)
26+
#include <WiFiClientSecure.h>
27+
#endif
28+
2329
#include <ArduinoCloudThing.h>
30+
#include <ArduinoMqttClient.h>
2431
#include <Arduino_DebugUtils.h>
2532
#include <Arduino_ConnectionHandler.h>
2633
#include "types/CloudWrapperBool.h"
@@ -46,8 +53,6 @@ typedef struct {
4653
int timeout;
4754
} mqttConnectionOptions;
4855

49-
//extern ConnectionHandler *ArduinoIoTPreferredConnection;
50-
5156
enum class ArduinoIoTConnectionStatus {
5257
IDLE,
5358
CONNECTING,
@@ -76,8 +81,9 @@ class ArduinoIoTCloudClass {
7681
ArduinoIoTCloudClass();
7782
~ArduinoIoTCloudClass();
7883

79-
int begin(ConnectionHandler& connection, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT);
80-
int begin(Client& net, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT);
84+
int begin(ConnectionHandler &connection, String device_id, String password, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT);
85+
int begin(ConnectionHandler &connection, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT);
86+
int begin(Client &net, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT);
8187
// Class constant declaration
8288
static const int MQTT_TRANSMIT_BUFFER_SIZE = 256;
8389
static const int TIMEOUT_FOR_LASTVALUES_SYNC = 10000;
@@ -190,6 +196,7 @@ class ArduinoIoTCloudClass {
190196
return iotStatus;
191197
}
192198
void setIoTConnectionState(ArduinoIoTConnectionStatus newState);
199+
193200
private:
194201
ArduinoIoTConnectionStatus iotStatus = ArduinoIoTConnectionStatus::IDLE;
195202
ConnectionHandler * _connection;
@@ -199,14 +206,19 @@ class ArduinoIoTCloudClass {
199206

200207
void sendPropertiesToCloud();
201208

202-
203-
String _device_id,
204-
_thing_id,
205-
_brokerAddress;
209+
String _device_id, _password, _thing_id, _brokerAddress;
206210
uint16_t _brokerPort;
211+
207212
ArduinoCloudThing Thing;
208-
BearSSLClient* _bearSslClient;
209-
MqttClient* _mqttClient;
213+
214+
#ifdef BOARD_HAS_ECCX08
215+
BearSSLClient *_sslClient;
216+
#elif defined(BOARD_ESP)
217+
WiFiClientSecure *_sslClient;
218+
X509List _certificate;
219+
#endif
220+
221+
MqttClient *_mqttClient;
210222
int _lastSyncRequestTickTime;
211223

212224

src/ArduinoIoTCloud_Defines.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_IOT_CLOUD_DEFINES_H_
19+
#define ARDUINO_IOT_CLOUD_DEFINES_H_
20+
21+
#if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWIFI1010) || \
22+
defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT)
23+
#define BOARD_HAS_ECCX08
24+
#endif
25+
26+
#if defined(ARDUINO_ESP8266_ESP12) || defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
27+
#define BOARD_ESP
28+
#endif
29+
30+
#endif /* ARDUINO_IOT_CLOUD_DEFINES_H_ */

src/CloudSerial.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
#define CLOUD_SERIAL_H
2020

2121
#include <Arduino.h>
22+
#if defined(ARDUINO_ESP8266_ESP12) || defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
23+
#include "utility/RingBuffer.h"
24+
#else
2225
#include <RingBuffer.h>
26+
#endif
2327

2428
#define CLOUD_SERIAL_TX_BUFFER_SIZE 64
2529
#define CLOUD_SERIAL_RX_BUFFER_SIZE 512

src/utility/ECCX08Cert.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
a commercial license, send an email to [email protected].
1616
*/
1717

18+
#include <ArduinoIoTCloud_Defines.h>
19+
20+
#ifdef BOARD_HAS_ECCX08
21+
1822
#include <ArduinoIoTCloudBearSSL.h>
1923
#include <bearssl/bearssl_hash.h>
2024
#include <ArduinoECCX08.h>
@@ -920,3 +924,5 @@ int ECCX08CertClass::appendEcdsaWithSHA256(byte out[]) {
920924
}
921925

922926
ECCX08CertClass ECCX08Cert;
927+
928+
#endif /* BOARD_HAS_ECCX08 */

src/utility/ECCX08Cert.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#ifndef _ECCX08_CERT_H_
1919
#define _ECCC08_CERT_H_
2020

21+
#include <ArduinoIoTCloud_Defines.h>
22+
23+
#ifdef BOARD_HAS_ECCX08
24+
2125
#include <Arduino.h>
2226

2327
class ECCX08CertClass {
@@ -132,4 +136,6 @@ class ECCX08CertClass {
132136

133137
extern ECCX08CertClass ECCX08Cert;
134138

135-
#endif
139+
#endif /* BOARD_HAS_ECCX08 */
140+
141+
#endif /* _ECCX08_CERT_H_ */

src/utility/ECCX08TLSConfig.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#ifndef _ECCX08_TLS_CONFIG_H_
1919
#define _ECCX08_TLS_CONFIG_H_
2020

21+
#include <ArduinoIoTCloud_Defines.h>
22+
23+
#ifdef BOARD_HAS_ECCX08
24+
2125
const byte DEFAULT_ECCX08_TLS_CONFIG[128] = {
2226
// Read only - start
2327
// SN[0:3]
@@ -102,4 +106,6 @@ const byte DEFAULT_ECCX08_TLS_CONFIG[128] = {
102106
0x1C, 0x00
103107
};
104108

105-
#endif
109+
#endif /* BOARD_HAS_ECCX08 */
110+
111+
#endif /* _ECCX08_TLS_CONFIG_H_ */

0 commit comments

Comments
 (0)