From ff5aa092947ce84b0219322d7d1141c710aad94b Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Wed, 31 Mar 2021 17:27:40 +0100 Subject: [PATCH] Fix more build warnings. (#111) --- M2Mqtt/IMqttNetworkChannel.cs | 2 +- M2Mqtt/Messages/MqttMsgBase.cs | 2 +- M2Mqtt/Messages/MqttMsgConnack.cs | 2 +- M2Mqtt/Messages/MqttMsgConnect.cs | 2 +- M2Mqtt/Messages/MqttMsgConnectEventArgs.cs | 1 + M2Mqtt/Messages/MqttMsgDisconnect.cs | 2 +- M2Mqtt/Messages/MqttMsgPingReq.cs | 2 +- M2Mqtt/Messages/MqttMsgPingResp.cs | 2 +- M2Mqtt/Messages/MqttMsgPuback.cs | 4 +- M2Mqtt/Messages/MqttMsgPubcomp.cs | 4 +- M2Mqtt/Messages/MqttMsgPublish.cs | 4 +- M2Mqtt/Messages/MqttMsgPublishEventArgs.cs | 1 + M2Mqtt/Messages/MqttMsgPublishedEventArgs.cs | 1 + M2Mqtt/Messages/MqttMsgPubrec.cs | 4 +- M2Mqtt/Messages/MqttMsgPubrel.cs | 4 +- M2Mqtt/Messages/MqttMsgSuback.cs | 4 +- M2Mqtt/Messages/MqttMsgSubscribe.cs | 9 +- M2Mqtt/Messages/MqttMsgSubscribeEventArgs.cs | 3 +- M2Mqtt/Messages/MqttMsgSubscribedEventArgs.cs | 3 +- M2Mqtt/Messages/MqttMsgUnsuback.cs | 4 +- M2Mqtt/Messages/MqttMsgUnsubscribe.cs | 9 +- .../Messages/MqttMsgUnsubscribeEventArgs.cs | 1 + .../Messages/MqttMsgUnsubscribedEventArgs.cs | 1 + M2Mqtt/MqttClient.cs | 94 +++++++----- M2Mqtt/MqttSecurity.cs | 4 +- M2Mqtt/MqttSettings.cs | 2 +- M2Mqtt/Nanoframework/Environment.cs | 2 +- M2Mqtt/Net/Fx.cs | 2 +- M2Mqtt/Net/MqttNetworkChannel.cs | 138 +++++++++--------- M2Mqtt/Properties/AssemblyInfo.cs | 8 +- M2Mqtt/Session/MqttSession.cs | 4 +- M2Mqtt/Utility/Trace.cs | 21 ++- M2Mqtt/WinRT/MqttNetworkChannel.cs | 2 +- M2Mqtt/nanoFramework.M2Mqtt.nfproj | 2 +- TestMqtt/Mosquito.TemperatureGauge/Program.cs | 19 +-- TestMqtt/Program.cs | 23 +-- 36 files changed, 223 insertions(+), 169 deletions(-) diff --git a/M2Mqtt/IMqttNetworkChannel.cs b/M2Mqtt/IMqttNetworkChannel.cs index 862f44b9..a9f3cf4d 100644 --- a/M2Mqtt/IMqttNetworkChannel.cs +++ b/M2Mqtt/IMqttNetworkChannel.cs @@ -1,5 +1,4 @@ /* -Copyright (c) 2019 The nanoFramework project contributors Copyright (c) 2013, 2014 Paolo Patierno All rights reserved. This program and the accompanying materials @@ -13,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ using System; diff --git a/M2Mqtt/Messages/MqttMsgBase.cs b/M2Mqtt/Messages/MqttMsgBase.cs index 27c7c3e2..334e9092 100644 --- a/M2Mqtt/Messages/MqttMsgBase.cs +++ b/M2Mqtt/Messages/MqttMsgBase.cs @@ -193,7 +193,7 @@ protected int encodeRemainingLength(int remainingLength, byte[] buffer, int inde digit = remainingLength % 128; remainingLength /= 128; if (remainingLength > 0) - digit = digit | 0x80; + digit |= 0x80; buffer[index++] = (byte)digit; } while (remainingLength > 0); return index; diff --git a/M2Mqtt/Messages/MqttMsgConnack.cs b/M2Mqtt/Messages/MqttMsgConnack.cs index 270087d1..0bcb8cf6 100644 --- a/M2Mqtt/Messages/MqttMsgConnack.cs +++ b/M2Mqtt/Messages/MqttMsgConnack.cs @@ -171,7 +171,7 @@ public override byte[] GetBytes(byte ProtocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgConnect.cs b/M2Mqtt/Messages/MqttMsgConnect.cs index 033b8cb1..d35b15b3 100644 --- a/M2Mqtt/Messages/MqttMsgConnect.cs +++ b/M2Mqtt/Messages/MqttMsgConnect.cs @@ -482,7 +482,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgConnectEventArgs.cs b/M2Mqtt/Messages/MqttMsgConnectEventArgs.cs index 759c547d..2c5104ed 100644 --- a/M2Mqtt/Messages/MqttMsgConnectEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgConnectEventArgs.cs @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) diff --git a/M2Mqtt/Messages/MqttMsgDisconnect.cs b/M2Mqtt/Messages/MqttMsgDisconnect.cs index ac27bc31..c5f72bb6 100644 --- a/M2Mqtt/Messages/MqttMsgDisconnect.cs +++ b/M2Mqtt/Messages/MqttMsgDisconnect.cs @@ -59,7 +59,7 @@ public static MqttMsgDisconnect Parse(byte fixedHeaderFirstByte, byte protocolVe /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { diff --git a/M2Mqtt/Messages/MqttMsgPingReq.cs b/M2Mqtt/Messages/MqttMsgPingReq.cs index b275f880..97310312 100644 --- a/M2Mqtt/Messages/MqttMsgPingReq.cs +++ b/M2Mqtt/Messages/MqttMsgPingReq.cs @@ -34,7 +34,7 @@ public MqttMsgPingReq() /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { diff --git a/M2Mqtt/Messages/MqttMsgPingResp.cs b/M2Mqtt/Messages/MqttMsgPingResp.cs index 93824123..c74bf1a0 100644 --- a/M2Mqtt/Messages/MqttMsgPingResp.cs +++ b/M2Mqtt/Messages/MqttMsgPingResp.cs @@ -60,7 +60,7 @@ public static MqttMsgPingResp Parse(byte fixedHeaderFirstByte, byte protocolVers /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { diff --git a/M2Mqtt/Messages/MqttMsgPuback.cs b/M2Mqtt/Messages/MqttMsgPuback.cs index 23772041..35c02a0a 100644 --- a/M2Mqtt/Messages/MqttMsgPuback.cs +++ b/M2Mqtt/Messages/MqttMsgPuback.cs @@ -34,7 +34,7 @@ public MqttMsgPuback() /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -59,7 +59,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgPubcomp.cs b/M2Mqtt/Messages/MqttMsgPubcomp.cs index 16a46fb4..d6736666 100644 --- a/M2Mqtt/Messages/MqttMsgPubcomp.cs +++ b/M2Mqtt/Messages/MqttMsgPubcomp.cs @@ -34,7 +34,7 @@ public MqttMsgPubcomp() /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -59,7 +59,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgPublish.cs b/M2Mqtt/Messages/MqttMsgPublish.cs index baa65a0d..519d2d77 100644 --- a/M2Mqtt/Messages/MqttMsgPublish.cs +++ b/M2Mqtt/Messages/MqttMsgPublish.cs @@ -97,7 +97,7 @@ public MqttMsgPublish(string topic, /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -148,7 +148,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgPublishEventArgs.cs b/M2Mqtt/Messages/MqttMsgPublishEventArgs.cs index efa36209..5a19143f 100644 --- a/M2Mqtt/Messages/MqttMsgPublishEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgPublishEventArgs.cs @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) diff --git a/M2Mqtt/Messages/MqttMsgPublishedEventArgs.cs b/M2Mqtt/Messages/MqttMsgPublishedEventArgs.cs index c10cfe31..360ff7fb 100644 --- a/M2Mqtt/Messages/MqttMsgPublishedEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgPublishedEventArgs.cs @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) diff --git a/M2Mqtt/Messages/MqttMsgPubrec.cs b/M2Mqtt/Messages/MqttMsgPubrec.cs index cf402a5a..7f516c1e 100644 --- a/M2Mqtt/Messages/MqttMsgPubrec.cs +++ b/M2Mqtt/Messages/MqttMsgPubrec.cs @@ -34,7 +34,7 @@ public MqttMsgPubrec() /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -59,7 +59,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgPubrel.cs b/M2Mqtt/Messages/MqttMsgPubrel.cs index ebe0d84c..4fd8bdc2 100644 --- a/M2Mqtt/Messages/MqttMsgPubrel.cs +++ b/M2Mqtt/Messages/MqttMsgPubrel.cs @@ -36,7 +36,7 @@ public MqttMsgPubrel() /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -61,7 +61,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgSuback.cs b/M2Mqtt/Messages/MqttMsgSuback.cs index 464d07da..26a7b04b 100644 --- a/M2Mqtt/Messages/MqttMsgSuback.cs +++ b/M2Mqtt/Messages/MqttMsgSuback.cs @@ -93,7 +93,7 @@ public static MqttMsgSuback Parse(byte fixedHeaderFirstByte, byte protocolVersio /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -124,7 +124,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgSubscribe.cs b/M2Mqtt/Messages/MqttMsgSubscribe.cs index 89c84bea..69957bbc 100644 --- a/M2Mqtt/Messages/MqttMsgSubscribe.cs +++ b/M2Mqtt/Messages/MqttMsgSubscribe.cs @@ -12,11 +12,12 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ using System; // if NOT .Net Micro Framework -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !NANOFRAMEWORK_1_0) +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !NANOFRAMEWORK_1_0) using System.Collections.Generic; #endif using System.Collections; @@ -130,7 +131,7 @@ public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, byte protocolVer // NOTE : before, I don't know how many topics will be in the payload (so use List) // if .Net Micro Framework -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0) IList tmpTopics = new ArrayList(); IList tmpQosLevels = new ArrayList(); // else other frameworks (.Net, .Net Compact, Mono, Windows Phone) @@ -168,7 +169,7 @@ public static MqttMsgSubscribe Parse(byte fixedHeaderFirstByte, byte protocolVer /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -220,7 +221,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgSubscribeEventArgs.cs b/M2Mqtt/Messages/MqttMsgSubscribeEventArgs.cs index 29578615..7a1171e2 100644 --- a/M2Mqtt/Messages/MqttMsgSubscribeEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgSubscribeEventArgs.cs @@ -12,9 +12,10 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || MF_FRAMEWORK_VERSION_V4_4) using Microsoft.SPOT; #else using System; diff --git a/M2Mqtt/Messages/MqttMsgSubscribedEventArgs.cs b/M2Mqtt/Messages/MqttMsgSubscribedEventArgs.cs index 522e863d..9aaa3624 100644 --- a/M2Mqtt/Messages/MqttMsgSubscribedEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgSubscribedEventArgs.cs @@ -12,9 +12,10 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || MF_FRAMEWORK_VERSION_V4_4) using Microsoft.SPOT; #else using System; diff --git a/M2Mqtt/Messages/MqttMsgUnsuback.cs b/M2Mqtt/Messages/MqttMsgUnsuback.cs index 216346fe..e6563ff4 100644 --- a/M2Mqtt/Messages/MqttMsgUnsuback.cs +++ b/M2Mqtt/Messages/MqttMsgUnsuback.cs @@ -69,7 +69,7 @@ public static MqttMsgUnsuback Parse(byte fixedHeaderFirstByte, byte protocolVers /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -94,7 +94,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgUnsubscribe.cs b/M2Mqtt/Messages/MqttMsgUnsubscribe.cs index 98a8a4f1..598703ae 100644 --- a/M2Mqtt/Messages/MqttMsgUnsubscribe.cs +++ b/M2Mqtt/Messages/MqttMsgUnsubscribe.cs @@ -12,11 +12,12 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ using System; // if NOT .Net Micro Framework -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !NANOFRAMEWORK_1_0) +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !NANOFRAMEWORK_1_0) using System.Collections.Generic; #endif using System.Collections; @@ -117,7 +118,7 @@ public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolV // NOTE : before, I don't know how many topics will be in the payload (so use List) // if .Net Micro Framework -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0) IList tmpTopics = new ArrayList(); // else other frameworks (.Net, .Net Compact, Mono, Windows Phone) #else @@ -147,7 +148,7 @@ public static MqttMsgUnsubscribe Parse(byte fixedHeaderFirstByte, byte protocolV /// /// Returns the bytes that represents the current object. /// - /// MQTT protocol version + /// MQTT protocol version /// An array of bytes that represents the current object. public override byte[] GetBytes(byte protocolVersion) { @@ -190,7 +191,7 @@ public override byte[] GetBytes(byte protocolVersion) do { fixedHeaderSize++; - temp = temp / 128; + temp /= 128; } while (temp > 0); // allocate buffer for message diff --git a/M2Mqtt/Messages/MqttMsgUnsubscribeEventArgs.cs b/M2Mqtt/Messages/MqttMsgUnsubscribeEventArgs.cs index a1dc3852..5c693702 100644 --- a/M2Mqtt/Messages/MqttMsgUnsubscribeEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgUnsubscribeEventArgs.cs @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) diff --git a/M2Mqtt/Messages/MqttMsgUnsubscribedEventArgs.cs b/M2Mqtt/Messages/MqttMsgUnsubscribedEventArgs.cs index d2bc26ec..7dc011a2 100644 --- a/M2Mqtt/Messages/MqttMsgUnsubscribedEventArgs.cs +++ b/M2Mqtt/Messages/MqttMsgUnsubscribedEventArgs.cs @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) diff --git a/M2Mqtt/MqttClient.cs b/M2Mqtt/MqttClient.cs index 9f71a21a..168155e9 100644 --- a/M2Mqtt/MqttClient.cs +++ b/M2Mqtt/MqttClient.cs @@ -1,5 +1,5 @@ /* -Copyright (c) 2019 The nanoFramework project contributors (c) 2013, 2014 Paolo Patierno +Copyright (c) 2013, 2014 Paolo Patierno All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 @@ -12,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ using System; @@ -288,7 +289,7 @@ public MqttClient(IPAddress brokerIpAddress) : [Obsolete("Use this ctor MqttClient(string brokerHostName, int brokerPort, bool secure, X509Certificate caCert) instead")] public MqttClient(IPAddress brokerIpAddress, int brokerPort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol) { -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) this.Init(brokerIpAddress.ToString(), brokerPort, secure, caCert, clientCert, sslProtocol, null, null); #else this.Init(brokerIpAddress.ToString(), brokerPort, secure, caCert, clientCert, sslProtocol); @@ -325,7 +326,7 @@ public MqttClient(string brokerHostName, int brokerPort, bool secure, X509Certif public MqttClient(string brokerHostName, int brokerPort, bool secure, MqttSslProtocols sslProtocol) #endif { -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0 ) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0 ) this.Init(brokerHostName, brokerPort, secure, caCert, clientCert, sslProtocol, null, null); #elif (WINDOWS_APP || WINDOWS_PHONE_APP) this.Init(brokerHostName, brokerPort, secure, sslProtocol); @@ -335,7 +336,7 @@ public MqttClient(string brokerHostName, int brokerPort, bool secure, MqttSslPro } -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) /// /// Constructor @@ -433,7 +434,7 @@ public MqttClient(IMqttNetworkChannel channel) /// CA certificate for secure connection /// Client certificate /// SSL/TLS protocol version -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) /// A RemoteCertificateValidationCallback delegate responsible for validating the certificate supplied by the remote party /// A LocalCertificateSelectionCallback delegate responsible for selecting the certificate used for authentication private void Init(string brokerHostName, int brokerPort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol, @@ -480,7 +481,7 @@ private void Init(string brokerHostName, int brokerPort, bool secure, X509Certif this.session = null; // create network channel -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) this.channel = new MqttNetworkChannel(this.brokerHostName, this.brokerPort, secure, caCert, clientCert, sslProtocol, userCertificateValidationCallback, userCertificateSelectionCallback); #elif (WINDOWS_APP || WINDOWS_PHONE_APP) this.channel = new MqttNetworkChannel(this.brokerHostName, this.brokerPort, secure, sslProtocol); @@ -822,8 +823,10 @@ public void Unsuback(ushort messageId) public ushort Subscribe(string[] topics, byte[] qosLevels) { MqttMsgSubscribe subscribe = - new MqttMsgSubscribe(topics, qosLevels); - subscribe.MessageId = this.GetMessageId(); + new MqttMsgSubscribe(topics, qosLevels) + { + MessageId = this.GetMessageId() + }; // enqueue subscribe request into the inflight queue this.EnqueueInflight(subscribe, MqttMsgFlow.ToPublish); @@ -839,8 +842,10 @@ public ushort Subscribe(string[] topics, byte[] qosLevels) public ushort Unsubscribe(string[] topics) { MqttMsgUnsubscribe unsubscribe = - new MqttMsgUnsubscribe(topics); - unsubscribe.MessageId = this.GetMessageId(); + new MqttMsgUnsubscribe(topics) + { + MessageId = this.GetMessageId() + }; // enqueue unsubscribe request into the inflight queue this.EnqueueInflight(unsubscribe, MqttMsgFlow.ToPublish); @@ -870,8 +875,10 @@ public ushort Publish(string topic, byte[] message) public ushort Publish(string topic, byte[] message, byte qosLevel, bool retain) { MqttMsgPublish publish = - new MqttMsgPublish(topic, message, false, qosLevel, retain); - publish.MessageId = this.GetMessageId(); + new MqttMsgPublish(topic, message, false, qosLevel, retain) + { + MessageId = this.GetMessageId() + }; // enqueue message to publish into the inflight queue bool enqueue = this.EnqueueInflight(publish, MqttMsgFlow.ToPublish); @@ -1096,7 +1103,7 @@ private MqttMsgBase SendReceive(byte[] msgBytes, int timeout) } catch (Exception e) { -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || WINDOWS_APP || WINDOWS_PHONE_APP || NANOFRAMEWORK_1_0) if (typeof(SocketException) == e.GetType()) { // connection reset by broker @@ -1111,7 +1118,7 @@ private MqttMsgBase SendReceive(byte[] msgBytes, int timeout) throw new MqttCommunicationException(e); } -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 ||MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) // wait for answer from broker if (this.syncEndReceiving.WaitOne(timeout, false)) #else @@ -1305,8 +1312,10 @@ private void EnqueueInternal(MqttMsgBase msg) // we need to re-send PUBCOMP only if (msgCtx == null) { - MqttMsgPubcomp pubcomp = new MqttMsgPubcomp(); - pubcomp.MessageId = msg.MessageId; + MqttMsgPubcomp pubcomp = new MqttMsgPubcomp + { + MessageId = msg.MessageId + }; this.Send(pubcomp); @@ -1675,7 +1684,7 @@ private void KeepAliveThread() while (this.isRunning) { -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) // waiting... this.keepAliveEvent.WaitOne(wait, false); #else @@ -1896,7 +1905,7 @@ private void ProcessInflightThread() { while (this.isRunning) { -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) // wait on message queueud to inflight this.inflightWaitHandle.WaitOne(timeout, false); #else @@ -1939,7 +1948,7 @@ private void ProcessInflightThread() msgContext = (MqttMsgContext)this.inflightQueue.Dequeue(); // get inflight message - msgInflight = (MqttMsgBase)msgContext.Message; + msgInflight = msgContext.Message; switch (msgContext.State) { @@ -2000,8 +2009,10 @@ private void ProcessInflightThread() // QoS 1, PUBLISH message received from broker to acknowledge, send PUBACK else if (msgContext.Flow == MqttMsgFlow.ToAcknowledge) { - MqttMsgPuback puback = new MqttMsgPuback(); - puback.MessageId = msgInflight.MessageId; + MqttMsgPuback puback = new MqttMsgPuback + { + MessageId = msgInflight.MessageId + }; this.Send(puback); @@ -2038,8 +2049,10 @@ private void ProcessInflightThread() // QoS 2, PUBLISH message received from broker to acknowledge, send PUBREC, state change to wait PUBREL else if (msgContext.Flow == MqttMsgFlow.ToAcknowledge) { - MqttMsgPubrec pubrec = new MqttMsgPubrec(); - pubrec.MessageId = msgInflight.MessageId; + MqttMsgPubrec pubrec = new MqttMsgPubrec + { + MessageId = msgInflight.MessageId + }; msgContext.State = MqttMsgState.WaitForPubrel; @@ -2097,7 +2110,7 @@ private void ProcessInflightThread() // PUBACK received for PUBLISH message with QoS Level 1, remove from session state if ((msgInflight.Type == MqttMsgBase.MQTT_MSG_PUBLISH_TYPE) && (this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0 ) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2139,7 +2152,7 @@ private void ProcessInflightThread() { // PUBACK not received in time, PUBLISH retries failed, need to remove from session inflight messages too if ((this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2199,8 +2212,10 @@ private void ProcessInflightThread() #endif } - MqttMsgPubrel pubrel = new MqttMsgPubrel(); - pubrel.MessageId = msgInflight.MessageId; + MqttMsgPubrel pubrel = new MqttMsgPubrel + { + MessageId = msgInflight.MessageId + }; msgContext.State = MqttMsgState.WaitForPubcomp; msgContext.Timestamp = Environment.TickCount; @@ -2238,7 +2253,7 @@ private void ProcessInflightThread() { // PUBREC not received in time, PUBLISH retries failed, need to remove from session inflight messages too if ((this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2293,8 +2308,10 @@ private void ProcessInflightThread() #endif } - MqttMsgPubcomp pubcomp = new MqttMsgPubcomp(); - pubcomp.MessageId = msgInflight.MessageId; + MqttMsgPubcomp pubcomp = new MqttMsgPubcomp + { + MessageId = msgInflight.MessageId + }; this.Send(pubcomp); @@ -2305,7 +2322,7 @@ private void ProcessInflightThread() // PUBREL received (and PUBCOMP sent) for PUBLISH message with QoS Level 2, remove from session state if ((msgInflight.Type == MqttMsgBase.MQTT_MSG_PUBLISH_TYPE) && (this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2368,7 +2385,7 @@ private void ProcessInflightThread() // PUBCOMP received for PUBLISH message with QoS Level 2, remove from session state if ((msgInflight.Type == MqttMsgBase.MQTT_MSG_PUBLISH_TYPE) && (this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2427,7 +2444,7 @@ private void ProcessInflightThread() { // PUBCOMP not received, PUBREL retries failed, need to remove from session inflight messages too if ((this.session != null) && -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) (this.session.InflightMessages.Contains(msgContext.Key))) #else (this.session.InflightMessages.ContainsKey(msgContext.Key))) @@ -2465,8 +2482,10 @@ private void ProcessInflightThread() // QoS 2, PUBREL message to send to broker, state change to wait PUBCOMP if (msgContext.Flow == MqttMsgFlow.ToPublish) { - MqttMsgPubrel pubrel = new MqttMsgPubrel(); - pubrel.MessageId = msgInflight.MessageId; + MqttMsgPubrel pubrel = new MqttMsgPubrel + { + MessageId = msgInflight.MessageId + }; msgContext.State = MqttMsgState.WaitForPubcomp; msgContext.Timestamp = Environment.TickCount; @@ -2516,8 +2535,13 @@ private void ProcessInflightThread() } } } +#if TRACE + catch (MqttCommunicationException e) + { +#else catch (MqttCommunicationException) { +#endif // possible exception on Send, I need to re-enqueue not sent message if (msgContext != null) // re-enqueue message diff --git a/M2Mqtt/MqttSecurity.cs b/M2Mqtt/MqttSecurity.cs index c85b45bd..e3c3d341 100644 --- a/M2Mqtt/MqttSecurity.cs +++ b/M2Mqtt/MqttSecurity.cs @@ -1,5 +1,4 @@ /* -Copyright (c) 2019 The nanoFramework project contributors Copyright (c) 2013, 2014 Paolo Patierno All rights reserved. This program and the accompanying materials @@ -13,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ namespace uPLibrary.Networking.M2Mqtt @@ -43,7 +43,7 @@ public enum MqttSslProtocols /// TLSv1_2, /// - /// TLS version 1.3 (currently unsupported) + /// TLS version 1.3 /// TLSv1_3 } diff --git a/M2Mqtt/MqttSettings.cs b/M2Mqtt/MqttSettings.cs index 816451d8..e0672f22 100644 --- a/M2Mqtt/MqttSettings.cs +++ b/M2Mqtt/MqttSettings.cs @@ -1,5 +1,4 @@ /* -Copyright (c) 2019 The nanoFramework project contributors Copyright (c) 2013, 2014 Paolo Patierno All rights reserved. This program and the accompanying materials @@ -13,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ namespace uPLibrary.Networking.M2Mqtt diff --git a/M2Mqtt/Nanoframework/Environment.cs b/M2Mqtt/Nanoframework/Environment.cs index 398cf502..79619a56 100644 --- a/M2Mqtt/Nanoframework/Environment.cs +++ b/M2Mqtt/Nanoframework/Environment.cs @@ -2,7 +2,7 @@ namespace System { - class Environment + static class Environment { public static int TickCount { diff --git a/M2Mqtt/Net/Fx.cs b/M2Mqtt/Net/Fx.cs index f3363b12..4aa85f1d 100644 --- a/M2Mqtt/Net/Fx.cs +++ b/M2Mqtt/Net/Fx.cs @@ -21,7 +21,7 @@ namespace uPLibrary.Networking.M2Mqtt /// /// Support methods fos specific framework /// - public class Fx + public static class Fx { /// /// Starts the thead diff --git a/M2Mqtt/Net/MqttNetworkChannel.cs b/M2Mqtt/Net/MqttNetworkChannel.cs index 0700004d..fd3363fa 100644 --- a/M2Mqtt/Net/MqttNetworkChannel.cs +++ b/M2Mqtt/Net/MqttNetworkChannel.cs @@ -12,10 +12,11 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ #if SSL -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) using Microsoft.SPOT.Net.Security; #else using System.Net.Security; @@ -33,29 +34,31 @@ namespace uPLibrary.Networking.M2Mqtt /// public class MqttNetworkChannel : IMqttNetworkChannel { -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) private readonly RemoteCertificateValidationCallback userCertificateValidationCallback; private readonly LocalCertificateSelectionCallback userCertificateSelectionCallback; #endif // remote host information - private string remoteHostName; - private IPAddress remoteIpAddress; - private int remotePort; + private readonly string remoteHostName; + private readonly IPAddress remoteIpAddress; + private readonly int remotePort; // socket for communication private Socket socket; // using SSL - private bool secure; + private readonly bool secure; // CA certificate (on client) - private X509Certificate caCert; + private readonly X509Certificate caCert; +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0) // Server certificate (on broker) - private X509Certificate serverCert; + private readonly X509Certificate serverCert; +#endif // client certificate (on client) - private X509Certificate clientCert; + private readonly X509Certificate clientCert; // SSL/TLS protocol version - private MqttSslProtocols sslProtocol; + private readonly MqttSslProtocols sslProtocol; /// /// Remote host name @@ -75,7 +78,7 @@ public class MqttNetworkChannel : IMqttNetworkChannel #if SSL // SSL stream private SslStream sslStream; -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !NANOFRAMEWORK_1_0) +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !NANOFRAMEWORK_1_0) private NetworkStream netStream; #endif #endif @@ -88,7 +91,7 @@ public bool DataAvailable get { #if SSL -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_1_0 ) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0 ) if (secure) return this.sslStream.DataAvailable; else @@ -110,7 +113,7 @@ public bool DataAvailable /// /// Socket opened with the client public MqttNetworkChannel(Socket socket) -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) : this(socket, false, null, MqttSslProtocols.None, null, null) #else : this(socket, false, null, MqttSslProtocols.None) @@ -126,7 +129,7 @@ public MqttNetworkChannel(Socket socket) /// Secure connection (SSL/TLS) /// Server X509 certificate for secure connection /// SSL/TLS protocol version -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) /// A RemoteCertificateValidationCallback delegate responsible for validating the certificate supplied by the remote party /// A LocalCertificateSelectionCallback delegate responsible for selecting the certificate used for authentication public MqttNetworkChannel(Socket socket, bool secure, X509Certificate serverCert, MqttSslProtocols sslProtocol, @@ -138,9 +141,9 @@ public MqttNetworkChannel(Socket socket, bool secure, X509Certificate serverCert { this.socket = socket; this.secure = secure; - this.serverCert = serverCert; this.sslProtocol = sslProtocol; -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) + this.serverCert = serverCert; this.userCertificateValidationCallback = userCertificateValidationCallback; this.userCertificateSelectionCallback = userCertificateSelectionCallback; #endif @@ -152,7 +155,7 @@ public MqttNetworkChannel(Socket socket, bool secure, X509Certificate serverCert /// Remote Host name /// Remote port public MqttNetworkChannel(string remoteHostName, int remotePort) -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) : this(remoteHostName, remotePort, false, null, null, MqttSslProtocols.None, null, null) #else : this(remoteHostName, remotePort, false, null, null, MqttSslProtocols.None) @@ -169,7 +172,7 @@ public MqttNetworkChannel(string remoteHostName, int remotePort) /// CA certificate /// Client certificate /// SSL/TLS protocol version -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) /// A RemoteCertificateValidationCallback delegate responsible for validating the certificate supplied by the remote party /// A LocalCertificateSelectionCallback delegate responsible for selecting the certificate used for authentication public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol, @@ -182,7 +185,7 @@ public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X5 IPAddress hostIpAddress = null; -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) try { @@ -233,7 +236,7 @@ public MqttNetworkChannel(string remoteHostName, int remotePort, bool secure, X5 this.caCert = caCert; this.clientCert = clientCert; this.sslProtocol = sslProtocol; -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) +#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || COMPACT_FRAMEWORK || NANOFRAMEWORK_1_0) this.userCertificateValidationCallback = userCertificateValidationCallback; this.userCertificateSelectionCallback = userCertificateSelectionCallback; #endif @@ -254,7 +257,7 @@ public void Connect() if (secure) { // create SSL stream -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_1_0) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0) this.sslStream = new SslStream(this.socket); #else this.netStream = new NetworkStream(this.socket); @@ -262,7 +265,7 @@ public void Connect() #endif // server authentication (SSL/TLS handshake) -#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3) +#if (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) if (clientCert != null) { @@ -405,7 +408,7 @@ public void Close() #if SSL if (this.secure) { -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !NANOFRAMEWORK_1_0) +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !NANOFRAMEWORK_1_0) this.netStream.Close(); #endif this.sslStream.Close(); @@ -421,79 +424,81 @@ public void Close() /// public void Accept() { -#if SSL +#if SSL && !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4 || NANOFRAMEWORK_1_0) // secure channel requested if (secure) { -#if !(MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || NANOFRAMEWORK_1_0) + this.netStream = new NetworkStream(this.socket); this.sslStream = new SslStream(this.netStream, false, this.userCertificateValidationCallback, this.userCertificateSelectionCallback); this.sslStream.AuthenticateAsServer(this.serverCert, false, MqttSslUtility.ToSslPlatformEnum(this.sslProtocol), false); -#endif - } - return; + } #else - return; + //Not used by framework. #endif } } - /// - /// IPAddress Utility class - /// - public static class IPAddressUtility - { /// - /// Return AddressFamily for the IP address + /// IPAddress Utility class /// - /// IP address to check - /// Address family - public static AddressFamily GetAddressFamily(this IPAddress ipAddress) + public static class IPAddressUtility { -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !NANOFRAMEWORK_1_0) + /// + /// Return AddressFamily for the IP address + /// + /// IP address to check + /// Address family + public static AddressFamily GetAddressFamily(this IPAddress ipAddress) + { +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !NANOFRAMEWORK_1_0) return ipAddress.AddressFamily; #else - return (ipAddress.ToString().IndexOf(':') != -1) ? - AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; + return (ipAddress.ToString().IndexOf(':') != -1) ? + AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; #endif + } } - } - /// - /// MQTT SSL utility class - /// - public static class MqttSslUtility - { /// - /// Defines the possible versions of Secure Sockets Layer (SSL). + /// MQTT SSL utility class /// - /// - /// Note: Following the recommendation of the .NET documentation, nanoFramework implementation does not have SSL3 nor Default because those are deprecated and unsecure. - /// -#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !COMPACT_FRAMEWORK) - public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol) + public static class MqttSslUtility { - switch (mqttSslProtocol) + /// + /// Defines the possible versions of Secure Sockets Layer (SSL). + /// + /// + /// Note: Following the recommendation of the .NET documentation, nanoFramework implementation does not have SSL3 nor Default because those are deprecated and unsecure. + /// +#if (!MF_FRAMEWORK_VERSION_V4_2 && !MF_FRAMEWORK_VERSION_V4_3 && !MF_FRAMEWORK_VERSION_V4_4 && !COMPACT_FRAMEWORK) + public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol) { - case MqttSslProtocols.None: - return SslProtocols.None; + switch (mqttSslProtocol) + { + case MqttSslProtocols.None: + return SslProtocols.None; #if !(NANOFRAMEWORK_1_0) //nanoFramework does not support this obsolete protocol. case MqttSslProtocols.SSLv3: return SslProtocols.Ssl3; #endif - case MqttSslProtocols.TLSv1_0: - return SslProtocols.Tls; - case MqttSslProtocols.TLSv1_1: - return SslProtocols.Tls11; - case MqttSslProtocols.TLSv1_2: - return SslProtocols.Tls12; - default: - throw new ArgumentException("SSL/TLS protocol version not supported"); + case MqttSslProtocols.TLSv1_0: + return SslProtocols.Tls; + case MqttSslProtocols.TLSv1_1: + return SslProtocols.Tls11; + case MqttSslProtocols.TLSv1_2: + return SslProtocols.Tls12; +#if NANOFRAMEWORK_1_0 //nanoFramework is awaiting support for this protocol. + case MqttSslProtocols.TLSv1_3: + throw new ArgumentException("TLS 1.3 is currently disabled, awaiting support from OS/Device Firmware."); +#endif + default: + throw new ArgumentException("SSL/TLS protocol version not supported"); + } } - } #elif (MF_FRAMEWORK_VERSION_V4_2 || MF_FRAMEWORK_VERSION_V4_3 || MF_FRAMEWORK_VERSION_V4_4) public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol) { @@ -510,5 +515,6 @@ public static SslProtocols ToSslPlatformEnum(MqttSslProtocols mqttSslProtocol) } } #endif - } } + } + diff --git a/M2Mqtt/Properties/AssemblyInfo.cs b/M2Mqtt/Properties/AssemblyInfo.cs index 5abab678..2d72852d 100644 --- a/M2Mqtt/Properties/AssemblyInfo.cs +++ b/M2Mqtt/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ /* -Copyright (c) 2019 The nanoFramework project contributors Copyright (c) 2013, 2014 Paolo Patierno All rights reserved. This program and the accompanying materials @@ -13,6 +12,7 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ using System.Reflection; @@ -22,12 +22,16 @@ Paolo Patierno - initial API and implementation and/or initial documentation // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. +#if (NANOFRAMEWORK_1_0) +[assembly: AssemblyTitle("nanoFramework M2Mqtt Client")] +#else [assembly: AssemblyTitle("M2Mqtt")] +#endif [assembly: AssemblyDescription("MQTT Client Library for M2M communication")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Paolo Patierno")] [assembly: AssemblyProduct("M2Mqtt")] -[assembly: AssemblyCopyright("Copyright © Paolo Patierno 2014")] +[assembly: AssemblyCopyright("Copyright © Paolo Patierno 2014 & .NET Foundation and Contributors")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/M2Mqtt/Session/MqttSession.cs b/M2Mqtt/Session/MqttSession.cs index 712a8547..fafd812c 100644 --- a/M2Mqtt/Session/MqttSession.cs +++ b/M2Mqtt/Session/MqttSession.cs @@ -36,7 +36,7 @@ public abstract class MqttSession /// /// Constructor /// - public MqttSession() + protected MqttSession() : this(null) { } @@ -45,7 +45,7 @@ public MqttSession() /// Constructor /// /// Client Id to create session - public MqttSession(string clientId) + protected MqttSession(string clientId) { this.ClientId = clientId; this.InflightMessages = new Hashtable(); diff --git a/M2Mqtt/Utility/Trace.cs b/M2Mqtt/Utility/Trace.cs index dfcc73f0..733e4bd5 100644 --- a/M2Mqtt/Utility/Trace.cs +++ b/M2Mqtt/Utility/Trace.cs @@ -12,8 +12,10 @@ and the Eclipse Distribution License is available at Contributors: Paolo Patierno - initial API and implementation and/or initial documentation + .NET Foundation and Contributors - nanoFramework support */ +using System; using System.Diagnostics; namespace uPLibrary.Networking.M2Mqtt.Utility @@ -23,6 +25,10 @@ namespace uPLibrary.Networking.M2Mqtt.Utility /// public enum TraceLevel { + /// + /// None + /// + None = 0x00, /// /// Error /// @@ -30,11 +36,11 @@ public enum TraceLevel /// /// Warning /// - Warning = 0x02, + Warning = 0x03, /// /// Information /// - Information = 0x04, + Information = 0x07, /// /// Verbose /// @@ -50,10 +56,10 @@ public enum TraceLevel } /// - /// delegate for writing trace + /// The callback to invoke to write traces. /// - /// Format - /// Arg + /// The format string for the arguments. + /// The arguments attached to the trace event. public delegate void WriteTrace(string format, params object[] args); /// @@ -64,7 +70,7 @@ public static class Trace /// /// Trace Level /// - public static TraceLevel TraceLevel; + public static TraceLevel TraceLevel { get; set; } /// /// Write Trace /// @@ -89,6 +95,7 @@ public static void Debug(string format, params object[] args) /// /// Trace level /// Format of the string + [Conditional("TRACE")] public static void WriteLine(TraceLevel level, string format) { if (TraceListener != null && (level & TraceLevel) > 0) @@ -103,6 +110,7 @@ public static void WriteLine(TraceLevel level, string format) /// Trace level /// Format of the string /// First argument + [Conditional("TRACE")] public static void WriteLine(TraceLevel level, string format, object arg1) { if (TraceListener != null && (level & TraceLevel) > 0) @@ -118,6 +126,7 @@ public static void WriteLine(TraceLevel level, string format, object arg1) /// Format of the string /// First argument /// Second argument + [Conditional("TRACE")] public static void WriteLine(TraceLevel level, string format, object arg1, object arg2) { if (TraceListener != null && (level & TraceLevel) > 0) diff --git a/M2Mqtt/WinRT/MqttNetworkChannel.cs b/M2Mqtt/WinRT/MqttNetworkChannel.cs index 305992c1..6b88f629 100644 --- a/M2Mqtt/WinRT/MqttNetworkChannel.cs +++ b/M2Mqtt/WinRT/MqttNetworkChannel.cs @@ -148,7 +148,7 @@ public void Connect() public void Accept() { // TODO : SSL support with StreamSocket / StreamSocketListener seems to be NOT supported - return; + //return; } } diff --git a/M2Mqtt/nanoFramework.M2Mqtt.nfproj b/M2Mqtt/nanoFramework.M2Mqtt.nfproj index 0c8a9604..727fe7f0 100644 --- a/M2Mqtt/nanoFramework.M2Mqtt.nfproj +++ b/M2Mqtt/nanoFramework.M2Mqtt.nfproj @@ -15,7 +15,7 @@ nanoFramework.M2Mqtt v1.0 Properties - SSL + SSL;TRACE bin\$(Configuration)\nanoFramework.M2Mqtt.xml diff --git a/TestMqtt/Mosquito.TemperatureGauge/Program.cs b/TestMqtt/Mosquito.TemperatureGauge/Program.cs index 857a872e..d3861e3d 100644 --- a/TestMqtt/Mosquito.TemperatureGauge/Program.cs +++ b/TestMqtt/Mosquito.TemperatureGauge/Program.cs @@ -5,6 +5,7 @@ using uPLibrary.Networking.M2Mqtt.Messages; using System.Net.NetworkInformation; using System.Security.Cryptography.X509Certificates; +using System.Diagnostics; namespace Mosquito.TemperatureGauge @@ -33,11 +34,11 @@ public static void Main() // use a unique id as client id, each time we start the application clientId = Guid.NewGuid().ToString(); - Console.WriteLine("Connecting to Mosquito test server..."); + Debug.WriteLine("Connecting to Mosquito test server..."); client.Connect(clientId); - Console.WriteLine("Connected to Mosquito test server"); + Debug.WriteLine("Connected to Mosquito test server"); while (running) { @@ -63,7 +64,7 @@ private static byte[] GetRandomTemperature() // convert to string formatted NN.NN var temperatureAsString = randomTemperature.ToString("N2"); - Console.WriteLine($"Temperature: {temperatureAsString}"); + Debug.WriteLine($"Temperature: {temperatureAsString}"); return Encoding.UTF8.GetBytes(temperatureAsString); } @@ -79,7 +80,7 @@ public static void SetupAndConnectNetwork() if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi - Console.WriteLine("Network connection is: Wi-Fi"); + Debug.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) @@ -96,7 +97,7 @@ public static void SetupAndConnectNetwork() else { // network interface is Ethernet - Console.WriteLine("Network connection is: Ethernet"); + Debug.WriteLine("Network connection is: Ethernet"); ni.EnableDhcp(); } @@ -112,7 +113,7 @@ public static void SetupAndConnectNetwork() static void WaitIP() { - Console.WriteLine("Waiting for IP..."); + Debug.WriteLine("Waiting for IP..."); while (true) { @@ -121,7 +122,7 @@ static void WaitIP() { if (ni.IPv4Address[0] != '0') { - Console.WriteLine($"We have an IP: {ni.IPv4Address}"); + Debug.WriteLine($"We have an IP: {ni.IPv4Address}"); break; } } @@ -132,7 +133,7 @@ static void WaitIP() private static void SetDateTime() { - Console.WriteLine("Setting up system clock..."); + Debug.WriteLine("Setting up system clock..."); // set system date time (needs to be accurate to the day in order to be able to validate a certificate) //Rtc.SetSystemTime(new DateTime(2018, 08, 02)); @@ -140,7 +141,7 @@ private static void SetDateTime() // if SNTP is available and enabled on target device this can be skipped because we should have a valid date & time while (DateTime.UtcNow.Year < 2018) { - Console.WriteLine("Waiting for valid date time..."); + Debug.WriteLine("Waiting for valid date time..."); // wait for valid date & time Thread.Sleep(1000); } diff --git a/TestMqtt/Program.cs b/TestMqtt/Program.cs index 9d248c2d..0c61f0d8 100644 --- a/TestMqtt/Program.cs +++ b/TestMqtt/Program.cs @@ -3,6 +3,7 @@ using System.Text; using System.Net.NetworkInformation; using System.Security.Cryptography.X509Certificates; +using System.Diagnostics; using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; @@ -40,11 +41,11 @@ public static void Main() //clientId = Guid.NewGuid().ToString(); clientId = new Guid(1, 23, 44, 32, 45, 33, 22, 11, 1, 2, 3).ToString(); - Console.WriteLine("Connecting MQTT"); + Debug.WriteLine("Connecting MQTT"); client.Connect(clientId); - Console.WriteLine("Connected MQTT"); + Debug.WriteLine("Connected MQTT"); // Subscribe topics // client.Subscribe(new string[] { "Test1", "Test2" }, new byte[] { 2, 2 }); @@ -56,10 +57,10 @@ public static void Main() "/Automation/Lights/#" }; - Console.WriteLine("Subscribe /Automation/Lights/#"); + Debug.WriteLine("Subscribe /Automation/Lights/#"); client.Subscribe(SubTopics, new byte[] { 2 }); - Console.WriteLine("Enter wait loop"); + Debug.WriteLine("Enter wait loop"); while (running) { Thread.Sleep(10000); @@ -71,7 +72,7 @@ public static void Main() catch (Exception ex) { // Do whatever please you with the exception caught - Console.WriteLine("Main exception " + ex.Message); + Debug.WriteLine("Main exception " + ex.Message); } // Wait before retry @@ -81,7 +82,7 @@ public static void Main() private static void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e) { - Console.WriteLine("Client_MqttMsgSubscribed "); + Debug.WriteLine("Client_MqttMsgSubscribed "); } private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) @@ -90,7 +91,7 @@ private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishE string message = Encoding.UTF8.GetString(e.Message,0,e.Message.Length); - Console.WriteLine("Publish Received Topic:" + topic + " Message:" + message); + Debug.WriteLine("Publish Received Topic:" + topic + " Message:" + message); } public static void SetupAndConnectNetwork() @@ -104,7 +105,7 @@ public static void SetupAndConnectNetwork() if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi - Console.WriteLine("Network connection is: Wi-Fi"); + Debug.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) @@ -121,7 +122,7 @@ public static void SetupAndConnectNetwork() else { // network interface is Ethernet - Console.WriteLine("Network connection is: Ethernet"); + Debug.WriteLine("Network connection is: Ethernet"); ni.EnableDhcp(); } @@ -137,7 +138,7 @@ public static void SetupAndConnectNetwork() static void WaitIP() { - Console.WriteLine("Waiting for IP..."); + Debug.WriteLine("Waiting for IP..."); while (true) { @@ -146,7 +147,7 @@ static void WaitIP() { if (ni.IPv4Address[0] != '0') { - Console.WriteLine($"We have an IP: {ni.IPv4Address}"); + Debug.WriteLine($"We have an IP: {ni.IPv4Address}"); break; } }