Skip to content

Commit

Permalink
Merge pull request #17 from Sympatron/no_std
Browse files Browse the repository at this point in the history
Make libosdp no_std compatible
  • Loading branch information
sidcha authored Oct 2, 2024
2 parents 8b8416c + 689ec95 commit 33f7076
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 32 deletions.
2 changes: 0 additions & 2 deletions libosdp-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ build-target = "0.4.0"
cc = "1.0.83"

[features]
default = ["std"]
std = []
packet_trace = []
data_trace = []
skip_mark_byte = []
2 changes: 1 addition & 1 deletion libosdp-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
//! Auto generated (bindgen) wrapper for LibOSDP C API exposed from osdp.h
//! [here](https://github.com/goToMain/libosdp/blob/master/include/osdp.h).

Expand Down
8 changes: 4 additions & 4 deletions libosdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ categories = ["development-tools", "embedded"]

[dependencies]
bitflags = "2.4.0"
libosdp-sys = "3.0.6"
embedded-io = { version = "0.6.1", features = ["alloc"] }
libosdp-sys = { path = "../libosdp-sys", default-features = false }
log = "0.4.20"
once_cell = "1.18.0"
serde = { version = "1.0.192", features = ["derive"] }
serde = { version = "1.0.192", features = ["derive", "alloc"], default-features = false }
thiserror = { version = "1.0.50", optional = true }

[dev-dependencies]
Expand All @@ -29,7 +29,7 @@ sha256 = "1.5.0"

[features]
default = ["std"]
std = ["thiserror"]
std = ["thiserror", "serde/std"]

[[example]]
name = "cp"
Expand Down
11 changes: 11 additions & 0 deletions libosdp/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//! This module provides a way to define an OSDP channel and export it to
//! LibOSDP.

use alloc::{boxed::Box, vec};
use core::ffi::c_void;

/// OSDP channel errors
Expand All @@ -29,6 +30,7 @@ pub enum ChannelError {
TransportError,
}

