<p>Kévin Redon has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/9882">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">sniffer: add waiting time WT update<br><br>the waiting time (WT) is used to detect timeouts (e.g. for<br>unresponsive card or just to signal an error/NAK). it is essential<br>to detect to timeout to end current data transfer (e.g. change state).<br>by default (after a reset) to timeout is 9600 ETU, but this can change<br>at two places:<br>- after the ATR using the value of TC2 (if present)<br>- after a PPS (only F is used, and not D)<br><br>because the timeout value can be larger than the 16-bit of the USART<br>TO register, an external variable needs to be used for the count down.<br><br>Change-Id: I9735660ffce161cec8d4e63fa60a66fc8ef57525<br>---<br>M firmware/libcommon/source/sniffer.c<br>1 file changed, 66 insertions(+), 6 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/82/9882/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 70d3734..e6fe68f 100644</span><br><span>--- a/firmware/libcommon/source/sniffer.c</span><br><span>+++ b/firmware/libcommon/source/sniffer.c</span><br><span>@@ -178,6 +178,11 @@</span><br><span> /*! Current index in TPDU packet */</span><br><span> uint16_t tpdu_packet_i = 0;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! Waiting Time (WT)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+uint32_t wt = 9600;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*------------------------------------------------------------------------------</span><br><span>  *         Internal functions</span><br><span>  *------------------------------------------------------------------------------*/</span><br><span>@@ -188,6 +193,27 @@</span><br><span>  */</span><br><span> static const uint8_t convention_convert_lut[256] = { 0xff, 0x7f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x62, 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%);">+/*! Update Waiting Time (WT)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @param[in] wi Waiting Integer (0 if unchanged)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @param[in] d Baud Rate divider (0 if unchanged)</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note set wt to be used by the receiver timeout</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note defined in ISO/IEC 7816-3:2006(E) section 8.1 and 10.2</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void update_wt(uint8_t wi, uint8_t d)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      static uint8_t wt_wi = 10; /* Waiting time Integer (WI), used to calculate the Waiting Time (WT) */</span><br><span style="color: hsl(120, 100%, 40%);">+   static uint8_t wt_d = 1; /* baud rate adjustment integer (the actual value, not the table index) */</span><br><span style="color: hsl(120, 100%, 40%);">+   </span><br><span style="color: hsl(120, 100%, 40%);">+      if (0!=wi) {</span><br><span style="color: hsl(120, 100%, 40%);">+          wt_wi = wi;</span><br><span style="color: hsl(120, 100%, 40%);">+   }</span><br><span style="color: hsl(120, 100%, 40%);">+     if (0!=d) {</span><br><span style="color: hsl(120, 100%, 40%);">+           wt_d = d;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+     wt = wt_wi*960UL*wt_d;</span><br><span style="color: hsl(120, 100%, 40%);">+        TRACE_INFO("WT updated to %u\n\r", wt);</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>@@ -203,6 +229,7 @@</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%);">+            update_wt(10, 1); /* reset WT time-out */</span><br><span>            change_flags |= SNIFF_CHANGE_FLAG_RESET_HOLD; /* set flag and let main loop send it */</span><br><span>               break;</span><br><span>       case ISO7816_S_WAIT_ATR:</span><br><span>@@ -287,6 +314,7 @@</span><br><span> {</span><br><span>  static uint8_t atr_hist_len = 0; /* store the number of expected historical bytes */</span><br><span>         static uint8_t y = 0; /* last mask of the upcoming TA, TB, TC, TD interface bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+  static uint8_t i = 0; /* interface byte subgroup number */</span><br><span> </span><br><span>       /* sanity check */</span><br><span>   if (ISO7816_S_IN_ATR!=iso_state) {</span><br><span>@@ -316,6 +344,7 @@</span><br><span>                     atr_i--; /* revert last byte */</span><br><span>                      TRACE_WARNING("Invalid TS received\n\r");</span><br><span>          }</span><br><span style="color: hsl(120, 100%, 40%);">+             i = 0; /* first interface byte sub-group is coming (T0 is kind of TD0) */</span><br><span>            break;</span><br><span>       case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */</span><br><span>      case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span>@@ -325,6 +354,7 @@</span><br><span>                        t_protocol_support |= (1<<(byte&0x0f)); /* remember supported protocol to know if TCK will be present */</span><br><span>           }</span><br><span>            y = (byte&0xf0); /* remember upcoming interface bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+          i++; /* next interface byte sub-group is coming */</span><br><span>           if (y&0x10) {</span><br><span>                    atr_state = ATR_S_WAIT_TA; /* wait for interface byte TA */</span><br><span>                  break;</span><br><span>@@ -340,6 +370,14 @@</span><br><span>                        break;</span><br><span>               }</span><br><span>    case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span style="color: hsl(120, 100%, 40%);">+               /* retrieve WI encoded in TC2*/</span><br><span style="color: hsl(120, 100%, 40%);">+               if (ATR_S_WAIT_TC==atr_state && 2==i) {</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (0==byte) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                update_wt(10, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                     } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              update_wt(byte, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                   }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span>            if (y&0x80) {</span><br><span>                    atr_state = ATR_S_WAIT_TD; /* wait for interface byte TD */</span><br><span>                  break;</span><br><span>@@ -548,6 +586,7 @@</span><br><span>                                 }</span><br><span>                            TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]);</span><br><span>                             update_fidi(sniff_usart.base, pps_cur[2]);</span><br><span style="color: hsl(120, 100%, 40%);">+                            update_wt(0, di_table[dn]);</span><br><span>                          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>@@ -706,6 +745,9 @@</span><br><span> /*! Interrupt Service Routine called on USART activity */</span><br><span> void Sniffer_usart_isr(void)</span><br><span> {</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Remaining Waiting Time (WI) counter (>16 bits) */</span><br><span style="color: hsl(120, 100%, 40%);">+       static volatile uint32_t wt_remaining = 9600;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>      /* Read channel status register */</span><br><span>   uint32_t csr = sniff_usart.base->US_CSR;</span><br><span>  /* Verify if there was an error */</span><br><span>@@ -717,10 +759,13 @@</span><br><span>           TRACE_WARNING("USART framing error\n\r");</span><br><span>          sniff_usart.base->US_CR |= US_CR_RSTSTA;</span><br><span>  }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  /* Verify if character has been received */</span><br><span>  if (csr & US_CSR_RXRDY) {</span><br><span>                /* Read communication data byte between phone and SIM */</span><br><span>             uint8_t byte = sniff_usart.base->US_RHR;</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Reset WT timer */</span><br><span style="color: hsl(120, 100%, 40%);">+          wt_remaining = wt;</span><br><span>           /* Store sniffed data into buffer (also clear interrupt */</span><br><span>           if (rbuf_is_full(&sniff_buffer)) {</span><br><span>                       TRACE_ERROR("USART buffer full\n\r");</span><br><span>@@ -728,13 +773,28 @@</span><br><span>                      rbuf_write(&sniff_buffer, byte);</span><br><span>                 }</span><br><span>    }</span><br><span style="color: hsl(0, 100%, 40%);">-       </span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>   /* Verify it WT timeout occurred, to detect unresponsive card */</span><br><span>     if (csr & US_CSR_TIMEOUT) {</span><br><span style="color: hsl(0, 100%, 40%);">-         /* Stop timeout until next character is received */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (wt_remaining<=(sniff_usart.base->US_RTOR&0xffff)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     /* Just set the flag and let the main loop handle it */</span><br><span style="color: hsl(120, 100%, 40%);">+                       change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT;</span><br><span style="color: hsl(120, 100%, 40%);">+                 /* Reset timeout value */</span><br><span style="color: hsl(120, 100%, 40%);">+                     wt_remaining = wt;</span><br><span style="color: hsl(120, 100%, 40%);">+            } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      wt_remaining -= (sniff_usart.base->US_RTOR&0xffff); /* be sure to subtract the actual timeout since the new might not have been set and reloaded yet */</span><br><span style="color: hsl(120, 100%, 40%);">+                }</span><br><span style="color: hsl(120, 100%, 40%);">+             if (wt_remaining>0xffff) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 sniff_usart.base->US_RTOR = 0xffff;</span><br><span style="color: hsl(120, 100%, 40%);">+                } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                      sniff_usart.base->US_RTOR = wt_remaining;</span><br><span style="color: hsl(120, 100%, 40%);">+          }</span><br><span style="color: hsl(120, 100%, 40%);">+             /* Stop timeout until next character is received (and clears the timeout flag) */</span><br><span>            sniff_usart.base->US_CR |= US_CR_STTTO;</span><br><span style="color: hsl(0, 100%, 40%);">-              /* Just set the flag and let the main loop handle it */</span><br><span style="color: hsl(0, 100%, 40%);">-         change_flags |= SNIFF_CHANGE_FLAG_TIMEOUT_WT;</span><br><span style="color: hsl(120, 100%, 40%);">+         if (!(change_flags & SNIFF_CHANGE_FLAG_TIMEOUT_WT)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                     /* Immediately restart the counter it the WT timeout did not occur (needs the timeout flag to be cleared) */</span><br><span style="color: hsl(120, 100%, 40%);">+                  sniff_usart.base->US_CR |= US_CR_RETTO;</span><br><span style="color: hsl(120, 100%, 40%);">+            }</span><br><span>    }</span><br><span> }</span><br><span> </span><br><span>@@ -825,8 +885,8 @@</span><br><span>     ISO7816_Init(&sniff_usart, CLK_SLAVE);</span><br><span>   /* Only receive data when sniffing */</span><br><span>        USART_SetReceiverEnabled(sniff_usart.base, 1);</span><br><span style="color: hsl(0, 100%, 40%);">-  /* Enable Receiver time-out WT to detect unresponsive cards */</span><br><span style="color: hsl(0, 100%, 40%);">-  sniff_usart.base->US_RTOR = 9600-12; /* -12 because the timer starts at the end of the 12 ETU frame */</span><br><span style="color: hsl(120, 100%, 40%);">+     /* Enable Receiver time-out to detect waiting time (WT) time-out (e.g. unresponsive cards) */</span><br><span style="color: hsl(120, 100%, 40%);">+ sniff_usart.base->US_RTOR = wt;</span><br><span>   /* Enable interrupt to indicate when data has been received or timeout occurred */</span><br><span>   USART_EnableIt(sniff_usart.base, US_IER_RXRDY | US_IER_TIMEOUT);</span><br><span>     /* Set USB priority lower than USART to not miss sniffing data (both at 0 per default) */</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/9882">change 9882</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/9882"/><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: I9735660ffce161cec8d4e63fa60a66fc8ef57525 </div>
<div style="display:none"> Gerrit-Change-Number: 9882 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kévin Redon <kredon@sysmocom.de> </div>