new frame format, smaller and with crc. Some code simplified

This commit is contained in:
Philippe Teuwen
2019-04-16 20:00:17 +02:00
parent 34467b7550
commit 44bbb7d2c7
7 changed files with 70 additions and 52 deletions

View File

@@ -119,6 +119,7 @@ CORESRCS = uart_posix.c \
util.c \
util_posix.c \
scandir.c \
crc16.c \
comms.c
CMDSRCS = crapto1/crapto1.c \
@@ -143,7 +144,6 @@ CMDSRCS = crapto1/crapto1.c \
mifare/mifarehost.c \
parity.c \
crc.c \
crc16.c \
crc64.c \
legic_prng.c \
iso15693tools.c \

View File

@@ -445,12 +445,13 @@ static int CmdPingNG(const char *Cmd) {
PrintAndLogEx(NORMAL, "Pinging with payload len=%d", len);
clearCommandBuffer();
UsbCommand resp;
UsbCommand c = {CMD_PING, {len, 0, 0}, {{0}}};
uint8_t data[USB_CMD_DATA_SIZE] = {0};
uint16_t cmd = CMD_PING;
if (len >= 4)
c.d.asDwords[0] = 0xAABBCCDD;
((uint32_t *)data)[0]=0xAABBCCDD;
if (len >= 8)
c.d.asDwords[(len-1)/4] = 0xDDCCBBAA;
SendCommandNG(&c, len);
((uint32_t *)data)[(len-1)/4] = 0xDDCCBBAA;
SendCommandNG(cmd, data, len);
if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
PrintAndLogEx(NORMAL, "PingNG successful");
if (len >= 4)

View File

@@ -10,6 +10,7 @@
//-----------------------------------------------------------------------------
#include "comms.h"
#include "crc16.h"
// Serial port that we are communicating with the PM3 on.
static serial_port sp = NULL;
@@ -25,7 +26,7 @@ static pthread_t USB_communication_thread;
// Transmit buffer.
static UsbCommand txBuffer;
static uint8_t txBufferNG[sizeof(UsbCommandNGPreamble) + sizeof(UsbCommand) + sizeof(UsbCommandNGPostamble)];
static uint8_t txBufferNG[USB_PACKET_NG_MAXLEN];
size_t txBufferNGLen;
static bool txBuffer_pending = false;
static pthread_mutex_t txBufferMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -86,10 +87,10 @@ void SendCommand(UsbCommand *c) {
//__atomic_test_and_set(&txcmd_pending, __ATOMIC_SEQ_CST);
}
void SendCommandNG(UsbCommand *c, size_t len) {
void SendCommandNG(uint16_t cmd, uint8_t* data, size_t len) {
#ifdef COMMS_DEBUG
PrintAndLogEx(NORMAL, "Sending %d bytes of payload | cmd %04x\n", len, c->cmd);
PrintAndLogEx(NORMAL, "Sending %d bytes of payload | cmd %04x\n", len, cmd);
#endif
if (offline) {
@@ -102,7 +103,7 @@ void SendCommandNG(UsbCommand *c, size_t len) {
}
UsbCommandNGPreamble *tx_pre = (UsbCommandNGPreamble *)txBufferNG;
UsbCommandNGPostamble *tx_post = (UsbCommandNGPostamble *)(txBufferNG + sizeof(UsbCommandNGPreamble) + sizeof(UsbCommandNG) + len);
UsbCommandNGPostamble *tx_post = (UsbCommandNGPostamble *)(txBufferNG + sizeof(UsbCommandNGPreamble) + len);
pthread_mutex_lock(&txBufferMutex);
/**
@@ -116,10 +117,12 @@ void SendCommandNG(UsbCommand *c, size_t len) {
tx_pre->magic = USB_PREAMBLE_MAGIC;
tx_pre->length = len;
memcpy(txBufferNG + sizeof(UsbCommandNGPreamble), c, sizeof(UsbCommandNG) + len);
// TODO CRC
tx_post->magic = USB_POSTAMBLE_MAGIC;
txBufferNGLen = sizeof(UsbCommandNGPreamble) + sizeof(UsbCommandNG) + len + sizeof(UsbCommandNGPostamble);
tx_pre->cmd = cmd;
memcpy(txBufferNG + sizeof(UsbCommandNGPreamble), data, len);
uint8_t first, second;
compute_crc(CRC_14443_A, txBufferNG, sizeof(UsbCommandNGPreamble) + len, &first, &second);
tx_post->crc = (first << 8) + second;
txBufferNGLen = sizeof(UsbCommandNGPreamble) + len + sizeof(UsbCommandNGPostamble);
txBuffer_pending = true;
// tell communication thread that a new command can be send

View File

@@ -52,7 +52,7 @@ bool IsOffline(void);
void *uart_receiver(void *targ);
void SendCommand(UsbCommand *c);
void SendCommandNG(UsbCommand *c, size_t len);
void SendCommandNG(uint16_t cmd, uint8_t* data, size_t len);
void clearCommandBuffer(void);
#define FLASHMODE_SPEED 460800