diff --git a/AstarteDeviceSDKCSharp/AstarteDeviceSDKCSharp.csproj b/AstarteDeviceSDKCSharp/AstarteDeviceSDKCSharp.csproj index 47e1e70..9074191 100644 --- a/AstarteDeviceSDKCSharp/AstarteDeviceSDKCSharp.csproj +++ b/AstarteDeviceSDKCSharp/AstarteDeviceSDKCSharp.csproj @@ -34,7 +34,7 @@ SPDX-License-Identifier: Apache-2.0 --> all - + diff --git a/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs b/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs index 51af932..42da34d 100644 --- a/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs +++ b/AstarteDeviceSDKCSharp/Crypto/AstarteCryptoStore.cs @@ -18,7 +18,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -using MQTTnet.Client.Options; +using MQTTnet.Client; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; @@ -55,13 +55,13 @@ private void LoadNewCertificate() } - public void SaveCertificateIfNotExist(X509Certificate2 x509Certificate) + public void SaveCertificateIfNotExist(X509Certificate2 x509Certificate2) { //Save certificate to file try { File.WriteAllText(Path.Combine(_cryptoStoreDir, "device.crt"), - new(PemEncoding.Write("CERTIFICATE", x509Certificate.GetRawCertData()).ToArray())); + new(PemEncoding.Write("CERTIFICATE", x509Certificate2.GetRawCertData()).ToArray())); } catch (DirectoryNotFoundException ex) { diff --git a/AstarteDeviceSDKCSharp/Crypto/AstarteMutualTLSParametersFactory.cs b/AstarteDeviceSDKCSharp/Crypto/AstarteMutualTLSParametersFactory.cs index 7aef1a9..1914cda 100644 --- a/AstarteDeviceSDKCSharp/Crypto/AstarteMutualTLSParametersFactory.cs +++ b/AstarteDeviceSDKCSharp/Crypto/AstarteMutualTLSParametersFactory.cs @@ -18,12 +18,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -using MQTTnet.Client.Options; +using MQTTnet.Client; using System.Security.Cryptography.X509Certificates; namespace AstarteDeviceSDKCSharp.Crypto { - public class AstarteMutualTLSParametersFactory : MqttClientOptionsBuilderTlsParameters + public class AstarteMutualTLSParametersFactory { private readonly MqttClientOptionsBuilderTlsParameters _tlsOptions; @@ -33,14 +33,19 @@ public AstarteMutualTLSParametersFactory(IAstarteCryptoStore cryptoStore) : base _tlsOptions = new MqttClientOptionsBuilderTlsParameters { UseTls = true, - Certificates = new List + Certificates = new List { cryptoStore.GetCertificate() }, IgnoreCertificateChainErrors = cryptoStore.IgnoreSSLErrors, IgnoreCertificateRevocationErrors = cryptoStore.IgnoreSSLErrors, SslProtocol = System.Security.Authentication.SslProtocols.Tls12, - AllowUntrustedCertificates = cryptoStore.IgnoreSSLErrors + AllowUntrustedCertificates = cryptoStore.IgnoreSSLErrors, + CertificateValidationHandler = eventArgs => + { + eventArgs.Certificate = cryptoStore.GetCertificate(); + return true; + } }; } diff --git a/AstarteDeviceSDKCSharp/Crypto/IAstarteCryptoStore.cs b/AstarteDeviceSDKCSharp/Crypto/IAstarteCryptoStore.cs index 0037232..9b8592c 100644 --- a/AstarteDeviceSDKCSharp/Crypto/IAstarteCryptoStore.cs +++ b/AstarteDeviceSDKCSharp/Crypto/IAstarteCryptoStore.cs @@ -18,7 +18,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -using MQTTnet.Client.Options; +using MQTTnet.Client; using System.Security.Cryptography.X509Certificates; namespace AstarteDeviceSDKCSharp.Crypto diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs index 81cbaa4..169fd39 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs @@ -26,12 +26,10 @@ using AstarteDeviceSDKCSharp.Utilities; using MQTTnet; using MQTTnet.Client; -using MQTTnet.Client.Connecting; -using MQTTnet.Client.Disconnecting; -using MQTTnet.Client.Publishing; using MQTTnet.Exceptions; using System.Diagnostics; using System.IO.Compression; +using System.Runtime.CompilerServices; using System.Text; namespace AstarteDeviceSDKCSharp.Transport.MQTT @@ -94,8 +92,17 @@ private async Task CompleteAstarteConnection(bool IsSessionPresent) Trace.WriteLine("Transport Connected"); } - _client.UseApplicationMessageReceivedHandler(OnMessageReceive); - _client.UseDisconnectedHandler(OnDisconnectAsync); + if (_client is not null) + { + _client.ApplicationMessageReceivedAsync += e => + { + OnMessageReceive(e); + return Task.CompletedTask; + }; + + _client.DisconnectedAsync += OnDisconnectAsync; + + } } @@ -114,8 +121,8 @@ public override async Task Connect() _client = await InitClientAsync(); } - var result = await _client.ConnectAsync(_connectionInfo.GetMqttConnectOptions(), - CancellationToken.None); + MqttClientConnectResult result = await _client.ConnectAsync(_connectionInfo.GetMqttConnectOptions(), + CancellationToken.None).ConfigureAwait(false); if (result.ResultCode == MqttClientConnectResultCode.Success) { @@ -192,6 +199,11 @@ private async Task SendEmptyCacheAsync() .WithRetainFlag(false) .Build(); + if (_client is null) + { + return; + } + MqttClientPublishResult result = await _client.PublishAsync(applicationMessage); if (result.ReasonCode != MqttClientPublishReasonCode.Success) { @@ -200,7 +212,7 @@ private async Task SendEmptyCacheAsync() } } - private Task OnDisconnectAsync(MqttClientDisconnectedEventArgs e) + Task OnDisconnectAsync(MqttClientDisconnectedEventArgs e) { if (_astarteTransportEventListener != null) { diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs index 8556dd6..ecb0d37 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs @@ -26,8 +26,8 @@ using AstarteDeviceSDKCSharp.Utilities; using MQTTnet; using MQTTnet.Client; -using MQTTnet.Client.Publishing; using MQTTnet.Exceptions; +using MQTTnet.Protocol; using System.Text; using static AstarteDeviceSDKCSharp.Protocol.AstarteInterfaceDatastreamMapping; @@ -69,7 +69,7 @@ public override async Task SendIndividualValue(AstarteInterface astarteInterface try { - await DoSendMqttMessage(topic, payload, qos); + await DoSendMqttMessage(topic, payload, (MqttQualityOfServiceLevel)qos); } catch (MqttCommunicationException ex) { @@ -85,7 +85,7 @@ public override async Task SendIndividualValue(AstarteInterface astarteInterface } - private async Task DoSendMqttMessage(string topic, byte[] payload, int qos) + private async Task DoSendMqttMessage(string topic, byte[] payload, MqttQualityOfServiceLevel qos) { var applicationMessage = new MqttApplicationMessageBuilder() .WithTopic(topic) @@ -96,13 +96,16 @@ private async Task DoSendMqttMessage(string topic, byte[] payload, int qos) try { + if (_client is not null) + { + MqttClientPublishResult result = await _client.PublishAsync(applicationMessage); - MqttClientPublishResult result = await _client.PublishAsync(applicationMessage); + if (result.ReasonCode != MqttClientPublishReasonCode.Success) + { + throw new AstarteTransportException + ($"Error publishing on MQTT. Code: {result.ReasonCode}"); + } - if (result.ReasonCode != MqttClientPublishReasonCode.Success) - { - throw new AstarteTransportException - ($"Error publishing on MQTT. Code: {result.ReasonCode}"); } } @@ -140,7 +143,7 @@ public override async Task SendIntrospection() .Remove(introspectionStringBuilder.Length - 1, 1); string introspection = introspectionStringBuilder.ToString(); - await DoSendMqttMessage(_baseTopic, Encoding.ASCII.GetBytes(introspection), 2); + await DoSendMqttMessage(_baseTopic, Encoding.ASCII.GetBytes(introspection), (MqttQualityOfServiceLevel)2); } public override async Task SendIndividualValue(AstarteInterface astarteInterface, @@ -167,7 +170,7 @@ public override async Task SendAggregate(AstarteAggregateDatastreamInterface ast try { - await DoSendMqttMessage(topic, payload, qos); + await DoSendMqttMessage(topic, payload, (MqttQualityOfServiceLevel)qos); } catch (MqttCommunicationException e) { @@ -251,7 +254,7 @@ public override void RetryFailedMessages() private async Task DoSendMessage(IAstarteFailedMessage failedMessage) { await DoSendMqttMessage(failedMessage.GetTopic(), failedMessage.GetPayload(), - failedMessage.GetQos()); + (MqttQualityOfServiceLevel)failedMessage.GetQos()); } private int QosFromReliability(AstarteInterfaceDatastreamMapping mapping) @@ -322,7 +325,7 @@ private async void DoSendMqttMessage(IAstarteFailedMessage failedMessage) { await DoSendMqttMessage(failedMessage.GetTopic(), failedMessage.GetPayload(), - failedMessage.GetQos()); + (MqttQualityOfServiceLevel)failedMessage.GetQos()); } private void HandleDatastreamFailedPublish(MqttCommunicationException e, diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/IMqttConnectionInfo.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/IMqttConnectionInfo.cs index 52bc0cf..57bc3a5 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/IMqttConnectionInfo.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/IMqttConnectionInfo.cs @@ -18,7 +18,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -using MQTTnet.Client.Options; +using MQTTnet.Client; namespace AstarteDeviceSDKCSharp.Transport.MQTT @@ -29,6 +29,6 @@ public interface IMqttConnectionInfo string GetClientId(); - IMqttClientOptions GetMqttConnectOptions(); + MqttClientOptions GetMqttConnectOptions(); } } diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/MutualSSLAuthenticationMqttConnectionInfo.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/MutualSSLAuthenticationMqttConnectionInfo.cs index e224bfe..61f6b08 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/MutualSSLAuthenticationMqttConnectionInfo.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/MutualSSLAuthenticationMqttConnectionInfo.cs @@ -18,7 +18,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -using MQTTnet.Client.Options; +using MQTTnet.Client; namespace AstarteDeviceSDKCSharp.Transport.MQTT { @@ -26,7 +26,7 @@ public class MutualSSLAuthenticationMqttConnectionInfo : IMqttConnectionInfo { private readonly Uri _brokerUrl; - private readonly IMqttClientOptions _mqttConnectOptions; + private readonly MqttClientOptions _mqttConnectOptions; private readonly string _clientId = string.Empty; public MutualSSLAuthenticationMqttConnectionInfo(Uri brokerUrl, string astarteRealm, @@ -38,7 +38,6 @@ public MutualSSLAuthenticationMqttConnectionInfo(Uri brokerUrl, string astarteRe .WithTcpServer(_brokerUrl.Host, _brokerUrl.Port) .WithTls(tlsOptions) .WithCleanSession(false) - .WithCommunicationTimeout(TimeSpan.FromSeconds(60)) .WithKeepAlivePeriod(TimeSpan.FromSeconds(60)) .WithSessionExpiryInterval(0) .Build(); @@ -50,6 +49,6 @@ public MutualSSLAuthenticationMqttConnectionInfo(Uri brokerUrl, string astarteRe public string GetClientId() => _clientId; - public IMqttClientOptions GetMqttConnectOptions() => _mqttConnectOptions; + public MqttClientOptions GetMqttConnectOptions() => _mqttConnectOptions; } }