-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring API and other fixes and improvements towards a 1.0 release
- Loading branch information
Showing
47 changed files
with
2,913 additions
and
932 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace nexus.protocols.ble | ||
{ | ||
/// <summary> | ||
/// The results of a connection attempt to a remote BLE device | ||
/// </summary> | ||
public struct BleDeviceConnection | ||
{ | ||
/// <summary> | ||
/// </summary> | ||
public BleDeviceConnection( ConnectionResult connectionResult, IBleGattServerConnection gattServer ) | ||
{ | ||
ConnectionResult = connectionResult; | ||
GattServer = gattServer; | ||
} | ||
|
||
/// <summary> | ||
/// The result of the connection attempt to a BLE device | ||
/// </summary> | ||
public ConnectionResult ConnectionResult { get; } | ||
|
||
/// <summary> | ||
/// The remote GATT server or null, if the connection was unsuccessful | ||
/// </summary> | ||
public IBleGattServerConnection GattServer { get; } | ||
} | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="BleDeviceConnection" /> | ||
/// </summary> | ||
[EditorBrowsable( EditorBrowsableState.Never )] | ||
public static class BleDeviceConnectionExtensions | ||
{ | ||
/// <summary> | ||
/// True if this <see cref="BleDeviceConnection" /> resulted in <see cref="ConnectionResult.Success" /> | ||
/// <remarks>Syntax sugar for <c>device.ConnectionResult == ConnectionResult.Success</c></remarks> | ||
/// </summary> | ||
public static Boolean IsSuccessful( this BleDeviceConnection device ) | ||
{ | ||
return device.ConnectionResult == ConnectionResult.Success; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
// Copyright Malachi Griffie | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Contracts; | ||
using System.IO; | ||
using nexus.core; | ||
using nexus.protocols.ble.advertisement; | ||
using nexus.protocols.ble.connection; | ||
using nexus.core.resharper; | ||
|
||
namespace nexus.protocols.ble | ||
{ | ||
|
@@ -89,39 +84,6 @@ public static Guid AddressToGuid( Byte[] address ) | |
} ); | ||
} | ||
|
||
/// <summary> | ||
/// Return true if <see cref="CharacteristicProperty.Indicate" /> is set on this characteristic | ||
/// </summary> | ||
public static Boolean CanIndicate( this CharacteristicProperty properties ) | ||
{ | ||
return (properties & CharacteristicProperty.Indicate) != 0; | ||
} | ||
|
||
/// <summary> | ||
/// Return true if <see cref="CharacteristicProperty.Notify" /> is set on this characteristic | ||
/// </summary> | ||
public static Boolean CanNotify( this CharacteristicProperty properties ) | ||
{ | ||
return (properties & CharacteristicProperty.Notify) != 0; | ||
} | ||
|
||
/// <summary> | ||
/// Return true if <see cref="CharacteristicProperty.Read" /> is set on this characteristic | ||
/// </summary> | ||
public static Boolean CanRead( this CharacteristicProperty properties ) | ||
{ | ||
return (properties & CharacteristicProperty.Read) != 0; | ||
} | ||
|
||
/// <summary> | ||
/// Return true if <see cref="CharacteristicProperty.Write" /> or <see cref="CharacteristicProperty.WriteNoResponse" /> are | ||
/// set on this characteristic | ||
/// </summary> | ||
public static Boolean CanWrite( this CharacteristicProperty properties ) | ||
{ | ||
return (properties & (CharacteristicProperty.Write | CharacteristicProperty.WriteNoResponse)) != 0; | ||
} | ||
|
||
/// <summary> | ||
/// Create a <see cref="Guid" /> from a Bluetooth Special Interest Group adopted key | ||
/// </summary> | ||
|
@@ -146,10 +108,12 @@ public static Guid CreateGuidFromAdoptedKey( this UInt16 adoptedKey ) | |
/// <exception cref="ArgumentNullException">If the provided value is null</exception> | ||
/// <exception cref="ArgumentException">If the provided value is not 4 characters in length</exception> | ||
/// <exception cref="FormatException">If the provided value cannot be parsed to a Guid</exception> | ||
public static Guid CreateGuidFromAdoptedKey( this String adoptedKey ) | ||
public static Guid CreateGuidFromAdoptedKey( [NotNull] this String adoptedKey ) | ||
{ | ||
Contract.Requires<ArgumentNullException>( adoptedKey != null ); | ||
Debug.Assert( adoptedKey != null, "adoptedKey != null" ); | ||
if(adoptedKey == null) | ||
{ | ||
throw new ArgumentNullException( nameof(adoptedKey) ); | ||
} | ||
if(adoptedKey.Length != 4) | ||
{ | ||
throw new ArgumentException( | ||
|
@@ -180,69 +144,5 @@ public static Boolean IsReservedKey( this Guid id ) | |
bytes[3] = 0; | ||
return s_adoptedKeyBase.Equals( id ); | ||
} | ||
|
||
/// <summary> | ||
/// True if this <see cref="BleDeviceConnection" /> resulted in <see cref="ConnectionResult.Success" /> | ||
/// </summary> | ||
public static Boolean IsSuccessful( this BleDeviceConnection device ) | ||
{ | ||
return device.ConnectionResult == ConnectionResult.Success; | ||
} | ||
|
||
/// <summary> | ||
/// Listen for NOTIFY events on this characteristic. | ||
/// </summary> | ||
public static IDisposable NotifyCharacteristicValue( this IBleGattServer server, Guid service, | ||
Guid characteristic, Action<Tuple<Guid, Byte[]>> onNotify, | ||
Action<Exception> onError = null ) | ||
{ | ||
Contract.Requires<ArgumentNullException>( server != null ); | ||
// ReSharper disable once PossibleNullReferenceException | ||
return server.NotifyCharacteristicValue( service, characteristic, Observer.Create( onNotify, null, onError ) ); | ||
} | ||
|
||
/// <summary> | ||
/// Listen for NOTIFY events on this characteristic. | ||
/// </summary> | ||
public static IDisposable NotifyCharacteristicValue( this IBleGattServer server, Guid service, | ||
Guid characteristic, Action<Byte[]> onNotify, | ||
Action<Exception> onError = null ) | ||
{ | ||
Contract.Requires<ArgumentNullException>( server != null ); | ||
// ReSharper disable once PossibleNullReferenceException | ||
return server.NotifyCharacteristicValue( | ||
service, | ||
characteristic, | ||
Observer.Create( ( Tuple<Guid, Byte[]> tuple ) => onNotify( tuple.Item2 ), null, onError ) ); | ||
} | ||
|
||
/// <summary> | ||
/// Parse <c>advD</c> payload data from advertising packet. You should never need to call this from client code, platform | ||
/// libraries should handle it. | ||
/// </summary> | ||
public static IEnumerable<AdvertisingDataItem> ParseAdvertisingPayloadData( Byte[] advD ) | ||
{ | ||
var records = new List<AdvertisingDataItem>(); | ||
var index = 0; | ||
while(index < advD?.Length) | ||
{ | ||
var length = advD[index]; | ||
index++; | ||
if(length > 0) | ||
{ | ||
if(!(advD.Length > index + length)) | ||
{ | ||
throw new InvalidDataException( | ||
"Advertising data specifies length {0} but only has {1} bytes remaining" | ||
.F( length, advD.Length ) ); | ||
} | ||
var type = advD[index]; | ||
var data = advD.Slice( index + 1, index + length ); | ||
index += length; | ||
records.Add( new AdvertisingDataItem( (AdvertisingDataType)type, data ) ); | ||
} | ||
} | ||
return records; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Copyright Malachi Griffie | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace nexus.protocols.ble.connection | ||
namespace nexus.protocols.ble | ||
{ | ||
/// <summary> | ||
/// The progress of a remote connection attempt | ||
|
@@ -14,7 +14,7 @@ public enum ConnectionProgress | |
/// <summary> | ||
/// SearchingForDevice | ||
/// </summary> | ||
SearchingForDevice = -1, | ||
SearchingForDevice = -2, | ||
/// <summary> | ||
/// Disconnected | ||
/// </summary> | ||
|
4 changes: 2 additions & 2 deletions
4
src/ble.net/connection/ConnectionResult.cs → src/ble.net/ConnectionResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Copyright Malachi Griffie | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace nexus.protocols.ble.connection | ||
namespace nexus.protocols.ble | ||
{ | ||
/// <summary> | ||
/// The result of a remote connection attempt | ||
|
4 changes: 2 additions & 2 deletions
4
src/ble.net/connection/ConnectionState.cs → src/ble.net/ConnectionState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Copyright Malachi Griffie | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
namespace nexus.protocols.ble.connection | ||
namespace nexus.protocols.ble | ||
{ | ||
/// <summary> | ||
/// The state of a remote connection | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright Malachi Griffie | ||
// Copyright M. Griffie <[email protected]> | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
|
@@ -28,7 +28,7 @@ public enum EnabledDisabledState | |
/// </summary> | ||
Enabling, | ||
/// <summary> | ||
/// The entity is aeabled and ready for use | ||
/// The entity is enabled and ready for use | ||
/// </summary> | ||
Enabled | ||
} | ||
|
Oops, something went wrong.