Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 16 | #include <Python.h> |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 17 | #include "../pneum/pneum.h" |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 18 | |
| 19 | static PyObject *pneum_callback = NULL; |
| 20 | |
| 21 | int |
| 22 | wrap_pneum_callback (char *data, int len) |
| 23 | { |
| 24 | PyGILState_STATE gstate; |
| 25 | PyObject *result;//, *arglist; |
| 26 | |
| 27 | gstate = PyGILState_Ensure(); |
| 28 | |
| 29 | /* Time to call the callback */ |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 30 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 31 | result = PyObject_CallFunction(pneum_callback, "y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 32 | #else |
| 33 | result = PyObject_CallFunction(pneum_callback, "s#", data, len); |
| 34 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 35 | if (result) |
| 36 | Py_DECREF(result); |
| 37 | else |
| 38 | PyErr_Print(); |
| 39 | |
| 40 | PyGILState_Release(gstate); |
| 41 | return (0); |
| 42 | } |
| 43 | |
| 44 | static PyObject * |
| 45 | wrap_connect (PyObject *self, PyObject *args) |
| 46 | { |
Ole Troan | b8602b5 | 2016-10-05 11:10:50 +0200 | [diff] [blame] | 47 | char * name, * chroot_prefix = NULL; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 48 | int rv; |
Ole Troan | b8602b5 | 2016-10-05 11:10:50 +0200 | [diff] [blame] | 49 | PyObject * temp; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 50 | |
Ole Troan | b8602b5 | 2016-10-05 11:10:50 +0200 | [diff] [blame] | 51 | if (!PyArg_ParseTuple(args, "sO|s:wrap_connect", &name, &temp, &chroot_prefix)) |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 52 | return (NULL); |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 53 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 54 | if (!PyCallable_Check(temp)) { |
| 55 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | Py_XINCREF(temp); /* Add a reference to new callback */ |
| 60 | Py_XDECREF(pneum_callback); /* Dispose of previous callback */ |
| 61 | pneum_callback = temp; /* Remember new callback */ |
| 62 | |
| 63 | Py_BEGIN_ALLOW_THREADS |
Ole Troan | b8602b5 | 2016-10-05 11:10:50 +0200 | [diff] [blame] | 64 | rv = pneum_connect(name, chroot_prefix); |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 65 | Py_END_ALLOW_THREADS |
| 66 | return PyLong_FromLong(rv); |
| 67 | } |
| 68 | |
| 69 | static PyObject * |
| 70 | wrap_disconnect (PyObject *self, PyObject *args) |
| 71 | { |
| 72 | int rv; |
| 73 | Py_BEGIN_ALLOW_THREADS |
| 74 | rv = pneum_disconnect(); |
| 75 | Py_END_ALLOW_THREADS |
| 76 | return PyLong_FromLong(rv); |
| 77 | } |
| 78 | static PyObject * |
| 79 | wrap_write (PyObject *self, PyObject *args) |
| 80 | { |
| 81 | char *data; |
| 82 | int len, rv; |
| 83 | |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 84 | if (!PyArg_ParseTuple(args, "s#", &data, &len)) |
| 85 | return NULL; |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 86 | Py_BEGIN_ALLOW_THREADS |
| 87 | rv = pneum_write(data, len); |
| 88 | Py_END_ALLOW_THREADS |
| 89 | |
| 90 | return PyLong_FromLong(rv); |
| 91 | } |
| 92 | |
| 93 | void vl_msg_api_free(void *); |
| 94 | |
| 95 | static PyObject * |
| 96 | wrap_read (PyObject *self, PyObject *args) |
| 97 | { |
| 98 | char *data; |
| 99 | int len, rv; |
| 100 | |
| 101 | Py_BEGIN_ALLOW_THREADS |
| 102 | rv = pneum_read(&data, &len); |
| 103 | Py_END_ALLOW_THREADS |
| 104 | |
| 105 | if (rv != 0) { Py_RETURN_NONE; } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 106 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 107 | PyObject *ret = Py_BuildValue("y#", data, len); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 108 | #else |
| 109 | PyObject *ret = Py_BuildValue("s#", data, len); |
| 110 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 111 | if (!ret) { Py_RETURN_NONE; } |
| 112 | |
| 113 | vl_msg_api_free(data); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | static PyMethodDef vpp_api_Methods[] = { |
| 118 | {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."}, |
| 119 | {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."}, |
| 120 | {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."}, |
| 121 | {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."}, |
| 122 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 123 | }; |
| 124 | |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 125 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 126 | PyMODINIT_FUNC |
| 127 | PyInit_vpp_api (void) |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 128 | #else |
| 129 | void |
| 130 | initvpp_api (void) |
| 131 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 132 | { |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 133 | #if PY_VERSION_HEX >= 0x03000000 |
| 134 | static struct PyModuleDef vpp_api_module = { |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 135 | #if PY_VERSION_HEX >= 0x03020000 |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 136 | PyModuleDef_HEAD_INIT, |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 137 | #else |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 138 | { |
| 139 | PyObject_HEAD_INIT(NULL) |
| 140 | NULL, /* m_init */ |
| 141 | 0, /* m_index */ |
| 142 | NULL, /* m_copy */ |
| 143 | }, |
Ole Troan | 5f9dcff | 2016-08-01 04:59:13 +0200 | [diff] [blame] | 144 | #endif |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 145 | (char *) "vpp_api", |
| 146 | NULL, |
| 147 | -1, |
| 148 | vpp_api_Methods, |
| 149 | NULL, |
| 150 | NULL, |
| 151 | NULL, |
| 152 | NULL |
| 153 | }; |
| 154 | #endif |
| 155 | |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 156 | /* Ensure threading is initialised */ |
| 157 | if (!PyEval_ThreadsInitialized()) { |
| 158 | PyEval_InitThreads(); |
| 159 | } |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 160 | |
| 161 | #if PY_VERSION_HEX >= 0x03000000 |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 162 | return PyModule_Create(&vpp_api_module); |
Ole Troan | d06b9f9 | 2016-04-25 13:11:19 +0200 | [diff] [blame] | 163 | #else |
| 164 | Py_InitModule((char *) "vpp_api", vpp_api_Methods); |
| 165 | return; |
| 166 | #endif |
Ole Troan | 6855f6c | 2016-04-09 03:16:30 +0200 | [diff] [blame] | 167 | } |