[Q] Howto Send/Return "unsigned char" arrays between "C" and "Perl5" Swig?
Ricky Marek <ricky.marek <at> nxp.com>
2009-07-02 14:29:48 GMT
Hello,
I'm new to SWIG.
The Scenario: I have some code that needs to interface with a low level driver that will receive or
send buffers through some hardware device (I2C in my case), and be able to run it from a Perl script.
Running native "C" code allows me to do all the required operations, but I'm required to run the same
from Perl.
The 'C' functions I want to be wrapped (and be visible to Perl) have the following signature:
int i2c_write(I2C_HANDLE *handle, int count, unsigned char *input_buffer);
int i2c_read(I2C_HANDLE *handle, int count, unsigned char *output_buffer);
Both the input and output buffers will contain binary data (not strings) and are pointers to an array,
previously allocated with enough room to work.
As far I have seen with SWIG is that "char *" (or/and unsigned "char *") is dealt in a special way
that can be used as an array...
The interface from Perl that I expect is that I will prepare an Perl array containing the characters
to be sent, or receive an array with the characters received. Is this possible?
<at> buffer = ( 10, 255, 23, 12, 70, 1 .. 255);
$count = I2C::write($handle, <at> buffer);
...
$count = I2C::read($handle, <at> buffer);
foreach $i ( <at> buffer(
{
print "Got $i\n";
}
Also the use of "pack" or "unpack" is acceptable.
Currently I'm using a "carrays.i" for this task, which adds a lot of overhead on the perl scripts..
and also a small warning on the manual tells me not to use "char *" with "%array_functions"
(end of section 8.2.2)
[Q] Is it safe to use "%array_functions" with "unsigned char *"?
Thanks in advance
--- Ricky Marek.
------------------------------------------------------------------------------