Structures with python functions and types
I have a c library that 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 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 correct results. I am afraid that I am not allocating decoding functions correctly? Which line
int (*decode) (void*, uint8_t*, uint8_t*, uint32_t);
Execution in a structure anyway?
+3
source to share
1 answer
This is the correct definition of the structure:
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()
After initialization, the structure is initialized to zero with ctypes
.
The decoding string in the structure is a function pointer. CFUNCTYPE
used to determine the return type and function pointer arguments.
+2
source to share