3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2010  Denver Gingerich (denver [at] ossguy [dot] com) 
  11   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  13   Permission to use, copy, modify, distribute, and sell this 
  14   software and its documentation for any purpose is hereby granted 
  15   without fee, provided that the above copyright notice appear in 
  16   all copies and that both that the copyright notice and this 
  17   permission notice and warranty disclaimer appear in supporting 
  18   documentation, and that the name of the author not be used in 
  19   advertising or publicity pertaining to distribution of the 
  20   software without specific, written prior permission. 
  22   The author disclaim all warranties with regard to this 
  23   software, including all implied warranties of merchantability 
  24   and fitness.  In no event shall the author be liable for any 
  25   special, indirect or consequential damages or any damages 
  26   whatsoever resulting from loss of use, data or profits, whether 
  27   in an action of contract, negligence or other tortious action, 
  28   arising out of or in connection with the use or performance of 
  34  *  Main source file for the MagStripe reader program. This file contains the main tasks of 
  35  *  the project and is responsible for the initial application hardware configuration. 
  38 #include "Magstripe.h" 
  40 /** Bit buffers to hold the read bits for each of the three magnetic card tracks before they are transmitted 
  41  *  to the host as keyboard presses. 
  43 static BitBuffer_t TrackDataBuffers
[TOTAL_TRACKS
]; 
  45 /** Pointer to the current track buffer being sent to the host. */ 
  46 static BitBuffer_t
* CurrentTrackBuffer 
= &TrackDataBuffers
[TOTAL_TRACKS
]; 
  48 /** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */ 
  49 static uint8_t PrevKeyboardHIDReportBuffer
[sizeof(USB_KeyboardReport_Data_t
)]; 
  51 /** LUFA HID Class driver interface configuration and state information. This structure is 
  52  *  passed to all HID Class driver functions, so that multiple instances of the same class 
  53  *  within a device can be differentiated from one another. 
  55 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface 
= 
  61                                 .ReportINEndpointNumber     
= KEYBOARD_EPNUM
, 
  62                                 .ReportINEndpointSize       
= KEYBOARD_EPSIZE
, 
  63                                 .ReportINEndpointDoubleBank 
= KEYBOARD_EPSIZE
, 
  65                                 .PrevReportINBuffer         
= PrevKeyboardHIDReportBuffer
, 
  66                                 .PrevReportINBufferSize     
= sizeof(PrevKeyboardHIDReportBuffer
), 
  71 /** Main program entry point. This routine contains the overall program flow, including initial 
  72  *  setup of all components and the main program loop. 
  78         for (uint8_t Buffer 
= 0; Buffer 
< TOTAL_TRACKS
; Buffer
++) 
  79           BitBuffer_Init(&TrackDataBuffers
[Buffer
]); 
  85                 if (Magstripe_GetStatus() & MAG_CARDPRESENT
) 
  88                 HID_Device_USBTask(&Keyboard_HID_Interface
); 
  93 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  94 void SetupHardware(void) 
  96         /* Disable watchdog if enabled by bootloader/fuses */ 
  97         MCUSR 
&= ~(1 << WDRF
); 
 100         /* Disable clock division */ 
 101         clock_prescale_set(clock_div_1
); 
 103         /* Hardware Initialization */ 
 108 /** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers 
 109  *  until they are read out to the host as a series of keyboard presses. 
 111 void ReadMagstripeData(void) 
 113         /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */ 
 118         } TrackInfo
[] = {{MAG_T1_CLOCK
, MAG_T1_DATA
}, 
 119                          {MAG_T2_CLOCK
, MAG_T2_DATA
}, 
 120                          {MAG_T3_CLOCK
, MAG_T3_DATA
}}; 
 122         uint8_t Magstripe_Prev 
= 0; 
 123         uint8_t Magstripe_LCL  
= Magstripe_GetStatus(); 
 125         while (Magstripe_LCL 
& MAG_CARDPRESENT
) 
 127                 for (uint8_t Track 
= 0; Track 
< TOTAL_TRACKS
; Track
++) 
 129                         bool DataPinLevel      
