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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "../pneum/pneum.h"
19 static PyObject *pneum_callback = NULL;
22 wrap_pneum_callback (char *data, int len)
24 PyGILState_STATE gstate;
25 PyObject *result;//, *arglist;
27 gstate = PyGILState_Ensure();
29 /* Time to call the callback */
30 #if PY_VERSION_HEX >= 0x03000000
31 result = PyObject_CallFunction(pneum_callback, "y#", data, len);
33 result = PyObject_CallFunction(pneum_callback, "s#", data, len);
40 PyGILState_Release(gstate);
45 wrap_connect (PyObject *self, PyObject *args)
51 if (!PyArg_ParseTuple(args, "sO:set_callback", &name, &temp))
54 if (!PyCallable_Check(temp)) {
55 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
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 */
63 Py_BEGIN_ALLOW_THREADS
64 rv = pneum_connect(name);
66 return PyLong_FromLong(rv);
70 wrap_disconnect (PyObject *self, PyObject *args)
73 Py_BEGIN_ALLOW_THREADS
74 rv = pneum_disconnect();
76 return PyLong_FromLong(rv);
79 wrap_write (PyObject *self, PyObject *args)
84 if (!PyArg_ParseTuple(args, "s#", &data, &len))
86 Py_BEGIN_ALLOW_THREADS
87 rv = pneum_write(data, len);
90 return PyLong_FromLong(rv);
93 void vl_msg_api_free(void *);
96 wrap_read (PyObject *self, PyObject *args)
101 Py_BEGIN_ALLOW_THREADS
102 rv = pneum_read(&data, &len);
105 if (rv != 0) { Py_RETURN_NONE; }
106 #if PY_VERSION_HEX >= 0x03000000
107 PyObject *ret = Py_BuildValue("y#", data, len);
109 PyObject *ret = Py_BuildValue("s#", data, len);
111 if (!ret) { Py_RETURN_NONE; }
113 vl_msg_api_free(data);
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 */
125 #if PY_VERSION_HEX >= 0x03000000
127 PyInit_vpp_api (void)
133 #if PY_VERSION_HEX >= 0x03000000
134 static struct PyModuleDef vpp_api_module = {
135 #if PY_VERSION_HEX >= 0x03020000
136 PyModuleDef_HEAD_INIT,
139 PyObject_HEAD_INIT(NULL)
156 /* Ensure threading is initialised */
157 if (!PyEval_ThreadsInitialized()) {
158 PyEval_InitThreads();
161 #if PY_VERSION_HEX >= 0x03000000
162 return PyModule_Create(&vpp_api_module);
164 Py_InitModule((char *) "vpp_api", vpp_api_Methods);