libosdp 0.2.0

Library implementation of IEC 60839-11-5 OSDP (Open Supervised Device Protocol)
Documentation
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
//
// Copyright (c) 2023-2024 Siddharth Chandrasekaran <sidcha.dev@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0

use alloc::format;
use core::str::FromStr;

use crate::OsdpError;

/// PD capability entity to be used inside [`PdCapability`]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PdCapEntity {
    compliance: u8,
    num_items: u8,
}

impl PdCapEntity {
    /// Create a new capability entity to be used inside [`PdCapability`].
    ///
    /// # Arguments
    ///
    /// * `compliance` - A number that indicates what the PD can do with this
    ///   capability. This number means different things for different
    ///   [`PdCapability`].
    /// * `num_items` - number of units of such capability in the PD. For
    ///    LED capability ([`PdCapability::LedControl`]), this would indicate
    ///    the number of controllable LEDs available on this PD.
    pub fn new(compliance: u8, num_items: u8) -> Self {
        Self {
            compliance,
            num_items,
        }
    }
}

// From "Compliance:10,NumItems:20" to PdCapEntry { compliance: 10, num_items: 20 }
impl FromStr for PdCapEntity {
    type Err = OsdpError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (compliance, num_items);
        if let Some((compliance_str, num_items_str)) = s.split_once(',') {
            if let Some((key, val)) = compliance_str.split_once(':') {
                if key != "Compliance" {
                    return Err(OsdpError::Parse(format!("PdCapEntry: {s}")));
                }
                compliance = val.parse::<u8>().unwrap()
            } else {
                return Err(OsdpError::Parse(format!("PdCapEntry: {s}")));
            }
            if let Some((key, val)) = num_items_str.split_once(':') {
                if key != "NumItems" {
                    return Err(OsdpError::Parse(format!("PdCapEntry: {s}")));
                }
                num_items = val.parse::<u8>().unwrap()
            } else {
                return Err(OsdpError::Parse(format!("PdCapEntry: {s}")));
            }
            Ok(Self {
                compliance,
                num_items,
            })
        } else {
            Err(OsdpError::Parse(format!("PdCapEntry: {s}")))
        }
    }
}

/// OSDP defined PD capabilities. PDs expose/advertise features they support to
/// the CP by means of "capabilities".
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum PdCapability {
    /// This function indicates the ability to monitor the status of a switch
    /// using a two-wire electrical connection between the PD and the switch.
    /// The on/off position of the switch indicates the state of an external
    /// device.
    ///
    /// The PD may simply resolve all circuit states to an open/closed
    /// status, or it may implement supervision of the monitoring circuit.
    /// A supervised circuit is able to indicate circuit fault status in
    /// addition to open/closed status.
    ContactStatusMonitoring(PdCapEntity),

    /// This function provides a switched output, typically in the form of a
    /// relay. The Output has two states: active or inactive. The Control Panel
    /// (CP) can directly set the Output's state, or, if the PD supports timed
    /// operations, the CP can specify a time period for the activation of the
    /// Output.
    OutputControl(PdCapEntity),

    /// This capability indicates the form of the card data is presented to the
    /// Control Panel.
    CardDataFormat(PdCapEntity),

    /// This capability indicates the presence of and type of LEDs.
    LedControl(PdCapEntity),

    /// This capability indicates the presence of and type of an audible
    /// annunciator (buzzer or similar tone generator).
    AudibleOutput(PdCapEntity),

    /// This capability indicates that the PD supports a text display emulating
    /// character-based display terminals.
    TextOutput(PdCapEntity),

    /// This capability indicates that the type of date and time awareness or
    /// time keeping ability of the PD.
    TimeKeeping(PdCapEntity),

    /// All PDs must be able to support the checksum mode. This capability
    /// indicates if the PD is capable of supporting CRC mode.
    CheckCharacterSupport(PdCapEntity),

    /// This capability indicates the extent to which the PD supports
    /// communication security (Secure Channel Communication)
    CommunicationSecurity(PdCapEntity),

    /// This capability indicates the maximum size single message the PD can
    /// receive.
    ReceiveBufferSize(PdCapEntity),

    /// This capability indicates the maximum size multi-part message which the
    /// PD can handle.
    LargestCombinedMessage(PdCapEntity),

    /// This capability indicates whether the PD supports the transparent mode
    /// used for communicating directly with a smart card.
    SmartCardSupport(PdCapEntity),

    /// This capability indicates the number of credential reader devices
    /// present. Compliance levels are bit fields to be assigned as needed.
    Readers(PdCapEntity),

    /// This capability indicates the ability of the reader to handle biometric
    /// input.
    Biometrics(PdCapEntity),

    /// This capability indicates if the reader is capable of supporting
    /// Secure Pin Entry (SPE) for smart cards
    SecurePinEntry(PdCapEntity),

    /// This capability indicates the version of OSDP the PD supports
    /// Compliance Levels:
    ///   0 - Unspecified
    ///   1 - IEC 60839-11-5
    ///   2 - SIA OSDP 2.2
    OsdpVersion(PdCapEntity),
}

