diff --git a/bindings/csharp/ScanContext.cs b/bindings/csharp/ScanContext.cs index 492c6fbe2..75e7a782e 100644 --- a/bindings/csharp/ScanContext.cs +++ b/bindings/csharp/ScanContext.cs @@ -17,7 +17,6 @@ namespace iio /// Class for getting information about the available contexts. 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); @@ -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); + /// /// Gets the uri and the description of all available contexts with USB backend. /// /// a containing the context's uri as key and its description as value. public Dictionary 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 context_info_dict = get_contexts_info(scan_block); + iio_scan_block_destroy(scan_block); + + return context_info_dict; } // @@ -50,8 +59,15 @@ public Dictionary get_usb_backend_contexts() /// a containing the context's uri as key and its description as value. public Dictionary 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 context_info_dict = get_contexts_info(scan_block); + iio_scan_block_destroy(scan_block); + + return context_info_dict; } // @@ -60,18 +76,35 @@ public Dictionary get_local_backend_contexts() /// a containing the context's uri as key and its description as value. public Dictionary 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 context_info_dict = get_contexts_info(scan_block); + iio_scan_block_destroy(scan_block); + + return context_info_dict; } - private Dictionary get_contexts_info() + private Dictionary 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 contexts_info = new Dictionary(); 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; diff --git a/bindings/csharp/examples/BackendsScanWithBlocks.cs b/bindings/csharp/examples/BackendsScanWithBlocks.cs new file mode 100644 index 000000000..3e939798b --- /dev/null +++ b/bindings/csharp/examples/BackendsScanWithBlocks.cs @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2025 Analog Devices, Inc. + * Author: Dan Nechita + * + * 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 usb_backends = scan_context.get_usb_backend_contexts(); + Console.WriteLine("Usb backends found: " + usb_backends.Count); + foreach (KeyValuePair 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 network_backends = scan_context.get_dns_sd_backend_contexts(); + Console.WriteLine("Network backends found: " + network_backends.Count); + foreach (KeyValuePair 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 local_backends; + try + { + local_backends = scan_context.get_local_backend_contexts(); + Console.WriteLine("Local backends found: " + local_backends.Count); + foreach (KeyValuePair 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"); + } + } +}