| 1 |
%module capi |
| 2 |
#define __attribute__(__x__) |
| 3 |
%{ |
| 4 |
#include "@top_srcdir@/include/team.h" |
| 5 |
|
| 6 |
static int PythonCallBack(struct team_handle *th, void *func_priv, |
| 7 |
team_change_type_mask_t type_mask); |
| 8 |
void py_team_change_handler_register(struct team_handle *th, |
| 9 |
struct team_change_handler *handler, |
| 10 |
PyObject *priv); |
| 11 |
void py_team_change_handler_unregister(struct team_handle *th, |
| 12 |
struct team_change_handler *handler, |
| 13 |
PyObject *priv); |
| 14 |
%} |
| 15 |
|
| 16 |
%include <cstring.i> |
| 17 |
%include <stdint.i> |
| 18 |
%include "typemaps.i" |
| 19 |
|
| 20 |
%apply int *OUTPUT {uint32_t *ifindex}; |
| 21 |
%cstring_output_allocate(char **mode_name, ) |
| 22 |
|
| 23 |
%typemap(in) (char *ifname, unsigned int maxlen) { |
| 24 |
$2 = PyInt_AsLong($input); |
| 25 |
$1 = (char *) malloc(sizeof(char) * $2); |
| 26 |
} |
| 27 |
|
| 28 |
%typemap(freearg) (char *ifname, unsigned int maxlen) { |
| 29 |
if ($1) free($1); |
| 30 |
} |
| 31 |
|
| 32 |
%apply (char *STRING, int LENGTH) { (const char *addr, unsigned int addr_len) }; |
| 33 |
int team_hwaddr_set(struct team_handle *th, uint32_t ifindex, const char *addr, unsigned int addr_len); |
| 34 |
|
| 35 |
%typemap(in) (char *addr, unsigned int addr_len) { |
| 36 |
$2 = PyInt_AsLong($input); |
| 37 |
$1 = (char *) malloc(sizeof(char) * $2); |
| 38 |
} |
| 39 |
%typemap(freearg) (char *addr, unsigned int addr_len) { |
| 40 |
if ($1) free($1); |
| 41 |
} |
| 42 |
%typemap(argout) (char *addr, unsigned int addr_len) { |
| 43 |
$result = SWIG_Python_AppendOutput($result, SWIG_FromCharPtrAndSize($1,$2)); |
| 44 |
} |
| 45 |
%apply char *OUTPUT {char *addr}; |
| 46 |
int team_hwaddr_get(struct team_handle *th, uint32_t ifindex, char *addr, unsigned int addr_len); |
| 47 |
|
| 48 |
%typemap(python, in) PyObject *PyFunc { |
| 49 |
if (!PyCallable_Check($input)) { |
| 50 |
PyErr_SetString(PyExc_TypeError, "Need a callable object!"); |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
$1 = $input; |
| 54 |
} |
| 55 |
|
| 56 |
%typemap(in) void (*func)(struct team_handle *, void *, |
| 57 |
team_change_type_mask_t) { |
| 58 |
$1 = (void *) $input; |
| 59 |
}; |
| 60 |
|
| 61 |
%{ |
| 62 |
struct _py_team_change_handler { |
| 63 |
struct team_change_handler handler; |
| 64 |
PyObject *orig_func; |
| 65 |
PyObject *orig_priv; |
| 66 |
}; |
| 67 |
%} |
| 68 |
|
| 69 |
%extend team_change_handler { |
| 70 |
team_change_handler(PyObject *func, |
| 71 |
team_change_type_mask_t type_mask) { |
| 72 |
struct _py_team_change_handler *py_handler; |
| 73 |
|
| 74 |
py_handler = malloc(sizeof(*py_handler)); |
| 75 |
if (!py_handler) |
| 76 |
return NULL; |
| 77 |
py_handler->handler.func = (void *) func; |
| 78 |
py_handler->handler.type_mask = type_mask; |
| 79 |
|
| 80 |
Py_XINCREF(py_handler->handler.func); |
| 81 |
return &py_handler->handler; |
| 82 |
} |
| 83 |
|
| 84 |
~team_change_handler() { |
| 85 |
Py_XDECREF($self->func); |
| 86 |
free($self); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void py_team_change_handler_register(struct team_handle *th, |
| 91 |
struct team_change_handler *handler, |
| 92 |
PyObject *priv); |
| 93 |
|
| 94 |
void py_team_change_handler_unregister(struct team_handle *th, |
| 95 |
struct team_change_handler *handler, |
| 96 |
PyObject *priv); |
| 97 |
|
| 98 |
%{ |
| 99 |
static int PythonCallBack(struct team_handle *th, void *func_priv, |
| 100 |
team_change_type_mask_t type_mask) |
| 101 |
{ |
| 102 |
PyObject *arglist; |
| 103 |
PyObject *result; |
| 104 |
struct _py_team_change_handler *py_handler; |
| 105 |
int retval = 0; |
| 106 |
|
| 107 |
py_handler = (struct _py_team_change_handler *) func_priv; |
| 108 |
arglist = Py_BuildValue("(Oi)", py_handler->orig_priv, type_mask); |
| 109 |
|
| 110 |
result = PyObject_CallObject(py_handler->orig_func, arglist); |
| 111 |
|
| 112 |
if (result == NULL) |
| 113 |
return -1; |
| 114 |
if (PyInt_Check(result)) |
| 115 |
retval = PyInt_AsLong(result); |
| 116 |
if (arglist != NULL) |
| 117 |
Py_DECREF(arglist); |
| 118 |
Py_XDECREF(result); |
| 119 |
|
| 120 |
return retval; |
| 121 |
} |
| 122 |
|
| 123 |
void py_team_change_handler_register(struct team_handle *th, |
| 124 |
struct team_change_handler *handler, |
| 125 |
PyObject *priv) |
| 126 |
{ |
| 127 |
struct _py_team_change_handler *py_handler; |
| 128 |
|
| 129 |
py_handler = (struct _py_team_change_handler *) handler; |
| 130 |
py_handler->orig_func = (PyObject *) handler->func; |
| 131 |
py_handler->orig_priv = priv; |
| 132 |
handler->func = PythonCallBack; |
| 133 |
|
| 134 |
Py_XINCREF(py_handler->orig_func); |
| 135 |
Py_XINCREF(py_handler->orig_priv); |
| 136 |
team_change_handler_register(th, handler, handler); |
| 137 |
} |
| 138 |
|
| 139 |
void py_team_change_handler_unregister(struct team_handle *th, |
| 140 |
struct team_change_handler *handler, |
| 141 |
PyObject *priv) |
| 142 |
{ |
| 143 |
struct _py_team_change_handler *py_handler; |
| 144 |
|
| 145 |
py_handler = (struct _py_team_change_handler *) handler; |
| 146 |
Py_XDECREF(py_handler->orig_priv); |
| 147 |
Py_XDECREF(py_handler->orig_func); |
| 148 |
team_change_handler_unregister(th, handler, handler); |
| 149 |
} |
| 150 |
%} |
| 151 |
|
| 152 |
%include @top_srcdir@/include/team.h |