\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