127 lines
3.3 KiB
Rust
127 lines
3.3 KiB
Rust
use crate::{
|
|
containers::ByteSlice,
|
|
mem::{heap_clone, heap_drop, heap_move_nonnull, heap_move_ok},
|
|
};
|
|
use servicepoint::{CommandCode, Header, Packet};
|
|
use std::ptr::NonNull;
|
|
|
|
/// Tries to load a [Packet] from the passed array with the specified length.
|
|
///
|
|
/// returns: NULL in case of an error, pointer to the allocated packet otherwise
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet {
|
|
let data = unsafe { data.as_slice() };
|
|
heap_move_ok(servicepoint::Packet::try_from(data))
|
|
}
|
|
|
|
/// Creates a raw [Packet] from parts.
|
|
///
|
|
/// returns: new instance. Will never return null.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_from_parts(
|
|
header: Header,
|
|
payload: ByteSlice,
|
|
) -> NonNull<Packet> {
|
|
let payload = if payload == ByteSlice::INVALID {
|
|
None
|
|
} else {
|
|
Some(Vec::from(unsafe { payload.as_slice() }))
|
|
};
|
|
|
|
heap_move_nonnull(Packet { header, payload })
|
|
}
|
|
|
|
/// Returns a pointer to the header field of the provided packet.
|
|
///
|
|
/// The returned header can be changed and will be valid for the lifetime of the packet.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_get_header(
|
|
packet: NonNull<Packet>,
|
|
) -> NonNull<Header> {
|
|
NonNull::from(unsafe { &mut (*packet.as_ptr()).header })
|
|
}
|
|
|
|
/// Returns a pointer to the current payload of the provided packet.
|
|
///
|
|
/// Returns an [ByteSlice::INVALID] instance in case the packet does not have any payload.
|
|
///
|
|
/// The returned memory can be changed and will be valid until a new payload is set.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_get_payload(
|
|
packet: NonNull<Packet>,
|
|
) -> ByteSlice {
|
|
unsafe {
|
|
match &mut (*packet.as_ptr()).payload {
|
|
None => ByteSlice::INVALID,
|
|
Some(payload) => ByteSlice::from_slice(payload),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Sets the payload of the provided packet to the provided data.
|
|
///
|
|
/// This makes previous payload pointers invalid.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_set_payload(
|
|
packet: NonNull<Packet>,
|
|
data: ByteSlice,
|
|
) {
|
|
unsafe {
|
|
(*packet.as_ptr()).payload = if data == ByteSlice::INVALID {
|
|
None
|
|
} else {
|
|
Some(data.as_slice().to_vec())
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Serialize the packet into the provided buffer.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// - if the buffer is not big enough to hold header+payload.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_serialize_to(
|
|
packet: NonNull<Packet>,
|
|
mut buffer: ByteSlice,
|
|
) {
|
|
unsafe {
|
|
packet.as_ref().serialize_to(buffer.as_slice_mut());
|
|
}
|
|
}
|
|
|
|
/// Clones a [Packet].
|
|
///
|
|
/// returns: a new [Packet] instance.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_clone(
|
|
packet: NonNull<Packet>,
|
|
) -> NonNull<Packet> {
|
|
unsafe { heap_clone(packet) }
|
|
}
|
|
|
|
/// Deallocates a [Packet].
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
|
|
unsafe { heap_drop(packet) }
|
|
}
|
|
|
|
/// Converts u16 into [CommandCode].
|
|
///
|
|
/// If the provided value is not valid, false is returned and result is not changed.
|
|
#[no_mangle]
|
|
pub unsafe extern "C" fn sp_u16_to_command_code(
|
|
code: u16,
|
|
result: *mut CommandCode,
|
|
) -> bool {
|
|
match CommandCode::try_from(code) {
|
|
Ok(code) => {
|
|
unsafe {
|
|
*result = code;
|
|
}
|
|
true
|
|
}
|
|
Err(_) => false,
|
|
}
|
|
}
|