diff --git a/armsrc/appmain.c b/armsrc/appmain.c index d453fe01e..3d205ac11 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1562,30 +1562,15 @@ void __attribute__((noreturn)) AppMain(void) { for (;;) { WDT_HIT(); - // Check if there is a usb packet available - if (usb_poll_validate_length()) { - PacketCommandNG rx; - if (receive_ng(&rx) == PM3_SUCCESS) { - PacketReceived(&rx); - } else { - Dbprintf("Error in frame reception"); - // TODO DOEGOX if error, shall we resync ? - } - } -#ifdef WITH_FPC_HOST - // Check if there is a FPC packet available -// TODO DOEGOX NG packets support here too + // Check if there is a packet available PacketCommandNG rx; - if (usart_readbuffer((uint8_t *)&rx)) { - reply_via_fpc = true; - rx.ng = false; - rx.magic = 0; - rx.length = USB_CMD_DATA_SIZE; - rx.crc = 0; + int16_t ret = receive_ng(&rx); + if (ret == PM3_SUCCESS) { PacketReceived(&rx); + } else if (ret != PM3_NODATA) { + Dbprintf("Error in frame reception"); + // TODO DOEGOX if error, shall we resync ? } - usart_readcheck((uint8_t *)&rx, sizeof(rx)); -#endif // Press button for one second to enter a possible standalone mode if (BUTTON_HELD(1000) > 0) { diff --git a/common/cmd.c b/common/cmd.c index f2dbe5b31..b08d88589 100644 --- a/common/cmd.c +++ b/common/cmd.c @@ -135,9 +135,11 @@ int16_t reply_ng(uint16_t cmd, int16_t status, uint8_t *data, size_t len) { return sendlen; } -int16_t receive_ng(PacketCommandNG *rx) { +static int16_t receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool fpc) { PacketCommandNGRaw rx_raw; - size_t bytes = usb_read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble)); + size_t bytes = read_ng((uint8_t *)&rx_raw.pre, sizeof(PacketCommandNGPreamble)); + if (bytes == 0) + return PM3_NODATA; if (bytes != sizeof(PacketCommandNGPreamble)) return PM3_EIO; rx->magic = rx_raw.pre.magic; @@ -147,12 +149,12 @@ int16_t receive_ng(PacketCommandNG *rx) { if (rx->length > USB_CMD_DATA_SIZE) return PM3_EOVFLOW; // Get the core and variable length payload - bytes = usb_read_ng((uint8_t *)&rx_raw.data, rx->length); + bytes = read_ng((uint8_t *)&rx_raw.data, rx->length); if (bytes != rx->length) return PM3_EIO; memcpy(rx->data.asBytes, rx_raw.data, rx->length); // Get the postamble - bytes = usb_read_ng((uint8_t *)&rx_raw.foopost, sizeof(PacketCommandNGPostamble)); + bytes = read_ng((uint8_t *)&rx_raw.foopost, sizeof(PacketCommandNGPostamble)); if (bytes != sizeof(PacketCommandNGPostamble)) return PM3_EIO; // Check CRC, accept MAGIC as placeholder @@ -163,15 +165,15 @@ int16_t receive_ng(PacketCommandNG *rx) { if ((first << 8) + second != rx->crc) return PM3_EIO; } - reply_via_fpc = false; + reply_via_fpc = fpc; rx->ng = true; } else { // Old style command PacketCommandOLD rx_old; memcpy(&rx_old, &rx_raw.pre, sizeof(PacketCommandNGPreamble)); - bytes = usb_read_ng(((uint8_t *)&rx_old) + sizeof(PacketCommandNGPreamble), sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)); + bytes = read_ng(((uint8_t *)&rx_old) + sizeof(PacketCommandNGPreamble), sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)); if (bytes != sizeof(PacketCommandOLD) - sizeof(PacketCommandNGPreamble)) return PM3_EIO; - reply_via_fpc = false; + reply_via_fpc = fpc; rx->ng = false; rx->magic = 0; rx->crc = 0; @@ -184,3 +186,17 @@ int16_t receive_ng(PacketCommandNG *rx) { } return PM3_SUCCESS; } + +int16_t receive_ng(PacketCommandNG *rx) { + + // Check if there is a packet available + if (usb_poll_validate_length()) + return receive_ng_internal(rx, usb_read_ng, false); + +#ifdef WITH_FPC_HOST + // Check if there is a FPC packet available + return receive_ng_internal(rx, usart_read_ng, true); +#else + return PM3_NODATA; +#endif +} diff --git a/common/usart.c b/common/usart.c index 9ac1b2c8d..928a0ee14 100644 --- a/common/usart.c +++ b/common/usart.c @@ -100,6 +100,11 @@ inline int16_t usart_readcommand(uint8_t *data) { return 0; } +uint32_t usart_read_ng(uint8_t *data, size_t len) { + // TODO DOEGOX + return 0; +} + inline bool usart_commandavailable(void) { return (pUS1->US_RCR == 0); } diff --git a/common/usart.h b/common/usart.h index 9200d8731..16d0d8773 100644 --- a/common/usart.h +++ b/common/usart.h @@ -14,6 +14,7 @@ int16_t usart_writebuffer(uint8_t *data, size_t len); bool usart_dataavailable(void); int16_t usart_readcommand(uint8_t *data); void usart_readcheck(uint8_t *data, size_t len); +uint32_t usart_read_ng(uint8_t *data, size_t len); bool usart_commandavailable(void); #endif diff --git a/include/usb_cmd.h b/include/usb_cmd.h index 9e060e80c..09b864a28 100644 --- a/include/usb_cmd.h +++ b/include/usb_cmd.h @@ -421,6 +421,8 @@ typedef struct { #define PM3_EMALLOC -12 // File error #define PM3_EFILE -13 +// No data +#define PM3_NODATA -98 // Quit program #define PM3_EFATAL -99