@@ -67,6 +67,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
6767, _messageTopicIn(" " )
6868, _dataTopicOut(" " )
6969, _dataTopicIn(" " )
70+ , _authMode(ArduinoIoTAuthenticationMode::CERTIFICATE)
7071#if OTA_ENABLED
7172, _ota(&_message_stream)
7273, _get_ota_confirmation{nullptr }
@@ -84,20 +85,24 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
8485 _connection = &connection;
8586 _brokerAddress = brokerAddress;
8687
88+ #if defined (BOARD_HAS_SECRET_KEY)
89+ /* If board supports and sketch is configured for username and password login */
90+ if (_password.length ()) {
91+ _authMode = ArduinoIoTAuthenticationMode::PASSWORD;
92+ }
93+ #endif
94+
8795 /* Setup broker TLS client */
88- _brokerClient.begin (connection);
96+ _brokerClient.begin (connection, _authMode );
8997
9098#if OTA_ENABLED
9199 /* Setup OTA TLS client */
92100 _otaClient.begin (connection);
93101#endif
94102
95- #if defined (BOARD_HAS_SECRET_KEY)
96- /* If board is not configured for username and password login */
97- if (!_password.length ())
103+ /* If board is configured for certificate authentication and mTLS */
104+ if (_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE)
98105 {
99- #endif
100-
101106#if defined(BOARD_HAS_SECURE_ELEMENT)
102107 if (!_selement.begin ())
103108 {
@@ -130,14 +135,11 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
130135 #endif
131136 _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? mqttPort () : brokerPort;
132137#endif
133-
134- #if defined(BOARD_HAS_SECRET_KEY)
135138 }
136139 else
137140 {
138141 _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
139142 }
140- #endif
141143
142144 /* Setup TimeService */
143145 _time_service.begin (_connection);
0 commit comments