]> git.zerfleddert.de Git - proxmark3-svn/blame - client/comms.c
USB comm: prepare for @micolous change (PR#463) (#587)
[proxmark3-svn] / client / comms.c
CommitLineData
f5ecd97b 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.
24serial_port sp;
25
26// If TRUE, then there is no active connection to the PM3, and we will drop commands sent.
27bool 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.
34static UsbCommand txcmd;
35volatile 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})
39static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
40
41// Points to the next empty position to write to
42static int cmd_head = 0;
43
44// Points to the position of the last unread command
45static int cmd_tail = 0;
46
47// to lock cmdBuffer operations from different threads
48static pthread_mutex_t cmdBufferMutex = PTHREAD_MUTEX_INITIALIZER;
49
50
51void SendCommand(UsbCommand *c) {
52 #if 0
53 printf("Sending %d bytes\n", sizeof(UsbCommand));
54 #endif
55
56 if (offline) {
57 PrintAndLog("Sending bytes to proxmark failed - offline");
58 return;
59 }
60 /**
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.
63 Not good.../holiman
64 **/
65 while(txcmd_pending);
66 txcmd = *c;
67 txcmd_pending = true;
68}
69
70
71/**
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.
76 */
77void clearCommandBuffer()
78{
79 //This is a very simple operation
80 pthread_mutex_lock(&cmdBufferMutex);
81 cmd_tail = cmd_head;
82 pthread_mutex_unlock(&cmdBufferMutex);
83}
84
85/**
86 * @brief storeCommand stores a USB command in a circular buffer
87 * @param UC
88 */
89void storeCommand(UsbCommand *command)
90{
91 pthread_mutex_lock(&cmdBufferMutex);
92 if( (cmd_head+1) % CMD_BUFFER_SIZE == cmd_tail)
93 {
94 // If these two are equal, we're about to overwrite in the
95 // circular buffer.
96 PrintAndLog("WARNING: Command buffer about to overwrite command! This needs to be fixed!");
97 }
98
99 // Store the command at the 'head' location
100 UsbCommand* destination = &cmdBuffer[cmd_head];
101 memcpy(destination, command, sizeof(UsbCommand));
102
103 cmd_head = (cmd_head +1) % CMD_BUFFER_SIZE; //increment head and wrap
104 pthread_mutex_unlock(&cmdBufferMutex);
105}
106
107
108/**
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
112 */
113int getCommand(UsbCommand* response)
114{
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);
119 return 0;
120 }
121
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;
127
128 pthread_mutex_unlock(&cmdBufferMutex);
129 return 1;
130}
131
132
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//-----------------------------------------------------------------------------
137void UsbCommandReceived(UsbCommand *UC)
138{
139 switch(UC->cmd) {
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);
147 return;
148 } break;
149
150 case CMD_DEBUG_PRINT_INTEGERS: {
151 PrintAndLog("#db# %08x, %08x, %08x \r\n", UC->arg[0], UC->arg[1], UC->arg[2]);
152 return;
153 } break;
154
155 case CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K: {
156 memcpy(sample_buf+(UC->arg[0]),UC->d.asBytes,UC->arg[1]);
157 return;
158 } break;
159
160 default:
161 storeCommand(UC);
162 break;
163 }
164
165}
166
167
168void
169#ifdef __has_attribute
170#if __has_attribute(force_align_arg_pointer)
171__attribute__((force_align_arg_pointer))
172#endif
173#endif
174*uart_receiver(void *targ) {
175 receiver_arg *arg = (receiver_arg*)targ;
176 size_t rxlen;
177 uint8_t rx[sizeof(UsbCommand)];
178 uint8_t *prx = rx;
179
180 while (arg->run) {
181 rxlen = 0;
182 if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
183 prx += rxlen;
184 if (prx-rx < sizeof(UsbCommand)) {
185 continue;
186 }
187 UsbCommandReceived((UsbCommand*)rx);
188 }
189 prx = rx;
190
191 if(txcmd_pending) {
192 if (!uart_send(sp, (uint8_t*) &txcmd, sizeof(UsbCommand))) {
193 PrintAndLog("Sending bytes to proxmark failed");
194 }
195 txcmd_pending = false;
196 }
197 }
198
199 pthread_exit(NULL);
200 return NULL;
201}
202
203
204/**
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.
210 * @param ms_timeout
211 * @return true if command was returned, otherwise false
212 */
213bool WaitForResponseTimeoutW(uint32_t cmd, UsbCommand* response, size_t ms_timeout, bool show_warning) {
214
215 UsbCommand resp;
216
217 if (response == NULL) {
218 response = &resp;
219 }
220
221 uint64_t start_time = msclock();
222
223 // Wait until the command is received
224 while (true) {
225 while(getCommand(response)) {
226 if(response->cmd == cmd){
227 return true;
228 }
229 }
230
231 if (msclock() - start_time > ms_timeout) {
232 break;
233 }
234
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;
239 }
240 }
241 return false;
242}
243
244
245bool WaitForResponseTimeout(uint32_t cmd, UsbCommand* response, size_t ms_timeout) {
246 return WaitForResponseTimeoutW(cmd, response, ms_timeout, true);
247}
248
249bool WaitForResponse(uint32_t cmd, UsbCommand* response) {
250 return WaitForResponseTimeoutW(cmd, response, -1, true);
251}
252
Impressum, Datenschutz