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.
Add a new C# example for IIO events similar to the iio_event.c.

Signed-off-by: AlexandraTrifan <Alexandra.Trifan@analog.com>
  • Loading branch information
AlexandraTrifan committed Jan 25, 2024
1 parent 7b973b5 commit 43c9eae
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 210 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
2 changes: 2 additions & 0 deletions bindings/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ if (MCS_EXECUTABLE)
${CMAKE_CURRENT_SOURCE_DIR}/ChannelsMask.cs
${CMAKE_CURRENT_SOURCE_DIR}/Context.cs
${CMAKE_CURRENT_SOURCE_DIR}/Device.cs
${CMAKE_CURRENT_SOURCE_DIR}/EventStream.cs
${CMAKE_CURRENT_SOURCE_DIR}/IOBuffer.cs
${CMAKE_CURRENT_SOURCE_DIR}/IIOEvent.cs
${CMAKE_CURRENT_SOURCE_DIR}/Trigger.cs
${CMAKE_CURRENT_SOURCE_DIR}/IioLib.cs
${CMAKE_CURRENT_SOURCE_DIR}/Scan.cs
Expand Down
55 changes: 17 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,15 @@ 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,
IIO_MOD_LIGHT_UVA,
IIO_MOD_LIGHT_UVB
}

/// <summary><see cref="iio.Channel.ChannelType"/> class:
Expand Down Expand Up @@ -146,6 +118,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 @@ -177,6 +153,9 @@ public struct DataFormat

/// <summary>Number of times length repeats</summary>
public uint repeat;

/// <summary>Contains a value to be added to the raw sample before applying the scale.</summary>
public double offset;
}

internal IntPtr chn;
Expand Down Expand Up @@ -281,7 +260,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
Loading

0 comments on commit 43c9eae

Please sign in to comment.