Skip to content

Commit 6a6c502

Browse files
committed
bindings/csharp: Update C# API that binds with the builtin backends api
The current C# bindings were still calling the libiio v0 api. Update to use the v1 api: iio_has_backend() needs updating as it now takes an extra argument iio_get_backends_count() and iio_get_backend() need renaming This results in renaming the C# API as well to match the C API. Signed-off-by: Dan Nechita <[email protected]>
1 parent 0796835 commit 6a6c502

File tree

2 files changed

+90
-65
lines changed

2 files changed

+90
-65
lines changed

bindings/csharp/Context.cs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -50,61 +50,6 @@ internal Version()
5050
}
5151
}
5252

53-
/// <summary>
54-
/// Represents the log levels for IIO.
55-
/// </summary>
56-
public enum IioLogLevel
57-
{
58-
NoLog = 1,
59-
Error = 2,
60-
Warning = 3,
61-
Info = 4,
62-
Debug = 5
63-
}
64-
65-
/// <summary>
66-
/// Represents the parameters for creating an IIO context.
67-
/// </summary>
68-
[StructLayout(LayoutKind.Sequential)]
69-
public struct IioContextParams
70-
{
71-
/// <summary>
72-
/// Handle to the standard output. If null, defaults to stdout.
73-
/// </summary>
74-
public IntPtr Out;
75-
76-
/// <summary>
77-
/// Handle to the error output. If null, defaults to stderr.
78-
/// </summary>
79-
public IntPtr Err;
80-
81-
/// <summary>
82-
/// Log level to use. Defaults to the log level specified at compilation.
83-
/// </summary>
84-
public IioLogLevel LogLevel;
85-
86-
/// <summary>
87-
/// Log level threshold for sending messages to stderr.
88-
/// </summary>
89-
public IioLogLevel StderrLevel;
90-
91-
/// <summary>
92-
/// Log level threshold for including timestamps in messages.
93-
/// </summary>
94-
public IioLogLevel TimestampLevel;
95-
96-
/// <summary>
97-
/// Timeout for I/O operations in milliseconds. If zero, the default timeout is used.
98-
/// </summary>
99-
public uint TimeoutMs;
100-
101-
/// <summary>
102-
/// Reserved for future fields. Must remain unused.
103-
/// </summary>
104-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
105-
public byte[] Reserved;
106-
}
107-
10853
/// <summary><see cref="iio.Context"/> class:
10954
/// Contains the representation of an IIO context.</summary>
11055
public class Context : IIOObject

bindings/csharp/IioLib.cs

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,61 @@ public IIOException(string fmt, int err)
6767
}
6868
}
6969

70+
/// <summary>
71+
/// Represents the log levels for IIO.
72+
/// </summary>
73+
public enum IioLogLevel
74+
{
75+
NoLog = 1,
76+
Error = 2,
77+
Warning = 3,
78+
Info = 4,
79+
Debug = 5
80+
}
81+
82+
/// <summary>
83+
/// Represents the parameters for creating an IIO context.
84+
/// </summary>
85+
[StructLayout(LayoutKind.Sequential)]
86+
public struct IioContextParams
87+
{
88+
/// <summary>
89+
/// Handle to the standard output. If null, defaults to stdout.
90+
/// </summary>
91+
public IntPtr Out;
92+
93+
/// <summary>
94+
/// Handle to the error output. If null, defaults to stderr.
95+
/// </summary>
96+
public IntPtr Err;
97+
98+
/// <summary>
99+
/// Log level to use. Defaults to the log level specified at compilation.
100+
/// </summary>
101+
public IioLogLevel LogLevel;
102+
103+
/// <summary>
104+
/// Log level threshold for sending messages to stderr.
105+
/// </summary>
106+
public IioLogLevel StderrLevel;
107+
108+
/// <summary>
109+
/// Log level threshold for including timestamps in messages.
110+
/// </summary>
111+
public IioLogLevel TimestampLevel;
112+
113+
/// <summary>
114+
/// Timeout for I/O operations in milliseconds. If zero, the default timeout is used.
115+
/// </summary>
116+
public uint TimeoutMs;
117+
118+
/// <summary>
119+
/// Reserved for future fields. Must remain unused.
120+
/// </summary>
121+
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
122+
public byte[] Reserved;
123+
}
124+
70125
public abstract class IIOObject : IDisposable
71126
{
72127
internal IntPtr hdl;
@@ -121,13 +176,13 @@ public static class IioLib
121176

122177
[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
123178
[return: MarshalAs(UnmanagedType.I1)]
124-
private static extern bool iio_has_backend([In()] string backend);
179+
private static extern bool iio_has_backend(IntPtr ctx_params, [In()] string backend);
125180

126181
[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
127-
private static extern int iio_get_backends_count();
182+
private static extern int iio_get_builtin_backends_count();
128183

129184
[DllImport(IioLib.dllname, CallingConvention = CallingConvention.Cdecl)]
130-
private static extern IntPtr iio_get_backend(uint index);
185+
private static extern IntPtr iio_get_builtin_backend(uint index);
131186

132187
/// <summary>Get a description of a negative error code.</summary>
133188
/// <param name="err">Negative error code.</param>
@@ -150,19 +205,44 @@ public static bool has_backend(string backend)
150205
if (backend == null)
151206
throw new IIOException("The backend string should not be null!");
152207

153-
return iio_has_backend(backend);
208+
return iio_has_backend(IntPtr.Zero, backend);
209+
}
210+
211+
/// <summary>Checks if the given backend is available or not.</summary>
212+
/// <param name="ctx_params">Context parameters.</param>
213+
/// <param name="backend">The backend's name.</param>
214+
public static bool has_backend(IioContextParams ctx_params, string backend)
215+
{
216+
if (backend == null)
217+
throw new IIOException("The backend string should not be null!");
218+
219+
// Allocate unmanaged memory for the structure
220+
IntPtr contextParamsPtr = Marshal.AllocHGlobal(Marshal.SizeOf<IioContextParams>());
221+
try
222+
{
223+
// Copy the managed structure to unmanaged memory
224+
Marshal.StructureToPtr(ctx_params, contextParamsPtr, false);
225+
226+
// Call the native function
227+
return iio_has_backend(contextParamsPtr, backend);
228+
}
229+
finally
230+
{
231+
// Free the unmanaged memory
232+
Marshal.FreeHGlobal(contextParamsPtr);
233+
}
154234
}
155235

156-
/// <summary>Gets the total number of available backends.</summary>
157-
public static int get_backends_count()
236+
/// <summary>Gets the total number of available builtin backends.</summary>
237+
public static int get_builtin_backends_count()
158238
{
159-
return iio_get_backends_count();
239+
return iio_get_builtin_backends_count();
160240
}
161241

162-
/// <summary>Gets the backend from the given index.</summary>
163-
public static string get_backend(uint index)
242+
/// <summary>Gets the builtin backend from the given index.</summary>
243+
public static string get_builtin_backend(uint index)
164244
{
165-
return Marshal.PtrToStringAnsi(iio_get_backend(index));
245+
return Marshal.PtrToStringAnsi(iio_get_builtin_backend(index));
166246
}
167247
}
168248
}

0 commit comments

Comments
 (0)