| 1 | /* File: ftdi1.i */ |
| 2 | |
| 3 | %module(docstring="Python interface to libftdi1") ftdi1 |
| 4 | %feature("autodoc","1"); |
| 5 | |
| 6 | #ifdef DOXYGEN |
| 7 | %include "ftdi1_doc.i" |
| 8 | #endif |
| 9 | |
| 10 | %{ |
| 11 | #include "Python.h" |
| 12 | |
| 13 | inline PyObject* charp2str(const char *v_, long len) |
| 14 | { |
| 15 | #if PY_MAJOR_VERSION >= 3 |
| 16 | return PyBytes_FromStringAndSize(v_, len); |
| 17 | #else |
| 18 | return PyString_FromStringAndSize(v_, len); |
| 19 | #endif |
| 20 | } |
| 21 | |
| 22 | inline char * str2charp_size(PyObject* pyObj, int * size) |
| 23 | { |
| 24 | char * v_ = 0; |
| 25 | #if PY_MAJOR_VERSION >= 3 |
| 26 | PyBytes_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size); |
| 27 | #else |
| 28 | PyString_AsStringAndSize(pyObj, &v_, (Py_ssize_t*)size); |
| 29 | #endif |
| 30 | return v_; |
| 31 | } |
| 32 | %} |
| 33 | |
| 34 | %include <typemaps.i> |
| 35 | %include <cstring.i> |
| 36 | |
| 37 | %typemap(in) unsigned char* = char*; |
| 38 | |
| 39 | %immutable ftdi_version_info::version_str; |
| 40 | %immutable ftdi_version_info::snapshot_str; |
| 41 | |
| 42 | %rename("%(strip:[ftdi_])s") ""; |
| 43 | |
| 44 | %newobject ftdi_new; |
| 45 | %typemap(newfree) struct ftdi_context *ftdi "ftdi_free($1);"; |
| 46 | %delobject ftdi_free; |
| 47 | |
| 48 | %define ftdi_usb_find_all_docstring |
| 49 | "usb_find_all(context, vendor, product) -> (return_code, devlist)" |
| 50 | %enddef |
| 51 | %feature("autodoc", ftdi_usb_find_all_docstring) ftdi_usb_find_all; |
| 52 | %typemap(in,numinputs=0) SWIGTYPE** OUTPUT ($*ltype temp) %{ $1 = &temp; %} |
| 53 | %typemap(argout) SWIGTYPE** OUTPUT %{ $result = SWIG_Python_AppendOutput($result, SWIG_NewPointerObj((void*)*$1,$*descriptor,0)); %} |
| 54 | %apply SWIGTYPE** OUTPUT { struct ftdi_device_list **devlist }; |
| 55 | int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, |
| 56 | int vendor, int product); |
| 57 | %clear struct ftdi_device_list **devlist; |
| 58 | |
| 59 | %define ftdi_usb_get_strings_docstring |
| 60 | "usb_get_strings(context, device) -> (return_code, manufacturer, description, serial)" |
| 61 | %enddef |
| 62 | %feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings; |
| 63 | %feature("autodoc", ftdi_usb_get_strings_docstring) ftdi_usb_get_strings2; |
| 64 | %apply char *OUTPUT { char * manufacturer, char * description, char * serial }; |
| 65 | %cstring_bounded_output( char * manufacturer, 256 ); |
| 66 | %cstring_bounded_output( char * description, 256 ); |
| 67 | %cstring_bounded_output( char * serial, 256 ); |
| 68 | %typemap(default,noblock=1) int mnf_len, int desc_len, int serial_len { $1 = 256; } |
| 69 | int ftdi_usb_get_strings(struct ftdi_context *ftdi, struct libusb_device *dev, |
| 70 | char * manufacturer, int mnf_len, |
| 71 | char * description, int desc_len, |
| 72 | char * serial, int serial_len); |
| 73 | int ftdi_usb_get_strings2(struct ftdi_context *ftdi, struct libusb_device *dev, |
| 74 | char * manufacturer, int mnf_len, |
| 75 | char * description, int desc_len, |
| 76 | char * serial, int serial_len); |
| 77 | %clear char * manufacturer, char * description, char * serial; |
| 78 | %clear int mnf_len, int desc_len, int serial_len; |
| 79 | |
| 80 | %define ftdi_read_data_docstring |
| 81 | "read_data(context) -> (return_code, buf)" |
| 82 | %enddef |
| 83 | %feature("autodoc", ftdi_read_data_docstring) ftdi_read_data; |
| 84 | %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} |
| 85 | %typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, $2)); free($1); %} |
| 86 | int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size); |
| 87 | %clear (unsigned char *buf, int size); |
| 88 | |
| 89 | %define ftdi_write_data_docstring |
| 90 | "write_data(context, data) -> return_code" |
| 91 | %enddef |
| 92 | %feature("autodoc", ftdi_write_data_docstring) ftdi_write_data; |
| 93 | %typemap(in,numinputs=1) (const unsigned char *buf, int size) %{ $1 = (unsigned char*)str2charp_size($input, &$2); %} |
| 94 | int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size); |
| 95 | %clear (const unsigned char *buf, int size); |
| 96 | |
| 97 | %apply int *OUTPUT { unsigned int *chunksize }; |
| 98 | int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); |
| 99 | int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize); |
| 100 | %clear unsigned int *chunksize; |
| 101 | |
| 102 | %define ftdi_read_pins_docstring |
| 103 | "read_pins(context) -> (return_code, pins)" |
| 104 | %enddef |
| 105 | %feature("autodoc", ftdi_read_pins_docstring) ftdi_read_pins; |
| 106 | %typemap(in,numinputs=0) unsigned char *pins ($*ltype temp) %{ $1 = &temp; %} |
| 107 | %typemap(argout) (unsigned char *pins) %{ $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, 1)); %} |
| 108 | int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins); |
| 109 | %clear unsigned char *pins; |
| 110 | |
| 111 | %typemap(in,numinputs=0) unsigned char *latency ($*ltype temp) %{ $1 = &temp; %} |
| 112 | %typemap(argout) (unsigned char *latency) %{ $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, 1)); %} |
| 113 | int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency); |
| 114 | %clear unsigned char *latency; |
| 115 | |
| 116 | %apply short *OUTPUT { unsigned short *status }; |
| 117 | int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status); |
| 118 | %clear unsigned short *status; |
| 119 | |
| 120 | %apply int *OUTPUT { int* value }; |
| 121 | int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value); |
| 122 | %clear int* value; |
| 123 | |
| 124 | %typemap(in,numinputs=1) (unsigned char *buf, int size) %{ $2 = PyInt_AsLong($input);$1 = (unsigned char*)malloc($2*sizeof(char)); %} |
| 125 | %typemap(argout) (unsigned char *buf, int size) %{ if(result<0) $2=0; $result = SWIG_Python_AppendOutput($result, charp2str((char*)$1, $2)); free($1); %} |
| 126 | int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size); |
| 127 | %clear (unsigned char *buf, int size); |
| 128 | |
| 129 | %define ftdi_read_eeprom_location_docstring |
| 130 | "read_eeprom_location(context, eeprom_addr) -> (return_code, eeprom_val)" |
| 131 | %enddef |
| 132 | %feature("autodoc", ftdi_read_eeprom_location_docstring) ftdi_read_eeprom_location; |
| 133 | %apply short *OUTPUT { unsigned short *eeprom_val }; |
| 134 | int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val); |
| 135 | %clear unsigned short *eeprom_val; |
| 136 | |
| 137 | %define ftdi_read_eeprom_docstring |
| 138 | "read_eeprom(context) -> (return_code, eeprom)" |
| 139 | %enddef |
| 140 | %feature("autodoc", ftdi_read_eeprom_docstring) ftdi_read_eeprom; |
| 141 | |
| 142 | %define ftdi_read_chipid_docstring |
| 143 | "ftdi_read_chipid(context) -> (return_code, chipid)" |
| 144 | %enddef |
| 145 | %feature("autodoc", ftdi_read_chipid_docstring) ftdi_read_chipid; |
| 146 | %apply int *OUTPUT { unsigned int *chipid }; |
| 147 | int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid); |
| 148 | %clear unsigned int *chipid; |
| 149 | |
| 150 | %include ftdi.h |
| 151 | %{ |
| 152 | #include <ftdi.h> |
| 153 | %} |
| 154 | |
| 155 | %include ftdi_i.h |
| 156 | %{ |
| 157 | #include <ftdi_i.h> |
| 158 | %} |
| 159 | |
| 160 | %pythoncode %{ |
| 161 | __version__ = get_library_version().version_str |
| 162 | %} |