Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to windows-sys #228

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ features = [
"minwindef", "ntdef", "setupapi", "winbase", "winerror", "winnt",
]

[target."cfg(windows)".dependencies.windows-sys]
version = "0.52.0"
features = [
"Win32_Storage",
"Win32_Storage_FileSystem",
"Win32_Security",
"Win32_Devices",
"Win32_Devices_Communication",
"Win32_Devices_DeviceAndDriverInstallation",
"Win32_Devices_Properties",
"Win32_Foundation",
"Win32_Media",
"Win32_Media_KernelStreaming",
"Win32_System_Diagnostics",
"Win32_System_Diagnostics_Debug",
"Win32_System_IO",
"Win32_System_Registry",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_Globalization",
]

[dependencies]
cfg-if = "1.0.0"
scopeguard = "1.1"
Expand Down
34 changes: 25 additions & 9 deletions src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@ use std::os::windows::prelude::*;
use std::time::Duration;
use std::{io, ptr};

use winapi::shared::minwindef::*;
use winapi::um::commapi::*;
use winapi::um::fileapi::*;
use winapi::um::handleapi::*;
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::winbase::*;
use winapi::um::winnt::{
DUPLICATE_SAME_ACCESS, FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE, MAXDWORD,
};
// use winapi::shared::minwindef::*;
// use winapi::um::commapi::*;
// use winapi::um::fileapi::*;
// use winapi::um::handleapi::*;
// use winapi::um::processthreadsapi::GetCurrentProcess;
// use winapi::um::winbase::*;
// use winapi::um::winnt::{
// DUPLICATE_SAME_ACCESS, FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE, MAXDWORD,
// };

use windows_sys::Win32::Storage::FileSystem::*;
use windows_sys::Win32::System::Threading::GetCurrentProcess;
use windows_sys::Win32::System::SystemServices::MAXDWORD;
use windows_sys::Win32::Foundation::{DUPLICATE_SAME_ACCESS, TRUE, GENERIC_READ, GENERIC_WRITE, HANDLE,
INVALID_HANDLE_VALUE, DuplicateHandle, CloseHandle};
use windows_sys::Win32::Devices::Communication::{ClearCommError, COMMTIMEOUTS,
SETRTS, CLRRTS, SETDTR, CLRDTR,
PURGE_RXABORT, PURGE_RXCLEAR, PURGE_TXABORT, PURGE_TXCLEAR,
MS_RLSD_ON, MS_CTS_ON, MS_DSR_ON ,MS_RING_ON, EscapeCommFunction, SetCommTimeouts,
GetCommModemStatus, PurgeComm, SetCommBreak, ClearCommBreak};

use crate::windows::dcb;
use crate::{
ClearBuffer, DataBits, Error, ErrorKind, FlowControl, Parity, Result, SerialPort,
SerialPortBuilder, StopBits,
};

use super::dcb::MyTrait;
//https://github.com/microsoft/windows-rs/issues/881
type DWORD = u32;
type LPVOID = *mut u8;

/// A serial port implementation for Windows COM ports
///
/// The port will be closed when the value is dropped. However, this struct
Expand Down
155 changes: 150 additions & 5 deletions src/windows/dcb.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::mem::MaybeUninit;
use winapi::shared::minwindef::*;
use winapi::um::commapi::*;
use winapi::um::winbase::*;
use winapi::um::winnt::HANDLE;
// use winapi::shared::minwindef::*;
// use winapi::um::commapi::*;
// use winapi::um::winbase::*;
// use winapi::um::winnt::HANDLE;

use windows_sys::Win32::Devices::Communication::*;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::Foundation::{TRUE, FALSE};
use windows_sys::Win32::System::WindowsProgramming::DTR_CONTROL_DISABLE;
//https://github.com/microsoft/windows-rs/issues/881
type DWORD = u32;

use crate::{DataBits, FlowControl, Parity, Result, StopBits};

