| Crates.io | freefare-sys |
| lib.rs | freefare-sys |
| version | 0.2.1 |
| created_at | 2016-06-11 00:13:46.06002+00 |
| updated_at | 2024-12-20 11:52:57.170849+00 |
| description | FFI bindings for the libfreefare library |
| homepage | https://github.com/dsgriffin/freefare-sys |
| repository | https://github.com/dsgriffin/freefare-sys |
| max_upload_size | |
| id | 5345 |
| size | 38,692 |
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.
You need to install libfreefare and libnfc before using this crate.
brew install libfreefare libnfc
sudo apt install libfreefare-dev libnfc-dev
If the two libraries above are not installed in the standard Homebrew or APT locations, you can override the following environment variables:
See build.rs for more details on how it works if needed.
Be sure to include libc too.
[dependencies]
libc = "0.2.0"
freefare-sys = "0.2.1"
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);
}
}
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!
MIT