Skip to content

Libiio v0 csharp scan context resouces cleanup #1246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: libiio-v0
Choose a base branch
from
Draft
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
53 changes: 43 additions & 10 deletions bindings/csharp/ScanContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace iio
/// Class for getting information about the available contexts.</summary>
public class ScanContext
{
private IntPtr scan_block;

[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_create_scan_block([In] string backend, uint flags);
Expand All @@ -34,14 +33,24 @@ public class ScanContext
[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern long iio_scan_block_scan(IntPtr blk);

[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void iio_scan_block_destroy(IntPtr blk);

/// <summary>
/// Gets the uri and the description of all available contexts with USB backend.
/// </summary>
/// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns>
public Dictionary<string, string> get_usb_backend_contexts()
{
this.scan_block = iio_create_scan_block("usb", 0);
return get_contexts_info();
IntPtr scan_block = iio_create_scan_block("usb", 0);
if (scan_block == IntPtr.Zero)
{
throw new Exception("Unable to create scan block for 'usb' backends");
}
Dictionary<string, string> context_info_dict = get_contexts_info(scan_block);
iio_scan_block_destroy(scan_block);

return context_info_dict;
}

// <summary>
Expand All @@ -50,8 +59,15 @@ public Dictionary<string, string> get_usb_backend_contexts()
/// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns>
public Dictionary<string, string> get_local_backend_contexts()
{
this.scan_block = iio_create_scan_block("local", 0);
return get_contexts_info();
IntPtr scan_block = iio_create_scan_block("local", 0);
if (scan_block == IntPtr.Zero)
{
throw new Exception("Unable to create scan block for 'local' backends");
}
Dictionary<string, string> context_info_dict = get_contexts_info(scan_block);
iio_scan_block_destroy(scan_block);

return context_info_dict;
}

// <summary>
Expand All @@ -60,18 +76,35 @@ public Dictionary<string, string> get_local_backend_contexts()
/// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns>
public Dictionary<string, string> get_dns_sd_backend_contexts()
{
this.scan_block = iio_create_scan_block("ip", 0);
return get_contexts_info();
IntPtr scan_block = iio_create_scan_block("ip", 0);
if (scan_block == IntPtr.Zero)
{
throw new Exception("Unable to create scan block for 'network' backends");
}
Dictionary<string, string> context_info_dict = get_contexts_info(scan_block);
iio_scan_block_destroy(scan_block);

return context_info_dict;
}

private Dictionary<string, string> get_contexts_info()
private Dictionary<string, string> get_contexts_info(IntPtr scan_block)
{
uint contexts_count = (uint)iio_scan_block_scan(this.scan_block);
long ret = iio_scan_block_scan(scan_block);
if (ret < 0)
{
throw new Exception("Failed to perform a scan for the given scan block");
}

uint contexts_count = (uint)ret;
Dictionary<string, string> contexts_info = new Dictionary<string, string>();

for (uint i = 0; i < contexts_count; i++)
{
IntPtr info = iio_scan_block_get_info(this.scan_block, i);
IntPtr info = iio_scan_block_get_info(scan_block, i);
if (info == IntPtr.Zero)
{
throw new Exception("Failed to get info about scan block at index: " + i);
}
string uri = Marshal.PtrToStringAnsi(iio_context_info_get_uri(info));
string description = Marshal.PtrToStringAnsi(iio_context_info_get_description(info));
contexts_info[uri] = description;
Expand Down
85 changes: 85 additions & 0 deletions bindings/csharp/examples/BackendsScanWithBlocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2025 Analog Devices, Inc.
* Author: Dan Nechita <dan.nechita@analog.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* */

using System;
using System.Collections.Generic;

using iio;

namespace libiio_scan_context_app_csharp
{
class Program
{

static void Main(string[] args)
{
Console.WriteLine("Creating an iio scan context instace" + Environment.NewLine);
ScanContext scan_context = new ScanContext();

Console.WriteLine("Scan for backends of type: usb");
try
{
Dictionary<string, string> usb_backends = scan_context.get_usb_backend_contexts();
Console.WriteLine("Usb backends found: " + usb_backends.Count);
foreach (KeyValuePair<string, string> kvp in usb_backends)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}

Console.WriteLine(Environment.NewLine + "Scan for backends of type: network");
try
{
Dictionary<string, string> network_backends = scan_context.get_dns_sd_backend_contexts();
Console.WriteLine("Network backends found: " + network_backends.Count);
foreach (KeyValuePair<string, string> kvp in network_backends)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}

Console.WriteLine(Environment.NewLine + "Scan for backends of type: local");
Dictionary<string, string> local_backends;
try
{
local_backends = scan_context.get_local_backend_contexts();
Console.WriteLine("Local backends found: " + local_backends.Count);
foreach (KeyValuePair<string, string> kvp in local_backends)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
Console.WriteLine("Most likely not running locally.");
}

Console.WriteLine(Environment.NewLine + "Succesfully closing");
}
}
}