2024-09-07 14:11:15 +02:00
|
|
|
//! C functions for interacting with `SPConnection`s
|
2024-05-28 22:23:21 +02:00
|
|
|
//!
|
|
|
|
//! prefix `sp_connection_`
|
|
|
|
|
2024-05-26 13:15:11 +02:00
|
|
|
use std::ffi::{c_char, CStr};
|
|
|
|
use std::ptr::null_mut;
|
|
|
|
|
2024-09-07 14:35:16 +02:00
|
|
|
use crate::{SPCommand, SPPacket};
|
2024-09-05 21:15:53 +02:00
|
|
|
|
|
|
|
/// A connection to the display.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```C
|
|
|
|
/// CConnection connection = sp_connection_open("172.23.42.29:2342");
|
|
|
|
/// if (connection != NULL)
|
2024-09-07 14:44:25 +02:00
|
|
|
/// sp_connection_send_command(connection, sp_command_clear());
|
2024-09-05 21:15:53 +02:00
|
|
|
/// ```
|
|
|
|
pub struct SPConnection(pub(crate) servicepoint::Connection);
|
2024-05-26 13:15:11 +02:00
|
|
|
|
2024-09-07 14:11:15 +02:00
|
|
|
/// Creates a new instance of `SPConnection`.
|
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
|
2024-09-07 14:11:15 +02:00
|
|
|
/// by explicitly calling `sp_connection_free`.
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_connection_open(
|
|
|
|
host: *const c_char,
|
2024-09-05 21:15:53 +02:00
|
|
|
) -> *mut SPConnection {
|
2024-05-26 13:15:11 +02:00
|
|
|
let host = CStr::from_ptr(host).to_str().expect("Bad encoding");
|
2024-09-05 21:15:53 +02:00
|
|
|
let connection = match servicepoint::Connection::open(host) {
|
2024-05-26 13:15:11 +02:00
|
|
|
Err(_) => return null_mut(),
|
|
|
|
Ok(value) => value,
|
|
|
|
};
|
|
|
|
|
2024-09-05 21:15:53 +02:00
|
|
|
Box::into_raw(Box::new(SPConnection(connection)))
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 14:11:15 +02:00
|
|
|
/// Sends a `SPPacket` to the display using the `SPConnection`.
|
2024-09-07 14:44:25 +02:00
|
|
|
///
|
2024-09-07 14:11:15 +02:00
|
|
|
/// The passed `SPPacket` gets consumed.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// returns: true in case of success
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-09-07 14:11:15 +02:00
|
|
|
/// - `SPConnection` points to a valid instance of `SPConnection`
|
|
|
|
/// - `SPPacket` points to a valid instance of `SPPacket`
|
|
|
|
/// - `SPPacket` is not used concurrently or after this call
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-07 14:35:16 +02:00
|
|
|
pub unsafe extern "C" fn sp_connection_send_packet(
|
2024-09-05 21:15:53 +02:00
|
|
|
connection: *const SPConnection,
|
|
|
|
packet: *mut SPPacket,
|
2024-05-26 13:15:11 +02:00
|
|
|
) -> bool {
|
2024-05-28 21:25:59 +02:00
|
|
|
let packet = Box::from_raw(packet);
|
2024-09-05 21:15:53 +02:00
|
|
|
(*connection).0.send((*packet).0).is_ok()
|
2024-05-26 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 14:35:16 +02:00
|
|
|
/// Sends a `SPCommand` to the display using the `SPConnection`.
|
2024-09-07 14:44:25 +02:00
|
|
|
///
|
2024-09-07 14:35:16 +02:00
|
|
|
/// The passed `SPCommand` gets consumed.
|
|
|
|
///
|
|
|
|
/// returns: true in case of success
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
|
|
|
/// - `connection` points to a valid instance of `SPConnection`
|
|
|
|
/// - `command` points to a valid instance of `SPPacket`
|
|
|
|
/// - `command` is not used concurrently or after this call
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn sp_connection_send_command(
|
|
|
|
connection: *const SPConnection,
|
|
|
|
command: *mut SPCommand,
|
|
|
|
) -> bool {
|
|
|
|
let command = (*Box::from_raw(command)).0;
|
|
|
|
(*connection).0.send(command).is_ok()
|
|
|
|
}
|
|
|
|
|
2024-09-07 14:11:15 +02:00
|
|
|
/// Closes and deallocates a `SPConnection`.
|
2024-05-28 21:25:59 +02:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// The caller has to make sure that:
|
|
|
|
///
|
2024-09-07 14:11:15 +02:00
|
|
|
/// - `this` points to a valid `SPConnection`
|
2024-05-28 21:25:59 +02:00
|
|
|
/// - `this` is not used concurrently or after this call
|
2024-05-26 13:15:11 +02:00
|
|
|
#[no_mangle]
|
2024-09-07 14:11:15 +02:00
|
|
|
pub unsafe extern "C" fn sp_connection_free(ptr: *mut SPConnection) {
|
2024-05-26 13:15:11 +02:00
|
|
|
_ = Box::from_raw(ptr);
|
|
|
|
}
|