#communication #nfc #devices #libnfc #libfreefare

sys freefare-sys

FFI bindings for the libfreefare library

5 releases

Uses old Rust 2015

new 0.2.1 Dec 20, 2024
0.2.0 Jan 30, 2018
0.1.2 Oct 15, 2016
0.1.1 Jun 21, 2016
0.1.0 Jun 11, 2016

#1506 in Hardware support

Download history 29/week @ 2024-08-29 19/week @ 2024-09-05 18/week @ 2024-09-12 29/week @ 2024-09-19 40/week @ 2024-09-26 16/week @ 2024-10-03 10/week @ 2024-10-10 13/week @ 2024-10-17 11/week @ 2024-10-24 28/week @ 2024-10-31 12/week @ 2024-11-07 5/week @ 2024-11-14 17/week @ 2024-11-21 16/week @ 2024-11-28 29/week @ 2024-12-05 37/week @ 2024-12-12

100 downloads per month
Used in 3 crates (2 directly)

MIT license

38KB
889 lines

freefare-sys

Crates.io

freefare-sys provides FFI bindings to libfreefare.

Following the *-sys package conventions, the freefare-sys package does not define higher-level abstractions over the native library.

Requirements

You need to install libfreefare and libnfc before using this crate.

MacOS + Homebrew

brew install libfreefare libnfc

Ubuntu + APT

sudo apt install libfreefare-dev libnfc-dev

Custom Paths

If the two libraries above are not installed in the standard Homebrew or APT locations, you can override the following environment variables:

  • LIBFREEFARE_PATH: Path to libfreefare (e.g. /path/to/libfreefare/lib).
  • LIBNFC_PATH: Path to libnfc (e.g. /path/to/libnfc/lib).

See build.rs for more details on how it works if needed.

Add it to your Cargo.toml

Be sure to include libc too.

[dependencies]
libc = "0.2.0"
freefare-sys = "0.2.1"

Example Usage

Note: this example requires both the freefare_sys and nfc_sys crates.

extern crate nfc_sys;
extern crate freefare_sys;

use std::ffi::CStr;
use std::ptr;

fn main() {
    unsafe {
        // Initialize NFC context
        let mut context: *mut nfc_sys::nfc_context = ptr::null_mut();
        nfc_sys::nfc_init(&mut context);

        if context.is_null() {
            eprintln!("Failed to initialize NFC context.");
            return;
        }
        println!("NFC context initialized.");

        // Open the NFC device
        let device = nfc_sys::nfc_open(context, ptr::null());
        if device.is_null() {
            eprintln!("Failed to open NFC device.");
            nfc_sys::nfc_exit(context);
            return;
        }
        println!("NFC device opened.");

        // Get the list of Freefare tags
        let tags = freefare_sys::freefare_get_tags(device);

        if tags.is_null() {
            eprintln!("No Freefare tags found.");
        } else {
            let mut tag_ptr = tags;

            // Iterate through the array of tag pointers
            while !(*tag_ptr).is_null() {
                let tag = *tag_ptr;

                // Get and print the tag UID
                let uid = freefare_sys::freefare_get_tag_uid(tag);
                if !uid.is_null() {
                    let uid_str = CStr::from_ptr(uid).to_string_lossy();
                    println!("Found tag with UID: {}", uid_str);

                     // Free the UID string
                    freefare_sys::freefare_free_tag(uid as *mut _);
                }

                // Get and print the tag type
                let tag_type = freefare_sys::freefare_get_tag_type(tag);
                match tag_type {
                    freefare_sys::Enum_freefare_tag_type::MIFARE_CLASSIC_1K => {
                        println!("Tag type: MIFARE Classic 1K");
                    }
                    freefare_sys::Enum_freefare_tag_type::MIFARE_CLASSIC_4K => {
                        println!("Tag type: MIFARE Classic 4K");
                    }
                    freefare_sys::Enum_freefare_tag_type::MIFARE_ULTRALIGHT => {
                        println!("Tag type: MIFARE Ultralight");
                    }
                    _ => println!("Unknown tag type"),
                }

                // Move to the next tag in the array
                tag_ptr = tag_ptr.add(1);
            }
        }

        // Clean up the list of tags
        freefare_sys::freefare_free_tags(tags);
        println!("Completed Freefare tag scan.");

        // Close NFC device and exit context
        nfc_sys::nfc_close(device);
        nfc_sys::nfc_exit(context);
    }
}

Contributing

If you've found a bug or have an idea, feel free to open an Issue. If you've got a fix or feature ready, open a PR. Thanks!

License

MIT

Dependencies

~65KB