debug/tracing updates

This commit is contained in:
Harald Welte
2016-03-20 14:58:35 +01:00
parent c58bba0833
commit 53079bbbac
3 changed files with 24 additions and 26 deletions

View File

@@ -17,7 +17,7 @@
* *
*/ */
#define TRACE_LEVEL 6 //#define TRACE_LEVEL 6
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
@@ -177,11 +177,10 @@ static void flush_rx_buffer(struct card_handle *ch)
rd->data_len = rctx->idx; rd->data_len = rctx->idx;
rd->hdr.msg_len = sizeof(*rd) + rd->data_len; rd->hdr.msg_len = sizeof(*rd) + rd->data_len;
llist_add_tail(&rctx->list, &ch->usb_tx_queue);
req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING);
/* no need for irqsafe operation, as the usb_tx_queue is
/* FIXME: call into USB code to see if this buffer can * processed only by the main loop context */
* be transmitted now */ llist_add_tail(&rctx->list, &ch->usb_tx_queue);
} }
/* convert a non-contiguous PTS request/responsei into a contiguous /* convert a non-contiguous PTS request/responsei into a contiguous
@@ -234,11 +233,10 @@ static void flush_pts(struct card_handle *ch)
ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req); ptsi->pts_len = serialize_pts(ptsi->req, ch->pts.req);
serialize_pts(ptsi->resp, ch->pts.resp); serialize_pts(ptsi->resp, ch->pts.resp);
llist_add_tail(&rctx->list, &ch->usb_tx_queue);
req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING); req_ctx_set_state(rctx, RCTX_S_USB_TX_PENDING);
/* no need for irqsafe operation, as the usb_tx_queue is
/* FIXME: call into USB code to see if this buffer can * processed only by the main loop context */
* be transmitted now */ llist_add_tail(&rctx->list, &ch->usb_tx_queue);
} }
static void emu_update_fidi(struct card_handle *ch) static void emu_update_fidi(struct card_handle *ch)
@@ -406,6 +404,9 @@ process_byte_pts(struct card_handle *ch, uint8_t byte)
flush_pts(ch); flush_pts(ch);
/* activate UART TX to transmit PTS response */ /* activate UART TX to transmit PTS response */
card_emu_uart_enable(ch->uart_chan, ENABLE_TX); card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
/* don't fall-through to the 'return ISO_S_IN_PTS'
* below, rather keep ISO7816 state as-is, it will be
* further updated by the tx-completion handler */
return -1; return -1;
} }
@@ -669,6 +670,8 @@ static int tx_byte_tpdu(struct card_handle *ch)
/* ensure we are aware of any data that might be pending for /* ensure we are aware of any data that might be pending for
* transmit */ * transmit */
if (!ch->uart_tx_ctx) { if (!ch->uart_tx_ctx) {
/* uart_tx_queue is filled from main loop, so no need
* for irq-safe operations */
if (llist_empty(&ch->uart_tx_queue)) if (llist_empty(&ch->uart_tx_queue))
return 0; return 0;
@@ -715,12 +718,8 @@ static int tx_byte_tpdu(struct card_handle *ch)
/* we have transmitted all bytes */ /* we have transmitted all bytes */
if (td->flags & CEMU_DATA_F_FINAL) { if (td->flags & CEMU_DATA_F_FINAL) {
/* this was the final part of the APDU, go /* this was the final part of the APDU, go
* back to state one*/ * back to state one */
card_set_state(ch, ISO_S_WAIT_TPDU); card_set_state(ch, ISO_S_WAIT_TPDU);
} else {
/* FIXME: call into USB code to chec if we need
* to submit a free buffer to accept
* further data on bulk out endpoint */
} }
} }
req_ctx_set_state(rctx, RCTX_S_FREE); req_ctx_set_state(rctx, RCTX_S_FREE);
@@ -746,7 +745,7 @@ void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte)
case ISO_S_WAIT_CLK: case ISO_S_WAIT_CLK:
case ISO_S_WAIT_RST: case ISO_S_WAIT_RST:
case ISO_S_WAIT_ATR: case ISO_S_WAIT_ATR:
TRACE_DEBUG("Received UART char in 7816 state %u\r\n", TRACE_ERROR("Received UART char in invalid 7816 state %u\r\n",
ch->state); ch->state);
/* we shouldn't receive any data from the reader yet! */ /* we shouldn't receive any data from the reader yet! */
break; break;

View File

