]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/Magstripe/Magstripe.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / Magstripe / Magstripe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
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.
21
22 The author disclaims 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
29 this software.
30 */
31
32 /** \file
33 *
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.
36 */
37
38 #include "Magstripe.h"
39
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.
42 */
43 static BitBuffer_t TrackDataBuffers[TOTAL_TRACKS];
44
45 /** Pointer to the current track buffer being sent to the host. */
46 static BitBuffer_t* CurrentTrackBuffer = &TrackDataBuffers[TOTAL_TRACKS];
47
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)];
50
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.
54 */
55 USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
56 {
57 .Config =
58 {
59 .InterfaceNumber = INTERFACE_ID_Keyboard,
60 .ReportINEndpoint =
61 {
62 .Address = KEYBOARD_EPADDR,
63 .Size = KEYBOARD_EPSIZE,
64 .Banks = 1,
65 },
66 .PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
67 .PrevReportINBufferSize = sizeof(PrevKeyboardHIDReportBuffer),
68 },
69 };
70
71
72 /** Main program entry point. This routine contains the overall program flow, including initial
73 * setup of all components and the main program loop.
74 */
75 int main(void)
76 {
77 SetupHardware();
78
79 for (uint8_t Buffer = 0; Buffer < TOTAL_TRACKS; Buffer++)
80 BitBuffer_Init(&TrackDataBuffers[Buffer]);
81
82 GlobalInterruptEnable();
83
84 for (;;)
85 {
86 if (Magstripe_GetStatus() & MAG_CARDPRESENT)
87 ReadMagstripeData();
88
89 HID_Device_USBTask(&Keyboard_HID_Interface);
90 USB_USBTask();
91 }
92 }
93
94 /** Configures the board hardware and chip peripherals for the demo's functionality. */
95 void SetupHardware(void)
96 {
97 #if (ARCH == ARCH_AVR8)
98 /* Disable watchdog if enabled by bootloader/fuses */
99 MCUSR &= ~(1 << WDRF);
100 wdt_disable();
101
102 /* Disable clock division */
103 clock_prescale_set(clock_div_1);
104 #endif
105
106 /* Hardware Initialization */
107 Magstripe_Init();
108 USB_Init();
109 }
110
111 /** Determines if a card has been inserted, and if so reads in each track's contents into the bit buffers
112 * until they are read out to the host as a series of keyboard presses.
113 */
114 void ReadMagstripeData(void)
115 {
116 /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */
117 const struct
118 {
119 uint8_t ClockMask;
120 uint8_t DataMask;
121 } TrackInfo[] = {{MAG_T1_CLOCK, MAG_T1_DATA},
122 {MAG_T2_CLOCK, MAG_T2_DATA},
123 {MAG_T3_CLOCK, MAG_T3_DATA}};
124
125 uint8_t Magstripe_Prev = 0;
126 uint8_t Magstripe_LCL = Magstripe_GetStatus();
127
128 while (Magstripe_LCL & MAG_CARDPRESENT)
129 {
130 for (uint8_t Track = 0; Track < TOTAL_TRACKS; Track++)
131 {
132 bool DataPinLevel = ((Magstripe_LCL & TrackInfo[Track].DataMask) != 0);
133 bool ClockPinLevel = ((Magstripe_LCL & TrackInfo[Track].ClockMask) != 0);
134 bool ClockLevelChanged = (((Magstripe_LCL ^ Magstripe_Prev) & TrackInfo[Track].ClockMask) != 0);
135
136 /* Sample data on rising clock edges from the card reader */
137 if (ClockPinLevel && ClockLevelChanged)
138 BitBuffer_StoreNextBit(&TrackDataBuffers[Track], DataPinLevel);
139 }
140
141 Magstripe_Prev = Magstripe_LCL;
142 Magstripe_LCL = Magstripe_GetStatus();
143 }
144
145 CurrentTrackBuffer = &TrackDataBuffers[0];
146 }
147
148 /** Event handler for the library USB Configuration Changed event. */
149 void EVENT_USB_Device_ConfigurationChanged(void)
150 {
151 HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
152
153 USB_Device_EnableSOFEvents();
154 }
155
156 /** Event handler for the library USB Control Request reception event. */
157 void EVENT_USB_Device_ControlRequest(void)
158 {
159 HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
160 }
161
162 /** Event handler for the USB device Start Of Frame event. */
163 void EVENT_USB_Device_StartOfFrame(void)
164 {
165 HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
166 }
167
168 /** HID class driver callback function for the creation of HID reports to the host.
169 *
170 * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
171 * \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
172 * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
173 * \param[out] ReportData Pointer to a buffer where the created report should be stored
174 * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
175 *
176 * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
177 */
178 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
179 uint8_t* const ReportID,
180 const uint8_t ReportType,
181 void* ReportData,
182 uint16_t* const ReportSize)
183 {
184 USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
185
186 static bool IsKeyReleaseReport;
187
188 /* Key reports must be interleaved with key release reports, or repeated keys will be ignored */
189 IsKeyReleaseReport = !IsKeyReleaseReport;
190
191 if ((IsKeyReleaseReport) || (CurrentTrackBuffer == &TrackDataBuffers[TOTAL_TRACKS]))
192 {
193 /* No more data to send, or key release report between key presses */
194 KeyboardReport->KeyCode[0] = KEY_NONE;
195 }
196 else if (!(CurrentTrackBuffer->Elements))
197 {
198 /* End of current track, send an enter press and change to the next track's buffer */
199 KeyboardReport->KeyCode[0] = KEY_ENTER;
200 CurrentTrackBuffer++;
201 }
202 else
203 {
204 /* Still data in the current track; convert next bit to a 1 or 0 keypress */
205 KeyboardReport->KeyCode[0] = BitBuffer_GetNextBit(CurrentTrackBuffer) ? KEY_1 : KEY_0;
206 }
207
208 *ReportSize = sizeof(USB_KeyboardReport_Data_t);
209 return false;
210 }
211
212 /** HID Class driver callback function for the processing of a received HID report from the host.
213 *
214 * \param[in] HIDInterfaceInfo Pointer to the HID interface structure for the HID interface being referenced
215 * \param[in] ReportID Report ID of the received report from the host
216 * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
217 * \param[in] ReportData Pointer to the report buffer where the received report is stored
218 * \param[in] ReportSize Size in bytes of the report received from the host
219 */
220 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
221 const uint8_t ReportID,
222 const uint8_t ReportType,
223 const void* ReportData,
224 const uint16_t ReportSize)
225 {
226 // Ignore keyboard LED reports from the host, but still need to declare the callback routine
227 }
228
Imprint / Impressum