sp_packet_from_parts, sp_bitmap_new_screen_sized

This commit is contained in:
Vinzenz Schroeter 2024-11-23 23:47:41 +01:00
parent 93657c9f85
commit 24e0eaaf07
2 changed files with 74 additions and 1 deletions

View file

@ -49,6 +49,22 @@ pub unsafe extern "C" fn sp_bitmap_new(
NonNull::from(Box::leak(result)) NonNull::from(Box::leak(result))
} }
/// Creates a new [SPBitmap] with a size matching the screen.
///
/// returns: [SPBitmap] initialized to all pixels off. 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_bitmap_free].
#[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new_screen_sized() -> NonNull<SPBitmap> {
let result = Box::new(SPBitmap(servicepoint::Bitmap::max_sized()));
NonNull::from(Box::leak(result))
}
/// Loads a [SPBitmap] with the specified dimensions from the provided data. /// Loads a [SPBitmap] with the specified dimensions from the provided data.
/// ///
/// # Arguments /// # Arguments

View file

@ -65,6 +65,63 @@ pub unsafe extern "C" fn sp_packet_try_load(
} }
} }
/// Creates a raw [SPPacket] from parts.
///
/// # Arguments
///
/// - `command_code` specifies which command this packet contains
/// - `a`, `b`, `c` and `d` are command-specific header values
/// - `payload` is the optional data that is part of the command
/// - `payload_len` is the size of the payload
///
/// returns: new instance. Will never return null.
///
/// # Panics
///
/// - when `payload` is null, but `payload_len` is not zero
/// - when `payload_len` is zero, but `payload` is nonnull
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `payload` points to a valid memory region of at least `payload_len` bytes
/// - `payload` is not written to concurrently
/// - the returned [SPPacket] instance is freed in some way, either by using a consuming function or
/// by explicitly calling [sp_packet_free].
#[no_mangle]
pub unsafe extern "C" fn sp_packet_from_parts(
command_code: u16,
a: u16,
b: u16,
c: u16,
d: u16,
payload: *const u8,
payload_len: usize,
) -> NonNull<SPPacket> {
assert_eq!(payload.is_null(), payload_len == 0);
let payload = if payload.is_null() {
vec![]
} else {
let payload = std::slice::from_raw_parts(payload, payload_len);
Vec::from(payload)
};
let packet = servicepoint::packet::Packet {
header: servicepoint::packet::Header {
command_code,
a,
b,
c,
d,
},
payload,
};
let result = Box::new(SPPacket(packet));
NonNull::from(Box::leak(result))
}
/// Clones a [SPPacket]. /// Clones a [SPPacket].
/// ///
/// Will never return NULL. /// Will never return NULL.
@ -94,7 +151,7 @@ pub unsafe extern "C" fn sp_packet_clone(
/// ///
/// # Panics /// # Panics
/// ///
/// - when `sp_packet_free` is NULL /// - when `packet` is NULL
/// ///
/// # Safety /// # Safety
/// ///