+#define PCSC_MAX_TRACELEN 60000
+static uint8_t pcsc_trace_buf[PCSC_MAX_TRACELEN];
+static bool tracing = false;
+static uint32_t traceLen = 0;
+
+
+uint8_t *pcsc_get_trace_addr(void)
+{
+ return pcsc_trace_buf;
+}
+
+
+uint32_t pcsc_get_traceLen(void)
+{
+ return traceLen;
+}
+
+
+static void pcsc_clear_trace(void)
+{
+ traceLen = 0;
+}
+
+
+static void pcsc_set_tracing(bool enable) {
+ tracing = enable;
+}
+
+
+static bool pcsc_LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, bool readerToTag)
+{
+ if (!tracing) return false;
+
+ uint8_t *trace = pcsc_trace_buf;
+
+ uint32_t num_paritybytes = (iLen-1)/8 + 1; // number of paritybytes
+ uint32_t duration = timestamp_end - timestamp_start;
+
+ // Return when trace is full
+ if (traceLen + sizeof(iLen) + sizeof(timestamp_start) + sizeof(duration) + num_paritybytes + iLen >= PCSC_MAX_TRACELEN) {
+ tracing = false; // don't trace any more
+ return false;
+ }
+ // Traceformat:
+ // 32 bits timestamp (little endian)
+ // 16 bits duration (little endian)
+ // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
+ // y Bytes data
+ // x Bytes parity (one byte per 8 bytes data)
+
+ // timestamp (start)
+ trace[traceLen++] = ((timestamp_start >> 0) & 0xff);
+ trace[traceLen++] = ((timestamp_start >> 8) & 0xff);
+ trace[traceLen++] = ((timestamp_start >> 16) & 0xff);
+ trace[traceLen++] = ((timestamp_start >> 24) & 0xff);
+
+ // duration
+ trace[traceLen++] = ((duration >> 0) & 0xff);
+ trace[traceLen++] = ((duration >> 8) & 0xff);
+
+ // data length
+ trace[traceLen++] = ((iLen >> 0) & 0xff);
+ trace[traceLen++] = ((iLen >> 8) & 0xff);
+
+ // readerToTag flag
+ if (!readerToTag) {
+ trace[traceLen - 1] |= 0x80;
+ }
+
+ // data bytes
+ if (btBytes != NULL && iLen != 0) {
+ for (int i = 0; i < iLen; i++) {
+ trace[traceLen++] = *btBytes++;
+ }
+ }
+
+ // dummy parity bytes
+ if (num_paritybytes != 0) {
+ for (int i = 0; i < num_paritybytes; i++) {
+ trace[traceLen++] = 0x00;
+ }
+ }
+
+ return true;
+}
+