1 /*
2  * libiio - Library for interfacing industrial I/O (IIO) devices
3  *
4  * Copyright (C) 2020 Analog Devices, Inc.
5  * Author: Paul 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.Linq;
21 using System.Runtime.InteropServices;
22 
23 namespace iio
24 {
25     /// <summary><see cref="iio.IioLib"/> class:
26     /// Contains the general methods from libiio.</summary>
27     public static class IioLib
28     {
29         [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
iio_strerror(int err, [In] string buf, ulong len)30         private static extern void iio_strerror(int err, [In] string buf, ulong len);
31 
32         [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
33         [return: MarshalAs(UnmanagedType.I1)]
iio_has_backend([In] string backend)34         private static extern bool iio_has_backend([In] string backend);
35 
36         [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
iio_get_backends_count()37         private static extern int iio_get_backends_count();
38 
39         [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
iio_get_backend(uint index)40         private static extern IntPtr iio_get_backend(uint index);
41 
42         /// <summary>Calls the iio_strerror method from libiio.</summary>
43         /// <param name="err">Error code.</param>
44         /// <param name="buf">Error message.</param>
strerror(int err, string buf)45         public static void strerror(int err, string buf)
46         {
47             if (buf == null)
48             {
49                 throw new System.Exception("The buffer should not be null!");
50             }
51             iio_strerror(err, buf, (ulong) buf.Length);
52         }
53 
54         /// <summary>Checks if the given backend is available or not.</summary>
55         /// <param name="backend">The backend's name.</param>
has_backend(string backend)56         public static bool has_backend(string backend)
57         {
58             if (backend == null)
59             {
60                 throw new System.Exception("The backend string should not be null!");
61             }
62             return iio_has_backend(backend);
63         }
64 
65         /// <summary>Gets the total number of available backends.</summary>
get_backends_count()66         public static int get_backends_count()
67         {
68             return iio_get_backends_count();
69         }
70 
71         /// <summary>Gets the backend from the given index.</summary>
get_backend(uint index)72         public static string get_backend(uint index)
73         {
74             return Marshal.PtrToStringAnsi(iio_get_backend(index));
75         }
76     }
77 }
78