PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/API/mercuryapi-1.29.4.34/c/src/samples/serialtime.c

https://bitbucket.org/jrevell_ortus/isa-application-linux-demo
C | 319 lines | 247 code | 46 blank | 26 comment | 37 complexity | 909865a48467dbc736899aeb0dd509ec MD5 | raw file
  1. /**
  2. * Sample program that shows the custom transportlistener
  3. * @file serialtime.c
  4. */
  5. #include <tm_reader.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdarg.h>
  9. #include <string.h>
  10. #include <inttypes.h>
  11. #if WIN32
  12. #define snprintf sprintf_s
  13. #endif
  14. /* Enable this to use transportListener */
  15. #ifndef USE_TRANSPORT_LISTENER
  16. #define USE_TRANSPORT_LISTENER 0
  17. #endif
  18. #define usage() {errx(1, "Please provide reader URL, such as:\n"\
  19. "tmr:///com4 or tmr:///com4 --ant 1,2\n"\
  20. "tmr://my-reader.example.com or tmr://my-reader.example.com --ant 1,2\n");}
  21. void errx(int exitval, const char *fmt, ...)
  22. {
  23. va_list ap;
  24. va_start(ap, fmt);
  25. vfprintf(stderr, fmt, ap);
  26. exit(exitval);
  27. }
  28. void checkerr(TMR_Reader* rp, TMR_Status ret, int exitval, const char *msg)
  29. {
  30. if (TMR_SUCCESS != ret)
  31. {
  32. errx(exitval, "Error %s: %s\n", msg, TMR_strerr(rp, ret));
  33. }
  34. }
  35. void timeStampSerialListener(bool tx, uint32_t dataLen, const uint8_t data[],
  36. uint32_t timeout, void *cookie)
  37. {
  38. FILE *out = cookie;
  39. uint32_t i;
  40. fprintf(out, "%s", tx ? "Sending: " : "Received:");
  41. for (i = 0; i < dataLen; i++)
  42. {
  43. if (i > 0 && (i & 15) == 0)
  44. {
  45. fprintf(out, "\n ");
  46. }
  47. fprintf(out, " %02x", data[i]);
  48. }
  49. fprintf(out, "\n");
  50. }
  51. void timeStampStringListener(bool tx,uint32_t dataLen, const uint8_t data[],uint32_t timeout, void *cookie)
  52. {
  53. FILE *out = cookie;
  54. fprintf(out, "%s", tx ? "Sending: " : "Received:");
  55. fprintf(out, "%s\n", data);
  56. }
  57. void parseAntennaList(uint8_t *antenna, uint8_t *antennaCount, char *args)
  58. {
  59. char *token = NULL;
  60. char *str = ",";
  61. uint8_t i = 0x00;
  62. int scans;
  63. /* get the first token */
  64. if (NULL == args)
  65. {
  66. fprintf(stdout, "Missing argument\n");
  67. usage();
  68. }
  69. token = strtok(args, str);
  70. if (NULL == token)
  71. {
  72. fprintf(stdout, "Missing argument after %s\n", args);
  73. usage();
  74. }
  75. while(NULL != token)
  76. {
  77. scans = sscanf(token, "%"SCNu8, &antenna[i]);
  78. if (1 != scans)
  79. {
  80. fprintf(stdout, "Can't parse '%s' as an 8-bit unsigned integer value\n", token);
  81. usage();
  82. }
  83. i++;
  84. token = strtok(NULL, str);
  85. }
  86. *antennaCount = i;
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. TMR_Reader r, *rp;
  91. TMR_Status ret;
  92. TMR_Region region;
  93. TMR_ReadPlan plan;
  94. uint8_t *antennaList = NULL;
  95. uint8_t buffer[20];
  96. uint8_t i;
  97. uint8_t antennaCount = 0x0;
  98. TMR_String model;
  99. char str[64];
  100. #if USE_TRANSPORT_LISTENER
  101. TMR_TransportListenerBlock tb;
  102. #endif
  103. if (argc < 2)
  104. {
  105. usage();
  106. }
  107. for (i = 2; i < argc; i+=2)
  108. {
  109. if(0x00 == strcmp("--ant", argv[i]))
  110. {
  111. if (NULL != antennaList)
  112. {
  113. fprintf(stdout, "Duplicate argument: --ant specified more than once\n");
  114. usage();
  115. }
  116. parseAntennaList(buffer, &antennaCount, argv[i+1]);
  117. antennaList = buffer;
  118. }
  119. else
  120. {
  121. fprintf(stdout, "Argument %s is not recognized\n", argv[i]);
  122. usage();
  123. }
  124. }
  125. rp = &r;
  126. ret = TMR_create(rp, argv[1]);
  127. checkerr(rp, ret, 1, "creating reader");
  128. #if USE_TRANSPORT_LISTENER
  129. if (TMR_READER_TYPE_SERIAL == rp->readerType)
  130. {
  131. tb.listener = timeStampSerialListener;
  132. }
  133. else
  134. {
  135. tb.listener = timeStampStringListener;
  136. }
  137. tb.cookie = stdout;
  138. TMR_addTransportListener(rp, &tb);
  139. #endif
  140. ret = TMR_connect(rp);
  141. checkerr(rp, ret, 1, "connecting reader");
  142. region = TMR_REGION_NONE;
  143. ret = TMR_paramGet(rp, TMR_PARAM_REGION_ID, &region);
  144. checkerr(rp, ret, 1, "getting region");
  145. if (TMR_REGION_NONE == region)
  146. {
  147. TMR_RegionList regions;
  148. TMR_Region _regionStore[32];
  149. regions.list = _regionStore;
  150. regions.max = sizeof(_regionStore)/sizeof(_regionStore[0]);
  151. regions.len = 0;
  152. ret = TMR_paramGet(rp, TMR_PARAM_REGION_SUPPORTEDREGIONS, &regions);
  153. checkerr(rp, ret, 1, "getting supported regions");
  154. if (regions.len < 1)
  155. {
  156. checkerr(rp, TMR_ERROR_INVALID_REGION, __LINE__, "Reader doesn't supportany regions");
  157. }
  158. region = regions.list[0];
  159. ret = TMR_paramSet(rp, TMR_PARAM_REGION_ID, &region);
  160. checkerr(rp, ret, 1, "setting region");
  161. }
  162. model.value = str;
  163. model.max = 64;
  164. TMR_paramGet(rp, TMR_PARAM_VERSION_MODEL, &model);
  165. if (((0 == strcmp("Sargas", model.value)) || (0 == strcmp("M6e Micro", model.value)) ||(0 == strcmp("M6e Nano", model.value)))
  166. && (NULL == antennaList))
  167. {
  168. fprintf(stdout, "Module doesn't has antenna detection support please provide antenna list\n");
  169. usage();
  170. }
  171. /**
  172. * for antenna configuration we need two parameters
  173. * 1. antennaCount : specifies the no of antennas should
  174. * be included in the read plan, out of the provided antenna list.
  175. * 2. antennaList : specifies a list of antennas for the read plan.
  176. **/
  177. // initialize the read plan
  178. ret = TMR_RP_init_simple(&plan, antennaCount, antennaList, TMR_TAG_PROTOCOL_GEN2, 1000);
  179. checkerr(rp, ret, 1, "initializing the read plan");
  180. /* Commit read plan */
  181. ret = TMR_paramSet(rp, TMR_PARAM_READ_PLAN, &plan);
  182. checkerr(rp, ret, 1, "setting read plan");
  183. ret = TMR_read(rp, 500, NULL);
  184. if (TMR_ERROR_TAG_ID_BUFFER_FULL == ret)
  185. {
  186. /* In case of TAG ID Buffer Full, extract the tags present
  187. * in buffer.
  188. */
  189. fprintf(stdout, "reading tags:%s\n", TMR_strerr(rp, ret));
  190. }
  191. else
  192. {
  193. checkerr(rp, ret, 1, "reading tags");
  194. }
  195. while (TMR_SUCCESS == TMR_hasMoreTags(rp))
  196. {
  197. TMR_TagReadData trd;
  198. char epcStr[128];
  199. char timeStr[128];
  200. ret = TMR_getNextTag(rp, &trd);
  201. checkerr(rp, ret, 1, "fetching tag");
  202. TMR_bytesToHex(trd.tag.epc, trd.tag.epcByteCount, epcStr);
  203. #ifdef WIN32
  204. if (rp->readerType == TMR_READER_TYPE_SERIAL)
  205. {
  206. FILETIME ft, utc;
  207. SYSTEMTIME st;
  208. char* timeEnd;
  209. char* end;
  210. utc.dwHighDateTime = trd.timestampHigh;
  211. utc.dwLowDateTime = trd.timestampLow;
  212. FileTimeToLocalFileTime( &utc, &ft );
  213. FileTimeToSystemTime( &ft, &st );
  214. timeEnd = timeStr + sizeof(timeStr)/sizeof(timeStr[0]);
  215. end = timeStr;
  216. end += sprintf(end, "%d-%d-%d", st.wYear,st.wMonth,st.wDay);
  217. end += sprintf(end, "T%d:%d:%d %d", st.wHour,st.wMinute,st.wSecond, st.wMilliseconds);
  218. end += sprintf(end, ".%06d", trd.dspMicros);
  219. }
  220. else
  221. {
  222. uint8_t shift;
  223. uint64_t timestamp;
  224. time_t seconds;
  225. int micros;
  226. char* timeEnd;
  227. char* end;
  228. shift = 32;
  229. timestamp = ((uint64_t)trd.timestampHigh<<shift) | trd.timestampLow;
  230. seconds = timestamp / 1000;
  231. micros = (timestamp % 1000) * 1000;
  232. /*
  233. * Timestamp already includes millisecond part of dspMicros,
  234. * so subtract this out before adding in dspMicros again
  235. */
  236. micros -= trd.dspMicros / 1000;
  237. micros += trd.dspMicros;
  238. timeEnd = timeStr + sizeof(timeStr)/sizeof(timeStr[0]);
  239. end = timeStr;
  240. end += strftime(end, timeEnd-end, "%Y-%m-%dT%H:%M:%S", localtime(&seconds));
  241. end += snprintf(end, timeEnd-end, ".%06d", micros);
  242. // end += strftime(end, timeEnd-end, "%z", localtime(&seconds));
  243. }
  244. #else
  245. {
  246. uint8_t shift;
  247. time_t timestamp;
  248. time_t seconds;
  249. int micros;
  250. char* timeEnd;
  251. char* end;
  252. shift = 32;
  253. timestamp = ((time_t)trd.timestampHigh<<shift) | trd.timestampLow;
  254. seconds = timestamp / 1000;
  255. micros = (timestamp % 1000) * 1000;
  256. /*
  257. * Timestamp already includes millisecond part of dspMicros,
  258. * so subtract this out before adding in dspMicros again
  259. */
  260. micros -= trd.dspMicros / 1000;
  261. micros += trd.dspMicros;
  262. timeEnd = timeStr + sizeof(timeStr)/sizeof(timeStr[0]);
  263. end = timeStr;
  264. end += strftime(end, timeEnd-end, "%FT%H:%M:%S", localtime(&seconds));
  265. end += snprintf(end, timeEnd-end, ".%06d", micros);
  266. end += strftime(end, timeEnd-end, "%z", localtime(&seconds));
  267. }
  268. #endif
  269. printf("EPC:%s ant:%d count:%d Time:%s\n", epcStr, trd.antenna, trd.readCount, timeStr);
  270. }
  271. TMR_destroy(rp);
  272. return 0;
  273. }