Skip to main content

Crate nfc

Crate nfc 

Source
Expand description

Safe Rust bindings for libnfc built on top of [nfc-sys].

The ffi module re-exports the raw nfc-sys API for advanced use cases. The top-level API in this crate focuses on ownership, resource cleanup, and slice-based wrappers for the parts of libnfc that can be expressed safely.

Some libnfc capabilities remain raw-only because the C API itself requires unchecked callbacks or driver pointers. In particular, custom driver registration and target emulation state machines still live in ffi.

§Native Dependency

This crate links against a system installation of libnfc. You must install the native library before building or running code that depends on nfc. The underlying nfc-sys build script handles the actual linking step.

Common setups:

  • macOS: brew install libnfc
  • Debian/Ubuntu: install the libnfc development package
  • Custom installs: configure your linker environment as needed for libnfc

If you need a libnfc capability that is not exposed through the safe API, use ffi directly and keep the native library installed as above.

§Example

use nfc::{version, Context};

fn main() -> nfc::Result<()> {
    let context = Context::new()?;
    println!("libnfc version: {}", version());

    for connstring in context.list_devices(8)? {
        println!("found device: {connstring}");
    }

    Ok(())
}

Re-exports§

pub use crate::error::Error;
pub use crate::error::Result;

Modules§

error
ffi
Raw re-exports from [nfc-sys].

Structs§

Context
Owns a libnfc context and releases it with nfc_exit on drop.
Device
Owns an open NFC device and closes it with nfc_close on drop.
TimedResponse
Result of a timed transceive operation.

Functions§

append_iso14443a_crc
Appends an ISO14443-A CRC to the provided buffer.
append_iso14443b_crc
Appends an ISO14443-B CRC to the provided buffer.
baud_rate_name
Converts a baud rate to the corresponding libnfc display string.
iso14443a_crc
Computes the ISO14443-A CRC for the provided data.
iso14443b_crc
Computes the ISO14443-B CRC for the provided data.
locate_iso14443a_historical_bytes
Locates the ISO14443-A historical bytes inside an ATS buffer.
modulation_type_name
Converts a modulation type to the corresponding libnfc display string.
target_to_string
Formats an NFC target using libnfc’s built-in target formatter.
version
Returns the linked libnfc version string as UTF-8.
version_cstr
Returns the linked libnfc version string.