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"
18 #include <vppinfra/hash.h>
20 static PyObject *pneum_callback = NULL;
23 wrap_pneum_callback (unsigned char * data, int len)
25 PyGILState_STATE gstate;
26 PyObject *result;//, *arglist;
28 gstate = PyGILState_Ensure();
30 /* Time to call the callback */
31 #if PY_VERSION_HEX >= 0x03000000
32 result = PyObject_CallFunction(pneum_callback, "y#", data, len);
34 result = PyObject_CallFunction(pneum_callback, "s#", data, len);
41 PyGILState_Release(gstate);
45 wrap_connect (PyObject *self, PyObject *args)
47 char * name, * chroot_prefix = NULL;
48 int rx_qlen=32; /* default rx queue length */
50 PyObject * temp = NULL;
51 pneum_callback_t cb = NULL;
53 if (!PyArg_ParseTuple(args, "s|Ois:wrap_connect",
54 &name, &temp, &rx_qlen, &chroot_prefix))
59 if (!PyCallable_Check(temp))
61 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
65 Py_XINCREF(temp); /* Add a reference to new callback */
66 Py_XDECREF(pneum_callback); /* Dispose of previous callback */
67 pneum_callback = temp; /* Remember new callback */
68 cb = wrap_pneum_callback;
70 Py_BEGIN_ALLOW_THREADS
71 rv = pneum_connect(name, chroot_prefix, cb, rx_qlen);
73 return PyLong_FromLong(rv);
77 wrap_disconnect (PyObject *self, PyObject *args)
80 Py_BEGIN_ALLOW_THREADS
81 rv = pneum_disconnect();
83 return PyLong_FromLong(rv);
86 wrap_write (PyObject *self, PyObject *args)
91 if (!PyArg_ParseTuple(args, "s#", &data, &len))
93 Py_BEGIN_ALLOW_THREADS
94 rv = pneum_write(data, len);
97 return PyLong_FromLong(rv);
101 wrap_read (PyObject *self, PyObject *args)
106 Py_BEGIN_ALLOW_THREADS
107 rv = pneum_read(&data, &len);
110 if (rv != 0) { Py_RETURN_NONE; }
111 #if PY_VERSION_HEX >= 0x03000000
112 PyObject *ret = Py_BuildValue("y#", data, len);
114 PyObject *ret = Py_BuildValue("s#", data, len);
116 if (!ret) { Py_RETURN_NONE; }
123 wrap_msg_table (PyObject *self, PyObject *args)
127 uword *h = pneum_msg_table_get_hash();
128 PyObject *ret = PyList_New(pneum_msg_table_size());
129 if (!ret) goto error;
130 hash_foreach_pair (hp, h,
132 PyObject *item = PyTuple_New(2);
133 if (!item) goto error;
134 rv = PyTuple_SetItem(item, 0, PyLong_FromLong((u32)hp->value[0]));
136 rv = PyTuple_SetItem(item, 1, PyString_FromString((char *)hp->key));
138 PyList_SetItem(ret, i, item);
145 /* TODO: Raise exception */
146 printf("msg_table failed");
150 static PyMethodDef vpp_api_Methods[] = {
151 {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."},
152 {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."},
153 {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."},
154 {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."},
155 {"msg_table", wrap_msg_table, METH_VARARGS, "Get API dictionary."},
156 {NULL, NULL, 0, NULL} /* Sentinel */
159 #if PY_VERSION_HEX >= 0x03000000
161 PyInit_vpp_api (void)
167 #if PY_VERSION_HEX >= 0x03000000
168 static struct PyModuleDef vpp_api_module = {
169 #if PY_VERSION_HEX >= 0x03020000
170 PyModuleDef_HEAD_INIT,
173 PyObject_HEAD_INIT(NULL)
190 /* Ensure threading is initialised */
191 if (!PyEval_ThreadsInitialized()) {
192 PyEval_InitThreads();
195 #if PY_VERSION_HEX >= 0x03000000
196 return PyModule_Create(&vpp_api_module);
198 Py_InitModule((char *) "vpp_api", vpp_api_Methods);