|
| 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 | +} |
0 commit comments