1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // Code for communicating with the proxmark3 hardware.
10 //-----------------------------------------------------------------------------
19 #include "util_posix.h"
23 // Serial port that we are communicating with the PM3 on.
26 // If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
30 // TODO: Use locks and execute this on the main thread, rather than the receiver
31 // thread. Running on the main thread means we need to be careful in the
32 // flasher, as it means SendCommand is no longer async, and can't be used as a
33 // buffer for a pending command when the connection is re-established.
34 static UsbCommand txcmd
;
35 volatile static bool txcmd_pending
= false;
37 // Used by UsbReceiveCommand as a ring buffer for messages that are yet to be
38 // processed by a command handler (WaitForResponse{,Timeout})
39 static UsbCommand cmdBuffer
[CMD_BUFFER_SIZE
];
41 // Points to the next empty position to write to
42 static int cmd_head
= 0;
44 // Points to the position of the last unread command
45 static int cmd_tail
= 0;
47 // to lock cmdBuffer operations from different threads
48 static pthread_mutex_t cmdBufferMutex
= PTHREAD_MUTEX_INITIALIZER
;
51 void SendCommand(UsbCommand
*c
) {
53 printf("Sending %d bytes\n", sizeof(UsbCommand
));
57 PrintAndLog("Sending bytes to proxmark failed - offline");
61 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
62 or disconnected. The main console thread is alive, but comm thread just spins here.
72 * @brief This method should be called when sending a new command to the pm3. In case any old
73 * responses from previous commands are stored in the buffer, a call to this method should clear them.
74 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
75 * operation. Right now we'll just have to live with this.
77 void clearCommandBuffer()
79 //This is a very simple operation
80 pthread_mutex_lock(&cmdBufferMutex
);
82 pthread_mutex_unlock(&cmdBufferMutex
);
86 * @brief storeCommand stores a USB command in a circular buffer
89 void storeCommand(UsbCommand
*command
)
91 pthread_mutex_lock(&cmdBufferMutex
);
92 if( (cmd_head
+1) % CMD_BUFFER_SIZE
== cmd_tail
)
94 // If these two are equal, we're about to overwrite in the
96 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
99 // Store the command at the 'head' location
100 UsbCommand
* destination
= &cmdBuffer
[cmd_head
];
101 memcpy(destination
, command
, sizeof(UsbCommand
));
103 cmd_head
= (cmd_head
+1) % CMD_BUFFER_SIZE
; //increment head and wrap
104 pthread_mutex_unlock(&cmdBufferMutex
);
109 * @brief getCommand gets a command from an internal circular buffer.
110 * @param response location to write command
111 * @return 1 if response was returned, 0 if nothing has been received
113 int getCommand(UsbCommand
* response
)
115 pthread_mutex_lock(&cmdBufferMutex
);
116 //If head == tail, there's nothing to read, or if we just got initialized
117 if (cmd_head
== cmd_tail
){
118 pthread_mutex_unlock(&cmdBufferMutex
);
122 //Pick out the next unread command
123 UsbCommand
* last_unread
= &cmdBuffer
[cmd_tail
];
124 memcpy(response
, last_unread
, sizeof(UsbCommand
));
125 //Increment tail - this is a circular buffer, so modulo buffer size
126 cmd_tail
= (cmd_tail
+ 1) % CMD_BUFFER_SIZE
;
128 pthread_mutex_unlock(&cmdBufferMutex
);
133 //-----------------------------------------------------------------------------
134 // Entry point into our code: called whenever we received a packet over USB
135 // that we weren't necessarily expecting, for example a debug print.
136 //-----------------------------------------------------------------------------
137 void UsbCommandReceived(UsbCommand
*UC
)
140 // First check if we are handling a debug message
141 case CMD_DEBUG_PRINT_STRING
: {
142 char s
[USB_CMD_DATA_SIZE
+1];
143 memset(s
, 0x00, sizeof(s
));
144 size_t len
= MIN(UC
->arg
[0],USB_CMD_DATA_SIZE
);
145 memcpy(s
,UC
->d
.asBytes
,len
);
146 PrintAndLog("#db# %s", s
);
150 case CMD_DEBUG_PRINT_INTEGERS
: {
151 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC
->arg
[0], UC
->arg
[1], UC
->arg
[2]);
155 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K
: {
156 memcpy(sample_buf
+(UC
->arg
[0]),UC
->d
.asBytes
,UC
->arg
[1]);
169 #ifdef __has_attribute
170 #if __has_attribute(force_align_arg_pointer)
171 __attribute__((force_align_arg_pointer
))
174 *uart_receiver(void *targ
) {
175 receiver_arg
*arg
= (receiver_arg
*)targ
;
177 uint8_t rx
[sizeof(UsbCommand
)];
182 if (uart_receive(sp
, prx
, sizeof(UsbCommand
) - (prx
-rx
), &rxlen
) && rxlen
) {
184 if (prx
-rx
< sizeof(UsbCommand
)) {
187 UsbCommandReceived((UsbCommand
*)rx
);
192 if (!uart_send(sp
, (uint8_t*) &txcmd
, sizeof(UsbCommand
))) {
193 PrintAndLog("Sending bytes to proxmark failed");
195 txcmd_pending
= false;
205 * Waits for a certain response type. This method waits for a maximum of
206 * ms_timeout milliseconds for a specified response command.
207 *@brief WaitForResponseTimeout
208 * @param cmd command to wait for
209 * @param response struct to copy received command into.
211 * @return true if command was returned, otherwise false
213 bool WaitForResponseTimeoutW(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
, bool show_warning
) {
217 if (response
== NULL
) {
221 uint64_t start_time
= msclock();
223 // Wait until the command is received
225 while(getCommand(response
)) {
226 if(response
->cmd
== cmd
){
231 if (msclock() - start_time
> ms_timeout
) {
235 if (msclock() - start_time
> 2000 && show_warning
) {
236 PrintAndLog("Waiting for a response from the proxmark...");
237 PrintAndLog("You can cancel this operation by pressing the pm3 button");
238 show_warning
= false;
245 bool WaitForResponseTimeout(uint32_t cmd
, UsbCommand
* response
, size_t ms_timeout
) {
246 return WaitForResponseTimeoutW(cmd
, response
, ms_timeout
, true);
249 bool WaitForResponse(uint32_t cmd
, UsbCommand
* response
) {
250 return WaitForResponseTimeoutW(cmd
, response
, -1, true);