Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/Linux.Bluetooth/AdvertisementMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ namespace Linux.Bluetooth
/// Advertisement Monitor class.
/// Requires 'Experimental = true' and 'KernelExperimental = true' in BlueZ main.conf
/// </summary>
public class AdvertisementMonitor : IAdvertisementMonitor1, IObjectManager
public class AdvertisementMonitor : IAdvertisementMonitor1, IObjectManager, IDisposable
{
public ObjectPath ObjectPath { get; }

public event EventHandler<Device>? DeviceFoundEvent;
public event EventHandler<Device>? DeviceLostEvent;

/// <summary>
/// Raised when the D-Bus connection backing this monitor drops.
/// </summary>
/// <remarks>
/// The connection is created by this monitor and never reconnects, so BlueZ loses the monitor
/// registration for good: no further DeviceFoundEvent or DeviceLostEvent is ever raised. Dispose this
/// monitor and create a new one to resume monitoring.
/// </remarks>
public event EventHandler? ConnectionLost;

private readonly Connection _conn;
private readonly AdvertisementMonitor1Properties _properties;
private readonly IAdvertisementMonitorManager1 _manager;
Expand All @@ -40,14 +50,37 @@ public async Task StartAsync()
await _conn.ConnectAsync();
await _conn.RegisterObjectAsync(this);
await _manager.RegisterMonitorAsync(ObjectPath);

_conn.StateChanged += OnConnectionStateChanged;
}

public async Task StopAsync()
{
_conn.StateChanged -= OnConnectionStateChanged;

await _manager.UnregisterMonitorAsync(ObjectPath);
_conn.UnregisterObject(this);
}

private void OnConnectionStateChanged(object sender, ConnectionStateChangedEventArgs e)
{
if (e.State == ConnectionState.Disconnected)
{
ConnectionLost?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
/// Closes the D-Bus connection opened by the constructor. Each monitor owns its own connection, so a
/// monitor dropped without disposing leaks its socket until finalization.
/// </summary>
public void Dispose()
{
_conn.StateChanged -= OnConnectionStateChanged;
_conn.Dispose();
GC.SuppressFinalize(this);
}

public Task ActivateAsync()
{
Console.WriteLine("Advertisement monitoring activated");
Expand Down
25 changes: 14 additions & 11 deletions src/Linux.Bluetooth/Extensions/WatchableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class WatchableExtensions
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IAdapter1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for AdvertisingManager's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -28,7 +28,7 @@ public static Task WaitForPropertyValueAsync<T>(this IAdapter1 obj, string prope
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this ILEAdvertisingManager1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for Device's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -39,7 +39,7 @@ public static Task WaitForPropertyValueAsync<T>(this ILEAdvertisingManager1 obj,
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IDevice1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for Battery's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -50,7 +50,7 @@ public static Task WaitForPropertyValueAsync<T>(this IDevice1 obj, string proper
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IBattery1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/*
/// <summary>Wait for GattService's Property and specified value to resolve.</summary>
Expand All @@ -62,7 +62,7 @@ public static Task WaitForPropertyValueAsync<T>(this IBattery1 obj, string prope
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattService1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for GattCharacteristic's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -73,7 +73,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattService1 obj, string p
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattCharacteristic1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>Wait for GattDescriptor's Property and specified value to resolve.</summary>
/// <typeparam name="T">Type of value.</typeparam>
Expand All @@ -84,7 +84,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattCharacteristic1 obj, s
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IGattDescriptor1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
*/

/// <summary>Wait for MediaControl's Property and specified value to resolve.</summary>
Expand All @@ -96,7 +96,7 @@ public static Task WaitForPropertyValueAsync<T>(this IGattDescriptor1 obj, strin
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
public static Task WaitForPropertyValueAsync<T>(this IMediaControl1 obj, string propertyName, T value, TimeSpan timeout)
=> WaitForPropertyValueInternalAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);
=> WaitForPropertyValueAsync(obj.GetAsync<T>, obj.WatchPropertiesAsync, propertyName, value, timeout);

/// <summary>
/// Wait for watchable objects property and specified value to resolve.
Expand All @@ -110,10 +110,13 @@ public static Task WaitForPropertyValueAsync<T>(this IMediaControl1 obj, string
/// <param name="timeout">TimeSpan to wait for.</param>
/// <returns>Task or exception.</returns>
/// <exception cref="TimeoutException">On timeout a <seealso cref="TimeoutException"/> is thrown.</exception>
private static async Task WaitForPropertyValueInternalAsync<T>(
public static async Task WaitForPropertyValueAsync<T>(
Func<string, Task<T>> getAsync,
Func<Action<PropertyChanges>, Task<IDisposable>> watchPropertiesAsync,
string propertyName, T value, TimeSpan timeout)
Func<Action<PropertyChanges>,
Task<IDisposable>> watchPropertiesAsync,
string propertyName,
T value,
TimeSpan timeout)
{
// TODO: Change to Task<bool> versus throwing an error.
var (watchTask, watcher) = WaitForPropertyValueInternal(watchPropertiesAsync, propertyName, value);
Expand Down