]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/usb_cdc.c
modify USB communications
[proxmark3-svn] / common / usb_cdc.c
index 56690ad87f69c42a095a2f515341dbf44304f858..fa26ea7d8e85323620f8d9ecd16726af0b18ff08 100644 (file)
  */\r
 \r
 #include "usb_cdc.h"\r
+\r
+#include <stddef.h>\r
+#include <stdint.h>\r
+#include <stdbool.h>\r
+\r
+#include "common.h"\r
 #include "at91sam7s512.h"\r
 #include "config_gpio.h"\r
 \r
-\r
 #define AT91C_EP_CONTROL     0\r
 #define AT91C_EP_OUT         1\r
 #define AT91C_EP_IN          2\r
 #define AT91C_EP_OUT_SIZE 0x40\r
 #define AT91C_EP_IN_SIZE  0x40\r
 \r
+// Language must always be 0.\r
+#define STR_LANGUAGE_CODES 0x00\r
+#define STR_MANUFACTURER   0x01\r
+#define STR_PRODUCT        0x02\r
+\r
+\r
 static const char devDescriptor[] = {\r
        /* Device descriptor */\r
        0x12,      // bLength\r
@@ -56,12 +67,13 @@ static const char devDescriptor[] = {
        0xc4,0x9a, // Vendor ID (0x9ac4 = J. Westhues)\r
        0x8f,0x4b, // Product ID (0x4b8f = Proxmark-3 RFID Instrument)\r
        0x01,0x00, // Device release number (0001)\r
-       0x01,      // iManufacturer\r
-       0x02,      // iProduct\r
+       STR_MANUFACTURER,  // iManufacturer\r
+       STR_PRODUCT,       // iProduct\r
        0x00,      // iSerialNumber\r
        0x01       // bNumConfigs\r
 };\r
 \r
+\r
 static const char cfgDescriptor[] = {\r
        /* ============== CONFIGURATION 1 =========== */\r
        /* Configuration 1 descriptor */\r
@@ -72,8 +84,8 @@ static const char cfgDescriptor[] = {
        0x02,   // CbNumInterfaces\r
        0x01,   // CbConfigurationValue\r
        0x00,   // CiConfiguration\r
-       0xC0,   // CbmAttributes 0xA0\r
-       0xFA,   // CMaxPower\r
+       0x80,   // CbmAttributes (Bus Powered)\r
+       0x4B,   // CMaxPower (150mA max current drawn from bus)\r
 \r
        /* Interface 0 Descriptor: Communication Class Interface */\r
        0x09, // bLength\r
@@ -152,57 +164,68 @@ static const char cfgDescriptor[] = {
        0x00    // bInterval\r
 };\r
 \r
+\r
 static const char StrDescLanguageCodes[] = {\r
-  4,                   // Length\r
-  0x03,                        // Type is string\r
-  0x09, 0x04   // supported language Code 0 = 0x0409 (English)\r
+       4,          // Length\r
+       0x03,       // Type is string\r
+       0x09, 0x04  // supported language Code 0 = 0x0409 (English)\r
 };\r
-       \r
+\r
+\r
+// Note: ModemManager (Linux) ignores Proxmark3 devices by matching the\r
+// manufacturer string "proxmark.org". Don't change this.\r
 static const char StrDescManufacturer[] = {\r
-  26,                  // Length\r
-  0x03,                        // Type is string\r
-  'p', 0x00,\r
-  'r', 0x00,\r
-  'o', 0x00,\r
-  'x', 0x00,\r
-  'm', 0x00,\r
-  'a', 0x00,\r
-  'r', 0x00,\r
-  'k', 0x00,\r
-  '.', 0x00,\r
-  'o', 0x00,\r
-  'r', 0x00,\r
-  'g', 0x00\r
+       26,         // Length\r
+       0x03,       // Type is string\r
+       'p', 0x00,\r
+       'r', 0x00,\r
+       'o', 0x00,\r
+       'x', 0x00,\r
+       'm', 0x00,\r
+       'a', 0x00,\r
+       'r', 0x00,\r
+       'k', 0x00,\r
+       '.', 0x00,\r
+       'o', 0x00,\r
+       'r', 0x00,\r
+       'g', 0x00\r
 };\r
 \r
+\r
 static const char StrDescProduct[] = {\r
-  8,                   // Length\r
-  0x03,                        // Type is string\r
-  'P', 0x00,\r
-  'M', 0x00,\r
-  '3', 0x00\r
-};\r
-       \r
-static const char* const pStrings[] =\r
-{\r
-    StrDescLanguageCodes,\r
-    StrDescManufacturer,\r
-       StrDescProduct\r
+       20,         // Length\r
+       0x03,       // Type is string\r
+       'p', 0x00,\r
+       'r', 0x00,\r
+       'o', 0x00,\r
+       'x', 0x00,\r
+       'm', 0x00,\r
+       'a', 0x00,\r
+       'r', 0x00,\r
+       'k', 0x00,\r
+       '3', 0x00\r
 };\r
 \r
-const char* getStringDescriptor(uint8_t idx)\r
-{\r
-    if(idx >= (sizeof(pStrings) / sizeof(pStrings[0]))) {\r
-        return(NULL);\r
-       } else {\r
-               return(pStrings[idx]);\r
+\r
+static const char* getStringDescriptor(uint8_t idx) {\r
+       switch (idx) {\r
+               case STR_LANGUAGE_CODES:\r
+                       return StrDescLanguageCodes;\r
+               case STR_MANUFACTURER:\r
+                       return StrDescManufacturer;\r
+               case STR_PRODUCT:\r
+                       return StrDescProduct;\r
+               default:\r
+                       return NULL;\r
        }\r
 }\r
 \r
+\r
 // Bitmap for all status bits in CSR which must be written as 1 to cause no effect\r
 #define REG_NO_EFFECT_1_ALL      AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 \\r
-                                |AT91C_UDP_STALLSENT   | AT91C_UDP_RXSETUP \\r
-                                |AT91C_UDP_TXCOMP\r
+                                                               |AT91C_UDP_STALLSENT   | AT91C_UDP_RXSETUP \\r
+                                                               |AT91C_UDP_TXCOMP\r
+\r
 \r
 // Clear flags in the UDP_CSR register\r
 #define UDP_CLEAR_EP_FLAGS(endpoint, flags) { \\r
@@ -211,7 +234,8 @@ const char* getStringDescriptor(uint8_t idx)
        reg |= REG_NO_EFFECT_1_ALL; \\r
        reg &= ~(flags); \\r
        pUdp->UDP_CSR[(endpoint)] = reg; \\r
-} \r
+}\r
+\r
 \r
 // Set flags in the UDP_CSR register\r
 #define UDP_SET_EP_FLAGS(endpoint, flags) { \\r
@@ -222,6 +246,7 @@ const char* getStringDescriptor(uint8_t idx)
        pUdp->UDP_CSR[(endpoint)] = reg; \\r
 }\r
 \r
+\r
 /* USB standard request codes */\r
 #define STD_GET_STATUS_ZERO           0x0080\r
 #define STD_GET_STATUS_INTERFACE      0x0081\r
@@ -249,6 +274,7 @@ const char* getStringDescriptor(uint8_t idx)
 #define SET_LINE_CODING               0x2021\r
 #define SET_CONTROL_LINE_STATE        0x2221\r
 \r
+\r
 typedef struct {\r
        unsigned int dwDTERRate;\r
        char bCharFormat;\r
@@ -256,6 +282,7 @@ typedef struct {
        char bDataBits;\r
 } AT91S_CDC_LINE_CODING, *AT91PS_CDC_LINE_CODING;\r
 \r
+\r
 AT91S_CDC_LINE_CODING line = {\r
        115200, // baudrate\r
        0,      // 1 Stop Bit\r
@@ -263,12 +290,12 @@ AT91S_CDC_LINE_CODING line = {
        8};     // 8 Data bits\r
 \r
 \r
-void AT91F_CDC_Enumerate();\r
+static void AT91F_CDC_Enumerate();\r
 \r
 AT91PS_UDP pUdp = AT91C_BASE_UDP;\r
-byte_t btConfiguration = 0;\r
-byte_t btConnection    = 0;\r
-byte_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
+uint8_t btConfiguration = 0;\r
+uint8_t btConnection    = 0;\r
+uint8_t btReceiveBank   = AT91C_UDP_RX_DATA_BK0;\r
 \r
 \r
 //*----------------------------------------------------------------------------\r
@@ -280,7 +307,7 @@ void usb_disable() {
        AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU;\r
 \r
        // Clear all lingering interrupts\r
-       if(pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
+       if (pUdp->UDP_ISR & AT91C_UDP_ENDBUSRES) {\r
                pUdp->UDP_ICR = AT91C_UDP_ENDBUSRES;\r
        }\r
 }\r
@@ -311,7 +338,7 @@ void usb_enable() {
        usb_disable();\r
 \r
        // Wait for a short while\r
-       for (volatile size_t i=0; i<0x100000; i++);\r
+       for (volatile size_t i = 0; i < 0x100000; i++);\r
 \r
        // Reconnect USB reconnect\r
        AT91C_BASE_PIOA->PIO_SODR = GPIO_USB_PU;\r
@@ -323,7 +350,7 @@ void usb_enable() {
 //* \fn    usb_check\r
 //* \brief Test if the device is configured and handle enumeration\r
 //*----------------------------------------------------------------------------\r
-bool usb_check() {\r
+static bool usb_check() {\r
        AT91_REG isr = pUdp->UDP_ISR;\r
 \r
        if (isr & AT91C_UDP_ENDBUSRES) {\r
@@ -343,8 +370,7 @@ bool usb_check() {
 }\r
 \r
 \r
-bool usb_poll()\r
-{\r
+bool usb_poll() {\r
        if (!usb_check()) return false;\r
        return (pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank);\r
 }\r
@@ -358,29 +384,29 @@ bool usb_poll()
        that the length available to read is non-zero, thus hopefully fixes the\r
        bug.\r
 **/\r
-bool usb_poll_validate_length()\r
-{\r
+bool usb_poll_validate_length() {\r
        if (!usb_check()) return false;\r
        if (!(pUdp->UDP_CSR[AT91C_EP_OUT] & btReceiveBank)) return false;\r
        return (pUdp->UDP_CSR[AT91C_EP_OUT] >> 16) >  0;\r
 }\r
 \r
+\r
 //*----------------------------------------------------------------------------\r
 //* \fn    usb_read\r
 //* \brief Read available data from Endpoint OUT\r
 //*----------------------------------------------------------------------------\r
-uint32_t usb_read(byte_t* data, size_t len) {\r
-       byte_t bank = btReceiveBank;\r
+static uint32_t usb_read(uint8_t* data, size_t len) {\r
+       uint8_t bank = btReceiveBank;\r
        uint32_t packetSize, nbBytesRcv = 0;\r
        uint32_t time_out = 0;\r
-  \r
+\r
        while (len)  {\r
                if (!usb_check()) break;\r
 \r
                if ( pUdp->UDP_CSR[AT91C_EP_OUT] & bank ) {\r
                        packetSize = MIN(pUdp->UDP_CSR[AT91C_EP_OUT] >> 16, len);\r
                        len -= packetSize;\r
-                       while(packetSize--)\r
+                       while (packetSize--)\r
                                data[nbBytesRcv++] = pUdp->UDP_FDR[AT91C_EP_OUT];\r
                        UDP_CLEAR_EP_FLAGS(AT91C_EP_OUT, bank);\r
                        if (bank == AT91C_UDP_RX_DATA_BK0) {\r
@@ -401,7 +427,7 @@ uint32_t usb_read(byte_t* data, size_t len) {
 //* \fn    usb_write\r
 //* \brief Send through endpoint 2\r
 //*----------------------------------------------------------------------------\r
-uint32_t usb_write(const byte_t* data, const size_t len) {\r
+static uint32_t usb_write(const uint8_t* data, const size_t len) {\r
        size_t length = len;\r
        uint32_t cpt = 0;\r
 \r
@@ -428,7 +454,8 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
                        if (!usb_check()) return length;\r
                }\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
+               while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)\r
+                       /* wait */;\r
                UDP_SET_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXPKTRDY);\r
        }\r
 \r
@@ -438,7 +465,8 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
        }\r
 \r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_IN, AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP);\r
+       while (pUdp->UDP_CSR[AT91C_EP_IN] & AT91C_UDP_TXCOMP)\r
+               /* wait */;\r
 \r
        return length;\r
 }\r
@@ -448,9 +476,6 @@ uint32_t usb_write(const byte_t* data, const size_t len) {
 //* \fn    AT91F_USB_SendData\r
 //* \brief Send Data through the control endpoint\r
 //*----------------------------------------------------------------------------\r
-unsigned int csrTab[100] = {0x00};\r
-unsigned char csrIdx = 0;\r
-\r
 static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {\r
        uint32_t cpt = 0;\r
        AT91_REG csr;\r
@@ -464,7 +489,8 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng
 \r
                if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
                        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-                       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
+                       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
+                               /* wait */;\r
                }\r
 \r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);\r
@@ -476,13 +502,14 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng
                                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
                                return;\r
                        }\r
-               } while ( !(csr & AT91C_UDP_TXCOMP) );\r
+               } while (!(csr & AT91C_UDP_TXCOMP));\r
 \r
        } while (length);\r
 \r
        if (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) {\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-               while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
+               while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
+                       /* wait */;\r
        }\r
 }\r
 \r
@@ -491,11 +518,13 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng
 //* \fn    AT91F_USB_SendZlp\r
 //* \brief Send zero length packet through the control endpoint\r
 //*----------------------------------------------------------------------------\r
-void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
+static void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {\r
        UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);\r
-       while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP) );\r
+       while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP))\r
+               /* wait */;\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXCOMP);\r
-       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP);\r
+       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP)\r
+               /* wait */;\r
 }\r
 \r
 \r
