Fixed convert string function in python wrapper.
[libftdi] / bindings / ftdi1.i
CommitLineData
bcf4a08b 1/* File: ftdi1.i */
4c5afeb9 2
bcf4a08b 3%module(docstring="Python interface to libftdi1") ftdi1
31fba51d 4%feature("autodoc","1");
f9e62be7
DW
5#ifdef DOXYGEN
6%include "ftdi1_doc.i"
7#endif
4c5afeb9 8
abc3a514
MZ
9%{
10#include "Python.h"
11
3b96824e
MZ
12PyObject* convertString( const char *v, Py_ssize_t len )
13{
abc3a514 14#if PY_MAJOR_VERSION >= 3
3b96824e 15 return PyBytes_FromStringAndSize(v, len);
abc3a514 16#else
3b96824e 17 return PyString_FromStringAndSize(v, len);
abc3a514 18#endif
3b96824e 19}
abc3a514
MZ
20%}
21
50280d45
MZ
22%include <typemaps.i>
23%include <cstring.i>
f5b59bd6 24
05c0dae8 25%typemap(in) unsigned char* = char*;
4c5afeb9 26
b060307c
TJ
27%immutable ftdi_version_info::version_str;
28%immutable ftdi_version_info::snapshot_str;
29
4c5afeb9 30%rename("%(strip:[ftdi_])s") "";
b790d38e 31
50280d45
MZ
32%newobject ftdi_new;
33%typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);";
34%delobject ftdi_free;
35
31fba51d
MZ
36%define ftdi_usb_find_all_docstring
37"usb_find_all(context, vendor, product) -> (return_code, devlist)"
38%enddef
39%feature("autodoc", ftdi_usb_find_all_docstring) ftdi_usb_find_all;
50280d45
MZ
40%typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) %{ $1 = &temp; %}
41%typemap(argout) SWIGTYPE** OUTPUT %{ $result = SWIG_Python_AppendOutput($result, SWIG_NewPointerObj((void*)*$1,$*descriptor,0)); %}
42%apply SWIGTYPE** OUTPUT { struct ftdi_device_list **devlist };
43 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
44 int vendor, int product);
45%clear struct ftdi_device_list **devlist;
46
31fba51d
MZ
47%define ftdi_usb_get_strings_docstring
48"usb_get_strings(context, device) -> (return_code, manufacturer, description, serial)"
49%enddef
50%feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings;
50280d45 51%apply char *OUTPUT { char * manufacturer, char * description, char * serial };
4c5afeb9
MZ
52%cstring_bounded_output( char * manufacturer, 256 );
53%cstring_bounded_output( char * description, 256 );
54%cstring_bounded_output( char * serial, 256 );
50280d45 55%typemap(default,noblock=1) int mnf_len, int desc_len, int serial_len { $1 = 256; }
4c5afeb9
MZ
56 int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
57 char * manufacturer, int mnf_len,
58 char * description, int desc_len,
59 char * serial, int serial_len);
50280d45
MZ
60%clear char * manufacturer, char * description, char * serial;
61%clear int mnf_len, int desc_len, int serial_len;
05c0dae8 62
31fba51d
MZ
63%define ftdi_read_data_docstring
64"read_data(context) -> (return_code, buf)"
65%enddef
66%feature("autodoc", ftdi_read_data_docstring) ftdi_read_data;
50d77f8f 67%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
abc3a514 68%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
05c0dae8 69 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
50d77f8f 70%clear (unsigned char *buf, int size);
f0005c33 71
05c0dae8
TH
72%apply int *OUTPUT { unsigned int *chunksize };
73 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
74 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
75%clear unsigned int *chunksize;
f0005c33 76
31fba51d
MZ
77%define ftdi_read_pins_docstring
78"read_pins(context) -> (return_code, pins)"
79%enddef
80%feature("autodoc", ftdi_read_pins_docstring) ftdi_read_pins;
05c0dae8
TH
81%apply char *OUTPUT { unsigned char *pins };
82 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
83%clear unsigned char *pins;
f0005c33 84
05c0dae8
TH
85%apply char *OUTPUT { unsigned char *latency };
86 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
f0005c33
TJ
87%clear unsigned char *latency;
88
4c5afeb9 89%apply short *OUTPUT { unsigned short *status };
05c0dae8 90 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
f0005c33
TJ
91%clear unsigned short *status;
92
4c5afeb9
MZ
93%apply int *OUTPUT { int* value };
94 int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
95%clear int* value;
f0005c33 96
50d77f8f 97%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
abc3a514 98%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
50280d45 99 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
50d77f8f 100%clear (unsigned char *buf, int size);
50280d45 101
31fba51d
MZ
102%define ftdi_read_eeprom_location_docstring
103"read_eeprom_location(context, eeprom_addr) -> (return_code, eeprom_val)"
104%enddef
105%feature("autodoc", ftdi_read_eeprom_location_docstring) ftdi_read_eeprom_location;
4c5afeb9
MZ
106%apply short *OUTPUT { unsigned short *eeprom_val };
107 int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
108%clear unsigned short *eeprom_val;
f0005c33 109
31fba51d
MZ
110%define ftdi_read_eeprom_docstring
111"read_eeprom(context) -> (return_code, eeprom)"
112%enddef
113%feature("autodoc", ftdi_read_eeprom_docstring) ftdi_read_eeprom;
114
115%define ftdi_read_chipid_docstring
116"read_pins(context) -> (return_code, chipid)"
117%enddef
118%feature("autodoc", ftdi_read_chipid_docstring) ftdi_read_chipid;
05c0dae8
TH
119%apply int *OUTPUT { unsigned int *chipid };
120 int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
121%clear unsigned int *chipid;
122
4c5afeb9
MZ
123%include ftdi.h
124%{
125#include <ftdi.h>
126%}
127
128%include ftdi_i.h
129%{
130#include <ftdi_i.h>
131%}