00001
00002 #include <ctype.h>
00003 #include <errno.h>
00004 #include <getopt.h>
00005 #include <inttypes.h>
00006 #include <time.h>
00007 #include <stdint.h>
00008 #include <stdarg.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <stdbool.h>
00013 #include <unistd.h>
00014 #include <readline/readline.h>
00015 #include <readline/history.h>
00016
00017 #include "tm_reader.h"
00018 #include "serial_reader_imp.h"
00019
00020
00021 bool connected;
00022
00023 TMR_Reader r, *rp;
00024 TMR_TransportListenerBlock tb, *listener;
00025 TMR_TagReadData tag;
00026
00027 void errx(int exitval, const char *fmt, ...);
00028 void serialPrinter(bool tx, uint32_t dataLen, const uint8_t data[],
00029 uint32_t timeout, void *cookie);
00030 void stringPrinter(bool tx, uint32_t dataLen, const uint8_t data[],
00031 uint32_t timeout, void *cookie);
00032
00033 void runcmd(int argc, char *argv[]);
00034 int parseEpc(const char *arg, TMR_TagData *tag, int *nchars);
00035 int parseFilter(const char *arg, TMR_TagFilter **filterp, int *nchars);
00036 int parseTagop(const char *arg, TMR_TagOp **tagopp, int *nchars);
00037 int parseU32List(const char *arg, TMR_uint32List *list, int *nchars);
00038 int parseU8List(const char *arg, TMR_uint8List *list, int *nchars);
00039 int parseWords(const char *arg, TMR_uint16List *list);
00040 int parseBytes(const char *arg, TMR_uint8List *list);
00041 int parseReadPlan(const char *arg, TMR_ReadPlan *plan, int *nchars);
00042 int parseLockAction(const char *arg, TMR_TagLockAction *action);
00043 void printU8List(TMR_uint8List *list);
00044 void printU32List(TMR_uint32List *list);
00045 void printPortValueList(TMR_PortValueList *list);
00046 void printReadPlan(TMR_ReadPlan *plan);
00047 void printFilter(TMR_TagFilter *filter);
00048 void printTagop(TMR_TagOp *tagop);
00049 const char *listname(const char *list[], int listlen, unsigned int id);
00050 int listid(const char *list[], int listlen, const char *name);
00051
00052 char *command_generator(const char *, int);
00053 char **demo_completion(const char *, int, int);
00054
00055 #define numberof(x) (sizeof((x))/sizeof((x)[0]))
00056
00057
00058 char *getcommand_interactive();
00059 char *getcommand_noninteractive();
00060 char *getcommand_interactive()
00061 {
00062 return readline(">>> ");
00063 }
00064
00065 int
00066 main(int argc, char *argv[])
00067 {
00068 char *uri;
00069 int opt;
00070 int verbose;
00071 TMR_Status ret;
00072 TMR_Region region;
00073
00074 char *saveptr, *arg, *args[16];
00075 char *line;
00076 char *(*getcommand)();
00077 verbose = 0;
00078 while ((opt = getopt(argc, argv, "v")) != -1)
00079 {
00080 switch (opt) {
00081 case 'v':
00082 verbose = 1;
00083 break;
00084 default:
00085 fprintf(stderr, "Usage: %s [-v] uri\n",
00086 argv[0]);
00087 exit(EXIT_FAILURE);
00088 }
00089 }
00090
00091 argc -= optind;
00092 argv += optind;
00093
00094 if (argc < 1)
00095 {
00096 printf("Usage: demo reader-uri <command> [args]\n"
00097 " (URI: 'tmr:///COM1' or 'tmr://astra-2100d3/' "
00098 "or 'tmr:///dev/ttyS0')\n\n");
00099 exit(1);
00100 }
00101
00102 uri = argv[0];
00103 rp = &r;
00104 ret = TMR_create(rp, uri);
00105
00106 if (TMR_SUCCESS != ret)
00107 {
00108
00109 }
00110
00111 if (TMR_READER_TYPE_SERIAL == rp->readerType)
00112 {
00113 tb.listener = serialPrinter;
00114 }
00115 else
00116 {
00117 tb.listener = stringPrinter;
00118 }
00119
00120 tb.cookie = stdout;
00121 }
00122
00123
00124 void
00125 serialPrinter(bool tx, uint32_t dataLen, const uint8_t data[],
00126 uint32_t timeout, void *cookie)
00127 {
00128 FILE *out;
00129 uint32_t i;
00130
00131 out = cookie;
00132
00133 fprintf(out, "%s", tx ? "Sending: " : "Received:");
00134 for (i = 0; i < dataLen; i++)
00135 {
00136 if (i > 0 && (i & 15) == 0)
00137 {
00138 fprintf(out, "\n ");
00139 }
00140 fprintf(out, " %02x", data[i]);
00141 }
00142 fprintf(out, "\n");
00143 }
00144
00145
00146 void stringPrinter(bool tx,uint32_t dataLen, const uint8_t data[],uint32_t timeout, void *cookie)
00147 {
00148 FILE *out = cookie;
00149
00150 fprintf(out, "%s", tx ? "Sending: " : "Received:");
00151 fprintf(out, "%s\n", data);
00152 }