]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - common/usb_cdc.c
modify USB communications
[proxmark3-svn] / common / usb_cdc.c
index 66b02e78870b6670b6c125fcc9e0cab625b75187..fa26ea7d8e85323620f8d9ecd16726af0b18ff08 100644 (file)
@@ -705,29 +705,57 @@ bool cmd_receive(UsbCommand* cmd) {
 \r
 \r
 // The function to send a response to the client via USB\r
-bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r
-       UsbCommand txcmd;\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
-       for (size_t i = 0; i < sizeof(UsbCommand); i++) {\r
-               ((uint8_t*)&txcmd)[i] = 0x00;\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
-       // Compose the outgoing command frame\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 && len) {\r
-               len = MIN(len, USB_CMD_DATA_SIZE);\r
-               for (size_t i = 0; i < len; i++) {\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
+       \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