Skip to content

Commit 09826f0

Browse files
authored
Fix descriptor events (#31)
1 parent 92dd040 commit 09826f0

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

nanoFramework.Device.Bluetooth/BluetoothEventListener.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public BaseEvent ProcessEvent(uint data1, uint data2, DateTime time)
3030
// Data1, Data2 is packed by PostManagedEvent, so we need to unpack the high word.
3131
//
3232
// Data1
33-
// DDCC00TT where DD = descriptorId, CC = characteristicId, TT = BluetoothEventType
33+
// DDCC00TT where DDCC = descriptorId, CC = characteristicId, TT = BluetoothEventType
3434
type = (BluetoothEventType)(data1 & 0xff),
3535
characteristicId = (ushort)((data1 >> 16) & 0x00ff),
36-
descriptorId = (ushort)(data1 >> 24),
36+
descriptorId = (ushort)(data1 >> 16),
3737
ID = (ushort)(data2 & 0xffff)
3838
};
3939
}

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattLocalCharacteristic.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public sealed class GattLocalCharacteristic
1919
// Each Characteristic will have unique _CharacteristicId for event lookup
2020
internal ushort _characteristicId;
2121

22+
private ushort _descriptorNextID;
2223
private readonly byte[] _characteristicUuid;
2324
private readonly GattProtectionLevel _writeProtectionLevel;
2425
private readonly GattProtectionLevel _readProtectionLevel;
@@ -71,6 +72,8 @@ internal GattLocalCharacteristic(Guid characteristicUuid, GattLocalCharacteristi
7172

7273
// Give it next id
7374
_characteristicId = NextCharacteristicIndex();
75+
// Start at 1 for descriptors
76+
_descriptorNextID = 1;
7477

7578
_userDescription = parameters.UserDescription;
7679
if (!string.IsNullOrEmpty(_userDescription))
@@ -82,7 +85,7 @@ internal GattLocalCharacteristic(Guid characteristicUuid, GattLocalCharacteristi
8285
dr.WriteString(_userDescription);
8386
dp.StaticValue = dr.DetachBuffer();
8487

85-
_userDescriptionDescriptor = new GattLocalDescriptor(GattDescriptorUuids.CharacteristicUserDescription, dp, this);
88+
_userDescriptionDescriptor = new GattLocalDescriptor(GattDescriptorUuids.CharacteristicUserDescription, dp, this, _descriptorNextID++);
8689
}
8790

8891
_presentationFormats = new ArrayList();
@@ -103,7 +106,7 @@ internal GattLocalCharacteristic(Guid characteristicUuid, GattLocalCharacteristi
103106
GattLocalDescriptorParameters dp = new GattLocalDescriptorParameters();
104107
dp.StaticValue = dr.DetachBuffer();
105108

106-
_presentationFormatsDescriptors.Add(new GattLocalDescriptor(GattDescriptorUuids.CharacteristicPresentationFormat, dp, this));
109+
_presentationFormatsDescriptors.Add(new GattLocalDescriptor(GattDescriptorUuids.CharacteristicPresentationFormat, dp, this, _descriptorNextID++));
107110
}
108111

109112
// Register with Events
@@ -147,8 +150,8 @@ public GattLocalDescriptorResult CreateDescriptor(Guid descriptorUuid, GattLocal
147150

148151
if (result == BluetoothError.Success)
149152
{
150-
decriptor = new GattLocalDescriptor(descriptorUuid, parameters, this);
151-
_descriptors.Add(new GattLocalDescriptor(descriptorUuid, parameters, this));
153+
decriptor = new GattLocalDescriptor(descriptorUuid, parameters, this, _descriptorNextID++);
154+
_descriptors.Add(decriptor);
152155
}
153156

154157
return new GattLocalDescriptorResult(decriptor, result);
@@ -262,15 +265,16 @@ internal void OnReadRequested(ushort descritorId, GattReadRequestedEventArgs e)
262265
bool handled = false;
263266

264267
// Static value for Characteristic ?
265-
if (_staticValue != null && descritorId == 0)
268+
int descritorIndex = (descritorId >> 8);
269+
if (_staticValue != null && descritorIndex == 0)
266270
{
267271
handled = true;
268272
// Handle static values internally, don't fire an event
269273
DataWriter writer = new DataWriter();
270274
writer.WriteBuffer(_staticValue);
271275
e.GetRequest().RespondWithValue(_staticValue);
272276
}
273-
else if (descritorId != 0)
277+
else if (descritorIndex != 0)
274278
{
275279
// Descriptor event, let descriptor handle it
276280
GattLocalDescriptor des = FindDescriptor(descritorId);
@@ -299,8 +303,10 @@ internal void OnWriteRequested(ushort descritorId, GattWriteRequestedEventArgs e
299303

300304
if (WriteRequested != null)
301305
{
306+
int descritorIndex = (descritorId >> 8);
307+
302308
// LocalCharacteristic event ?
303-
if (descritorId == 0)
309+
if (descritorIndex == 0)
304310
{
305311
handled = true;
306312
WriteRequested?.Invoke(this, e);

nanoFramework.Device.Bluetooth/GenericAttributeProfile/GattLocalDescriptor.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ namespace nanoFramework.Device.Bluetooth.GenericAttributeProfile
1212
/// </summary>
1313
public sealed class GattLocalDescriptor
1414
{
15-
private static ushort GattLocalDescriptorIndex = 0;
16-
1715
// Each Descriptor will have unique _descriptorId for event lookup, events for descriptors are handled by User app.
1816
// This comprises of characteristic ID + GattLocalDescriptorIndex in the form
1917
// x'DDCC' where DD is Descriptor and CC characteristic
@@ -41,7 +39,7 @@ public sealed class GattLocalDescriptor
4139
/// <param name="WriteRequestEventArgs">Event arguments</param>
4240
public delegate void GattLocalDescriptorWriteEventHandler(GattLocalCharacteristic sender, GattWriteRequestedEventArgs WriteRequestEventArgs);
4341

44-
internal GattLocalDescriptor(Guid uuid, GattLocalDescriptorParameters parameters, GattLocalCharacteristic charactisic)
42+
internal GattLocalDescriptor(Guid uuid, GattLocalDescriptorParameters parameters, GattLocalCharacteristic charactisic, int descriptorIndex)
4543
{
4644
_uuid = uuid.ToByteArray();
4745
_charactisic = charactisic;
@@ -50,12 +48,7 @@ internal GattLocalDescriptor(Guid uuid, GattLocalDescriptorParameters parameters
5048
_readProtectionLevel = parameters.ReadProtectionLevel;
5149
_staticValue = parameters.StaticValue;
5250

53-
_descriptorId = (ushort)((NextDescriptorIndex() << 8) + _charactisic._characteristicId);
54-
}
55-
56-
private static ushort NextDescriptorIndex()
57-
{
58-
return ++GattLocalDescriptorIndex;
51+
_descriptorId = (ushort)((descriptorIndex << 8) + _charactisic._characteristicId);
5952
}
6053

6154
/// <summary>

nanoFramework.Device.Bluetooth/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
////////////////////////////////////////////////////////////////
1919
// update this whenever the native assembly signature changes //
20-
[assembly: AssemblyNativeVersion("100.0.0.2")]
20+
[assembly: AssemblyNativeVersion("100.0.0.3")]
2121
////////////////////////////////////////////////////////////////
2222

2323

0 commit comments

Comments
 (0)