Skip to content

Commit

Permalink
refactoring example
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Feb 11, 2024
1 parent 7cbae15 commit cb8e9d1
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
extern crate libc;
#![no_std]

use libc::{uint8_t, size_t};
use core::slice;

// Representing the C enum in Rust
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum ParseStatus {
ParseOk = 0,
ParseError = 1,
}

extern "C" {
pub fn parse_mbus(data: *const uint8_t, length: size_t) -> ParseStatus;
}


#[no_mangle]
pub extern "C" fn parse_mbus(data: *const uint8_t, length: size_t) -> ParseStatus {

if data.is_null() || length == 0 {
return ParseStatus::ParseError;
}

let slice = unsafe {
std::slice::from_raw_parts(data, length as usize)
slice::from_raw_parts(data, length as usize)
};

/* dummy code */
// Implement your parsing logic here
if slice.len() == 5 {
ParseStatus::ParseOk
} else {
Expand Down

0 comments on commit cb8e9d1

Please sign in to comment.