Skip to content

Commit

Permalink
bindings/csharp: Update the C# bindings to match the new libiio API.
Browse files Browse the repository at this point in the history
Add new objects for iio_attr and iio_event, fix existing IIO objects
and update the ExampleProgram.cs file.

Signed-off-by: AlexandraTrifan <[email protected]>
  • Loading branch information
AlexandraTrifan committed Jan 19, 2024
1 parent 7b973b5 commit add3f07
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 200 deletions.
48 changes: 41 additions & 7 deletions bindings/csharp/Attr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,69 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace iio
{
/// <summary><see cref="iio.Attr"/> class:
/// Contains the representation of a channel or device attribute.</summary>
public abstract class Attr
/// Contains the representation of an iio_attr.</summary>
public class Attr
{
[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_attr_read_raw(IntPtr attr,
[Out()] StringBuilder dst, uint len);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_attr_write_raw(IntPtr attr, IntPtr src,
uint len);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_attr_get_name(IntPtr attr);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_attr_get_filename(IntPtr attr);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_attr_get_static_value(IntPtr attr);

internal IntPtr attr;

/// <summary>The name of this attribute.</summary>
public readonly string name;

/// <summary>The filename in sysfs to which this attribute is bound.</summary>
public readonly string filename;

internal Attr(string name, string filename = null)
internal Attr(IntPtr attr)
{
this.filename = filename == null ? name : filename;
this.name = name;
this.attr = attr;
this.name = Marshal.PtrToStringAnsi(iio_attr_get_name(attr));
this.filename = Marshal.PtrToStringAnsi(iio_attr_get_filename(attr));
}

/// <summary>Read the value of this attribute as a <c>string</c>.</summary>
/// <exception cref="IioLib.IIOException">The attribute could not be read.</exception>
public abstract string read();
public string read()
{
StringBuilder builder = new StringBuilder(1024);
int err = iio_attr_read_raw(attr, builder, (uint)builder.Capacity);
if (err < 0)
throw new IIOException("Unable to read attribute", err);
return builder.ToString();
}

/// <summary>Set this attribute to the value contained in the <c>string</c> argument.</summary>
/// <param name="val">The <c>string</c> value to set the parameter to.</param>
/// <exception cref="IioLib.IIOException">The attribute could not be written.</exception>
public abstract void write(string val);
public void write(string val)
{
IntPtr valptr = Marshal.StringToHGlobalAnsi(val);
int err = iio_attr_write_raw(attr, valptr, (uint)val.Length);
if (err < 0)
throw new IIOException("Unable to write attribute", err);
}

/// <summary>Read the value of this attribute as a <c>bool</c>.</summary>
/// <exception cref="IioLib.IIOException">The attribute could not be read.</exception>
Expand Down
4 changes: 3 additions & 1 deletion bindings/csharp/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public Block(IOBuffer buf, uint size)

protected override void Destroy()
{
iio_block_destroy(hdl);
if (!stream_block) {
iio_block_destroy(hdl);
}
}

public int enqueue(uint bytes_used = 0, bool cyclic = false)
Expand Down
50 changes: 12 additions & 38 deletions bindings/csharp/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,6 @@ namespace iio
/// Contains the representation of an input or output channel.</summary>
public class Channel
{
private class ChannelAttr : Attr
{
private IntPtr chn;

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_attr_read_raw(IntPtr chn, [In()] string name, [Out()] StringBuilder val, uint len);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_channel_attr_write_string(IntPtr chn, [In()] string name, string val);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_channel_attr_get_filename(IntPtr chn, [In()] string attr);

public ChannelAttr(IntPtr chn, string name) : base(name, Marshal.PtrToStringAnsi(iio_channel_attr_get_filename(chn, name)))
{
this.chn = chn;
}

public override string read()
{
StringBuilder builder = new StringBuilder(1024);
int err = iio_channel_attr_read_raw(chn, name, builder, (uint) builder.Capacity);
if (err < 0)
throw new IIOException("Unable to read channel attribute", err);

return builder.ToString();
}

public override void write(string str)
{
int err = iio_channel_attr_write_string(chn, name, str);
if (err < 0)
throw new IIOException("Unable to write channel attribute", err);
}
}

/// <summary><see cref="iio.Channel.ChannelModifier"/> class:
/// Contains the available channel modifiers.</summary>
public enum ChannelModifier
Expand Down Expand Up @@ -104,7 +68,13 @@ public enum ChannelModifier
IIO_MOD_PM10,
IIO_MOD_ETHANOL,
IIO_MOD_H2,
IIO_MOD_O2
IIO_MOD_O2,
IIO_MOD_LINEAR_X,
IIO_MOD_LINEAR_Y,
IIO_MOD_LINEAR_Z,
IIO_MOD_PITCH,
IIO_MOD_YAW,
IIO_MOD_ROLL
}

/// <summary><see cref="iio.Channel.ChannelType"/> class:
Expand Down Expand Up @@ -146,6 +116,10 @@ public enum ChannelType
IIO_POSITIONRELATIVE,
IIO_PHASE,
IIO_MASSCONCENTRATION,
IIO_DELTA_ANGL,
IIO_DELTA_VELOCITY,
IIO_COLORTEMP,
IIO_CHROMATICITY,
IIO_CHAN_TYPE_UNKNOWN = Int32.MaxValue
}

Expand Down Expand Up @@ -281,7 +255,7 @@ internal Channel(Device dev, IntPtr chn)

for (uint i = 0; i < nb_attrs; i++)
{
attrs.Add(new ChannelAttr(this.chn, Marshal.PtrToStringAnsi(iio_channel_get_attr(chn, i))));
attrs.Add(new Attr(iio_channel_get_attr(chn, i)));
}

IntPtr name_ptr = iio_channel_get_name(this.chn);
Expand Down
12 changes: 3 additions & 9 deletions bindings/csharp/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static extern IIOPtr iio_create_context(IntPtr ctx_params,
private static extern uint iio_context_get_attrs_count(IntPtr ctx);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_context_get_attr(IntPtr ctx, uint index, out IntPtr name_ptr, out IntPtr value_ptr);
private static extern IntPtr iio_context_get_attr(IntPtr ctx, uint index);

/// <summary>A XML representation of the current context.</summary>
public readonly string xml;
Expand Down Expand Up @@ -153,14 +153,8 @@ private Context(IIOPtr ptr)

for (uint i = 0; i < nbAttrs; i++)
{
IntPtr name_ptr;
IntPtr value_ptr;

iio_context_get_attr(hdl, i, out name_ptr, out value_ptr);
string attr_name = Marshal.PtrToStringAnsi(name_ptr);
string attr_value = Marshal.PtrToStringAnsi(value_ptr);

attrs[attr_name] = attr_value;
Attr attr = new Attr(iio_context_get_attr(hdl, i));
attrs[attr.name] = attr.read();
}
}

Expand Down
119 changes: 2 additions & 117 deletions bindings/csharp/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,105 +19,6 @@ namespace iio
/// Contains the representation of an IIO device.</summary>
public class Device
{
private class DeviceAttr : Attr
{
internal IntPtr dev;

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_attr_read(IntPtr dev, [In()] string name, [Out()] StringBuilder val, uint len);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_attr_write(IntPtr dev, [In()] string name, [In()] string val);

public DeviceAttr(IntPtr dev, string name) : base(name)
{
this.dev = dev;
}

public override string read()
{
StringBuilder builder = new StringBuilder(1024);
int err = iio_device_attr_read(dev, name, builder, 1024);
if (err < 0)
throw new IIOException("Unable to read device attribute", err);

return builder.ToString();
}

public override void write(string str)
{
int err = iio_device_attr_write(dev, name, str);
if (err < 0)
throw new IIOException("Unable to write device attribute", err);
}
}

private class DeviceDebugAttr : Attr
{
private IntPtr dev;

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_debug_attr_read(IntPtr dev, [In()] string name, [Out()] StringBuilder val, uint len);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_debug_attr_write(IntPtr dev, [In()] string name, [In()] string val);

public DeviceDebugAttr(IntPtr dev, string name) : base(name)
{
this.dev = dev;
}

public override string read()
{
StringBuilder builder = new StringBuilder(1024);
int err = iio_device_debug_attr_read(dev, name, builder, 1024);
if (err < 0)
throw new IIOException("Unable to read debug attribute", err);

return builder.ToString();
}

public override void write(string str)
{
int err = iio_device_debug_attr_write(dev, name, str);
if (err < 0)
throw new IIOException("Unable to write debug attribute", err);
}
}

private class DeviceBufferAttr : Attr
{
private IntPtr dev;

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_buffer_attr_read(IntPtr dev, [In] string name, [Out] StringBuilder val, uint len);

[DllImport(IioLib.dllname, 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(16384);
int err = iio_device_buffer_attr_read(dev, name, builder, 16384);
if (err < 0)
throw new IIOException("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 IIOException("Unable to write buffer attribute", err);
}
}

/// <summary>Gets the context of the current device.</summary>
public readonly Context ctx;

Expand All @@ -142,18 +43,12 @@ public override void write(string str)
[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_device_get_debug_attrs_count(IntPtr dev);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern uint iio_device_get_buffer_attrs_count(IntPtr dev);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_device_get_attr(IntPtr dev, uint index);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_device_get_debug_attr(IntPtr dev, uint index);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_device_get_buffer_attr(IntPtr dev, uint index);

[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
private static extern int iio_device_get_trigger(IntPtr dev, IntPtr triggerptr);

Expand Down Expand Up @@ -190,9 +85,6 @@ public override void write(string str)
/// <summary>A <c>list</c> of all the debug attributes that this device has.</summary>
public readonly List<Attr> debug_attrs;

/// <summary>A <c>list</c> of all the buffer attributes that this device has.</summary>
public List<Attr> buffer_attrs { get; private set; }

/// <summary>A <c>list</c> of all the <see cref="iio.Channel"/> objects that this device possesses.</summary>
public readonly List<Channel> channels;

Expand All @@ -203,12 +95,10 @@ internal Device(Context ctx, IntPtr dev)
channels = new List<Channel>();
attrs = new List<Attr>();
debug_attrs = new List<Attr>();
buffer_attrs = new List<Attr>();

uint nb_channels = iio_device_get_channels_count(dev);
uint nb_attrs = iio_device_get_attrs_count(dev);
uint nb_debug_attrs = iio_device_get_debug_attrs_count(dev);
uint nb_buffer_attrs = iio_device_get_buffer_attrs_count(dev);

for (uint i = 0; i < nb_channels; i++)
{
Expand All @@ -217,17 +107,12 @@ internal Device(Context ctx, IntPtr dev)

for (uint i = 0; i < nb_attrs; i++)
{
attrs.Add(new DeviceAttr(dev, Marshal.PtrToStringAnsi(iio_device_get_attr(dev, i))));
attrs.Add(new Attr(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))));
debug_attrs.Add(new Attr(iio_device_get_debug_attr(dev, i)));
}

id = Marshal.PtrToStringAnsi(iio_device_get_id(dev));
Expand Down
Loading

0 comments on commit add3f07

Please sign in to comment.