0

I am trying to use libfreefare in python and I decided to use ctypes. I understood how to reproduct structures in Python, however I don't know the way to reproduce a specific thing.

Consider this structure :

struct mifare_tag {
    nfc_device *device;
    nfc_iso14443a_info info;
    const struct supported_tag *tag_info;
    int active;
};

And the following typedef :

typedef struct mifare_tag *MifareTag;

How to make the equivalent of MifareTag in ctypes ?

1 Answer 1

1

This is a run-of-the-mill struct declaration in ctypes. Something like this (without knowing the nfc_*-types, it can't be complete obviously.

from ctypes import Structure, POINTER, c_int


class mifare_tag(Structure):

    _fields_ = [
        ("device", POINTER(nfc_device)),
        ("info", nfc_iso14443a_info),
        ("tag_info", POINTER(supported_tag)),
        ("active", c_int),
        ]


MifareTag = POINTER(mifare_tag)
Sign up to request clarification or add additional context in comments.

7 Comments

I'm only starting with ctypes, so just to be sure about your code : MifareTag would be a class that I can use as a normal one, right ?
@LukeMarlin, MifareTag is a pointer type.
eryksun says it - MifareTag is a pointer. Don't be confused by the sprinkling of "struct" here and there, it's just idiosyncratic C.The important bit of the typedef is, that it defines MifareTag to be of type "struct mifare_tag ". So no, it is *not a normal object. Now it depends on your library/code. If you get values of MifareTag back from the lib, just use the contents-attribute to access the various items in it. If you have to create a new tag, use the mifare_tag-class, and the pointer or byref calls to pass it to the lib.
@deets Thank you, so I can create MifareTag in python, which would effectively be a wrapper of a mifare_tag value, right ?
I am currently trying to make other struct definitions, but each one leads to many others and I foresee it will be tedious. Should I switch to something like cython, or swig ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.