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