1 /* 2 * libiio - Library for interfacing industrial I/O (IIO) devices 3 * 4 * Copyright (C) 2020 Analog Devices, Inc. 5 * Author: Cristian Iacob <cristian.iacob@analog.com> 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * */ 18 19 using System; 20 using System.Collections.Generic; 21 using System.Linq; 22 using System.Runtime.InteropServices; 23 24 namespace iio 25 { 26 /// <summary> <see cref="iio.ScanContext"/> class: 27 /// Class for getting information about the available contexts.</summary> 28 public class ScanContext 29 { 30 private IntPtr scan_block; 31 32 [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] iio_create_scan_block([In] string backend, uint flags)33 private static extern IntPtr iio_create_scan_block([In] string backend, uint flags); 34 35 [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] iio_scan_block_get_info(IntPtr blk, uint index)36 private static extern IntPtr iio_scan_block_get_info(IntPtr blk, uint index); 37 38 [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] iio_context_info_get_description(IntPtr info)39 private static extern IntPtr iio_context_info_get_description(IntPtr info); 40 41 [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] iio_context_info_get_uri(IntPtr info)42 private static extern IntPtr iio_context_info_get_uri(IntPtr info); 43 44 [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] iio_scan_block_scan(IntPtr blk)45 private static extern long iio_scan_block_scan(IntPtr blk); 46 47 /// <summary> 48 /// Gets the uri and the description of all available contexts with USB backend. 49 /// </summary> 50 /// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns> get_usb_backend_contexts()51 public Dictionary<string, string> get_usb_backend_contexts() 52 { 53 this.scan_block = iio_create_scan_block("usb", 0); 54 return get_contexts_info(); 55 } 56 57 // <summary> 58 /// Gets the uri and the description of all available contexts with local backend. 59 /// </summary> 60 /// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns> get_local_backend_contexts()61 public Dictionary<string, string> get_local_backend_contexts() 62 { 63 this.scan_block = iio_create_scan_block("local", 0); 64 return get_contexts_info(); 65 } 66 67 // <summary> 68 /// Gets the uri and the description of all available contexts from dns_sd backend. 69 /// </summary> 70 /// <returns>a <see cref="Dictionary{String, String}"/> containing the context's uri as key and its description as value.</returns> get_dns_sd_backend_contexts()71 public Dictionary<string, string> get_dns_sd_backend_contexts() 72 { 73 this.scan_block = iio_create_scan_block("ip", 0); 74 return get_contexts_info(); 75 } 76 get_contexts_info()77 private Dictionary<string, string> get_contexts_info() 78 { 79 uint contexts_count = (uint)iio_scan_block_scan(this.scan_block); 80 Dictionary<string, string> contexts_info = new Dictionary<string, string>(); 81 82 for (uint i = 0; i < contexts_count; i++) 83 { 84 IntPtr info = iio_scan_block_get_info(this.scan_block, i); 85 string uri = Marshal.PtrToStringAnsi(iio_context_info_get_uri(info)); 86 string description = Marshal.PtrToStringAnsi(iio_context_info_get_description(info)); 87 contexts_info[uri] = description; 88 } 89 90 return contexts_info; 91 } 92 } 93 } 94