blob: d1795aa13a7e1bb551b6394e838cc7b08be45692 [file] [log] [blame]
Ole Troan6855f6c2016-04-09 03:16:30 +02001#include <Python.h>
2#include "pneum.h"
3
4static PyObject *pneum_callback = NULL;
5
6int
7wrap_pneum_callback (char *data, int len)
8{
9 PyGILState_STATE gstate;
10 PyObject *result;//, *arglist;
11
12 gstate = PyGILState_Ensure();
13
14 /* Time to call the callback */
Ole Troand06b9f92016-04-25 13:11:19 +020015#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +020016 result = PyObject_CallFunction(pneum_callback, "y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +020017#else
18 result = PyObject_CallFunction(pneum_callback, "s#", data, len);
19#endif
Ole Troan6855f6c2016-04-09 03:16:30 +020020 if (result)
21 Py_DECREF(result);
22 else
23 PyErr_Print();
24
25 PyGILState_Release(gstate);
26 return (0);
27}
28
29static PyObject *
30wrap_connect (PyObject *self, PyObject *args)
31{
32 char *name;
33 int rv;
34 PyObject *temp;
35
36 if (!PyArg_ParseTuple(args, "sO:set_callback", &name, &temp))
37 return (NULL);
38
39 if (!PyCallable_Check(temp)) {
40 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
41 return NULL;
42 }
43
44 Py_XINCREF(temp); /* Add a reference to new callback */
45 Py_XDECREF(pneum_callback); /* Dispose of previous callback */
46 pneum_callback = temp; /* Remember new callback */
47
48 Py_BEGIN_ALLOW_THREADS
49 rv = pneum_connect(name);
50 Py_END_ALLOW_THREADS
51 return PyLong_FromLong(rv);
52}
53
54static PyObject *
55wrap_disconnect (PyObject *self, PyObject *args)
56{
57 int rv;
58 Py_BEGIN_ALLOW_THREADS
59 rv = pneum_disconnect();
60 Py_END_ALLOW_THREADS
61 return PyLong_FromLong(rv);
62}
63static PyObject *
64wrap_write (PyObject *self, PyObject *args)
65{
66 char *data;
67 int len, rv;
68
69 if (!PyArg_ParseTuple(args, "s#", &data, &len))
70 return NULL;
71 Py_BEGIN_ALLOW_THREADS
72 rv = pneum_write(data, len);
73 Py_END_ALLOW_THREADS
74
75 return PyLong_FromLong(rv);
76}
77
78void vl_msg_api_free(void *);
79
80static PyObject *
81wrap_read (PyObject *self, PyObject *args)
82{
83 char *data;
84 int len, rv;
85
86 Py_BEGIN_ALLOW_THREADS
87 rv = pneum_read(&data, &len);
88 Py_END_ALLOW_THREADS
89
90 if (rv != 0) { Py_RETURN_NONE; }
Ole Troand06b9f92016-04-25 13:11:19 +020091#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +020092 PyObject *ret = Py_BuildValue("y#", data, len);
Ole Troand06b9f92016-04-25 13:11:19 +020093#else
94 PyObject *ret = Py_BuildValue("s#", data, len);
95#endif
Ole Troan6855f6c2016-04-09 03:16:30 +020096 if (!ret) { Py_RETURN_NONE; }
97
98 vl_msg_api_free(data);
99 return ret;
100}
101
102static PyMethodDef vpp_api_Methods[] = {
103 {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."},
104 {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."},
105 {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."},
106 {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."},
107 {NULL, NULL, 0, NULL} /* Sentinel */
108};
109
Ole Troand06b9f92016-04-25 13:11:19 +0200110#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200111PyMODINIT_FUNC
112PyInit_vpp_api (void)
Ole Troand06b9f92016-04-25 13:11:19 +0200113#else
114void
115initvpp_api (void)
116#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200117{
Ole Troand06b9f92016-04-25 13:11:19 +0200118#if PY_VERSION_HEX >= 0x03000000
119 static struct PyModuleDef vpp_api_module = {
120# if PY_VERSION_HEX >= 0x03020000
121 PyModuleDef_HEAD_INIT,
122# else
123 {
124 PyObject_HEAD_INIT(NULL)
125 NULL, /* m_init */
126 0, /* m_index */
127 NULL, /* m_copy */
128 },
129# endif
130 (char *) "vpp_api",
131 NULL,
132 -1,
133 vpp_api_Methods,
134 NULL,
135 NULL,
136 NULL,
137 NULL
138 };
139#endif
140
Ole Troan6855f6c2016-04-09 03:16:30 +0200141 /* Ensure threading is initialised */
142 if (!PyEval_ThreadsInitialized()) {
143 PyEval_InitThreads();
144 }
Ole Troand06b9f92016-04-25 13:11:19 +0200145
146#if PY_VERSION_HEX >= 0x03000000
Ole Troan6855f6c2016-04-09 03:16:30 +0200147 return PyModule_Create(&vpp_api_module);
Ole Troand06b9f92016-04-25 13:11:19 +0200148#else
149 Py_InitModule((char *) "vpp_api", vpp_api_Methods);
150 return;
151#endif
Ole Troan6855f6c2016-04-09 03:16:30 +0200152}