add command code constants, send header

This commit is contained in:
Vinzenz Schroeter 2025-04-12 21:47:23 +02:00
commit ce70ecd9e2
12 changed files with 141 additions and 60 deletions

View file

@ -1,5 +1,5 @@
use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
use servicepoint::{Header, Packet, TypedCommand};
use servicepoint::{CommandCode, Header, Packet, TypedCommand};
use std::ptr::NonNull;
/// Turns a [TypedCommand] into a [Packet].
@ -65,7 +65,7 @@ pub unsafe extern "C" fn sp_packet_get_header(
pub unsafe extern "C" fn sp_packet_get_payload(
packet: NonNull<Packet>,
) -> ByteSlice {
unsafe { ByteSlice::from_slice(&mut *(*packet.as_ptr()).payload) }
unsafe { ByteSlice::from_slice(&mut (*packet.as_ptr()).payload) }
}
/// Sets the payload of the provided packet to the provided data.
@ -87,7 +87,7 @@ pub unsafe extern "C" fn sp_packet_set_payload(
#[no_mangle]
pub unsafe extern "C" fn sp_packet_serialize_to(
packet: NonNull<Packet>,
buffer: ByteSlice,
mut buffer: ByteSlice,
) {
unsafe {
packet.as_ref().serialize_to(buffer.as_slice_mut());
@ -107,3 +107,19 @@ pub unsafe extern "C" fn sp_packet_clone(
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
unsafe { heap_drop(packet) }
}
#[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,
}
}