File: ScanContext.cs

package info (click to toggle)
libiio 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,832 kB
  • sloc: ansic: 21,880; python: 1,844; cs: 1,232; sh: 1,062; cpp: 688; yacc: 441; xml: 192; lex: 172; makefile: 40
file content (83 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * libiio - Library for interfacing industrial I/O (IIO) devices
 *
 * Copyright (C) 2020 Analog Devices, Inc.
 * Author: Cristian Iacob <cristian.iacob@analog.com>
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace iio
{
    /// <summary> <see cref="iio.ScanContext"/> class:
    /// 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);

        [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr iio_scan_block_get_info(IntPtr blk, uint index);

        [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr iio_context_info_get_description(IntPtr info);

        [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr iio_context_info_get_uri(IntPtr info);

        [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern long iio_scan_block_scan(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();
        }

        // <summary>
        /// Gets the uri and the description of all available contexts with local 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_local_backend_contexts()
        {
            this.scan_block = iio_create_scan_block("local", 0);
            return get_contexts_info();
        }

        // <summary>
        /// Gets the uri and the description of all available contexts from dns_sd 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_dns_sd_backend_contexts()
        {
            this.scan_block = iio_create_scan_block("ip", 0);
            return get_contexts_info();
        }

        private Dictionary<string, string> get_contexts_info()
        {
            uint contexts_count = (uint)iio_scan_block_scan(this.scan_block);
            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);
                string uri = Marshal.PtrToStringAnsi(iio_context_info_get_uri(info));
                string description = Marshal.PtrToStringAnsi(iio_context_info_get_description(info));
                contexts_info[uri] = description;
            }

            return contexts_info;
        }
    }
}