<commit_before>/*
 * HydraBus/HydraNFC
 *
 * Copyright (C) 2014-2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include <stdio.h> /* sprintf */
#include <string.h> /* memcpy */

#include "ch.h"
#include "hal.h"

#include "mcu.h"
#include "trf797x.h"
#include "types.h"

#include "hydranfc.h"
#include "hydranfc_cmd_sniff_iso14443.h"
#include "hydranfc_cmd_sniff_downsampling.h"

#include "common.h"
#include "microsd.h"
#include "ff.h"
#include "bsp_uart.h"

/* Disable D4 & TST output pin (used only for debug purpose) */
#undef D4_OFF
#define D4_OFF

#undef D4_ON
#define D4_ON

#undef TST_OFF
#define TST_OFF

#undef TST_ON
#define TST_ON

// Statistics/debug info on time spent to write data on UART
//#define STAT_UART_WRITE

#define PROTOCOL_OPTIONS_VERSION (0) /* Versions reserved on 3bits BIT0 to BIT2 */
#define PROTOCOL_OPTIONS_START_OF_FRAME_TIMESTAMP (BIT3) /* Include 32bits start of frame timestamp(1/168MHz increment) at start of each frame */
#define PROTOCOL_OPTIONS_END_OF_FRAME_TIMESTAMP (BIT4) /* Include 32bits end of frame timestamp(1/168MHz increment) at end of each frame */
#define PROTOCOL_OPTIONS_PARITY (BIT5) /* Include an additional byte with parity(0 or 1) after each data byte */
#define PROTOCOL_OPTIONS_RAW (BIT6) /* Raw Data (modulation not decoded) */
/*
 For option PROTOCOL_OPTIONS_RAW
 - Each output byte(8bits) shall represent 1 bit data TypeA or TypeB @106kbps with following Modulation & Bit Coding:
   - PCD to PICC TypeA => Modulation 100% ASK, Bit Coding Modified Miller
   - PICC to PCD TypeA => Modulation OOK, Bit Coding Manchester
   - PCD to PICC TypeB => Modulation 10% ASK, Bit Coding NRZ
   - PICC to PCD TypeB => Modulation BPSK, Bit Coding NRZ - L
 - Note when this option is set PROTOCOL_OPTIONS_PARITY is ignored as in raw mode we have everything ...
*/

#define PROTOCOL_MODULATION_UNKNOWN (0)
#define PROTOCOL_MODULATION_TYPEA_MILLER_MODIFIED_106KBPS (1) // MILLER MODIFIED = PCD / Readed
#define PROTOCOL_MODULATION_TYPEA_MANCHESTER_106KBPS (2) // MILLER MODIFIED = PICC / Tag
#define PROTOCOL_MODULATION_14443AB_RAW_848KBPS (3) // 14443A or B RAW 848KHz

typedef struct {
	uint8_t protocol_options; /* See define PROTOCOL_OPTIONS_XXX */
	uint8_t protocol_modulation; /* See define PROTOCOL_MODULATION_XXX */
	uint16_t data_size; /* Data size (in bytes) in this frame(including this header) */
	/* N Data */
} sniff_14443a_bin_frame_header_t;

/* INIT_NFC_PROTOCOL */
typedef enum {
	ISO14443A = 0,
	ISO14443B
} INIT_NFC_PROTOCOL;

filename_t write_filename;

