Fix to previous patch (make CDCHost demo deallocate pipes when an invalid CDC interfa...
[pub/USBasp.git] / Projects / Magstripe / Magstripe.c
index aef7812..24172d8 100644 (file)
  \r
 #include "Magstripe.h"\r
 \r
-/* Project Tags, for reading out using the ButtLoad project */\r
-BUTTLOADTAG(ProjName,    "Magstripe Reader");\r
-BUTTLOADTAG(BuildTime,   __TIME__);\r
-BUTTLOADTAG(BuildDate,   __DATE__);\r
-BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);\r
-\r
 /* Scheduler Task List */\r
 TASK_LIST\r
 {\r
-       { Task: USB_USBTask          , TaskStatus: TASK_STOP },\r
-       { Task: USB_Keyboard_Report  , TaskStatus: TASK_STOP },\r
-       { Task: Magstripe_Read       , TaskStatus: TASK_STOP },\r
+       { .Task = USB_USBTask          , .TaskStatus = TASK_STOP },\r
+       { .Task = USB_Keyboard_Report  , .TaskStatus = TASK_STOP },\r
+       { .Task = Magstripe_Read       , .TaskStatus = TASK_STOP },\r
 };\r
 \r
 /* Global Variables */\r
@@ -78,7 +72,7 @@ BitBuffer_t Track2Data;
 /** Circular buffer to hold the read bits from track 3 of the inserted magnetic card. */\r
 BitBuffer_t Track3Data;\r
 \r
-/** Delay counter between sucessive key strokes. This is to prevent the OS from ignoring multiple keys in a short\r
+/** Delay counter between successive key strokes. This is to prevent the OS from ignoring multiple keys in a short\r
  *  period of time due to key repeats. Two milliseconds works for most OSes.\r
  */\r
 uint8_t KeyDelayRemaining;\r
@@ -161,104 +155,85 @@ EVENT_HANDLER(USB_ConfigurationChanged)
 EVENT_HANDLER(USB_UnhandledControlPacket)\r
 {\r
        /* Handle HID Class specific requests */\r
-       switch (bRequest)\r
+       switch (USB_ControlRequest.bRequest)\r
        {\r
                case REQ_GetReport:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
                                USB_KeyboardReport_Data_t KeyboardReportData;\r
 \r
                                /* Create the next keyboard report for transmission to the host */\r
                                GetNextReport(&KeyboardReportData);\r
 \r
-                               /* Ignore report type and ID number value */\r
-                               Endpoint_Discard_Word();\r
-                               \r
-                               /* Ignore unused Interface number value */\r
-                               Endpoint_Discard_Word();\r
-\r
-                               /* Read in the number of bytes in the report to send to the host */\r
-                               uint16_t wLength = Endpoint_Read_Word_LE();\r
-                               \r
-                               /* If trying to send more bytes than exist to the host, clamp the value at the report size */\r
-                               if (wLength > sizeof(KeyboardReportData))\r
-                                 wLength = sizeof(KeyboardReportData);\r
-\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
        \r
                                /* Write the report data to the control endpoint */\r
-                               Endpoint_Write_Control_Stream_LE(&KeyboardReportData, wLength);\r
+                               Endpoint_Write_Control_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));\r
                                \r
                                /* Finalize the stream transfer to send the last packet or clear the host abort */\r
-                               Endpoint_ClearSetupOUT();\r
+                               Endpoint_ClearOUT();\r
                        }\r
                \r
                        break;\r
                case REQ_GetProtocol:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Write the current protocol flag to the host */\r
                                Endpoint_Write_Byte(UsingReportProtocol);\r
                                \r
                                /* Send the flag to the host */\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearIN();\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
-                               Endpoint_ClearSetupOUT();\r
+                               while (!(Endpoint_IsOUTReceived()));\r
+                               Endpoint_ClearOUT();\r
                        }\r
                        \r
                        break;\r
                case REQ_SetProtocol:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               /* Read in the wValue parameter containing the new protocol mode */\r
-                               uint16_t wValue = Endpoint_Read_Word_LE();\r
-                               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
 \r
                                /* Set or clear the flag depending on what the host indicates that the current Protocol should be */\r
