Skip to content

Commit 6847c0e

Browse files
committed
Add begin method to configure Clients not using ConnectionHandler
1 parent d2b2980 commit 6847c0e

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

Diff for: src/ArduinoIoTCloudTCP.cpp

+56-48
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,70 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
7979
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
8080
{
8181
_connection = &connection;
82-
_brokerAddress = brokerAddress;
83-
#ifdef BOARD_HAS_SECRET_KEY
84-
_brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
85-
#else
86-
_brokerPort = brokerPort;
87-
#endif
8882

8983
/* Setup broker TLS client */
90-
_brokerTLSClient.begin(connection);
84+
TLSClientBroker *brokerTLSClient = new TLSClientBroker();
85+
brokerTLSClient->begin(connection);
9186

9287
#if OTA_ENABLED
9388
/* Setup OTA TLS client */
94-
_otaTLSClient.begin(connection);
89+
TLSClientOta *otaTLSClient = new TLSClientOta();
90+
otaTLSClient->begin(connection);
91+
#endif
92+
93+
#if defined(BOARD_HAS_SECRET_KEY)
94+
/* If board is not configured for username and password login */
95+
if(!_password.length())
96+
{
97+
#endif
98+
99+
#if defined(BOARD_HAS_SECURE_ELEMENT)
100+
if (!_selement.begin())
101+
{
102+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
103+
#if defined(ARDUINO_UNOWIFIR4)
104+
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
105+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
106+
}
107+
#endif
108+
return 0;
109+
}
110+
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
111+
{
112+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
113+
return 0;
114+
}
115+
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
116+
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
117+
{
118+
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
119+
return 0;
120+
}
121+
brokerTLSClient->setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
122+
#if OTA_ENABLED
123+
otaTLSClient->setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
124+
#endif
125+
#endif
126+
#endif
127+
128+
#if defined(BOARD_HAS_SECRET_KEY)
129+
}
95130
#endif
96131

97132
/* Setup TimeService */
98133
_time_service.begin(_connection);
99134

135+
return begin(brokerTLSClient, otaTLSClient, enable_watchdog, brokerAddress, brokerPort);
136+
}
137+
138+
int ArduinoIoTCloudTCP::begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
139+
{
140+
_brokerTLSClient = mqttClient,
141+
_otaTLSClient = otaClient;
142+
100143
/* Setup retry timers */
101144
_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
102-
return begin(enable_watchdog, _brokerAddress, _brokerPort);
145+
return begin(enable_watchdog, brokerAddress, brokerPort);
103146
}
104147

105148
void ArduinoIoTCloudTCP::update()
@@ -155,45 +198,10 @@ void ArduinoIoTCloudTCP::printDebugInfo()
155198
int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
156199
{
157200
_brokerAddress = brokerAddress;
201+
#ifdef BOARD_HAS_SECRET_KEY
202+
_brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
203+
#else
158204
_brokerPort = brokerPort;
159-
160-
#if defined(BOARD_HAS_SECRET_KEY)
161-
/* If board is not configured for username and password login */
162-
if(!_password.length())
163-
{
164-
#endif
165-
166-
#if defined(BOARD_HAS_SECURE_ELEMENT)
167-
if (!_selement.begin())
168-
{
169-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
170-
#if defined(ARDUINO_UNOWIFIR4)
171-
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
172-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
173-
}
174-
#endif
175-
return 0;
176-
}
177-
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
178-
{
179-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
180-
return 0;
181-
}
182-
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
183-
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
184-
{
185-
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
186-
return 0;
187-
}
188-
_brokerTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
189-
#if OTA_ENABLED
190-
_otaTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
191-
#endif
192-
#endif
193-
#endif
194-
195-
#if defined(BOARD_HAS_SECRET_KEY)
196-
}
197205
#endif
198206

199207
_mqttClient.setClient(_brokerTLSClient);
@@ -217,7 +225,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
217225
_device.begin();
218226

219227
#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
220-
_ota.setClient(&_otaTLSClient);
228+
_ota.setClient(_otaTLSClient);
221229
#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
222230

223231
#if OTA_ENABLED && defined(OTA_BASIC_AUTH)

Diff for: src/ArduinoIoTCloudTCP.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
7575
virtual void printDebugInfo() override;
7676

7777
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
78+
int begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
7879

7980
#ifdef BOARD_HAS_SECRET_KEY
8081
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
@@ -138,7 +139,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
138139
#endif
139140
#endif
140141

141-
TLSClientBroker _brokerTLSClient;
142+
Client * _brokerTLSClient;
142143
MqttClient _mqttClient;
143144

144145
String _messageTopicOut;
@@ -148,7 +149,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
148149

149150

150151
#if OTA_ENABLED
151-
TLSClientOta _otaTLSClient;
152+
Client * _otaTLSClient;
152153
ArduinoCloudOTA _ota;
153154
onOTARequestCallbackFunc _get_ota_confirmation;
154155
#endif /* OTA_ENABLED */

0 commit comments

Comments
 (0)