#define TRF7970_DATA_SIZE (384)
#define SPI_RX_DMA_SIZE (4)
uint8_t spi_rx_dma_buf[2][SPI_RX_DMA_SIZE+12] __attribute__ ((aligned (16)));
uint8_t htoa[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uint8_t tmp_buf[16];
uint8_t irq_no;
volatile int irq_sampling = 0;

static uint32_t old_u32_data, u32_data, old_data_bit;

/*
* SPI1 configuration structure.
* Speed not used in Slave mode, CPHA=0, CPOL=0, 8bits frames, MSb transmitted first.
* The slave select line is internally selected.
*/
static const SPIConfig spi1cfg = {
	NULL, /* spicb, */
	/* HW dependent part.*/
	GPIOA, /* Port */
	15, /* Pin */
	(SPI_CR1_BR_2), /* CR1 Register */
	0 /* CR2 Register */
};

#define CountLeadingZero(x) (__CLZ(x))
#define SWAP32(x) (__REV(x))

uint8_t* sniffer_get_buffer(void)
{
	return &g_sbuf[0];
}

uint32_t sniffer_get_buffer_max_size(void)
{
	return NB_SBUFFER;
}

uint32_t sniffer_get_size(void)
{
	return g_sbuf_idx;
}

void initSPI1(void)
{
	uint32_t i;

	/* Clear buffer */
	for(i=0; i<SPI_RX_DMA_SIZE; i++) {
		spi_rx_dma_buf[0][i]=0;
		spi_rx_dma_buf[1][i]=0;
	}

	/*
	* Initializes the SPI driver 1.
	*/
	spiSlaveStart(&SPID1, &spi1cfg);
	/* SPI DMA Start using double buffer */
	dmaStreamSetMemory0(SPID1.dmarx, spi_rx_dma_buf[0]);
	dmaStreamSetMemory1(SPID1.dmarx, spi_rx_dma_buf[1]);
	dmaStreamSetTransactionSize(SPID1.dmarx, SPI_RX_DMA_SIZE);
	dmaStreamSetMode(SPID1.dmarx, SPID1.rxdmamode | STM32_DMA_CR_MINC |
			 STM32_DMA_CR_EN);
}

void tprint_str(const char *data, uint32_t size)
{
	if (size > 0) {
		if (SDU1.config->usbp->state == USB_ACTIVE)
			chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)data, size);

		if (SDU2.config->usbp->state == USB_ACTIVE)
			chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)data, size);
	}
}

void tprintf(const char *fmt, ...)
{
	va_list va_args;
	int real_size;
#define TPRINTF_BUFF_SIZE (255)
	char tprintf_buff[TPRINTF_BUFF_SIZE+1];

	va_start(va_args, fmt);
	real_size = vsnprintf(tprintf_buff, TPRINTF_BUFF_SIZE, fmt, va_args);

	if (SDU1.config->usbp->state == USB_ACTIVE)
		chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)tprintf_buff, real_size);

	if (SDU2.config->usbp->state == USB_ACTIVE)
		chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)tprintf_buff, real_size);

	va_end(va_args);
}

void initUART1_sniff(void)
{
	mode_config_proto_t mode_conf;
	bsp_status_t uart_bsp_status;

	mode_conf.config.uart.dev_speed = 8400000; /* 8.4Megabauds */
	mode_conf.config.uart.dev_parity = 0; /* 0 No Parity */
	mode_conf.config.uart.dev_stop_bit = 1; /* 1 Stop Bit */
	uart_bsp_status = bsp_uart_init(BSP_DEV_UART1,&mode_conf);
	if(uart_bsp_status == BSP_OK)
	{
		tprintf("Init UART1 8.4Mbauds 8N1 OK\r\nConnect FTDI C232HM-DDHSL-0 RXD(Yellow) to PA9(HydraBus UART1 TXD) for live sniffing session\r\n");
	}else
	{
		tprintf("Init UART1 6Mbauds 8N1 Error:%d\r\n", uart_bsp_status);
	}
}

void deinitUART1_sniff(void)
{
	bsp_uart_deinit(BSP_DEV_UART1);
}

/* Stop driver to exit Sniff NFC mode */
void terminate_sniff_nfc(void)
{
	spiStop(&SPID1);
}<commit_msg>HydraNFC sniffer fix UART Init missing bus_mode configuration set to BSP_UART_MODE_UART<commit_after>/*
 * HydraBus/HydraNFC
 *
 * Copyright (C) 2014-2015 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdarg.h>
#include <stdio.h> /* sprintf */
#include <string.h> /* memcpy */

