nfc1 0.7.0

High-level safe Rust bindings for libnfc.
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use nfc1_sys::{
	nfc_version,
	str_nfc_baud_rate,
	str_nfc_modulation_type,
	iso14443a_crc as nfc_iso14443a_crc,
	iso14443a_crc_append as nfc_iso14443a_crc_append,
	iso14443b_crc as nfc_iso14443b_crc,
	iso14443b_crc_append as nfc_iso14443b_crc_append,
};
use std::ffi::CStr;
use std::os::raw::c_int;
use std::time::Duration;
use std::string::ToString;
use std::io::{ErrorKind, Error as IoError};

mod target;
mod context;
mod device;
#[cfg(test)]
mod test;

pub use target::Target;
pub use device::Device;
pub use context::Context;
pub use target::info as target_info;

/// Safe error type representing the NFC_E* constants
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Error {
	// rs-nfc1 errors
	Malloc,
	Undefined(c_int),
	UndefinedModulationType,
	NoDeviceFound,

	// libnfc errors
	Io,
	InvalidArgument,
	DeviceNotSupported,
	NoSuchDeviceFound,
	BufferOverflow,
	Timeout,
	OperationAborted,
	NotImplemented,
	TargetReleased,
	RfTransmissionError,
	MifareAuthFailed,
	Soft,
	Chip,
}

impl From<c_int> for Error {
	fn from(input: c_int) -> Self {
		match input {
			nfc1_sys::NFC_EIO => Self::Io,
			nfc1_sys::NFC_EINVARG => Self::InvalidArgument,
			nfc1_sys::NFC_EDEVNOTSUPP => Self::DeviceNotSupported,
			nfc1_sys::NFC_ENOTSUCHDEV => Self::NoSuchDeviceFound,
			nfc1_sys::NFC_EOVFLOW => Self::BufferOverflow,
			nfc1_sys::NFC_ETIMEOUT => Self::Timeout,
			nfc1_sys::NFC_EOPABORTED => Self::OperationAborted,
			nfc1_sys::NFC_ENOTIMPL => Self::NotImplemented,
			nfc1_sys::NFC_ETGRELEASED => Self::TargetReleased,
			nfc1_sys::NFC_ERFTRANS => Self::RfTransmissionError,
			nfc1_sys::NFC_EMFCAUTHFAIL => Self::MifareAuthFailed,
			nfc1_sys::NFC_ESOFT => Self::Soft,
			nfc1_sys::NFC_ECHIP => Self::Chip,
			_ => Self::Undefined(input),
		}
	}
}

impl From<Error> for IoError {
	fn from(input: Error) -> Self {
		match input {
			// rs-nfc1 errors
			Error::Malloc => IoError::from(ErrorKind::Interrupted),
			Error::Undefined(_) => IoError::from(ErrorKind::Other),
			Error::UndefinedModulationType => IoError::from(ErrorKind::InvalidInput),
			Error::NoDeviceFound => IoError::from(ErrorKind::NotFound),

			// libnfc errors
			Error::Io => IoError::from(ErrorKind::Other),
			Error::InvalidArgument => IoError::from(ErrorKind::InvalidInput),
			Error::DeviceNotSupported => IoError::from(ErrorKind::Other),
			Error::NoSuchDeviceFound => IoError::from(ErrorKind::NotFound),
			Error::BufferOverflow => IoError::from(ErrorKind::Other),
			Error::Timeout => IoError::from(ErrorKind::TimedOut),
			Error::OperationAborted => IoError::from(ErrorKind::Interrupted),
			Error::NotImplemented => IoError::from(ErrorKind::Other),
			Error::TargetReleased => IoError::from(ErrorKind::Other),
			Error::RfTransmissionError => IoError::from(ErrorKind::Interrupted),
			Error::MifareAuthFailed => IoError::from(ErrorKind::PermissionDenied),
			Error::Soft => IoError::from(ErrorKind::Other),
			Error::Chip => IoError::from(ErrorKind::Other),
		}
	}
}

impl std::fmt::Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		match self {
			// rs-nfc1 errors
			Error::Malloc => write!(f, "Memory allocation error"),
			Error::Undefined(errno) => write!(f, "Unknown libnfc error: {}", errno),
			Error::UndefinedModulationType => write!(f, "Undefined modulation type"),
			Error::NoDeviceFound => write!(f, "No device found"),

