| 1 | /*\r |
| 2 | * Proxmark send and receive commands\r |
| 3 | *\r |
| 4 | * Copyright (c) 2012, Roel Verdult\r |
| 5 | * All rights reserved.\r |
| 6 | *\r |
| 7 | * Redistribution and use in source and binary forms, with or without\r |
| 8 | * modification, are permitted provided that the following conditions are met:\r |
| 9 | * 1. Redistributions of source code must retain the above copyright\r |
| 10 | * notice, this list of conditions and the following disclaimer.\r |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright\r |
| 12 | * notice, this list of conditions and the following disclaimer in the\r |
| 13 | * documentation and/or other materials provided with the distribution.\r |
| 14 | * 3. Neither the name of the copyright holders nor the\r |
| 15 | * names of its contributors may be used to endorse or promote products\r |
| 16 | * derived from this software without specific prior written permission.\r |
| 17 | *\r |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r |
| 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r |
| 28 | *\r |
| 29 | * @file cmd.c\r |
| 30 | * @brief\r |
| 31 | */\r |
| 32 | \r |
| 33 | #include "cmd.h"\r |
| 34 | #include "string.h"\r |
| 35 | #include "proxmark3.h"\r |
| 36 | \r |
| 37 | bool cmd_receive(UsbCommand* cmd) {\r |
| 38 | \r |
| 39 | // Check if there is a usb packet available\r |
| 40 | if (!usb_poll()) return false;\r |
| 41 | \r |
| 42 | // Try to retrieve the available command frame\r |
| 43 | size_t rxlen = usb_read((byte_t*)cmd,sizeof(UsbCommand));\r |
| 44 | \r |
| 45 | // Check if the transfer was complete\r |
| 46 | if (rxlen != sizeof(UsbCommand)) return false;\r |
| 47 | \r |
| 48 | // Received command successfully\r |
| 49 | return true;\r |
| 50 | }\r |
| 51 | \r |
| 52 | bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {\r |
| 53 | UsbCommand txcmd;\r |
| 54 | \r |
| 55 | for (size_t i=0; i<sizeof(UsbCommand); i++) {\r |
| 56 | ((byte_t*)&txcmd)[i] = 0x00;\r |
| 57 | }\r |
| 58 | \r |
| 59 | // Compose the outgoing command frame\r |
| 60 | txcmd.cmd = cmd;\r |
| 61 | txcmd.arg[0] = arg0;\r |
| 62 | txcmd.arg[1] = arg1; \r |
| 63 | txcmd.arg[2] = arg2;\r |
| 64 | \r |
| 65 | // Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE\r |
| 66 | if (data && len) {\r |
| 67 | len = MIN(len,USB_CMD_DATA_SIZE);\r |
| 68 | for (size_t i=0; i<len; i++) {\r |
| 69 | txcmd.d.asBytes[i] = ((byte_t*)data)[i];\r |
| 70 | }\r |
| 71 | }\r |
| 72 | \r |
| 73 | // Send frame and make sure all bytes are transmitted\r |
| 74 | if (usb_write((byte_t*)&txcmd,sizeof(UsbCommand)) != 0) return false;\r |
| 75 | \r |
| 76 | return true;\r |
| 77 | }\r |
| 78 | \r |
| 79 | \r |