uem_reader/
errors.rs

1//! Crate error types
2
3use enum_iterator::{all, Sequence};
4use thiserror::Error;
5
6#[allow(dead_code)]
7#[derive(Error, Debug, Clone)]
8/// General errors for library methods
9pub enum UemError {
10    #[error("Operation in progress")]
11    /// Indicates that current operation
12    /// is still in progress
13    PendingOperation,
14    #[error("Feature not supported")]
15    /// Indicates that the requested feature
16    /// is not supported yet
17    UnsupportedFeature,
18    #[error("Communication data lost")]
19    /// Indicates that communication channel
20    /// has detected a problem with
21    /// data consistency
22    LostCommunicationData,
23    #[error("Incorrect parameter")]
24    /// A method parameter has incorrect value
25    IncorrectParameter,
26    #[error("Unexpected error")]
27    /// Some unexpected error occured
28    Unexpected,
29    #[error("Access error")]
30    /// Access to the requested feature
31    /// is not granted
32    Access,
33    #[error("Not transacted")]
34    /// Failed to transact data with a remote device
35    NotTransacted,
36    #[error("Incorrect reader name")]
37    /// The supplied reader name is incorrect
38    IncorrectReaderName,
39    #[error("Failed to connect to the reader")]
40    /// Reader connection has been unsuccessful
41    ReaderConnectionFailed,
42    #[error("Reader not connected")]
43    /// Reader is not in connected state
44    ReaderNotConnected,
45    #[error("Reader already connected")]
46    /// Reader is already connected
47    ReaderAlreadyConnected,
48    #[error("Incorrect reader response")]
49    /// There were errors in a reader
50    /// response data
51    ReaderIncorrectResponse,
52    #[error("Reader not responding")]
53    /// Waiting for a reader response
54    /// timed out
55    ReaderResponseFailure,
56    #[error("Reader returned error code")]
57    /// There is an internal error code received
58    /// from a reader, followed by response vector (optional). 
59    /// Check [error value](UemInternalError).
60    ReaderUnsuccessful(UemInternalError, Option<Vec<u8>>),
61    #[error("SAM: APDU error")]
62    /// There was an APDU responde error.
63    /// It can be decoded using SAM documentation.
64    SamApdu,
65    #[error("SAM: Invalid MAC")]
66    /// Failed to check MAC signature
67    /// while interacting with SAM module
68    SamInvalidMac,
69    #[error("SAM: Authentication failed")]
70    /// Failed to authenticate with SAM module
71    SamAuthenticationFailed,
72}
73
74#[repr(u8)]
75#[derive(Debug, PartialEq, Sequence, Clone, Copy)]
76/// Error codes returned by a reader
77pub enum UemInternalError {
78    /// No RFID card found in vicinity
79    NoTag = 0xFF,
80    /// CRC radio transaction check failed
81    Crc = 0xFE,
82    /// Failed to perform operation with
83    /// supplied key
84    WrongKey = 0xFC,
85    /// Parity bit transaction check failed
86    Parity = 0xFB,
87    /// Received an error result code from a card
88    ResultCode = 0xFA,
89    /// Wrong protocol selected for this command
90    Protocol = 0xF9,
91    /// Unknown serial number of a device
92    SerialNumber = 0xF8,
93    /// Failed to load key into flash memory
94    LoadKey = 0xF7,
95    /// The card is not in a valid 
96    /// authentication state
97    NotAuthenticated = 0xF6,
98    /// Incorrect bits count during transation
99    BitCount = 0xF5,
100    /// Incorrect byte count during transation
101    ByteCount = 0xF4,
102    /// Failed to write data block on a card
103    WriteData = 0xF1,
104    /// Failed to increment card counter
105    Increment = 0xF0,
106    /// Failed to decrement card counter
107    Decrement = 0xEF,
108    /// Failed to read card data block
109    ReadData = 0xEE,
110    /// Internal radio exchange buffer overflow
111    Overflow = 0xED,
112    /// Reader-card transaction frame is malformed
113    Framing = 0xEB,
114    /// The requested operation is not implemented
115    /// is a card
116    UnknownOperation = 0xE9,
117    /// Collision detected during card activation
118    Collision = 0xE8,
119    Reset = 0xE7,
120    Interface = 0xE6,
121    /// Bitwise avticollision is not supported
122    /// by a card
123    NoBitwiseAnticoll = 0xE4,
124    /// Error with coding of bytes in transaction
125    Coding = 0xE1,
126    /// The required hardware is absent in a reader
127    HardwareAbsent = 0xD8,
128    /// The requested command is not implemented
129    /// in a reader
130    UnknownCommand = 0xD7,
131    /// The requested command is not supported 
132    /// by a reader
133    CommandNotSupported = 0xD6,
134    /// The radio chip is in wrong mode.
135    /// Check that you selected right mode:
136    /// ISO14443A/B or ISO15693
137    WrongMfrcMode = 0xD5,
138    /// Both reader and host should be in plain,
139    /// authenticated and/or encrypted 
140    /// transaction modes
141    WrongCryptoMode = 0xD4,
142    /// Please erase reader flash memory before
143    /// writing new data in it
144    FlashEraseRequired = 0xD3,
145    /// The required key is absent in a flash memory
146    FlashKeyAbsent = 0xD2,
147    /// Failed to transceive data with a card
148    Transceive = 0xD1,
149    /// Stack overflow for ISO 15693 card
150    IcodeStackOverflow = 0xD0,
151    /// Failed to perform Halt operation 
152    /// with ISO 14443B card
153    HaltB = 0xCF,
154    /// Flash reader memory is in use
155    FlashOperating = 0xCE,
156    /// Some internal call within reader is failed
157    InternalCall = 0xCD,
158    /// Error while performing cascade level 3
159    /// activation with a card
160    CascadeLevel10 = 0xCC,
161    /// The requested card speed is not supported
162    BaudrateNotSupported = 0xCA,
163    /// SAM module has timed out to respond
164    SamTimeout = 0xC9,
165    /// There was an error with SAM APDU response
166    SamApdu = 0xC8,
167    /// Wrong RFID card MAC response
168    /// while interacting with SAM module
169    SamCardMac = 0xC7,
170    /// Failed to authenticate with SAM module
171    SamAuthentication = 0xC6,
172    /// Incorrect bytes count sent/received from
173    /// SAM module
174    SamByteCount = 0xC5,
175    /// Incorrect command parameter value
176    ParameterValue = 0xC4,
177    /// Series of error responses for
178    /// Mifare Classic card type
179    MifareClassicNacF = 0xBF,
180    MifareClassicNacE = 0xBE,
181    MifareClassicNacD = 0xBD,
182    MifareClassicNacC = 0xBC,
183    MifareClassicNacB = 0xBB,
184    MifareClassicNacA = 0xBA,
185    MifareClassicNac9 = 0xB9,
186    MifareClassicNac8 = 0xB8,
187    MifareClassicNac7 = 0xB7,
188    MifareClassicNac6 = 0xB6,
189    MifareClassicNac5 = 0xB5,
190    MifareClassicNac4 = 0xB4,
191    MifareClassicNac3 = 0xB3,
192    MifareClassicNac2 = 0xB2,
193    MifareClassicNac1 = 0xB1,
194    MifareClassicNac0 = 0xB0,
195    /// Series of error responses for 
196    /// Mifare Plus type cards
197    MifarePlusGeneralManipulate = 0xAF,
198    MifarePlusCardMac = 0xAE,
199    MifarePlusEv1NotSupported = 0xAD,
200    MifarePlusLength = 0xAC,
201    MifarePlusNoStateForCommand = 0xAB,
202    MifarePlusNotExistingBlock = 0xAA,
203    MifarePlusBlockNumber = 0xA9,
204    MifarePlusMac = 0xA8,
205    MifarePlusCommandOverflow = 0xA7,
206    MifarePlusAuthentication = 0xA6,
207    MifarePlusEv1Tmac = 0xA5,
208    /// The requested feature is not yet implemented
209    NotYetImplemented = 0x9C,
210    /// CRC16 command/response check failed
211    Crc16 = 0x9B,
212    /// The reception buffer on reader radio chip
213    /// has overflowed
214    ReceiveBufferOverflow = 0x90,
215    /// Error in internal reader library
216    InternalReaderLibrary = 0x85,
217    /// Incorrect format of a card value block
218    ValueBlockFormat = 0x84,
219    /// A command parameter is not supported
220    UnsupportedParameter = 0x83,
221    /// Commands in a chain are malformed
222    IncompleteChaining = 0x82,
223    /// Reader is overheating
224    Temperature = 0x81,
225    /// Unknown error
226    Unknown = 0x80,
227}
228
229impl UemInternalError {
230    pub(crate) fn from_byte(code: u8) -> Self {
231        for err in all::<Self>() {
232            if err.clone() as u8 == code {
233                return err;
234            }
235        }
236        Self::Unknown
237    }
238}