Skip to content

Commit

Permalink
Make libosdp no_std compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Sympatron committed Sep 27, 2024
1 parent 13154da commit 13a698f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 16 deletions.
10 changes: 6 additions & 4 deletions libosdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ categories = ["development-tools", "embedded"]

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

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

[features]
default = ["std"]
std = ["thiserror"]
arc = ["once_cell"]
std = ["libosdp-sys/std", "thiserror", "serde/std", "once_cell/std", "arc"]

[[example]]
name = "cp"
Expand Down
13 changes: 13 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 crate::{vec, Box};
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,17 @@ 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() {
//TODO determine if this is the correct error kind
// embedded_io::ErrorKind::TimedOut => ChannelError::WouldBlock,
_ => 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
2 changes: 1 addition & 1 deletion libosdp/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! are specified by OSDP specification. This module is responsible to handling
//! such commands though [`OsdpCommand`].
use crate::OsdpStatusReport;
use crate::{OsdpStatusReport, Vec};
use serde::{Deserialize, Serialize};

use super::ConvertEndian;
Expand Down
4 changes: 2 additions & 2 deletions libosdp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
//! 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,
file::OsdpFileOps, Box, OsdpCommand, OsdpError, OsdpEvent, OsdpFlag, PdCapability, PdId,
PdInfo, Vec,
};
use core::ffi::c_void;
use log::{debug, error, info, warn};
Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! etc.,). They do this by creating an "event" and sending it to the CP. This
//! module is responsible to handling such events though [`OsdpEvent`].
use crate::OsdpError;
use crate::{OsdpError, 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 crate::{vec, Box};
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
10 changes: 8 additions & 2 deletions libosdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ pub use pdcap::*;
pub use pdid::*;
pub use pdinfo::*;

#[cfg(feature = "arc")]
use alloc::sync::Arc;
#[allow(unused_imports)]
use alloc::{
borrow::ToOwned, boxed::Box, ffi::CString, format, str::FromStr, string::String, sync::Arc,
vec, vec::Vec,
borrow::ToOwned, boxed::Box, ffi::CString, format, str::FromStr, string::String, vec, vec::Vec,
};
#[cfg(feature = "arc")]
use once_cell::sync::Lazy;
#[cfg(feature = "std")]
use thiserror::Error;
Expand Down Expand Up @@ -216,22 +218,26 @@ fn cstr_to_string(s: *const ::core::ffi::c_char) -> String {
s.to_str().unwrap().to_owned()
}

#[cfg(feature = "arc")]
static VERSION: Lazy<Arc<String>> = Lazy::new(|| {
let s = unsafe { libosdp_sys::osdp_get_version() };
Arc::new(cstr_to_string(s))
});

#[cfg(feature = "arc")]
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
#[cfg(feature = "arc")]
pub fn get_version() -> String {
VERSION.as_ref().clone()
}

/// Get LibOSDP source info string
#[cfg(feature = "arc")]
pub fn get_source_info() -> String {
SOURCE_INFO.as_ref().clone()
}
2 changes: 1 addition & 1 deletion libosdp/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! happens on the PD itself (such as card read, key press, etc.,) snd sends it
//! to the CP.
use crate::{OsdpCommand, OsdpError, OsdpEvent, OsdpFileOps, PdCapability, PdInfo};
use crate::{Box, OsdpCommand, OsdpError, OsdpEvent, OsdpFileOps, PdCapability, PdInfo, Vec};
use core::ffi::c_void;
use log::{debug, error, info, warn};

Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/pdcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use core::str::FromStr;

use crate::OsdpError;
use crate::{format, OsdpError};

/// PD capability entity to be used inside [`PdCapability`]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
Expand Down
4 changes: 2 additions & 2 deletions libosdp/src/pdinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use alloc::ffi::CString;

use crate::{Channel, OsdpError, OsdpFlag, PdCapability, PdId};
use crate::{format, Box, Channel, OsdpError, OsdpFlag, PdCapability, PdId, String, Vec};

/// OSDP PD Information. This struct is used to describe a PD to LibOSDP
#[derive(Debug, Default)]
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 13a698f

Please sign in to comment.