Browse Source

echo response

develop
Robin Thoni 7 years ago
parent
commit
75fe08bef2
1 changed files with 55 additions and 190 deletions
  1. 55
    190
      main.ino

+ 55
- 190
main.ino View File

@@ -6,8 +6,8 @@
6 6
 #define STATUS_LED 13
7 7
 
8 8
 #define READER_VCC
9
-#define READER_RST 4
10
-#define READER_CLK 3
9
+#define READER_RST 3
10
+#define READER_CLK
11 11
 #define READER_GND
12 12
 #define READER_VPP
13 13
 #define READER_IO  2
@@ -24,12 +24,9 @@ SmartCardReader cardReader(CARD_IO, CARD_RST, CARD_VCC, CARD_PRE, CARD_CLK, fals
24 24
 uint8_t readerAtr[MAX_ATR_BYTES];
25 25
 int readerAtrSize = 0;
26 26
 
27
-//SoftwareSerial8e1 emulatorSerial(READER_IO, 5);
28
-bool isResetUp;
29
-int baudRate = -1;
30
-int emulatorAtrEtu = -1;
31
-bool emulatorIoData = false;
32
-#ifndef digitalPinToInterrupt
27
+int emulatorAtrBaudRate = -1;
28
+int emulatorCommandBaudRate = -1;
29
+
33 30
 int digitalPinToInterrupt(int pin)
34 31
 {
35 32
     if (pin == 2)
@@ -43,124 +40,41 @@ int digitalPinToInterrupt(int pin)
43 40
     return -1;
44 41
 }
45 42
 
46
-#endif
47
-
48
-void emulatorComputeEtu()
43
+void emulatorComputeBaudRate()
49 44
 {
50
-    baudRate = 9600;
51
-    emulatorAtrEtu = 1000000 / baudRate;
45
+    emulatorAtrBaudRate = 9600;
46
+    emulatorCommandBaudRate = 13171;
52 47
 }
53 48
 
54
-void emulatorSendByte(char byte, int etu)
49
+void emulatorSend(const uint8_t* atr, size_t size, int baudRate)
55 50
 {
56
-    unsigned long nextBitTime = micros() + (2 * etu);
57
-    digitalWrite(READER_IO, HIGH);
58
-    int bitNumber = 0;
59
-    int parity = 0;
60
-    while (bitNumber < 11)
61
-    {
62
-        if (nextBitTime <= micros())
63
-        {
64
-            if (bitNumber == 0)
65
-            {
66
-                digitalWrite(READER_IO, LOW);
67
-            }
68
-            else if (bitNumber < 9)
69
-            {
70
-                int a = bitRead(byte, bitNumber - 1);
71
-                parity += a;
72
-                digitalWrite(READER_IO, a);
73
-            }
74
-            else if (bitNumber == 9)
75
-            {
76
-                digitalWrite(READER_IO, parity % 2);
77
-            }
78
-            else if (bitNumber == 10)
79
-            {
80
-                digitalWrite(READER_IO, HIGH);
81
-                break;
82
-            }
83
-            nextBitTime += etu;
84
-            ++bitNumber;
85
-        }
86
-    }
51
+    SoftwareSerial8e1 emulatorSerial(5, READER_IO);
52
+    emulatorSerial.begin(baudRate);
53
+    emulatorSerial.write(atr, size);
54
+    emulatorSerial.end();
87 55
 }
88 56
 
89
-void emulatorSend(const uint8_t* atr, size_t size, int etu)
57
+bool emulatorRead(uint8_t* data, size_t* size, unsigned long timeout, int baudRate)
90 58
 {
91
-    pinMode(5, OUTPUT);
92
-    digitalWrite(5, HIGH);
93
-    pinMode(READER_IO, OUTPUT);
94
-    for (int i = 0; i < size; ++i)
59
+    unsigned long end = micros() + timeout;
60
+    SoftwareSerial8e1 emulatorSerial(READER_IO, 5);
61
+    emulatorSerial.begin(baudRate);
62
+    while (emulatorSerial.available() < 4 && (timeout == 0 || end >= micros()));
63
+    if (timeout != 0 && end < micros())
95 64
     {
96
-        emulatorSendByte(atr[i], etu);
97
-        delayMicroseconds(4*etu);
65
+        return false;
98 66
     }
99
-    digitalWrite(READER_IO, LOW);
100
-    pinMode(READER_IO, INPUT);
101
-    digitalWrite(5, LOW);
102
-}
103
-
104
-uint8_t emulatorReadByte(int etu)
105
-{
106
-//    while (digitalRead(READER_IO));
107
-
108
-    unsigned long nextBitTime = micros() + (3 * etu / 2);
109
-    uint8_t byte = 0;
110
-    int bitNumber = 0;
111
-    int parity = 0;
112
-    while (bitNumber < 10)
67
+    int available = 0;
68
+    do
113 69
     {
114
-        if (nextBitTime <= micros())
115
-        {
116
-            if (bitNumber < 8)
117
-            {
118
-                int bit = digitalRead(READER_IO);
119
-                bitWrite(byte, bitNumber, bit);
120
-            }
121
-            else if (bitNumber == 8)
122
-            {
123
-                int parity = digitalRead(READER_IO);
124
-            }
125
-            else if (bitNumber == 9)
126
-            {
127
-                break;
128
-            }
129
-            nextBitTime += etu;
130
-            ++bitNumber;
131
-        }
132
-    }
133
-    return byte;
134
-}
135
-
136
-void emulatorReadCommand(int etu)
137
-{
138
-    pinMode(READER_IO, INPUT);
139
-
140
-    uint8_t cla = emulatorReadByte(etu);
141
-    uint8_t ins = emulatorReadByte(etu);
142
-    uint8_t p1 = emulatorReadByte(etu);
143
-    uint8_t p2 = emulatorReadByte(etu);
144
-    Serial.print((int)cla);
145
-    Serial.print(' ');
146
-    Serial.print((int)ins);
147
-    Serial.print(' ');
148
-    Serial.print((int)p1);
149
-    Serial.print(' ');
150
-    Serial.println((int)p2);
151
-
152
-//    pinMode(READER_IO, OUTPUT);
153
-}
154
-
155
-void emulatorSetupIoInterrupt()
156
-{
157
-    attachInterrupt(digitalPinToInterrupt(READER_IO), emulatorOnIoInterrupt, FALLING);
158
-}
159
-
160
-void emulatorOnIoInterrupt()
161
-{
162
-    detachInterrupt(digitalPinToInterrupt(READER_IO));
163
-    emulatorIoData = true;
70
+        available = emulatorSerial.available();
71
+        delayMicroseconds(2 * 1000000 / baudRate);
72
+    } while (available != emulatorSerial.available());
73
+    *size = *size > available ? available : *size;
74
+    emulatorSerial.readBytes((char*)data, *size);
75
+
76
+    emulatorSerial.end();
77
+    return true;
164 78
 }