			// libnfc errors
			Error::Io => write!(f, "Input/output error, device may not be usable anymore without re-opening it"),
			Error::InvalidArgument => write!(f, "Invalid argument(s)"),
			Error::DeviceNotSupported => write!(f, "Operation not supported by device"),
			Error::NoSuchDeviceFound => write!(f, "No such device found"),
			Error::BufferOverflow => write!(f, "Buffer overflow"),
			Error::Timeout => write!(f, "Operation timed out"),
			Error::OperationAborted => write!(f, "Operation aborted (by user)"),
			Error::NotImplemented => write!(f, "Not (yet) implemented"),
			Error::TargetReleased => write!(f, "Target released"),
			Error::RfTransmissionError => write!(f, "Error during RF transmission"),
			Error::MifareAuthFailed => write!(f, "MIFARE Classic: authentication failed"),
			Error::Soft => write!(f, "Software error (allocation, file/pipe creation, etc.)"),
			Error::Chip => write!(f, "Device internal chip error"),
		}
	}
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;

pub(crate) fn wrap_err(res: c_int) -> Result<()> {
	if res < 0 {
		return Err(res.into());
	}
	Ok(())
}

pub(crate) fn wrap_err_usize(res: c_int) -> Result<usize> {
	if res < 0 {
		return Err(res.into());
	}
	Ok(res as usize)
}

/// Safe version of the int values used for timeouts in libnfc
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Timeout {
	None,
	Default,
	Duration(Duration),
}

impl From<Timeout> for c_int {
	fn from(input: Timeout) -> Self {
		match input {
			Timeout::None => 0,
			Timeout::Default => -1,
			Timeout::Duration(duration) => duration.as_millis() as c_int,
		}
	}
}

impl From<c_int> for Timeout {
	fn from(input: c_int) -> Self {
		if input < 0 {
			Timeout::Default
		} else {
			match input {
				0 => Timeout::None,
				millis => Timeout::Duration(Duration::from_millis(millis as u64)),
			}
		}
	}
}

/// Safe version of nfc_mode
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Mode {
	Target,
	Initiator,
}

impl From<Mode> for nfc1_sys::nfc_mode {
	fn from(input: Mode) -> nfc1_sys::nfc_mode {
		match input {
			Mode::Target => nfc1_sys::nfc_mode_N_TARGET,
			Mode::Initiator => nfc1_sys::nfc_mode_N_INITIATOR,
		}
	}
}

/// Safe version of nfc_baud_rate
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum BaudRate {
	Baud106,
	Baud212,
	Baud424,
	Baud847,
	Undefined,
}

impl ToString for BaudRate {
	fn to_string(&self) -> String {
		let baud_rate: nfc1_sys::nfc_baud_rate = self.clone().into();
		return unsafe { CStr::from_ptr(str_nfc_baud_rate(baud_rate)) }.to_string_lossy().into_owned();
	}
}

impl From<BaudRate> for nfc1_sys::nfc_baud_rate {
	fn from(input: BaudRate) -> nfc1_sys::nfc_baud_rate {
		match input {
			BaudRate::Baud106 => nfc1_sys::nfc_baud_rate_NBR_106,
			BaudRate::Baud212 => nfc1_sys::nfc_baud_rate_NBR_212,
			BaudRate::Baud424 => nfc1_sys::nfc_baud_rate_NBR_424,
			BaudRate::Baud847 => nfc1_sys::nfc_baud_rate_NBR_847,
			BaudRate::Undefined => nfc1_sys::nfc_baud_rate_NBR_UNDEFINED,
		}
	}
}

impl From<nfc1_sys::nfc_baud_rate> for BaudRate {
	fn from(input: nfc1_sys::nfc_baud_rate) -> Self {
		match input {
			nfc1_sys::nfc_baud_rate_NBR_106 => BaudRate::Baud106,
			nfc1_sys::nfc_baud_rate_NBR_212 => BaudRate::Baud212,
			nfc1_sys::nfc_baud_rate_NBR_424 => BaudRate::Baud424,
			nfc1_sys::nfc_baud_rate_NBR_847 => BaudRate::Baud847,
			_ => BaudRate::Undefined,
		}
	}
}