#include "ch.h"
#include "hal.h"

#include "mcu.h"
#include "trf797x.h"
#include "types.h"

#include "hydranfc.h"
#include "hydranfc_cmd_sniff_iso14443.h"
#include "hydranfc_cmd_sniff_downsampling.h"

#include "common.h"
#include "microsd.h"
#include "ff.h"
#include "bsp_uart.h"

/* Disable D4 & TST output pin (used only for debug purpose) */
#undef D4_OFF
#define D4_OFF

#undef D4_ON
#define D4_ON

#undef TST_OFF
#define TST_OFF

#undef TST_ON
#define TST_ON

// Statistics/debug info on time spent to write data on UART
//#define STAT_UART_WRITE

#define PROTOCOL_OPTIONS_VERSION (0) /* Versions reserved on 3bits BIT0 to BIT2 */
#define PROTOCOL_OPTIONS_START_OF_FRAME_TIMESTAMP (BIT3) /* Include 32bits start of frame timestamp(1/168MHz increment) at start of each frame */
#define PROTOCOL_OPTIONS_END_OF_FRAME_TIMESTAMP (BIT4) /* Include 32bits end of frame timestamp(1/168MHz increment) at end of each frame */
#define PROTOCOL_OPTIONS_PARITY (BIT5) /* Include an additional byte with parity(0 or 1) after each data byte */
#define PROTOCOL_OPTIONS_RAW (BIT6) /* Raw Data (modulation not decoded) */
/*
 For option PROTOCOL_OPTIONS_RAW
 - Each output byte(8bits) shall represent 1 bit data TypeA or TypeB @106kbps with following Modulation & Bit Coding:
   - PCD to PICC TypeA => Modulation 100% ASK, Bit Coding Modified Miller
   - PICC to PCD TypeA => Modulation OOK, Bit Coding Manchester
   - PCD to PICC TypeB => Modulation 10% ASK, Bit Coding NRZ
   - PICC to PCD TypeB => Modulation BPSK, Bit Coding NRZ - L
 - Note when this option is set PROTOCOL_OPTIONS_PARITY is ignored as in raw mode we have everything ...
*/

#define PROTOCOL_MODULATION_UNKNOWN (0)
#define PROTOCOL_MODULATION_TYPEA_MILLER_MODIFIED_106KBPS (1) // MILLER MODIFIED = PCD / Readed
#define PROTOCOL_MODULATION_TYPEA_MANCHESTER_106KBPS (2) // MILLER MODIFIED = PICC / Tag
#define PROTOCOL_MODULATION_14443AB_RAW_848KBPS (3) // 14443A or B RAW 848KHz

typedef struct {
	uint8_t protocol_options; /* See define PROTOCOL_OPTIONS_XXX */
	uint8_t protocol_modulation; /* See define PROTOCOL_MODULATION_XXX */
	uint16_t data_size; /* Data size (in bytes) in this frame(including this header) */
	/* N Data */
} sniff_14443a_bin_frame_header_t;

/* INIT_NFC_PROTOCOL */
typedef enum {
	ISO14443A = 0,
	ISO14443B
} INIT_NFC_PROTOCOL;

filename_t write_filename;

