You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.ino 7.2KB

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