#bindings #low-level #safe-bindings #libnfc #nfc #high-level #nfc1

sys nfc1-sys

Low-level Rust bindings for libnfc. For high-level safe bindings, see crate nfc1.

10 releases

0.3.5 May 23, 2023
0.3.4 Mar 11, 2023
0.3.1 Jan 13, 2023
0.3.0 Dec 7, 2022
0.1.4 Apr 27, 2021

#1335 in Hardware support

Download history 1/week @ 2024-02-07 88/week @ 2024-02-14 17/week @ 2024-02-21 17/week @ 2024-02-28 9/week @ 2024-03-06 37/week @ 2024-03-13 1/week @ 2024-03-20 18/week @ 2024-03-27 42/week @ 2024-04-03 8/week @ 2024-04-10

69 downloads per month
Used in 4 crates (via nfc1)

MIT license

1MB
20K SLoC

C 19K SLoC // 0.2% comments Automake 430 SLoC // 0.0% comments Rust 373 SLoC // 0.0% comments M4 340 SLoC // 0.1% comments Shell 207 SLoC // 0.3% comments Batch 95 SLoC Bitbake 33 SLoC

Contains (obscure autoconf code, 7KB) vendor/nfc/configure.ac, (obscure autoconf code, 3KB) vendor/usb-compat-0.1/configure.ac

nfc1-sys

Crates.io

This crate provides low-level bindings to libnfc, generated by bindgen.

In contrast to nfc-sys, this crate additionally provides:

  • Metadata which allows dependent crates to find the nfc/nfc.h header, compile native code that depends on libnfc or link to it in Rust code.
  • Vendored submodule copy of libnfc (with build tweaks for x86_64-pc-windows-msvc), which means you don't have to separately install libnfc to use this crate. The vendoring is optional and can be disabled by removing the vendored feature.

Features

Feature Default? Description
vendored Yes Use vendored libnfc, instead of installed one on the platform.
drivers Yes Add chip or driver specific symbols from vendored libnfc.
logging No Enables logging when using the vendored libnfc.
conffiles No Enables config files on vendored libnfc.
envvars No Enables environment variables on vendored libnfc.

Usage

Add nfc1-sys as a dependency in your project's Cargo.toml file:

[dependencies]
nfc1-sys = "0.3"

Import the nfc1_sys crate in your project, then you can use all functions starting with nfc_ from libnfc.

See the libnfc wiki or libnfc 1.8.0 examples for information on how to use it. The API is the same, as this is just a binding.

Usage example

Note that there is quite a lot of unsafe code here. This is because this is just a simple binding, not a safe wrapper.

extern crate nfc1_sys;
use std::ffi::CStr;
use std::mem::MaybeUninit;
use nfc1_sys::{nfc_context, nfc_device, nfc_version, nfc_init, nfc_open, nfc_device_get_name, nfc_close, nfc_exit};

fn main() {
	let version = unsafe { CStr::from_ptr(nfc_version()) }.to_str().unwrap();
	println!("libnfc v{}", version);

	let context: *mut nfc_context = unsafe {
		let mut ctx = MaybeUninit::uninit();
		nfc_init(ctx.as_mut_ptr());
		ctx.assume_init()
	};

	let device: *mut nfc_device = unsafe {
		let pnd = nfc_open(context, std::ptr::null_mut());
		if pnd.as_ref().is_none() {
			nfc_exit(context);
			panic!("Error opening NFC reader");
		}
		pnd
	};

	let device_name = unsafe { CStr::from_ptr(nfc_device_get_name(device)) }.to_str().unwrap();
	println!("NFC reader: {} opened", device_name);

	unsafe {
		nfc_close(device);
		nfc_exit(context);
	}
}

Dependencies

~0–2.3MB
~46K SLoC