-                               UsingReportProtocol = (wValue != 0x0000);\r
+                               UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);\r
                                \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               while (!(Endpoint_IsINReady()));\r
+                               Endpoint_ClearIN();\r
                        }\r
                        \r
                        break;\r
                case REQ_SetIdle:\r
-                       if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {\r
-                               /* Read in the wValue parameter containing the idle period */\r
-                               uint16_t wValue = Endpoint_Read_Word_LE();\r
-                               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Get idle period in MSB */\r
-                               IdleCount = (wValue >> 8);\r
+                               IdleCount = (USB_ControlRequest.wValue >> 8);\r
                                \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupINReady()));\r
-                               Endpoint_ClearSetupIN();\r
+                               while (!(Endpoint_IsINReady()));\r
+                               Endpoint_ClearIN();\r
                        }\r
                        \r
                        break;\r
                case REQ_GetIdle:\r
-                       if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
                        {               \r
-                               Endpoint_ClearSetupReceived();\r
+                               Endpoint_ClearSETUP();\r
                                \r
                                /* Write the current idle duration to the host */\r
                                Endpoint_Write_Byte(IdleCount);\r
                                \r
                                /* Send the flag to the host */\r
-                               Endpoint_ClearSetupIN();\r
+                               Endpoint_ClearIN();\r
 \r
                                /* Acknowledge status stage */\r
-                               while (!(Endpoint_IsSetupOUTReceived()));\r
-                               Endpoint_ClearSetupOUT();\r
+                               while (!(Endpoint_IsOUTReceived()));\r
+                               Endpoint_ClearOUT();\r
                        }\r
 \r
                        break;\r
@@ -312,7 +287,7 @@ bool GetNextReport(USB_KeyboardReport_Data_t* ReportData)
                /* Set the flag indicating that a null report must eventually be sent to release all pressed keys */\r
                MustRelease = true;\r
 \r
-               /* Only send the next key on odd reports, so that they are interpersed with null reports to release keys */\r
+               /* Only send the next key on odd reports, so that they are interspersed with null reports to release keys */\r
                if (OddReport)\r
                {\r
                        /* Set the report key code to the key code for the next data bit */\r
@@ -337,12 +312,12 @@ bool GetNextReport(USB_KeyboardReport_Data_t* ReportData)
        return false;\r
 }\r
 \r
-/** Task to read out data from inserted magnetic cards and place the seperate track data into their respective\r
+/** Task to read out data from inserted magnetic cards and place the separate track data into their respective\r
  *  data buffers for later sending to the host as keyboard key presses.\r
  */\r
 TASK(Magstripe_Read)\r
 {\r
-       /* Arrays to hold the buffer pointers, clock and data bit masks for the seperate card tracks */\r
+       /* Arrays to hold the buffer pointers, clock and data bit masks for the separate card tracks */\r
        const struct\r
        {\r
                BitBuffer_t* Buffer;\r
@@ -396,7 +371,7 @@ TASK(Magstripe_Read)
 }\r
 \r
 /** Task for the magnetic card reading and keyboard report generation. This task waits until a card is inserted,\r
- *  then reads off the card data and sends it to the host as a series of keyboard keypresses via keyboard reports.\r
+ *  then reads off the card data and sends it to the host as a series of keyboard key presses via keyboard reports.\r
  */\r
 TASK(USB_Keyboard_Report)\r
 {\r
@@ -410,7 +385,7 @@ TASK(USB_Keyboard_Report)
                Endpoint_SelectEndpoint(KEYBOARD_EPNUM);\r
 \r
                /* Check if Keyboard Endpoint Ready for Read/Write */\r
-               if (Endpoint_ReadWriteAllowed())\r
+               if (Endpoint_IsReadWriteAllowed())\r
                {\r
                        /* Only fetch the next key to send once the period between key presses has elapsed */\r
                        if (!(KeyDelayRemaining))\r
@@ -436,7 +411,7 @@ TASK(USB_Keyboard_Report)
                                Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(USB_KeyboardReport_Data_t));\r
 \r
                                /* Finalize the stream transfer to send the last packet */\r
-                               Endpoint_ClearCurrentBank();\r
+                               Endpoint_ClearIN();\r
 \r
                                /* Reset the key delay period counter */\r
                                KeyDelayRemaining = 2;\r