forked from goToMain/libosdp-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.rs
466 lines (416 loc) · 15.5 KB
/
events.rs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// Copyright (c) 2023-2024 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
//! OSDP PDs have to send messages to it's controlling unit - CP to intimate it
//! about various events that originate there (such as key press, card reads,
//! etc.,). They do this by creating an "event" and sending it to the CP. This
//! module is responsible to handling such events though [`OsdpEvent`].
use crate::OsdpError;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use super::ConvertEndian;
type Result<T> = core::result::Result<T, OsdpError>;
#[cfg(feature = "defmt-03")]
use defmt::panic;
/// Various card formats that a PD can support. This is sent to CP when a PD
/// must report a card read
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum OsdpCardFormats {
/// Card format is not specified
Unspecified,
/// Wiegand format
Wiegand,
/// Ascii format
#[default]
Ascii,
}
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
}
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND => {
OsdpCardFormats::Wiegand
}
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII => OsdpCardFormats::Ascii,
_ => panic!("Unknown osdp card format"),
}
}
}
impl From<OsdpCardFormats> for libosdp_sys::osdp_event_cardread_format_e {
fn from(val: OsdpCardFormats) -> Self {
match val {
OsdpCardFormats::Unspecified => {
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_UNSPECIFIED
}
OsdpCardFormats::Wiegand => {
libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND
}
OsdpCardFormats::Ascii => libosdp_sys::osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII,
}
}
}
/// Event that describes card read activity on the PD
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct OsdpEventCardRead {
/// Reader (another device connected to this PD) which caused this event
///
/// 0 - self
/// 1 - fist connected reader
/// 2 - second connected reader
/// ....
pub reader_no: i32,
/// Format of the card that was read
pub format: OsdpCardFormats,
/// The direction of the PD where the card read happened (some PDs have two
/// physical card readers to put on either side of a door).
///
/// false - Forward
/// true - Backward
pub direction: bool,
/// Number of valid data bits in [`OsdpEventCardRead::data`] when the card
/// format is not [`OsdpCardFormats::Ascii`]. For [`OsdpCardFormats::Ascii`], this
/// field is set to zero.
pub nr_bits: usize,
/// Card data; bytes or bits depending on [`OsdpCardFormats`]
pub data: Vec<u8>,
}
impl OsdpEventCardRead {
/// Create an ASCII card read event for self and direction set to forward
pub fn new_ascii(data: Vec<u8>) -> Self {
Self {
reader_no: 0,
format: OsdpCardFormats::Ascii,
direction: false,
nr_bits: 0,
data,
}
}
/// Create a Wiegand card read event for self and direction set to forward
pub fn new_wiegand(nr_bits: usize, data: Vec<u8>) -> Result<Self> {
if nr_bits > data.len() * 8 {
return Err(OsdpError::Command);
}
Ok(Self {
reader_no: 0,
format: OsdpCardFormats::Wiegand,
direction: false,
nr_bits,
data,
})
}
}
impl From<libosdp_sys::osdp_event_cardread> for OsdpEventCardRead {
fn from(value: libosdp_sys::osdp_event_cardread) -> Self {
let direction = value.direction == 1;
let format = value.format.into();
let len = value.length as usize;
let (nr_bits, nr_bytes) = match format {
OsdpCardFormats::Ascii => (0, len),
_ => (len, len.div_ceil(8)),
};
let data = value.data[0..nr_bytes].to_vec();
OsdpEventCardRead {
reader_no: value.reader_no,
format,
direction,
nr_bits,
data,
}
}
}
impl From<OsdpEventCardRead> for libosdp_sys::osdp_event_cardread {
fn from(value: OsdpEventCardRead) -> Self {
let mut data = [0; libosdp_sys::OSDP_EVENT_CARDREAD_MAX_DATALEN as usize];
let length = match value.format {
OsdpCardFormats::Ascii => value.data.len() as i32,
_ => value.nr_bits as i32,
};
data[..value.data.len()].copy_from_slice(&value.data[..]);
libosdp_sys::osdp_event_cardread {
reader_no: value.reader_no,
format: value.format.into(),
direction: value.direction as i32,
length,
data,
}
}
}
/// Event to describe a key press activity on the PD
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct OsdpEventKeyPress {
/// Reader (another device connected to this PD) which caused this event
///
/// 0 - self
/// 1 - fist connected reader
/// 2 - second connected reader
/// ....
pub reader_no: i32,
/// Key data
pub data: Vec<u8>,
}
impl OsdpEventKeyPress {
/// Create key press event for the keys specified in `data`.
pub fn new(data: Vec<u8>) -> Self {
Self { reader_no: 0, data }
}
}
impl From<libosdp_sys::osdp_event_keypress> for OsdpEventKeyPress {
fn from(value: libosdp_sys::osdp_event_keypress) -> Self {
let n = value.length as usize;
let data = value.data[0..n].to_vec();
OsdpEventKeyPress {
reader_no: value.reader_no,
data,
}
}
}
impl From<OsdpEventKeyPress> for libosdp_sys::osdp_event_keypress {
fn from(value: OsdpEventKeyPress) -> Self {
let mut data = [0; libosdp_sys::OSDP_EVENT_KEYPRESS_MAX_DATALEN as usize];
data[..value.data.len()].copy_from_slice(&value.data[..]);
libosdp_sys::osdp_event_keypress {
reader_no: value.reader_no,
length: value.data.len() as i32,
data,
}
}
}
/// Event to transport a Manufacturer specific command's response.
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct OsdpEventMfgReply {
/// 3-byte IEEE assigned OUI used as vendor code
pub vendor_code: (u8, u8, u8),
/// 1-byte reply code
pub reply: u8,
/// Reply data (if any)
pub data: Vec<u8>,
}
impl From<libosdp_sys::osdp_event_mfgrep> for OsdpEventMfgReply {
fn from(value: libosdp_sys::osdp_event_mfgrep) -> Self {
let n = value.length as usize;
let data = value.data[0..n].to_vec();
let bytes = value.vendor_code.to_le_bytes();
let vendor_code: (u8, u8, u8) = (bytes[0], bytes[1], bytes[2]);
OsdpEventMfgReply {
vendor_code,
reply: value.command,
data,
}
}
}
impl From<OsdpEventMfgReply> for libosdp_sys::osdp_event_mfgrep {
fn from(value: OsdpEventMfgReply) -> Self {
let mut data = [0; libosdp_sys::OSDP_EVENT_MFGREP_MAX_DATALEN as usize];
data[..value.data.len()].copy_from_slice(&value.data[..]);
libosdp_sys::osdp_event_mfgrep {
vendor_code: value.vendor_code.as_le(),
command: value.reply,
length: value.data.len() as u8,
data,
}
}
}
/// Status report type
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum OsdpStatusReportType {
/// Input status report
Input,
/// Output status report
Output,
/// Remote status report
Remote,
/// Local status report
Local,
}
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
}
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_OUTPUT => {
OsdpStatusReportType::Output
}
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_REMOTE => {
OsdpStatusReportType::Remote
}
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_LOCAL => {
OsdpStatusReportType::Local
}
_ => panic!("Invalid enum entry"),
}
}
}
impl From<OsdpStatusReportType> for libosdp_sys::osdp_status_report_type {
fn from(value: OsdpStatusReportType) -> Self {
match value {
OsdpStatusReportType::Input => {
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_INPUT
}
OsdpStatusReportType::Output => {
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_OUTPUT
}
OsdpStatusReportType::Remote => {
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_REMOTE
}
OsdpStatusReportType::Local => {
libosdp_sys::osdp_status_report_type_OSDP_STATUS_REPORT_LOCAL
}
}
}
}
/// Event to describe various status changes on PD
///
/// This event is used by the PD to indicate status such as input, output,
/// tamper, etc.,. up to a maximum of 32 status bits can be reported. The values
/// of the least significant N bit of status are considered, where N is the
/// number of items as described in the corresponding capability codes,
/// - PdCapability::OutputControl
/// - PdCapability::ContactStatusMonitoring
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct OsdpStatusReport {
type_: OsdpStatusReportType,
nr_entries: usize,
mask: u32,
}
impl OsdpStatusReport {
/// Create an input event with given bit mask
pub fn new_input(nr_entries: usize, mask: u32) -> Self {
Self {
type_: OsdpStatusReportType::Input,
nr_entries,
mask,
}
}
/// Create an output event with given bit mask
pub fn new_output(nr_entries: usize, mask: u32) -> Self {
Self {
type_: OsdpStatusReportType::Output,
nr_entries,
mask,
}
}
}
impl From<libosdp_sys::osdp_status_report> for OsdpStatusReport {
fn from(value: libosdp_sys::osdp_status_report) -> Self {
OsdpStatusReport {
type_: value.type_.into(),
nr_entries: value.nr_entries as usize,
mask: value.mask,
}
}
}
impl From<OsdpStatusReport> for libosdp_sys::osdp_status_report {
fn from(value: OsdpStatusReport) -> Self {
libosdp_sys::osdp_status_report {
type_: value.type_.into(),
nr_entries: value.nr_entries as i32,
mask: value.mask,
}
}
}
/// CP to intimate it about various events that originate there (such as key
/// press, card reads, etc.,). They do this by creating an “event” and sending
/// it to the CP. This module is responsible to handling such events though
/// OsdpEvent.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum OsdpEvent {
/// Event that describes card read activity on the PD
CardRead(OsdpEventCardRead),
/// Event to describe a key press activity on the PD
KeyPress(OsdpEventKeyPress),
/// Event to transport a Manufacturer specific command’s response
MfgReply(OsdpEventMfgReply),
/// Event to describe a input/output/tamper/power status change
Status(OsdpStatusReport),
}
impl From<OsdpEvent> for libosdp_sys::osdp_event {
fn from(value: OsdpEvent) -> Self {
match value {
OsdpEvent::CardRead(e) => libosdp_sys::osdp_event {
type_: libosdp_sys::osdp_event_type_OSDP_EVENT_CARDREAD,
__bindgen_anon_1: libosdp_sys::osdp_event__bindgen_ty_1 {
cardread: e.clone().into(),
},
},
OsdpEvent::KeyPress(e) => libosdp_sys::osdp_event {
type_: libosdp_sys::osdp_event_type_OSDP_EVENT_KEYPRESS,
__bindgen_anon_1: libosdp_sys::osdp_event__bindgen_ty_1 {
keypress: e.clone().into(),
},
},
OsdpEvent::MfgReply(e) => libosdp_sys::osdp_event {
type_: libosdp_sys::osdp_event_type_OSDP_EVENT_MFGREP,
__bindgen_anon_1: libosdp_sys::osdp_event__bindgen_ty_1 {
mfgrep: e.clone().into(),
},
},
OsdpEvent::Status(e) => libosdp_sys::osdp_event {
type_: libosdp_sys::osdp_event_type_OSDP_EVENT_STATUS,
__bindgen_anon_1: libosdp_sys::osdp_event__bindgen_ty_1 { status: e.into() },
},
}
}
}
impl From<libosdp_sys::osdp_event> for OsdpEvent {
fn from(value: libosdp_sys::osdp_event) -> Self {
match value.type_ {
libosdp_sys::osdp_event_type_OSDP_EVENT_CARDREAD => {
OsdpEvent::CardRead(unsafe { value.__bindgen_anon_1.cardread.into() })
}
libosdp_sys::osdp_event_type_OSDP_EVENT_KEYPRESS => {
OsdpEvent::KeyPress(unsafe { value.__bindgen_anon_1.keypress.into() })
}
libosdp_sys::osdp_event_type_OSDP_EVENT_MFGREP => {
OsdpEvent::MfgReply(unsafe { value.__bindgen_anon_1.mfgrep.into() })
}
libosdp_sys::osdp_event_type_OSDP_EVENT_STATUS => {
OsdpEvent::Status(unsafe { value.__bindgen_anon_1.status.into() })
}
_ => panic!("Unknown event"),
}
}
}
#[cfg(test)]
mod tests {
use super::OsdpEventCardRead;
use libosdp_sys::{
osdp_event_cardread, osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII,
osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND,
};
#[test]
fn test_event_cardread() {
let event = OsdpEventCardRead::new_ascii(vec![0x55, 0xAA]);
let event_struct: osdp_event_cardread = event.clone().into();
assert_eq!(event_struct.length, 2);
assert_eq!(event_struct.direction, 0);
assert_eq!(
event_struct.format,
osdp_event_cardread_format_e_OSDP_CARD_FMT_ASCII
);
assert_eq!(event_struct.data[0], 0x55);
assert_eq!(event_struct.data[1], 0xAA);
assert_eq!(event, event_struct.into());
let event = OsdpEventCardRead::new_wiegand(15, vec![0x55, 0xAA]).unwrap();
let event_struct: osdp_event_cardread = event.clone().into();
assert_eq!(event_struct.length, 15);
assert_eq!(event_struct.direction, 0);
assert_eq!(
event_struct.format,
osdp_event_cardread_format_e_OSDP_CARD_FMT_RAW_WIEGAND
);
assert_eq!(event_struct.data[0], 0x55);
assert_eq!(event_struct.data[1], 0xAA);
assert_eq!(event, event_struct.into());
}
}