00001
00006 #include <tm_reader.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <stdarg.h>
00010 #include <string.h>
00011 #include <inttypes.h>
00012 #ifndef WIN32
00013 #include <unistd.h>
00014 #endif
00015
00016
00017 #ifndef USE_TRANSPORT_LISTENER
00018 #define USE_TRANSPORT_LISTENER 0
00019 #endif
00020
00021 #define usage() {errx(1, "Please provide reader URL, such as:\n"\
00022 "tmr:///com4 or tmr:///com4 --ant 1,2\n"\
00023 "tmr://my-reader.example.com or tmr://my-reader.example.com --ant 1,2\n");}
00024
00025 void errx(int exitval, const char *fmt, ...)
00026 {
00027 va_list ap;
00028
00029 va_start(ap, fmt);
00030 vfprintf(stderr, fmt, ap);
00031
00032 exit(exitval);
00033 }
00034
00035 void checkerr(TMR_Reader* rp, TMR_Status ret, int exitval, const char *msg)
00036 {
00037 if (TMR_SUCCESS != ret)
00038 {
00039 errx(exitval, "Error %s: %s\n", msg, TMR_strerr(rp, ret));
00040 }
00041 }
00042
00043 void serialPrinter(bool tx, uint32_t dataLen, const uint8_t data[],
00044 uint32_t timeout, void *cookie)
00045 {
00046 FILE *out = cookie;
00047 uint32_t i;
00048
00049 fprintf(out, "%s", tx ? "Sending: " : "Received:");
00050 for (i = 0; i < dataLen; i++)
00051 {
00052 if (i > 0 && (i & 15) == 0)
00053 fprintf(out, "\n ");
00054 fprintf(out, " %02x", data[i]);
00055 }
00056 fprintf(out, "\n");
00057 }
00058
00059 void stringPrinter(bool tx,uint32_t dataLen, const uint8_t data[],uint32_t timeout, void *cookie)
00060 {
00061 FILE *out = cookie;
00062
00063 fprintf(out, "%s", tx ? "Sending: " : "Received:");
00064 fprintf(out, "%s\n", data);
00065 }
00066
00067 void parseAntennaList(uint8_t *antenna, uint8_t *antennaCount, char *args)
00068 {
00069 char *token = NULL;
00070 char *str = ",";
00071 uint8_t i = 0x00;
00072 int scans;
00073
00074
00075 if (NULL == args)
00076 {
00077 fprintf(stdout, "Missing argument\n");
00078 usage();
00079 }
00080
00081 token = strtok(args, str);
00082 if (NULL == token)
00083 {
00084 fprintf(stdout, "Missing argument after %s\n", args);
00085 usage();
00086 }
00087
00088 while(NULL != token)
00089 {
00090 scans = sscanf(token, "%"SCNu8, &antenna[i]);
00091 if (1 != scans)
00092 {
00093 fprintf(stdout, "Can't parse '%s' as an 8-bit unsigned integer value\n", token);
00094 usage();
00095 }
00096 i++;
00097 token = strtok(NULL, str);
00098 }
00099 *antennaCount = i;
00100 }
00101
00102 void callback(TMR_Reader *reader, const TMR_TagReadData *t, void *cookie);
00103 void exceptionCallback(TMR_Reader *reader, TMR_Status error, void *cookie);
00104
00105 int main(int argc, char *argv[])
00106 {
00107
00108 #ifndef TMR_ENABLE_BACKGROUND_READS
00109 errx(1, "This sample requires background read functionality.\n"
00110 "Please enable TMR_ENABLE_BACKGROUND_READS in tm_config.h\n"
00111 "to run this codelet\n");
00112 return -1;
00113 #else
00114
00115 TMR_Reader r, *rp;
00116 TMR_Status ret;
00117 TMR_Region region;
00118 TMR_ReadPlan plan;
00119 TMR_ReadListenerBlock rlb;
00120 TMR_ReadExceptionListenerBlock reb;
00121 uint8_t *antennaList = NULL;
00122 uint8_t buffer[20];
00123 uint8_t i;
00124 uint8_t antennaCount = 0x0;
00125 TMR_String model;
00126 char str[64];
00127 #if USE_TRANSPORT_LISTENER
00128 TMR_TransportListenerBlock tb;
00129 #endif
00130
00131 if (argc < 2)
00132 {
00133 usage();
00134 }
00135
00136 for (i = 2; i < argc; i+=2)
00137 {
00138 if(0x00 == strcmp("--ant", argv[i]))
00139 {
00140 if (NULL != antennaList)
00141 {
00142 fprintf(stdout, "Duplicate argument: --ant specified more than once\n");
00143 usage();
00144 }
00145 parseAntennaList(buffer, &antennaCount, argv[i+1]);
00146 antennaList = buffer;
00147 }
00148 else
00149 {
00150 fprintf(stdout, "Argument %s is not recognized\n", argv[i]);
00151 usage();
00152 }
00153 }
00154
00155 rp = &r;
00156 ret = TMR_create(rp, argv[1]);
00157 checkerr(rp, ret, 1, "creating reader");
00158
00159 #if USE_TRANSPORT_LISTENER
00160
00161 if (TMR_READER_TYPE_SERIAL == rp->readerType)
00162 {
00163 tb.listener = serialPrinter;
00164 }
00165 else
00166 {
00167 tb.listener = stringPrinter;
00168 }
00169 tb.cookie = stdout;
00170
00171 TMR_addTransportListener(rp, &tb);
00172 #endif
00173
00174 ret = TMR_connect(rp);
00175 checkerr(rp, ret, 1, "connecting reader");
00176
00177 region = TMR_REGION_NONE;
00178 ret = TMR_paramGet(rp, TMR_PARAM_REGION_ID, ®ion);
00179 checkerr(rp, ret, 1, "getting region");
00180
00181 if (TMR_REGION_NONE == region)
00182 {
00183 TMR_RegionList regions;
00184 TMR_Region _regionStore[32];
00185 regions.list = _regionStore;
00186 regions.max = sizeof(_regionStore)/sizeof(_regionStore[0]);
00187 regions.len = 0;
00188
00189 ret = TMR_paramGet(rp, TMR_PARAM_REGION_SUPPORTEDREGIONS, ®ions);
00190 checkerr(rp, ret, __LINE__, "getting supported regions");
00191
00192 if (regions.len < 1)
00193 {
00194 checkerr(rp, TMR_ERROR_INVALID_REGION, __LINE__, "Reader doesn't supportany regions");
00195 }
00196 region = regions.list[0];
00197 ret = TMR_paramSet(rp, TMR_PARAM_REGION_ID, ®ion);
00198 checkerr(rp, ret, 1, "setting region");
00199 }
00200
00201 model.value = str;
00202 model.max = 64;
00203 TMR_paramGet(rp, TMR_PARAM_VERSION_MODEL, &model);
00204 if (((0 == strcmp("Sargas", model.value)) || (0 == strcmp("M6e Micro", model.value)) ||(0 == strcmp("M6e Nano", model.value)))
00205 && (NULL == antennaList))
00206 {
00207 fprintf(stdout, "Module doesn't has antenna detection support please provide antenna list\n");
00208 usage();
00209 }
00210
00218
00219 ret = TMR_RP_init_simple(&plan, antennaCount, antennaList, TMR_TAG_PROTOCOL_GEN2, 1000);
00220 checkerr(rp, ret, 1, "initializing the read plan");
00221
00222
00223 ret = TMR_paramSet(rp, TMR_PARAM_READ_PLAN, &plan);
00224 checkerr(rp, ret, 1, "setting read plan");
00225
00226 rlb.listener = callback;
00227 rlb.cookie = NULL;
00228
00229 reb.listener = exceptionCallback;
00230 reb.cookie = NULL;
00231
00232 ret = TMR_addReadListener(rp, &rlb);
00233 checkerr(rp, ret, 1, "adding read listener");
00234
00235 ret = TMR_addReadExceptionListener(rp, &reb);
00236 checkerr(rp, ret, 1, "adding exception listener");
00237
00238 ret = TMR_startReading(rp);
00239 checkerr(rp, ret, 1, "starting reading");
00240
00241 #ifndef WIN32
00242 sleep(5);
00243 #else
00244 Sleep(5000);
00245 #endif
00246
00247 ret = TMR_stopReading(rp);
00248 checkerr(rp, ret, 1, "stopping reading");
00249
00250 TMR_destroy(rp);
00251 return 0;
00252
00253 #endif
00254 }
00255
00256
00257 void
00258 callback(TMR_Reader *reader, const TMR_TagReadData *t, void *cookie)
00259 {
00260 char epcStr[128];
00261
00262 TMR_bytesToHex(t->tag.epc, t->tag.epcByteCount, epcStr);
00263 printf("Background read: %s\n", epcStr);
00264 }
00265
00266 void
00267 exceptionCallback(TMR_Reader *reader, TMR_Status error, void *cookie)
00268 {
00269 fprintf(stdout, "Error:%s\n", TMR_strerr(reader, error));
00270 }