]> git.karo-electronics.de Git - magstrip.git/blob - lmain.c
19ab4a3978b5c193d7581731b119aa11bd24d5f4
[magstrip.git] / lmain.c
1 /* Test application for Omron card reader
2  * Copyright (C) 2009 Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/select.h>
22
23 #include "uart.h"
24
25 #define DLE     0x10
26 #define STX     0x02
27 #define ETX     0x03
28 #define ENQ     0x05
29 #define ACK     0x06
30 #define NAK     0x15
31 #define EOT     0x04
32
33
34 void send_command(int fd, char *cmd, int len)
35 {
36 unsigned char bcc;
37 int pos;
38 char *sbuf = NULL;
39 unsigned int sbpos = 0;
40
41         sbuf = (char *)malloc(len + 5);
42         if (sbuf == NULL) {
43                 perror("malloc");
44                 return;
45         }
46
47         sbuf[sbpos++] = DLE;
48         sbuf[sbpos++] = STX;
49         bcc = 0;
50         pos = 0;
51         while (pos < len) {
52                 sbuf[sbpos++] = cmd[pos];
53                 bcc ^= cmd[pos];
54                 if (cmd[pos] == DLE)
55                         sbuf[sbpos++] = cmd[pos];
56                 pos++;
57         }
58         sbuf[sbpos++] = DLE;
59         sbuf[sbpos++] = ETX;
60         bcc ^= ETX;
61         sbuf[sbpos++] = bcc;
62
63         write_uart(sbuf, sbpos);
64         free(sbuf);
65 }
66
67 void print_help(void)
68 {
69         fprintf(stderr, "r              -       initial RESET, required after power-on\n");
70         fprintf(stderr, "i              -       C:2 insert permit request\n");
71         fprintf(stderr, "s              -       C10 C/R status request\n");
72         fprintf(stderr, "S              -       C11 sensor status\n");
73         fprintf(stderr, "1              -       Reads ISO #1 and sends data\n");
74         fprintf(stderr, "2              -       Reads ISO #2 and sends data\n");
75         fprintf(stderr, "3              -       Reads ISO #3 and sends data\n");
76         fprintf(stderr, "m              -       Sends the read data in memory by this command\n");
77         fprintf(stderr, "M              -       Sends multi-tracks data in the memory by this command and parameter\n");
78         fprintf(stderr, "q              -       close serial and quite\n");
79         fprintf(stderr, "h              -       this help\n");
80 }
81
82 int process_user_cmd(char *rxbuf, int len, int ufd)
83 {
84 int in_cmd = 0;
85
86         switch (rxbuf[0]) {
87                 case 'i':
88                         fprintf(stderr, "Insert permit request... ");
89                         send_command(ufd, "C:2", 3);
90                         in_cmd = 1;
91                         break;
92                 case 'r':
93                         fprintf(stderr, "RESET request... ");
94                         send_command(ufd, "C00", 3);
95                         in_cmd = 1;
96                         break;
97                 case 'e':
98                         fprintf(stderr, "Eject request... ");
99                         send_command(ufd, "C30", 3);
100                         in_cmd = 1;
101                         break;
102                 case 's':
103                         fprintf(stderr, "Status request... ");
104                         send_command(ufd, "C10", 3);
105                         in_cmd = 1;
106                         break;
107                 case 'S':
108                         fprintf(stderr, "Status request... ");
109                         send_command(ufd, "C11", 3);
110                         in_cmd = 1;
111                         break;
112                 case '1':
113                         fprintf(stderr, "Status request... ");
114                         send_command(ufd, "C61", 3);
115                         in_cmd = 1;
116                         break;
117                 case '2':
118                         fprintf(stderr, "Status request... ");
119                         send_command(ufd, "C62", 3);
120                         in_cmd = 1;
121                         break;
122                 case '3':
123                         fprintf(stderr, "Status request... ");
124                         send_command(ufd, "C63", 3);
125                         in_cmd = 1;
126                         break;
127                 case 'h':
128                         print_help();
129                         in_cmd = 0;
130                         break;
131                 case 'q':
132                         fprintf(stderr, "Exiting...\n");
133                         in_cmd = -1;
134                         break;
135                 default:
136                         break;
137         }
138
139         return in_cmd;
140 }
141
142 void process_read_packet(char *rxbuf, int rxpos)
143 {
144 int bpos;
145
146         bpos = 2; // index into read buffer
147         if (rxbuf[bpos] == 'P') {
148                 fprintf(stderr, "positive, ");
149                 bpos++;
150                 fprintf(stderr, "cmd was '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
151                 bpos+=2;
152                 if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='0') {
153                         fprintf(stderr, "no card present, ");
154                 } else if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='1') {
155                         fprintf(stderr, "card at takeout, ");
156                 } else if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='2') {
157                         fprintf(stderr, "card present, ");
158                 } else
159                         fprintf(stderr, "stat is '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
160         } else if (rxbuf[bpos] == 'N') {
161                 fprintf(stderr, "negative, ");
162                 bpos++;
163                 fprintf(stderr, "cmd was '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
164                 bpos+=2;
165                 if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='0') {
166                         fprintf(stderr, "undefined command, ");
167                 } else if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='1') {
168                         fprintf(stderr, "command sequence error, ");
169                 } else if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='2') {
170                         fprintf(stderr, "command data error, ");
171                 } else if (rxbuf[bpos]=='0' && rxbuf[bpos+1]=='3') {
172                         fprintf(stderr, "write track setting error, ");
173                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='0') {
174                         fprintf(stderr, "SS read error, ");
175                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='1') {
176                         fprintf(stderr, "ES read error, ");
177                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='2') {
178                         fprintf(stderr, "VRC read error, ");
179                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='3') {
180                         fprintf(stderr, "LRC read error, ");
181                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='4') {
182                         fprintf(stderr, "No encode read error, ");
183                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='5') {
184                         fprintf(stderr, "No data read error, ");
185                 } else if (rxbuf[bpos]=='4' && rxbuf[bpos+1]=='6') {
186                         fprintf(stderr, "Jitter read error, ");
187                 } else
188                         fprintf(stderr, "stat is '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
189         }
190         bpos+=2;
191         if (bpos < rxpos-3)
192                 fprintf(stderr, "\ndata: ");
193         while (bpos < rxpos-3) {
194                 unsigned char c = rxbuf[bpos++];
195                 if (c > 31 || c < 128)
196                         fprintf(stderr, "%c", c);
197                 else
198                         fprintf(stderr, "0x%02x ", c);
199                 
200         }
201         fprintf(stderr, "\n");
202 }
203
204 int main(int argc, char **argv)
205 {
206 char *ser_dev;
207 char rxbuf[256];
208 int ufd, rxlen=0, rxpos=0, ret;
209 int in_cmd = 0;
210 fd_set rfds;
211 struct timeval tval;
212
213         if (argc > 1)
214                 ser_dev = argv[1];
215         else
216                 ser_dev = "/dev/ttyS0";
217         if (open_uart(ser_dev, B9600) != 0)
218                 return -1;
219         ufd = uart_get_fd();
220         // fprintf(stderr, "UART fd = %d\n", ufd);
221
222         FD_ZERO(&rfds);
223         FD_SET(ufd, &rfds);
224         FD_SET(0, &rfds);
225         tval.tv_sec = 0;
226         tval.tv_usec = 5000000;
227
228         fprintf(stderr, "OMRON MVFW card reader terminal\non serial %s\nEnter 'h' for help\n", ser_dev);
229         while (1) {
230                 ret = select((ufd+1), &rfds, NULL, NULL, &tval);
231                 if (ret == 0) {
232                         FD_ZERO(&rfds);
233                         FD_SET(ufd, &rfds);
234                         FD_SET(0, &rfds);
235                         tval.tv_sec = 0;
236                         tval.tv_usec = 5000000;
237                         if (rxpos > 0)
238                                 fprintf(stderr, "Select tmo but buffer not empty!\n");
239                         memset(rxbuf, 0, 256);
240                         rxpos = 0; // invalidate buffer
241                 } else if (ret > 0) {
242                         if (FD_ISSET(ufd, &rfds)) {
243                                 // fprintf(stderr, "**** select something to read ****\n");
244                                 rxlen = uart_read(rxbuf+rxpos, 255-rxpos);
245                                 rxpos += rxlen;
246                                 // fprintf(stderr, "  read %d bytes\n", rxlen);
247                                 if (rxpos == 2 && rxbuf[0] == DLE && rxbuf[1] == ACK && in_cmd == 1) {
248                                         fprintf(stderr, "got ACK requesting execute \n");
249                                         put_uart(DLE);
250                                         put_uart(ENQ);
251                                         in_cmd = 0;
252                                         memset(rxbuf, 0, 256);
253                                         rxpos = 0;
254                                 } else if (rxpos > 2 && rxbuf[rxpos-3]==DLE && rxbuf[rxpos-2]==ETX) {
255                                         // data is between DLE-STX and DLE-ETX-BCC
256                                         process_read_packet(rxbuf, rxpos);
257                                         memset(rxbuf, 0, 256);
258                                         rxpos = 0;
259                                 } else
260                                         fprintf(stderr, "buf not complete yet\n");
261                         }
262                         if (FD_ISSET(0, &rfds)) {
263                                 // fprintf(stderr, "**** keypress\n");
264                                 ret = read(0, rxbuf, 255);
265                                 if (ret > 0) {
266                                         in_cmd = process_user_cmd(rxbuf, ret, ufd);
267                                         if (in_cmd == -1) {
268                                                 close(ufd);
269                                                 exit(0);
270                                         }
271                                 }
272                         }
273                         FD_ZERO(&rfds);
274                         FD_SET(ufd, &rfds);
275                         FD_SET(0, &rfds);
276
277                         fprintf(stderr, "\n> ");
278                 } else {
279                         fprintf(stderr, "select error\n");
280                         break;
281                 }
282         }
283
284         close_uart();
285
286 return 0;
287 }