= ((Magstripe_LCL 
& TrackInfo
[Track
].DataMask
) != 0); 
 130                         bool ClockPinLevel     
= ((Magstripe_LCL 
& TrackInfo
[Track
].ClockMask
) != 0); 
 131                         bool ClockLevelChanged 
= (((Magstripe_LCL 
^ Magstripe_Prev
) & TrackInfo
[Track
].ClockMask
) != 0); 
 133                         /* Sample data on rising clock edges from the card reader */ 
 134                         if (ClockPinLevel 
&& ClockLevelChanged
) 
 135                           BitBuffer_StoreNextBit(&TrackDataBuffers
[Track
], DataPinLevel
); 
 138                 Magstripe_Prev 
= Magstripe_LCL
; 
 139                 Magstripe_LCL  
= Magstripe_GetStatus(); 
 142         CurrentTrackBuffer 
= &TrackDataBuffers
[0]; 
 145 /** Event handler for the library USB Configuration Changed event. */ 
 146 void EVENT_USB_Device_ConfigurationChanged(void) 
 148         HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface
); 
 150         USB_Device_EnableSOFEvents(); 
 153 /** Event handler for the library USB Control Request reception event. */ 
 154 void EVENT_USB_Device_ControlRequest(void) 
 156         HID_Device_ProcessControlRequest(&Keyboard_HID_Interface
); 
 159 /** Event handler for the USB device Start Of Frame event. */ 
 160 void EVENT_USB_Device_StartOfFrame(void) 
 162         HID_Device_MillisecondElapsed(&Keyboard_HID_Interface
); 
 165 /** HID class driver callback function for the creation of HID reports to the host. 
 167  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced 
 168  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID 
 169  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature 
 170  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored 
 171  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent) 
 173  *  \return Boolean true to force the sending of the report, false to let the library determine if it needs to be sent 
 175 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 176                                          uint8_t* const ReportID
, 
 177                                          const uint8_t ReportType
, 
 179                                          uint16_t* const ReportSize
) 
 181         USB_KeyboardReport_Data_t
* KeyboardReport 
= (USB_KeyboardReport_Data_t
*)ReportData
; 
 183         static bool IsKeyReleaseReport
; 
 185         /* Key reports must be interleaved with key release reports, or repeated keys will be ignored */ 
 186         IsKeyReleaseReport 
= !IsKeyReleaseReport
; 
 188         if ((IsKeyReleaseReport
) || (CurrentTrackBuffer 
== &TrackDataBuffers
[TOTAL_TRACKS
])) 
 190                 /* No more data to send, or key release report between key presses */ 
 191                 KeyboardReport
->KeyCode
[0] = KEY_NONE
; 
 193         else if (!(CurrentTrackBuffer
->Elements
)) 
 195                 /* End of current track, send an enter press and change to the next track's buffer */ 
 196                 KeyboardReport
->KeyCode
[0] = KEY_ENTER
; 
 197                 CurrentTrackBuffer
++; 
 201                 /* Still data in the current track; convert next bit to a 1 or 0 keypress */ 
 202                 KeyboardReport
->KeyCode
[0] = BitBuffer_GetNextBit(CurrentTrackBuffer
) ? KEY_1 
: KEY_0
; 
 205         *ReportSize 
= sizeof(USB_KeyboardReport_Data_t
); 
 209 /** HID Class driver callback function for the processing of a received HID report from the host. 
 211  *  \param[in] HIDInterfaceInfo  Pointer to the HID interface structure for the HID interface being referenced 
 212  *  \param[in] ReportID          Report ID of the received report from the host 
 213  *  \param[in] ReportType        The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature 
 214  *  \param[in] ReportData        Pointer to the report buffer where the received report is stored 
 215  *  \param[in] ReportSize        Size in bytes of the report received from the host 
 217 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t
* const HIDInterfaceInfo
, 
 218                                           const uint8_t ReportID
, 
 219                                           const uint8_t ReportType
, 
 220                                           const void* ReportData
, 
 221                                           const uint16_t ReportSize
) 
 223         // Ignore keyboard LED reports from the host, but still need to declare the callback routine