Skip to content

Commit

Permalink
Add begin method to configure Clients not using ConnectionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Jun 25, 2024
1 parent d2b2980 commit 6847c0e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
104 changes: 56 additions & 48 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,70 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_connection = &connection;
_brokerAddress = brokerAddress;
#ifdef BOARD_HAS_SECRET_KEY
_brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
#else
_brokerPort = brokerPort;
#endif

/* Setup broker TLS client */
_brokerTLSClient.begin(connection);
TLSClientBroker *brokerTLSClient = new TLSClientBroker();
brokerTLSClient->begin(connection);

#if OTA_ENABLED
/* Setup OTA TLS client */
_otaTLSClient.begin(connection);
TLSClientOta *otaTLSClient = new TLSClientOta();
otaTLSClient->begin(connection);
#endif

#if defined(BOARD_HAS_SECRET_KEY)
/* If board is not configured for username and password login */
if(!_password.length())
{
#endif

#if defined(BOARD_HAS_SECURE_ELEMENT)
if (!_selement.begin())
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
#if defined(ARDUINO_UNOWIFIR4)
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
}
#endif
return 0;
}
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
return 0;
}
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
return 0;
}
brokerTLSClient->setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
#if OTA_ENABLED
otaTLSClient->setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
#endif
#endif
#endif

#if defined(BOARD_HAS_SECRET_KEY)
}
#endif

/* Setup TimeService */
_time_service.begin(_connection);

return begin(brokerTLSClient, otaTLSClient, enable_watchdog, brokerAddress, brokerPort);
}

int ArduinoIoTCloudTCP::begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_brokerTLSClient = mqttClient,
_otaTLSClient = otaClient;

/* Setup retry timers */
_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
return begin(enable_watchdog, _brokerAddress, _brokerPort);
return begin(enable_watchdog, brokerAddress, brokerPort);
}

void ArduinoIoTCloudTCP::update()
Expand Down Expand Up @@ -155,45 +198,10 @@ void ArduinoIoTCloudTCP::printDebugInfo()
int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
{
_brokerAddress = brokerAddress;
#ifdef BOARD_HAS_SECRET_KEY
_brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
#else
_brokerPort = brokerPort;

#if defined(BOARD_HAS_SECRET_KEY)
/* If board is not configured for username and password login */
if(!_password.length())
{
#endif

#if defined(BOARD_HAS_SECURE_ELEMENT)
if (!_selement.begin())
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__);
#if defined(ARDUINO_UNOWIFIR4)
if (String(WiFi.firmwareVersion()) < String("0.4.1")) {
DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion());
}
#endif
return 0;
}
if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__);
return 0;
}
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate))
{
DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__);
return 0;
}
_brokerTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
#if OTA_ENABLED
_otaTLSClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
#endif
#endif
#endif

#if defined(BOARD_HAS_SECRET_KEY)
}
#endif

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

#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)
_ota.setClient(&_otaTLSClient);
_ota.setClient(_otaTLSClient);
#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD)

#if OTA_ENABLED && defined(OTA_BASIC_AUTH)
Expand Down
5 changes: 3 additions & 2 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
virtual void printDebugInfo() override;

int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
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);

#ifdef BOARD_HAS_SECRET_KEY
inline void setBoardId (String const device_id) { setDeviceId(device_id); }
Expand Down Expand Up @@ -138,7 +139,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
#endif
#endif

TLSClientBroker _brokerTLSClient;
Client * _brokerTLSClient;
MqttClient _mqttClient;

String _messageTopicOut;
Expand All @@ -148,7 +149,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass


#if OTA_ENABLED
TLSClientOta _otaTLSClient;
Client * _otaTLSClient;
ArduinoCloudOTA _ota;
onOTARequestCallbackFunc _get_ota_confirmation;
#endif /* OTA_ENABLED */
Expand Down

0 comments on commit 6847c0e

Please sign in to comment.