111 lines
		
	
	
	
		
			3.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
	
		
			3.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
use crate::ByteSlice;
 | 
						|
use servicepoint::{Header, Packet, TypedCommand};
 | 
						|
use std::ptr::NonNull;
 | 
						|
 | 
						|
/// Turns a [TypedCommand] into a [Packet].
 | 
						|
/// The [TypedCommand] gets consumed.
 | 
						|
///
 | 
						|
/// Returns NULL in case of an error.
 | 
						|
#[no_mangle]
 | 
						|
pub unsafe extern "C" fn sp_packet_from_command(
 | 
						|
    command: NonNull<TypedCommand>,
 | 
						|
) -> *mut Packet {
 | 
						|
    let command = unsafe { *Box::from_raw(command.as_ptr()) };
 | 
						|
    if let Ok(packet) = command.try_into() {
 | 
						|
        Box::leak(Box::new(packet))
 | 
						|
    } else {
 | 
						|
        std::ptr::null_mut()
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/// 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() };
 | 
						|
    match servicepoint::Packet::try_from(data) {
 | 
						|
        Err(_) => std::ptr::null_mut(),
 | 
						|
        Ok(packet) => Box::into_raw(Box::new(packet)),
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/// 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: *const ByteSlice,
 | 
						|
) -> NonNull<Packet> {
 | 
						|
    let payload = if payload.is_null() {
 | 
						|
        vec![]
 | 
						|
    } else {
 | 
						|
        let payload = unsafe { (*payload).as_slice() };
 | 
						|
        Vec::from(payload)
 | 
						|
    };
 | 
						|
 | 
						|
    let packet = Box::new(Packet { header, payload });
 | 
						|
    NonNull::from(Box::leak(packet))
 | 
						|
}
 | 
						|
 | 
						|
/// 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(&mut unsafe { (*packet.as_ptr()).header })
 | 
						|
}
 | 
						|
 | 
						|
/// Returns a pointer to the current payload of the provided packet.
 | 
						|
///
 | 
						|
/// 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 { ByteSlice::from_slice(&mut *(*packet.as_ptr()).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 = 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>,
 | 
						|
    buffer: ByteSlice,
 | 
						|
) {
 | 
						|
    unsafe {
 | 
						|
        packet.as_ref().serialize_to(buffer.as_slice_mut());
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/// Clones a [Packet].
 | 
						|
#[no_mangle]
 | 
						|
pub unsafe extern "C" fn sp_packet_clone(
 | 
						|
    packet: NonNull<Packet>,
 | 
						|
) -> NonNull<Packet> {
 | 
						|
    let result = Box::new(unsafe { packet.as_ref().clone() });
 | 
						|
    NonNull::from(Box::leak(result))
 | 
						|
}
 | 
						|
 | 
						|
/// Deallocates a [Packet].
 | 
						|
#[no_mangle]
 | 
						|
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
 | 
						|
    _ = unsafe { Box::from_raw(packet.as_ptr()) }
 | 
						|
}
 |