Skip to content

Commit

Permalink
Merge pull request #18 from Sympatron/unsound-pdinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sidcha committed Oct 11, 2024
2 parents 33f7076 + 931491e commit e770f7f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
2 changes: 1 addition & 1 deletion libosdp-sys/vendor
Submodule vendor updated 49 files
+1 −1 CMakeLists.txt
+3 −5 README.md
+6 −6 configure.sh
+1 −1 doc/api/command-structure.rst
+1 −1 doc/api/event-structure.rst
+63 −12 doc/libosdp/build-and-install.rst
+4 −2 doc/libosdp/debugging.rst
+4 −0 doc/osdpctl/index.rst
+1 −1 doc/protocol/commands-and-replies.rst
+12 −0 examples/c/Makefile
+2 −2 examples/c/cp_app.c
+2 −2 examples/c/pd_app.c
+10 −2 examples/cpp/Makefile
+11 −2 examples/cpp/cp_app.cpp
+10 −10 examples/cpp/pd_app.cpp
+2 −2 examples/python/cp_app.py
+14 −11 examples/python/pd_app.py
+73 −6 include/osdp.h
+3 −3 python/osdp/__init__.py
+8 −0 python/osdp/constants.py
+32 −7 python/osdp/control_panel.py
+8 −6 python/osdp/key_store.py
+1 −1 python/osdp/peripheral_device.py
+57 −0 python/osdp_sys/cp.c
+50 −0 python/osdp_sys/data.c
+9 −1 python/osdp_sys/module.c
+1 −0 python/osdp_sys/module.h
+6 −5 python/setup.py
+47 −0 scripts/run_tests.sh
+23 −0 shell.nix
+17 −9 src/CMakeLists.txt
+3 −1 src/crypto/openssl.c
+1 −1 src/osdp_common.c
+71 −6 src/osdp_common.h
+5 −3 src/osdp_config.h.in
+176 −96 src/osdp_cp.c
+0 −0 src/osdp_diag.c
+0 −0 src/osdp_diag.h
+160 −61 src/osdp_file.c
+7 −3 src/osdp_file.h
+32 −35 src/osdp_pd.c
+36 −26 src/osdp_phy.c
+0 −0 tests/pytest/_test_status.py
+5 −0 tests/pytest/run.sh
+24 −2 tests/pytest/test_commands.py
+62 −0 tests/pytest/test_data.py
+14 −7 tests/pytest/test_events.py
+10 −5 tests/unit-tests/CMakeLists.txt
+1 −1 utils
9 changes: 4 additions & 5 deletions libosdp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ where
trampoline::<F>
}

fn cp_setup(info: Vec<libosdp_sys::osdp_pd_info_t>) -> Result<*mut c_void> {
let ctx = unsafe { libosdp_sys::osdp_cp_setup(info.len() as i32, info.as_ptr()) };
fn cp_setup(info: Vec<crate::OsdpPdInfoHandle>) -> Result<*mut c_void> {
let ctx = unsafe { libosdp_sys::osdp_cp_setup(info.len() as i32, info.as_ptr() as *const _) };
if ctx.is_null() {
Err(OsdpError::Setup)
} else {
Expand All @@ -74,12 +74,11 @@ unsafe impl Send for ControlPanel {}

impl ControlPanel {
/// Create a new CP context for the list of PDs described by the [`PdInfo`] vector.
pub fn new(mut pd_info: Vec<PdInfo>) -> Result<Self> {
pub fn new(pd_info: Vec<PdInfo>) -> Result<Self> {
if pd_info.len() > 126 {
return Err(OsdpError::PdInfo("max PD count exceeded"));
}
let info: Vec<libosdp_sys::osdp_pd_info_t> =
pd_info.iter_mut().map(|i| i.as_struct()).collect();
let info: Vec<crate::OsdpPdInfoHandle> = pd_info.into_iter().map(|i| i.into()).collect();
unsafe { libosdp_sys::osdp_set_log_callback(Some(log_handler)) };
Ok(Self {
ctx: cp_setup(info)?,
Expand Down
5 changes: 3 additions & 2 deletions libosdp/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ where
trampoline::<F>
}

fn pd_setup(mut info: PdInfo) -> Result<*mut c_void> {
let ctx = unsafe { libosdp_sys::osdp_pd_setup(&info.as_struct()) };
fn pd_setup(info: PdInfo) -> Result<*mut c_void> {
let info: crate::OsdpPdInfoHandle = info.into();
let ctx = unsafe { libosdp_sys::osdp_pd_setup(&*info) };
if ctx.is_null() {
Err(OsdpError::Setup)
} else {
Expand Down
81 changes: 64 additions & 17 deletions libosdp/src/pdinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//
// SPDX-License-Identifier: Apache-2.0

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

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

/// OSDP PD Information. This struct is used to describe a PD to LibOSDP
Expand Down Expand Up @@ -247,24 +247,71 @@ impl PdInfoBuilder {
}
}

impl PdInfo {
/// Get a C-repr struct for PdInfo that LibOSDP can operate on.
pub fn as_struct(&mut self) -> libosdp_sys::osdp_pd_info_t {
let scbk;
if let Some(key) = self.scbk.as_mut() {
scbk = key.as_mut_ptr();
#[repr(transparent)]
pub(crate) struct OsdpPdInfoHandle(pub libosdp_sys::osdp_pd_info_t);

impl Deref for OsdpPdInfoHandle {
type Target = libosdp_sys::osdp_pd_info_t;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<PdInfo> for OsdpPdInfoHandle {
fn from(info: PdInfo) -> OsdpPdInfoHandle {
let scbk = if let Some(key) = info.scbk {
Box::into_raw(Box::new(key)) as *mut _
} else {
scbk = core::ptr::null_mut::<u8>();
}
libosdp_sys::osdp_pd_info_t {
name: self.name.as_ptr(),
baud_rate: self.baud_rate,
address: self.address,
flags: self.flags.bits() as i32,
id: self.id.into(),
cap: self.cap.as_ptr(),
channel: self.channel.take().unwrap().into(),
core::ptr::null_mut::<u8>()
};
let cap = if !info.cap.is_empty() {
let mut cap = info.cap.clone();
cap.reserve(1);
cap.push(libosdp_sys::osdp_pd_cap {
function_code: -1i8 as u8,
compliance_level: 0,
num_items: 0,
});
Box::into_raw(cap.into_boxed_slice()) as *mut _
} else {
core::ptr::null_mut::<libosdp_sys::osdp_pd_cap>()
};
OsdpPdInfoHandle(libosdp_sys::osdp_pd_info_t {
name: info.name.clone().into_raw(),
baud_rate: info.baud_rate,
address: info.address,
flags: info.flags.bits() as i32,
id: info.id.into(),
cap: cap as *mut _,
channel: info.channel.unwrap().into(),
scbk,
})
}
}

impl Drop for OsdpPdInfoHandle {
fn drop(&mut self) {
unsafe {
let info = self.0;
if !info.name.is_null() {
drop(CString::from_raw(info.name as *mut _));
}
if !info.cap.is_null() {
let mut cap = info.cap as *mut libosdp_sys::osdp_pd_cap;
while (*cap).function_code != -1i8 as u8 {
cap = cap.add(1);
}
let len = (cap.offset_from(info.cap) + 1) as usize;
drop(Vec::from_raw_parts(
info.cap as *mut libosdp_sys::osdp_pd_cap,
len,
len,
));
}
if !info.scbk.is_null() {
drop(Box::from_raw(info.scbk as *mut [u8; 16]));
}
}
}
}

0 comments on commit e770f7f

Please sign in to comment.