diff --git a/libosdp-sys/Cargo.toml b/libosdp-sys/Cargo.toml index 0181ef1..38753dd 100644 --- a/libosdp-sys/Cargo.toml +++ b/libosdp-sys/Cargo.toml @@ -19,8 +19,6 @@ build-target = "0.4.0" cc = "1.0.83" [features] -default = ["std"] -std = [] packet_trace = [] data_trace = [] skip_mark_byte = [] diff --git a/libosdp-sys/src/lib.rs b/libosdp-sys/src/lib.rs index 44945f4..2016d07 100644 --- a/libosdp-sys/src/lib.rs +++ b/libosdp-sys/src/lib.rs @@ -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). diff --git a/libosdp/Cargo.toml b/libosdp/Cargo.toml index e1eaa98..3e760a1 100644 --- a/libosdp/Cargo.toml +++ b/libosdp/Cargo.toml @@ -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] @@ -29,7 +29,7 @@ sha256 = "1.5.0" [features] default = ["std"] -std = ["thiserror"] +std = ["thiserror", "serde/std"] [[example]] name = "cp" diff --git a/libosdp/src/channel.rs b/libosdp/src/channel.rs index 3e89694..eace4f7 100644 --- a/libosdp/src/channel.rs +++ b/libosdp/src/channel.rs @@ -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 @@ -29,6 +30,7 @@ pub enum ChannelError { TransportError, } +#[cfg(feature = "std")] impl From for ChannelError { fn from(value: std::io::Error) -> Self { match value.kind() { @@ -38,6 +40,15 @@ impl From for ChannelError { } } +#[cfg(not(feature = "std"))] +impl From 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 { diff --git a/libosdp/src/commands.rs b/libosdp/src/commands.rs index 43af879..2945fc5 100644 --- a/libosdp/src/commands.rs +++ b/libosdp/src/commands.rs @@ -8,6 +8,7 @@ //! such commands though [`OsdpCommand`]. use crate::OsdpStatusReport; +use alloc::vec::Vec; use serde::{Deserialize, Serialize}; use super::ConvertEndian; diff --git a/libosdp/src/cp.rs b/libosdp/src/cp.rs index 0a611b8..e05e9b5 100644 --- a/libosdp/src/cp.rs +++ b/libosdp/src/cp.rs @@ -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}; diff --git a/libosdp/src/events.rs b/libosdp/src/events.rs index 1056322..5438d1d 100644 --- a/libosdp/src/events.rs +++ b/libosdp/src/events.rs @@ -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; diff --git a/libosdp/src/file.rs b/libosdp/src/file.rs index d107fe8..477776c 100644 --- a/libosdp/src/file.rs +++ b/libosdp/src/file.rs @@ -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 = core::result::Result; @@ -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 } @@ -70,7 +71,7 @@ unsafe extern "C" fn file_write( let ctx: *mut Box = 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) => { diff --git a/libosdp/src/lib.rs b/libosdp/src/lib.rs index 166c6a8..f22d726 100644 --- a/libosdp/src/lib.rs +++ b/libosdp/src/lib.rs @@ -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; @@ -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 { @@ -216,22 +213,16 @@ fn cstr_to_string(s: *const ::core::ffi::c_char) -> String { s.to_str().unwrap().to_owned() } -static VERSION: Lazy> = Lazy::new(|| { - let s = unsafe { libosdp_sys::osdp_get_version() }; - Arc::new(cstr_to_string(s)) -}); - -static SOURCE_INFO: Lazy> = 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() } diff --git a/libosdp/src/pd.rs b/libosdp/src/pd.rs index e341788..4180afe 100644 --- a/libosdp/src/pd.rs +++ b/libosdp/src/pd.rs @@ -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}; diff --git a/libosdp/src/pdcap.rs b/libosdp/src/pdcap.rs index 5fa19d9..d17adb0 100644 --- a/libosdp/src/pdcap.rs +++ b/libosdp/src/pdcap.rs @@ -3,6 +3,7 @@ // // SPDX-License-Identifier: Apache-2.0 +use alloc::format; use core::str::FromStr; use crate::OsdpError; diff --git a/libosdp/src/pdinfo.rs b/libosdp/src/pdinfo.rs index cb4ba29..95be5af 100644 --- a/libosdp/src/pdinfo.rs +++ b/libosdp/src/pdinfo.rs @@ -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}; @@ -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::(); + scbk = core::ptr::null_mut::(); } libosdp_sys::osdp_pd_info_t { name: self.name.as_ptr(),