mirror of
https://gitea.osmocom.org/sim-card/simtrace2.git
synced 2026-03-17 05:38:33 +03:00
Sniffer with ringbuf, works SOMETIMES
* Sniffer uses phone endpoints for communication and the ringbuffer
routines the phone communication uses
* Most times the Usart1 interrupt is not triggered, and therefore
no values are recorded
This commit is contained in:
42
firmware/src_simtrace/host_communication.c
Normal file
42
firmware/src_simtrace/host_communication.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "board.h"
|
||||
|
||||
static volatile bool write_to_host_in_progress = false;
|
||||
|
||||
void USB_write_callback(uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
|
||||
{
|
||||
if (status != USBD_STATUS_SUCCESS) {
|
||||
TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status);
|
||||
}
|
||||
write_to_host_in_progress = false;
|
||||
printf("WR_CB\n");
|
||||
}
|
||||
|
||||
int send_to_host()
|
||||
{
|
||||
static uint8_t msg[RING_BUFLEN];
|
||||
int ret = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; !rbuf_is_empty(&sim_rcv_buf) && i < sizeof(msg); i++) {
|
||||
msg[i] = rbuf_read(&sim_rcv_buf);
|
||||
}
|
||||
printf("Wr %d\n", i);
|
||||
write_to_host_in_progress = true;
|
||||
ret = USBD_Write( PHONE_DATAIN, msg, i, (TransferCallback)&USB_write_callback, 0 );
|
||||
if (ret != USBD_STATUS_SUCCESS) {
|
||||
TRACE_ERROR("Error sending to host (%x)\n", ret);
|
||||
write_to_host_in_progress = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int check_data_from_phone()
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if((rbuf_is_empty(&sim_rcv_buf) || write_to_host_in_progress == true)) {
|
||||
return ret;
|
||||
}
|
||||
ret = send_to_host();
|
||||
return ret;
|
||||
}
|
||||
@@ -124,7 +124,6 @@ static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
|
||||
/** Variable for state of send and receive froom USART */
|
||||
static uint8_t StateUsartGlobal = USART_RCV;
|
||||
|
||||
static bool write_to_host_in_progress = false;
|
||||
static uint8_t host_to_sim_buf[BUFLEN];
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
@@ -306,42 +305,6 @@ void Phone_init( void ) {
|
||||
}
|
||||
|
||||
|
||||
void USB_write_callback(uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
|
||||
{
|
||||
if (status != USBD_STATUS_SUCCESS) {
|
||||
TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status);
|
||||
}
|
||||
write_to_host_in_progress = false;
|
||||
}
|
||||
|
||||
int send_to_host()
|
||||
{
|
||||
static uint8_t msg[RING_BUFLEN];
|
||||
int ret = 0;
|
||||
int i;
|
||||
|
||||
for(i = 0; !rbuf_is_empty(&sim_rcv_buf); i++) {
|
||||
msg[i] = rbuf_read(&sim_rcv_buf);
|
||||
}
|
||||
write_to_host_in_progress = true;
|
||||
ret = USBD_Write( PHONE_DATAIN, msg, i, (TransferCallback)&USB_write_callback, 0 );
|
||||
return ret;
|
||||
}
|
||||
|
||||
int check_data_from_phone()
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
while (rbuf_is_empty(&sim_rcv_buf) || write_to_host_in_progress == true)
|
||||
__WFI();
|
||||
ret = send_to_host();
|
||||
if (ret != USBD_STATUS_SUCCESS) {
|
||||
TRACE_ERROR("Error sending to host (%x)", ret);
|
||||
return ret;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Sniffed Phone to SIM card communication:
|
||||
// phone > sim : RST
|
||||
// phone < sim : ATR
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define RING_BUFLEN 64
|
||||
#define RING_BUFLEN 1024
|
||||
|
||||
typedef struct ringbuf {
|
||||
uint8_t buf[RING_BUFLEN];
|
||||
|
||||
@@ -46,6 +46,7 @@ typedef struct {
|
||||
extern const USBConfigurationDescriptor *configurationDescriptorsArr[];
|
||||
|
||||
/* Helper functions */
|
||||
int check_data_from_phone();
|
||||
|
||||
// FIXME: static function definitions
|
||||
extern uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive );
|
||||
|
||||
@@ -102,7 +102,6 @@ void USART1_IrqHandler( void )
|
||||
}
|
||||
*/
|
||||
uint32_t csr = USART_PHONE->US_CSR;
|
||||
// PR("---- stat: %x\n\r", csr);
|
||||
|
||||
if (csr & US_CSR_TXRDY) {
|
||||
/* transmit buffer empty, nothing to transmit */
|
||||
@@ -111,14 +110,15 @@ void USART1_IrqHandler( void )
|
||||
stat = (csr&(US_CSR_OVRE|US_CSR_FRAME|
|
||||
US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|
|
||||
(1<<10)));
|
||||
// int c = (USART_PHONE->US_RHR) & 0xFF;
|
||||
uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
|
||||
// printf(" %x", c);
|
||||
|
||||
if (stat == 0 ) {
|
||||
/* Fill char into buffer */
|
||||
rbuf_write(&sim_rcv_buf, (USART_PHONE->US_RHR) & 0xFF);
|
||||
rbuf_write(&sim_rcv_buf, c);
|
||||
} else {
|
||||
PR("e %x st: %x\n", (USART_PHONE->US_RHR) & 0xFF, stat);
|
||||
rbuf_write(&sim_rcv_buf, c);
|
||||
PR("e %x st: %x\n", c, stat);
|
||||
} /* else: error occured */
|
||||
|
||||
char_stat = stat;
|
||||
|
||||
@@ -65,14 +65,17 @@ static const Pin pPwr[] = {
|
||||
*-----------------------------------------------------------------------------*/
|
||||
|
||||
void Sniffer_configure( void ){
|
||||
TRACE_INFO("Sniffer config\n");
|
||||
}
|
||||
|
||||
void Sniffer_exit( void ){
|
||||
TRACE_INFO("Sniffer exit\n");
|
||||
USART_SetReceiverEnabled(USART_PHONE, 0);
|
||||
}
|
||||
|
||||
void Sniffer_init( void )
|
||||
{
|
||||
TRACE_INFO("Sniffer Init\n");
|
||||
/* Configure ISO7816 driver */
|
||||
PIO_Configure( pinsISO7816_sniff, PIO_LISTSIZE( pinsISO7816_sniff ) ) ;
|
||||
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
|
||||
@@ -86,14 +89,5 @@ void Sniffer_init( void )
|
||||
|
||||
void Sniffer_run( void )
|
||||
{
|
||||
#if 0
|
||||
if (rcvdChar != 0) {
|
||||
/* DATA_IN for host side is data_out for simtrace side */
|
||||
/* FIXME: Performancewise sending a USB packet for every byte is a disaster */
|
||||
PR("----- %x %x %x ..\n\r", buf.buf[0], buf.buf[1],buf.buf[2] );
|
||||
USBD_Write( DATAIN, (void *) buf.buf, BUFLEN, 0, 0 );
|
||||
PR("----- Rcvd char\n\r");
|
||||
rcvdChar = 0;
|
||||
}
|
||||
#endif
|
||||
check_data_from_phone();
|
||||
}
|
||||
|
||||
@@ -200,9 +200,9 @@ const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffe
|
||||
sizeof(USBEndpointDescriptor),
|
||||
USBGenericDescriptor_ENDPOINT,
|
||||
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
|
||||
DATAOUT),
|
||||
PHONE_DATAOUT),
|
||||
USBEndpointDescriptor_BULK,
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAOUT),
|
||||
USBEndpointDescriptor_MAXBULKSIZE_FS),
|
||||
0 /* Must be 0 for full-speed bulk endpoints */
|
||||
},
|
||||
@@ -211,9 +211,9 @@ const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffe
|
||||
sizeof(USBEndpointDescriptor),
|
||||
USBGenericDescriptor_ENDPOINT,
|
||||
USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
|
||||
DATAIN),
|
||||
PHONE_DATAIN),
|
||||
USBEndpointDescriptor_BULK,
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_DATAIN),
|
||||
USBEndpointDescriptor_MAXBULKSIZE_FS),
|
||||
0 /* Must be 0 for full-speed bulk endpoints */
|
||||
},
|
||||
@@ -221,9 +221,9 @@ const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffe
|
||||
{
|
||||
sizeof(USBEndpointDescriptor),
|
||||
USBGenericDescriptor_ENDPOINT,
|
||||
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, INT ),
|
||||
USBEndpointDescriptor_ADDRESS( USBEndpointDescriptor_IN, PHONE_INT ),
|
||||
USBEndpointDescriptor_INTERRUPT,
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(INT),
|
||||
MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(PHONE_INT),
|
||||
USBEndpointDescriptor_MAXINTERRUPTSIZE_FS),
|
||||
0x10
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user