@@ -503,11 +532,13 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
 //* \fn    AT91F_USB_SendStall\r
 //* \brief Stall the control endpoint\r
 //*----------------------------------------------------------------------------\r
-void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
+static void AT91F_USB_SendStall(AT91PS_UDP pUdp) {\r
        UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);\r
-       while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR) );\r
+       while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR))\r
+               /* wait */;\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR);\r
-       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR));\r
+       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & (AT91C_UDP_FORCESTALL | AT91C_UDP_ISOERROR))\r
+               /* wait */;\r
 }\r
 \r
 \r
@@ -515,11 +546,11 @@ void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
 //* \fn    AT91F_CDC_Enumerate\r
 //* \brief This function is a callback invoked when a SETUP packet is received\r
 //*----------------------------------------------------------------------------\r
-void AT91F_CDC_Enumerate() {\r
-       byte_t bmRequestType, bRequest;\r
+static void AT91F_CDC_Enumerate() {\r
+       uint8_t bmRequestType, bRequest;\r
        uint16_t wValue, wIndex, wLength, wStatus;\r
 \r
-       if ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP) )\r
+       if (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP))\r
                return;\r
 \r
        bmRequestType = pUdp->UDP_FDR[AT91C_EP_CONTROL];\r
@@ -531,12 +562,14 @@ void AT91F_CDC_Enumerate() {
        wLength       = (pUdp->UDP_FDR[AT91C_EP_CONTROL] & 0xFF);\r
        wLength      |= (pUdp->UDP_FDR[AT91C_EP_CONTROL] << 8);\r
 \r
-       if (bmRequestType & 0x80) {     // Data Phase Transfer Direction Device to Host\r
+       if (bmRequestType & 0x80) { // Data Phase Transfer Direction Device to Host\r
                UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_DIR);\r
-               while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_DIR) );\r
+               while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_DIR))\r
+                       /* wait */;\r
        }\r
        UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RXSETUP);\r
