]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - main.c
e652d02e6f305725dd18501fbef009d9c8985235
[coffee/mt-apps.git] / main.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 #define PORT_NAME      "/dev/ttyUSB0"
21 #define PORT_INTERFACE 1 //Serial
22
23 #define JSON_NUM(NAME) printf("\"" #NAME "\": %d", NAME)
24 #define JSON_STR(NAME) printf("\"" #NAME "\": \"%s\"", NAME)
25 #define JSON_START() printf("{\n")
26 #define JSON_NEXT() printf(",\n")
27 #define JSON_END() printf("\n}\n")
28
29 static int run = 1;
30
31 static void signal_exit(int sig)
32 {
33     fprintf(stderr, "signal %d\n", sig);
34     run = 0;
35 }
36
37 static void *read_cards(void *ptr)
38 {
39     UFR_STATUS status;
40
41     uint8_t card_type;
42     uint8_t sak;
43     uint8_t uid_num[10];
44     uint8_t uid_size;
45     char uid[24];
46
47     fprintf(stderr, "uFCoder library version: %s\n", GetDllVersionStr());
48
49     status = ReaderOpenEx(READER_TYPE, PORT_NAME, PORT_INTERFACE, 0);
50     if (status != UFR_OK) {
51         fprintf(stderr, "ReaderOpenEx: %s\n", UFR_Status2String(status));
52         return NULL;
53     }
54
55     //status = ReaderUISignal(0, 0);
56     //printf("ReaderUISignal: %s\n", UFR_Status2String(status));
57
58     fputs(GetReaderDescription(), stderr);
59
60     while (run) {
61         status = GetDlogicCardType(&card_type);
62         if (status != UFR_OK) {
63             if (status != UFR_NO_CARD) {
64                 fprintf(stderr, "GetDlogicCardType: %s\n", UFR_Status2String(status));
65             }
66             usleep(10000);
67             continue;
68         }
69
70         status = GetCardIdEx(&sak, uid_num, &uid_size);
71         if (status != UFR_OK) {
72             if (status != UFR_NO_CARD) {
73                 fprintf(stderr, "GetCardIdEx: %s\n", UFR_Status2String(status));
74             }
75             usleep(10000);
76             continue;
77         }
78
79         for (uint8_t i = 0; i < uid_size; i++) {
80             sprintf(&uid[2*i], "%02x", uid_num[i]);
81         }
82
83         JSON_START();
84         JSON_NUM(card_type); JSON_NEXT();
85         JSON_NUM(sak);       JSON_NEXT();
86         JSON_NUM(uid_size);  JSON_NEXT();
87         JSON_STR(uid);       JSON_END();
88
89         usleep(500000);
90     }
91
92     status = ReaderClose();
93     fprintf(stderr, "ReaderClose: %s\n", UFR_Status2String(status));
94
95     return NULL;
96 }
97
98 int main(void)
99 {
100     pthread_t t;
101
102     signal(SIGINT, signal_exit);
103     signal(SIGTERM, signal_exit);
104
105     if (pthread_create(&t, NULL, read_cards, NULL)) {
106         perror("pthread_create");
107         return -1;
108     }
109
110     pthread_join(t, NULL);
111
112     return 0;
113 }