mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-22 14:13:42 -07:00
First try at disconnect/reconnect detection. No idea if it's going to work on WIN32.
This commit is contained in:
parent
bc3b2f7f94
commit
96aab0688a
4 changed files with 71 additions and 13 deletions
|
@ -405,11 +405,19 @@ int CmdTune(const char *Cmd)
|
||||||
|
|
||||||
int CmdVersion(const char *Cmd)
|
int CmdVersion(const char *Cmd)
|
||||||
{
|
{
|
||||||
|
return CmdVersionC(Cmd, false);
|
||||||
|
}
|
||||||
|
|
||||||
clearCommandBuffer();
|
int CmdVersionC(const char *Cmd, const bool no_cache)
|
||||||
|
{
|
||||||
UsbCommand c = {CMD_VERSION};
|
UsbCommand c = {CMD_VERSION};
|
||||||
static UsbCommand resp = {0, {0, 0, 0}};
|
static UsbCommand resp = {0, {0, 0, 0}};
|
||||||
|
|
||||||
|
if( no_cache )
|
||||||
|
memset( &resp, 0, sizeof(resp) );
|
||||||
|
else
|
||||||
|
clearCommandBuffer();
|
||||||
|
|
||||||
if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available
|
if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
|
if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
|
||||||
|
|
|
@ -23,5 +23,6 @@ int CmdSetDivisor(const char *Cmd);
|
||||||
int CmdSetMux(const char *Cmd);
|
int CmdSetMux(const char *Cmd);
|
||||||
int CmdTune(const char *Cmd);
|
int CmdTune(const char *Cmd);
|
||||||
int CmdVersion(const char *Cmd);
|
int CmdVersion(const char *Cmd);
|
||||||
|
int CmdVersionC(const char *Cmd, const bool no_cache);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
pthread_mutex_t print_lock;
|
pthread_mutex_t print_lock;
|
||||||
|
|
||||||
static serial_port sp;
|
static serial_port sp;
|
||||||
|
static char* sp_name;
|
||||||
static UsbCommand txcmd;
|
static UsbCommand txcmd;
|
||||||
volatile static bool txcmd_pending = false;
|
volatile static bool txcmd_pending = false;
|
||||||
|
|
||||||
|
@ -70,16 +71,63 @@ byte_t* prx = rx;
|
||||||
static void *uart_receiver(void *targ) {
|
static void *uart_receiver(void *targ) {
|
||||||
struct receiver_arg *arg = (struct receiver_arg*)targ;
|
struct receiver_arg *arg = (struct receiver_arg*)targ;
|
||||||
size_t rxlen;
|
size_t rxlen;
|
||||||
|
bool need_reconnect = false;
|
||||||
|
UsbCommand version_cmd = {CMD_VERSION};
|
||||||
|
static bool request_version = false;
|
||||||
|
|
||||||
while (arg->run) {
|
while (arg->run) {
|
||||||
rxlen = 0;
|
if( need_reconnect ) {
|
||||||
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) && rxlen) {
|
sp = uart_open(sp_name);
|
||||||
prx += rxlen;
|
|
||||||
if (prx-rx < sizeof(UsbCommand)) {
|
if( sp == INVALID_SERIAL_PORT || sp == CLAIMED_SERIAL_PORT )
|
||||||
|
{
|
||||||
|
PrintAndLog("Reconnect failed, retrying...");
|
||||||
|
|
||||||
|
if( txcmd_pending ) {
|
||||||
|
PrintAndLog("Cannot send bytes to offline proxmark");
|
||||||
|
txcmd_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep(2);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
UsbCommandReceived((UsbCommand*)rx);
|
|
||||||
|
PrintAndLog("Proxmark reconnected!");
|
||||||
|
need_reconnect = false;
|
||||||
|
offline = 0;
|
||||||
|
//CmdVersionW(NULL, true);
|
||||||
|
clearCommandBuffer();
|
||||||
|
uart_send(sp, (byte_t*) &version_cmd, sizeof(UsbCommand)); // request it from the HW
|
||||||
|
request_version = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rxlen = 0;
|
||||||
|
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-rx), &rxlen) ) {
|
||||||
|
if( rxlen ) {
|
||||||
|
prx += rxlen;
|
||||||
|
if (prx-rx < sizeof(UsbCommand)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
UsbCommandReceived((UsbCommand*)rx);
|
||||||
|
|
||||||
|
if( request_version && ((UsbCommand*)rx)->cmd == CMD_ACK)
|
||||||
|
{
|
||||||
|
request_version = false;
|
||||||
|
CmdVersionW(NULL, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PrintAndLog("Receiving data from proxmark failed, attempting a reconnect");
|
||||||
|
uart_close(sp);
|
||||||
|
sp = INVALID_SERIAL_PORT;
|
||||||
|
offline = 1;
|
||||||
|
txcmd_pending = false;
|
||||||
|
need_reconnect = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
prx = rx;
|
prx = rx;
|
||||||
|
|
||||||
if(txcmd_pending) {
|
if(txcmd_pending) {
|
||||||
|
@ -351,6 +399,7 @@ int main(int argc, char* argv[]) {
|
||||||
set_my_executable_path();
|
set_my_executable_path();
|
||||||
|
|
||||||
// open uart
|
// open uart
|
||||||
|
sp_name = argv[1];
|
||||||
if (!waitCOMPort) {
|
if (!waitCOMPort) {
|
||||||
sp = uart_open(argv[1]);
|
sp = uart_open(argv[1]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -405,7 +454,7 @@ int main(int argc, char* argv[]) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Clean up the port
|
// Clean up the port
|
||||||
if (usb_present) {
|
if (sp != INVALID_SERIAL_PORT && sp != CLAIMED_SERIAL_PORT) {
|
||||||
uart_close(sp);
|
uart_close(sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -141,7 +141,7 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_
|
||||||
|
|
||||||
// Reset the output count
|
// Reset the output count
|
||||||
*pszRxLen = 0;
|
*pszRxLen = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Reset file descriptor
|
// Reset file descriptor
|
||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
|
@ -153,12 +153,12 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read time-out
|
// Read time-out
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
if (*pszRxLen == 0) {
|
if (*pszRxLen == 0) {
|
||||||
// Error, we received no data
|
// Error, we received no data
|
||||||
return false;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// We received some data, but nothing more is available
|
// We received some data, but nothing more is available
|
||||||
return true;
|
return true;
|
||||||
|
@ -168,7 +168,7 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_
|
||||||
// Retrieve the count of the incoming bytes
|
// Retrieve the count of the incoming bytes
|
||||||
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
|
res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
|
||||||
if (res < 0) return false;
|
if (res < 0) return false;
|
||||||
|
|
||||||
// Cap the number of bytes, so we don't overrun the buffer
|
// Cap the number of bytes, so we don't overrun the buffer
|
||||||
if (pszMaxRxLen - (*pszRxLen) < byteCount) {
|
if (pszMaxRxLen - (*pszRxLen) < byteCount) {
|
||||||
byteCount = pszMaxRxLen - (*pszRxLen);
|
byteCount = pszMaxRxLen - (*pszRxLen);
|
||||||
|
@ -178,8 +178,8 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_
|
||||||
res = read(((serial_port_unix*)sp)->fd, pbtRx+(*pszRxLen), byteCount);
|
res = read(((serial_port_unix*)sp)->fd, pbtRx+(*pszRxLen), byteCount);
|
||||||
|
|
||||||
// Stop if the OS has some troubles reading the data
|
// Stop if the OS has some troubles reading the data
|
||||||
if (res <= 0) return false;
|
if (res < 0) return false;
|
||||||
|
|
||||||
*pszRxLen += res;
|
*pszRxLen += res;
|
||||||
|
|
||||||
if (*pszRxLen == pszMaxRxLen) {
|
if (*pszRxLen == pszMaxRxLen) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue