From 2f1061f95b277a0fc621b52538de9c3c623c07ee Mon Sep 17 00:00:00 2001
From: Philippe Teuwen <phil@teuwen.org>
Date: Tue, 7 May 2019 23:35:09 +0200
Subject: [PATCH] rework ping

---
 armsrc/appmain.c     |  6 +-----
 client/cmdhficlass.c |  4 ++--
 client/cmdhfmf.c     |  2 +-
 client/cmdhw.c       | 30 ++++++++++++------------------
 client/cmdlfawid.c   |  8 ++++----
 client/cmdlfhid.c    |  8 ++++----
 client/comms.c       |  1 -
 client/scripting.c   |  2 +-
 8 files changed, 25 insertions(+), 36 deletions(-)

diff --git a/armsrc/appmain.c b/armsrc/appmain.c
index 89b5d1cf1..99aeca262 100644
--- a/armsrc/appmain.c
+++ b/armsrc/appmain.c
@@ -1521,11 +1521,7 @@ static void PacketReceived(PacketCommandNG *packet) {
         case CMD_CAPABILITIES:
             SendCapabilities();
         case CMD_PING:
-            if (packet->ng) {
-                reply_ng(CMD_PING, PM3_SUCCESS, packet->data.asBytes, packet->length);
-            } else {
-                reply_mix(CMD_ACK, reply_via_fpc, 0, 0, 0, 0);
-            }
+            reply_ng(CMD_PING, PM3_SUCCESS, packet->data.asBytes, packet->length);
             break;
 #ifdef WITH_LCD
         case CMD_LCD_RESET:
diff --git a/client/cmdhficlass.c b/client/cmdhficlass.c
index 023cf9107..ddafd5a46 100644
--- a/client/cmdhficlass.c
+++ b/client/cmdhficlass.c
@@ -606,7 +606,7 @@ static int CmdHFiClassELoad(const char *Cmd) {
     // Disable fast mode and send a dummy command to make it effective
     conn.block_after_ACK = false;
     SendCommandNG(CMD_PING, NULL, 0);
-    WaitForResponseTimeout(CMD_ACK, NULL, 1000);
+    WaitForResponseTimeout(CMD_PING, NULL, 1000);
 
     PrintAndLogEx(SUCCESS, "sent %d bytes of data to device emulator memory", bytes_sent);
     return 0;
@@ -2038,7 +2038,7 @@ out:
     // Disable fast mode and send a dummy command to make it effective
     conn.block_after_ACK = false;
     SendCommandNG(CMD_PING, NULL, 0);
-    WaitForResponseTimeout(CMD_ACK, NULL, 1000);
+    WaitForResponseTimeout(CMD_PING, NULL, 1000);
 
     DropField();
     free(pre);
diff --git a/client/cmdhfmf.c b/client/cmdhfmf.c
index 82eb6c208..9f127917a 100644
--- a/client/cmdhfmf.c
+++ b/client/cmdhfmf.c
@@ -2027,7 +2027,7 @@ out:
     // Disable fast mode and send a dummy command to make it effective
     conn.block_after_ACK = false;
     SendCommandNG(CMD_PING, NULL, 0);
-    WaitForResponseTimeout(CMD_ACK, NULL, 1000);
+    WaitForResponseTimeout(CMD_PING, NULL, 1000);
 
 
     if (createDumpFile) {
diff --git a/client/cmdhw.c b/client/cmdhw.c
index 4eda2dc66..51ae266fc 100644
--- a/client/cmdhw.c
+++ b/client/cmdhw.c
@@ -465,22 +465,14 @@ static int CmdStatus(const char *Cmd) {
 }
 
 static int CmdPing(const char *Cmd) {
-    (void)Cmd; // Cmd is not used so far
-    clearCommandBuffer();
-    PacketResponseNG resp;
-    SendCommandNG(CMD_PING, NULL, 0);
-    if (WaitForResponseTimeout(CMD_ACK, &resp, 1000))
-        PrintAndLogEx(SUCCESS, "Ping " _GREEN_("successful"));
-    else
-        PrintAndLogEx(WARNING, "Ping " _RED_("failed"));
-    return PM3_SUCCESS;
-}
-
-static int CmdPingNG(const char *Cmd) {
     uint32_t len = strtol(Cmd, NULL, 0);
     if (len > PM3_CMD_DATA_SIZE)
         len = PM3_CMD_DATA_SIZE;
-    PrintAndLogEx(INFO, "PingNG sent with payload len=%d", len);
+    if (len) {
+        PrintAndLogEx(INFO, "Ping sent with payload len=%d", len);
+    } else {
+        PrintAndLogEx(INFO, "Ping sent");
+    }
     clearCommandBuffer();
     PacketResponseNG resp;
     uint8_t data[PM3_CMD_DATA_SIZE] = {0};
@@ -489,11 +481,14 @@ static int CmdPingNG(const char *Cmd) {
     SendCommandNG(CMD_PING, data, len);
     if (WaitForResponseTimeout(CMD_PING, &resp, 1000)) {
         bool error = false;
-        if (len)
+        if (len) {
             error = memcmp(data, resp.data.asBytes, len) != 0;
-        PrintAndLogEx((error)? ERR:SUCCESS, "PingNG response received, content is %s", error ? _RED_("NOT ok") : _GREEN_("ok"));
+            PrintAndLogEx((error)? ERR:SUCCESS, "Ping response " _GREEN_("received") "and content is %s", error ? _RED_("NOT ok") : _GREEN_("ok"));
+        } else {
+            PrintAndLogEx((error)? ERR:SUCCESS, "Ping response " _GREEN_("received"));
+        }
     } else
-        PrintAndLogEx(WARNING, "PingNG response " _RED_("timeout"));
+        PrintAndLogEx(WARNING, "Ping response " _RED_("timeout"));
     return PM3_SUCCESS;
 }
 
@@ -538,8 +533,7 @@ static command_t CommandTable[] = {
     {"fpgaoff",       CmdFPGAOff,     IfPm3Present,    "Set FPGA off"},
     {"lcd",           CmdLCD,         IfPm3Lcd,        "<HEX command> <count> -- Send command/data to LCD"},
     {"lcdreset",      CmdLCDReset,    IfPm3Lcd,        "Hardware reset LCD"},
-    {"ping",          CmdPing,        IfPm3Present,    "Test if the Proxmark3 is responding"},
-    {"pingng",        CmdPingNG,      IfPm3Present,    "Test if the Proxmark3 is responsive, using new frame format (experimental)"},
+    {"ping",          CmdPing,        IfPm3Present,    "Test if the Proxmark3 is responsive"},
     {"readmem",       CmdReadmem,     IfPm3Present,    "[address] -- Read memory at decimal address from flash"},
     {"reset",         CmdReset,       IfPm3Present,    "Reset the Proxmark3"},
     {"setlfdivisor",  CmdSetDivisor,  IfPm3Present,    "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"},
diff --git a/client/cmdlfawid.c b/client/cmdlfawid.c
index 8e29e77bf..946656276 100644
--- a/client/cmdlfawid.c
+++ b/client/cmdlfawid.c
@@ -87,12 +87,12 @@ static int usage_lf_awid_brute(void) {
 }
 
 static bool sendPing(void) {
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
     clearCommandBuffer();
     PacketResponseNG resp;
-    if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000))
+    if (!WaitForResponseTimeout(CMD_PING, &resp, 1000))
         return false;
     return true;
 }
diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c
index fc9decced..b712cb50f 100644
--- a/client/cmdlfhid.c
+++ b/client/cmdlfhid.c
@@ -93,12 +93,12 @@ static int usage_lf_hid_brute(void) {
 
 // sending three times.  Didn't seem to break the previous sim?
 static bool sendPing(void) {
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
-    SendCommandMIX(CMD_PING, 1, 2, 3, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
+    SendCommandNG(CMD_PING, NULL, 0);
     clearCommandBuffer();
     PacketResponseNG resp;
-    if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000))
+    if (!WaitForResponseTimeout(CMD_PING, &resp, 1000))
         return false;
     return true;
 }
diff --git a/client/comms.c b/client/comms.c
index 504f40c9e..c48a1bcdd 100644
--- a/client/comms.c
+++ b/client/comms.c
@@ -608,7 +608,6 @@ int TestProxmark(void) {
             return PM3_EIO;
 
         SendCommandNG(CMD_CAPABILITIES, NULL, 0);
-        
         if (WaitForResponseTimeoutW(CMD_PING, &resp, 1000, false)) {
             memcpy(&pm3_capabilities, resp.data.asBytes, resp.length);
             conn.send_via_fpc_usart = pm3_capabilities.via_fpc;
diff --git a/client/scripting.c b/client/scripting.c
index c8fa2f163..91fc5b427 100644
--- a/client/scripting.c
+++ b/client/scripting.c
@@ -46,7 +46,7 @@ static int l_fast_push_mode(lua_State *L) {
     // Disable fast mode and send a dummy command to make it effective
     if (enable == false) {
         SendCommandNG(CMD_PING, NULL, 0);
-        WaitForResponseTimeout(CMD_ACK, NULL, 1000);
+        WaitForResponseTimeout(CMD_PING, NULL, 1000);
     }
 
     //Push the retval on the stack