/// Safe version of nfc_property
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Property {
	TimeoutCommand,
	TimeoutAtr,
	TimeoutCom,
	HandleCrc,
	HandleParity,
	ActivateField,
	ActivateCrypto1,
	InfiniteSelect,
	AcceptInvalidFrames,
	AcceptMultipleFrames,
	AutoIso144434,
	EasyFraming,
	ForceIso14443A,
	ForceIso14443B,
	ForceSpeed106,
}

impl From<Property> for nfc1_sys::nfc_property {
	fn from(input: Property) -> nfc1_sys::nfc_property {
		match input {
			Property::TimeoutCommand => nfc1_sys::nfc_property_NP_TIMEOUT_COMMAND,
			Property::TimeoutAtr => nfc1_sys::nfc_property_NP_TIMEOUT_ATR,
			Property::TimeoutCom => nfc1_sys::nfc_property_NP_TIMEOUT_COM,
			Property::HandleCrc => nfc1_sys::nfc_property_NP_HANDLE_CRC,
			Property::HandleParity => nfc1_sys::nfc_property_NP_HANDLE_PARITY,
			Property::ActivateField => nfc1_sys::nfc_property_NP_ACTIVATE_FIELD,
			Property::ActivateCrypto1 => nfc1_sys::nfc_property_NP_ACTIVATE_CRYPTO1,
			Property::InfiniteSelect => nfc1_sys::nfc_property_NP_INFINITE_SELECT,
			Property::AcceptInvalidFrames => nfc1_sys::nfc_property_NP_ACCEPT_INVALID_FRAMES,
			Property::AcceptMultipleFrames => nfc1_sys::nfc_property_NP_ACCEPT_MULTIPLE_FRAMES,
			Property::AutoIso144434 => nfc1_sys::nfc_property_NP_AUTO_ISO14443_4,
			Property::EasyFraming => nfc1_sys::nfc_property_NP_EASY_FRAMING,
			Property::ForceIso14443A => nfc1_sys::nfc_property_NP_FORCE_ISO14443_A,
			Property::ForceIso14443B => nfc1_sys::nfc_property_NP_FORCE_ISO14443_B,
			Property::ForceSpeed106 => nfc1_sys::nfc_property_NP_FORCE_SPEED_106,
		}
	}
}

/// Safe version of nfc_modulation_type
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ModulationType {
	Iso14443a,
	Jewel,
	Iso14443b,
	Iso14443bi,
	Iso14443b2sr,
	Iso14443b2ct,
	Felica,
	Dep,
	Barcode,
	Iso14443biClass,
	Undefined,
}

impl ToString for ModulationType {
	fn to_string(&self) -> String {
		let modulation_type: nfc1_sys::nfc_modulation_type = self.clone().into();
		return unsafe { CStr::from_ptr(str_nfc_modulation_type(modulation_type)) }.to_string_lossy().into_owned();
	}
}

impl From<ModulationType> for nfc1_sys::nfc_modulation_type {
	fn from(input: ModulationType) -> nfc1_sys::nfc_modulation_type {
		match input {
			ModulationType::Iso14443a => nfc1_sys::nfc_modulation_type_NMT_ISO14443A,
			ModulationType::Jewel => nfc1_sys::nfc_modulation_type_NMT_JEWEL,
			ModulationType::Iso14443b => nfc1_sys::nfc_modulation_type_NMT_ISO14443B,
			ModulationType::Iso14443bi => nfc1_sys::nfc_modulation_type_NMT_ISO14443BI,
			ModulationType::Iso14443b2sr => nfc1_sys::nfc_modulation_type_NMT_ISO14443B2SR,
			ModulationType::Iso14443b2ct => nfc1_sys::nfc_modulation_type_NMT_ISO14443B2CT,
			ModulationType::Felica => nfc1_sys::nfc_modulation_type_NMT_FELICA,
			ModulationType::Dep => nfc1_sys::nfc_modulation_type_NMT_DEP,
			ModulationType::Barcode => nfc1_sys::nfc_modulation_type_NMT_BARCODE,
			ModulationType::Iso14443biClass => nfc1_sys::nfc_modulation_type_NMT_ISO14443BICLASS,
			ModulationType::Undefined => 0,
		}
	}
}

