python bindings: python3 support
[libftdi] / bindings / ftdi1.i
1 /* File: ftdi1.i */
2
3 %module(docstring="Python interface to libftdi1") ftdi1
4
5 %{
6 #include "Python.h"
7
8 inline PyObject* convertString( const char *v, Py_ssize_t len )
9 #if PY_MAJOR_VERSION >= 3
10 { return PyUnicode_FromStringAndSize(v, len); }
11 #else
12 { return PyString_FromStringAndSize(v, len); }
13 #endif
14 %}
15
16 %include <typemaps.i>
17 %include <cstring.i>
18
19 %typemap(in) unsigned char* = char*;
20
21 %immutable ftdi_version_info::version_str;
22 %immutable ftdi_version_info::snapshot_str;
23
24 %rename("%(strip:[ftdi_])s") "";
25
26 %newobject ftdi_new;
27 %typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);";
28 %delobject ftdi_free;
29
30 %typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) %{ $1 = &temp; %}
31 %typemap(argout) SWIGTYPE** OUTPUT %{ $result = SWIG_Python_AppendOutput($result, SWIG_NewPointerObj((void*)*$1,$*descriptor,0)); %}
32 %apply SWIGTYPE** OUTPUT { struct ftdi_device_list **devlist };
33     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
34                           int vendor, int product);
35 %clear struct ftdi_device_list **devlist;
36
37 %apply char *OUTPUT { char * manufacturer, char * description, char * serial };
38 %cstring_bounded_output( char * manufacturer, 256 );
39 %cstring_bounded_output( char * description, 256 );
40 %cstring_bounded_output( char * serial, 256 );
41 %typemap(default,noblock=1) int mnf_len, int desc_len, int serial_len { $1 = 256; }
42     int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev,
43                              char * manufacturer, int mnf_len,
44                              char * description, int desc_len,
45                              char * serial, int serial_len);
46 %clear char * manufacturer, char * description, char * serial;
47 %clear int mnf_len, int desc_len, int serial_len;
48
49 %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
50 %typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
51     int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
52 %clear (unsigned char *buf, int size);
53
54 %apply int *OUTPUT { unsigned int *chunksize };
55     int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
56     int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize);
57 %clear unsigned int *chunksize;
58
59 %apply char *OUTPUT { unsigned char *pins };
60     int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
61 %clear unsigned char *pins;
62
63 %apply char *OUTPUT { unsigned char *latency };
64     int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
65 %clear unsigned char *latency;
66
67 %apply short *OUTPUT { unsigned short *status };
68     int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
69 %clear unsigned short *status;
70
71 %apply int *OUTPUT { int* value };
72     int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value);
73 %clear int* value;
74
75 %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
76 %typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
77     int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
78 %clear (unsigned char *buf, int size);
79
80 %apply short *OUTPUT { unsigned short *eeprom_val };
81     int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val);
82 %clear unsigned short *eeprom_val;
83
84 %apply int *OUTPUT { unsigned int *chipid };
85     int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid);
86 %clear unsigned int *chipid;
87
88 %include ftdi.h
89 %{
90 #include <ftdi.h>
91 %}
92
93 %include ftdi_i.h
94 %{
95 #include <ftdi_i.h>
96 %}