-       while ( (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)  );\r
+       while (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RXSETUP)\r
+               /* wait */;\r
 \r
        // Handle supported standard device request Cf Table 9-3 in USB specification Rev 1.1\r
        switch ((bRequest << 8) | bmRequestType) {\r
@@ -573,11 +606,11 @@ void AT91F_CDC_Enumerate() {
                AT91F_USB_SendData(pUdp, (char *) &(btConfiguration), sizeof(btConfiguration));\r
                break;\r
        case STD_GET_STATUS_ZERO:\r
-               wStatus = 0;    // Device is Bus powered, remote wakeup disabled\r
+               wStatus = 0;    // Device is Bus powered, remote wakeup disabled\r
                AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
                break;\r
        case STD_GET_STATUS_INTERFACE:\r
-               wStatus = 0;    // reserved for future use\r
+               wStatus = 0;    // reserved for future use\r
                AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
                break;\r
        case STD_GET_STATUS_ENDPOINT:\r
@@ -586,17 +619,15 @@ void AT91F_CDC_Enumerate() {
                if ((pUdp->UDP_GLBSTATE & AT91C_UDP_CONFG) && (wIndex <= AT91C_EP_NOTIFY)) {\r
                        wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
                        AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == AT91C_EP_CONTROL)) {\r
+               } else if ((pUdp->UDP_GLBSTATE & AT91C_UDP_FADDEN) && (wIndex == AT91C_EP_CONTROL)) {\r
                        wStatus = (pUdp->UDP_CSR[wIndex] & AT91C_UDP_EPEDS) ? 0 : 1;\r
                        AT91F_USB_SendData(pUdp, (char *) &wStatus, sizeof(wStatus));\r
-               }\r
-               else\r
+               } else\r
                        AT91F_USB_SendStall(pUdp);\r
                break;\r
        case STD_SET_FEATURE_ZERO:\r
                AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               break;\r
        case STD_SET_FEATURE_INTERFACE:\r
                AT91F_USB_SendZlp(pUdp);\r
                break;\r
@@ -605,13 +636,12 @@ void AT91F_CDC_Enumerate() {
                if ((wValue == 0) && (wIndex >= AT91C_EP_OUT) && (wIndex <= AT91C_EP_NOTIFY)) {\r
                        pUdp->UDP_CSR[wIndex] = 0;\r
                        AT91F_USB_SendZlp(pUdp);\r
-               }\r
-               else\r
+               } else\r
                        AT91F_USB_SendStall(pUdp);\r
                break;\r
        case STD_CLEAR_FEATURE_ZERO:\r
                AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               break;\r
        case STD_CLEAR_FEATURE_INTERFACE:\r
                AT91F_USB_SendZlp(pUdp);\r
                break;\r
@@ -632,7 +662,8 @@ void AT91F_CDC_Enumerate() {
 \r
        // handle CDC class requests\r
        case SET_LINE_CODING:\r
-               while ( !(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0) );\r
+               while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0))\r
+                       /* wait */;\r
                UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);\r
                AT91F_USB_SendZlp(pUdp);\r
                break;\r