impl From<nfc1_sys::nfc_modulation_type> for ModulationType {
	fn from(input: nfc1_sys::nfc_modulation_type) -> Self {
		match input {
			nfc1_sys::nfc_modulation_type_NMT_ISO14443A => ModulationType::Iso14443a,
			nfc1_sys::nfc_modulation_type_NMT_JEWEL => ModulationType::Jewel,
			nfc1_sys::nfc_modulation_type_NMT_ISO14443B => ModulationType::Iso14443b,
			nfc1_sys::nfc_modulation_type_NMT_ISO14443BI => ModulationType::Iso14443bi,
			nfc1_sys::nfc_modulation_type_NMT_ISO14443B2SR => ModulationType::Iso14443b2sr,
			nfc1_sys::nfc_modulation_type_NMT_ISO14443B2CT => ModulationType::Iso14443b2ct,
			nfc1_sys::nfc_modulation_type_NMT_FELICA => ModulationType::Felica,
			nfc1_sys::nfc_modulation_type_NMT_DEP => ModulationType::Dep,
			nfc1_sys::nfc_modulation_type_NMT_BARCODE => ModulationType::Barcode,
			nfc1_sys::nfc_modulation_type_NMT_ISO14443BICLASS => ModulationType::Iso14443biClass,
			_ => ModulationType::Undefined,
		}
	}
}

/// Safe version of nfc_modulation
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Modulation {
	pub modulation_type: ModulationType,
	pub baud_rate: BaudRate,
}

impl From<&Modulation> for nfc1_sys::nfc_modulation {
	fn from(input: &Modulation) -> nfc1_sys::nfc_modulation {
		nfc1_sys::nfc_modulation {
			nmt: input.modulation_type.into(),
			nbr: input.baud_rate.into(),
		}
	}
}

impl From<nfc1_sys::nfc_modulation> for Modulation {
	fn from(input: nfc1_sys::nfc_modulation) -> Self {
		Self{
			modulation_type: input.nmt.into(),
			baud_rate: input.nbr.into(),
		}
	}
}

/// Safe version of nfc_dep_mode
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DepMode {
	Undefined,
	Passive,
	Active,
}

impl From<DepMode> for nfc1_sys::nfc_baud_rate {
	fn from(input: DepMode) -> nfc1_sys::nfc_baud_rate {
		match input {
			DepMode::Undefined => nfc1_sys::nfc_dep_mode_NDM_UNDEFINED,
			DepMode::Passive => nfc1_sys::nfc_dep_mode_NDM_PASSIVE,
			DepMode::Active => nfc1_sys::nfc_dep_mode_NDM_ACTIVE,
		}
	}
}

impl From<nfc1_sys::nfc_baud_rate> for DepMode {
	fn from(input: nfc1_sys::nfc_baud_rate) -> DepMode {
		match input {
			nfc1_sys::nfc_dep_mode_NDM_PASSIVE => DepMode::Passive,
			nfc1_sys::nfc_dep_mode_NDM_ACTIVE => DepMode::Active,
			_ => DepMode::Undefined,
		}
	}
}

// Misc. functions

pub fn iso14443a_crc(data: &mut [u8]) -> Vec<u8> {
	let mut crc = vec![0u8; 2];
	unsafe { nfc_iso14443a_crc(data.as_mut_ptr(), data.len(), crc.as_mut_ptr()) };
	crc
}

pub fn iso14443a_crc_append(data: &mut Vec<u8>) {
	unsafe { nfc_iso14443a_crc_append(data.as_mut_ptr(), data.len()) }
}

pub fn iso14443b_crc(data: &mut [u8]) -> Vec<u8> {
	let mut crc = vec![0u8; 2];
	unsafe { nfc_iso14443b_crc(data.as_mut_ptr(), data.len(), crc.as_mut_ptr()) };
	crc
}

pub fn iso14443b_crc_append(data: &mut Vec<u8>) {
	unsafe { nfc_iso14443b_crc_append(data.as_mut_ptr(), data.len()) }
}

pub fn iso14443a_locate_historical_bytes(ats: &[u8]) -> Option<&[u8]> {
	if ats.len() != 0 {
		let mut offset = 1;
		if ats[0] & 0x10 != 0 { // TA
			offset += 1;
		}
		if ats[0] & 0x20 != 0 { // TB
			offset += 1;
		}
		if ats[0] & 0x40 != 0 { // TC
			offset += 1;
		}
		if ats.len() > offset {
			// *pszTk = (szAts - offset);
			return Some(&ats[offset..]);
		}
	}
	None
}

pub fn version() -> &'static str {
	// SAFETY: nfc_version returns a constant string (#define)
	unsafe { CStr::from_ptr(nfc_version()) }.to_str().unwrap()
}