Fixed convert string function in python wrapper.
[libftdi] / bindings / ftdi1.i
... / ...
CommitLineData
1/* File: ftdi1.i */
2
3%module(docstring="Python interface to libftdi1") ftdi1
4%feature("autodoc","1");
5#ifdef DOXYGEN
6%include "ftdi1_doc.i"
7#endif
8
9%{
10#include "Python.h"
11
12PyObject* convertString( const char *v, Py_ssize_t len )
13{
14#if PY_MAJOR_VERSION >= 3
15 return PyBytes_FromStringAndSize(v, len);
16#else
17 return PyString_FromStringAndSize(v, len);
18#endif
19}
20%}
21
22%include <typemaps.i>
23%include <cstring.i>
24
25%typemap(in) unsigned char* = char*;
26
27%immutable ftdi_version_info::version_str;
28%immutable ftdi_version_info::snapshot_str;
29
30%rename("%(strip:[ftdi_])s") "";
31
32%newobject ftdi_new;
33%typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);";
34%delobject ftdi_free;
35
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;
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
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;
51%apply char *OUTPUT { char * manufacturer, char * description, char * serial };
52%cstring_bounded_output( char * manufacturer, 256 );
53%cstring_bounded_output( char * description, 256 );
54%cstring_bounded_output( char * serial, 256 );
55%typemap(default,noblock=1) int mnf_len, int desc_len, int serial_len { $1 = 256; }
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);
60%clear char * manufacturer, char * description, char * serial;
61%clear int mnf_len, int desc_len, int serial_len;
62
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;
67%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
68%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
69 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
70%clear (unsigned char *buf, int size);
71
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;
76
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;
81%apply char *OUTPUT { unsigned char *pins };
82 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins);
83%clear unsigned char *pins;
84
85%apply char *OUTPUT { unsigned char *latency };
86 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency);
87%clear unsigned char *latency;
88
89%apply short *OUTPUT { unsigned short *status };
90 int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status);
91%clear unsigned short *status;
92
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;
96
97%typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %}
98%typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, convertString((char*)$1, $2)); free($1); %}
99 int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size);
100%clear (unsigned char *buf, int size);
101
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;
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;
109
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;
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
123%include ftdi.h
124%{
125#include <ftdi.h>
126%}
127
128%include ftdi_i.h
129%{
130#include <ftdi_i.h>
131%}