]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_rfid.c
main renamed
[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 "uFCoder.h"
18
19 #define READER_TYPE    1              //uFR type (1Mbps)
20 #ifndef PORT_NAME
21 #define PORT_NAME      "/dev/ttyUSB0" //"" for auto
22 #endif
23 #define PORT_INTERFACE 1              //Serial
24
25 #define JSON_NUM(NAME) printf("\"" #NAME "\": %d", NAME)
26 #define JSON_STR(NAME) printf("\"" #NAME "\": \"%s\"", NAME)
27 #define JSON_START() printf("{\n")
28 #define JSON_NEXT() printf(",\n")
29 #define JSON_END() printf("\n}\n")
30
31 static int run = 1;
32
33 static void signal_exit(int sig)
34 {
35     run = 0;
36     fprintf(stderr, "signal %d, terminating\n", sig);
37 }
38
39 static void *read_cards(void *ptr)
40 {
41     UFR_STATUS status;
42
43     uint8_t card_type;
44     uint8_t sak;
45     uint8_t uid_num[10];
46     uint8_t uid_size;
47     char uid[24];
48
49     fprintf(stderr, "uFCoder library version: %s\n", GetDllVersionStr());
50
51     status = ReaderOpenEx(READER_TYPE, PORT_NAME, PORT_INTERFACE, 0);
52     if (status != UFR_OK) {
53         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
54         return NULL;
55     }
56
57     //status = ReaderUISignal(0, 0);
58     //printf("ReaderUISignal: %s\n", UFR_Status2String(status));
59
60     fputs(GetReaderDescription(), stderr);
61
62     while (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         usleep(500000);
92     }
93
94     status = ReaderClose();
95     fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
96
97     return NULL;
98 }
99
100 int main(void)
101 {
102     pthread_t t;
103
104     signal(SIGINT, signal_exit);
105     signal(SIGTERM, signal_exit);
106
107     if (pthread_create(&t, NULL, read_cards, NULL)) {
108         perror("pthread_create");
109         return -1;
110     }
111
112     pthread_join(t, NULL);
113
114     return 0;
115 }