]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_rfid.c
doc
[coffee/mt-apps.git] / mt_rfid.c
1 /**
2  * mt_rfid.c
3  * poll the uFR reader and print JSON formatted card info to stdout.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <pthread.h>
10 #include <signal.h>
11
12 #include "mt_rfid.h"
13 #include "uFCoder.h"
14
15 // really simple JSON helpers
16 #define JSON_NUM(NAME) printf("\"" #NAME "\": %d", NAME)
17 #define JSON_STR(NAME) printf("\"" #NAME "\": \"%s\"", NAME)
18 #define JSON_START() printf("{\n")
19 #define JSON_NEXT() printf(",\n")
20 #define JSON_END() printf("\n}\n")
21
22 static mt_rfid_t ufr = {READER_TYPE, PORT_NAME, PORT_INTERFACE, 0, 1};
23
24 static void signal_exit(int sig)
25 {
26     ufr.run = 0;
27     fprintf(stderr, "signal %d, terminating\n", sig);
28 }
29
30 void *mt_rfid_run(void *ptr)
31 {
32     mt_rfid_t *self = (mt_rfid_t *)ptr;
33     UFR_STATUS status;
34
35     uint8_t card_type;
36     uint8_t sak;         //select acknowledge
37     uint8_t uid_num[10]; //uid as bytes
38     uint8_t uid_size;
39     char uid[24];        //uid as a string
40
41     fprintf(stderr, "uFCoder library version: %s\n", GetDllVersionStr());
42
43     status = ReaderOpenEx(self->reader_type, self->port_name, self->port_interface, 0);
44     if (status != UFR_OK) {
45         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
46         return NULL;
47     }
48
49     fputs(GetReaderDescription(), stderr);
50
51     while (self->run) {
52         status = GetDlogicCardType(&card_type);
53         if (status != UFR_OK) {
54             if (status != UFR_NO_CARD) {
55                 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
56             }
57             usleep(10000);
58             continue;
59         }
60
61         status = GetCardIdEx(&sak, uid_num, &uid_size);
62         if (status != UFR_OK) {
63             if (status != UFR_NO_CARD) {
64                 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
65             }
66             usleep(10000);
67             continue;
68         }
69
70         for (uint8_t i = 0; i < uid_size; i++) {
71             sprintf(&uid[2*i], "%02x", uid_num[i]);
72         }
73
74         JSON_START();
75         JSON_NUM(card_type); JSON_NEXT();
76         JSON_NUM(sak);       JSON_NEXT();
77         JSON_NUM(uid_size);  JSON_NEXT();
78         JSON_STR(uid);       JSON_END();
79
80         if (self->beep) {
81             ReaderUISignal(0, 1);
82         }
83
84         usleep(500000);
85     }
86
87     status = ReaderClose();
88     fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
89
90     return NULL;
91 }
92
93 int main(int argc, char **argv)
94 {
95     pthread_t t;
96     char c;
97
98     while ((c = getopt(argc, argv, "p:bh")) != -1) {
99         switch (c) {
100             case 'p':
101                 ufr.port_name = optarg;
102                 break;
103             case 'b':
104                 ufr.beep = 1;
105                 break;
106         }
107     }
108
109     // avoid sigaction for windows compatibility
110     signal(SIGINT, signal_exit);
111     signal(SIGTERM, signal_exit);
112
113     if (pthread_create(&t, NULL, mt_rfid_run, (void *)&ufr)) {
114         perror("pthread_create");
115         return -1;
116     }
117
118     pthread_join(t, NULL);
119
120     return 0;
121 }