blob: 5763707b517fdb649bb045bee4e698c56431c771 [file] [log] [blame]
Ole Troan5f9dcff2016-08-01 04:59:13 +02001/*
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 Troan6855f6c2016-04-09 03:16:30 +020016#include <Python.h>
Ole Troan5f9dcff2016-08-01 04:59:13 +020017#include "../pneum/pneum.h"
Dave Barach557d1282016-11-10 14:22:49 -050018#include <vppinfra/hash.h>
Ole Troan6855f6c2016-04-09 03:16:30 +020019
20static PyObject *pneum_callback = NULL;
21
Dave Barach557d1282016-11-10 14:22:49 -050022static void
23wrap_pneum_callback (unsigned char * data, int len)
Ole Troan6855f6c2016-04-09 03:16:30 +020024{
25 PyGILState_STATE gstate;
26 PyObject *result;//, *arglist;
27
28 gstate = PyGILState_Ensure();
29
30 /* Time to call the callback */
Ole Troand06b9f92016-04-25 13:11:19 +020031#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +020032 result = PyObject_CallFunction(pneum_callback, "y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +020033#else
34 result = PyObject_CallFunction(pneum_callback, "s#", data, len);
35#endif
Ole Troan6855f6c2016-04-09 03:16:30 +020036 if (result)
37 Py_DECREF(result);
38 else
39 PyErr_Print();
40
41 PyGILState_Release(gstate);
Ole Troan6855f6c2016-04-09 03:16:30 +020042}
43
44static PyObject *
45wrap_connect (PyObject *self, PyObject *args)
46{
Ole Troanb8602b52016-10-05 11:10:50 +020047 char * name, * chroot_prefix = NULL;
Ole Troan6855f6c2016-04-09 03:16:30 +020048 int rv;
Dave Barach557d1282016-11-10 14:22:49 -050049 PyObject * temp = NULL;
50 pneum_callback_t cb = NULL;
Ole Troan6855f6c2016-04-09 03:16:30 +020051
Dave Barach557d1282016-11-10 14:22:49 -050052 if (!PyArg_ParseTuple(args, "s|Os:wrap_connect",
53 &name, &temp, &chroot_prefix))
Ole Troan6855f6c2016-04-09 03:16:30 +020054 return (NULL);
Ole Troan5f9dcff2016-08-01 04:59:13 +020055
Dave Barach557d1282016-11-10 14:22:49 -050056 if (temp)
57 {
58 if (!PyCallable_Check(temp))
59 {
60 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
61 return NULL;
62 }
Ole Troan6855f6c2016-04-09 03:16:30 +020063
Dave Barach557d1282016-11-10 14:22:49 -050064 Py_XINCREF(temp); /* Add a reference to new callback */
65 Py_XDECREF(pneum_callback); /* Dispose of previous callback */
66 pneum_callback = temp; /* Remember new callback */
67 cb = wrap_pneum_callback;
68 }
Ole Troan6855f6c2016-04-09 03:16:30 +020069 Py_BEGIN_ALLOW_THREADS
Dave Barach557d1282016-11-10 14:22:49 -050070 rv = pneum_connect(name, chroot_prefix, cb);
Ole Troan6855f6c2016-04-09 03:16:30 +020071 Py_END_ALLOW_THREADS
72 return PyLong_FromLong(rv);
73}
74
75static PyObject *
76wrap_disconnect (PyObject *self, PyObject *args)
77{
78 int rv;
79 Py_BEGIN_ALLOW_THREADS
80 rv = pneum_disconnect();
81 Py_END_ALLOW_THREADS
82 return PyLong_FromLong(rv);
83}
84static PyObject *
85wrap_write (PyObject *self, PyObject *args)
86{
87 char *data;
88 int len, rv;
89
Ole Troan5f9dcff2016-08-01 04:59:13 +020090 if (!PyArg_ParseTuple(args, "s#", &data, &len))
91 return NULL;
Ole Troan6855f6c2016-04-09 03:16:30 +020092 Py_BEGIN_ALLOW_THREADS
93 rv = pneum_write(data, len);
94 Py_END_ALLOW_THREADS
95
96 return PyLong_FromLong(rv);
97}
98
Ole Troan6855f6c2016-04-09 03:16:30 +020099static PyObject *
100wrap_read (PyObject *self, PyObject *args)
101{
102 char *data;
103 int len, rv;
104
105 Py_BEGIN_ALLOW_THREADS
106 rv = pneum_read(&data, &len);
107 Py_END_ALLOW_THREADS
108
109 if (rv != 0) { Py_RETURN_NONE; }
Ole Troand06b9f92016-04-25 13:11:19 +0200110#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200111 PyObject *ret = Py_BuildValue("y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +0200112#else
113 PyObject *ret = Py_BuildValue("s#", data, len);
114#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200115 if (!ret) { Py_RETURN_NONE; }
116
Dave Barach557d1282016-11-10 14:22:49 -0500117 pneum_free(data);
Ole Troan6855f6c2016-04-09 03:16:30 +0200118 return ret;
119}
120
Dave Barach557d1282016-11-10 14:22:49 -0500121static PyObject *
122wrap_msg_table (PyObject *self, PyObject *args)
123{
124 int i = 0, rv = 0;
125 hash_pair_t *hp;
126 uword *h = pneum_msg_table_get_hash();
127 PyObject *ret = PyList_New(pneum_msg_table_size());
128 if (!ret) goto error;
129 hash_foreach_pair (hp, h,
130 ({
131 PyObject *item = PyTuple_New(2);
132 if (!item) goto error;
133 rv = PyTuple_SetItem(item, 0, PyLong_FromLong((u32)hp->value[0]));
134 if (rv) goto error;
135 rv = PyTuple_SetItem(item, 1, PyString_FromString((char *)hp->key));
136 if (rv) goto error;
137 PyList_SetItem(ret, i, item);
138 i++;
139 }));
140
141 return ret;
142
143 error:
144 /* TODO: Raise exception */
145 printf("msg_table failed");
146 Py_RETURN_NONE;
147}
148
Ole Troan6855f6c2016-04-09 03:16:30 +0200149static PyMethodDef vpp_api_Methods[] = {
150 {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."},
151 {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."},
152 {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."},
153 {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."},
Dave Barach557d1282016-11-10 14:22:49 -0500154 {"msg_table", wrap_msg_table, METH_VARARGS, "Get API dictionary."},
Ole Troan6855f6c2016-04-09 03:16:30 +0200155 {NULL, NULL, 0, NULL} /* Sentinel */
156};
157
Ole Troand06b9f92016-04-25 13:11:19 +0200158#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200159PyMODINIT_FUNC
160PyInit_vpp_api (void)
Ole Troand06b9f92016-04-25 13:11:19 +0200161#else
162void
163initvpp_api (void)
164#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200165{
Ole Troand06b9f92016-04-25 13:11:19 +0200166#if PY_VERSION_HEX >= 0x03000000
167 static struct PyModuleDef vpp_api_module = {
Ole Troan5f9dcff2016-08-01 04:59:13 +0200168#if PY_VERSION_HEX >= 0x03020000
Ole Troand06b9f92016-04-25 13:11:19 +0200169 PyModuleDef_HEAD_INIT,
Ole Troan5f9dcff2016-08-01 04:59:13 +0200170#else
Ole Troand06b9f92016-04-25 13:11:19 +0200171 {
172 PyObject_HEAD_INIT(NULL)
173 NULL, /* m_init */
174 0, /* m_index */
175 NULL, /* m_copy */
176 },
Ole Troan5f9dcff2016-08-01 04:59:13 +0200177#endif
Ole Troand06b9f92016-04-25 13:11:19 +0200178 (char *) "vpp_api",
179 NULL,
180 -1,
181 vpp_api_Methods,
182 NULL,
183 NULL,
184 NULL,
185 NULL
186 };
187#endif
188
Ole Troan6855f6c2016-04-09 03:16:30 +0200189 /* Ensure threading is initialised */
190 if (!PyEval_ThreadsInitialized()) {
191 PyEval_InitThreads();
192 }
Ole Troand06b9f92016-04-25 13:11:19 +0200193
194#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200195 return PyModule_Create(&vpp_api_module);
Ole Troand06b9f92016-04-25 13:11:19 +0200196#else
197 Py_InitModule((char *) "vpp_api", vpp_api_Methods);
198 return;
199#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200200}