Skip to content

Commit ac83e92

Browse files
authoredOct 31, 2024··
Merge pull request #25 from Sympatron/size
Optimize size for embedded devices
2 parents 5dc816c + ff5a16a commit ac83e92

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed
 

‎libosdp/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ categories = ["development-tools", "embedded"]
1616
bitflags = "2.4.0"
1717
embedded-io = { version = "0.6.1", features = ["alloc"] }
1818
libosdp-sys = { path = "../libosdp-sys", default-features = false }
19-
log = "0.4.20"
19+
log = { version = "0.4.20", optional = true }
2020
serde = { version = "1.0.192", features = ["derive", "alloc"], default-features = false }
2121
thiserror = { version = "1.0.50", optional = true }
2222
defmt = { version = "0.3", optional = true, features = ["alloc"] }
23+
itoa = "1.0.11"
2324

2425
[dev-dependencies]
2526
env_logger = "0.11.3"
@@ -31,7 +32,8 @@ sha256 = "1.5.0"
3132
[features]
3233
default = ["std"]
3334
defmt-03 = ["embedded-io/defmt-03", "dep:defmt"]
34-
std = ["thiserror", "serde/std", "log/std"]
35+
log = ["dep:log"]
36+
std = ["thiserror", "serde/std", "log", "log/std"]
3537

3638
[[example]]
3739
name = "cp"

‎libosdp/src/cp.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,33 @@ use alloc::{boxed::Box, vec::Vec};
1414
use core::ffi::c_void;
1515
#[cfg(feature = "defmt-03")]
1616
use defmt::{debug, error, info, warn};
17-
#[cfg(not(feature = "defmt-03"))]
17+
#[cfg(all(feature = "log", not(feature = "defmt-03")))]
1818
use log::{debug, error, info, warn};
1919

2020
type Result<T> = core::result::Result<T, OsdpError>;
2121

2222
unsafe extern "C" fn log_handler(
23-
log_level: ::core::ffi::c_int,
23+
_log_level: ::core::ffi::c_int,
2424
_file: *const ::core::ffi::c_char,
2525
_line: ::core::ffi::c_ulong,
26-
msg: *const ::core::ffi::c_char,
26+
_msg: *const ::core::ffi::c_char,
2727
) {
28-
let msg = crate::cstr_to_string(msg);
29-
let msg = msg.trim();
30-
match log_level as libosdp_sys::osdp_log_level_e {
31-
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("CP: {}", msg),
32-
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("CP: {}", msg),
33-
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("CP: {}", msg),
34-
libosdp_sys::osdp_log_level_e_OSDP_LOG_ERROR => error!("CP: {}", msg),
35-
libosdp_sys::osdp_log_level_e_OSDP_LOG_WARNING => warn!("CP: {}", msg),
36-
libosdp_sys::osdp_log_level_e_OSDP_LOG_NOTICE => warn!("CP: {}", msg),
37-
libosdp_sys::osdp_log_level_e_OSDP_LOG_INFO => info!("CP: {}", msg),
38-
libosdp_sys::osdp_log_level_e_OSDP_LOG_DEBUG => debug!("CP: {}", msg),
39-
_ => panic!("Unknown log level"),
40-
};
28+
#[cfg(any(feature = "log", feature = "defmt-03"))]
29+
{
30+
let msg = crate::cstr_to_string(_msg);
31+
let msg = msg.trim();
32+
match _log_level as libosdp_sys::osdp_log_level_e {
33+
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("CP: {}", msg),
34+
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("CP: {}", msg),
35+
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("CP: {}", msg),
36+
libosdp_sys::osdp_log_level_e_OSDP_LOG_ERROR => error!("CP: {}", msg),
37+
libosdp_sys::osdp_log_level_e_OSDP_LOG_WARNING => warn!("CP: {}", msg),
38+
libosdp_sys::osdp_log_level_e_OSDP_LOG_NOTICE => warn!("CP: {}", msg),
39+
libosdp_sys::osdp_log_level_e_OSDP_LOG_INFO => info!("CP: {}", msg),
40+
libosdp_sys::osdp_log_level_e_OSDP_LOG_DEBUG => debug!("CP: {}", msg),
41+
_ => panic!("Unknown log level"),
42+
};
43+
}
4144
}
4245

