diff --git a/bindings/csharp/CMakeLists.txt b/bindings/csharp/CMakeLists.txt index 332307816..e17ffdf86 100644 --- a/bindings/csharp/CMakeLists.txt +++ b/bindings/csharp/CMakeLists.txt @@ -43,6 +43,7 @@ if (MCS_EXECUTABLE) ${CMAKE_CURRENT_SOURCE_DIR}/Device.cs ${CMAKE_CURRENT_SOURCE_DIR}/IOBuffer.cs ${CMAKE_CURRENT_SOURCE_DIR}/Trigger.cs + ${CMAKE_CURRENT_SOURCE_DIR}/IioLib.cs ${LIBIIO_CS_INFO} ) diff --git a/bindings/csharp/Channel.cs b/bindings/csharp/Channel.cs index 2b7332167..bfff4bd2c 100644 --- a/bindings/csharp/Channel.cs +++ b/bindings/csharp/Channel.cs @@ -69,8 +69,99 @@ public override void write(string str) } } + /// class: + /// Contains the available channel modifiers. + public enum ChannelModifier + { + IIO_NO_MOD, + IIO_MOD_X, + IIO_MOD_Y, + IIO_MOD_Z, + IIO_MOD_X_AND_Y, + IIO_MOD_X_AND_Z, + IIO_MOD_Y_AND_Z, + IIO_MOD_X_AND_Y_AND_Z, + IIO_MOD_X_OR_Y, + IIO_MOD_X_OR_Z, + IIO_MOD_Y_OR_Z, + IIO_MOD_X_OR_Y_OR_Z, + IIO_MOD_LIGHT_BOTH, + IIO_MOD_LIGHT_IR, + IIO_MOD_ROOT_SUM_SQUARED_X_Y, + IIO_MOD_SUM_SQUARED_X_Y_Z, + IIO_MOD_LIGHT_CLEAR, + IIO_MOD_LIGHT_RED, + IIO_MOD_LIGHT_GREEN, + IIO_MOD_LIGHT_BLUE, + IIO_MOD_QUATERNION, + IIO_MOD_TEMP_AMBIENT, + IIO_MOD_TEMP_OBJECT, + IIO_MOD_NORTH_MAGN, + IIO_MOD_NORTH_TRUE, + IIO_MOD_NORTH_MAGN_TILT_COMP, + IIO_MOD_NORTH_TRUE_TILT_COMP, + IIO_MOD_RUNNING, + IIO_MOD_JOGGING, + IIO_MOD_WALKING, + IIO_MOD_STILL, + IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z, + IIO_MOD_I, + IIO_MOD_Q, + IIO_MOD_CO2, + IIO_MOD_VOC, + IIO_MOD_LIGHT_UV, + IIO_MOD_LIGHT_DUV, + IIO_MOD_PM1, + IIO_MOD_PM2P5, + IIO_MOD_PM4, + IIO_MOD_PM10, + IIO_MOD_ETHANOL, + IIO_MOD_H2 + } + + /// class: + /// Contains the available channel types. + public enum ChannelType + { + IIO_VOLTAGE, + IIO_CURRENT, + IIO_POWER, + IIO_ACCEL, + IIO_ANGL_VEL, + IIO_MAGN, + IIO_LIGHT, + IIO_INTENSITY, + IIO_PROXIMITY, + IIO_TEMP, + IIO_INCLI, + IIO_ROT, + IIO_ANGL, + IIO_TIMESTAMP, + IIO_CAPACITANCE, + IIO_ALTVOLTAGE, + IIO_CCT, + IIO_PRESSURE, + IIO_HUMIDITYRELATIVE, + IIO_ACTIVITY, + IIO_STEPS, + IIO_ENERGY, + IIO_DISTANCE, + IIO_VELOCITY, + IIO_CONCENTRATION, + IIO_RESISTANCE, + IIO_PH, + IIO_UVINDEX, + IIO_ELECTRICALCONDUCTIVITY, + IIO_COUNT, + IIO_INDEX, + IIO_GRAVITY, + IIO_POSITIONRELATIVE, + IIO_PHASE, + IIO_MASSCONCENTRATION, + IIO_CHAN_TYPE_UNKNOWN = Int32.MaxValue + } - private IntPtr chn; + internal IntPtr chn; private uint sample_size; [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] @@ -118,6 +209,30 @@ public override void write(string str) [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_channel_get_data_format(IntPtr chn); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_channel_get_index(IntPtr chn); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_channel_get_device(IntPtr chn); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_get_context(IntPtr dev); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_channel_get_modifier(IntPtr chn); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_channel_get_type(IntPtr chn); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_channel_find_attr(IntPtr chn, [In] string name); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void iio_channel_convert(IntPtr chn, IntPtr dst, IntPtr src); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void iio_channel_convert_inverse(IntPtr chn, IntPtr dst, IntPtr src); + /// The name of this channel. public readonly string name; @@ -139,11 +254,19 @@ public override void write(string str) /// A list of all the attributes that this channel has. public readonly List attrs; + /// The modifier of this channel. + public readonly ChannelModifier modifier; + + /// The type of this channel. + public readonly ChannelType type; + internal Channel(IntPtr chn) { this.chn = chn; attrs = new List(); sample_size = (uint)Marshal.ReadInt32(iio_channel_get_data_format(this.chn)) / 8; + modifier = (ChannelModifier) iio_channel_get_modifier(chn); + type = (ChannelType) iio_channel_get_type(chn); uint nb_attrs = iio_channel_get_attrs_count(chn); for (uint i = 0; i < nb_attrs; i++) @@ -258,5 +381,46 @@ public uint write(IOBuffer buffer, byte[] array, bool raw = false) return count; } + + /// Get the index of this channel. + public long get_index() + { + return iio_channel_get_index(chn); + } + + /// Finds the attribute of the current channel with the given name. + /// + /// There is no attribute with the given name. + public Attr find_attribute(string attribute) + { + IntPtr attr = iio_channel_find_attr(chn, attribute); + + if (attr == IntPtr.Zero) + { + throw new Exception("There is no attribute with the given name!"); + } + + return new ChannelAttr(chn, Marshal.PtrToStringAnsi(attr)); + } + + /// Finds the device of the current channel. + /// + public Device get_device() + { + IntPtr dev_ptr = iio_channel_get_device(chn); + return new Device(new Context(dev_ptr), dev_ptr); + } + + /// Converts the data from the hardware format to the format of the arhitecture on which libiio is running. + public void convert(IntPtr dst, IntPtr src) + { + iio_channel_convert(chn, dst, src); + } + + /// Converts the data from the arhitecture on which libiio is running to the hardware format. + public void convert_inverse(IntPtr dst, IntPtr src) + { + iio_channel_convert_inverse(chn, dst, src); + } } } diff --git a/bindings/csharp/Context.cs b/bindings/csharp/Context.cs index 5ab83ad07..54206aa0d 100644 --- a/bindings/csharp/Context.cs +++ b/bindings/csharp/Context.cs @@ -44,6 +44,7 @@ internal Version(uint major, uint minor, string git_tag) public class Context : IDisposable { private IntPtr ctx; + public readonly string backend; [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_create_network_context( @@ -58,6 +59,9 @@ private static extern IntPtr iio_create_context_from_uri( [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_create_default_context(); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_create_local_context(); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern void iio_context_destroy(IntPtr ctx); @@ -92,6 +96,18 @@ private static extern IntPtr iio_create_context_from_uri( [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_context_clone(IntPtr ctx); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern uint iio_context_get_attrs_count(IntPtr ctx); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_context_get_attr(IntPtr ctx, uint index, IntPtr name_ptr, IntPtr value_ptr); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_context_find_device(IntPtr ctx, [In] string name); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_context_info_get_description(IntPtr info); + /// A XML representation of the current context. public readonly string xml; @@ -106,6 +122,9 @@ private static extern IntPtr iio_create_context_from_uri( /// A List of all the IIO devices present on the current context. public readonly List devices; + /// A Dictionary of all the attributes of the current channel. (key, value) = (name, value) + public readonly Dictionary attrs; + /// Initializes a new instance of the class, /// using the provided URI. For compatibility with existing code, providing /// an IP address or a hostname here will automatically create a network @@ -113,7 +132,10 @@ private static extern IntPtr iio_create_context_from_uri( /// URI to use for the IIO context creation /// an instance of the class /// The IIO context could not be created. - public Context(string uri) : this(getContextFromString(uri)) {} + public Context(string uri) : this(getContextFromString(uri)) + { + backend = getBackendFromUri(uri); + } /// Initializes a new instance of the class, /// using the local or the network backend of the IIO library. @@ -135,7 +157,37 @@ private static IntPtr getContextFromString(string str) return ptr; } - private Context(IntPtr ctx) + private static string getBackendFromUri(string uri) + { + if (uri.StartsWith("local:")) + { + return "local"; + } + + if (uri.StartsWith("xml:")) + { + return "xml"; + } + + if (uri.StartsWith("ip:")) + { + return "ip"; + } + + if (uri.StartsWith("usb:")) + { + return "usb"; + } + + if (uri.StartsWith("serial:")) + { + return "serial"; + } + + return null; + } + + internal Context(IntPtr ctx) { this.ctx = ctx; @@ -179,6 +231,24 @@ private Context(IntPtr ctx) throw new Exception("Unable to read backend version"); } backend_version = new Version(major, minor, builder.ToString()); + + attrs = new Dictionary(); + + for (uint i = 0; i < iio_context_get_attrs_count(ctx); i++) + { + string attr_name = ""; + GCHandle name_handle = GCHandle.Alloc(attr_name, GCHandleType.Pinned); + IntPtr name_ptr = GCHandle.ToIntPtr(name_handle); + string attr_value = ""; + GCHandle value_handle = GCHandle.Alloc(attr_value, GCHandleType.Pinned); + IntPtr value_ptr = GCHandle.ToIntPtr(value_handle); + + iio_context_get_attr(ctx, i, name_ptr, value_ptr); + attr_name = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(name_ptr)); + attr_value = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(value_ptr)); + + attrs[attr_name] = attr_value; + } } ~Context() @@ -246,5 +316,20 @@ private void Dispose(bool clean) ctx = IntPtr.Zero; } } + + /// Finds the device with the given name from the current context. + /// The name of the device. + /// There is no device with the given name. + public Device find_device(string device) + { + IntPtr dev = iio_context_find_device(ctx, device); + + if (dev == IntPtr.Zero) + { + throw new Exception("There is no device with the given name!"); + } + + return new Device(this, dev); + } } } diff --git a/bindings/csharp/Device.cs b/bindings/csharp/Device.cs index c52e132b6..ee46b1756 100644 --- a/bindings/csharp/Device.cs +++ b/bindings/csharp/Device.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -31,7 +32,7 @@ public class Device { private class DeviceAttr : Attr { - private IntPtr dev; + internal IntPtr dev; [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int iio_device_attr_read(IntPtr dev, [In()] string name, [Out()] StringBuilder val, uint len); @@ -101,7 +102,43 @@ public override void write(string str) } } - private Context ctx; + private class DeviceBufferAttr : Attr + { + private IntPtr dev; + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_device_buffer_attr_read(IntPtr dev, [In] string name, [Out] StringBuilder val, uint len); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_device_buffer_attr_write(IntPtr dev, [In] string name, [In] string val); + + public DeviceBufferAttr(IntPtr dev, string name) : base(name) + { + this.dev = dev; + } + + public override string read() + { + StringBuilder builder = new StringBuilder(1024); + int err = iio_device_buffer_attr_read(dev, name, builder, 1024); + if (err < 0) + { + throw new Exception("Unable to read buffer attribute " + err); + } + return builder.ToString(); + } + + public override void write(string str) + { + int err = iio_device_buffer_attr_write(dev, name, str); + if (err < 0) + { + throw new Exception("Unable to write buffer attribute " + err); + } + } + } + + internal Context ctx; [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_device_get_id(IntPtr dev); @@ -121,12 +158,18 @@ public override void write(string str) [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern uint iio_device_get_debug_attrs_count(IntPtr dev); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern uint iio_device_get_buffer_attrs_count(IntPtr dev); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_device_get_attr(IntPtr dev, uint index); [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_device_get_debug_attr(IntPtr dev, uint index); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_get_buffer_attr(IntPtr dev, uint index); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int iio_device_get_trigger(IntPtr dev, IntPtr triggerptr); @@ -142,6 +185,27 @@ public override void write(string str) [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern int iio_device_reg_read(IntPtr dev, uint addr, ref uint value); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_get_context(IntPtr dev); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_device_set_kernel_buffers_count(IntPtr dev, uint nb); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_find_buffer_attr(IntPtr dev, [In] string name); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_find_debug_attr(IntPtr dev, [In] string name); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_find_attr(IntPtr dev, [In] string name); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_find_channel(IntPtr dev, [In] string name, [In] bool output); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_device_identify_filename(IntPtr dev, [In] string filename, out IntPtr chn_ptr, out IntPtr attr); + internal IntPtr dev; /// An identifier of this device. @@ -157,6 +221,8 @@ public override void write(string str) /// A list of all the debug attributes that this device has. public readonly List debug_attrs; + public readonly List buffer_attrs; + /// A list of all the objects that this device possesses. public readonly List channels; @@ -167,10 +233,12 @@ internal Device(Context ctx, IntPtr dev) channels = new List(); attrs = new List(); debug_attrs = new List(); + buffer_attrs = new List(); uint nb_channels = iio_device_get_channels_count(dev), nb_attrs = iio_device_get_attrs_count(dev), - nb_debug_attrs = iio_device_get_debug_attrs_count(dev); + nb_debug_attrs = iio_device_get_debug_attrs_count(dev), + nb_buffer_attrs = iio_device_get_buffer_attrs_count(dev); for (uint i = 0; i < nb_channels; i++) { @@ -181,11 +249,17 @@ internal Device(Context ctx, IntPtr dev) { attrs.Add(new DeviceAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_attr(dev, i)))); } + for (uint i = 0; i < nb_debug_attrs; i++) { debug_attrs.Add(new DeviceDebugAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_debug_attr(dev, i)))); } + for (uint i = 0; i < nb_buffer_attrs; i++) + { + buffer_attrs.Add(new DeviceBufferAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_buffer_attr(dev, i)))); + } + id = Marshal.PtrToStringAnsi(iio_device_get_id(dev)); IntPtr name_ptr = iio_device_get_name(dev); @@ -292,5 +366,94 @@ public uint reg_read(uint addr) } return value; } + + /// Sets the number of active kernel buffers for this device. + /// The number of kernel buffers. + public int set_kernel_buffers_count(uint nb) + { + return iio_device_set_kernel_buffers_count(dev, nb); + } + + /// Gets the context of the current device. + /// An instance of the class. + public Context get_context() + { + return new Context(iio_device_get_context(dev)); + } + + /// Finds the channel with the given name from the current device. + /// The name of the channel. + /// true if you are looking for an output channel, otherwise false. + /// An instance of the class. + /// There is no channel with the given name. + public Channel find_channel(string channel, bool output) + { + IntPtr chn = iio_device_find_channel(dev, channel, output); + + if (chn == IntPtr.Zero) + { + throw new Exception("There is no channel with the given name!"); + } + + return new Channel(chn); + } + + /// Finds the attribute with the given name from the current device. + /// The name of the attribute. + /// An instance of the class. + /// There is no attribute with the given name. + public Attr find_attribute(string attribute) + { + IntPtr attr = iio_device_find_attr(dev, attribute); + + if (attr == IntPtr.Zero) + { + throw new Exception("This device has no attribute with the given name!"); + } + + return new DeviceAttr(dev, Marshal.PtrToStringAnsi(attr)); + } + + /// Finds the debug attribute with the given name from the current device. + /// The name of the debug attribute. + /// An instance of the class. + /// There is no debug attribute with the given name. + public Attr find_debug_attribute(string attribute) + { + IntPtr attr = iio_device_find_debug_attr(dev, attribute); + + if (attr == IntPtr.Zero) + { + throw new Exception("This device has no debug attribute with the given name!"); + } + + return new DeviceDebugAttr(dev, Marshal.PtrToStringAnsi(attr)); + } + + /// Finds the buffer attribute with the given name from the current device. + /// The name of the buffer attribute. + /// An instance of the class. + /// There is no attribute with the given name. + public Attr find_buffer_attribute(string attribute) + { + IntPtr attr = iio_device_find_buffer_attr(dev, attribute); + + if (attr == IntPtr.Zero) + { + throw new Exception("This device has no buffer attribute with the given name!"); + } + + return new DeviceBufferAttr(dev, Marshal.PtrToStringAnsi(attr)); + } + + /// Finds the channel attribute coresponding to the given filename from the current device. + /// The name of the attribute. + /// Output variable. It will contain a pointer to the resulting . + /// Output variable. It will contain a pointer to the resulting . + /// C errorcode if error encountered, otherwise 0. + public int identify_filename(string filename, IntPtr chn_ptr, IntPtr attr) + { + return iio_device_identify_filename(dev, filename, out chn_ptr, out attr); + } } } diff --git a/bindings/csharp/IOBuffer.cs b/bindings/csharp/IOBuffer.cs index 2f3487d9e..55cc28c12 100644 --- a/bindings/csharp/IOBuffer.cs +++ b/bindings/csharp/IOBuffer.cs @@ -20,6 +20,8 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; +using System.Runtime.Remoting.Contexts; +using System.Security.Authentication.ExtendedProtection; using System.Text; using System.Threading.Tasks; @@ -50,6 +52,27 @@ private static extern IntPtr iio_device_create_buffer(IntPtr dev, uint samples_c [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr iio_buffer_end(IntPtr buf); + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_buffer_get_poll_fd(IntPtr buf); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_buffer_set_blocking_mode(IntPtr buf, bool blocking); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void iio_buffer_cancel(IntPtr buf); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_buffer_first(IntPtr buf, IntPtr chn); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern long iio_buffer_step(IntPtr buf); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_device_get_context(IntPtr dev); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_buffer_get_device(IntPtr buf); + internal IntPtr buf; /// The size of this buffer, in samples. @@ -166,5 +189,45 @@ public void read(byte[] array) } Marshal.Copy(iio_buffer_start(buf), array, 0, (int)length); } + + /// Returns poll file descriptor for the current buffer. + public int get_poll_fd() + { + return iio_buffer_get_poll_fd(buf); + } + + /// Sets the blocking behavior of the current buffer. + /// true if blocking buffer, otherwise false + public int set_blocking_mode(bool blocking) + { + return iio_buffer_set_blocking_mode(buf, blocking); + } + + /// Cancels the current buffer. + public void cancel() + { + iio_buffer_cancel(buf); + } + + /// Gets the device of the current buffer. + /// The device of the current buffer. + public Device get_device() + { + IntPtr dev = iio_buffer_get_device(buf); + return new Device(new Context(iio_device_get_context(dev)), dev); + } + + /// Gets a pointer to the first sample from the current buffer for a specific channel. + /// The channel for which to find the first sample. + public IntPtr first(Channel ch) + { + return iio_buffer_first(buf, ch.chn); + } + + /// Gets the step size of the current buffer. + public long step() + { + return iio_buffer_step(buf); + } } } diff --git a/bindings/csharp/IioLib.cs b/bindings/csharp/IioLib.cs new file mode 100644 index 000000000..38f89a405 --- /dev/null +++ b/bindings/csharp/IioLib.cs @@ -0,0 +1,73 @@ +/* + * libiio - Library for interfacing industrial I/O (IIO) devices + * + * Copyright (C) 2015 Analog Devices, Inc. + * Author: Paul Cristian Iacob + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * */ + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace iio +{ + /// class: + /// Contains the general methods from libiio. + public class IioLib + { + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern void iio_strerror(int err, [In] string buf, ulong len); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + [return: MarshalAs(UnmanagedType.I1)] + private static extern bool iio_has_backend([In] string backend); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern int iio_get_backends_count(); + + [DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr iio_get_backend(uint index); + + /// Calls the iio_strerror method from libiio. + /// Error code. + /// Error message. + public static void strerror(int err, string buf) + { + iio_strerror(err, buf, (ulong) buf.Length); + } + + /// Checks if the given backend is available or not. + /// The backend's name. + public static bool has_backend(string backend) + { + return iio_has_backend(backend); + } + + /// Gets the total number of available backends. + public static int get_backends_count() + { + return iio_get_backends_count(); + } + + /// Gets the backend from the given index. + public static string get_backend(uint index) + { + return Marshal.PtrToStringAnsi(iio_get_backend(index)); + } + } +}