Skip to content
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down Expand Up @@ -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:
Expand Down
75 changes: 75 additions & 0 deletions mercury.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"
},
Expand Down Expand Up @@ -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 */
};

Expand Down