<p>Kévin Redon has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/9873">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">sniffer USB: implement USB communication and send parsed messages<br><br>Change-Id: Ice7817480705f2124b08c1ff9a8826558b6d8b2b<br>---<br>M firmware/libcommon/source/sniffer.c<br>1 file changed, 234 insertions(+), 63 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/73/9873/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/firmware/libcommon/source/sniffer.c b/firmware/libcommon/source/sniffer.c</span><br><span>index 9d88c3d..88ac84f 100644</span><br><span>--- a/firmware/libcommon/source/sniffer.c</span><br><span>+++ b/firmware/libcommon/source/sniffer.c</span><br><span>@@ -34,6 +34,13 @@</span><br><span> #include <string.h></span><br><span> #include "utils.h"</span><br><span> #include "iso7816_fidi.h"</span><br><span style="color: hsl(120, 100%, 40%);">+/* USB related libraries */</span><br><span style="color: hsl(120, 100%, 40%);">+#include "osmocom/core/linuxlist.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "osmocom/core/msgb.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "llist_irqsafe.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "usb_buf.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "simtrace_usb.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "simtrace_prot.h"</span><br><span> </span><br><span> /*------------------------------------------------------------------------------</span><br><span>  *         Internal definitions</span><br><span>@@ -133,6 +140,9 @@</span><br><span> /*! Ring buffer to store sniffer communication data */</span><br><span> static struct ringbuf sniff_buffer;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/* Flags  to know is the card status changed (see SIMTRACE_MSGT_DT_SNIFF_CHANGE flags) */</span><br><span style="color: hsl(120, 100%, 40%);">+volatile uint32_t change_flags = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* ISO 7816 variables */</span><br><span> /*! ISO 7816-3 state */</span><br><span> enum iso7816_3_sniff_state iso_state = ISO7816_S_RESET;</span><br><span>@@ -178,6 +188,35 @@</span><br><span>  */</span><br><span> static const uint8_t convention_convert_lut[256] = { 0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f, 0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f, 0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17, 0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07, 0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b, 0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b, 0x0b, 0xf3, 0x73, 0xb3, 0x33, 0xd3, 0x53, 0x93, 0x13, 0xe3, 0x63, 0xa3, 0x23, 0xc3, 0x43, 0x83, 0x03, 0xfd, 0x7d, 0xbd, 0x3d, 0xdd, 0x5d, 0x9d, 0x1d, 0xed, 0x6d, 0xad, 0x2d, 0xcd, 0x4d, 0x8d, 0x0d, 0xf5, 0x75, 0xb5, 0x35, 0xd5, 0x55, 0x95, 0x15, 0xe5, 0x65, 0xa5, 0x25, 0xc5, 0x45, 0x85, 0x05, 0xf9, 0x79, 0xb9, 0x39, 0xd9, 0x59, 0x99, 0x19, 0xe9, 0x69, 0xa9, 0x29, 0xc9, 0x49, 0x89, 0x09, 0xf1, 0x71, 0xb1, 0x31, 0xd1, 0x51, 0x91, 0x11, 0xe1, 0x61, 0xa1, 0x21, 0xc1, 0x41, 0x81, 0x01, 0xfe, 0x7e, 0xbe, 0x3e, 0xde, 0x5e, 0x9e, 0x1e, 0xee, 0x6e, 0xae, 0x2e, 0xce, 0x4e, 0x8e, 0x0e, 0xf6, 0x76, 0xb6, 0x36, 0xd6, 0x56, 0x96, 0x16, 0xe6, 0x66, 0xa6, 0x26, 0xc6, 0x46, 0x86, 0x06, 0xfa, 0x7a, 0xba, 0x3a, 0xda, 0x5a, 0x9a, 0x1a, 0xea, 0x6a, 0xaa, 0x2a, 0xca, 0x4a, 0x8a, 0x0a, 0xf2, 0x72, 0xb2, 0x32, 0xd2, 0x52, 0x92, 0x12, 0xe2, 0x62, 0xa2, 0x22, 0xc2, 0x42, 0x82, 0x02, 0xfc, 0x7c, 0xbc, 0x3c, 0xdc, 0x5c, 0x9c, 0x1c, 0xec, 0x6c, 0xac, 0x2c, 0xcc, 0x4c, 0x8c, 0x0c, 0xf4, 0x74, 0xb4, 0x34, 0xd4, 0x54, 0x94, 0x14, 0xe4, 0x64, 0xa4, 0x24, 0xc4, 0x44, 0x84, 0x04, 0xf8, 0x78, 0xb8, 0x38, 0xd8, 0x58, 0x98, 0x18, 0xe8, 0x68, 0xa8, 0x28, 0xc8, 0x48, 0x88, 0x08, 0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10, 0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00, };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send card change flags over USB</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @param[in] flags change flags corresponding to SIMTRACE_MSGT_SNIFF_CHANGE </span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_send_change(uint32_t flags)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check flags */</span><br><span style="color: hsl(120, 100%, 40%);">+     if(0==flags) { /* no changes */</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Send message over USB */</span><br><span style="color: hsl(120, 100%, 40%);">+   struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!usb_msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     struct simtrace_msg_hdr *usb_msg_header;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_CHANGE;</span><br><span style="color: hsl(120, 100%, 40%);">+     usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sniff_change *usb_sniff_change;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_sniff_change = (struct sniff_change *) msgb_put(usb_msg, sizeof(*usb_sniff_change));</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_sniff_change->flags = flags;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_len = msgb_length(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_buf_submit(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! Update the ISO 7816-3 state</span><br><span>  *  @param[in] iso_state_new new ISO 7816-3 state to update to</span><br><span>  */</span><br><span>@@ -193,9 +232,11 @@</span><br><span>     switch (iso_state_new) {</span><br><span>     case ISO7816_S_RESET:</span><br><span>                update_fidi(sniff_usart.base, 0x11); /* reset baud rate to default Di/Fi values */</span><br><span style="color: hsl(120, 100%, 40%);">+            usb_send_change(SNIFF_CHANGE_FLAG_RESET_HOLD); /* send reset change to host software over USB */</span><br><span>             break;</span><br><span>       case ISO7816_S_WAIT_ATR:</span><br><span>             rbuf_reset(&sniff_buffer); /* reset buffer for new communication */</span><br><span style="color: hsl(120, 100%, 40%);">+               usb_send_change(SNIFF_CHANGE_FLAG_RESET_RELEASE); /* send reset change to host software over USB */</span><br><span>          break;</span><br><span>       case ISO7816_S_IN_ATR:</span><br><span>               atr_i = 0;</span><br><span>@@ -220,20 +261,51 @@</span><br><span>   //TRACE_INFO("Changed to ISO 7816-3 state %u\n\r", iso_state); /* don't print since this is function is also called by ISRs */</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! Print current ATR */</span><br><span style="color: hsl(0, 100%, 40%);">-static void print_atr(void)</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send current ATR over USB</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note Also print the ATR over serial</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_send_atr(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Check state */</span><br><span>    if (ISO7816_S_IN_ATR!=iso_state) {</span><br><span>           TRACE_WARNING("Can't print ATR in ISO 7816-3 state %u\n\r", iso_state);</span><br><span>                return;</span><br><span>      }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (atr_i>=ARRAY_SIZE(atr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+              TRACE_ERROR("ATR buffer overflow\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+             return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* Show activity on LED */</span><br><span>   led_blink(LED_GREEN, BLINK_2O_F);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Print ATR */</span><br><span>      printf("ATR: ");</span><br><span style="color: hsl(0, 100%, 40%);">-      for (uint8_t i=0; i<atr_i && i<ARRAY_SIZE(atr); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+  for (uint8_t i=0; i<atr_i; i++) {</span><br><span>                 printf("%02x ", atr[i]);</span><br><span>   }</span><br><span>    printf("\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Send ATR over USB */</span><br><span style="color: hsl(120, 100%, 40%);">+       struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!usb_msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     struct simtrace_msg_hdr *usb_msg_header;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_ATR;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sniff_data *usb_sniff_data_atr;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_sniff_data_atr = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_atr));</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_sniff_data_atr->complete = true;</span><br><span style="color: hsl(120, 100%, 40%);">+       usb_sniff_data_atr->length = atr_i;</span><br><span style="color: hsl(120, 100%, 40%);">+        uint8_t *data = msgb_put(usb_msg, usb_sniff_data_atr->length);</span><br><span style="color: hsl(120, 100%, 40%);">+     memcpy(data, atr, atr_i);</span><br><span style="color: hsl(120, 100%, 40%);">+     usb_msg_header->msg_len = msgb_length(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_buf_submit(usb_msg);</span><br><span> }</span><br><span> </span><br><span> /*! Process ATR byte</span><br><span>@@ -317,7 +389,7 @@</span><br><span>              }</span><br><span>    case ATR_S_WAIT_TCK:  /* see ISO/IEC 7816-3:2006 section 8.2.5 */</span><br><span>            /* we could verify the checksum, but we are just here to sniff */</span><br><span style="color: hsl(0, 100%, 40%);">-               print_atr(); /* print ATR for info */</span><br><span style="color: hsl(120, 100%, 40%);">+         usb_send_atr(); /* send ATR to host software using USB */</span><br><span>            change_state(ISO7816_S_WAIT_TPDU); /* go to next state */</span><br><span>            break;</span><br><span>       default:</span><br><span>@@ -325,12 +397,14 @@</span><br><span>     }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! Print current PPS */</span><br><span style="color: hsl(0, 100%, 40%);">-static void print_pps(void)</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send current PPS over USB</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note Also print the PPS over serial</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_send_pps(void)</span><br><span> {</span><br><span>     uint8_t *pps_cur; /* current PPS (request or response) */</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   /* sanity check */</span><br><span style="color: hsl(120, 100%, 40%);">+    /* Sanity check */</span><br><span>   if (ISO7816_S_IN_PPS_REQ==iso_state) {</span><br><span>               pps_cur = pps_req;</span><br><span>   } else if (ISO7816_S_IN_PPS_RSP==iso_state) {</span><br><span>@@ -340,21 +414,76 @@</span><br><span>                return;</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   led_blink(LED_GREEN, BLINK_2O_F);</span><br><span style="color: hsl(0, 100%, 40%);">-       printf("PPS %s : ", ISO7816_S_IN_PPS_REQ==iso_state ? "REQUEST" : "RESPONSE");</span><br><span style="color: hsl(0, 100%, 40%);">-    printf("%02x ", pps_cur[0]);</span><br><span style="color: hsl(0, 100%, 40%);">-  printf("%02x ", pps_cur[1]);</span><br><span style="color: hsl(120, 100%, 40%);">+        /* Get only relevant data */</span><br><span style="color: hsl(120, 100%, 40%);">+  uint8_t pps[6];</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t pps_i = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+    pps[pps_i++] = pps_cur[0];</span><br><span style="color: hsl(120, 100%, 40%);">+    pps[pps_i++] = pps_cur[1];</span><br><span>   if (pps_cur[1]&0x10) {</span><br><span style="color: hsl(0, 100%, 40%);">-              printf("%02x ", pps_cur[2]);</span><br><span style="color: hsl(120, 100%, 40%);">+                pps[pps_i++] = pps_cur[2];</span><br><span>   }</span><br><span>    if (pps_cur[1]&0x20) {</span><br><span style="color: hsl(0, 100%, 40%);">-              printf("%02x ", pps_cur[3]);</span><br><span style="color: hsl(120, 100%, 40%);">+                pps[pps_i++] = pps_cur[3];</span><br><span>   }</span><br><span>    if (pps_cur[1]&0x40) {</span><br><span style="color: hsl(0, 100%, 40%);">-              printf("%02x ", pps_cur[4]);</span><br><span style="color: hsl(120, 100%, 40%);">+                pps[pps_i++] = pps_cur[4];</span><br><span>   }</span><br><span style="color: hsl(0, 100%, 40%);">-       printf("%02x ", pps_cur[5]);</span><br><span style="color: hsl(120, 100%, 40%);">+        pps[pps_i++] = pps_cur[5];</span><br><span style="color: hsl(120, 100%, 40%);">+    </span><br><span style="color: hsl(120, 100%, 40%);">+      /* Show activity on LED */</span><br><span style="color: hsl(120, 100%, 40%);">+    led_blink(LED_GREEN, BLINK_2O_F);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Print PPS */</span><br><span style="color: hsl(120, 100%, 40%);">+       printf("PPS: ");</span><br><span style="color: hsl(120, 100%, 40%);">+    for (uint8_t i=0; i<pps_i; i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+          printf("%02x ", pps[i]);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span>    printf("\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Send message over USB */</span><br><span style="color: hsl(120, 100%, 40%);">+   struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!usb_msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     struct simtrace_msg_hdr *usb_msg_header;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_PPS;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sniff_data *usb_sniff_data_pps;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_sniff_data_pps = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_pps));</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_sniff_data_pps->complete = true;</span><br><span style="color: hsl(120, 100%, 40%);">+       usb_sniff_data_pps->length = pps_i;</span><br><span style="color: hsl(120, 100%, 40%);">+        uint8_t *data = msgb_put(usb_msg, usb_sniff_data_pps->length);</span><br><span style="color: hsl(120, 100%, 40%);">+     memcpy(data, pps, pps_i);</span><br><span style="color: hsl(120, 100%, 40%);">+     usb_msg_header->msg_len = msgb_length(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_buf_submit(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send Fi/Di change over USB</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @param[in] fidi Fi/Di factor as encoded in TA1 </span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_send_fidi(uint8_t fidi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Send message over USB */</span><br><span style="color: hsl(120, 100%, 40%);">+   struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!usb_msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     struct simtrace_msg_hdr *usb_msg_header;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_FIDI;</span><br><span style="color: hsl(120, 100%, 40%);">+       usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sniff_fidi *usb_sniff_fidi;</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_sniff_fidi = (struct sniff_fidi *) msgb_put(usb_msg, sizeof(*usb_sniff_fidi));</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_sniff_fidi->fidi = fidi;</span><br><span style="color: hsl(120, 100%, 40%);">+       usb_msg_header->msg_len = msgb_length(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_buf_submit(usb_msg);</span><br><span> }</span><br><span> </span><br><span> static void process_byte_pps(uint8_t byte)</span><br><span>@@ -420,7 +549,7 @@</span><br><span>                        check ^= pps_cur[4];</span><br><span>                 }</span><br><span>            check ^= pps_cur[5];</span><br><span style="color: hsl(0, 100%, 40%);">-            print_pps(); /* print PPS for info */</span><br><span style="color: hsl(120, 100%, 40%);">+         usb_send_pps(); /* send PPS to host software using USB */</span><br><span>            if (ISO7816_S_IN_PPS_REQ==iso_state) {</span><br><span>                       if (0==check) { /* checksum is valid */</span><br><span>                              change_state(ISO7816_S_WAIT_PPS_RSP); /* go to next state */</span><br><span>@@ -439,7 +568,7 @@</span><br><span>                           }</span><br><span>                            TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fn, dn);</span><br><span>                                 update_fidi(sniff_usart.base, pps_cur[2]);</span><br><span style="color: hsl(0, 100%, 40%);">-              rbuf_reset(&sniff_buffer); /* reset buffer for new communication */</span><br><span style="color: hsl(120, 100%, 40%);">+                               usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */</span><br><span>                    } else { /* checksum is invalid */</span><br><span>                           TRACE_INFO("PPS negotiation failed\n\r");</span><br><span>                  }</span><br><span>@@ -451,20 +580,47 @@</span><br><span>    }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*! Print current TPDU */</span><br><span style="color: hsl(0, 100%, 40%);">-static void print_tpdu(void)</span><br><span style="color: hsl(120, 100%, 40%);">+/*! Send current TPDU over USB</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note Also print the TPDU over serial</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void usb_send_tpdu(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Check state */</span><br><span>    if (ISO7816_S_IN_TPDU!=iso_state) {</span><br><span>          TRACE_WARNING("Can't print TPDU in ISO 7816-3 state %u\n\r", iso_state);</span><br><span>               return;</span><br><span>      }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* Show activity on LED */</span><br><span>   led_blink(LED_GREEN, BLINK_2O_F);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Print TPDU */</span><br><span>     printf("TPDU: ");</span><br><span>  for (uint8_t i=0; i<tpdu_packet_i && i<ARRAY_SIZE(tpdu_packet); i++) {</span><br><span>                 printf("%02x ", tpdu_packet[i]);</span><br><span>   }</span><br><span>    printf("\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Send ATR over USB */</span><br><span style="color: hsl(120, 100%, 40%);">+       struct msgb *usb_msg = usb_buf_alloc(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!usb_msg) {</span><br><span style="color: hsl(120, 100%, 40%);">+               return;</span><br><span style="color: hsl(120, 100%, 40%);">+       }</span><br><span style="color: hsl(120, 100%, 40%);">+     struct simtrace_msg_hdr *usb_msg_header;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_msg->l1h = msgb_put(usb_msg, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+ usb_msg_header = (struct simtrace_msg_hdr *) usb_msg->l1h;</span><br><span style="color: hsl(120, 100%, 40%);">+ memset(usb_msg_header, 0, sizeof(*usb_msg_header));</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_class = SIMTRACE_MSGC_SNIFF;</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_msg_header->msg_type = SIMTRACE_MSGT_SNIFF_ATR;</span><br><span style="color: hsl(120, 100%, 40%);">+        usb_msg->l2h = usb_msg->l1h + sizeof(*usb_msg_header);</span><br><span style="color: hsl(120, 100%, 40%);">+  struct sniff_data *usb_sniff_data_tpdu;</span><br><span style="color: hsl(120, 100%, 40%);">+       usb_sniff_data_tpdu = (struct sniff_data *) msgb_put(usb_msg, sizeof(*usb_sniff_data_tpdu));</span><br><span style="color: hsl(120, 100%, 40%);">+  usb_sniff_data_tpdu->complete = true;</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_sniff_data_tpdu->length = tpdu_packet_i;</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t *data = msgb_put(usb_msg, usb_sniff_data_tpdu->length);</span><br><span style="color: hsl(120, 100%, 40%);">+    memcpy(data, tpdu_packet, tpdu_packet_i);</span><br><span style="color: hsl(120, 100%, 40%);">+     usb_msg_header->msg_len = msgb_length(usb_msg);</span><br><span style="color: hsl(120, 100%, 40%);">+    usb_buf_submit(usb_msg);</span><br><span> }</span><br><span> </span><br><span> static void process_byte_tpdu(uint8_t byte)</span><br><span>@@ -530,7 +686,7 @@</span><br><span>               break;</span><br><span>       case TPDU_S_SW2:</span><br><span>             tpdu_packet[tpdu_packet_i++] = byte;</span><br><span style="color: hsl(0, 100%, 40%);">-            print_tpdu(); /* print TPDU for info */</span><br><span style="color: hsl(120, 100%, 40%);">+               usb_send_tpdu(); /* send TPDU to host software using USB */</span><br><span>          change_state(ISO7816_S_WAIT_TPDU); /* this is the end of the TPDU */</span><br><span>                 break;</span><br><span>       case TPDU_S_DATA_SINGLE:</span><br><span>@@ -554,47 +710,6 @@</span><br><span>      }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static void check_sniffed_data(void)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">- /* Handle sniffed data */</span><br><span style="color: hsl(0, 100%, 40%);">-       while (!rbuf_is_empty(&sniff_buffer)) {</span><br><span style="color: hsl(0, 100%, 40%);">-             uint8_t byte = rbuf_read(&sniff_buffer);</span><br><span style="color: hsl(0, 100%, 40%);">-            TRACE_WARNING_WP("< 0x%02x\n\r", byte);</span><br><span style="color: hsl(0, 100%, 40%);">-            switch (iso_state) { /* Handle byte depending on state */</span><br><span style="color: hsl(0, 100%, 40%);">-               case ISO7816_S_RESET: /* During reset we shouldn't receive any data */</span><br><span style="color: hsl(0, 100%, 40%);">-                      break;</span><br><span style="color: hsl(0, 100%, 40%);">-          case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */</span><br><span style="color: hsl(0, 100%, 40%);">-                  change_state(ISO7816_S_IN_ATR); /* go to next state */</span><br><span style="color: hsl(0, 100%, 40%);">-          case ISO7816_S_IN_ATR: /* More ATR data incoming */</span><br><span style="color: hsl(0, 100%, 40%);">-                     process_byte_atr(byte);</span><br><span style="color: hsl(0, 100%, 40%);">-                 break;</span><br><span style="color: hsl(0, 100%, 40%);">-          case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */</span><br><span style="color: hsl(0, 100%, 40%);">-                case ISO7816_S_WAIT_PPS_RSP:</span><br><span style="color: hsl(0, 100%, 40%);">-                    if (byte == 0xff) {</span><br><span style="color: hsl(0, 100%, 40%);">-                             if (ISO7816_S_WAIT_PPS_RSP==iso_state) {</span><br><span style="color: hsl(0, 100%, 40%);">-                                        change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */</span><br><span style="color: hsl(0, 100%, 40%);">-                               } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                                        change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */</span><br><span style="color: hsl(0, 100%, 40%);">-                               }</span><br><span style="color: hsl(0, 100%, 40%);">-                               process_byte_pps(byte);</span><br><span style="color: hsl(0, 100%, 40%);">-                         break;</span><br><span style="color: hsl(0, 100%, 40%);">-                  }</span><br><span style="color: hsl(0, 100%, 40%);">-               case ISO7816_S_IN_TPDU: /* More TPDU data incoming */</span><br><span style="color: hsl(0, 100%, 40%);">-                   if (ISO7816_S_WAIT_TPDU==iso_state) {</span><br><span style="color: hsl(0, 100%, 40%);">-                           change_state(ISO7816_S_IN_TPDU);</span><br><span style="color: hsl(0, 100%, 40%);">-                        }</span><br><span style="color: hsl(0, 100%, 40%);">-                       process_byte_tpdu(byte);</span><br><span style="color: hsl(0, 100%, 40%);">-                        break;</span><br><span style="color: hsl(0, 100%, 40%);">-          case ISO7816_S_IN_PPS_REQ:</span><br><span style="color: hsl(0, 100%, 40%);">-              case ISO7816_S_IN_PPS_RSP:</span><br><span style="color: hsl(0, 100%, 40%);">-                      process_byte_pps(byte);</span><br><span style="color: hsl(0, 100%, 40%);">-                 break;</span><br><span style="color: hsl(0, 100%, 40%);">-          default:</span><br><span style="color: hsl(0, 100%, 40%);">-                        TRACE_ERROR("Data received in unknown state %u\n\r", iso_state);</span><br><span style="color: hsl(0, 100%, 40%);">-              }</span><br><span style="color: hsl(0, 100%, 40%);">-       }</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> /*! Interrupt Service Routine called on USART activity */</span><br><span> void Sniffer_usart_isr(void)</span><br><span> {</span><br><span>@@ -634,6 +749,7 @@</span><br><span>           default:</span><br><span>                     break;</span><br><span>               }</span><br><span style="color: hsl(120, 100%, 40%);">+             usb_send_change(SNIFF_CHANGE_FLAG_TIMEOUT_WT); /* send timeout to host software over USB */</span><br><span>  }</span><br><span> }</span><br><span> </span><br><span>@@ -690,11 +806,14 @@</span><br><span> void Sniffer_exit(void)</span><br><span> {</span><br><span>   TRACE_INFO("Sniffer exit\n\r");</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Disable USART */</span><br><span>  USART_DisableIt(sniff_usart.base, US_IER_RXRDY);</span><br><span>     /* NOTE: don't forget to set the IRQ according to the USART peripheral used */</span><br><span>   NVIC_DisableIRQ(IRQ_USART_SIM);</span><br><span>      USART_SetReceiverEnabled(sniff_usart.base, 0);</span><br><span style="color: hsl(0, 100%, 40%);">-  </span><br><span style="color: hsl(120, 100%, 40%);">+      /* Disable RST IRQ */</span><br><span style="color: hsl(120, 100%, 40%);">+ PIO_DisableIt(&pin_rst);</span><br><span style="color: hsl(120, 100%, 40%);">+  NVIC_DisableIRQ(PIOA_IRQn); /* CAUTION this needs to match to the correct port */</span><br><span> }</span><br><span> </span><br><span> /* called when *Sniffer* configuration is set by host */</span><br><span>@@ -737,6 +856,58 @@</span><br><span> /* Main (idle/busy) loop of this USB configuration */</span><br><span> void Sniffer_run(void)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-   check_sniffed_data();</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Handle USB queue */</span><br><span style="color: hsl(120, 100%, 40%);">+        /* first try to send any pending messages on INT */</span><br><span style="color: hsl(120, 100%, 40%);">+   usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT);</span><br><span style="color: hsl(120, 100%, 40%);">+ /* then try to send any pending messages on IN */</span><br><span style="color: hsl(120, 100%, 40%);">+     usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN);</span><br><span style="color: hsl(120, 100%, 40%);">+      /* ensure we can handle incoming USB messages from the host */</span><br><span style="color: hsl(120, 100%, 40%);">+        /* currently we don't need any incoming data</span><br><span style="color: hsl(120, 100%, 40%);">+      usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT);</span><br><span style="color: hsl(120, 100%, 40%);">+   struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT);</span><br><span style="color: hsl(120, 100%, 40%);">+       process_any_usb_commands(queue);</span><br><span style="color: hsl(120, 100%, 40%);">+      */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* Handle sniffed data */</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!rbuf_is_empty(&sniff_buffer)) { /* use if instead of while to let the main loop restart the watchdog */</span><br><span style="color: hsl(120, 100%, 40%);">+              uint8_t byte = rbuf_read(&sniff_buffer);</span><br><span style="color: hsl(120, 100%, 40%);">+          TRACE_DEBUG_WP("< 0x%02x\n\r", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+            switch (iso_state) { /* Handle byte depending on state */</span><br><span style="color: hsl(120, 100%, 40%);">+             case ISO7816_S_RESET: /* During reset we shouldn't receive any data */</span><br><span style="color: hsl(120, 100%, 40%);">+                    break;</span><br><span style="color: hsl(120, 100%, 40%);">+                case ISO7816_S_WAIT_ATR: /* After a reset we expect the ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+                        change_state(ISO7816_S_IN_ATR); /* go to next state */</span><br><span style="color: hsl(120, 100%, 40%);">+                case ISO7816_S_IN_ATR: /* More ATR data incoming */</span><br><span style="color: hsl(120, 100%, 40%);">+                   process_byte_atr(byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                       break;</span><br><span style="color: hsl(120, 100%, 40%);">+                case ISO7816_S_WAIT_TPDU: /* After the ATR we expect TPDU or PPS data */</span><br><span style="color: hsl(120, 100%, 40%);">+              case ISO7816_S_WAIT_PPS_RSP:</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (byte == 0xff) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           if (ISO7816_S_WAIT_PPS_RSP==iso_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      change_state(ISO7816_S_IN_PPS_RSP); /* Go to PPS state */</span><br><span style="color: hsl(120, 100%, 40%);">+                             } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      change_state(ISO7816_S_IN_PPS_REQ); /* Go to PPS state */</span><br><span style="color: hsl(120, 100%, 40%);">+                             }</span><br><span style="color: hsl(120, 100%, 40%);">+                             process_byte_pps(byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                               break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        }</span><br><span style="color: hsl(120, 100%, 40%);">+             case ISO7816_S_IN_TPDU: /* More TPDU data incoming */</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (ISO7816_S_WAIT_TPDU==iso_state) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         change_state(ISO7816_S_IN_TPDU);</span><br><span style="color: hsl(120, 100%, 40%);">+                      }</span><br><span style="color: hsl(120, 100%, 40%);">+                     process_byte_tpdu(byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                      break;</span><br><span style="color: hsl(120, 100%, 40%);">+                case ISO7816_S_IN_PPS_REQ:</span><br><span style="color: hsl(120, 100%, 40%);">+            case ISO7816_S_IN_PPS_RSP:</span><br><span style="color: hsl(120, 100%, 40%);">+                    process_byte_pps(byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                       break;</span><br><span style="color: hsl(120, 100%, 40%);">+                default:</span><br><span style="color: hsl(120, 100%, 40%);">+                      TRACE_ERROR("Data received in unknown state %u\n\r", iso_state);</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Handle flags */</span><br><span style="color: hsl(120, 100%, 40%);">+    if (change_flags) { /* WARNING this is not synced with the data buffer handling */</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span> }</span><br><span> #endif /* HAVE_SNIFFER */</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/9873">change 9873</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/9873"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: simtrace2 </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ice7817480705f2124b08c1ff9a8826558b6d8b2b </div>
<div style="display:none"> Gerrit-Change-Number: 9873 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kévin Redon <kredon@sysmocom.de> </div>