Skip to content

Commit

Permalink
libosdp: Do not rely on a concrete type for enums
Browse files Browse the repository at this point in the history
Windows treats enums as i32 while unix treats enums as u32. This is not
an issue for C/C++ code but it certainly is for Rust code that interact
with them. Fix this by not replying either of those concrete types and
using the bindgen generated type for enums.

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Mar 14, 2024
1 parent 4f52546 commit b5e852e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libosdp/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub enum OsdpLedColor {

impl From<u8> for OsdpLedColor {
fn from(value: u8) -> Self {
match value as u32 {
match value as libosdp_sys::osdp_led_color_e {
libosdp_sys::osdp_led_color_e_OSDP_LED_COLOR_NONE => OsdpLedColor::None,
libosdp_sys::osdp_led_color_e_OSDP_LED_COLOR_RED => OsdpLedColor::Red,
libosdp_sys::osdp_led_color_e_OSDP_LED_COLOR_GREEN => OsdpLedColor::Green,
Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unsafe extern "C" fn log_handler(
) {
let msg = crate::cstr_to_string(msg);
let msg = msg.trim();
match log_level as u32 {
match log_level as libosdp_sys::osdp_log_level_e {
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("CP: {msg}"),
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("CP: {msg}"),
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("CP: {msg}"),
Expand Down
12 changes: 6 additions & 6 deletions libosdp/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub enum OsdpCardFormats {
Ascii,
}

impl From<u32> for OsdpCardFormats {
fn from(value: u32) -> Self {
impl From<libosdp_sys::osdp_event_cardread_format_e> for OsdpCardFormats {
fn from(value: libosdp_sys::osdp_event_cardread_format_e) -> Self {
match value {
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_UNSPECIFIED => {
OsdpCardFormats::Unspecified
Expand All @@ -45,7 +45,7 @@ impl From<u32> for OsdpCardFormats {
}
}

impl From<OsdpCardFormats> for u32 {
impl From<OsdpCardFormats> for libosdp_sys::osdp_event_cardread_format_e {
fn from(val: OsdpCardFormats) -> Self {
match val {
OsdpCardFormats::Unspecified => {
Expand Down Expand Up @@ -252,8 +252,8 @@ pub enum OsdpStatusReportType {
Local,
}

impl From<u32> for OsdpStatusReportType {
fn from(value: u32) -> Self {
impl From<libosdp_sys::osdp_status_report_type> for OsdpStatusReportType {
fn from(value: libosdp_sys::osdp_status_report_type) -> Self {
match value {
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_INPUT => {
OsdpStatusReportType::Input
Expand All @@ -272,7 +272,7 @@ impl From<u32> for OsdpStatusReportType {
}
}

impl From<OsdpStatusReportType> for u32 {
impl From<OsdpStatusReportType> for libosdp_sys::osdp_status_report_type {
fn from(value: OsdpStatusReportType) -> Self {
match value {
OsdpStatusReportType::Input => {
Expand Down
36 changes: 33 additions & 3 deletions libosdp/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,40 @@
//! This module adds the required components to achieve this effect.

use crate::OsdpError;
use std::{ffi::c_void, fs::File, os::unix::prelude::FileExt, path::PathBuf};
use std::{ffi::c_void, fs::File, path::PathBuf};

#[cfg(not(target_os = "windows"))]
use std::os::unix::prelude::FileExt;
#[cfg(target_os = "windows")]
use std::os::windows::fs::FileExt;

type Result<T> = std::result::Result<T, OsdpError>;

trait OffsetRead {
fn pread(&self, buf: &mut [u8], offset: u64) -> std::io::Result<usize>;
fn pwrite(&self, buf: &[u8], offset: u64) -> std::io::Result<usize>;
}

impl OffsetRead for std::fs::File {
#[inline(always)]
fn pread(&self, buf: &mut [u8], offset: u64) -> std::io::Result<usize> {
#[cfg(not(target_os = "windows"))]
return self.read_at(buf, offset);

#[cfg(target_os = "windows")]
return self.seek_read(buf, offset);
}

#[inline(always)]
fn pwrite(&self, buf: &[u8], offset: u64) -> std::io::Result<usize> {
#[cfg(not(target_os = "windows"))]
return self.write_at(buf, offset);

#[cfg(target_os = "windows")]
return self.seek_write(buf, offset);
}
}

/// OSDP file transfer context
#[derive(Debug)]
pub struct OsdpFile {
Expand Down Expand Up @@ -49,7 +79,7 @@ unsafe extern "C" fn raw_file_read(
}
let file = ctx.file.as_ref().unwrap();
let mut read_buf = vec![0u8; size as usize];
let len = match file.read_at(&mut read_buf, offset as u64) {
let len = match file.pread(&mut read_buf, offset as u64) {
Ok(len) => len as i32,
Err(_) => -1,
};
Expand All @@ -70,7 +100,7 @@ unsafe extern "C" fn raw_file_write(
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);
let file = ctx.file.as_ref().unwrap();
match file.write_at(&write_buf, offset as u64) {
match file.pwrite(&write_buf, offset as u64) {
Ok(len) => len as i32,
Err(_) => -1,
}
Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unsafe extern "C" fn log_handler(
) {
let msg = crate::cstr_to_string(msg);
let msg = msg.trim();
match log_level as u32 {
match log_level as libosdp_sys::osdp_log_level_e {
libosdp_sys::osdp_log_level_e_OSDP_LOG_EMERG => error!("PD: {msg}"),
libosdp_sys::osdp_log_level_e_OSDP_LOG_ALERT => error!("PD: {msg}"),
libosdp_sys::osdp_log_level_e_OSDP_LOG_CRIT => error!("PD: {msg}"),
Expand Down
2 changes: 1 addition & 1 deletion libosdp/src/pdcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl FromStr for PdCapability {

impl From<libosdp_sys::osdp_pd_cap> for PdCapability {
fn from(value: libosdp_sys::osdp_pd_cap) -> Self {
let function_code = value.function_code as u32;
let function_code = value.function_code as libosdp_sys::osdp_pd_cap_function_code_e;
let e = PdCapEntity {
compliance: value.compliance_level,
num_items: value.num_items,
Expand Down

0 comments on commit b5e852e

Please sign in to comment.