Magstripe.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  9. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** \file
  28. *
  29. * Main source file for the MagStripe reader program. This file contains the main tasks of
  30. * the project and is responsible for the initial application hardware configuration.
  31. */
  32. #include "Magstripe.h"
  33. /** Bit buffers to hold the read bits for each of the three magnetic card tracks before they are transmitted
  34. * to the host as keyboard presses.
  35. */
  36. static BitBuffer_t TrackDataBuffers[TOTAL_TRACKS];
  37. /** Pointer to the current track buffer being sent to the host. */
  38. static BitBuffer_t* CurrentTrackBuffer = &TrackDataBuffers[TOTAL_TRACKS];
  39. /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
  40. static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
  41. /** LUFA HID Class driver interface configuration and state information. This structure is
  42. * passed to all HID Class driver functions, so that multiple instances of the same class
  43. * within a device can be differentiated from one another.
  44. */
  45. USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
  46. {
  47. .Config =
  48. {
  49. .InterfaceNumber = INTERFACE_ID_Keyboard,
  50. .ReportINEndpoint =
  51. {
  52. .Address = KEYBOARD_EPADDR,
  53. .Size = KEYBOARD_EPSIZE,
  54. .Banks = 1,
  55. },
  56. .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
  57. .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
  58. },
  59. };
  60. /** Main program entry point. This routine contains the overall program flow, including initial
  61. * setup of all components and the main program loop.
  62. */
  63. int main(void)
  64. {
  65. SetupHardware();
  66. for (uint8_t Buffer = 0; Buffer < TOTAL_TRACKS; Buffer++)
  67. BitBuffer_Init(&TrackDataBuffers[Buffer]);
  68. GlobalInterruptEnable();
  69. for (;;)
  70. {
  71. if (Magstripe_GetStatus() & MAG_CARDPRESENT)
  72. ReadMagstripeData();
  73. HID_Device_USBTask(&Keyboard_HID_Interface);
  74. USB_USBTask();
  75. }
  76. }
  77. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  78. void SetupHardware(void)
  79. {
  80. #if (ARCH == ARCH_AVR8)
  81. /* Disable watchdog if enabled by bootloader/fuses */
  82. MCUSR &= ~(1 << WDRF);
  83. wdt_disable();
  84. /* Disable clock division */
  85. clock_prescale_set(clock_div_1);
  86. #endif
  87. /* Hardware Initialization */
  88. Magstripe_Init();
  89. USB_Init();
  90. }
  91. /** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers
  92. * until they are read out to the host as a series of keyboard presses.
  93. */
  94. void ReadMagstripeData(void)
  95. {
  96. /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
  97. const struct
  98. {
  99. uint8_t ClockMask;
  100. uint8_t DataMask;
  101. } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
  102. {MAG_T2_CLOCK, MAG_T2_DATA},
  103. {MAG_T3_CLOCK, MAG_T3_DATA}};
  104. uint8_t Magstripe_Prev = 0;
  105. uint8_t Magstripe_LCL = Magstripe_GetStatus();
  106. while (Magstripe_LCL & MAG_CARDPRESENT)
  107. {
  108. for (uint8_t Track = 0; Track < TOTAL_TRACKS; Track++)
  109. {
  110. bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
  111. bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
  112. bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
  113. /* Sample data on rising clock edges from the card reader */
  114. if (ClockPinLevel && ClockLevelChanged)
  115. BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
  116. }
  117. Magstripe_Prev = Magstripe_LCL;
  118. Magstripe_LCL = Magstripe_GetStatus();
  119. }
  120. CurrentTrackBuffer = &TrackDataBuffers[0];
  121. }
  122. /** Event handler for the library USB Configuration Changed event. */
  123. void EVENT_USB_Device_ConfigurationChanged(void)
  124. {
  125. HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
  126. USB_Device_EnableSOFEvents();
  127. }
  128. /** Event handler for the library USB Control Request reception event. */
  129. void EVENT_USB_Device_ControlRequest(void)
  130. {
  131. HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
  132. }
  133. /** Event handler for the USB device Start Of Frame event. */
  134. void EVENT_USB_Device_StartOfFrame(void)
  135. {
  136. HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
  137. }
  138. /** HID class driver callback function for the creation of HID reports to the host.
  139. *
  140. * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
  141. * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
  142. * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
  143. * \param[out] ReportData Pointer to a buffer where the created report should be stored
  144. * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
  145. *
  146. * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
  147. */
  148. bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  149. uint8_t* const ReportID,
  150. const uint8_t ReportType,
  151. void* ReportData,
  152. uint16_t* const ReportSize)
  153. {
  154. USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
  155. static bool IsKeyReleaseReport;
  156. /* Key reports must be interleaved with key release reports, or repeated keys will be ignored */
  157. IsKeyReleaseReport = !IsKeyReleaseReport;
  158. if ((IsKeyReleaseReport) || (CurrentTrackBuffer == &TrackDataBuffers[TOTAL_TRACKS]))
  159. {
  160. /* No more data to send, or key release report between key presses */
  161. KeyboardReport->KeyCode[0] = KEY_NONE;
  162. }
  163. else if (!(CurrentTrackBuffer->Elements))
  164. {
  165. /* End of current track, send an enter press and change to the next track's buffer */
  166. KeyboardReport->KeyCode[0] = KEY_ENTER;
  167. CurrentTrackBuffer++;
  168. }
  169. else
  170. {
  171. /* Still data in the current track; convert next bit to a 1 or 0 keypress */
  172. KeyboardReport->KeyCode[0] = BitBuffer_GetNextBit(CurrentTrackBuffer) ? KEY_1 : KEY_0;
  173. }
  174. *ReportSize = sizeof(USB_KeyboardReport_Data_t);
  175. return false;
  176. }
  177. /** HID Class driver callback function for the processing of a received HID report from the host.
  178. *
  179. * \param[in] HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
  180. * \param[in] ReportID Report ID of the received report from the host
  181. * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
  182. * \param[in] ReportData Pointer to the report buffer where the received report is stored
  183. * \param[in] ReportSize Size in bytes of the report received from the host
  184. */
  185. void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
  186. const uint8_t ReportID,
  187. const uint8_t ReportType,
  188. const void* ReportData,
  189. const uint16_t ReportSize)
  190. {
  191. // Ignore keyboard LED reports from the host, but still need to declare the callback routine
  192. }