4346
extern "C" fn trampoline<F>(data: *mut c_void, pd: i32, event: *mut libosdp_sys::osdp_event) -> i32

‎libosdp/src/file.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use alloc::{boxed::Box, vec};
1010
use core::ffi::c_void;
1111
#[cfg(feature = "defmt-03")]
1212
use defmt::error;
13-
#[cfg(not(feature = "defmt-03"))]
13+
#[cfg(all(feature = "log", not(feature = "defmt-03")))]
1414
use log::error;
1515

1616
type Result<T> = core::result::Result<T, crate::OsdpError>;
@@ -44,8 +44,9 @@ unsafe extern "C" fn file_open(data: *mut c_void, file_id: i32, size: *mut i32)
4444
}
4545
0
4646
}
47-
Err(e) => {
48-
error!("open: {:?}", e);
47+
Err(_e) => {
48+
#[cfg(any(feature = "log", feature = "defmt-03"))]
49+
error!("open: {:?}", _e);
4950
-1
5051
}
5152
}
@@ -57,8 +58,9 @@ unsafe extern "C" fn file_read(data: *mut c_void, buf: *mut c_void, size: i32, o
5758
let mut read_buf = vec![0u8; size as usize];
5859
let len = match ctx.offset_read(&mut read_buf, offset as u64) {
5960
Ok(len) => len as i32,
60-
Err(e) => {
61-
error!("file_read: {:?}", e);
61+
Err(_e) => {
62+
#[cfg(any(feature = "log", feature = "defmt-03"))]
63+
error!("file_read: {:?}", _e);
6264
-1
6365
}
6466
};
@@ -78,8 +80,9 @@ unsafe extern "C" fn file_write(
7880
core::ptr::copy_nonoverlapping(buf as *mut u8, write_buf.as_mut_ptr(), size as usize);
7981
match ctx.offset_write(&write_buf, offset as u64) {
8082
Ok(len) => len as i32,
81-
Err(e) => {
82-
error!("file_write: {:?}", e);
83+
Err(_e) => {
84+
#[cfg(any(feature = "log", feature = "defmt-03"))]
85+
error!("file_write: {:?}", _e);
8386
-1
8487
}
8588
}
@@ -90,8 +93,9 @@ unsafe extern "C" fn file_close(data: *mut c_void) -> i32 {
9093
let ctx = ctx.as_mut().unwrap();
9194
match ctx.close() {
9295
Ok(_) => 0,
93-
Err(e) => {
94-
error!("file_close: {:?}", e);
96+
Err(_e) => {
97+
#[cfg(any(feature = "log", feature = "defmt-03"))]
98+
error!("file_close: {:?}", _e);
9599
-1
96100
}
97101
}

‎libosdp/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ impl core::str::FromStr for OsdpFlag {
227227
}
228228
}
229229

230+
#[allow(dead_code)]
230231
fn cstr_to_string(s: *const ::core::ffi::c_char) -> String {
231232
let s = unsafe { core::ffi::CStr::from_ptr(s) };
232233
s.to_str().unwrap().to_owned()

‎libosdp/src/pd.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,41 @@
1313
//! to the CP.
1414
1515
use crate::{
16-
Box, Channel, OsdpCommand, OsdpError, OsdpEvent, OsdpFileOps, PdCapability, PdInfo,
17-
PdInfoBuilder,
16+
Channel, OsdpCommand, OsdpError, OsdpEvent, OsdpFileOps, PdCapability, PdInfo, PdInfoBuilder,
1817
};
18+
use alloc::{boxed::Box, vec::Vec};
1919
use core::ffi::c_void;
2020
#[cfg(feature = "defmt-03")]
2121
use defmt::{debug, error, info, warn};
22-
#[cfg(not(feature = "defmt-03"))]
22+
#[cfg(all(feature = "log", not(feature = "defmt-03")))]
2323
use log::{debug, error, info, warn};
2424

2525
type Result<T> = core::result::Result<T, OsdpError>;
2626
type CommandCallback =
2727
unsafe extern "C" fn(data: *mut c_void, event: *mut libosdp_sys::osdp_cmd) -> i32;
2828

2929
unsafe extern "C" fn log_handler(
30-
log_level: ::core::ffi::c_int,
30+
_log_level: ::core::ffi::c_int,
3131
_file: *const ::core::ffi::c_char,
3232
_line: ::core::ffi::c_ulong,
33-
msg: *const ::core::ffi::c_char,
33+
_msg: *const ::core::ffi::c_char,
3434
) {
35-
let msg = crate::cstr_to_string(msg);
36-
let msg = msg.trim();
37-
match log_level as libosdp_sys::osdp_log_level_e {
38-
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("PD: {}", msg),
39-
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("PD: {}", msg),
40-
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("PD: {}", msg),
41-
libosdp_sys::osdp_log_level_e_OSDP_LOG_ERROR => error!("PD: {}", msg),
42-
libosdp_sys::osdp_log_level_e_OSDP_LOG_WARNING => warn!("PD: {}", msg),
43-
libosdp_sys::osdp_log_level_e_OSDP_LOG_NOTICE => warn!("PD: {}", msg),
44-
libosdp_sys::osdp_log_level_e_OSDP_LOG_INFO => info!("PD: {}", msg),
45-
libosdp_sys::osdp_log_level_e_OSDP_LOG_DEBUG => debug!("PD: {}", msg),
46-
_ => panic!("Unknown log level"),
47-
};
35+
#[cfg(any(feature = "log", feature = "defmt-03"))]
36+
{
37+
let msg = crate::cstr_to_string(_msg);
38+
let msg = msg.trim();
39+
match _log_level as libosdp_sys::osdp_log_level_e {
40+
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("PD: {}", msg),
41+
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("PD: {}", msg),
42+
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("PD: {}", msg),
43+
libosdp_sys::osdp_log_level_e_OSDP_LOG_ERROR => error!("PD: {}", msg),
44+
libosdp_sys::osdp_log_level_e_OSDP_LOG_WARNING => warn!("PD: {}", msg),
45+
libosdp_sys::osdp_log_level_e_OSDP_LOG_NOTICE => warn!("PD: {}", msg),
46+
libosdp_sys::osdp_log_level_e_OSDP_LOG_INFO => info!("PD: {}", msg),
47+
libosdp_sys::osdp_log_level_e_OSDP_LOG_DEBUG => debug!("PD: {}", msg),
48+
_ => panic!("Unknown log level"),
49+
};
50+
}
4851
}
4952

5053
extern "C" fn trampoline<F>(data: *mut c_void, cmd: *mut libosdp_sys::osdp_cmd) -> i32

‎libosdp/src/pdinfo.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// SPDX-License-Identifier: Apache-2.0
55

66
use crate::{OsdpError, OsdpFlag, PdCapability, PdId};
7-
use alloc::{boxed::Box, ffi::CString, format, string::String, vec::Vec};
7+
use alloc::{boxed::Box, ffi::CString, string::String, vec::Vec};
88
use core::ops::Deref;
99

1010
/// OSDP PD Information. This struct is used to describe a PD to LibOSDP
@@ -163,7 +163,7 @@ impl PdInfoBuilder {
163163
/// Set 7 bit PD address; the special address 0x7F is used for broadcast. So
164164
/// there can be 2^7-1 valid addresses on a bus.
165165
pub fn address(mut self, address: i32) -> Result<PdInfoBuilder, OsdpError> {
166-
if address > 126 {
166+
if address < 0 || address > 126 {
167167
return Err(OsdpError::PdInfoBuilder("invalid address"));
168168
}
169169
self.address = address;
@@ -231,9 +231,17 @@ impl PdInfoBuilder {
231231

232232
/// Finalize the PdInfo from the current builder
233233
pub fn build(self) -> PdInfo {
234-
let name = self
235-
.name
236-
.unwrap_or_else(|| CString::new(format!("PD-{}", self.address)).unwrap());
234+
let name = self.name.unwrap_or_else(|| {
235+
let mut buffer = itoa::Buffer::new();
236+
let s = buffer.format(self.address as u8);
237+
let mut buf = [0u8; 6];
238+
let buf = &mut buf[..3 + s.len()];
239+
buf[..3].copy_from_slice(b"PD-");
240+
buf[3..].copy_from_slice(s.as_bytes());
241+
CString::new(buf)
242+
.ok() // panic can only happen if buf contained a null byte, which it never will
243+
.expect("fallback PD name could not be constrcuted")
244+
});
237245
PdInfo {
238246
name,
239247
address: self.address,

0 commit comments

Comments
 (0)
Please sign in to comment.