#[cfg(feature = "std")]
impl From<std::io::Error> for ChannelError {
fn from(value: std::io::Error) -> Self {
match value.kind() {
Expand All @@ -38,6 +40,15 @@ impl From<std::io::Error> for ChannelError {
}
}

#[cfg(not(feature = "std"))]
impl<E: embedded_io::Error + Sized> From<E> for ChannelError {
fn from(value: E) -> Self {
match value.kind() {
_ => ChannelError::TransportError,
}
}
}

/// The Channel trait acts as an interface for all channel implementors. See
/// module description for the definition of a "channel" in LibOSDP.
pub trait Channel: Send + Sync {
Expand Down
1 change: 1 addition & 0 deletions libosdp/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! such commands though [`OsdpCommand`].

use crate::OsdpStatusReport;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use super::ConvertEndian;
Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//! The Control Panel (CP) is responsible to connecting to and managing multiple Peripheral Devices
//! (PD) on the OSDP bus. It can send commands to and receive events from PDs.

#[cfg(feature = "std")]
use crate::{
file::OsdpFileOps, OsdpCommand, OsdpError, OsdpEvent, OsdpFlag, PdCapability, PdId, PdInfo,
};
use alloc::{boxed::Box, vec::Vec};
use core::ffi::c_void;
use log::{debug, error, info, warn};

Expand Down
1 change: 1 addition & 0 deletions libosdp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! module is responsible to handling such events though [`OsdpEvent`].

use crate::OsdpError;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use super::ConvertEndian;
Expand Down
5 changes: 3 additions & 2 deletions libosdp/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! OSDP provides a means to send files from CP to a Peripheral Device (PD).
//! This module adds the required components to achieve this effect.

use alloc::{boxed::Box, vec};
use core::ffi::c_void;

type Result<T> = core::result::Result<T, crate::OsdpError>;
Expand Down Expand Up @@ -57,7 +58,7 @@ unsafe extern "C" fn file_read(data: *mut c_void, buf: *mut c_void, size: i32, o
-1
}
};
std::ptr::copy_nonoverlapping(read_buf.as_mut_ptr(), buf as *mut u8, len as usize);
core::ptr::copy_nonoverlapping(read_buf.as_mut_ptr(), buf as *mut u8, len as usize);
len
}

Expand All @@ -70,7 +71,7 @@ unsafe extern "C" fn file_write(
let ctx: *mut Box<dyn OsdpFileOps> = data as *mut _;
let ctx = ctx.as_ref().unwrap();
let mut write_buf = vec![0u8; size as usize];
std::ptr::copy_nonoverlapping(buf as *mut u8, write_buf.as_mut_ptr(), size as usize);
core::ptr::copy_nonoverlapping(buf as *mut u8, write_buf.as_mut_ptr(), size as usize);
match ctx.offset_write(&write_buf, offset as u64) {
Ok(len) => len as i32,
Err(e) => {
Expand Down
31 changes: 11 additions & 20 deletions libosdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ pub use pdid::*;
pub use pdinfo::*;

#[allow(unused_imports)]
use alloc::{
borrow::ToOwned, boxed::Box, ffi::CString, format, str::FromStr, string::String, sync::Arc,
vec, vec::Vec,
};
use once_cell::sync::Lazy;
use alloc::{borrow::ToOwned, boxed::Box, format, string::String};

#[cfg(feature = "std")]
use thiserror::Error;

Expand Down Expand Up @@ -198,7 +195,7 @@ bitflags::bitflags! {
}
}

impl FromStr for OsdpFlag {
impl core::str::FromStr for OsdpFlag {
type Err = OsdpError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -216,22 +213,16 @@ fn cstr_to_string(s: *const ::core::ffi::c_char) -> String {
s.to_str().unwrap().to_owned()
}

static VERSION: Lazy<Arc<String>> = Lazy::new(|| {
let s = unsafe { libosdp_sys::osdp_get_version() };
Arc::new(cstr_to_string(s))
});

static SOURCE_INFO: Lazy<Arc<String>> = Lazy::new(|| {
let s = unsafe { libosdp_sys::osdp_get_source_info() };
Arc::new(cstr_to_string(s))
});

/// Get LibOSDP version
pub fn get_version() -> String {
VERSION.as_ref().clone()
pub fn get_version() -> &'static str {
let s = unsafe { libosdp_sys::osdp_get_version() };
let s = unsafe { core::ffi::CStr::from_ptr(s) };
s.to_str().unwrap()
}

/// Get LibOSDP source info string
pub fn get_source_info() -> String {
SOURCE_INFO.as_ref().clone()
pub fn get_source_info() -> &'static str {
let s = unsafe { libosdp_sys::osdp_get_source_info() };
let s = unsafe { core::ffi::CStr::from_ptr(s) };
s.to_str().unwrap()
}
1 change: 1 addition & 0 deletions libosdp/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! to the CP.

use crate::{OsdpCommand, OsdpError, OsdpEvent, OsdpFileOps, PdCapability, PdInfo};
use alloc::{boxed::Box, vec::Vec};
use core::ffi::c_void;
use log::{debug, error, info, warn};

Expand Down
1 change: 1 addition & 0 deletions libosdp/src/pdcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//
// SPDX-License-Identifier: Apache-2.0

use alloc::format;
use core::str::FromStr;

use crate::OsdpError;
Expand Down
4 changes: 2 additions & 2 deletions libosdp/src/pdinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// SPDX-License-Identifier: Apache-2.0

use alloc::ffi::CString;
use alloc::{boxed::Box, ffi::CString, format, string::String, vec::Vec};

use crate::{Channel, OsdpError, OsdpFlag, PdCapability, PdId};

Expand Down Expand Up @@ -254,7 +254,7 @@ impl PdInfo {
if let Some(key) = self.scbk.as_mut() {
scbk = key.as_mut_ptr();
} else {
scbk = std::ptr::null_mut::<u8>();
scbk = core::ptr::null_mut::<u8>();
}
libosdp_sys::osdp_pd_info_t {
name: self.name.as_ptr(),
Expand Down

0 comments on commit 33f7076

Please sign in to comment.