Skip to content

Commit

Permalink
When advertising a service with a 16bit UUID, the service is listed a…
Browse files Browse the repository at this point in the history
…s a 128bit UUID on NRF Connect (#90)

Signed-off-by: Serge Gommers <[email protected]>
  • Loading branch information
sergegommers authored Jun 15, 2024
1 parent 3670be2 commit 4c8f4e1
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
7 changes: 7 additions & 0 deletions Tests/NFUnitTest1/NFUnitTest1.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
</PropertyGroup>
<ItemGroup>
<Compile Include="TestUuidUtilities.cs" />
<Compile Include="UnitTest1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -37,6 +38,9 @@
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nanoFramework.System.Runtime">

This comment has been minimized.

Copy link
@CoryCharlton

CoryCharlton Jun 15, 2024

Member

nanoFramework.System.Runtime should no longer be required for the latest version of the TestFramework

<HintPath>..\..\packages\nanoFramework.System.Runtime.1.0.27\lib\nanoFramework.System.Runtime.dll</HintPath>
</Reference>
<Reference Include="nanoFramework.System.Text, Version=1.2.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll</HintPath>
<Private>True</Private>
Expand All @@ -57,6 +61,9 @@
<ItemGroup>
<ProjectReference Include="..\..\nanoFramework.Device.Bluetooth\nanoFramework.Device.Bluetooth.nfproj" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.lock.json" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- MANUAL UPDATE HERE -->
<ProjectExtensions>
Expand Down
89 changes: 89 additions & 0 deletions Tests/NFUnitTest1/TestUuidUtilities.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Tests the different conversion and type detection functions for Bluetooth uuids
/// </summary>
[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");
}
}
}
2 changes: 1 addition & 1 deletion Tests/NFUnitTest1/nano.runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging>
<IsRealHardware>False</IsRealHardware>
<IsRealHardware>true</IsRealHardware>
</nanoFrameworkAdapter>
</RunSettings>
1 change: 1 addition & 0 deletions Tests/NFUnitTest1/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.15.5" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Runtime" version="1.0.27" targetFramework="netnano1.0" />
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />
<package id="nanoFramework.TestFramework" version="2.1.107" targetFramework="netnano1.0" developmentDependency="true" />
</packages>
6 changes: 6 additions & 0 deletions Tests/NFUnitTest1/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down
33 changes: 19 additions & 14 deletions nanoFramework.Device.Bluetooth/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand All @@ -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])
{
Expand All @@ -183,6 +183,12 @@ public static bool IsBluetoothSigUUID(Guid uuid)
/// <returns>True if Sig UUID is 16bit UUID</returns>
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);
}
Expand All @@ -194,21 +200,20 @@ public static bool IsBluetoothSigUUID16(Guid uuid)
/// <returns>UUID type.</returns>
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;
}
}
Expand Down

0 comments on commit 4c8f4e1

Please sign in to comment.