@@ -645,6 +676,86 @@ void AT91F_CDC_Enumerate() {
                break;\r
        default:\r
                AT91F_USB_SendStall(pUdp);\r
-           break;\r
+               break;\r
        }\r
 }\r
+\r
+\r
+//***************************************************************************\r
+// Interface to the main program\r
+//***************************************************************************\r
+\r
+// The function to receive a command from the client via USB\r
+bool cmd_receive(UsbCommand* cmd) {\r
+\r
+       // Check if there is a usb packet available\r
+       if (!usb_poll())\r
+               return false;\r
+\r
+       // Try to retrieve the available command frame\r
+       size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand));\r
+\r
+       // Check if the transfer was complete\r
+       if (rxlen != sizeof(UsbCommand))\r
+               return false;\r
+\r
+       // Received command successfully\r
+       return true;\r
+}\r
+\r
+\r
+// The function to send a response to the client via USB\r
+bool cmd_send(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbResponse txcmd;\r
+\r
+       // Compose the outgoing response frame\r
+       txcmd.cmd = cmd | CMD_VARIABLE_SIZE_FLAG;\r
+       txcmd.arg[0] = arg0;\r
+       txcmd.arg[1] = arg1;\r
+       txcmd.arg[2] = arg2;\r
+\r
+       // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
+                       txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+               }\r
+               txcmd.datalen = datalen;\r
+       } else {\r
+               txcmd.datalen = 0;\r
+       }\r
+\r
+       // Send frame and make sure all bytes are transmitted\r
+       size_t tx_size = offsetof(UsbResponse, d) + datalen;\r
+       if (usb_write((uint8_t*)&txcmd, tx_size) != 0) return false;\r
+\r
+       return true;\r
+}\r
+\r
+\r
+// For compatibility only: legacy function to send a response with fixed size to the client via USB\r
+bool cmd_send_old(uint16_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, uint16_t datalen) {\r
+\r
+       UsbCommand txcmd;\r
+\r
+       // Compose the outgoing response frame\r
+       txcmd.cmd = cmd;\r
+       txcmd.arg[0] = arg0;\r
+       txcmd.arg[1] = arg1;\r
+       txcmd.arg[2] = arg2;\r
+\r
+       // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r
+       if (data) {\r
+               datalen = MIN(datalen, USB_CMD_DATA_SIZE);\r
+               for (uint16_t i = 0; i < datalen; i++) {\r
+                       txcmd.d.asBytes[i] = ((uint8_t*)data)[i];\r
+               }\r
+       }\r
+       \r
+       // Send frame and make sure all bytes are transmitted\r
+       if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;\r
+\r
+       return true;\r
+}\r
+\r
Impressum, Datenschutz