]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/comms.c
Comms refactor (prerequisite of libproxmark work) (#371)
[proxmark3-svn] / client / comms.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
4 //
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
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Code for communicating with the proxmark3 hardware.
10 //-----------------------------------------------------------------------------
11
12 #include <pthread.h>
13
14 #include "comms.h"
15 #include "uart.h"
16 #include "ui.h"
17 #include "common.h"
18 #include "data.h"
19 #include "util_posix.h"
20
21 // Declare globals.
22
23 // Serial port that we are communicating with the PM3 on.
24 static serial_port* port;
25
26 // If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
27 static bool offline;
28
29 // Transmit buffer.
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 static bool txcmd_pending;
36
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];
40
41 // Points to the next empty position to write to
42 static int cmd_head = 0;
43
44 // Points to the position of the last unread command
45 static int cmd_tail = 0;
46
47 // to lock cmdBuffer operations from different threads
48 static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
49
50 // These wrappers are required because it is not possible to access a static
51 // global variable outside of the context of a single file.
52
53 void SetSerialPort(serial_port* new_port) {
54 port = new_port;
55 }
56
57 serial_port* GetSerialPort() {
58 return port;
59 }
60
61 void SetOffline(bool new_offline) {
62 offline = new_offline;
63 }
64
65 bool IsOffline() {
66 return offline;
67 }
68
69 void SendCommand(UsbCommand *c) {
70 #ifdef COMMS_DEBUG
71 printf("Sending %04x cmd\n", c->cmd);
72 #endif
73
74 if (offline) {
75 PrintAndLog("Sending bytes to proxmark failed - offline");
76 return;
77 }
78 /**
79 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
80 or disconnected. The main console thread is alive, but comm thread just spins here.
81 Not good.../holiman
82 **/
83 while(txcmd_pending);
84 txcmd = *c;
85 txcmd_pending = true;
86 }
87
88 /**
89 * @brief This method should be called when sending a new command to the pm3. In case any old
90 * responses from previous commands are stored in the buffer, a call to this method should clear them.
91 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
92 * operation. Right now we'll just have to live with this.
93 */
94 void clearCommandBuffer()
95 {
96 //This is a very simple operation
97 pthread_mutex_lock(&cmdBufferMutex);
98 cmd_tail = cmd_head;
99 pthread_mutex_unlock(&cmdBufferMutex);
100 }
101
102 /**
103 * @brief storeCommand stores a USB command in a circular buffer
104 * @param UC
105 */
106 void storeCommand(UsbCommand *command)
107 {
108 pthread_mutex_lock(&cmdBufferMutex);
109 if( (cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
110 {
111 // If these two are equal, we're about to overwrite in the
112 // circular buffer.
113 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
114 }
115
116 // Store the command at the 'head' location
117 UsbCommand* destination = &cmdBuffer[cmd_head];
118 memcpy(destination, command, sizeof(UsbCommand));
119
120 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
121 pthread_mutex_unlock(&cmdBufferMutex);
122 }
123
124
125 /**
126 * @brief getCommand gets a command from an internal circular buffer.
127 * @param response location to write command
128 * @return 1 if response was returned, 0 if nothing has been received
129 */
130 int getCommand(UsbCommand* response)
131 {
132 pthread_mutex_lock(&cmdBufferMutex);
133 //If head == tail, there's nothing to read, or if we just got initialized
134 if (cmd_head == cmd_tail){
135 pthread_mutex_unlock(&cmdBufferMutex);
136 return 0;
137 }
138
139 //Pick out the next unread command
140 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
141 memcpy(response, last_unread, sizeof(UsbCommand));
142 //Increment tail - this is a circular buffer, so modulo buffer size
143 cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
144
145 pthread_mutex_unlock(&cmdBufferMutex);
146 return 1;
147 }
148
149 //-----------------------------------------------------------------------------
150 // Entry point into our code: called whenever we received a packet over USB
151 // that we weren't necessarily expecting, for example a debug print.
152 //-----------------------------------------------------------------------------
153 void UsbCommandReceived(UsbCommand *UC)
154 {
155 switch(UC->cmd) {
156
157 // First check if we are handling a debug message
158 case CMD_DEBUG_PRINT_STRING: {
159 char s[USB_CMD_DATA_SIZE+1];
160 memset(s, 0x00, sizeof(s));
161 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
162 memcpy(s,UC->d.asBytes,len);
163 PrintAndLog("#db# %s", s);
164 return;
165 }
166
167 case CMD_DEBUG_PRINT_INTEGERS: {
168 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
169 return;
170 }
171
172 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
173 // FIXME: This does unsanitised copies into memory when we don't know
174 // the size of the buffer.
175 if (sample_buf) {
176 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
177 }
178 return;
179 }
180
181 default: {
182 storeCommand(UC);
183 return;
184 }
185 }
186 }
187
188 // Gets a single command from a proxmark3 device. This should never be used
189 // with the full client.
190 //
191 // @param conn A receiver_arg structure.
192 // @param command A buffer to store the received command.
193 bool ReceiveCommand(receiver_arg* conn, UsbCommand* command) {
194 // Local recieve buffer
195 size_t rxlen;
196 byte_t rx[sizeof(UsbCommand)];
197 byte_t* prx = rx;
198
199 while (conn->run) {
200 rxlen = 0;
201 if (uart_receive(port, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
202 prx += rxlen;
203 if (prx-rx < sizeof(UsbCommand)) {
204 // Keep reading until we have a completed response.
205 continue;
206 }
207
208 // We have a completed response.
209 memcpy(command, rx, sizeof(UsbCommand));
210 return true;
211 }
212
213 if (prx == rx) {
214 // We got no complete command while waiting, give up control
215 return false;
216 }
217 }
218
219 // did not get a complete command before being cancelled.
220 return false;
221 }
222
223 // Worker thread for processing incoming events from the PM3
224 void *uart_receiver(void *targ) {
225 receiver_arg *conn = (receiver_arg*)targ;
226 UsbCommand rx;
227
228 while (conn->run) {
229 #ifdef COMMS_DEBUG
230 printf("uart_receiver: get lock\n");
231 #endif
232 // Lock up receives, in case they try to take it away from us.
233 pthread_mutex_lock(&conn->recv_lock);
234 #ifdef COMMS_DEBUG
235 printf("uart_receiver: lock acquired\n");
236 #endif
237
238 if (port == NULL) {
239 #ifdef COMMS_DEBUG
240 printf("uart_receiver: port disappeared\n");
241 #endif
242 // Our port disappeared, stall. This code path matters for the flasher,
243 // where it is fiddling with the serial port under us.
244 pthread_mutex_unlock(&conn->recv_lock);
245 msleep(10);
246 continue;
247 }
248
249 bool got_command = ReceiveCommand(conn, &rx);
250 #ifdef COMMS_DEBUG
251 printf("uart_receiver: got command\n");
252 #endif
253 pthread_mutex_unlock(&conn->recv_lock);
254
255 if (got_command) {
256 UsbCommandReceived(&rx);
257 }
258
259 // We aren't normally trying to transmit in the flasher when the port would
260 // be reset, so we can just keep going at this point.
261 if (txcmd_pending) {
262 if (!uart_send(port, (byte_t*) &txcmd, sizeof(UsbCommand))) {
263 PrintAndLog("Sending bytes to proxmark failed");
264 }
265 txcmd_pending = false;
266 }
267 }
268
269 pthread_exit(NULL);
270 return NULL;
271 }
272
273 /**
274 * Waits for a certain response type. This method waits for a maximum of
275 * ms_timeout milliseconds for a specified response command.
276 *@brief WaitForResponseTimeout
277 * @param cmd command to wait for, or CMD_ANY to take any command.
278 * @param response struct to copy received command into.
279 * @param ms_timeout
280 * @param show_warning
281 * @return true if command was returned, otherwise false
282 */
283 bool WaitForResponseTimeoutW(uint64_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
284 UsbCommand resp;
285
286 #ifdef COMMS_DEBUG
287 printf("Waiting for %04x cmd\n", cmd);
288 #endif
289
290 if (response == NULL) {
291 response = &resp;
292 }
293
294 uint64_t start_time = msclock();
295
296 // Wait until the command is received
297 for (;;) {
298 while(getCommand(response)) {
299 if (cmd == CMD_ANY || response->cmd == cmd) {
300 return true;
301 }
302 }
303
304 if (msclock() - start_time > ms_timeout) {
305 // We timed out.
306 break;
307 }
308
309 if (msclock() - start_time > 2000 && show_warning) {
310 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
311 PrintAndLog("Waiting for a response from the proxmark...");
312 PrintAndLog("Don't forget to cancel its operation first by pressing on the button");
313 break;
314 }
315 }
316 return false;
317 }
318
319 bool WaitForResponseTimeout(uint64_t cmd, UsbCommand* response, size_t ms_timeout) {
320 return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
321 }
322
323 bool WaitForResponse(uint64_t cmd, UsbCommand* response) {
324 return WaitForResponseTimeout(cmd, response, -1);
325 }
Impressum, Datenschutz