c - How to pass N bytes of parameters to a function called by pointer -


my software drive embedded device run c code on ti dsp tms320f2812.

the communication done via usb serial port emulation.

at point, device side, need parse message means "call function @ given address given parameters". message contains:

  • 4 bytes function address.
  • 1 byte parameter(s) size (in byte).
  • n bytes parameter(s) data.

here code use:

typedef void    (*void_fct_void)    (void); typedef void    (*void_fct_int16)   (int16); typedef void    (*void_fct_int32)   (int32); typedef void    (*void_fct_2int32)  (int32, int32);  ...  uint32 address; uint16 sizein;  address = hw_usb_read_4bytes(); sizein = hw_usb_read_1byte();  switch(sizein) {     case 0:         ((void_fct_void) address)();         break;                   case 2:         ((void_fct_int16) address)(hw_usb_read_2bytes());         break;     case 4:         ((void_fct_int32) address)(hw_usb_read_4bytes());         break;     case 8:         ((void_fct_2int32) address)(hw_usb_read_4bytes(), hw_usb_read_4bytes());         break; } 

i wonder if there no way make more generic , avoid switch, like:

uint32 address; uint16 sizein;  address = hw_usb_read_4bytes(); sizein = hw_usb_read_1byte();  putnbytesonparamsstack(sizein); // magic function, call hw_usb_read_1byte 'sizein' times. ((void_fct_void) address)(); 

also target functions numerous , used embedded code, can't change signatures.

you might use libffi (if supports target processor architecture, calling conventions, , abi) enables "emulate" arbirary call.

however, should pass pointer data, not data itself.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -