Ok, been wanting to get into Rust a while now and also work on my NFC projects.
So I'm using nfc1-sys crate with my project right now.
nfc1-sys is just bindings to libnfc C-library if I understand correctly? I'm still learning to work with pointers from C code, but I think I figured a bit out already, until now.
The problem now is I have this libnfc C-function that is supposed to fill a array (targets) of a specific type with info about NFC tags detected.
Here is the func I'm calling:
extern "C" {
pub fn nfc_initiator_list_passive_targets(
pnd: *mut nfc_device,
nm: nfc_modulation,
ant: *mut nfc_target,
szTargets: size_t,
) -> ::std::os::raw::c_int;
}
And here is my code I have tried using it:
// let mut targets: [MaybeUninit<nfc_target>; 8] = MaybeUninit::uninit_array();
// let mut targets: [MaybeUninit<nfc_target>; 8] = MaybeUninit::uninit().transpose();
let result = unsafe {
nfc_initiator_list_passive_targets(self.device, modulation, targets, 8);
};
The part I'm having problems both getting to work and understanding is the "targets" argument.
If I understand libnfc and the bindings to Rust correctly I am supposed to get a array of the size 8 out of this function, every item in the array should be a nfc_target struct, filled with info.
I have already worked with libnfc in a C program and there I use the following code that works:
So here I create a array of the nfc_target struct and then pass that as a argument to the libnfc func that seems to accept a array (ant) fine:
nfc_initiator_list_passive_targets(nfc_device *pnd,
const nfc_modulation nm,
nfc_target ant[], const size_t szTargets)
nfc_target targets[8];
if (nfc_initiator_list_passive_targets(reader->device, modulation, targets, 8) > 0) {
printf("Found a chip (or more) in field, selecting first found DESFire\n");
}
After that I can access info about the first tag by using targets[0]
I want to do something similar in my Rust program.
Preferably get a array of targets (tags) back from the func so I can get info about them and then select the one I need, but I would be happy to just get the first tag in targets array.
Is there a problem with in the C lib its an actually , but in the Rust binding its a pointer?
When looking for tips and trying to figure it out, I also found this issue:
After that I switched to Rust nightly but I still can't figure it out.
It seems this functionality I'm after, or working with is a something that is subject to change so I feel even more lost about how I should figure this out.
I also want to learn if I'm doing and thinking correctly, so I have some questions about the code too:
As I understand it, if I want to use a pointer as a argument to a C func, and that C func changes the data the pointer points to (a out-pointer, is that the right term?) I need to use maybeUninit first on the argument, because Rust don't like uninitialized pointers, right?
The problem seems to be that I actually have to initialize my pointer with a array of maybeUninit and the type of the struct, and that seems to complicate it a bit much for me to figure it out.
This code complains about a unstable feature so I switched to Nightly, but I'm unsure if I'm using it correctly, like this line:
let mut targets: [MaybeUninit<nfc_target>; 8] = MaybeUninit::uninit_array();
But how do I actually then pass the pointer to the libnfc func when the func seems to accept a *mut nfc_target type?