165 79
 
166 80
 void readerResetAtr()
@@ -172,11 +86,17 @@ void readerResetAtr()
172 86
     readerAtrSize = 0;
173 87
 }
174 88
 
89
+void emulatorOnReset()
90
+{
91
+    Serial.println("Reset");
92
+    emulatorComputeBaudRate();
93
+    emulatorSend(readerAtr, readerAtrSize, emulatorAtrBaudRate);
94
+}
95
+
175 96
 void setup() {
176 97
     Serial.begin(9600);
177 98
     Serial.println("Initializing...");
178 99
 
179
-    pinMode(READER_CLK, INPUT);
180 100
     pinMode(READER_RST, INPUT);
181 101
     pinMode(READER_IO, OUTPUT);
182 102
     digitalWrite(READER_IO, HIGH);
@@ -184,7 +104,7 @@ void setup() {
184 104
     pinMode(STATUS_LED, OUTPUT);
185 105
     digitalWrite(STATUS_LED, LOW);
186 106
 
187
-    isResetUp = digitalRead(READER_RST);
107
+    attachInterrupt(digitalPinToInterrupt(READER_RST), emulatorOnReset, RISING);
188 108
 
189 109
     Serial.println("Initialized");
190 110
 
@@ -193,14 +113,6 @@ void setup() {
193 113
 
194 114
 void loop() {
195 115
 
196
-//    if (emulatorIoData)
197
-//    {
198
-//        emulatorIoData = false;
199
-//        uint8_t b = emulatorReadCommand(12171);
200
-//        emulatorSetupIoInterrupt();
201
-//        Serial.println()
202
-//    }
203
-
204 116
     if (readerAtrSize == 0)
205 117
     {
206 118
         if (cardReader.cardInserted())
@@ -213,79 +125,32 @@ void loop() {
213 125
     }
214 126
     else
215 127
     {
216
-        int resetStatus = digitalRead(READER_RST);
217 128
         if (!cardReader.cardInserted())
218 129
         {
219 130
             readerResetAtr();
220 131
             digitalWrite(STATUS_LED, LOW);
221 132
         }
222
-        else if (!isResetUp && resetStatus)
133
+        else
223 134
         {
224
-            Serial.println("Reset");
225
-            emulatorComputeEtu();
226
-            emulatorSend(readerAtr, readerAtrSize, emulatorAtrEtu);
227
-
228
-            int baudRate = 12171;
229
-            int etu = 1000000 / baudRate;
230
-            size_t dataSize = 0;
231 135
             uint8_t data[42];
232
-
233
-            {
234
-                SoftwareSerial8e1 emulatorSerial(READER_IO, 5);
235
-                emulatorSerial.begin(baudRate);
236
-                while (emulatorSerial.available() < 8);
237
-                dataSize = emulatorSerial.available();
238
-                emulatorSerial.readBytes((char *) data, dataSize);
239
-                emulatorSerial.end();
240
-            }
241
-
242
-            delayMicroseconds(1000);
243
-            {
244
-                SoftwareSerial8e1 emulatorSerial(5, READER_IO);
245
-                emulatorSerial.begin(baudRate);
246
-                const uint8_t resp[] = {0x90, 0x00};
247
-                emulatorSerial.write(resp, sizeof(resp));
248
-                emulatorSerial.end();
249
-            }
250
-
251
-
252
-            for (int i = 0; i < dataSize; ++i)
136
+            size_t dataSize = sizeof(data) - 2;
137
+            if (emulatorRead(data, &dataSize, 100000, emulatorCommandBaudRate))
253 138
             {
254
-                Serial.print((int) data[i]);
255
-                Serial.print(' ');
139
+                delayMicroseconds(2300);
140
+
141
+//                data[dataSize++] = 0x6d;
142
+//                data[dataSize++] = 0x00;
143
+//                emulatorSend(data, dataSize, emulatorCommandBaudRate);
144
+                const uint8_t resp[] = {data[0], data[dataSize - 1]};
145
+                emulatorSend(resp, sizeof(resp), emulatorCommandBaudRate);
146
+
147
+                for (int i = 0; i < dataSize; ++i)
148
+                {
149
+                    Serial.print((int) data[i]);
150
+                    Serial.print(' ');
151
+                }
152
+                Serial.println();
256 153
             }
257
-            Serial.println();
258
-//            emulatorSend(resp, sizeof(resp), etu);
259
-//            emulatorSetupIoInterrupt();
260
-//                for (size_t i = 0; i < readerAtrSize; ++i)
261
-//                {
262
-//                    emulatorSerial.write(readerAtr[i]);
263
-//                }
264
-//                while (1)
265
-//                {
266
-////                    delayMicroseconds(10000);
267
-//                    bool has = false;
268
-//                    while (emulatorSerial.available())
269
-//                    {
270
-//                        Serial.print((int) emulatorSerial.read());
271
-//                        Serial.print(' ');
272
-//                        has = true;
273
-//                    }
274
-//                    if (has)
275
-//                    {
276
-//                        Serial.println();
277
-//                        const uint8_t resp[] = {0x90, 0x00};
278
-//                        emulatorSendAtr(resp, sizeof(resp), 12171);
279
-//                        emulatorSerial.begin(12171);
280
-////                        emulatorSerial.write((uint8_t)0x90);
281
-////                        emulatorSerial.write((uint8_t)0x00);
282
-//                    }
283
-//                }
284
-//            while (true)
285
-//            {
286
-//                emulatorReadCommand(12171);
287
-//            }
288 154
         }
289
-        isResetUp = resetStatus;
290 155
     }
291 156
 }

Loading…
Cancel
Save