#define TRF7970_DATA_SIZE (384)
#define SPI_RX_DMA_SIZE (4)
uint8_t spi_rx_dma_buf[2][SPI_RX_DMA_SIZE+12] __attribute__ ((aligned (16)));
uint8_t htoa[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uint8_t tmp_buf[16];
uint8_t irq_no;
volatile int irq_sampling = 0;

static uint32_t old_u32_data, u32_data, old_data_bit;

/*
* SPI1 configuration structure.
* Speed not used in Slave mode, CPHA=0, CPOL=0, 8bits frames, MSb transmitted first.
* The slave select line is internally selected.
*/
static const SPIConfig spi1cfg = {
	NULL, /* spicb, */
	/* HW dependent part.*/
	GPIOA, /* Port */
	15, /* Pin */
	(SPI_CR1_BR_2), /* CR1 Register */
	0 /* CR2 Register */
};

#define CountLeadingZero(x) (__CLZ(x))
#define SWAP32(x) (__REV(x))

uint8_t* sniffer_get_buffer(void)
{
	return &g_sbuf[0];
}

uint32_t sniffer_get_buffer_max_size(void)
{
	return NB_SBUFFER;
}

uint32_t sniffer_get_size(void)
{
	return g_sbuf_idx;
}

void initSPI1(void)
{
	uint32_t i;

	/* Clear buffer */
	for(i=0; i<SPI_RX_DMA_SIZE; i++) {
		spi_rx_dma_buf[0][i]=0;
		spi_rx_dma_buf[1][i]=0;
	}

	/*
	* Initializes the SPI driver 1.
	*/
	spiSlaveStart(&SPID1, &spi1cfg);
	/* SPI DMA Start using double buffer */
	dmaStreamSetMemory0(SPID1.dmarx, spi_rx_dma_buf[0]);
	dmaStreamSetMemory1(SPID1.dmarx, spi_rx_dma_buf[1]);
	dmaStreamSetTransactionSize(SPID1.dmarx, SPI_RX_DMA_SIZE);
	dmaStreamSetMode(SPID1.dmarx, SPID1.rxdmamode | STM32_DMA_CR_MINC |
			 STM32_DMA_CR_EN);
}

void tprint_str(const char *data, uint32_t size)
{
	if (size > 0) {
		if (SDU1.config->usbp->state == USB_ACTIVE)
			chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)data, size);

		if (SDU2.config->usbp->state == USB_ACTIVE)
			chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)data, size);
	}
}

void tprintf(const char *fmt, ...)
{
	va_list va_args;
	int real_size;
#define TPRINTF_BUFF_SIZE (255)
	char tprintf_buff[TPRINTF_BUFF_SIZE+1];

	va_start(va_args, fmt);
	real_size = vsnprintf(tprintf_buff, TPRINTF_BUFF_SIZE, fmt, va_args);

	if (SDU1.config->usbp->state == USB_ACTIVE)
		chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)tprintf_buff, real_size);

	if (SDU2.config->usbp->state == USB_ACTIVE)
		chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)tprintf_buff, real_size);

	va_end(va_args);
}

void initUART1_sniff(void)
{
	mode_config_proto_t mode_conf;
	bsp_status_t uart_bsp_status;

	mode_conf.config.uart.dev_speed = 8400000; /* 8.4Megabauds */
	mode_conf.config.uart.dev_parity = 0; /* 0 No Parity */
	mode_conf.config.uart.dev_stop_bit = 1; /* 1 Stop Bit */
	mode_conf.config.uart.bus_mode = BSP_UART_MODE_UART;
	uart_bsp_status = bsp_uart_init(BSP_DEV_UART1,&mode_conf);
	if(uart_bsp_status == BSP_OK)
	{
		tprintf("Init UART1 8.4Mbauds 8N1 OK\r\nConnect FTDI C232HM-DDHSL-0 RXD(Yellow) to PA9(HydraBus UART1 TXD) for live sniffing session\r\n");
	}else
	{
		tprintf("Init UART1 6Mbauds 8N1 Error:%d\r\n", uart_bsp_status);
	}
}

void deinitUART1_sniff(void)
{
	bsp_uart_deinit(BSP_DEV_UART1);
}

/* Stop driver to exit Sniff NFC mode */
void terminate_sniff_nfc(void)
{
	spiStop(&SPID1);
}
{'fraction_non_alphanumeric': 0.11959514170040486, 'fraction_numerical': 0.038380566801619435, 'mean_word_length': 4.90956937799043, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988181', 'n_tokens_mistral': 5179, 'n_tokens_neox': 4871, 'n_words': 1194}