Skip to content

Commit

Permalink
csharp: added missing bindings.
Browse files Browse the repository at this point in the history
Added the missing C# bindings.

Signed-off-by: Iacob <[email protected]>
  • Loading branch information
cristi-iacob committed May 15, 2020
1 parent 6fc25d5 commit 42fba29
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 6 deletions.
1 change: 1 addition & 0 deletions bindings/csharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

Expand Down
166 changes: 165 additions & 1 deletion bindings/csharp/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,99 @@ public override void write(string str)
}
}

/// <summary><see cref="iio.Channel.ChannelModifier"/> class:
/// Contains the available channel modifiers.</summary>
public enum ChannelModifier: int
{
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
}

/// <summary><see cref="iio.Channel.ChannelType"/> class:
/// Contains the available channel types.</summary>
public enum ChannelType: int
{
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)]
Expand Down Expand Up @@ -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);

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

Expand All @@ -139,11 +254,19 @@ public override void write(string str)
/// <summary>A <c>list</c> of all the attributes that this channel has.</summary>
public readonly List<Attr> attrs;

/// <summary>The modifier of this channel.</summary>
public ChannelModifier modifier { get; private set; }

/// <summary>The type of this channel.</summary>
public ChannelType type { get; private set; }

internal Channel(IntPtr chn)
{
this.chn = chn;
attrs = new List<Attr>();
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++)
Expand Down Expand Up @@ -258,5 +381,46 @@ public uint write(IOBuffer buffer, byte[] array, bool raw = false)

return count;
}

/// <summary>Get the index of this channel.</summary>
public long get_index()
{
return iio_channel_get_index(chn);
}

/// <summary>Finds the attribute of the current channel with the given name.</summary>
/// <returns><see cref="iio.Channel.ChannelAttr"/></returns>
/// <exception cref="System.Exception">There is no attribute with the given name.</exception>
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));
}

/// <summary>Finds the device of the current channel.</summary>
/// <returns><see cref="iio.Device"/></returns>
public Device get_device()
{
IntPtr dev_ptr = iio_channel_get_device(chn);
return new Device(new Context(dev_ptr), dev_ptr);
}

/// <summary>Converts the data from the hardware format to the format of the arhitecture on which libiio is running.</summary>
public void convert(IntPtr dst, IntPtr src)
{
iio_channel_convert(chn, dst, src);
}

/// <summary>Converts the data from the arhitecture on which libiio is running to the hardware format.</summary>
public void convert_inverse(IntPtr dst, IntPtr src)
{
iio_channel_convert_inverse(chn, dst, src);
}
}
}
55 changes: 53 additions & 2 deletions bindings/csharp/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal Version(uint major, uint minor, string git_tag)
/// Contains the representation of an IIO context.</summary>
public class Context : IDisposable
{
private IntPtr ctx;
internal IntPtr ctx;

[DllImport("libiio.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr iio_create_network_context(
Expand All @@ -58,6 +58,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);

Expand Down Expand Up @@ -92,6 +95,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);

/// <summary>A XML representation of the current context.</summary>
public readonly string xml;

Expand All @@ -106,6 +121,9 @@ private static extern IntPtr iio_create_context_from_uri(
/// <summary>A <c>List</c> of all the IIO devices present on the current context.</summary>
public readonly List<Device> devices;

/// <summary>A <c>Dictionary</c> of all the attributes of the current channel. (key, value) = (name, value)</summary>
public Dictionary<string, string> attrs { get; private set; }

/// <summary>Initializes a new instance of the <see cref="iio.Context"/> class,
/// using the provided URI. For compatibility with existing code, providing
/// an IP address or a hostname here will automatically create a network
Expand Down Expand Up @@ -135,7 +153,7 @@ private static IntPtr getContextFromString(string str)
return ptr;
}

private Context(IntPtr ctx)
internal Context(IntPtr ctx)
{
this.ctx = ctx;

Expand Down Expand Up @@ -179,6 +197,24 @@ private Context(IntPtr ctx)
throw new Exception("Unable to read backend version");
}
backend_version = new Version(major, minor, builder.ToString());

attrs = new Dictionary<string, string>();

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()
Expand Down Expand Up @@ -246,5 +282,20 @@ private void Dispose(bool clean)
ctx = IntPtr.Zero;
}
}

/// <summary>Finds the device with the given name from the current context.</summary>
/// <param name="device">The name of the device.</param>
/// <exception cref="System.Exception">There is no device with the given name.</exception>
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);
}
}
}
Loading

0 comments on commit 42fba29

Please sign in to comment.