From efa7ee21d00a70558a05733116aab911ecbf9e08 Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Wed, 23 Oct 2024 16:28:20 +0200 Subject: [PATCH 1/7] java/iot3core: add TLS capability to the MQTT client --- .../java/com/orange/iot3core/IoT3Core.java | 70 ++++++------------- .../orange/iot3core/clients/MqttClient.java | 25 +++---- 2 files changed, 31 insertions(+), 64 deletions(-) diff --git a/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java b/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java index 4308a47f..c3dd3b55 100644 --- a/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java +++ b/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java @@ -31,51 +31,24 @@ public class IoT3Core { * Instantiate the IoT3.0 Core SDK. * * @param mqttHost MQTT broker address + * @param mqttPort port of the MQTT broker * @param mqttUsername MQTT username * @param mqttPassword MQTT password * @param mqttClientId unique MQTT client ID - * @param ioT3CoreCallback interface to retrieve the different clients outputs - * @param telemetryHost Open Telemetry server address - */ - public IoT3Core(String mqttHost, - String mqttUsername, - String mqttPassword, - String mqttClientId, - IoT3CoreCallback ioT3CoreCallback, - String telemetryHost) { - this(mqttHost, - 1883, - 8883, - mqttUsername, - mqttPassword, - mqttClientId, - ioT3CoreCallback, - telemetryHost, - -1, - "/telemetry", - "default", - "default"); - } - - /** - * Instantiate the IoT3.0 Core SDK. - * - * @param mqttHost MQTT broker address - * @param mqttPortTcp TCP port of the MQTT broker - * @param mqttPortTls TLS port of the MQTT broker - * @param mqttUsername MQTT username - * @param mqttPassword MQTT password - * @param mqttClientId unique MQTT client ID + * @param mqttUseTls use TLS for a secure connection with the MQTT broker * @param ioT3CoreCallback interface to retrieve the different clients outputs * @param telemetryHost Open Telemetry server address * @param telemetryPort port of the Open Telemetry server + * @param telemetryEndpoint endpoint of the Open Telemetry server URL + * @param telemetryUsername Open Telemetry username + * @param telemetryPassword Open Telemetry password */ - public IoT3Core(String mqttHost, - int mqttPortTcp, - int mqttPortTls, + private IoT3Core(String mqttHost, + int mqttPort, String mqttUsername, String mqttPassword, String mqttClientId, + boolean mqttUseTls, IoT3CoreCallback ioT3CoreCallback, String telemetryHost, int telemetryPort, @@ -95,12 +68,11 @@ public IoT3Core(String mqttHost, // instantiate the MQTT client this.mqttClient = new MqttClient( mqttHost, - mqttPortTcp, - mqttPortTls, + mqttPort, mqttUsername, mqttPassword, mqttClientId, - null, + mqttUseTls, new MqttCallback() { @Override public void connectionLost(Throwable cause) { @@ -230,11 +202,11 @@ public void mqttPublish(String topic, String message, boolean retain) { */ public static class IoT3CoreBuilder { private String mqttHost; - private int mqttPortTcp; - private int mqttPortTls; + private int mqttPort; private String mqttUsername; private String mqttPassword; private String mqttClientId; + private boolean mqttUseTls; private IoT3CoreCallback ioT3CoreCallback; private String telemetryHost; private int telemetryPort; @@ -251,24 +223,24 @@ public IoT3CoreBuilder() {} * Set the MQTT parameters of your IoT3Core instance. * * @param mqttHost the host or IP address of the MQTT broker - * @param mqttPortTcp the TCP port of the MQTT broker - * @param mqttPortTls the TLS port of the MQTT broker + * @param mqttPort the port of the MQTT broker * @param mqttUsername the username for authentication with the MQTT broker * @param mqttPassword the password for authentication with the MQTT broker * @param mqttClientId your client ID + * @param mqttUseTls use TLS for a secure connection with the MQTT broker */ public IoT3CoreBuilder mqttParams(String mqttHost, - int mqttPortTcp, - int mqttPortTls, + int mqttPort, String mqttUsername, String mqttPassword, - String mqttClientId) { + String mqttClientId, + boolean mqttUseTls) { this.mqttHost = mqttHost; - this.mqttPortTcp = mqttPortTcp; - this.mqttPortTls = mqttPortTls; + this.mqttPort = mqttPort; this.mqttUsername = mqttUsername; this.mqttPassword = mqttPassword; this.mqttClientId = mqttClientId; + this.mqttUseTls = mqttUseTls; return this; } @@ -313,11 +285,11 @@ public IoT3CoreBuilder callback(IoT3CoreCallback ioT3CoreCallback) { public IoT3Core build() { return new IoT3Core( mqttHost, - mqttPortTcp, - mqttPortTls, + mqttPort, mqttUsername, mqttPassword, mqttClientId, + mqttUseTls, ioT3CoreCallback, telemetryHost, telemetryPort, diff --git a/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java b/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java index e1f4a20e..e4a356b2 100644 --- a/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java +++ b/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java @@ -7,7 +7,6 @@ */ package com.orange.iot3core.clients; -import com.hivemq.client.mqtt.MqttClientSslConfig; import com.hivemq.client.mqtt.MqttGlobalPublishFilter; import com.hivemq.client.mqtt.datatypes.MqttQos; import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient; @@ -42,12 +41,11 @@ public class MqttClient { private boolean tlsConnection = false; public MqttClient(String serverHost, - int tcpPort, - int tlsPort, + int serverPort, String username, String password, String clientId, - MqttClientSslConfig sslConfig, + boolean useTls, MqttCallback callback, OpenTelemetryClient openTelemetryClient) { this.callback = callback; @@ -57,6 +55,7 @@ public MqttClient(String serverHost, .useMqttVersion5() .identifier(clientId) .serverHost(serverHost) + .serverPort(serverPort) .addDisconnectedListener(context1 -> { LOGGER.log(Level.INFO, "Disconnected from MQTT broker " + serverHost); callback.connectionLost(context1.getCause()); @@ -73,14 +72,10 @@ public MqttClient(String serverHost, .applySimpleAuth(); } - if(sslConfig == null) { - mqttClient = mqttClientBuilder.serverPort(tcpPort) - .buildAsync(); + if(useTls) { + mqttClient = mqttClientBuilder.sslWithDefaultConfig().buildAsync(); } else { - mqttClient = mqttClientBuilder.serverPort(tlsPort) - .sslConfig(sslConfig) - .buildAsync(); - tlsConnection = true; + mqttClient = mqttClientBuilder.buildAsync(); } // single callback for processing messages received on subscribed topics @@ -93,7 +88,7 @@ public void disconnect() { if(mqttClient != null) { mqttClient.disconnect().whenComplete((mqtt5DisconnectResult, throwable) -> { if(throwable != null) { - LOGGER.log(Level.WARNING, "Error during disconnection"); + LOGGER.log(Level.WARNING, "Error during disconnection: " + throwable.getMessage()); } else { LOGGER.log(Level.INFO, "Disconnected"); } @@ -108,7 +103,7 @@ public void connect() { .send() .whenComplete((connAck, throwable) -> { if(throwable != null) { - LOGGER.log(Level.INFO, "Error during connection to the server"); + LOGGER.log(Level.INFO, "Error during connection to the server: " + throwable.getMessage()); } else { LOGGER.log(Level.INFO, "Success connecting to the server"); } @@ -123,7 +118,7 @@ public void subscribeToTopic(String topic) { .send() .whenComplete((subAck, throwable) -> { if (throwable != null) { - LOGGER.log(Level.WARNING, "Subscribed fail!"); + LOGGER.log(Level.WARNING, "Subscription failed: " + throwable.getMessage()); } else { LOGGER.log(Level.FINE, "Subscribed!"); } @@ -142,7 +137,7 @@ public void unsubscribeFromTopic(String topic) { .send() .whenComplete((subAck, throwable) -> { if (throwable != null) { - LOGGER.log(Level.WARNING, "Unsubscribed fail!"); + LOGGER.log(Level.WARNING, "Unsubscription failed: " + throwable.getMessage()); } else { LOGGER.log(Level.INFO, "Unsubscribed!"); } From f2d2fb212c298c28bed91be240472c0dd75cfb6f Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Wed, 23 Oct 2024 16:30:45 +0200 Subject: [PATCH 2/7] java/iot3mobility: add TLS and telemetry options to IoT3Mobility --- .../com/orange/iot3mobility/IoT3Mobility.java | 208 ++++++++++++++---- 1 file changed, 169 insertions(+), 39 deletions(-) diff --git a/java/iot3/mobility/src/main/java/com/orange/iot3mobility/IoT3Mobility.java b/java/iot3/mobility/src/main/java/com/orange/iot3mobility/IoT3Mobility.java index 8eab46e3..3457b6fc 100644 --- a/java/iot3/mobility/src/main/java/com/orange/iot3mobility/IoT3Mobility.java +++ b/java/iot3/mobility/src/main/java/com/orange/iot3mobility/IoT3Mobility.java @@ -49,60 +49,83 @@ public class IoT3Mobility { /** * Instantiate the IoT3.0 Mobility SDK. * - * @param host server address, provided by Orange - * @param username username, provided by Orange - * @param password password, provided by Orange - * @param uuid your unique user ID, usually in the form com_userType_id (e.g. ora_car_123) - * @param context can be a specific project name or can be provided by Orange + * @param uuid unique user identifier + * @param context specific project or client name + * @param mqttHost MQTT broker address + * @param mqttPort port of the MQTT broker + * @param mqttUsername MQTT username + * @param mqttPassword MQTT password + * @param mqttUseTls use TLS for a secure connection with the MQTT broker * @param ioT3MobilityCallback callback to retrieve connection status + * @param telemetryHost Open Telemetry server address + * @param telemetryPort port of the Open Telemetry server + * @param telemetryEndpoint endpoint of the Open Telemetry server URL + * @param telemetryUsername Open Telemetry username + * @param telemetryPassword Open Telemetry password */ - public IoT3Mobility(String host, - String username, - String password, - String uuid, + private IoT3Mobility(String uuid, String context, + String mqttHost, + int mqttPort, + String mqttUsername, + String mqttPassword, + boolean mqttUseTls, IoT3MobilityCallback ioT3MobilityCallback, - String telemetryHost) { + String telemetryHost, + int telemetryPort, + String telemetryEndpoint, + String telemetryUsername, + String telemetryPassword) { this.uuid = uuid; // random stationId at the moment, will be an option to set it later on this.stationId = Utils.randomBetween(999, 99999999); - ioT3Core = new IoT3Core( - host, - username, - password, - uuid, - new IoT3CoreCallback() { - @Override - public void mqttConnectionLost(Throwable throwable) { - ioT3MobilityCallback.connectionLost(throwable); - } - @Override - public void mqttMessageArrived(String topic, String message) { - processMessage(topic, message); - } + IoT3CoreCallback ioT3CoreCallback = new IoT3CoreCallback() { + @Override + public void mqttConnectionLost(Throwable throwable) { + ioT3MobilityCallback.connectionLost(throwable); + } - @Override - public void mqttConnectComplete(boolean reconnect, String serverURI) { - ioT3MobilityCallback.connectComplete(reconnect, serverURI); - } + @Override + public void mqttMessageArrived(String topic, String message) { + processMessage(topic, message); + } - @Override - public void mqttMessagePublished(Throwable publishFailure) { + @Override + public void mqttConnectComplete(boolean reconnect, String serverURI) { + ioT3MobilityCallback.connectComplete(reconnect, serverURI); + } - } + @Override + public void mqttMessagePublished(Throwable publishFailure) { - @Override - public void mqttSubscriptionComplete(Throwable subscribeFailure) { + } - } + @Override + public void mqttSubscriptionComplete(Throwable subscribeFailure) { - @Override - public void mqttUnsubscriptionComplete(Throwable unsubscribeFailure) { + } - } - }, - telemetryHost); + @Override + public void mqttUnsubscriptionComplete(Throwable unsubscribeFailure) { + + } + }; + + ioT3Core = new IoT3Core.IoT3CoreBuilder() + .mqttParams(mqttHost, + mqttPort, + mqttUsername, + mqttPassword, + uuid, + mqttUseTls) + .telemetryParams(telemetryHost, + telemetryPort, + telemetryEndpoint, + telemetryUsername, + telemetryPassword) + .callback(ioT3CoreCallback) + .build(); roIManager = new RoIManager(ioT3Core, uuid, context); @@ -359,4 +382,111 @@ public void sendCpm(CPM cpm) { if(ioT3Core != null) ioT3Core.mqttPublish(topic, cpm.getJson().toString()); } + /** + * Build an instance of IoT3Mobility. + */ + public static class IoT3MobilityBuilder { + private final String uuid; + private final String context; + private String mqttHost; + private int mqttPort; + private String mqttUsername; + private String mqttPassword; + private boolean mqttUseTls; + private IoT3MobilityCallback ioT3MobilityCallback; + private String telemetryHost; + private int telemetryPort; + private String telemetryEndpoint; + private String telemetryUsername; + private String telemetryPassword; + + /** + * Start building an instance of IoT3Mobility. + * + * @param uuid unique user identifier + * @param context specific project or client name + */ + public IoT3MobilityBuilder(String uuid, String context) { + this.uuid = uuid; + this.context = context; + } + + /** + * Set the MQTT parameters of your IoT3Mobility instance. + * + * @param mqttHost the host or IP address of the MQTT broker + * @param mqttPort the port of the MQTT broker + * @param mqttUsername the username for authentication with the MQTT broker + * @param mqttPassword the password for authentication with the MQTT broker + * @param mqttUseTls use TLS for a secure connection with the MQTT broker + */ + public IoT3Mobility.IoT3MobilityBuilder mqttParams(String mqttHost, + int mqttPort, + String mqttUsername, + String mqttPassword, + boolean mqttUseTls) { + this.mqttHost = mqttHost; + this.mqttPort = mqttPort; + this.mqttUsername = mqttUsername; + this.mqttPassword = mqttPassword; + this.mqttUseTls = mqttUseTls; + return this; + } + + /** + * Set the OpenTelemetry parameters of your IoT3Mobility instance. + * + * @param telemetryHost the host or IP address of the OpenTelemetry server + * @param telemetryPort the port of the OpenTelemetry server + * @param telemetryEndpoint the endpoint of the OpenTelemetry server (e.g. /endpoint/example) + * @param telemetryUsername the username for authentication with the OpenTelemetry server + * @param telemetryPassword the password for authentication with the OpenTelemetry server + */ + public IoT3Mobility.IoT3MobilityBuilder telemetryParams(String telemetryHost, + int telemetryPort, + String telemetryEndpoint, + String telemetryUsername, + String telemetryPassword) { + this.telemetryHost = telemetryHost; + this.telemetryPort = telemetryPort; + this.telemetryEndpoint = telemetryEndpoint; + this.telemetryUsername = telemetryUsername; + this.telemetryPassword = telemetryPassword; + return this; + } + + /** + * Set the callback of your IoT3Mobility instance. + * + * @param ioT3MobilityCallback callback to be notified of mainly MQTT-related events, e.g. message reception + * or connection status + */ + public IoT3Mobility.IoT3MobilityBuilder callback(IoT3MobilityCallback ioT3MobilityCallback) { + this.ioT3MobilityCallback = ioT3MobilityCallback; + return this; + } + + /** + * Build the IoT3Mobility instance. + * + * @return Iot3Mobility instance + */ + public IoT3Mobility build() { + return new IoT3Mobility( + uuid, + context, + mqttHost, + mqttPort, + mqttUsername, + mqttPassword, + mqttUseTls, + ioT3MobilityCallback, + telemetryHost, + telemetryPort, + telemetryEndpoint, + telemetryUsername, + telemetryPassword); + } + } + } From 97a350c13ddf1542af73d514c6c6d274cf4edfda Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Wed, 23 Oct 2024 16:31:47 +0200 Subject: [PATCH 3/7] java/iot3examples: add TLS option to Iot3CoreExample --- .../src/main/java/com/orange/Iot3CoreExample.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java b/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java index 620d1fbc..aab1d0a6 100644 --- a/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java +++ b/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java @@ -11,11 +11,11 @@ public class Iot3CoreExample { // MQTT parameters private static final String EXAMPLE_MQTT_HOST = "mqtt_host"; - private static final int EXAMPLE_MQTT_PORT_TCP = 1883; - private static final int EXAMPLE_MQTT_PORT_TLS = 8883; + private static final int EXAMPLE_MQTT_PORT = 1883; private static final String EXAMPLE_MQTT_USERNAME = "mqtt_username"; private static final String EXAMPLE_MQTT_PASSWORD = "mqtt_password"; private static final String EXAMPLE_MQTT_CLIENT_ID = "mqtt_client_id"; + private static final boolean EXAMPLE_MQTT_USE_TLS = true; // OpenTelemetry parameters private static final String EXAMPLE_OTL_HOST = "telemetry_host"; private static final int EXAMPLE_OTL_PORT = 4318; @@ -29,11 +29,11 @@ public static void main(String[] args) { // instantiate IoT3Core and its callback ioT3Core = new IoT3Core.IoT3CoreBuilder() .mqttParams(EXAMPLE_MQTT_HOST, - EXAMPLE_MQTT_PORT_TCP, - EXAMPLE_MQTT_PORT_TLS, + EXAMPLE_MQTT_PORT, EXAMPLE_MQTT_USERNAME, EXAMPLE_MQTT_PASSWORD, - EXAMPLE_MQTT_CLIENT_ID) + EXAMPLE_MQTT_CLIENT_ID, + EXAMPLE_MQTT_USE_TLS) .telemetryParams(EXAMPLE_OTL_HOST, EXAMPLE_OTL_PORT, EXAMPLE_OTL_ENDPOINT, From c7135a55c2988a5bf4eb4285dd69d8e3537dc66e Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Wed, 23 Oct 2024 16:32:10 +0200 Subject: [PATCH 4/7] java/iot3examples: add TLS and telemetry options to Iot3MobilityExample --- .../java/com/orange/Iot3MobilityExample.java | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/java/iot3/examples/src/main/java/com/orange/Iot3MobilityExample.java b/java/iot3/examples/src/main/java/com/orange/Iot3MobilityExample.java index 05f08122..b348725a 100644 --- a/java/iot3/examples/src/main/java/com/orange/Iot3MobilityExample.java +++ b/java/iot3/examples/src/main/java/com/orange/Iot3MobilityExample.java @@ -30,24 +30,37 @@ public class Iot3MobilityExample { - private static final String EXAMPLE_HOST = "host"; - private static final String EXAMPLE_USERNAME = "username"; - private static final String EXAMPLE_PASSWORD = "password"; private static final String EXAMPLE_UUID = "uuid"; private static final String EXAMPLE_CONTEXT = "context"; - private static final String EXAMPLE_TELEMETRY_HOST = "telemetry_host"; + // MQTT parameters + private static final String EXAMPLE_MQTT_HOST = "mqtt_host"; + private static final int EXAMPLE_MQTT_PORT = 1883; + private static final String EXAMPLE_MQTT_USERNAME = "mqtt_username"; + private static final String EXAMPLE_MQTT_PASSWORD = "mqtt_password"; + private static final boolean EXAMPLE_MQTT_USE_TLS = false; + // OpenTelemetry parameters + private static final String EXAMPLE_OTL_HOST = "telemetry_host"; + private static final int EXAMPLE_OTL_PORT = 4318; + private static final String EXAMPLE_OTL_ENDPOINT = "/telemetry/endpoint"; + private static final String EXAMPLE_OTL_USERNAME = "telemetry_username"; + private static final String EXAMPLE_OTL_PASSWORD = "telemetry_password"; private static IoT3Mobility ioT3Mobility; public static void main(String[] args) { // instantiate IoT3Mobility and its callback - ioT3Mobility = new IoT3Mobility( - EXAMPLE_HOST, - EXAMPLE_USERNAME, - EXAMPLE_PASSWORD, - EXAMPLE_UUID, // serves to identify the road user, app or infrastructure - EXAMPLE_CONTEXT, // serves as the root of the MQTT mobility topics - new IoT3MobilityCallback() { + ioT3Mobility = new IoT3Mobility.IoT3MobilityBuilder(EXAMPLE_UUID, EXAMPLE_CONTEXT) + .mqttParams(EXAMPLE_MQTT_HOST, + EXAMPLE_MQTT_PORT, + EXAMPLE_MQTT_USERNAME, + EXAMPLE_MQTT_PASSWORD, + EXAMPLE_MQTT_USE_TLS) + .telemetryParams(EXAMPLE_OTL_HOST, + EXAMPLE_OTL_PORT, + EXAMPLE_OTL_ENDPOINT, + EXAMPLE_OTL_USERNAME, + EXAMPLE_OTL_PASSWORD) + .callback(new IoT3MobilityCallback() { @Override public void connectionLost(Throwable cause) { System.out.println("MQTT Connection lost..."); @@ -57,8 +70,8 @@ public void connectionLost(Throwable cause) { public void connectComplete(boolean reconnect, String serverURI) { System.out.println("MQTT connection complete: " + serverURI); } - }, - EXAMPLE_TELEMETRY_HOST); + }) + .build(); // set the RoadHazardCallback to be informed of road hazards in the corresponding Region of Interest (RoI) ioT3Mobility.setRoadHazardCallback(new IoT3RoadHazardCallback() { @@ -90,13 +103,13 @@ public void newRoadUser(RoadUser roadUser) { // RoadUser is a simple object provided by IoT3Mobility System.out.println("New Road User: " + roadUser.getUuid()); LatLng position = roadUser.getPosition(); - System.out.println("Road User position: " + position.toString()); + System.out.println("Road User position: " + position); // the CAM on which this object is based can still be accessed CAM originalCam = roadUser.getCam(); double latitude = originalCam.getBasicContainer().getPosition().getLatitudeDegree(); double longitude = originalCam.getBasicContainer().getPosition().getLongitudeDegree(); LatLng camPosition = new LatLng(latitude, longitude); - System.out.println("CAM position: " + camPosition.toString()); + System.out.println("CAM position: " + camPosition); } @Override From 229f1817b868a6162bd318fbf86e464782ddac5f Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Thu, 24 Oct 2024 12:04:56 +0200 Subject: [PATCH 5/7] java/iot3core: add method to IoT3Core to check that the connection is secured --- .../core/src/main/java/com/orange/iot3core/IoT3Core.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java b/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java index c3dd3b55..84d54ee2 100644 --- a/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java +++ b/java/iot3/core/src/main/java/com/orange/iot3core/IoT3Core.java @@ -197,6 +197,14 @@ public void mqttPublish(String topic, String message, boolean retain) { if(mqttClient != null) mqttClient.publishMessage(topic, message, retain); } + /** + * Check that the MQTT connection is secured with TLS + */ + public boolean isMqttConnectionSecured() { + if(mqttClient != null) return mqttClient.isConnected() && mqttClient.isConnectionSecured(); + else return false; + } + /** * Build an instance of IoT3Core. */ From 845c1805e6c7f24ab2b0b23510dbc1637e5f1e20 Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Thu, 24 Oct 2024 12:05:42 +0200 Subject: [PATCH 6/7] java/iot3core: fix the isConnectionSecured method of the MqttClient --- .../main/java/com/orange/iot3core/clients/MqttClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java b/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java index e4a356b2..28a237d0 100644 --- a/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java +++ b/java/iot3/core/src/main/java/com/orange/iot3core/clients/MqttClient.java @@ -38,7 +38,7 @@ public class MqttClient { private final MqttCallback callback; private final OpenTelemetryClient openTelemetryClient; - private boolean tlsConnection = false; + private final boolean useTls; public MqttClient(String serverHost, int serverPort, @@ -50,6 +50,7 @@ public MqttClient(String serverHost, OpenTelemetryClient openTelemetryClient) { this.callback = callback; this.openTelemetryClient = openTelemetryClient; + this.useTls = useTls; Mqtt5ClientBuilder mqttClientBuilder = com.hivemq.client.mqtt.MqttClient.builder() .useMqttVersion5() @@ -94,7 +95,6 @@ public void disconnect() { } }); } - tlsConnection = false; } public void connect() { @@ -265,7 +265,7 @@ public boolean isConnected() { } public boolean isConnectionSecured() { - return isConnected() && tlsConnection; + return isConnected() && useTls; } public boolean isValidMqttPubTopic(String topic) { From de073f8b1d7ebf0c63239d67bda3e346d578a820 Mon Sep 17 00:00:00 2001 From: Mathieu Lefebvre Date: Thu, 24 Oct 2024 12:06:10 +0200 Subject: [PATCH 7/7] java/iot3examples: add check for secured connection --- .../examples/src/main/java/com/orange/Iot3CoreExample.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java b/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java index aab1d0a6..69982243 100644 --- a/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java +++ b/java/iot3/examples/src/main/java/com/orange/Iot3CoreExample.java @@ -78,6 +78,9 @@ public void mqttUnsubscriptionComplete(Throwable unsubscribeFailure) { } private static void onConnectionComplete() { + // Check if MQTT connection is secured + if(ioT3Core.isMqttConnectionSecured()) System.out.println("MQTT connection is SECURED"); + else System.out.println("MQTT connection is NOT SECURED"); // subscribe to the root test topic and to all iot3 topics using the wildcard # ioT3Core.mqttSubscribe("test"); ioT3Core.mqttSubscribe("test/iot3/#");