#[rustfmt::skip]
impl FromStr for PdCapability {
    type Err = OsdpError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if let Some((cap, ent)) = s.split_once(':') {
            match cap {
                "ContactStatusMonitoring" => {
                    Ok(PdCapability::ContactStatusMonitoring(PdCapEntity::from_str(ent)?))
                },
                "OutputControl" => {
                    Ok(PdCapability::OutputControl(PdCapEntity::from_str(ent)?))
                },
                "CardDataFormat" => {
                    Ok(PdCapability::CardDataFormat(PdCapEntity::from_str(ent)?))
                },
                "LedControl" => {
                    Ok(PdCapability::LedControl(PdCapEntity::from_str(ent)?))
                },
                "AudibleOutput" => {
                    Ok(PdCapability::AudibleOutput(PdCapEntity::from_str(ent)?))
                },
                "TextOutput" => {
                    Ok(PdCapability::TextOutput(PdCapEntity::from_str(ent)?))
                },
                "TimeKeeping" => {
                    Ok(PdCapability::TimeKeeping(PdCapEntity::from_str(ent)?))
                },
                "CheckCharacterSupport" => {
                    Ok(PdCapability::CheckCharacterSupport(PdCapEntity::from_str(ent)?))
                },
                "CommunicationSecurity" => {
                    Ok(PdCapability::CommunicationSecurity(PdCapEntity::from_str(ent)?))
                },
                "ReceiveBufferSize" => {
                    Ok(PdCapability::ReceiveBufferSize(PdCapEntity::from_str(ent)?))
                }
                "LargestCombinedMessage" => {
                    Ok(PdCapability::LargestCombinedMessage(PdCapEntity::from_str(ent)?))
                },
                "SmartCardSupport" => {
                    Ok(PdCapability::SmartCardSupport(PdCapEntity::from_str(ent)?))
                },
                "Readers" => {
                    Ok(PdCapability::Readers(PdCapEntity::from_str(ent)?))
                },
                "Biometrics" => {
                    Ok(PdCapability::Biometrics(PdCapEntity::from_str(ent)?))
                },
                "SecurePinEntry" => {
                    Ok(PdCapability::SecurePinEntry(PdCapEntity::from_str(ent)?))
                },
                "OsdpVersion" => {
                    Ok(PdCapability::OsdpVersion(PdCapEntity::from_str(ent)?))
                },
                _ => Err(OsdpError::Parse(format!("PdCapability: {s}"))),
            }
        } else {
            Err(OsdpError::Parse(format!("PdCapability: {s}")))
        }
    }
}

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 libosdp_sys::osdp_pd_cap_function_code_e;
        let e = PdCapEntity {
            compliance: value.compliance_level,
            num_items: value.num_items,
        };
        match function_code {
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CONTACT_STATUS_MONITORING => {
                PdCapability::ContactStatusMonitoring(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_OUTPUT_CONTROL => {
                PdCapability::OutputControl(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CARD_DATA_FORMAT => {
                PdCapability::CardDataFormat(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_LED_CONTROL => {
                PdCapability::LedControl(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_AUDIBLE_OUTPUT => {
                PdCapability::AudibleOutput(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_TEXT_OUTPUT => {
                PdCapability::TextOutput(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_TIME_KEEPING => {
                PdCapability::TimeKeeping(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CHECK_CHARACTER_SUPPORT => {
                PdCapability::CheckCharacterSupport(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_COMMUNICATION_SECURITY => {
                PdCapability::CommunicationSecurity(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_RECEIVE_BUFFERSIZE => {
                PdCapability::ReceiveBufferSize(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_LARGEST_COMBINED_MESSAGE_SIZE => {
                PdCapability::LargestCombinedMessage(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_SMART_CARD_SUPPORT => {
                PdCapability::SmartCardSupport(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READERS => {
                PdCapability::Readers(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_BIOMETRICS => {
                PdCapability::Biometrics(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_SECURE_PIN_ENTRY => {
                PdCapability::SecurePinEntry(e)
            }
            libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_OSDP_VERSION => {
                PdCapability::OsdpVersion(e)
            }
            _ => panic!("Unknown function code"),
        }
    }
}

#[rustfmt::skip]
impl From<PdCapability> for u8 {
    fn from(val: PdCapability) -> Self {
        match val {
            PdCapability::ContactStatusMonitoring(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CONTACT_STATUS_MONITORING as u8
            }
            PdCapability::OutputControl(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_OUTPUT_CONTROL as u8
            }
            PdCapability::CardDataFormat(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CARD_DATA_FORMAT as u8
            }
            PdCapability::LedControl(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_LED_CONTROL as u8
            }
            PdCapability::AudibleOutput(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_AUDIBLE_OUTPUT as u8
            }
            PdCapability::TextOutput(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READER_TEXT_OUTPUT as u8
            }
            PdCapability::TimeKeeping(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_TIME_KEEPING as u8
            }
            PdCapability::CheckCharacterSupport(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_CHECK_CHARACTER_SUPPORT as u8
            }
            PdCapability::CommunicationSecurity(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_COMMUNICATION_SECURITY as u8
            }
            PdCapability::ReceiveBufferSize(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_RECEIVE_BUFFERSIZE as u8
            }
            PdCapability::LargestCombinedMessage(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_LARGEST_COMBINED_MESSAGE_SIZE as u8
            }
            PdCapability::SmartCardSupport(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_SMART_CARD_SUPPORT as u8
            }
            PdCapability::Readers(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_READERS as u8
            }
            PdCapability::Biometrics(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_BIOMETRICS as u8
            }
            PdCapability::SecurePinEntry(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_SECURE_PIN_ENTRY as u8
            }
            PdCapability::OsdpVersion(_) => {
                libosdp_sys::osdp_pd_cap_function_code_e_OSDP_PD_CAP_OSDP_VERSION as u8
            }
        }
    }
}

impl From<PdCapability> for libosdp_sys::osdp_pd_cap {
    fn from(value: PdCapability) -> Self {
        let function_code: u8 = value.clone().into();
        match value {
            PdCapability::ContactStatusMonitoring(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::OutputControl(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::CardDataFormat(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::LedControl(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::AudibleOutput(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::TextOutput(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::TimeKeeping(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::CheckCharacterSupport(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::CommunicationSecurity(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::ReceiveBufferSize(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::LargestCombinedMessage(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::SmartCardSupport(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::Readers(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::Biometrics(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::SecurePinEntry(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
            PdCapability::OsdpVersion(e) => libosdp_sys::osdp_pd_cap {
                function_code,
                compliance_level: e.compliance,
                num_items: e.num_items,
            },
        }
    }
}