blob: 18c4f2338697cdec1442f2803d2444e0f0f39a51 [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"
Ole Troan6855f6c2016-04-09 03:16:30 +020018
19static PyObject *pneum_callback = NULL;
20
21int
22wrap_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 Troand06b9f92016-04-25 13:11:19 +020030#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +020031 result = PyObject_CallFunction(pneum_callback, "y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +020032#else
33 result = PyObject_CallFunction(pneum_callback, "s#", data, len);
34#endif
Ole Troan6855f6c2016-04-09 03:16:30 +020035 if (result)
36 Py_DECREF(result);
37 else
38 PyErr_Print();
39
40 PyGILState_Release(gstate);
41 return (0);
42}
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;
Ole Troanb8602b52016-10-05 11:10:50 +020049 PyObject * temp;
Ole Troan6855f6c2016-04-09 03:16:30 +020050
Ole Troanb8602b52016-10-05 11:10:50 +020051 if (!PyArg_ParseTuple(args, "sO|s:wrap_connect", &name, &temp, &chroot_prefix))
Ole Troan6855f6c2016-04-09 03:16:30 +020052 return (NULL);
Ole Troan5f9dcff2016-08-01 04:59:13 +020053
Ole Troan6855f6c2016-04-09 03:16:30 +020054 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 Troanb8602b52016-10-05 11:10:50 +020064 rv = pneum_connect(name, chroot_prefix);
Ole Troan6855f6c2016-04-09 03:16:30 +020065 Py_END_ALLOW_THREADS
66 return PyLong_FromLong(rv);
67}
68
69static PyObject *
70wrap_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}
78static PyObject *
79wrap_write (PyObject *self, PyObject *args)
80{
81 char *data;
82 int len, rv;
83
Ole Troan5f9dcff2016-08-01 04:59:13 +020084 if (!PyArg_ParseTuple(args, "s#", &data, &len))
85 return NULL;
Ole Troan6855f6c2016-04-09 03:16:30 +020086 Py_BEGIN_ALLOW_THREADS
87 rv = pneum_write(data, len);
88 Py_END_ALLOW_THREADS
89
90 return PyLong_FromLong(rv);
91}
92
93void vl_msg_api_free(void *);
94
95static PyObject *
96wrap_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 Troand06b9f92016-04-25 13:11:19 +0200106#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200107 PyObject *ret = Py_BuildValue("y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +0200108#else
109 PyObject *ret = Py_BuildValue("s#", data, len);
110#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200111 if (!ret) { Py_RETURN_NONE; }
112
113 vl_msg_api_free(data);
114 return ret;
115}
116
117static 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 Troand06b9f92016-04-25 13:11:19 +0200125#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200126PyMODINIT_FUNC
127PyInit_vpp_api (void)
Ole Troand06b9f92016-04-25 13:11:19 +0200128#else
129void
130initvpp_api (void)
131#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200132{
Ole Troand06b9f92016-04-25 13:11:19 +0200133#if PY_VERSION_HEX >= 0x03000000
134 static struct PyModuleDef vpp_api_module = {
Ole Troan5f9dcff2016-08-01 04:59:13 +0200135#if PY_VERSION_HEX >= 0x03020000
Ole Troand06b9f92016-04-25 13:11:19 +0200136 PyModuleDef_HEAD_INIT,
Ole Troan5f9dcff2016-08-01 04:59:13 +0200137#else
Ole Troand06b9f92016-04-25 13:11:19 +0200138 {
139 PyObject_HEAD_INIT(NULL)
140 NULL, /* m_init */
141 0, /* m_index */
142 NULL, /* m_copy */
143 },
Ole Troan5f9dcff2016-08-01 04:59:13 +0200144#endif
Ole Troand06b9f92016-04-25 13:11:19 +0200145 (char *) "vpp_api",
146 NULL,
147 -1,
148 vpp_api_Methods,
149 NULL,
150 NULL,
151 NULL,
152 NULL
153 };
154#endif
155
Ole Troan6855f6c2016-04-09 03:16:30 +0200156 /* Ensure threading is initialised */
157 if (!PyEval_ThreadsInitialized()) {
158 PyEval_InitThreads();
159 }
Ole Troand06b9f92016-04-25 13:11:19 +0200160
161#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200162 return PyModule_Create(&vpp_api_module);
Ole Troand06b9f92016-04-25 13:11:19 +0200163#else
164 Py_InitModule((char *) "vpp_api", vpp_api_Methods);
165 return;
166#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200167}