SmartCard reader. PC is interface through USB or TCPport. SmartCard is interfaced through UART@ 1MHz, DIV372

Dependencies:   EthernetNetIf mbed

Committer:
bcalin1984
Date:
Sun Feb 27 22:20:40 2011 +0000
Revision:
0:5bf6fcf71548

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcalin1984 0:5bf6fcf71548 1 /*
bcalin1984 0:5bf6fcf71548 2 Main file: encapsulates the glue that links USB-serial or TCP port to the SmartCardReader
bcalin1984 0:5bf6fcf71548 3
bcalin1984 0:5bf6fcf71548 4 Author: Calin Bira
bcalin1984 0:5bf6fcf71548 5 Date: 28.02.2011
bcalin1984 0:5bf6fcf71548 6 */
bcalin1984 0:5bf6fcf71548 7
bcalin1984 0:5bf6fcf71548 8 #include "mbed.h"
bcalin1984 0:5bf6fcf71548 9 #include "SmartCardReader.h"
bcalin1984 0:5bf6fcf71548 10 #include <string.h>
bcalin1984 0:5bf6fcf71548 11 #include "commons.h"
bcalin1984 0:5bf6fcf71548 12 #include "TcpServer.h"
bcalin1984 0:5bf6fcf71548 13
bcalin1984 0:5bf6fcf71548 14 Serial computer(USBTX, USBRX); // tx, rx
bcalin1984 0:5bf6fcf71548 15 Serial smartcard(p13, p14); // tx, rx
bcalin1984 0:5bf6fcf71548 16 DigitalOut reset(p20);
bcalin1984 0:5bf6fcf71548 17 DigitalOut vcc(p18);
bcalin1984 0:5bf6fcf71548 18 PwmOut clk(p21);//clock line
bcalin1984 0:5bf6fcf71548 19
bcalin1984 0:5bf6fcf71548 20 TcpServer TCPS(10779);
bcalin1984 0:5bf6fcf71548 21 #define SendToHost(x,y) \
bcalin1984 0:5bf6fcf71548 22 computer.printf(x,y);\
bcalin1984 0:5bf6fcf71548 23 TCPS.Tprintf(x,y)
bcalin1984 0:5bf6fcf71548 24
bcalin1984 0:5bf6fcf71548 25 SmartCardReader SCR(&smartcard, &vcc, &reset, &clk, "8E1");
bcalin1984 0:5bf6fcf71548 26
bcalin1984 0:5bf6fcf71548 27 static const int CommandBufferLen = 2*MAX_MSG_SIZE + 20;//two bytes per char, and extra 20 Bytes for other params
bcalin1984 0:5bf6fcf71548 28 static const int AnswerBufferLen = 2*MAX_MSG_SIZE + 20;//two bytes per char, and extra 20 Bytes for other params
bcalin1984 0:5bf6fcf71548 29 static char CommandBuffer[CommandBufferLen];
bcalin1984 0:5bf6fcf71548 30 static int CommandBufferIndex;
bcalin1984 0:5bf6fcf71548 31 static char AnswerBuffer[AnswerBufferLen];
bcalin1984 0:5bf6fcf71548 32 static char tmp[MAX_MSG_SIZE];
bcalin1984 0:5bf6fcf71548 33
bcalin1984 0:5bf6fcf71548 34 #define CMD_ERR_INVALID_FORMAT 0x0001
bcalin1984 0:5bf6fcf71548 35 #define CMD_ERR_OK 0x0000
bcalin1984 0:5bf6fcf71548 36 #define CMD_ERR_RECEIVE_TIMEOUT 0x00CA
bcalin1984 0:5bf6fcf71548 37
bcalin1984 0:5bf6fcf71548 38 void DecodeCommand(char* cmd, int len)
bcalin1984 0:5bf6fcf71548 39 {
bcalin1984 0:5bf6fcf71548 40 int errorCode = CMD_ERR_OK;
bcalin1984 0:5bf6fcf71548 41 char * pch;
bcalin1984 0:5bf6fcf71548 42 pch = strtok (cmd," ");
bcalin1984 0:5bf6fcf71548 43
bcalin1984 0:5bf6fcf71548 44 if (len < 6)
bcalin1984 0:5bf6fcf71548 45 {
bcalin1984 0:5bf6fcf71548 46 errorCode = CMD_ERR_INVALID_FORMAT;
bcalin1984 0:5bf6fcf71548 47 SendToHost("ERR %04x\r\n",errorCode);
bcalin1984 0:5bf6fcf71548 48 }
bcalin1984 0:5bf6fcf71548 49 else if (strncmp(pch,"ISCR",4)==0)
bcalin1984 0:5bf6fcf71548 50 {
bcalin1984 0:5bf6fcf71548 51 SCR.cold_reset();
bcalin1984 0:5bf6fcf71548 52 SendToHost("ISCR %04X\r\n",errorCode);
bcalin1984 0:5bf6fcf71548 53 }
bcalin1984 0:5bf6fcf71548 54 else if (strncmp(pch,"ISWR",4)==0)
bcalin1984 0:5bf6fcf71548 55 {
bcalin1984 0:5bf6fcf71548 56 SCR.warm_reset();
bcalin1984 0:5bf6fcf71548 57 SendToHost("ISWR %04X\r\n",errorCode);
bcalin1984 0:5bf6fcf71548 58 }
bcalin1984 0:5bf6fcf71548 59 else if (strncmp(pch,"ISSB",4)==0)
bcalin1984 0:5bf6fcf71548 60 {
bcalin1984 0:5bf6fcf71548 61 pch = strtok (NULL, " ");//size
bcalin1984 0:5bf6fcf71548 62 if (pch == NULL) {errorCode = CMD_ERR_INVALID_FORMAT; SendToHost("ERR %04X\r\n",errorCode); return;}
bcalin1984 0:5bf6fcf71548 63
bcalin1984 0:5bf6fcf71548 64 int len = atoi(pch);
bcalin1984 0:5bf6fcf71548 65 int len_copy = len;
bcalin1984 0:5bf6fcf71548 66 pch = strtok (NULL, " ");//the other chars
bcalin1984 0:5bf6fcf71548 67 if (pch == NULL) {errorCode = CMD_ERR_INVALID_FORMAT;SendToHost("ERR %04X\r\n",errorCode); return;}
bcalin1984 0:5bf6fcf71548 68
bcalin1984 0:5bf6fcf71548 69 char *src = tmp;
bcalin1984 0:5bf6fcf71548 70 while (len > 0)
bcalin1984 0:5bf6fcf71548 71 {
bcalin1984 0:5bf6fcf71548 72 sscanf(pch, "%2x", src++);
bcalin1984 0:5bf6fcf71548 73 pch += 2;
bcalin1984 0:5bf6fcf71548 74 len--;
bcalin1984 0:5bf6fcf71548 75 }
bcalin1984 0:5bf6fcf71548 76
bcalin1984 0:5bf6fcf71548 77 SCR.send(tmp,len_copy);
bcalin1984 0:5bf6fcf71548 78 SendToHost("ISSB %04X\r\n",errorCode);
bcalin1984 0:5bf6fcf71548 79 }
bcalin1984 0:5bf6fcf71548 80 else if (strncmp(pch,"ISRB",4)==0)
bcalin1984 0:5bf6fcf71548 81 {
bcalin1984 0:5bf6fcf71548 82 char *dest;
bcalin1984 0:5bf6fcf71548 83 char *src = tmp;
bcalin1984 0:5bf6fcf71548 84 pch = strtok (NULL, " ");
bcalin1984 0:5bf6fcf71548 85 int expectedLen = atoi(pch);
bcalin1984 0:5bf6fcf71548 86 int len = SCR.receive(tmp);
bcalin1984 0:5bf6fcf71548 87 if (expectedLen > len) errorCode = CMD_ERR_RECEIVE_TIMEOUT;
bcalin1984 0:5bf6fcf71548 88
bcalin1984 0:5bf6fcf71548 89 sprintf(AnswerBuffer,"ISRB %04X %d ",errorCode,len);
bcalin1984 0:5bf6fcf71548 90 dest = AnswerBuffer + strlen(AnswerBuffer);
bcalin1984 0:5bf6fcf71548 91 while (len > 0)
bcalin1984 0:5bf6fcf71548 92 {
bcalin1984 0:5bf6fcf71548 93 sprintf(dest,"%02X",*src);
bcalin1984 0:5bf6fcf71548 94 src++;dest+=2;len--;
bcalin1984 0:5bf6fcf71548 95 }
bcalin1984 0:5bf6fcf71548 96 *dest++ = '\r';
bcalin1984 0:5bf6fcf71548 97 *dest++ = '\n';
bcalin1984 0:5bf6fcf71548 98 *dest++ = '\0';
bcalin1984 0:5bf6fcf71548 99
bcalin1984 0:5bf6fcf71548 100 SendToHost("%s",AnswerBuffer);
bcalin1984 0:5bf6fcf71548 101 }
bcalin1984 0:5bf6fcf71548 102
bcalin1984 0:5bf6fcf71548 103 while (pch !=NULL)
bcalin1984 0:5bf6fcf71548 104 {
bcalin1984 0:5bf6fcf71548 105 pch = strtok (NULL, " ");
bcalin1984 0:5bf6fcf71548 106 }
bcalin1984 0:5bf6fcf71548 107 }
bcalin1984 0:5bf6fcf71548 108
bcalin1984 0:5bf6fcf71548 109 void AssembleCommand(char *buf, int len)
bcalin1984 0:5bf6fcf71548 110 {
bcalin1984 0:5bf6fcf71548 111 for (int i=0; i<len; i++)
bcalin1984 0:5bf6fcf71548 112 {
bcalin1984 0:5bf6fcf71548 113 if (CommandBufferIndex < CommandBufferLen)
bcalin1984 0:5bf6fcf71548 114 {
bcalin1984 0:5bf6fcf71548 115 CommandBuffer[CommandBufferIndex] = buf[i];
bcalin1984 0:5bf6fcf71548 116 if (CommandBuffer[CommandBufferIndex] == 10)
bcalin1984 0:5bf6fcf71548 117 {
bcalin1984 0:5bf6fcf71548 118 DecodeCommand(CommandBuffer, CommandBufferIndex);
bcalin1984 0:5bf6fcf71548 119 CommandBufferIndex = 0;
bcalin1984 0:5bf6fcf71548 120 }
bcalin1984 0:5bf6fcf71548 121 else CommandBufferIndex++;
bcalin1984 0:5bf6fcf71548 122 }
bcalin1984 0:5bf6fcf71548 123 else
bcalin1984 0:5bf6fcf71548 124 {
bcalin1984 0:5bf6fcf71548 125 CommandBufferIndex = 0;
bcalin1984 0:5bf6fcf71548 126 }
bcalin1984 0:5bf6fcf71548 127
bcalin1984 0:5bf6fcf71548 128 }
bcalin1984 0:5bf6fcf71548 129
bcalin1984 0:5bf6fcf71548 130
bcalin1984 0:5bf6fcf71548 131
bcalin1984 0:5bf6fcf71548 132 }
bcalin1984 0:5bf6fcf71548 133
bcalin1984 0:5bf6fcf71548 134
bcalin1984 0:5bf6fcf71548 135 //TcpServer TCPS(10779);
bcalin1984 0:5bf6fcf71548 136 int main()
bcalin1984 0:5bf6fcf71548 137 {
bcalin1984 0:5bf6fcf71548 138 char ch;
bcalin1984 0:5bf6fcf71548 139 SCR.cold_reset();
bcalin1984 0:5bf6fcf71548 140 TCPS.SetReceiveCallback(&AssembleCommand);
bcalin1984 0:5bf6fcf71548 141 CommandBufferIndex = 0;
bcalin1984 0:5bf6fcf71548 142
bcalin1984 0:5bf6fcf71548 143 while(1)
bcalin1984 0:5bf6fcf71548 144 {
bcalin1984 0:5bf6fcf71548 145 while (computer.readable()) //if there is data from pc
bcalin1984 0:5bf6fcf71548 146 {
bcalin1984 0:5bf6fcf71548 147 ch = computer.getc();
bcalin1984 0:5bf6fcf71548 148 AssembleCommand(&ch,1);
bcalin1984 0:5bf6fcf71548 149 }
bcalin1984 0:5bf6fcf71548 150
bcalin1984 0:5bf6fcf71548 151 SCR.doEvents();
bcalin1984 0:5bf6fcf71548 152 TCPS.doEvents();
bcalin1984 0:5bf6fcf71548 153 }
bcalin1984 0:5bf6fcf71548 154 }