You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpspacket.c 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Python binding for the packet.c module.
  3. *
  4. * This file is Copyright (c) 2010-2018 by the GPSD project
  5. * SPDX-License-Identifier: BSD-2-clause
  6. *
  7. */
  8. #include "gpsd_config.h" /* must be before all includes */
  9. /* Python.h insists on setting GNU_SOURCE, _POSIX_C_SOURCE and
  10. * _XOPEN_SOURCE. Without guards. */
  11. #undef _GNU_SOURCE
  12. #undef _POSIX_C_SOURCE
  13. #undef _XOPEN_SOURCE
  14. #include <Python.h>
  15. #include <stdio.h>
  16. #include "gpsd.h"
  17. #include "python_compatibility.h"
  18. static PyObject *ErrorObject = NULL;
  19. static PyObject *report_callback = NULL;
  20. static void basic_report(const char *buf)
  21. {
  22. (void)fputs(buf, stderr);
  23. }
  24. void errout_reset(struct gpsd_errout_t *errout)
  25. {
  26. errout->debug = LOG_SHOUT;
  27. errout->report = basic_report;
  28. }
  29. void gpsd_log(int errlevel,
  30. const struct gpsd_errout_t *errout UNUSED,
  31. const char *fmt, ... )
  32. {
  33. char buf[BUFSIZ];
  34. PyObject *args;
  35. va_list ap;
  36. if (!report_callback) /* no callback defined, exit early */
  37. return;
  38. if (!PyCallable_Check(report_callback)) {
  39. PyErr_SetString(ErrorObject, "Cannot call Python callback function");
  40. return;
  41. }
  42. va_start(ap, fmt);
  43. (void)vsnprintf(buf, sizeof(buf), fmt, ap);
  44. va_end(ap);
  45. args = Py_BuildValue("(is)", errlevel, buf);
  46. if (!args)
  47. return;
  48. PyObject *result = PyObject_Call(report_callback, args, NULL);
  49. Py_XDECREF(result);
  50. Py_DECREF(args);
  51. }
  52. static PyTypeObject Lexer_Type;
  53. typedef struct {
  54. PyObject_HEAD
  55. struct gps_lexer_t lexer;
  56. } LexerObject;
  57. static LexerObject *
  58. newLexerObject(PyObject *arg UNUSED)
  59. {
  60. LexerObject *self;
  61. self = PyObject_New(LexerObject, &Lexer_Type);
  62. if (self == NULL)
  63. return NULL;
  64. memset(&self->lexer, 0, sizeof(struct gps_lexer_t));
  65. packet_reset(&self->lexer);
  66. return self;
  67. }
  68. /* Lexer methods */
  69. static int
  70. Lexer_init(LexerObject *self)
  71. {
  72. packet_reset(&self->lexer);
  73. return 0;
  74. }
  75. static PyObject *
  76. Lexer_get(LexerObject *self, PyObject *args)
  77. {
  78. ssize_t len;
  79. int fd;
  80. if (!PyArg_ParseTuple(args, "i;missing or invalid file descriptor argument to gps.packet.get", &fd))
  81. return NULL;
  82. len = packet_get(fd, &self->lexer);
  83. if (PyErr_Occurred())
  84. return NULL;
  85. return Py_BuildValue("(i, i, " GPSD_PY_BYTE_FORMAT ", i)",
  86. len,
  87. self->lexer.type,
  88. self->lexer.outbuffer,
  89. self->lexer.outbuflen,
  90. self->lexer.char_counter);
  91. }
  92. static PyObject *
  93. Lexer_reset(LexerObject *self)
  94. {
  95. packet_reset(&self->lexer);
  96. if (PyErr_Occurred())
  97. return NULL;
  98. return 0;
  99. }
  100. static void
  101. Lexer_dealloc(LexerObject *self)
  102. {
  103. PyObject_Del(self);
  104. }
  105. static PyMethodDef Lexer_methods[] = {
  106. {"get", (PyCFunction)Lexer_get, METH_VARARGS,
  107. PyDoc_STR("Get a packet from a file descriptor.")},
  108. {"reset", (PyCFunction)Lexer_reset, METH_NOARGS,
  109. PyDoc_STR("Reset the packet lexer to ground state.")},
  110. {NULL, NULL} /* sentinel */
  111. };
  112. PyDoc_STRVAR(Lexer__doc__,
  113. "GPS packet lexer object\n\
  114. \n\
  115. Fetch a single packet from file descriptor");
  116. static PyTypeObject Lexer_Type = {
  117. /* The ob_type field must be initialized in the module init function
  118. * to be portable to Windows without using C++. */
  119. PyVarObject_HEAD_INIT(NULL, 0)
  120. "gps.packet.lexer", /*tp_name*/
  121. sizeof(LexerObject), /*tp_basicsize*/
  122. 0, /*tp_itemsize*/
  123. /* methods */
  124. (destructor)Lexer_dealloc, /*tp_dealloc*/
  125. 0, /*tp_print*/
  126. 0, /*tp_getattr*/
  127. 0, /*tp_setattr*/
  128. 0, /*tp_compare*/
  129. 0, /*tp_repr*/
  130. 0, /*tp_as_number*/
  131. 0, /*tp_as_sequence*/
  132. 0, /*tp_as_mapping*/
  133. 0, /*tp_hash*/
  134. 0, /*tp_call*/
  135. 0, /*tp_str*/
  136. PyObject_GenericGetAttr, /*tp_getattro*/
  137. 0, /*tp_setattro*/
  138. 0, /*tp_as_buffer*/
  139. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  140. Lexer__doc__, /*tp_doc*/
  141. 0, /*tp_traverse*/
  142. 0, /*tp_clear*/
  143. 0, /*tp_richcompare*/
  144. 0, /*tp_weaklistoffset*/
  145. 0, /*tp_iter*/
  146. 0, /*tp_iternext*/
  147. Lexer_methods, /*tp_methods*/
  148. 0, /*tp_members*/
  149. 0, /*tp_getset*/
  150. 0, /*tp_base*/
  151. 0, /*tp_dict*/
  152. 0, /*tp_descr_get*/
  153. 0, /*tp_descr_set*/
  154. 0, /*tp_dictoffset*/
  155. (initproc)Lexer_init, /*tp_init*/
  156. 0, /*tp_alloc*/
  157. 0, /*tp_new*/
  158. 0, /*tp_free*/
  159. 0, /*tp_is_gc*/
  160. };
  161. /* Function of no arguments returning new Lexer object */
  162. static PyObject *
  163. gpspacket_new(PyObject *self UNUSED, PyObject *args UNUSED)
  164. {
  165. LexerObject *rv;
  166. if (!PyArg_ParseTuple(args, ":new"))
  167. return NULL;
  168. rv = newLexerObject(args);
  169. if (rv == NULL)
  170. return NULL;
  171. return (PyObject *)rv;
  172. }
  173. PyDoc_STRVAR(register_report__doc__,
  174. "register_report(callback)\n\
  175. \n\
  176. callback must be a callable object expecting a string as parameter.");
  177. static PyObject *
  178. register_report(LexerObject *self UNUSED, PyObject *args)
  179. {
  180. PyObject *callback = NULL;
  181. if (!PyArg_ParseTuple(args, "O:register_report", &callback))
  182. return NULL;
  183. if (!PyCallable_Check(callback)) {
  184. PyErr_SetString(PyExc_TypeError, "First argument must be callable");
  185. return NULL;
  186. }
  187. if (report_callback) {
  188. Py_DECREF(report_callback);
  189. report_callback = NULL;
  190. }
  191. report_callback = callback;
  192. Py_INCREF(report_callback);
  193. Py_INCREF(Py_None);
  194. return Py_None;
  195. }
  196. /* List of functions defined in the module */
  197. static PyMethodDef packet_methods[] = {
  198. {"new", gpspacket_new, METH_VARARGS,
  199. PyDoc_STR("new() -> new packet-lexer object")},
  200. {"register_report", (PyCFunction)register_report, METH_VARARGS,
  201. register_report__doc__},
  202. {NULL, NULL} /* sentinel */
  203. };
  204. PyDoc_STRVAR(module_doc,
  205. "Python binding of the libgpsd module for recognizing GPS packets.\n\
  206. The new() function returns a new packet-lexer instance. Lexer instances\n\
  207. have two methods:\n\
  208. get() takes a file descriptor argument and returns a tuple consisting of\n\
  209. the integer packet type and string packet value. On end of file it returns\n\
  210. (-1, '').\n\
  211. reset() resets the packet-lexer to its initial state.\n\
  212. The module also has a register_report() function that accepts a callback\n\
  213. for debug message reporting. The callback will get two arguments, the error\n\
  214. level of the message and the message itself.\n\
  215. ");
  216. /* banishes a pointless compiler warning */
  217. extern PyMODINIT_FUNC initpacket(void);
  218. // cppcheck-suppress unusedFunction
  219. GPSD_PY_MODULE_INIT(packet)
  220. {
  221. PyObject *m;
  222. /* Create the module and add the functions */
  223. GPSD_PY_MODULE_DEF(m, "packet", module_doc, packet_methods)
  224. if (m == NULL || PyType_Ready(&Lexer_Type) < 0)
  225. return GPSD_PY_MODULE_ERROR_VAL;
  226. PyModule_AddIntConstant(m, "BAD_PACKET", BAD_PACKET);
  227. PyModule_AddIntConstant(m, "COMMENT_PACKET", COMMENT_PACKET);
  228. PyModule_AddIntConstant(m, "NMEA_PACKET", NMEA_PACKET);
  229. PyModule_AddIntConstant(m, "AIVDM_PACKET", AIVDM_PACKET);
  230. PyModule_AddIntConstant(m, "GARMINTXT_PACKET", GARMINTXT_PACKET);
  231. PyModule_AddIntConstant(m, "SIRF_PACKET", SIRF_PACKET);
  232. PyModule_AddIntConstant(m, "ZODIAC_PACKET", ZODIAC_PACKET);
  233. PyModule_AddIntConstant(m, "TSIP_PACKET", TSIP_PACKET);
  234. PyModule_AddIntConstant(m, "EVERMORE_PACKET", EVERMORE_PACKET);
  235. PyModule_AddIntConstant(m, "ITALK_PACKET", ITALK_PACKET);
  236. PyModule_AddIntConstant(m, "GARMIN_PACKET", GARMIN_PACKET);
  237. PyModule_AddIntConstant(m, "NAVCOM_PACKET", NAVCOM_PACKET);
  238. PyModule_AddIntConstant(m, "UBX_PACKET", UBX_PACKET);
  239. PyModule_AddIntConstant(m, "SUPERSTAR2_PACKET", SUPERSTAR2_PACKET);
  240. PyModule_AddIntConstant(m, "ONCORE_PACKET", ONCORE_PACKET);
  241. PyModule_AddIntConstant(m, "GEOSTAR_PACKET", GEOSTAR_PACKET);
  242. PyModule_AddIntConstant(m, "RTCM2_PACKET", RTCM2_PACKET);
  243. PyModule_AddIntConstant(m, "RTCM3_PACKET", RTCM3_PACKET);
  244. PyModule_AddIntConstant(m, "JSON_PACKET", JSON_PACKET);
  245. PyModule_AddIntConstant(m, "PACKET_TYPES", PACKET_TYPES);
  246. PyModule_AddIntConstant(m, "LOG_ERROR", LOG_ERROR);
  247. PyModule_AddIntConstant(m, "LOG_SHOUT", LOG_SHOUT);
  248. PyModule_AddIntConstant(m, "LOG_WARN", LOG_WARN);
  249. PyModule_AddIntConstant(m, "LOG_CLIENT", LOG_CLIENT);
  250. PyModule_AddIntConstant(m, "LOG_INF", LOG_INF);
  251. PyModule_AddIntConstant(m, "LOG_PROG", LOG_PROG);
  252. PyModule_AddIntConstant(m, "LOG_IO", LOG_IO);
  253. PyModule_AddIntConstant(m, "LOG_DATA", LOG_DATA);
  254. PyModule_AddIntConstant(m, "LOG_SPIN", LOG_SPIN);
  255. PyModule_AddIntConstant(m, "LOG_RAW", LOG_RAW);
  256. return GPSD_PY_MODULE_SUCCESS_VAL(m);
  257. }