Skip to content

Commit 4c8f4e1

Browse files
authored
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 <[email protected]>
1 parent 3670be2 commit 4c8f4e1

File tree

6 files changed

+123
-15
lines changed

6 files changed

+123
-15
lines changed

Tests/NFUnitTest1/NFUnitTest1.nfproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<RunSettingsFilePath>$(MSBuildProjectDirectory)\nano.runsettings</RunSettingsFilePath>
3030
</PropertyGroup>
3131
<ItemGroup>
32+
<Compile Include="TestUuidUtilities.cs" />
3233
<Compile Include="UnitTest1.cs" />
3334
<Compile Include="Properties\AssemblyInfo.cs" />
3435
</ItemGroup>
@@ -37,6 +38,9 @@
3738
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.15.5\lib\mscorlib.dll</HintPath>
3839
<Private>True</Private>
3940
</Reference>
41+
<Reference Include="nanoFramework.System.Runtime">
42+
<HintPath>..\..\packages\nanoFramework.System.Runtime.1.0.27\lib\nanoFramework.System.Runtime.dll</HintPath>
43+
</Reference>
4044
<Reference Include="nanoFramework.System.Text, Version=1.2.54.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
4145
<HintPath>..\..\packages\nanoFramework.System.Text.1.2.54\lib\nanoFramework.System.Text.dll</HintPath>
4246
<Private>True</Private>
@@ -57,6 +61,9 @@
5761
<ItemGroup>
5862
<ProjectReference Include="..\..\nanoFramework.Device.Bluetooth\nanoFramework.Device.Bluetooth.nfproj" />
5963
</ItemGroup>
64+
<ItemGroup>
65+
<Content Include="packages.lock.json" />
66+
</ItemGroup>
6067
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
6168
<!-- MANUAL UPDATE HERE -->
6269
<ProjectExtensions>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using nanoFramework.TestFramework;
8+
using nanoFramework.Device.Bluetooth;
9+
10+
namespace NFUnitTest1
11+
{
12+
/// <summary>
13+
/// Tests the different conversion and type detection functions for Bluetooth uuids
14+
/// </summary>
15+
[TestClass]
16+
public class TestUuidUtilities
17+
{
18+
[TestMethod]
19+
public void BaseUuidIsBluetoothSigUuid()
20+
{
21+
var baseUuid = new Guid("00000000-0000-1000-8000-00805f9b34fb");
22+
23+
Assert.IsTrue(Utilities.IsBluetoothSigUUID(baseUuid));
24+
25+
// the base uid marks the start of the pre-allocated range of 16bit or 32bit uuid values
26+
// the 16bit or 32bit value is 0 so we give it the Uuid16 type
27+
Assert.IsTrue(Utilities.TypeOfUuid(baseUuid) == Utilities.UuidType.Uuid16, "Expecting a 16bit uuid");
28+
}
29+
30+
[TestMethod]
31+
public void NonBaseUuidIs128Uuid()
32+
{
33+
var uuid = new Guid("00000000-0000-1000-8000-00805f9b34fc"); // last digit differs from base uuid
34+
35+
Assert.IsFalse(Utilities.IsBluetoothSigUUID(uuid));
36+
37+
// as this uid is not based on the base uid, the type is a random Uuid128
38+
Assert.IsTrue(Utilities.TypeOfUuid(uuid) == Utilities.UuidType.Uuid128, "Expecting a 128bit uuid");
39+
}
40+
41+
[TestMethod]
42+
public void Test16BitUuids()
43+
{
44+
ushort value16 = 0x1234;
45+
46+
var serviceUid = Utilities.CreateUuidFromShortCode(value16);
47+
48+
var bytes = serviceUid.ToByteArray();
49+
Assert.AreEqual((byte)0x34, bytes[0]);
50+
Assert.AreEqual((byte)0x12, bytes[1]);
51+
Assert.AreEqual((byte)0x00, bytes[2]);
52+
Assert.AreEqual((byte)0x00, bytes[3]);
53+
54+
// the uuid must be recognized as falling in the range of 16 or 32bit uuids
55+
Assert.IsTrue(Utilities.IsBluetoothSigUUID(serviceUid));
56+
57+
Assert.IsTrue(Utilities.TypeOfUuid(serviceUid) == Utilities.UuidType.Uuid16, "Expecting a 16bit uuid");
58+
59+
ushort result = Utilities.ConvertUuidToShortId(serviceUid);
60+
61+
Assert.AreEqual(value16, result, "After conversion, the end result must be the same value we started with");
62+
63+
var refGuid = new Guid("00001234-0000-1000-8000-00805F9B34FB");
64+
65+
Assert.AreEqual(refGuid, serviceUid, "The 16bit value is in the expected place of the 128bit uuid");
66+
}
67+
68+
[TestMethod]
69+
public void Test32BitUuids()
70+
{
71+
var uuid32 = new Guid("12345678-0000-1000-8000-00805F9B34FB"); // 32bit value equals 0x12345678
72+
73+
var bytes = uuid32.ToByteArray();
74+
Assert.AreEqual((byte)0x78, bytes[0]);
75+
Assert.AreEqual((byte)0x56, bytes[1]);
76+
Assert.AreEqual((byte)0x34, bytes[2]);
77+
Assert.AreEqual((byte)0x12, bytes[3]);
78+
79+
// the uuid must be recognized as falling in the range of 16 or 32bit uuids
80+
Assert.IsTrue(Utilities.IsBluetoothSigUUID(uuid32));
81+
82+
Assert.IsTrue(Utilities.TypeOfUuid(uuid32) == Utilities.UuidType.Uuid32, "Expecting a 32bit uuid");
83+
84+
var result = Utilities.ConvertUuidToIntId(uuid32);
85+
86+
Assert.AreEqual(0x12345678, result, "After conversion, the end result must be the same value we started with");
87+
}
88+
}
89+
}

