diff --git a/README.md b/README.md index a5495b58..340231de 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,16 @@ def stats_received(stats): reader.enable_stats(stats_received) ``` +#### reader.enable_transportlog() +Print the transport logs logs to stdout + +The function must be called before `reader.start_reading()`. + +For example: +```python +reader.enable_transportlog() +``` + ### reader.enable_exception_handler(*callback*) Provide reader exception handling The function must be called before `reader.start_reading()`. @@ -292,6 +302,19 @@ print(reader.get_sofware_version()) #### reader.get_serial() Returns a serial number of the reader, the same number printed on the barcode label. +#### reader.get_read_state() +Returns the read state of the reader. + +The avaiable states are : +``` +TMR_READ_STATE_IDLE +TMR_READ_STATE_STARTING +TMR_READ_STATE_STARTED +TMR_READ_STATE_ACTIVE +TMR_READ_STATE_DONE +UNKNOWN +``` + #### reader.set_region(*region*) Controls the Region of Operation for the connected device: * *region* represents the regulatory region that the device will operate in. Supported values are: diff --git a/mercury.c b/mercury.c index f4fc0fcf..ce3344e3 100644 --- a/mercury.c +++ b/mercury.c @@ -40,9 +40,11 @@ typedef struct { TMR_ReadListenerBlock readListener; TMR_StatsListenerBlock statsListener; TMR_ReadExceptionListenerBlock exceptionListener; + TMR_TransportListenerBlock transportLogListener; PyObject *readCallback; PyObject *statsCallback; PyObject *exceptionCallback; + PyObject *transportLogCallback; } Reader; typedef struct { @@ -96,6 +98,11 @@ object2str(PyObject *name) return NULL; } } +static void +invoke_transportlog_callback_serial(bool tx, uint32_t dataLen, const uint8_t data[],uint32_t timeout, void *cookie); + +static void +invoke_transportlog_callback_string(bool tx, uint32_t dataLen, const uint8_t data[],uint32_t timeout, void *cookie); typedef struct { const char* name; @@ -188,6 +195,14 @@ Reader_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if ((ret = TMR_addReadExceptionListener(&self->reader, &self->exceptionListener)) != TMR_SUCCESS) goto fail; + if(TMR_READER_TYPE_SERIAL == self->reader.readerType ){ + self->transportLogListener.listener = invoke_transportlog_callback_serial; + } + else{ + self->transportLogListener.listener = invoke_transportlog_callback_string; + } + self->transportLogListener.cookie = stdout; + if ((ret = TMR_connect(&self->reader)) != TMR_SUCCESS) goto fail; @@ -767,6 +782,14 @@ Reader_enable_stats(Reader *self, PyObject *args, PyObject *kwds) } +static PyObject * +Reader_enable_transportlog(Reader *self){ + int ret; + if ((ret = TMR_addTransportListener(&self->reader, &self->transportLogListener)) != TMR_SUCCESS) + return NULL; + Py_RETURN_NONE; +} + static PyObject * Reader_enable_exception_handler(Reader *self, PyObject *args, PyObject *kwds) { @@ -832,6 +855,32 @@ Reader_start_reading(Reader *self, PyObject *args, PyObject *kwds) Py_RETURN_NONE; } + +static void +invoke_transportlog_callback_string(bool tx, uint32_t dataLen, const uint8_t data[], + uint32_t timeout, void *cookie){ + FILE *out = cookie; + fprintf(out, "%s", tx ? "Sending: " : "Received:"); + fprintf(out, "%s\n", data); +} + +static void +invoke_transportlog_callback_serial(bool tx, uint32_t dataLen, const uint8_t data[], + uint32_t timeout, void *cookie){ + FILE *out = cookie; + + uint32_t i; + printf("tranport_log\n"); + fprintf(out, "%s", tx ? "Sending: " : "Received:"); + for (i = 0; i < dataLen; i++) + { + if (i > 0 && (i & 15) == 0) + fprintf(out, "\n "); + fprintf(out, " %02x", data[i]); + } + fprintf(out, "\n"); +} + static void invoke_exception_callback(TMR_Reader *reader, const TMR_Status error, void *cookie){ Reader *self = (Reader *)cookie; @@ -1126,6 +1175,26 @@ Reader_get_serial(Reader* self) return get_string(&self->reader, TMR_PARAM_VERSION_SERIAL); } +static PyObject * +Reader_get_read_state(Reader* self) +{ + switch (self->reader.readState) + { + case TMR_READ_STATE_IDLE: + return PyUnicode_FromString("TMR_READ_STATE_IDLE"); + case TMR_READ_STATE_STARTING: + return PyUnicode_FromString("TMR_READ_STATE_STARTING"); + case TMR_READ_STATE_STARTED: + return PyUnicode_FromString("TMR_READ_STATE_STARTED"); + case TMR_READ_STATE_ACTIVE: + return PyUnicode_FromString("TMR_READ_STATE_ACTIVE"); + case TMR_READ_STATE_DONE: + return PyUnicode_FromString("TMR_READ_STATE_DONE"); + default: + return PyUnicode_FromString("UNKNOWN"); + } +} + typedef struct { const char* name; TMR_Region region; @@ -1869,6 +1938,9 @@ static PyMethodDef Reader_methods[] = { {"enable_exception_handler",(PyCFunction)Reader_enable_exception_handler, METH_VARARGS | METH_KEYWORDS, "Provide callback for reader exception handling" }, + {"enable_transportlog",(PyCFunction)Reader_enable_transportlog, METH_VARARGS | METH_KEYWORDS, + "Provide callback for reader transport logs" + }, {"start_reading", (PyCFunction)Reader_start_reading, METH_VARARGS | METH_KEYWORDS, "Start reading tags asynchronously" }, @@ -1996,6 +2068,9 @@ static PyMethodDef Reader_methods[] = { {"get_temperature", (PyCFunction)Reader_get_temperature, METH_NOARGS, "Returns the chip temperature" }, + {"get_read_state", (PyCFunction)Reader_get_read_state, METH_NOARGS, + "Returns the current state of the reader" + }, {NULL} /* Sentinel */ };