From 4c8f4e1ab5597ce6dc97522a29955cdf8a09eeb1 Mon Sep 17 00:00:00 2001 From: Serge Gommers Date: Sun, 16 Jun 2024 00:27:31 +0200 Subject: [PATCH 1/2] When advertising a service with a 16bit UUID, the service is listed as a 128bit UUID on NRF Connect (#90) Signed-off-by: Serge Gommers --- Tests/NFUnitTest1/NFUnitTest1.nfproj | 7 ++ Tests/NFUnitTest1/TestUuidUtilities.cs | 89 +++++++++++++++++++++ Tests/NFUnitTest1/nano.runsettings | 2 +- Tests/NFUnitTest1/packages.config | 1 + Tests/NFUnitTest1/packages.lock.json | 6 ++ nanoFramework.Device.Bluetooth/Utilities.cs | 33 ++++---- 6 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 Tests/NFUnitTest1/TestUuidUtilities.cs diff --git a/Tests/NFUnitTest1/NFUnitTest1.nfproj b/Tests/NFUnitTest1/NFUnitTest1.nfproj index 4260b3d..2157a94 100644 --- a/Tests/NFUnitTest1/NFUnitTest1.nfproj +++ b/Tests/NFUnitTest1/NFUnitTest1.nfproj @@ -29,6 +29,7 @@ $(MSBuildProjectDirectory)\nano.runsettings + @@ -37,6 +38,9 @@ ..\..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll True + + ..\..\packages\nanoFramework.System.Runtime.1.0.27\lib\nanoFramework.System.Runtime.dll + ..\..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll True @@ -57,6 +61,9 @@ + + + diff --git a/Tests/NFUnitTest1/TestUuidUtilities.cs b/Tests/NFUnitTest1/TestUuidUtilities.cs new file mode 100644 index 0000000..e09787c --- /dev/null +++ b/Tests/NFUnitTest1/TestUuidUtilities.cs @@ -0,0 +1,89 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +using System; +using nanoFramework.TestFramework; +using nanoFramework.Device.Bluetooth; + +namespace NFUnitTest1 +{ + /// + /// Tests the different conversion and type detection functions for Bluetooth uuids + /// + [TestClass] + public class TestUuidUtilities + { + [TestMethod] + public void BaseUuidIsBluetoothSigUuid() + { + var baseUuid = new Guid("00000000-0000-1000-8000-00805f9b34fb"); + + Assert.IsTrue(Utilities.IsBluetoothSigUUID(baseUuid)); + + // the base uid marks the start of the pre-allocated range of 16bit or 32bit uuid values + // the 16bit or 32bit value is 0 so we give it the Uuid16 type + Assert.IsTrue(Utilities.TypeOfUuid(baseUuid) == Utilities.UuidType.Uuid16, "Expecting a 16bit uuid"); + } + + [TestMethod] + public void NonBaseUuidIs128Uuid() + { + var uuid = new Guid("00000000-0000-1000-8000-00805f9b34fc"); // last digit differs from base uuid + + Assert.IsFalse(Utilities.IsBluetoothSigUUID(uuid)); + + // as this uid is not based on the base uid, the type is a random Uuid128 + Assert.IsTrue(Utilities.TypeOfUuid(uuid) == Utilities.UuidType.Uuid128, "Expecting a 128bit uuid"); + } + + [TestMethod] + public void Test16BitUuids() + { + ushort value16 = 0x1234; + + var serviceUid = Utilities.CreateUuidFromShortCode(value16); + + var bytes = serviceUid.ToByteArray(); + Assert.AreEqual((byte)0x34, bytes[0]); + Assert.AreEqual((byte)0x12, bytes[1]); + Assert.AreEqual((byte)0x00, bytes[2]); + Assert.AreEqual((byte)0x00, bytes[3]); + + // the uuid must be recognized as falling in the range of 16 or 32bit uuids + Assert.IsTrue(Utilities.IsBluetoothSigUUID(serviceUid)); + + Assert.IsTrue(Utilities.TypeOfUuid(serviceUid) == Utilities.UuidType.Uuid16, "Expecting a 16bit uuid"); + + ushort result = Utilities.ConvertUuidToShortId(serviceUid); + + Assert.AreEqual(value16, result, "After conversion, the end result must be the same value we started with"); + + var refGuid = new Guid("00001234-0000-1000-8000-00805F9B34FB"); + + Assert.AreEqual(refGuid, serviceUid, "The 16bit value is in the expected place of the 128bit uuid"); + } + + [TestMethod] + public void Test32BitUuids() + { + var uuid32 = new Guid("12345678-0000-1000-8000-00805F9B34FB"); // 32bit value equals 0x12345678 + + var bytes = uuid32.ToByteArray(); + Assert.AreEqual((byte)0x78, bytes[0]); + Assert.AreEqual((byte)0x56, bytes[1]); + Assert.AreEqual((byte)0x34, bytes[2]); + Assert.AreEqual((byte)0x12, bytes[3]); + + // the uuid must be recognized as falling in the range of 16 or 32bit uuids + Assert.IsTrue(Utilities.IsBluetoothSigUUID(uuid32)); + + Assert.IsTrue(Utilities.TypeOfUuid(uuid32) == Utilities.UuidType.Uuid32, "Expecting a 32bit uuid"); + + var result = Utilities.ConvertUuidToIntId(uuid32); + + Assert.AreEqual(0x12345678, result, "After conversion, the end result must be the same value we started with"); + } + } +} diff --git a/Tests/NFUnitTest1/nano.runsettings b/Tests/NFUnitTest1/nano.runsettings index e82b99e..306e6cb 100644 --- a/Tests/NFUnitTest1/nano.runsettings +++ b/Tests/NFUnitTest1/nano.runsettings @@ -10,6 +10,6 @@ None - False + true \ No newline at end of file diff --git a/Tests/NFUnitTest1/packages.config b/Tests/NFUnitTest1/packages.config index 26efe0c..b32befd 100644 --- a/Tests/NFUnitTest1/packages.config +++ b/Tests/NFUnitTest1/packages.config @@ -1,6 +1,7 @@  + \ No newline at end of file diff --git a/Tests/NFUnitTest1/packages.lock.json b/Tests/NFUnitTest1/packages.lock.json index 0386822..e8be72c 100644 --- a/Tests/NFUnitTest1/packages.lock.json +++ b/Tests/NFUnitTest1/packages.lock.json @@ -8,6 +8,12 @@ "resolved": "1.15.5", "contentHash": "u2+GvAp1uxLrGdILACAZy+EVKOs28EQ52j8Lz7599egXZ3GBGejjnR2ofhjMQwzrJLlgtyrsx8nSLngDfJNsAg==" }, + "nanoFramework.System.Runtime": { + "type": "Direct", + "requested": "[1.0.27, 1.0.27]", + "resolved": "1.0.27", + "contentHash": "aMwvQV6AR+XlDc+dHR/fWWnTe5dc7LDaqZS0ccfmd31Y39dZnmX3iQB2wXWtZGvXLCC+obc3IF3Vc70FiG0raw==" + }, "nanoFramework.System.Text": { "type": "Direct", "requested": "[1.2.54, 1.2.54]", diff --git a/nanoFramework.Device.Bluetooth/Utilities.cs b/nanoFramework.Device.Bluetooth/Utilities.cs index 673a6aa..8871302 100644 --- a/nanoFramework.Device.Bluetooth/Utilities.cs +++ b/nanoFramework.Device.Bluetooth/Utilities.cs @@ -138,7 +138,7 @@ public static ushort ConvertUuidToShortId(Guid uuid) public static UInt32 ConvertUuidToIntId(Guid uuid) { byte[] bytes = uuid.ToByteArray(); - return (UInt32)((bytes[2] << 24) + (bytes[3] << 16) + (bytes[0] << 8) + bytes[1]); + return (UInt32)(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | bytes[3] << 24); } /// @@ -165,7 +165,7 @@ public static Guid CreateUuidFromShortCode(ushort uuid16) public static bool IsBluetoothSigUUID(Guid uuid) { byte[] bytes = uuid.ToByteArray(); - for (int index = 0; index < baseUuid.Length; index++) + for (int index = 4; index < baseUuid.Length; index++) { if (baseUuid[index] != bytes[index]) { @@ -183,6 +183,12 @@ public static bool IsBluetoothSigUUID(Guid uuid) /// True if Sig UUID is 16bit UUID public static bool IsBluetoothSigUUID16(Guid uuid) { + // first check if the uid is based on the bluetooth base uid + if (!IsBluetoothSigUUID(uuid)) + { + return false; + } + byte[] bytes = uuid.ToByteArray(); return (bytes[2] == 0) && (bytes[3] == 0); } @@ -194,21 +200,20 @@ public static bool IsBluetoothSigUUID16(Guid uuid) /// UUID type. public static UuidType TypeOfUuid(Guid uuid) { - if (IsBluetoothSigUUID(uuid)) + // 16bit values are based on the base uid and have the 2 MSB set to 0 + if (IsBluetoothSigUUID16(uuid)) { - // 16 bit or 32 bit Bluetooth SIG UUID - if (IsBluetoothSigUUID16(uuid)) - { - // 16bit UUID - return UuidType.Uuid16; - } - else - { - // 32bit UUID - return UuidType.Uuid32; - } + // 16bit UUID + return UuidType.Uuid16; + } + // a 32bit value is based on the base uid + else if (IsBluetoothSigUUID(uuid)) + { + // 32bit UUID + return UuidType.Uuid32; } + // the uid is not based on the base uid return UuidType.Uuid128; } } From 664ae970f766437dfccb3c4e0c982c903b005386 Mon Sep 17 00:00:00 2001 From: nfbot Date: Sat, 15 Jun 2024 22:32:41 +0000 Subject: [PATCH 2/2] Update CHANGELOG for v1.1.73 ***NO_CI*** --- CHANGELOG.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fdff0e..f1468de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,23 @@ ## [**Changes available only in 'Preview' NuGet packages:**](https://github.com/nanoframework/nanoframework.Device.Bluetooth/tree/HEAD) -[Full Changelog](https://github.com/nanoframework/nanoframework.Device.Bluetooth/compare/v1.1.60...HEAD) +[Full Changelog](https://github.com/nanoframework/nanoframework.Device.Bluetooth/compare/v1.1.65...HEAD) + +**Implemented enhancements:** + +- When advertising a service with a 16bit UUID, the service is listed as a 128bit UUID on NRF Connect [\#90](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/90) + +**Documentation and other chores:** + +- Update 1 NuGet dependencies [\#91](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/91) +- Update 1 NuGet dependencies [\#89](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/89) +- Update 1 NuGet dependencies [\#88](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/88) +- Update 1 NuGet dependencies [\#87](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/87) +- Update 1 NuGet dependencies [\#86](https://github.com/nanoframework/nanoFramework.Device.Bluetooth/pull/86) + +## [v1.1.65](https://github.com/nanoframework/nanoframework.Device.Bluetooth/tree/v1.1.65) (2024-05-09) + +[Full Changelog](https://github.com/nanoframework/nanoframework.Device.Bluetooth/compare/v1.1.60...v1.1.65) **Documentation and other chores:**