@@ -79,7 +79,8 @@ static void usb_read_cb(uint8_t *arg, uint8_t status, uint32_t transferred,
struct req_ctx *rctx = (struct req_ctx *) arg; struct req_ctx *rctx = (struct req_ctx *) arg;
struct llist_head *queue = (struct llist_head *) usbep_in_progress[rctx->ep]; struct llist_head *queue = (struct llist_head *) usbep_in_progress[rctx->ep];
TRACE_DEBUG("%s (EP=%u)\r\n", __func__, rctx->ep); TRACE_DEBUG("%s (EP=%u, len=%u, q=%p)\r\n", __func__,
rctx->ep, transferred, queue);
usbep_in_progress[rctx->ep] = 0; usbep_in_progress[rctx->ep] = 0;

View File

@@ -1,4 +1,4 @@
#define TRACE_LEVEL 6 //#define TRACE_LEVEL 6
#include "board.h" #include "board.h"
#include "simtrace.h" #include "simtrace.h"
@@ -145,16 +145,18 @@ int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
Usart_info *ui = &usart_info[uart_chan]; Usart_info *ui = &usart_info[uart_chan];
ISO7816_SendChar(byte, ui); ISO7816_SendChar(byte, ui);
#else #else
int i = 0; int i = 1;
while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) { while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) {
if (!(i%1000000)) { if (!(i%1000000)) {
printf("s: %x %02X", usart->US_CSR, usart->US_RHR & 0xFF); TRACE_ERROR("s: %x %02X\r\n",
usart->US_CSR, usart->US_RHR & 0xFF);
usart->US_CR = US_CR_RSTTX; usart->US_CR = US_CR_RSTTX;
usart->US_CR = US_CR_RSTRX; usart->US_CR = US_CR_RSTRX;
} }
i++;
} }
usart->US_THR = byte; usart->US_THR = byte;
TRACE_ERROR("Sx%02x\r\n", byte); //TRACE_ERROR("Sx%02x\r\n", byte);
#endif #endif
return 1; return 1;
} }
@@ -460,8 +462,6 @@ static void dispatch_received_rctx(struct req_ctx *rctx, struct cardem_inst *ci)
* one message. USB endpoints are streams that don't * one message. USB endpoints are streams that don't
* preserve the message boundaries */ * preserve the message boundaries */
mh = (struct cardemu_usb_msg_hdr *) rctx->data; mh = (struct cardemu_usb_msg_hdr *) rctx->data;
TRACE_DEBUG("rctx->tot_len=%d, mh->msg_len=%d\r\n",
rctx->tot_len, mh->msg_len);
if (mh->msg_len == rctx->tot_len) { if (mh->msg_len == rctx->tot_len) {
/* fast path: only one message in buffer */ /* fast path: only one message in buffer */
dispatch_usb_command(rctx, ci); dispatch_usb_command(rctx, ci);
@@ -473,8 +473,6 @@ static void dispatch_received_rctx(struct req_ctx *rctx, struct cardem_inst *ci)
for (mh = (struct cardemu_usb_msg_hdr *) rctx->data; for (mh = (struct cardemu_usb_msg_hdr *) rctx->data;
(uint8_t *)mh < rctx->data + rctx->tot_len; (uint8_t *)mh < rctx->data + rctx->tot_len;
mh = (struct cardemu_usb_msg_hdr * ) ((uint8_t *)mh + mh->msg_len)) { mh = (struct cardemu_usb_msg_hdr * ) ((uint8_t *)mh + mh->msg_len)) {
TRACE_DEBUG("Segment %d, offs=%d, len=%d\r\n", i,
(uint8_t *)mh - rctx->data, mh->msg_len);
segm = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_MAIN_PROCESSING); segm = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_MAIN_PROCESSING);
if (!segm) { if (!segm) {
TRACE_ERROR("ENOMEM during rctx segmentation\r\n"); TRACE_ERROR("ENOMEM during rctx segmentation\r\n");
@@ -532,13 +530,13 @@ void mode_cardemu_run(void)
uint8_t byte = rbuf_read(&ci->rb); uint8_t byte = rbuf_read(&ci->rb);
__enable_irq(); __enable_irq();
card_emu_process_rx_byte(ci->ch, byte); card_emu_process_rx_byte(ci->ch, byte);
TRACE_ERROR("Rx%02x\r\n", byte); //TRACE_ERROR("Rx%02x\r\n", byte);
} }
queue = card_emu_get_usb_tx_queue(ci->ch); queue = card_emu_get_usb_tx_queue(ci->ch);
int usb_pending = llist_count(queue); int usb_pending = llist_count(queue);
if (usb_pending != ci->usb_pending_old) { if (usb_pending != ci->usb_pending_old) {
// printf("usb_pending=%d\r\n", usb_pending); TRACE_DEBUG("usb_pending=%d\r\n", usb_pending);
ci->usb_pending_old = usb_pending; ci->usb_pending_old = usb_pending;
} }
usb_refill_to_host(queue, ci->ep_in); usb_refill_to_host(queue, ci->ep_in);