Skip to content

Commit

Permalink
first steps towards a c lib
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Feb 11, 2024
1 parent f08deb2 commit 7cbae15
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
edition = "2021"
crate-type = ["rlib","staticlib", "cdylib"]

[dependencies]
libc = "0.2"

[dev-dependencies]
criterion = "0.5.1"
walkdir = "2.3.1"
Expand Down
9 changes: 9 additions & 0 deletions include/mbus_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ extern "C" {
}
#endif


typedef enum {
ParseOk,
ParseError,
} ParseStatus;


ParseStatus parse_mbus(const uint8_t* data, size_t length);

#endif // MBUS_PARSER_H
34 changes: 34 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
extern crate libc;
use libc::{uint8_t, size_t};

// 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)
};

/* dummy code */
if slice.len() == 5 {
ParseStatus::ParseOk
} else {
ParseStatus::ParseError
}
}

0 comments on commit 7cbae15

Please sign in to comment.