1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
pub mod usb;
pub mod com;
use crate::errors::*;
use crate::commands::*;
use crate::card::*;
pub use crate::reader::usb::find_usb_readers;
use std::sync::{Arc, Mutex};
use std::{time::Duration};
pub(crate) const TIMEOUT: Duration = Duration::from_secs(1);
pub type UemReader = Arc<Mutex<dyn UemReaderInternal>>;
type UemGeneralResult<T> = core::result::Result<T, UemError>;
pub type UemResult = UemGeneralResult<()>;
pub type UemResultVec = UemGeneralResult<Vec<u8>>;
pub type UemResultCardA = UemGeneralResult<UemCardIso14443A>;
pub type UemResultCardB = UemGeneralResult<UemCardIso14443B>;
impl UemCommandsTrait for UemReader {
fn commands(&mut self) -> UemCommands {
UemCommands::new(self)
}
}
pub trait UemReaderInternal {
fn open(&mut self) -> UemResult;
fn close(&mut self) -> core::result::Result<(), UemError>;
fn send(&mut self, command: &[u8]) -> UemResultVec;
}
impl UemReaderInternal for UemReader {
fn open(&mut self) -> UemResult {
self.lock().unwrap().open()
}
fn close(&mut self) -> core::result::Result<(), UemError> {
self.lock().unwrap().close()
}
fn send(&mut self, command: &[u8]) -> UemResultVec {
self.lock().unwrap().send(command)
}
}
pub(crate) mod processing {
use crate::{helpers::*, reader::*};
pub(crate) trait CommandsCounter {
fn commands_count(&self) -> u8;
fn increment_commands(&mut self);
}
pub(crate) fn prepare_command(reader: &mut impl CommandsCounter, data: &[u8]) -> Vec<u8> {
let mut raw_data: Vec<u8> = vec![];
raw_data.push(0x00);
raw_data.push(reader.commands_count());
reader.increment_commands();
let mut tmp_v = vec![];
data.clone_into(&mut tmp_v);
raw_data.append(&mut tmp_v);
let mut fsc = crc16(&raw_data);
raw_data.append(&mut fsc);
let mut tmp_data = byte_stuff(&raw_data);
let mut raw_data: Vec<u8> = vec![];
raw_data.reserve(2 + tmp_data.len());
raw_data.push(0xFD);
raw_data.append(&mut tmp_data);
raw_data.push(0xFE);
return raw_data;
}
pub(crate) fn parse_response(raw_data: &Vec<u8>) -> UemResultVec {
let raw_data = unbyte_stuff(raw_data);
if (raw_data[0] & 0xFF) != 0xFD {
return Err(UemError::ReaderUnsuccessful(UemInternalError::Protocol, None));
}
if (raw_data[raw_data.len()-1] & 0xFF) != 0xFE {
return Err(UemError::ReaderUnsuccessful(UemInternalError::Protocol, None));
}
let fsc = crc16(&raw_data[1..raw_data.len()-3]);
if (fsc[0] & 0xFF) != (raw_data[raw_data.len()-3] & 0xFF) {
return Err(UemError::ReaderUnsuccessful(UemInternalError::Crc, None));}
if (fsc[1] & 0xFF) != (raw_data[raw_data.len()-2] & 0xFF) {
return Err(UemError::ReaderUnsuccessful(UemInternalError::Crc, None));
}
let data = raw_data[3..raw_data.len()-3].to_vec();
Ok(data)
}
}