4
votes

I'm writing framework in Python 2.7 which would provide functionality to test C written APIs. I have the DLL and the C source code itself. The problem is that some of the C API functions require as an input structure of a callback functions and I can't find the way how to create structure in Python which fields are the C function pointers. I know how to create callback function in Python using ctypes, but I need to pack those callbacks in C structure to pass to C API. All these in code looks like this:

The C function pointers and the C structure:

#define DRIVERCALLAPI
typedef void*( DRIVERCALLAPI *fn_DRV_COMM_Open )(void*);
typedef void(DRIVERCALLAPI *fn_DRV_COMM_Close)(void*);
typedef int(DRIVERCALLAPI *fn_DRV_COMM_Transfer)(void *, unsigned char *, int);

typedef struct DriverCallbacks_t
{
    fn_DRV_COMM_Open                drvcomm_Open;
    fn_DRV_COMM_Close               drvcomm_Close;
    fn_DRV_COMM_Transfer            drvcomm_Transfer;
} DriverCallbacks;

typedef struct InitDataEntry_t
{
    char iface[64];
    void* handle;
} InitDataEntry;

Where handle points to an object of DriverCallbacks.

typedef struct InitDataContainer_t
{
    uint32_t size;
    uint32_t id; 
    InitDataEntry* data;
} InitDataContainer;

The pointer of a InitDataContainer should be passed to the API function.

void* dev_Create( void* args )

The API initializes the callback functions with appropriate functions to use them later. I need to somehow create Python structures of DriverCallbacks InitDataEntry and InitDataContainer. Any hint on how it can be achieved ? Thanks in advance !

1

1 Answers

2
votes

After many experiments I finally found how to create Python structures which correspond to C structures with function pointers as fields. The idea is to use void pointers instead i.e. c_void_p from ctypes. For the provided example the python code would be:

from ctypes import *
class DriverCallbacks(Structure):
    _fields_ = [
        ("drvcomm_Open",     c_void_p),
        ("drvcomm_Close",    c_void_p),
        ("drvcomm_Transfer", c_void_p)
    ]

class InitDataEntry(Structure):
    _fields_ = [
        ("iface",   64 * c_byte),
        ("handle",  c_void_p)
    ]

class InitDataContainer(Structure):
    _fields_ = [
        ("size",    c_uint),
        ("id",      c_uint),
        ("data",    POINTER(InitDataEntry))
    ]

The creation of the objects and the library function call could be like this (it's tested and works for me):

lib = cdll.LoadLibrary(LIBNAME)

driverFuncList = DriverCallbacks()
driverFuncList.drvcomm_Open = cast(lib.ftdi_open, c_void_p)
driverFuncList.drvcomm_Close = cast(lib.ftdi_close, c_void_p)
driverFuncList.drvcomm_Transfer = cast(lib.ftdi_transfer, c_void_p)

initData = InitDataEntry()
libc = cdll.msvcrt
libc.strcpy(byref(initData.iface), c_char_p("DriverCallbacks"))
initData.handle = cast(pointer(driverFuncList), c_void_p)

initDataCont = InitDataContainer()
initDataCont.size = c_uint(3)
initDataCont.id = c_uint(0)
initDataCont.data = pointer(initData)

ret = lib.dev_Create(byref(initDataCont))

The driverFuncList object can be filled also from within the C library if there is a such function which sets the callback function pointers.