1 /* Test application for Omron card reader
2 * Copyright (C) 2009 Nils Faerber <nils.faerber@kernelconcepts.de>
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.
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.
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.
21 #include <sys/select.h>
34 void send_command(int fd, char *cmd, int len)
39 unsigned int sbpos = 0;
41 sbuf = (char *)malloc(len + 5);
52 sbuf[sbpos++] = cmd[pos];
55 sbuf[sbpos++] = cmd[pos];
63 write_uart(sbuf, sbpos);
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");
82 int process_user_cmd(char *rxbuf, int len, int ufd)
88 fprintf(stderr, "Insert permit request... ");
89 send_command(ufd, "C:2", 3);
93 fprintf(stderr, "RESET request... ");
94 send_command(ufd, "C00", 3);
98 fprintf(stderr, "Eject request... ");
99 send_command(ufd, "C30", 3);
103 fprintf(stderr, "Status request... ");
104 send_command(ufd, "C10", 3);
108 fprintf(stderr, "Status request... ");
109 send_command(ufd, "C11", 3);
113 fprintf(stderr, "Status request... ");
114 send_command(ufd, "C61", 3);
118 fprintf(stderr, "Status request... ");
119 send_command(ufd, "C62", 3);
123 fprintf(stderr, "Status request... ");
124 send_command(ufd, "C63", 3);
132 fprintf(stderr, "Exiting...\n");
142 void process_read_packet(char *rxbuf, int rxpos)
146 bpos = 2; // index into read buffer
147 if (rxbuf[bpos] == 'P') {
148 fprintf(stderr, "positive, ");
150 fprintf(stderr, "cmd was '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
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, ");
159 fprintf(stderr, "stat is '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
160 } else if (rxbuf[bpos] == 'N') {
161 fprintf(stderr, "negative, ");
163 fprintf(stderr, "cmd was '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
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, ");
188 fprintf(stderr, "stat is '%c%c', ", rxbuf[bpos], rxbuf[bpos+1]);
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);
198 fprintf(stderr, "0x%02x ", c);
201 fprintf(stderr, "\n");
204 int main(int argc, char **argv)
208 int ufd, rxlen=0, rxpos=0, ret;
216 ser_dev = "/dev/ttyS0";
217 if (open_uart(ser_dev, B9600) != 0)
220 // fprintf(stderr, "UART fd = %d\n", ufd);
226 tval.tv_usec = 5000000;
228 fprintf(stderr, "OMRON MVFW card reader terminal\non serial %s\nEnter 'h' for help\n", ser_dev);
230 ret = select((ufd+1), &rfds, NULL, NULL, &tval);
236 tval.tv_usec = 5000000;
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);
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");
252 memset(rxbuf, 0, 256);
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);
260 fprintf(stderr, "buf not complete yet\n");
262 if (FD_ISSET(0, &rfds)) {
263 // fprintf(stderr, "**** keypress\n");
264 ret = read(0, rxbuf, 255);
266 in_cmd = process_user_cmd(rxbuf, ret, ufd);
277 fprintf(stderr, "\n> ");
279 fprintf(stderr, "select error\n");