Tests/NFUnitTest1/nano.runsettings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</RunConfiguration>
1111
<nanoFrameworkAdapter>
1212
<Logging>None</Logging>
13-
<IsRealHardware>False</IsRealHardware>
13+
<IsRealHardware>true</IsRealHardware>
1414
</nanoFrameworkAdapter>
1515
</RunSettings>

Tests/NFUnitTest1/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.15.5" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.System.Runtime" version="1.0.27" targetFramework="netnano1.0" />
45
<package id="nanoFramework.System.Text" version="1.2.54" targetFramework="netnano1.0" />
56
<package id="nanoFramework.TestFramework" version="2.1.107" targetFramework="netnano1.0" developmentDependency="true" />
67
</packages>

Tests/NFUnitTest1/packages.lock.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"resolved": "1.15.5",
99
"contentHash": "u2+GvAp1uxLrGdILACAZy+EVKOs28EQ52j8Lz7599egXZ3GBGejjnR2ofhjMQwzrJLlgtyrsx8nSLngDfJNsAg=="
1010
},
11+
"nanoFramework.System.Runtime": {
12+
"type": "Direct",
13+
"requested": "[1.0.27, 1.0.27]",
14+
"resolved": "1.0.27",
15+
"contentHash": "aMwvQV6AR+XlDc+dHR/fWWnTe5dc7LDaqZS0ccfmd31Y39dZnmX3iQB2wXWtZGvXLCC+obc3IF3Vc70FiG0raw=="
16+
},
1117
"nanoFramework.System.Text": {
1218
"type": "Direct",
1319
"requested": "[1.2.54, 1.2.54]",

nanoFramework.Device.Bluetooth/Utilities.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static ushort ConvertUuidToShortId(Guid uuid)
138138
public static UInt32 ConvertUuidToIntId(Guid uuid)
139139
{
140140
byte[] bytes = uuid.ToByteArray();
141-
return (UInt32)((bytes[2] << 24) + (bytes[3] << 16) + (bytes[0] << 8) + bytes[1]);
141+
return (UInt32)(bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | bytes[3] << 24);
142142
}
143143

144144
/// <summary>
@@ -165,7 +165,7 @@ public static Guid CreateUuidFromShortCode(ushort uuid16)
165165
public static bool IsBluetoothSigUUID(Guid uuid)
166166
{
167167
byte[] bytes = uuid.ToByteArray();
168-
for (int index = 0; index < baseUuid.Length; index++)
168+
for (int index = 4; index < baseUuid.Length; index++)
169169
{
170170
if (baseUuid[index] != bytes[index])
171171
{
@@ -183,6 +183,12 @@ public static bool IsBluetoothSigUUID(Guid uuid)
183183
/// <returns>True if Sig UUID is 16bit UUID</returns>
184184
public static bool IsBluetoothSigUUID16(Guid uuid)
185185
{
186+
// first check if the uid is based on the bluetooth base uid
187+
if (!IsBluetoothSigUUID(uuid))
188+
{
189+
return false;
190+
}
191+
186192
byte[] bytes = uuid.ToByteArray();
187193
return (bytes[2] == 0) && (bytes[3] == 0);
188194
}
@@ -194,21 +200,20 @@ public static bool IsBluetoothSigUUID16(Guid uuid)
194200
/// <returns>UUID type.</returns>
195201
public static UuidType TypeOfUuid(Guid uuid)
196202
{
197-
if (IsBluetoothSigUUID(uuid))
203+
// 16bit values are based on the base uid and have the 2 MSB set to 0
204+
if (IsBluetoothSigUUID16(uuid))
198205
{
199-
// 16 bit or 32 bit Bluetooth SIG UUID
200-
if (IsBluetoothSigUUID16(uuid))
201-
{
202-
// 16bit UUID
203-
return UuidType.Uuid16;
204-
}
205-
else
206-
{
207-
// 32bit UUID
208-
return UuidType.Uuid32;
209-
}
206+
// 16bit UUID
207+
return UuidType.Uuid16;
208+
}
209+
// a 32bit value is based on the base uid
210+
else if (IsBluetoothSigUUID(uuid))
211+
{
212+
// 32bit UUID
213+
return UuidType.Uuid32;
210214
}
211215

216+
// the uid is not based on the base uid
212217
return UuidType.Uuid128;
213218
}
214219
}

0 commit comments

Comments
 (0)