0
votes

For several days I'm trying to integrate API given as dll with python using ctypes. It went well until I got stuck on accessing output parameters returned by callback function. At the beggining I decided to use pyclibrary so it would create ctypes types for me by parsing API header file. When I encountered previously mentioned problem I also tried classic approach with manual types creation. Effect is the same.

API callback function and structures prototypes provided in the header:

 typedef struct
 {
    unsigned short size;
    error_status error;
    unsigned short detector;
    unsigned short module;
    void* data;

 } Event;

typedef void ( *eventCallback_t )( const event_id eventID, const Event* eventData, void* customData);

My problem is hot to get to Event.data provided by callback. Event.data contains different data depending on the event type. Currently I focus on this one:

 typedef struct
 {
   unsigned short nbDetectors;
   //! structure of detector status
   struct GeneralStatus
   {
      unsigned int lastError;           
      float temperature;                
      detector_state state;         
      unsigned short mode;          

      struct EnergyStatus
      {
        power_source powerSource;           
        frontend_position frontendPosition; 

        struct BatteryStatus
        {
            bool present;                       
            unsigned short charge;              
            float voltageLevel;                 
            float temperature;                  
            unsigned short chargeCycles;        
            unsigned short accuracy;            
            bool needCalibration;               
            unsigned short fullChargeCapacity;  

        } batteryStatus;

    }* energyStatus;

  }* generalStatus;

} HardwareStatus;

My ctypes definitions:

       class Event(Structure):
          _fields_ = [
            ("size", c_ushort),
            ("error_status", c_ushort),
            ("detector", c_ushort),
            ("module", c_ushort),
            ("data", c_void_p)
        ]
      
       class BatteryStatus(Structure):
          _fields_ = [
            ("present", c_bool),
            ("charge", c_ushort),
            ("voltageLevel", c_float),
            ("temperature", c_float),
            ("chargeCycles", c_ushort),
            ("accuracy", c_ushort),
            ("needCalibration", c_bool),
            ("fullchargeCapacity", c_ushort),

        ]

    class EnergyStatus(Structure):
        _fields_ = [
            ("powerSource", c_ushort),
            ("frontendPosition", c_ushort),
            ("batteryStatus", POINTER(BatteryStatus))
        ]
    class GeneralStatus(Structure):
        _fields_ = [
            ("lastError", c_uint),
            ("temperature", c_float),
            ("state", c_ushort),
            ("mode", c_ushort),
            ("energyStatus", POINTER(EnergyStatus)),
        ]

    class HardwareStatus(Structure):
        _fields_ = [
            ("nbDetectors", c_ushort),
            ("generalStatus", POINTER(GeneralStatus))
        ]

My ctypes callback handler is defined as follows:

def event_callback(self, event_id, event, custom_data):
    if event_id == self.api.EVT_HARDWARE_STATUS:    
        hw_status = HardwareStatus(event[0].data) 
        print("HW_STATUS for" + str(hw_status.nbDetectors))
        status = hw_status.detectorStatus[0]
        print(str(status.lastError))

My ctypes callback definition:

   EVTCALLBACK = CFUNCTYPE(c_ushort, POINTER(Event), c_void_p)

I can easily get Event.data.nbDetectors but on trying to access Event.data.generalStatus[0] I'm getting NULL pointer access error

The code I provided is one of several versions I've tested already. Probably I'm missing some knowledge about ctypes. I will be grateful for any help, tips etc.

1
One thing I noticed is BatteryStatus is an embedded structure in EnergyStatus, not a pointer. Make sure the C sizeof() your structures matches the ctypes.sizeof() your Python definitions. You may need to define __pack__ for your structures if padding is involved. - Mark Tolonen

1 Answers

0
votes

It seems I have a knowledge gap in my coding knowledge. When using void pointers casting is needed:

cast(event[0].data, POINTER(HardwareStatus))