简体   繁体   中英

structures with functions and python ctypes

I have ac library I would like to wrap with python 2.7 ctypes.

typedef struct SRSLTE_API{
  void *ptr;
  uint32_t R;
  uint32_t K;
  uint32_t framebits;
  bool tail_biting;
  float gain_quant; 
  int16_t gain_quant_s; 
  int (*decode) (void*, uint8_t*, uint8_t*, uint32_t);
  int (*decode_f) (void*, float*, uint8_t*, uint32_t);
  void (*free) (void*);
  uint8_t *tmp;
  uint8_t *symbols_uc;
}srslte_viterbi_t;

How do I create this structure in python? This is what I currently have.

from ctypes import *
class srslte_viterbi_t(Structure):
    _fields_ = [("ptr", c_void_p),
                ("R", c_uint),
                ("K", c_uint),
                ("framebits", c_uint),
                ("tail_biting", c_bool),
                ("gain_quant", c_float),
                ("gain_quant_s", c_short),
                ("decode", POINTER(c_int)),
                ("decode_f", POINTER(c_int)),
                ("free", c_void_p),
                ("tmp", POINTER(c_ubyte)),
                ("symbols_uc", POINTER(c_ubyte))
                ]
viterbi_t = srslte_viterbi_t(None, c_uint(0), c_uint(0), c_uint(0), 
    c_bool(False), c_float(0.0), c_short(0), None, None, None, None, None)

This structure compiles but does not give the correct results. I fear I am not allocating the decode functions correctly? What is the line

int (*decode) (void*, uint8_t*, uint8_t*, uint32_t);

Doing in a structure anyway?

This is the correct definition of the struct:

from ctypes import *

class srslte_viterbi_t(Structure):
    _fields_ = [
        ('ptr',c_void_p),
        ('R',c_uint32),
        ('K',c_uint32),
        ('framebits',c_uint32),
        ('tail_biting',c_bool),
        ('gain_quant',c_float),
        ('gain_quant_s',c_int16),
        ('decode',CFUNCTYPE(c_int,c_void_p,POINTER(c_uint8),POINTER(c_uint8),c_uint32)),
        ('decode_f',CFUNCTYPE(c_int,c_void_p,POINTER(c_float),POINTER(c_uint8),c_uint32)),
        ('free',CFUNCTYPE(None,c_void_p)),
        ('tmp',POINTER(c_uint8)),
        ('symbols_uc',POINTER(c_uint8))]

viterbi_t = srslte_viterbi_t()

The structure is zero-initialized by ctypes when instantiated.

The decode line in the structure is a function pointer. CFUNCTYPE is used to define the function pointer return type and arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM