Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 1 | #include <Python.h> |
| 2 | #include "pneum.h" |
| 3 | |
| 4 | static PyObject *pneum_callback = NULL; |
| 5 | |
| 6 | int |
| 7 | wrap_pneum_callback (char *data, int len) |
| 8 | { |
| 9 | PyGILState_STATE gstate; |
| 10 | PyObject *result;//, *arglist; |
| 11 | |
| 12 | gstate = PyGILState_Ensure(); |
| 13 | |
| 14 | /* Time to call the callback */ |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 15 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 16 | result = PyObject_CallFunction(pneum_callback, "y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 17 | #else |
| 18 | result = PyObject_CallFunction(pneum_callback, "s#", data, len); |
| 19 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 20 | if (result) |
| 21 | Py_DECREF(result); |
| 22 | else |
| 23 | PyErr_Print(); |
| 24 | |
| 25 | PyGILState_Release(gstate); |
| 26 | return (0); |
| 27 | } |
| 28 | |
| 29 | static PyObject * |
| 30 | wrap_connect (PyObject *self, PyObject *args) |
| 31 | { |
| 32 | char *name; |
| 33 | int rv; |
| 34 | PyObject *temp; |
| 35 | |
| 36 | if (!PyArg_ParseTuple(args, "sO:set_callback", &name, &temp)) |
| 37 | return (NULL); |
| 38 | |
| 39 | if (!PyCallable_Check(temp)) { |
| 40 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | Py_XINCREF(temp); /* Add a reference to new callback */ |
| 45 | Py_XDECREF(pneum_callback); /* Dispose of previous callback */ |
| 46 | pneum_callback = temp; /* Remember new callback */ |
| 47 | |
| 48 | Py_BEGIN_ALLOW_THREADS |
| 49 | rv = pneum_connect(name); |
| 50 | Py_END_ALLOW_THREADS |
| 51 | return PyLong_FromLong(rv); |
| 52 | } |
| 53 | |
| 54 | static PyObject * |
| 55 | wrap_disconnect (PyObject *self, PyObject *args) |
| 56 | { |
| 57 | int rv; |
| 58 | Py_BEGIN_ALLOW_THREADS |
| 59 | rv = pneum_disconnect(); |
| 60 | Py_END_ALLOW_THREADS |
| 61 | return PyLong_FromLong(rv); |
| 62 | } |
| 63 | static PyObject * |
| 64 | wrap_write (PyObject *self, PyObject *args) |
| 65 | { |
| 66 | char *data; |
| 67 | int len, rv; |
| 68 | |
| 69 | if (!PyArg_ParseTuple(args, "s#", &data, &len)) |
| 70 | return NULL; |
| 71 | Py_BEGIN_ALLOW_THREADS |
| 72 | rv = pneum_write(data, len); |
| 73 | Py_END_ALLOW_THREADS |
| 74 | |
| 75 | return PyLong_FromLong(rv); |
| 76 | } |
| 77 | |
| 78 | void vl_msg_api_free(void *); |
| 79 | |
| 80 | static PyObject * |
| 81 | wrap_read (PyObject *self, PyObject *args) |
| 82 | { |
| 83 | char *data; |
| 84 | int len, rv; |
| 85 | |
| 86 | Py_BEGIN_ALLOW_THREADS |
| 87 | rv = pneum_read(&data, &len); |
| 88 | Py_END_ALLOW_THREADS |
| 89 | |
| 90 | if (rv != 0) { Py_RETURN_NONE; } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 91 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 92 | PyObject *ret = Py_BuildValue("y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 93 | #else |
| 94 | PyObject *ret = Py_BuildValue("s#", data, len); |
| 95 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 96 | if (!ret) { Py_RETURN_NONE; } |
| 97 | |
| 98 | vl_msg_api_free(data); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static PyMethodDef vpp_api_Methods[] = { |
| 103 | {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."}, |
| 104 | {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."}, |
| 105 | {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."}, |
| 106 | {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."}, |
| 107 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 108 | }; |
| 109 | |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 110 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 111 | PyMODINIT_FUNC |
| 112 | PyInit_vpp_api (void) |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 113 | #else |
| 114 | void |
| 115 | initvpp_api (void) |
| 116 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 117 | { |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 118 | #if PY_VERSION_HEX >= 0x03000000 |
| 119 | static struct PyModuleDef vpp_api_module = { |
| 120 | # if PY_VERSION_HEX >= 0x03020000 |
| 121 | PyModuleDef_HEAD_INIT, |
| 122 | # else |
| 123 | { |
| 124 | PyObject_HEAD_INIT(NULL) |
| 125 | NULL, /* m_init */ |
| 126 | 0, /* m_index */ |
| 127 | NULL, /* m_copy */ |
| 128 | }, |
| 129 | # endif |
| 130 | (char *) "vpp_api", |
| 131 | NULL, |
| 132 | -1, |
| 133 | vpp_api_Methods, |
| 134 | NULL, |
| 135 | NULL, |
| 136 | NULL, |
| 137 | NULL |
| 138 | }; |
| 139 | #endif |
| 140 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 141 | /* Ensure threading is initialised */ |
| 142 | if (!PyEval_ThreadsInitialized()) { |
| 143 | PyEval_InitThreads(); |
| 144 | } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 145 | |
| 146 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 147 | return PyModule_Create(&vpp_api_module); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 148 | #else |
| 149 | Py_InitModule((char *) "vpp_api", vpp_api_Methods); |
| 150 | return; |
| 151 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 152 | } |