From 5a29168eaddaaa47909f2ae84c41d0743996215f Mon Sep 17 00:00:00 2001 From: henryk Date: Fri, 14 Dec 2007 23:13:20 +0000 Subject: Sanitize and clarify the len handling in rx buffers. Fix miller decoder git-svn-id: https://svn.openpcd.org:2342/trunk@387 6dc7ffe9-61d6-0310-9af1-9938baff3ed1 --- openpicc/application/iso14443_layer3a.c | 6 ++++-- openpicc/application/iso14443a_miller.c | 30 ++++++++++++++++++------------ openpicc/application/main.c | 4 ++-- openpicc/application/ssc_picc.c | 18 ++++++++++-------- openpicc/application/ssc_picc.h | 14 ++++++++++---- 5 files changed, 44 insertions(+), 28 deletions(-) (limited to 'openpicc') diff --git a/openpicc/application/iso14443_layer3a.c b/openpicc/application/iso14443_layer3a.c index 4e077d5..7d91250 100644 --- a/openpicc/application/iso14443_layer3a.c +++ b/openpicc/application/iso14443_layer3a.c @@ -311,8 +311,10 @@ void iso14443_layer3a_state_machine (void *pvParameters) } if(1) { DumpStringToUSB("Decoded: "); - iso14443a_decode_miller(&received_frame, buffer->data, buffer->len); - DumpBufferToUSB((char*)received_frame.data, 100); + DumpUIntToUSB(buffer->len); + DumpStringToUSB(" "); + iso14443a_decode_miller(&received_frame, buffer->data, buffer->len/8); + DumpBufferToUSB((char*)received_frame.data, received_frame.numbytes + (received_frame.numbits+7)/8); DumpStringToUSB("\n\r"); } /* For debugging, wait 1ms, then wait for another frame diff --git a/openpicc/application/iso14443a_miller.c b/openpicc/application/iso14443a_miller.c index deb9a8a..3d132c2 100644 --- a/openpicc/application/iso14443a_miller.c +++ b/openpicc/application/iso14443a_miller.c @@ -55,38 +55,39 @@ int iso14443a_decode_miller(iso14443_frame *frame, enum miller_sequence current_seq; unsigned int bitpos = 0; - memset(frame, 0, sizeof(frame)); + memset(frame, 0, sizeof(*frame)); frame->type = TYPE_A; frame->parameters.a.parity = GIVEN_PARITY; for(i=0; i>(j*OVERSAMPLING_RATE)) & ~(~0 << OVERSAMPLING_RATE); switch(sample) { - case SEQ_X: current_seq = SEQUENCE_X; break; - case SEQ_Y: current_seq = SEQUENCE_Y; break; - case SEQ_Z: current_seq = SEQUENCE_Z; break; - default: current_seq = SEQUENCE_Y; + case SEQ_X: current_seq = SEQUENCE_X; DumpStringToUSB("X"); break; + case SEQ_Y: current_seq = SEQUENCE_Y; DumpStringToUSB("Y"); break; + case SEQ_Z: current_seq = SEQUENCE_Z; DumpStringToUSB("Z"); break; + default: DumpUIntToUSB(sample); current_seq = SEQUENCE_Y; } switch(current_seq) { - case SEQ_X: - DumpStringToUSB("X"); + case SEQUENCE_X: bit = 1; break; - case SEQ_Y: /* Fall-through to SEQ_Z */ - DumpStringToUSB("Y"); + case SEQUENCE_Y: /* Fall-through to SEQUENCE_Z */ if(last_bit == 0) { bit = BIT_ENDMARKER; + DumpStringToUSB("!"); break; } - case SEQ_Z: - DumpStringToUSB("Z"); + case SEQUENCE_Z: bit = 0; break; } switch(bit) { case BIT_ENDMARKER: - bitpos--; + bitpos-=2; /* Subtract this sequence and the previous sequence (which was a 0) */ break; case 0: /* Fall-through */ case 1: { @@ -106,6 +107,11 @@ int iso14443a_decode_miller(iso14443_frame *frame, frame->numbytes = bitpos/9; frame->numbits = bitpos%9; + DumpStringToUSB(" "); + DumpUIntToUSB(frame->numbytes); + DumpStringToUSB(" bytes, "); + DumpUIntToUSB(frame->numbits); + DumpStringToUSB(" bits"); DumpStringToUSB("\n\r"); return 0; diff --git a/openpicc/application/main.c b/openpicc/application/main.c index a093d70..19407e6 100644 --- a/openpicc/application/main.c +++ b/openpicc/application/main.c @@ -90,7 +90,7 @@ void main_help_print_buffer(ssc_dma_rx_buffer_t *buffer, int *pktcount) ISO14443A_SHORT_TYPE *tmp = (ISO14443A_SHORT_TYPE*)buffer->data; int i, dumped = 0; unsigned int j; - for(i = buffer->len / sizeof(*tmp); i >= 0 ; i--) { + for(i = buffer->len / (sizeof(*tmp)*8); i >= 0 ; i--) { if( *tmp != 0x00000000 ) { if(dumped == 0) { DumpUIntToUSB(buffer->len); @@ -101,7 +101,7 @@ void main_help_print_buffer(ssc_dma_rx_buffer_t *buffer, int *pktcount) DumpStringToUSB(" "); } dumped = 1; - DumpUIntToUSB(buffer->len / sizeof(*tmp) - i); + DumpUIntToUSB(buffer->len / (sizeof(*tmp)*8) - i); DumpStringToUSB(": "); for(j=0; j> j) & 0x1) ? '1' : '_' , 0); diff --git a/openpicc/application/ssc_picc.c b/openpicc/application/ssc_picc.c index 3da1dbe..085fa48 100644 --- a/openpicc/application/ssc_picc.c +++ b/openpicc/application/ssc_picc.c @@ -70,16 +70,16 @@ static struct ssc_state ssc_state; /* Note: Only use 8, 16 or 32 for the transfersize. (These are the sizes used by the PDC and even though * the SSC supports different sizes, all PDC tranfers will be either 8, 16 or 32, rounding up.) */ -static const struct {u_int16_t transfersize; u_int16_t transfers;} ssc_sizes[] = { +static const ssc_mode_def ssc_sizes[] = { /* Undefined, no size set */ - [SSC_MODE_NONE] = {0, 0}, + [SSC_MODE_NONE] = {SSC_MODE_NONE, 0, 0}, /* 14443A Short Frame: 1 transfer of ISO14443A_SHORT_LEN bits */ - [SSC_MODE_14443A_SHORT] = {ISO14443A_SHORT_TRANSFER_SIZE, 1}, + [SSC_MODE_14443A_SHORT] = {SSC_MODE_14443A_SHORT, ISO14443A_SHORT_TRANSFER_SIZE, 1}, /* 14443A Standard Frame: FIXME 16 transfers of 32 bits (maximum number), resulting in 512 samples */ - [SSC_MODE_14443A_STANDARD] = {32, 4}, - [SSC_MODE_14443B] = {32, 16}, /* 64 bytes */ - [SSC_MODE_EDGE_ONE_SHOT] = {32, 16}, /* 64 bytes */ - [SSC_MODE_CONTINUOUS] = {32, 511}, /* 2044 bytes */ + [SSC_MODE_14443A_STANDARD] = {SSC_MODE_14443A_STANDARD, 32, 4}, + [SSC_MODE_14443B] = {SSC_MODE_14443B, 32, 16}, /* 64 bytes */ + [SSC_MODE_EDGE_ONE_SHOT] = {SSC_MODE_EDGE_ONE_SHOT, 32, 16}, /* 64 bytes */ + [SSC_MODE_CONTINUOUS] = {SSC_MODE_CONTINUOUS, 32, 511}, /* 2044 bytes */ }; /* ************** SSC BUFFER HANDLING *********************** */ @@ -153,6 +153,7 @@ static int __ramfunc __ssc_rx_load(int secondary) DEBUGR("filling SSC RX%u dma ctx: %u (len=%u) ", secondary, req_ctx_num(buffer), buffer->size); buffer->len = ssc_sizes[ssc_state.mode].transfersize * ssc_sizes[ssc_state.mode].transfers; + buffer->reception_mode = &ssc_sizes[ssc_state.mode]; if(ssc_state.buffer[secondary] != NULL) { /* This condition is not expected to happen and would probably indicate a bug @@ -198,7 +199,7 @@ static ssc_dma_rx_buffer_t* __ramfunc __ssc_rx_unload(int secondary) u_int16_t remaining_transfers = (secondary ? rx_pdc->PDC_RNCR : rx_pdc->PDC_RCR); u_int8_t* next_transfer_location = (u_int8_t*)(secondary ? rx_pdc->PDC_RNPR : rx_pdc->PDC_RPR); - u_int32_t remaining_size = ssc_sizes[buffer->reception_mode].transfersize * remaining_transfers; + u_int32_t remaining_size = buffer->reception_mode->transfersize * remaining_transfers; /* Consistency check */ if( next_transfer_location - remaining_size != buffer->data ) { ssc_buffer_errors++; @@ -430,6 +431,7 @@ static void __ramfunc ssc_irq(void) if (ssc_sr & AT91C_SSC_ENDRX) { /* Ignore empty frames */ if (ssc_state.mode == SSC_MODE_CONTINUOUS) { + /* This code section is probably bitrotten by now. */ tmp = (u_int32_t*)ssc_state.buffer[0]->data; emptyframe = 1; for(i = (ssc_state.buffer[0]->len) / 4 - 8/*WTF?*/; i > 0; i--) { diff --git a/openpicc/application/ssc_picc.h b/openpicc/application/ssc_picc.h index 5eb1160..a01b16d 100644 --- a/openpicc/application/ssc_picc.h +++ b/openpicc/application/ssc_picc.h @@ -48,7 +48,7 @@ typedef enum { } ssc_metric; extern int ssc_get_metric(ssc_metric metric); -#define SSC_DMA_BUFFER_SIZE 2048 +#define SSC_RX_BUFFER_SIZE 2048 #define SSC_DMA_BUFFER_COUNT 4 typedef enum { @@ -59,11 +59,17 @@ typedef enum { PREFILLED, /* The buffer has been prefilled for later usage (only used for TX) */ } ssc_dma_buffer_state_t; +typedef struct { + enum ssc_mode mode; + u_int16_t transfersize; + u_int16_t transfers; +} ssc_mode_def; + typedef struct { volatile ssc_dma_buffer_state_t state; - u_int32_t len; /* Length of the content */ - enum ssc_mode reception_mode; /* The SSC mode that the buffer has been loaded for (affects element size and count) */ - u_int8_t data[SSC_DMA_BUFFER_SIZE]; + u_int32_t len; /* Length of the content, in samples */ + const ssc_mode_def *reception_mode; /* Pointer to the SSC mode definition that the buffer has been loaded for (affects element size and count) */ + u_int8_t data[SSC_RX_BUFFER_SIZE]; } ssc_dma_rx_buffer_t; extern xQueueHandle ssc_rx_queue; -- cgit v1.2.3