add fake connection to C API

This commit is contained in:
Vinzenz Schroeter 2024-10-20 12:29:08 +02:00
parent 240766dc11
commit 05aff8b2dd
2 changed files with 37 additions and 0 deletions

View file

@ -46,6 +46,22 @@ pub unsafe extern "C" fn sp_connection_open(
Box::into_raw(Box::new(SPConnection(connection))) Box::into_raw(Box::new(SPConnection(connection)))
} }
/// Creates a new instance of [SPConnection] for testing that does not actually send anything.
///
/// returns: a new instance. Will never return NULL.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_connection_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_connection_fake() -> NonNull<SPConnection> {
let result = Box::new(SPConnection(servicepoint::Connection::Fake));
NonNull::from(Box::leak(result))
}
/// Sends a [SPPacket] to the display using the [SPConnection]. /// Sends a [SPPacket] to the display using the [SPConnection].
/// ///
/// The passed `packet` gets consumed. /// The passed `packet` gets consumed.

View file

@ -37,6 +37,24 @@ namespace ServicePoint
return native == null ? null : new Connection(native); return native == null ? null : new Connection(native);
} }
/// <summary>
/// Creates a new instance of [SPConnection] for testing that does not actually send anything.
///
/// returns: a new instance. Will never return NULL.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_connection_free`.
/// </summary>
[System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static Connection Fake()
{
return new Connection(Connection.sp_connection_fake());
}
/// <summary> /// <summary>
/// Sends a [SPPacket] to the display using the [SPConnection]. /// Sends a [SPPacket] to the display using the [SPConnection].
/// ///
@ -141,6 +159,9 @@ namespace ServicePoint
[DllImport(__DllName, EntryPoint = "sp_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "sp_connection_open", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPConnection* sp_connection_open(byte* host); private static extern SPConnection* sp_connection_open(byte* host);
[DllImport(__DllName, EntryPoint = "sp_connection_fake", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern SPConnection* sp_connection_fake();
[DllImport(__DllName, EntryPoint = "sp_connection_send_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "sp_connection_send_packet", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.U1)] [return: MarshalAs(UnmanagedType.U1)]
private static extern bool sp_connection_send_packet(SPConnection* connection, SPPacket* packet); private static extern bool sp_connection_send_packet(SPConnection* connection, SPPacket* packet);