Skip to content

Commit

Permalink
Dispose DotNet-BlueZ devices that are not being watched (#5)
Browse files Browse the repository at this point in the history
* Update DotNet-BlueZ

Update submodule to DotNet-BlueZ version that targets netstandard2.1

* Update NRuuviTag.Listener.Linux.csproj

Update DotNetBlueZ reference path

* Dispose found devices if ignored

Modifies the Adapter.DeviceFound handler to dispose of the device in the event arguments if the device is not being added to the list of watchers. This is an attempt to prevent the error seen in #4 from occurring.

* Update Tmds.DBus version

Updates Tmds.DBus to version 0.20.0

* Update version.json
  • Loading branch information
wazzamatazz authored Sep 17, 2024
1 parent 0a4f865 commit 3349040
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
building via build.ps1 or build.sh. It is defined here to allow Visual Studio to build with
the solution with the correct version number.
-->
<Version>0.12.1</Version>
<Version>0.13.0</Version>
</PropertyGroup>

<Choose>
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
<PackageVersion Include="System.Net.WebSockets.Client" Version="4.3.2" />
<PackageVersion Include="System.Text.Json" Version="6.0.6" />
<PackageVersion Include="System.Threading.Channels" Version="6.0.0" />
<PackageVersion Include="Tmds.DBus" Version="0.11.0" />
<PackageVersion Include="Tmds.DBus" Version="0.20.0" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions build/version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Major": 0,
"Minor": 12,
"Patch": 2,
"Minor": 13,
"Patch": 0,
"PreRelease": ""
}
62 changes: 40 additions & 22 deletions src/NRuuviTag.Listener.Linux/BlueZListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,25 @@ void UpdateDeviceProperties(Device1Properties properties, Tmds.DBus.PropertyChan

// Adds a watcher for the specified device so that we can emit new samples when the
// device properties change.
async Task AddDeviceWatcher(HashtagChris.DotNetBlueZ.Device device, Device1Properties properties) {
async Task<bool> AddDeviceWatcher(HashtagChris.DotNetBlueZ.Device device, Device1Properties properties) {
if (!running || cancellationToken.IsCancellationRequested) {
return;
return false;
}

await @lock.WaitAsync(cancellationToken).ConfigureAwait(false);
try {
if (watchers.ContainsKey(properties.Address)) {
return;
return false;
}

// Emit initial scan result.
EmitDeviceProperties(properties);

watchers[properties.Address] = await device.WatchPropertiesAsync(changes => {
UpdateDeviceProperties(properties, changes);
}).ConfigureAwait(false);

return true;
}
finally {
@lock.Release();
Expand All @@ -132,29 +137,42 @@ async Task AddDeviceWatcher(HashtagChris.DotNetBlueZ.Device device, Device1Prope

// Handler for when BlueZ detects a new device.
adapter.DeviceFound += async (sender, args) => {
if (!running || cancellationToken.IsCancellationRequested) {
return;
}
var disposeDevice = false;
var props = await args.Device.GetAllAsync().ConfigureAwait(false);
if (cancellationToken.IsCancellationRequested) {
return;
}
try {
if (!running || cancellationToken.IsCancellationRequested) {
disposeDevice = true;
return;
}
if (props.ManufacturerData == null || !props.ManufacturerData.ContainsKey(Constants.ManufacturerId)) {
// This is not a RuuviTag.
return;
}
var props = await args.Device.GetAllAsync().ConfigureAwait(false);
if (cancellationToken.IsCancellationRequested) {
disposeDevice = true;
return;
}
if (filter != null && !filter.Invoke(props.Address)) {
// We are not interested in this RuuviTag.
return;
}
if (props.ManufacturerData == null || !props.ManufacturerData.ContainsKey(Constants.ManufacturerId)) {
// This is not a RuuviTag.
disposeDevice = true;
return;
}
if (filter != null && !filter.Invoke(props.Address)) {
// We are not interested in this RuuviTag.
disposeDevice = true;
return;
}
// Emit initial scan result.
EmitDeviceProperties(props);
// Watch for changes to this device.
await AddDeviceWatcher(args.Device, props).ConfigureAwait(false);
// Watch for changes to this device.
if (!await AddDeviceWatcher(args.Device, props).ConfigureAwait(false)) {
disposeDevice = true;
}
}
finally {
if (disposeDevice) {
args.Device.Dispose();
}
}
};

try {
Expand Down
4 changes: 2 additions & 2 deletions src/NRuuviTag.Listener.Linux/NRuuviTag.Listener.Linux.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
</ItemGroup>

<ItemGroup>
<Content Include="..\submodules\DotNet-BlueZ\src\bin\$(Configuration)\netstandard2.0\DotNetBlueZ.dll">
<Content Include="..\submodules\DotNet-BlueZ\src\bin\$(Configuration)\$(TargetFramework)\DotNetBlueZ.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/submodules/DotNet-BlueZ

0 comments on commit 3349040

Please sign in to comment.