-
Notifications
You must be signed in to change notification settings - Fork 63
/
mercury.c
2395 lines (2043 loc) · 67 KB
/
mercury.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016-2020 Petr Gotthard <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <tm_reader.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#define MAX_ANTENNA_COUNT 16
#define MAX_GPIO_COUNT 4
#define MAX_DATA_AREA 258
#define numberof(x) (sizeof((x))/sizeof((x)[0]))
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
TMR_Reader reader;
TMR_TagFilter *tag_filter;
TMR_TagOp tagop;
uint8_t antennas[MAX_ANTENNA_COUNT];
TMR_ReadListenerBlock readListener;
TMR_StatsListenerBlock statsListener;
TMR_ReadExceptionListenerBlock exceptionListener;
PyObject *readCallback;
PyObject *statsCallback;
PyObject *exceptionCallback;
pthread_mutex_t stopReadingLock;
} Reader;
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
TMR_TagData tag;
} TagData;
typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
TMR_TagReadData data;
PyObject *epcMemData;
PyObject *tidMemData;
PyObject *userMemData;
PyObject *reservedMemData;
} TagReadData;
typedef struct {
PyObject_HEAD
int8_t temperature;
PyObject *protocol;
uint16_t antenna;
uint32_t frequency;
} ReaderStatsData;
static PyTypeObject TagReadDataType;
static PyTypeObject ReaderStatsDataType;
static void
invoke_read_callback(TMR_Reader *reader, const TMR_TagReadData *tag, void *cookie);
static void
invoke_stats_callback(TMR_Reader *reader,const TMR_Reader_StatsValues *reader_stats, void *cookie);
static void
invoke_exception_callback(TMR_Reader *reader, const TMR_Status ret, void *cookie);
static const char *
object2str(PyObject *name)
{
if (PyBytes_Check(name))
return PyBytes_AsString(name);
#if PY_MAJOR_VERSION >= 3
else if (PyUnicode_Check(name))
return PyUnicode_AsUTF8(name);
#endif
else
{
PyErr_SetString(PyExc_TypeError, "expecting string");
return NULL;
}
}
typedef struct {
const char* name;
TMR_TagProtocol protocol;
} Protocols;
static Protocols Reader_protocols[] = {
{"ISO180006B", TMR_TAG_PROTOCOL_ISO180006B},
{"GEN2", TMR_TAG_PROTOCOL_GEN2},
{"UCODE", TMR_TAG_PROTOCOL_ISO180006B_UCODE},
{"IPX64", TMR_TAG_PROTOCOL_IPX64},
{"IPX256", TMR_TAG_PROTOCOL_IPX256},
{"ATA", TMR_TAG_PROTOCOL_ATA},
{NULL, TMR_TAG_PROTOCOL_NONE}
};
static TMR_TagProtocol str2protocol(const char *name)
{
Protocols *prot;
if (name == NULL)
return TMR_TAG_PROTOCOL_NONE;
for(prot = Reader_protocols; prot->name != NULL; prot++)
{
if(strcmp(prot->name, name) == 0)
return prot->protocol;
}
PyErr_SetString(PyExc_TypeError, "unknown protocol");
return TMR_TAG_PROTOCOL_NONE;
}
static const char* protocol2str(TMR_TagProtocol protocol)
{
Protocols *prot;
for(prot = Reader_protocols; prot->name != NULL; prot++)
{
if(prot->protocol == protocol)
return prot->name;
}
return NULL;
}
static PyObject *
Reader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Reader *self;
char *deviceUri;
uint32_t baudRate = 0;
uint8_t antenna = 255;
char *protostr = NULL;
TMR_Status ret;
static char *kwlist[] = {"uri", "baudrate", "antenna", "protocol", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|Ibs", kwlist, &deviceUri, &baudRate, &antenna, &protostr))
return NULL;
self = (Reader *)type->tp_alloc(type, 0);
if (self == NULL)
return NULL;
self->tag_filter = NULL;
pthread_mutex_init(&self->stopReadingLock, NULL);
if ((ret = TMR_create(&self->reader, deviceUri)) != TMR_SUCCESS)
goto fail;
if (baudRate > 0)
{
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_BAUDRATE, &baudRate)) != TMR_SUCCESS)
goto fail;
}
/* install the callback wrapper for asynchronous reading */
self->readListener.listener = invoke_read_callback;
self->readListener.cookie = self;
if ((ret = TMR_addReadListener(&self->reader, &self->readListener)) != TMR_SUCCESS)
goto fail;
/* install callback wrapper for asychronous stats */
self->statsListener.listener = invoke_stats_callback;
self->statsListener.cookie = self;
if ((ret = TMR_addStatsListener(&self->reader, &self->statsListener)) != TMR_SUCCESS)
goto fail;
self->exceptionListener.listener = invoke_exception_callback;
self->exceptionListener.cookie = self;
if ((ret = TMR_addReadExceptionListener(&self->reader, &self->exceptionListener)) != TMR_SUCCESS)
goto fail;
if ((ret = TMR_connect(&self->reader)) != TMR_SUCCESS)
goto fail;
if (antenna < 255)
{
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_TAGOP_ANTENNA, &antenna)) != TMR_SUCCESS)
goto fail;
}
if (protostr != NULL)
{
TMR_TagProtocol protocol;
if ((protocol = str2protocol(protostr)) == TMR_TAG_PROTOCOL_NONE)
goto error;
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_TAGOP_PROTOCOL, &protocol)) != TMR_SUCCESS)
goto fail;
}
return (PyObject *)self;
fail:
PyErr_SetString(PyExc_TypeError, TMR_strerr(&self->reader, ret));
error:
Py_TYPE(self)->tp_free((PyObject*)self);
return NULL;
}
static void
free_filter(TMR_TagFilter *tag_filter)
{
uint16_t i;
if(tag_filter != NULL)
{
if(tag_filter->type == TMR_FILTER_TYPE_GEN2_SELECT)
{
free(tag_filter->u.gen2Select.mask);
}
else if(tag_filter->type == TMR_FILTER_TYPE_MULTI)
{
for(i = 0; i < tag_filter->u.multiFilterList.len; i++)
free_filter(tag_filter->u.multiFilterList.tagFilterList[i]);
free(tag_filter->u.multiFilterList.tagFilterList);
}
}
}
static void
reset_filter(TMR_TagFilter **tag_filter)
{
if(*tag_filter != NULL)
{
free_filter(*tag_filter);
free(*tag_filter);
*tag_filter = NULL;
}
}
static void
Reader_dealloc(Reader* self)
{
reset_filter(&self->tag_filter);
TMR_destroy(&self->reader);
Py_TYPE(self)->tp_free((PyObject*)self);
pthread_mutex_destroy(&self->stopReadingLock);
}
static
uint8_t as_uint8(PyObject *item)
{
long num;
if (PyLong_Check(item))
num = PyLong_AsLong(item);
#if PY_MAJOR_VERSION < 3
else if (PyInt_Check(item))
num = PyInt_AsLong(item);
#endif
else
return 255;
if (num < 0 || num > 255)
return 255;
return (uint8_t)num;
}
static int str2bank(const char *text)
{
if (text == NULL)
return 0;
if(strcmp(text, "reserved") == 0)
return TMR_GEN2_BANK_RESERVED_ENABLED;
else if(strcmp(text, "epc") == 0)
return TMR_GEN2_BANK_EPC | TMR_GEN2_BANK_EPC_ENABLED;
else if(strcmp(text, "tid") == 0)
return TMR_GEN2_BANK_TID | TMR_GEN2_BANK_TID_ENABLED;
else if(strcmp(text, "user") == 0)
return TMR_GEN2_BANK_USER | TMR_GEN2_BANK_USER_ENABLED;
else
{
PyErr_SetString(PyExc_TypeError, "invalid bank name");
return 0;
}
}
typedef struct {
const char* name;
TMR_GEN2_Select_action action;
} Actions;
static Actions Reader_actions[] = {
{"on&off", ON_N_OFF},
{"on&nop", ON_N_NOP},
{"nop&off", NOP_N_OFF},
{"neg&nop", NEG_N_NOP},
{"off&on", OFF_N_ON},
{"off&nop", OFF_N_NOP},
{"nop&on", NOP_N_ON},
{"nop&neg", NOP_N_NEG},
{NULL, -1}
};
static TMR_GEN2_Select_action str2action(const char *name)
{
Actions *act;
if (name == NULL)
return -1;
for(act = Reader_actions; act->name != NULL; act++)
{
if(strcmp(act->name, name) == 0)
return act->action;
}
PyErr_SetString(PyExc_TypeError, "invalid action");
return -1;
}
static int
parse_gen2mask(TMR_TagFilter *tag_filter, TMR_GEN2_Bank bank, uint32_t offset, PyObject *mask)
{
Py_buffer data;
uint8_t *target;
if(PyObject_GetBuffer(mask, &data, PyBUF_SIMPLE) == 0)
{
target = (uint8_t*)malloc(data.len/2);
TMR_hexToBytes(data.buf, target, data.len/2, NULL);
PyBuffer_Release(&data);
TMR_TF_init_gen2_select(tag_filter, false, bank, offset, 8*data.len/2, target);
return 1;
}
else
{
PyErr_SetString(PyExc_TypeError, "byte string expected");
return 0;
}
}
static int
parse_gen2filter(TMR_TagFilter *tag_filter, PyObject *arg, TMR_GEN2_Select_action defaction)
{
if(PyObject_CheckBuffer(arg))
{
/* EPC starts at bit 32 */
if(!parse_gen2mask(tag_filter, TMR_GEN2_BANK_EPC, 32, arg))
return 0;
tag_filter->u.gen2Select.action = defaction;
}
else
{
PyObject *obj;
/* parse bank and mask, set defaults */
if((obj = PyDict_GetItemString(arg, "reserved")) != NULL)
{
if(!parse_gen2mask(tag_filter, TMR_GEN2_BANK_RESERVED, 0, obj))
return 0;
}
else if((obj = PyDict_GetItemString(arg, "epc")) != NULL)
{
/* EPC starts at bit 32 */
if(!parse_gen2mask(tag_filter, TMR_GEN2_BANK_EPC, 32, obj))
return 0;
}
else if((obj = PyDict_GetItemString(arg, "tid")) != NULL)
{
if(!parse_gen2mask(tag_filter, TMR_GEN2_BANK_TID, 0, obj))
return 0;
}
else if((obj = PyDict_GetItemString(arg, "user")) != NULL)
{
if(!parse_gen2mask(tag_filter, TMR_GEN2_BANK_USER, 0, obj))
return 0;
}
else
{
PyErr_SetString(PyExc_TypeError, "reserved, epc, tid or user mask expected");
return 0;
}
if((obj = PyDict_GetItemString(arg, "invert")) != NULL)
{
if(obj == Py_False)
tag_filter->u.gen2Select.invert = 0;
else if(obj == Py_True)
tag_filter->u.gen2Select.invert = 1;
else
{
PyErr_SetString(PyExc_TypeError, "invalid invert flag: boolean expected");
/* target will be freed by the caller */
return 0;
}
}
if((obj = PyDict_GetItemString(arg, "bit")) != NULL)
{
if(PyLong_Check(obj))
tag_filter->u.gen2Select.bitPointer = PyLong_AsLong(obj);
else
{
PyErr_SetString(PyExc_TypeError, "invalid bit offset: number expected");
return 0;
}
}
if((obj = PyDict_GetItemString(arg, "len")) != NULL)
{
if(PyLong_Check(obj))
tag_filter->u.gen2Select.maskBitLength = PyLong_AsLong(obj);
else
{
PyErr_SetString(PyExc_TypeError, "invalid mask length: number expected");
return 0;
}
}
if((obj = PyDict_GetItemString(arg, "action")) != NULL)
{
if (obj == Py_None)
tag_filter->u.gen2Select.action = defaction;
else if ((tag_filter->u.gen2Select.action = str2action(object2str(obj))) == -1)
return 0;
}
else
tag_filter->u.gen2Select.action = defaction;
}
return 1;
}
static int
parse_multifilter(TMR_TagFilter **tag_filter, PyObject *arg)
{
/* no filter */
if (arg == NULL || arg == Py_None)
{
*tag_filter = NULL;
return 1;
}
/* tag data filter - non-protocol-specific */
else if(PyObject_CheckBuffer(arg))
{
Py_buffer data;
TMR_TagData target;
if(PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) == 0)
{
target.epcByteCount = data.len/2;
TMR_hexToBytes(data.buf, target.epc, target.epcByteCount, NULL);
PyBuffer_Release(&data);
*tag_filter = (TMR_TagFilter*)calloc(1, sizeof(TMR_TagFilter));
TMR_TF_init_tag(*tag_filter, &target);
return 1;
}
else
{
PyErr_SetString(PyExc_TypeError, "byte string expected");
return 0;
}
}
/* Gen2 Select filter */
else if(PyDict_Check(arg))
{
*tag_filter = (TMR_TagFilter*)calloc(1, sizeof(TMR_TagFilter));
if(parse_gen2filter(*tag_filter, arg, ON_N_OFF))
return 1;
else
{
reset_filter(tag_filter);
return 0;
}
}
/* Multi Gen2 select filter */
else if(PyList_Check(arg))
{
Py_ssize_t i;
Py_ssize_t size = PyList_Size(arg);
/* we need one for the multifilter and then one for each of its items */
*tag_filter = (TMR_TagFilter*)calloc(size+1, sizeof(TMR_TagFilter));
(*tag_filter)[0].type = TMR_FILTER_TYPE_MULTI;
(*tag_filter)[0].u.multiFilterList.tagFilterList = (TMR_TagFilter**)calloc(size, sizeof(TMR_TagFilter*));
(*tag_filter)[0].u.multiFilterList.max = size;
(*tag_filter)[0].u.multiFilterList.len = size;
for(i = 0; i < size; i++)
{
if(parse_gen2filter(*tag_filter+(i+1), PyList_GetItem(arg, i), i == 0 ? ON_N_OFF : ON_N_NOP))
(*tag_filter)[0].u.multiFilterList.tagFilterList[i] = *tag_filter+(i+1);
else
{
reset_filter(tag_filter);
return 0;
}
}
return 1;
}
else
{
PyErr_SetString(PyExc_TypeError, "filter expected");
return 0;
}
}
static PyObject *
Reader_set_read_plan(Reader *self, PyObject *args, PyObject *kwds)
{
PyObject *list;
char *s;
TMR_TagProtocol protocol;
TMR_ReadPlan plan;
TMR_Status ret;
int i;
uint8_t ant_count;
PyObject *epc_target = NULL;
PyObject *bank = NULL;
int readPower = 0;
static char *kwlist[] = {"antennas", "protocol", "epc_target", "bank", "read_power", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!s|OOi", kwlist, &PyList_Type, &list, &s, &epc_target, &bank, &readPower))
return NULL;
if ((protocol = str2protocol(s)) == TMR_TAG_PROTOCOL_NONE)
return NULL;
if ((ant_count = PyList_Size(list)) > MAX_ANTENNA_COUNT)
{
PyErr_SetString(PyExc_TypeError, "Too many antennas");
return NULL;
}
for (i = 0; i < ant_count; i++)
{
uint8_t num;
if ((num = as_uint8(PyList_GetItem(list, i))) == 255)
{
PyErr_SetString(PyExc_TypeError, "expecting a list of 8-bit integers");
return NULL;
}
self->antennas[i] = num;
}
if ((ret = TMR_RP_init_simple(&plan, ant_count, self->antennas, protocol, 1000)) != TMR_SUCCESS)
goto fail;
reset_filter(&self->tag_filter);
if(parse_multifilter(&self->tag_filter, epc_target))
TMR_RP_set_filter(&plan, self->tag_filter);
else
return NULL;
if (bank != NULL)
{
int op = 0;
if(PyList_Check(bank))
{
for (i = 0; i < PyList_Size(bank); i++)
{
int op2;
if ((op2 = str2bank(object2str(PyList_GetItem(bank, i)))) == 0)
return NULL;
op |= op2;
}
}
else
{
if ((op = str2bank(object2str(bank))) == 0)
return NULL;
}
if ((ret = TMR_TagOp_init_GEN2_ReadData(&self->tagop, op, 0, 0)) != TMR_SUCCESS)
goto fail;
TMR_RP_set_tagop(&plan, &self->tagop);
}
else
TMR_RP_set_tagop(&plan, NULL);
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_READ_PLAN, &plan)) != TMR_SUCCESS)
goto fail;
if (readPower > 0)
{
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_RADIO_READPOWER, &readPower)) != TMR_SUCCESS)
goto fail;
}
Py_RETURN_NONE;
fail:
PyErr_SetString(PyExc_TypeError, TMR_strerr(&self->reader, ret));
return NULL;
}
static PyObject *
Reader_write(Reader *self, PyObject *args, PyObject *kwds)
{
char* epc_data;
PyObject *epc_target = NULL;
Py_ssize_t data_len;
TMR_Status ret;
TMR_TagData data;
TMR_TagFilter *tag_filter = NULL;
// Read call arguments.
static char *kwlist[] = {"epc_code", "epc_target", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#|O", kwlist, &epc_data, &data_len, &epc_target))
return NULL;
/* build data tag to be writen */
data.epcByteCount = data_len/2;
TMR_hexToBytes(epc_data, data.epc, data.epcByteCount, NULL);
/* build target tag to search */
if(!parse_multifilter(&tag_filter, epc_target))
return NULL;
// Write data tag on target tag.
ret = TMR_writeTag(&self->reader, tag_filter, &data);
reset_filter(&tag_filter);
// In case of not target tag found.
if (ret == TMR_ERROR_NO_TAGS_FOUND)
Py_RETURN_FALSE;
else if(ret != TMR_SUCCESS)
{
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
return NULL;
}
Py_RETURN_TRUE;
}
static PyObject *
PyByteArray_FromUInt8List(const TMR_uint8List *array)
{
if(array->len > 0)
return PyByteArray_FromStringAndSize((const char *)array->list, array->len < array->max ? array->len : array->max);
else
return NULL;
}
static PyObject *
Reader_read(Reader *self, PyObject *args, PyObject *kwds)
{
int timeout = 500;
PyObject *list;
TMR_Status ret;
static char *kwlist[] = {"timeout", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i", kwlist, &timeout))
return NULL;
Py_BEGIN_ALLOW_THREADS
ret = TMR_read(&self->reader, timeout, NULL);
Py_END_ALLOW_THREADS
/* In case of TAG ID Buffer Full, extract the tags present in buffer. */
if (ret != TMR_SUCCESS && ret != TMR_ERROR_TAG_ID_BUFFER_FULL)
{
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
return NULL;
}
/* create empty list */
list = PyList_New(0);
if(list == NULL)
return PyErr_NoMemory();
while (TMR_hasMoreTags(&self->reader) == TMR_SUCCESS)
{
TagReadData *tag;
uint8_t dataBuf1[MAX_DATA_AREA];
uint8_t dataBuf2[MAX_DATA_AREA];
uint8_t dataBuf3[MAX_DATA_AREA];
uint8_t dataBuf4[MAX_DATA_AREA];
tag = PyObject_New(TagReadData, &TagReadDataType);
if(tag == NULL)
{
Py_XDECREF(list);
return PyErr_NoMemory();
}
TMR_TRD_init(&tag->data);
tag->epcMemData = NULL;
tag->tidMemData = NULL;
tag->userMemData = NULL;
tag->reservedMemData = NULL;
TMR_TRD_MEMBANK_init_data(&tag->data.epcMemData, MAX_DATA_AREA, dataBuf1);
TMR_TRD_MEMBANK_init_data(&tag->data.tidMemData, MAX_DATA_AREA, dataBuf2);
TMR_TRD_MEMBANK_init_data(&tag->data.userMemData, MAX_DATA_AREA, dataBuf3);
TMR_TRD_MEMBANK_init_data(&tag->data.reservedMemData, MAX_DATA_AREA, dataBuf4);
if ((ret = TMR_getNextTag(&self->reader, &tag->data)) != TMR_SUCCESS)
{
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
Py_XDECREF(tag);
Py_XDECREF(list);
return NULL;
}
tag->epcMemData = PyByteArray_FromUInt8List(&tag->data.epcMemData);
tag->tidMemData = PyByteArray_FromUInt8List(&tag->data.tidMemData);
tag->userMemData = PyByteArray_FromUInt8List(&tag->data.userMemData);
tag->reservedMemData = PyByteArray_FromUInt8List(&tag->data.reservedMemData);
PyList_Append(list, (PyObject *)tag);
Py_XDECREF(tag);
}
return list;
}
static PyObject *
Reader_enable_stats(Reader *self, PyObject *args, PyObject *kwds)
{
PyObject *temp;
TMR_Status ret;
TMR_Reader_StatsFlag setFlag = TMR_READER_STATS_FLAG_TEMPERATURE | TMR_READER_STATS_FLAG_FREQUENCY | TMR_READER_STATS_FLAG_PROTOCOL | TMR_READER_STATS_FLAG_ANTENNA_PORTS;
static char *kwlist[] = {"callback" , NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &temp))
return NULL;
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL;
}
Py_XDECREF(self->statsCallback);
Py_XINCREF(temp);
self->statsCallback = temp;
if ((ret = TMR_paramSet(&self->reader,TMR_PARAM_READER_STATS_ENABLE,&setFlag)) != TMR_SUCCESS)
{
PyErr_SetString(PyExc_TypeError, TMR_strerr(&self->reader, ret));
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
Reader_enable_exception_handler(Reader *self, PyObject *args, PyObject *kwds)
{
PyObject *temp;
static char *kwlist[] = {"callback" , NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &temp))
return NULL;
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL;
}
Py_XDECREF(self->exceptionCallback);
Py_XINCREF(temp);
self->exceptionCallback = temp;
Py_RETURN_NONE;
}
static PyObject *
Reader_start_reading(Reader *self, PyObject *args, PyObject *kwds)
{
PyObject *temp;
int onTime = 250;
int offTime = 0;
TMR_Status ret;
static char *kwlist[] = {"callback", "on_time", "off_time", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ii", kwlist, &temp, &onTime, &offTime))
return NULL;
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL;
}
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_READ_ASYNCONTIME, &onTime)) != TMR_SUCCESS)
{
PyErr_SetString(PyExc_TypeError, TMR_strerr(&self->reader, ret));
return NULL;
}
if ((ret = TMR_paramSet(&self->reader, TMR_PARAM_READ_ASYNCOFFTIME, &offTime)) != TMR_SUCCESS)
{
PyErr_SetString(PyExc_TypeError, TMR_strerr(&self->reader, ret));
return NULL;
}
Py_XDECREF(self->readCallback);
Py_XINCREF(temp);
self->readCallback = temp;
if ((ret = TMR_startReading(&self->reader)) != TMR_SUCCESS)
{
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
return NULL;
}
Py_RETURN_NONE;
}
static void
invoke_exception_callback(TMR_Reader *reader, const TMR_Status error, void *cookie){
Reader *self = (Reader *)cookie;
if(self && self->exceptionCallback)
{
PyObject *arglist;
PyObject *result;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
arglist = Py_BuildValue("(s)", TMR_strerr(&self->reader, error));
result = PyObject_CallObject(self->exceptionCallback, arglist);
if(result != NULL)
Py_DECREF(result);
else
PyErr_Print();
Py_DECREF(arglist);
PyGILState_Release(gstate);
}
}
static void
invoke_read_callback(TMR_Reader *reader, const TMR_TagReadData *pdata, void *cookie)
{
Reader *self = (Reader *)cookie;
if(self && self->readCallback)
{
TagReadData *tag;
PyObject *arglist;
PyObject *result;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
tag = PyObject_New(TagReadData, &TagReadDataType);
/* make a hard-copy */
memcpy(&tag->data, pdata, sizeof(TMR_TagReadData));
tag->epcMemData = NULL;
tag->tidMemData = NULL;
tag->userMemData = NULL;
tag->reservedMemData = NULL;
arglist = Py_BuildValue("(O)", tag);
result = PyObject_CallObject(self->readCallback, arglist);
if(result != NULL)
Py_DECREF(result);
else
PyErr_Print();
Py_DECREF(arglist);
Py_DECREF(tag);
PyGILState_Release(gstate);
}
}
static void
invoke_stats_callback(TMR_Reader *reader, const TMR_Reader_StatsValues *pdata, void *cookie){
Reader *self = (Reader *)cookie;
if(self && self->statsCallback)
{
ReaderStatsData *stats;
PyObject *arglist;
PyObject *result;
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
stats = PyObject_New(ReaderStatsData, &ReaderStatsDataType);
if (TMR_READER_STATS_FLAG_TEMPERATURE & pdata->valid)
stats->temperature = pdata->temperature;
if (TMR_READER_STATS_FLAG_PROTOCOL & pdata->valid)
stats->protocol = Py_BuildValue("s", protocol2str(pdata->protocol));
if (TMR_READER_STATS_FLAG_ANTENNA_PORTS & pdata->valid)
stats->antenna = pdata->antenna;
if (TMR_READER_STATS_FLAG_FREQUENCY & pdata->valid)
stats->frequency = pdata->frequency;
arglist = Py_BuildValue("(O)", stats);
result = PyObject_CallObject(self->statsCallback, arglist);
if(result != NULL)
Py_DECREF(result);
else
PyErr_Print();
Py_DECREF(arglist);
Py_DECREF(stats);
PyGILState_Release(gstate);
}
}
static PyObject *
Reader_stop_reading(Reader* self)
{
TMR_Status ret;
Py_BEGIN_ALLOW_THREADS // release the GIL to avoid deadlock
// use mutex to prevent concurrent calls to TMR_stopReading
pthread_mutex_lock(&self->stopReadingLock);
ret = TMR_stopReading(&self->reader);
pthread_mutex_unlock(&self->stopReadingLock);
Py_END_ALLOW_THREADS // reacquire the GIL
if (ret != TMR_SUCCESS)
{
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
Reader_destroy_reader(Reader* self)
{
TMR_destroy(&self->reader);
Py_RETURN_NONE;
}
static PyObject *
Reader_read_tag_mem(Reader *self, PyObject *args, PyObject *kwds)
{
TMR_Status ret;
PyObject *epc_target = NULL;
uint32_t bank, address, count;
TMR_TagFilter *tag_filter = NULL;
uint8_t *buf;
PyObject *result;
static char *kwlist[] = {"bank", "address", "count", "epc_target", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "III|O", kwlist, &bank, &address, &count, &epc_target))
return NULL;
if(!parse_multifilter(&tag_filter, epc_target))
return NULL;
buf = malloc(count);
ret = TMR_readTagMemBytes(&self->reader, tag_filter, bank, address, (uint16_t)count, buf);
reset_filter(&tag_filter);
if (ret == TMR_ERROR_NO_TAGS_FOUND)
Py_RETURN_NONE;
else if (ret != TMR_SUCCESS)
{
free(buf);
PyErr_SetString(PyExc_RuntimeError, TMR_strerr(&self->reader, ret));
return NULL;
}
result = PyByteArray_FromStringAndSize((const char *)buf, count);
free(buf);
return result;
}
static PyObject *
Reader_write_tag_mem(Reader *self, PyObject *args, PyObject *kwds)
{
TMR_Status ret;
PyObject *epc_target = NULL;
uint32_t bank, address;
PyObject *data;
TMR_TagFilter *tag_filter = NULL;
static char *kwlist[] = {"bank", "address", "data", "epc_target", NULL};