-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3390897
commit d22c471
Showing
12 changed files
with
416 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM mcr.microsoft.com/dotnet/sdk:9.0 | ||
|
||
RUN wget https://aka.ms/getvsdbgsh && \ | ||
sh getvsdbgsh -v latest -l /vsdbg |
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
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,10 @@ | ||
{ | ||
"version": "1", | ||
"environments": [ | ||
{ | ||
"name": ".NET 9 Linux", | ||
"type": "docker", | ||
"dockerFile": "dockerfile.test" | ||
} | ||
] | ||
} |
84 changes: 84 additions & 0 deletions
84
tests/ShortDev.Microsoft.ConnectedDevices.Test/E2E/BluetoothHandler.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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using ShortDev.Microsoft.ConnectedDevices.Transports; | ||
using ShortDev.Microsoft.ConnectedDevices.Transports.Bluetooth; | ||
using System.IO.Pipes; | ||
using System.Net.NetworkInformation; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices.Test.E2E; | ||
|
||
internal sealed class BluetoothHandler(DeviceContainer container, DeviceContainer.Device device) : IBluetoothHandler | ||
{ | ||
public PhysicalAddress MacAddress => PhysicalAddress.Parse(device.Address); | ||
|
||
public Task<CdpSocket> ConnectRfcommAsync(EndpointInfo endpoint, RfcommOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var device = container.FindDevice(endpoint.Address) | ||
?? throw new KeyNotFoundException("Could not find device"); | ||
|
||
return Task.FromResult( | ||
device.ConnectFrom(new(CdpTransportType.Rfcomm, device.Address, options.ServiceId ?? "")) | ||
); | ||
} | ||
|
||
public async Task ListenRfcommAsync(RfcommOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
device.ConnectionRequest += OnNewConnection; | ||
|
||
await cancellationToken.AwaitCancellation(); | ||
|
||
device.ConnectionRequest -= OnNewConnection; | ||
|
||
void OnNewConnection(EndpointInfo client, ref (Stream Input, Stream Output)? clientStream) | ||
{ | ||
AnonymousPipeServerStream serverInputStream = new(PipeDirection.In); | ||
AnonymousPipeServerStream serverOutputStream = new(PipeDirection.Out); | ||
|
||
// Accept connection | ||
clientStream = ( | ||
new AnonymousPipeClientStream(PipeDirection.In, serverOutputStream.GetClientHandleAsString()), | ||
new AnonymousPipeClientStream(PipeDirection.Out, serverInputStream.GetClientHandleAsString()) | ||
); | ||
|
||
options.SocketConnected?.Invoke(new CdpSocket() | ||
{ | ||
InputStream = serverInputStream, | ||
OutputStream = serverOutputStream, | ||
Endpoint = client, | ||
Close = () => | ||
{ | ||
serverInputStream.Dispose(); | ||
serverOutputStream.Dispose(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public async Task AdvertiseBLeBeaconAsync(AdvertiseOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var data = options.BeaconData.ToArray(); | ||
container.Advertise(device, (uint)options.ManufacturerId, data); | ||
|
||
await cancellationToken.AwaitCancellation(); | ||
|
||
container.TryRemove(device); | ||
} | ||
|
||
public async Task ScanBLeAsync(ScanOptions scanOptions, CancellationToken cancellationToken = default) | ||
{ | ||
container.FoundDevice += OnNewDevice; | ||
|
||
await cancellationToken.AwaitCancellation(); | ||
|
||
container.FoundDevice -= OnNewDevice; | ||
|
||
void OnNewDevice(DeviceContainer.Device device, DeviceContainer.Adverstisement ad) | ||
{ | ||
if (ad.Manufacturer != Constants.BLeBeaconManufacturerId) | ||
return; | ||
|
||
if (!BLeBeacon.TryParse(ad.Data.ToArray(), out var beaconData)) | ||
return; | ||
|
||
scanOptions.OnDeviceDiscovered?.Invoke(beaconData); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
tests/ShortDev.Microsoft.ConnectedDevices.Test/E2E/DeviceContainer.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using ShortDev.Microsoft.ConnectedDevices.Transports; | ||
using System.Collections.Concurrent; | ||
|
||
namespace ShortDev.Microsoft.ConnectedDevices.Test.E2E; | ||
|
||
internal sealed class DeviceContainer | ||
{ | ||
readonly ConcurrentDictionary<Device, List<Adverstisement>> _registry = []; | ||
sealed record Entry(Device Device, List<Adverstisement> Adverstisements); | ||
|
||
public Device? FindDevice(string address) | ||
=> _registry.FirstOrDefault(x => x.Key.Address == address).Key; | ||
|
||
public void Advertise(Device device, uint manufacturer, ReadOnlyMemory<byte> data) | ||
{ | ||
var list = _registry.GetOrAdd(device, static key => []); | ||
lock (list) | ||
{ | ||
list.Add(new(manufacturer, data)); | ||
} | ||
FoundDevice?.Invoke(device, new(manufacturer, data)); | ||
} | ||
|
||
public bool TryRemove(Device device) | ||
=> _registry.Remove(device, out _); | ||
|
||
public event Action<Device, Adverstisement>? FoundDevice; | ||
|
||
public sealed record Adverstisement(uint Manufacturer, ReadOnlyMemory<byte> Data); | ||
public sealed record Device(CdpTransportType TransportType, string Address) | ||
{ | ||
public CdpSocket ConnectFrom(EndpointInfo client) | ||
{ | ||
(Stream Input, Stream Output)? stream = null; | ||
ConnectionRequest?.Invoke(client, ref stream); | ||
|
||
if (stream is null) | ||
throw new InvalidOperationException("Server did not accept"); | ||
|
||
return new CdpSocket() | ||
{ | ||
InputStream = stream.Value.Input, | ||
OutputStream = stream.Value.Output, | ||
Endpoint = new(TransportType, Address, "Some Service Id"), | ||
Close = () => | ||
{ | ||
stream.Value.Output.Dispose(); | ||
stream.Value.Input.Dispose(); | ||
} | ||
}; | ||
} | ||
|
||
public event ConnectionRequestHandler? ConnectionRequest; | ||
|
||
public delegate void ConnectionRequestHandler(EndpointInfo client, ref (Stream Input, Stream Output)? stream); | ||
} | ||
} |
Oops, something went wrong.