From: Uwe Hermann Date: Fri, 2 Apr 2010 18:18:27 +0000 (+0200) Subject: Start of code base layout restructuring. X-Git-Tag: libsigrokdecode-0.1.0~349 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=1c6fa20f549c0565ce4fae6ddf7d5eb60db2ddea;p=libsigrokdecode.git Start of code base layout restructuring. --- 1c6fa20f549c0565ce4fae6ddf7d5eb60db2ddea diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..6aac2c1 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,31 @@ +## +## This file is part of the sigrok project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +SUBDIRS = scripts + +lib_LTLIBRARIES = libsigrokdecode.la + +libsigrokdecode_la_SOURCES = decode.c + +libsigrokdecode_la_CPPFLAGS = $(CPPFLAGS_PYTHON) +libsigrokdecode_la_LDFLAGS = $(LDFLAGS_PYTHON) + +include_HEADERS = sigrokdecode.h + diff --git a/decode.c b/decode.c new file mode 100644 index 0000000..bc4b531 --- /dev/null +++ b/decode.c @@ -0,0 +1,175 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include /* First, so we avoid a _POSIX_C_SOURCE warning. */ +#include + +/** + * Initialize libsigrokdecode. + * + * @return 0 upon success, non-zero otherwise. + */ +int sigrokdecode_init(void) +{ + /* Py_Initialize() returns void and usually cannot fail. */ + Py_Initialize(); + + /* FIXME */ + PySys_SetPath("decode/scripts"); + + return 0; +} + +/** + * TODO + * + * @param name TODO + * @return 0 upon success, non-zero otherwise. + */ +int sigrokdecode_load_decoder_file(const char *name) +{ + /* TODO */ + return 0; +} + +/** + * Run the specified decoder function. + * + * @param decodername TODO + * @param inbuf TODO + * @param inbuflen TODO + * @param outbuf TODO + * @param outbuflen TODO + * @return 0 upon success, non-zero otherwise. + */ +int sigrokdecode_run_decoder(const char *decodername, uint8_t *inbuf, + uint64_t inbuflen, uint8_t **outbuf, + uint64_t *outbuflen) +{ + const char *decoder_filename = "transitioncounter"; /* FIXME */ + PyObject *py_name, *py_module, *py_func, *py_args; + PyObject *py_value, *py_result; + int ret; + + /* TODO: Use #defines for the return codes. */ + + /* Return an error upon unusable input. */ + if (decodername == NULL) + return -1; + if (inbuf == NULL) + return -2; + if (inbuflen == 0) /* No point in working on empty buffers. */ + return -3; + if (outbuf == NULL) + return -4; + if (outbuflen == NULL) + return -5; + + /* Get the name of the decoder module/file as Python string. */ + if (!(py_name = PyString_FromString(decoder_filename))) { + PyErr_Print(); + return -6; + } + + /* "Import" the python file/module. */ + if (!(py_module = PyImport_Import(py_name))) { + PyErr_Print(); + Py_DECREF(py_name); + return -7; + } + Py_DECREF(py_name); + + /* Get the decoder/function name as Python callable object. */ + py_func = PyObject_GetAttrString(py_module, decodername); + if (!py_func || !PyCallable_Check(py_func)) { + if (PyErr_Occurred()) + PyErr_Print(); + Py_DECREF(py_module); + return -8; + } + + /* Create a Python tuple of size 1. */ + if (!(py_args = PyTuple_New(1))) { + PyErr_Print(); + Py_DECREF(py_func); + Py_DECREF(py_module); + return -9; + } + + /* Get the input buffer as Python "string" (byte array). */ + /* TODO: int vs. uint64_t for 'inbuflen'? */ + if (!(py_value = Py_BuildValue("s#", inbuf, inbuflen))) { + PyErr_Print(); + Py_DECREF(py_args); + Py_DECREF(py_func); + Py_DECREF(py_module); + return -10; + } + + if (PyTuple_SetItem(py_args, 0, py_value) != 0) { + PyErr_Print(); + Py_DECREF(py_value); + Py_DECREF(py_args); + Py_DECREF(py_func); + Py_DECREF(py_module); + return -11; + } + + if (!(py_result = PyObject_CallObject(py_func, py_args))) { + PyErr_Print(); + Py_DECREF(py_value); + Py_DECREF(py_args); + Py_DECREF(py_func); + Py_DECREF(py_module); + return -12; + } + + if ((ret = PyObject_AsCharBuffer(py_result, (const char **)outbuf, + (Py_ssize_t *)outbuflen))) { + PyErr_Print(); + Py_DECREF(py_result); + Py_DECREF(py_value); + Py_DECREF(py_args); + Py_DECREF(py_func); + Py_DECREF(py_module); + return -13; + } + + Py_DECREF(py_result); + // Py_DECREF(py_value); + Py_DECREF(py_args); + Py_DECREF(py_func); + Py_DECREF(py_module); + + return 0; +} + +/** + * Shutdown libsigrokdecode. + * + * @return 0 upon success, non-zero otherwise. + */ +int sigrokdecode_shutdown(void) +{ + /* Py_Finalize() returns void, any finalization errors are ignored. */ + Py_Finalize(); + + return 0; +} diff --git a/scripts/Makefile.am b/scripts/Makefile.am new file mode 100644 index 0000000..a0601a9 --- /dev/null +++ b/scripts/Makefile.am @@ -0,0 +1,22 @@ +## +## This file is part of the sigrok project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +dist_pkgdata_SCRIPTS = transitioncounter.py + diff --git a/scripts/transitioncounter.py b/scripts/transitioncounter.py new file mode 100644 index 0000000..ba799ec --- /dev/null +++ b/scripts/transitioncounter.py @@ -0,0 +1,37 @@ +## +## This file is part of the sigrok project. +## +## Copyright (C) 2010 Uwe Hermann +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +# FIXME: So far this is just a simple test for the Python embedding code. +def sigrokdecode_count_transitions(inbuf): + """Counts the low->high and high->low transitions in the specified + channel(s) of the signal.""" + + outbuf = 'AB' # FIXME + + # print len(inbuf) + # print type(inbuf) + + # print inbuf + # print str(inbuf[0]) + # print str(inbuf[1]) + + # return inbuf + return outbuf + diff --git a/sigrokdecode.h b/sigrokdecode.h new file mode 100644 index 0000000..7805d85 --- /dev/null +++ b/sigrokdecode.h @@ -0,0 +1,34 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2010 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef LIBSIGROKDECODE_SIGROKDECODE_H +#define LIBSIGROKDECODE_SIGROKDECODE_H + +#include /* First, so we avoid a _POSIX_C_SOURCE warning. */ +#include + +int sigrokdecode_init(void); +int sigrokdecode_load_decoder_file(const char *name); +int sigrokdecode_run_decoder(const char *decodername, uint8_t *inbuf, + uint64_t inbuflen, uint8_t **outbuf, + uint64_t *outbuflen); +int sigrokdecode_shutdown(void); + +#endif