]> git.zerfleddert.de Git - proxmark3-svn/blob - client/comms.c
2dd5534ce7ed53e4c608292d94cef90d4f0aa261
[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 serial_port sp;
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 volatile static bool txcmd_pending = false;
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 SetOffline(bool new_offline) {
54 offline = new_offline;
55 }
56
57 bool IsOffline() {
58 return offline;
59 }
60
61 void SendCommand(UsbCommand *c) {
62 #ifdef COMMS_DEBUG
63 printf("Sending %04x cmd\n", c->cmd);
64 #endif
65
66 if (offline) {
67 PrintAndLog("Sending bytes to proxmark failed - offline");
68 return;
69 }
70 /**
71 The while-loop below causes hangups at times, when the pm3 unit is unresponsive
72 or disconnected. The main console thread is alive, but comm thread just spins here.
73 Not good.../holiman
74 **/
75 while(txcmd_pending);
76 txcmd = *c;
77 txcmd_pending = true;
78 }
79
80
81 /**
82 * @brief This method should be called when sending a new command to the pm3. In case any old
83 * responses from previous commands are stored in the buffer, a call to this method should clear them.
84 * A better method could have been to have explicit command-ACKS, so we can know which ACK goes to which
85 * operation. Right now we'll just have to live with this.
86 */
87 void clearCommandBuffer()
88 {
89 //This is a very simple operation
90 pthread_mutex_lock(&cmdBufferMutex);
91 cmd_tail = cmd_head;
92 pthread_mutex_unlock(&cmdBufferMutex);
93 }
94
95 /**
96 * @brief storeCommand stores a USB command in a circular buffer
97 * @param UC
98 */
99 void storeCommand(UsbCommand *command)
100 {
101 pthread_mutex_lock(&cmdBufferMutex);
102 if( (cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
103 {
104 // If these two are equal, we're about to overwrite in the
105 // circular buffer.
106 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
107 }
108
109 // Store the command at the 'head' location
110 UsbCommand* destination = &cmdBuffer[cmd_head];
111 memcpy(destination, command, sizeof(UsbCommand));
112
113 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
114 pthread_mutex_unlock(&cmdBufferMutex);
115 }
116
117
118 /**
119 * @brief getCommand gets a command from an internal circular buffer.
120 * @param response location to write command
121 * @return 1 if response was returned, 0 if nothing has been received
122 */
123 int getCommand(UsbCommand* response)
124 {
125 pthread_mutex_lock(&cmdBufferMutex);
126 //If head == tail, there's nothing to read, or if we just got initialized
127 if (cmd_head == cmd_tail){
128 pthread_mutex_unlock(&cmdBufferMutex);
129 return 0;
130 }
131
132 //Pick out the next unread command
133 UsbCommand* last_unread = &cmdBuffer[cmd_tail];
134 memcpy(response, last_unread, sizeof(UsbCommand));
135 //Increment tail - this is a circular buffer, so modulo buffer size
136 cmd_tail = (cmd_tail + 1) % CMD_BUFFER_SIZE;
137
138 pthread_mutex_unlock(&cmdBufferMutex);
139 return 1;
140 }
141
142
143 //-----------------------------------------------------------------------------
144 // Entry point into our code: called whenever we received a packet over USB
145 // that we weren't necessarily expecting, for example a debug print.
146 //-----------------------------------------------------------------------------
147 void UsbCommandReceived(UsbCommand *UC)
148 {
149 switch(UC->cmd) {
150 // First check if we are handling a debug message
151 case CMD_DEBUG_PRINT_STRING: {
152 char s[USB_CMD_DATA_SIZE+1];
153 memset(s, 0x00, sizeof(s));
154 size_t len = MIN(UC->arg[0],USB_CMD_DATA_SIZE);
155 memcpy(s,UC->d.asBytes,len);
156 PrintAndLog("#db# %s", s);
157 return;
158 } break;
159
160 case CMD_DEBUG_PRINT_INTEGERS: {
161 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
162 return;
163 } break;
164
165 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
166 // FIXME: This does unsanitised copies into memory when we don't know
167 // the size of the buffer.
168 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
169 return;
170 } break;
171
172 default:
173 storeCommand(UC);
174 break;
175 }
176
177 }
178
179
180 void
181 #ifdef __has_attribute
182 #if __has_attribute(force_align_arg_pointer)
183 __attribute__((force_align_arg_pointer))
184 #endif
185 #endif
186 *uart_receiver(void *targ) {
187 receiver_arg *arg = (receiver_arg*)targ;
188 size_t rxlen;
189 uint8_t rx[sizeof(UsbCommand)];
190 uint8_t *prx = rx;
191
192 while (arg->run) {
193 rxlen = 0;
194 if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
195 prx += rxlen;
196 if (prx-rx < sizeof(UsbCommand)) {
197 continue;
198 }
199 UsbCommandReceived((UsbCommand*)rx);
200 }
201 prx = rx;
202
203 if(txcmd_pending) {
204 if (!uart_send(sp, (uint8_t*) &txcmd, sizeof(UsbCommand))) {
205 PrintAndLog("Sending bytes to proxmark failed");
206 }
207 txcmd_pending = false;
208 }
209 }
210
211 pthread_exit(NULL);
212 return NULL;
213 }
214
215
216 /**
217 * Waits for a certain response type. This method waits for a maximum of
218 * ms_timeout milliseconds for a specified response command.
219 *@brief WaitForResponseTimeout
220 * @param cmd command to wait for, or CMD_UNKNOWN to take any command.
221 * @param response struct to copy received command into.
222 * @param ms_timeout
223 * @param show_warning
224 * @return true if command was returned, otherwise false
225 */
226 bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
227
228 UsbCommand resp;
229
230 #ifdef COMMS_DEBUG
231 printf("Waiting for %04x cmd\n", cmd);
232 #endif
233
234 if (response == NULL) {
235 response = &resp;
236 }
237
238 uint64_t start_time = msclock();
239
240 // Wait until the command is received
241 while (true) {
242 while(getCommand(response)) {
243 if (cmd == CMD_UNKNOWN || response->cmd == cmd) {
244 return true;
245 }
246 }
247
248 if (msclock() - start_time > ms_timeout) {
249 break;
250 }
251
252 if (msclock() - start_time > 2000 && show_warning) {
253 // 2 seconds elapsed (but this doesn't mean the timeout was exceeded)
254 PrintAndLog("Waiting for a response from the proxmark...");
255 PrintAndLog("You can cancel this operation by pressing the pm3 button");
256 show_warning = false;
257 }
258 }
259 return false;
260 }
261
262
263 bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
264 return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
265 }
266
267 bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
268 return WaitForResponseTimeoutW(cmd, response, -1, true);
269 }
270
Impressum, Datenschutz