2024-05-28 22:23:21 +02:00
|
|
|
//! C functions for interacting with `Connection`s
|
|
|
|
//!
|
|
|
|
//! prefix `sp_connection_`
|
|
|
|
|
2024-05-26 13:15:11 +02:00
|
|
|
use std::ffi::{c_char, CStr};
|
|
|
|
use std::ptr::null_mut;
|
|
|
|
|
2024-05-28 22:23:21 +02:00
|
|
|
use servicepoint::{Connection, Packet};
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Creates a new instance of `Connection`.
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// returns: NULL if connection fails, or connected instance
|
2024-05-26 13:15:11 +02:00
|
|
|
///
|
2024-05-28 21:25:59 +02:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Bad string encoding
|
|
|
|
///
|
|
|
|
/// # 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_dealloc`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_connection_open(
|
|
|
|
host: *const c_char,
|
|
|
|
) -> *mut Connection {
|
|
|
|
let host = CStr::from_ptr(host).to_str().expect("Bad encoding");
|
|
|
|
let connection = match Connection::open(host) {
|
|
|
|
Err(_) => return null_mut(),
|
|
|
|
Ok(value) => value,
|
|
|
|
};
|
|
|
|
|
|
|
|
Box::into_raw(Box::new(connection))
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Sends a `Packet` to the display using the `Connection`.
|
|
|
|
/// The passed `Packet` gets consumed.
|
|
|
|
///
|
|
|
|
/// returns: true in case of success
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `connection` points to a valid instance of `Connection`
|
|
|
|
/// - `packet` points to a valid instance of `Packet`
|
|
|
|
/// - `packet` is not used concurrently or after this call
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_connection_send(
|
|
|
|
connection: *const Connection,
|
2024-05-28 21:25:59 +02:00
|
|
|
packet: *mut Packet,
|
2024-05-26 13:15:11 +02:00
|
|
|
) -> bool {
|
2024-05-28 21:25:59 +02:00
|
|
|
let packet = Box::from_raw(packet);
|
2024-05-26 13:15:11 +02:00
|
|
|
(*connection).send(*packet).is_ok()
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:25:59 +02:00
|
|
|
/// Closes and deallocates a `Connection`.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `this` points to a valid `Connection`
|
|
|
|
/// - `this` is not used concurrently or after this call
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_connection_dealloc(ptr: *mut Connection) {
|
|
|
|
_ = Box::from_raw(ptr);
|
|
|
|
}
|