Expand Down Expand Up @@ -31,7 +38,10 @@ pub(crate) fn init(dcb: &mut DCB) {
// dcb.StopBits
dcb.XonChar = 17;
dcb.XoffChar = 19;
dcb.ErrorChar = '\0' as winapi::ctypes::c_char;
//dcb.ErrorChar = '\0' as winapi::ctypes::c_char;
//https://github.com/microsoft/windows-rs/issues/2188
// dcb.ErrorChar = '\0' as i8;
dcb.ErrorChar = '\0' as u8;
dcb.EofChar = 26;
// dcb.EvtChar
// always true for communications resources
Expand All @@ -55,6 +65,141 @@ pub(crate) fn init(dcb: &mut DCB) {
dcb.set_fAbortOnError(FALSE as DWORD);
}

//https://github.com/rust-lang/rfcs/issues/314
//currently windows-sys does not implement bit fields
// pub struct MyDCB(DCB);
pub trait MyTrait {
fn set_fBinary(&mut self, fBinary: DWORD) -> ();
fn set_fParity(&mut self, fParity: DWORD) -> ();
fn set_fOutxCtsFlow(&mut self, fOutxCtsFlow: DWORD) -> ();
fn set_fOutxDsrFlow(&mut self, fOutxDsrFlow: DWORD) -> ();
fn set_fDtrControl(&mut self, fDtrControl: DWORD) -> ();
fn set_fDsrSensitivity(&mut self, fDsrSensitivity: DWORD) -> ();
fn set_fTXContinueOnXoff(&mut self, fTXContinueOnXoff: DWORD) -> ();
fn set_fOutX(&mut self, fOutX: DWORD) -> ();
fn set_fInX(&mut self, fInX: DWORD) -> ();
fn set_fErrorChar(&mut self, fErrorChar: DWORD) -> ();
fn set_fNull(&mut self, fNull: DWORD) -> ();
fn set_fRtsControl(&mut self, fRtsControl: DWORD) -> ();
fn set_fAbortOnError(&mut self, fAbortOnError: DWORD) -> ();

fn fBinary(&self) -> DWORD;
fn fParity(&self) -> DWORD;
fn fOutxCtsFlow(&self) -> DWORD;
fn fOutxDsrFlow(&self) -> DWORD;
fn fDtrControl(&self) -> DWORD;
fn fDsrSensitivity(&self) -> DWORD;
fn fTXContinueOnXoff(&self) -> DWORD;
fn fOutX(&self) -> DWORD;
fn fInX(&self) -> DWORD;
fn fErrorChar(&self) -> DWORD;
fn fNull(&self) -> DWORD;
fn fRtsControl(&self) -> DWORD;
fn fAbortOnError(&self) -> DWORD;

}

impl MyTrait for DCB {
fn set_fBinary(&mut self, f_binary: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0000_0001) | (f_binary & 0x1);
}
fn set_fParity(&mut self, f_parity: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0000_0010) | ((f_parity & 0x1) << 1);
}
fn set_fOutxCtsFlow(&mut self, f_outx_cts_flow: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0000_0100) | ((f_outx_cts_flow & 0x1) << 2);
}
fn set_fOutxDsrFlow(&mut self, f_outx_dsr_flow: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0000_1000) | ((f_outx_dsr_flow & 0x1) << 3);
}
fn set_fDtrControl(&mut self, f_dtr_control: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0001_0000) | ((f_dtr_control & 0x1) << 4);
self._bitfield = (self._bitfield & !0b0000_0000_0010_0000) | ((f_dtr_control & 0x2) << 4);
}
fn set_fDsrSensitivity(&mut self, f_dsr_sensitivity: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_0100_0000) | ((f_dsr_sensitivity & 0x1) << 6);
}
fn set_fTXContinueOnXoff(&mut self, f_tx_continue_on_x_off: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0000_1000_0000) | ((f_tx_continue_on_x_off & 0x1) << 7);
}
fn set_fOutX(&mut self, f_out_x: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0001_0000_0000) | ((f_out_x & 0x1) << 8);
}
fn set_fInX(&mut self, f_in_x: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0010_0000_0000) | ((f_in_x & 0x1) << 9);
}
fn set_fErrorChar(&mut self, f_error_char: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_0100_0000_0000) | ((f_error_char & 0x1) << 10);
}
fn set_fNull(&mut self, f_null: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0000_1000_0000_0000) | ((f_null & 0x1) << 11);
}
fn set_fRtsControl(&mut self, f_rts_control: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0001_0000_0000_0000) | ((f_rts_control & 0x1) << 12);
self._bitfield = (self._bitfield & !0b0010_0000_0000_0000) | ((f_rts_control & 0x2) << 12);
}
fn set_fAbortOnError(&mut self, f_abort_on_error: DWORD) -> () {
self._bitfield = (self._bitfield & !0b0100_0000_0000_0000) | ((f_abort_on_error & 0x1) << 14);
}

fn fBinary(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 0);
bit
}
fn fParity(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 1);
bit >> 1
}
fn fOutxCtsFlow(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 2);
bit >> 2
}
fn fOutxDsrFlow(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 3);
bit >> 3
}
fn fDtrControl(&self) -> DWORD {
let bit1: u32 = self._bitfield & (1 << 4);
let bit2: u32 = self._bitfield & (1 << 5);
let bit:u32 = bit1 | bit2;
bit >> 4
}
fn fDsrSensitivity(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 6);
bit >> 6
}
fn fTXContinueOnXoff(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 7);
bit >> 7
}
fn fOutX(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 8);
bit >> 8
}
fn fInX(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 9);
bit >> 9
}
fn fErrorChar(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 10);
bit >> 10
}
fn fNull(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 11);
bit >> 11
}
fn fRtsControl(&self) -> DWORD {
let bit1: u32 = self._bitfield & (1 << 12);
let bit2: u32 = self._bitfield & (1 << 13);
let bit:u32 = bit1 | bit2;
bit >> 12
}
fn fAbortOnError(&self) -> DWORD {
let bit: u32 = self._bitfield & (1 << 14);
bit >> 14
}
}

pub(crate) fn set_dcb(handle: HANDLE, mut dcb: DCB) -> Result<()> {
if unsafe { SetCommState(handle, &mut dcb as *mut _) != 0 } {
Ok(())
Expand Down
Loading
Loading