<p>laforge <strong>merged</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/osmo-ccid-firmware/+/15690">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">ISO7816-3 FSMs as osmo_fsm<br><br>This implements ISO 7816-3 T=0 as three finite state machines using<br>osmo_fsm.<br><br>Change-Id: I0145b77e6165d36d33f18ef3a452f2c37913bd73<br>---<br>M ccid_common/ccid_device.h<br>A ccid_common/iso7816_fsm.c<br>A ccid_common/iso7816_fsm.h<br>M ccid_host/Makefile<br>M ccid_host/ccid_main_functionfs.c<br>A ccid_host/logging.c<br>A ccid_host/logging.h<br>M sysmoOCTSIM/libosmo_emb.c<br>M sysmoOCTSIM/logging.h<br>9 files changed, 1,318 insertions(+), 29 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/ccid_common/ccid_device.h b/ccid_common/ccid_device.h</span><br><span>index afd4a7a..ae3ab5b 100644</span><br><span>--- a/ccid_common/ccid_device.h</span><br><span>+++ b/ccid_common/ccid_device.h</span><br><span>@@ -3,11 +3,7 @@</span><br><span> #include <stdint.h></span><br><span> </span><br><span> #include "ccid_proto.h"</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-enum {</span><br><span style="color: hsl(0, 100%, 40%);">-  DCCID,</span><br><span style="color: hsl(0, 100%, 40%);">-  DUSB,</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(120, 100%, 40%);">+#include "logging.h"</span><br><span> </span><br><span> #define NR_SLOTS 8</span><br><span> </span><br><span>diff --git a/ccid_common/iso7816_fsm.c b/ccid_common/iso7816_fsm.c</span><br><span>new file mode 100644</span><br><span>index 0000000..f8fe14a</span><br><span>--- /dev/null</span><br><span>+++ b/ccid_common/iso7816_fsm.c</span><br><span>@@ -0,0 +1,1178 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* ISO 7816-3 Finite State Machine (reader side)</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2019 by Harald Welte <laforge@gnumonks.org></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * inspired by earlier work</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de></span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; either version 2 of the License, or</span><br><span style="color: hsl(120, 100%, 40%);">+ * (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA</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%);">+#include <string.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/utils.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 <osmocom/core/fsm.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/logging.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/sim/sim.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "logging.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "cuart.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "iso7816_fsm.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Section 8.2: the Answer-to-Reset (... a string of at most 32 bytes) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define MAX_ATR_SIZE 32</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define S(x)    (1 << (x))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! ISO 7816-3 states */</span><br><span style="color: hsl(120, 100%, 40%);">+enum iso7816_3_state {</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_S_RESET, /*!< in Reset */</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_S_WAIT_ATR, /*!< waiting for ATR to start */</span><br><span style="color: hsl(120, 100%, 40%);">+       ISO7816_S_IN_ATR, /*!< while we are receiving the ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+   ISO7816_S_WAIT_TPDU, /*!< waiting for start of new TPDU */</span><br><span style="color: hsl(120, 100%, 40%);">+ ISO7816_S_IN_TPDU, /*!< inside a single TPDU */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_S_IN_PPS_REQ, /*!< while we are inside the PPS request */</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_S_WAIT_PPS_RSP, /*!< waiting for start of the PPS response */</span><br><span style="color: hsl(120, 100%, 40%);">+      ISO7816_S_IN_PPS_RSP, /*!< while we are inside the PPS request */</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%);">+/*! Answer-To-Reset (ATR) sub-states of ISO7816_S_IN_ATR</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note defined in ISO/IEC 7816-3:2006(E) section 8</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+enum atr_state {</span><br><span style="color: hsl(120, 100%, 40%);">+    ATR_S_WAIT_TS, /*!< initial byte */</span><br><span style="color: hsl(120, 100%, 40%);">+        ATR_S_WAIT_T0, /*!< format byte */</span><br><span style="color: hsl(120, 100%, 40%);">+ ATR_S_WAIT_TA, /*!< first sub-group interface byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      ATR_S_WAIT_TB, /*!< second sub-group interface byte */</span><br><span style="color: hsl(120, 100%, 40%);">+     ATR_S_WAIT_TC, /*!< third sub-group interface byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      ATR_S_WAIT_TD, /*!< fourth sub-group interface byte */</span><br><span style="color: hsl(120, 100%, 40%);">+     ATR_S_WAIT_HIST, /*!< historical byte */</span><br><span style="color: hsl(120, 100%, 40%);">+   ATR_S_WAIT_TCK, /*!< check byte */</span><br><span style="color: hsl(120, 100%, 40%);">+ ATR_S_DONE</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%);">+/*! Protocol and Parameters Selection (PPS) sub-states of ISO7816_S_IN_PTS_REQ/ISO7816_S_IN_PTS_RSP</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note defined in ISO/IEC 7816-3:2006(E) section 9</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+enum pps_state {</span><br><span style="color: hsl(120, 100%, 40%);">+   PPS_S_WAIT_PPSS, /*!< initial byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      PPS_S_WAIT_PPS0, /*!< format byte */</span><br><span style="color: hsl(120, 100%, 40%);">+       PPS_S_WAIT_PPS1, /*!< first parameter byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      PPS_S_WAIT_PPS2, /*!< second parameter byte */</span><br><span style="color: hsl(120, 100%, 40%);">+     PPS_S_WAIT_PPS3, /*!< third parameter byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      PPS_S_WAIT_PCK, /*!< check byte */</span><br><span style="color: hsl(120, 100%, 40%);">+ PPS_S_WAIT_END, /*!< all done */</span><br><span style="color: hsl(120, 100%, 40%);">+   PPS_S_DONE</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%);">+/*! Transport Protocol Data Unit (TPDU) sub-states of ISO7816_S_IN_TPDU</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @note defined in ISO/IEC 7816-3:2006(E) section 10 and 12</span><br><span style="color: hsl(120, 100%, 40%);">+ *  @remark APDUs are formed by one or more command+response TPDUs</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+enum tpdu_state {</span><br><span style="color: hsl(120, 100%, 40%);">+  TPDU_S_INIT, /*!< initial state */</span><br><span style="color: hsl(120, 100%, 40%);">+ TPDU_S_TX_HDR, /*!< transmitting hdr, waiting for completion */</span><br><span style="color: hsl(120, 100%, 40%);">+    TPDU_S_PROCEDURE, /*!< procedure byte (could also be SW1) */</span><br><span style="color: hsl(120, 100%, 40%);">+       TPDU_S_TX_REMAINING, /*!< Tx remaining data bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+       TPDU_S_TX_SINGLE, /*!< Tx single data byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      TPDU_S_RX_REMAINING, /*!< Rx remaining data bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+       TPDU_S_RX_SINGLE, /*!< Rx single data byte */</span><br><span style="color: hsl(120, 100%, 40%);">+      TPDU_S_SW1, /*!< first status word */</span><br><span style="color: hsl(120, 100%, 40%);">+      TPDU_S_SW2, /*!< second status word */</span><br><span style="color: hsl(120, 100%, 40%);">+     TPDU_S_DONE,</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%);">+/* FSM timer enumeration */</span><br><span style="color: hsl(120, 100%, 40%);">+enum iso7816_3_timer {</span><br><span style="color: hsl(120, 100%, 40%);">+       T_WAIT_ATR = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+       T_GUARD,</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%);">+/* forward declarations */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fsm iso7816_3_fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fsm atr_fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fsm tpdu_fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+static struct osmo_fsm pps_fsm;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* look-up table for bit-wise inversion to convert from "inverse convention" to normal */</span><br><span style="color: hsl(120, 100%, 40%);">+static const uint8_t convention_convert_lut[256] = {</span><br><span style="color: hsl(120, 100%, 40%);">+    0xff, 0x7f, 0xbf, 0x3f, 0xdf, 0x5f, 0x9f, 0x1f, 0xef, 0x6f, 0xaf, 0x2f, 0xcf, 0x4f, 0x8f, 0x0f,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf7, 0x77, 0xb7, 0x37, 0xd7, 0x57, 0x97, 0x17, 0xe7, 0x67, 0xa7, 0x27, 0xc7, 0x47, 0x87, 0x07,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xfb, 0x7b, 0xbb, 0x3b, 0xdb, 0x5b, 0x9b, 0x1b, 0xeb, 0x6b, 0xab, 0x2b, 0xcb, 0x4b, 0x8b, 0x0b,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf3, 0x73, 0xb3, 0x33, 0xd3, 0x53, 0x93, 0x13, 0xe3, 0x63, 0xa3, 0x23, 0xc3, 0x43, 0x83, 0x03,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xfd, 0x7d, 0xbd, 0x3d, 0xdd, 0x5d, 0x9d, 0x1d, 0xed, 0x6d, 0xad, 0x2d, 0xcd, 0x4d, 0x8d, 0x0d,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf5, 0x75, 0xb5, 0x35, 0xd5, 0x55, 0x95, 0x15, 0xe5, 0x65, 0xa5, 0x25, 0xc5, 0x45, 0x85, 0x05,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf9, 0x79, 0xb9, 0x39, 0xd9, 0x59, 0x99, 0x19, 0xe9, 0x69, 0xa9, 0x29, 0xc9, 0x49, 0x89, 0x09,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf1, 0x71, 0xb1, 0x31, 0xd1, 0x51, 0x91, 0x11, 0xe1, 0x61, 0xa1, 0x21, 0xc1, 0x41, 0x81, 0x01,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xfe, 0x7e, 0xbe, 0x3e, 0xde, 0x5e, 0x9e, 0x1e, 0xee, 0x6e, 0xae, 0x2e, 0xce, 0x4e, 0x8e, 0x0e,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf6, 0x76, 0xb6, 0x36, 0xd6, 0x56, 0x96, 0x16, 0xe6, 0x66, 0xa6, 0x26, 0xc6, 0x46, 0x86, 0x06,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xfa, 0x7a, 0xba, 0x3a, 0xda, 0x5a, 0x9a, 0x1a, 0xea, 0x6a, 0xaa, 0x2a, 0xca, 0x4a, 0x8a, 0x0a,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf2, 0x72, 0xb2, 0x32, 0xd2, 0x52, 0x92, 0x12, 0xe2, 0x62, 0xa2, 0x22, 0xc2, 0x42, 0x82, 0x02,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xfc, 0x7c, 0xbc, 0x3c, 0xdc, 0x5c, 0x9c, 0x1c, 0xec, 0x6c, 0xac, 0x2c, 0xcc, 0x4c, 0x8c, 0x0c,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf4, 0x74, 0xb4, 0x34, 0xd4, 0x54, 0x94, 0x14, 0xe4, 0x64, 0xa4, 0x24, 0xc4, 0x44, 0x84, 0x04,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf8, 0x78, 0xb8, 0x38, 0xd8, 0x58, 0x98, 0x18, 0xe8, 0x68, 0xa8, 0x28, 0xc8, 0x48, 0x88, 0x08,</span><br><span style="color: hsl(120, 100%, 40%);">+       0xf0, 0x70, 0xb0, 0x30, 0xd0, 0x50, 0x90, 0x10, 0xe0, 0x60, 0xa0, 0x20, 0xc0, 0x40, 0x80, 0x00,</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%);">+ * ISO7816-3 Main FSM</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%);">+static const struct value_string iso7816_3_event_names[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+     { ISO7816_E_RX_SINGLE,          "UART_RX_SINGLE" },</span><br><span style="color: hsl(120, 100%, 40%);">+ { ISO7816_E_RX_COMPL,           "UART_RX_COMPL" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_TX_COMPL,           "UART_TX_COMPL" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_POWER_UP_IND,       "POWER_UP_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+   { ISO7816_E_RESET_REL_IND,      "RESET_REL_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_RX_ERR_IND,         "RX_ERR_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { ISO7816_E_TX_ERR_IND,         "TX_ERR_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { ISO7816_E_ATR_DONE_IND,       "ATR_DONE_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+   { ISO7816_E_TPDU_DONE_IND,      "TPDU_DONE_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_XCEIVE_TPDU_CMD,    "XCEIVE_TPDU_CMD" },</span><br><span style="color: hsl(120, 100%, 40%);">+        /* allstate events */</span><br><span style="color: hsl(120, 100%, 40%);">+ { ISO7816_E_WTIME_EXP,          "WAIT_TIME_EXP" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_HW_ERR_IND,         "HW_ERR_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { ISO7816_E_SW_ERR_IND,         "SW_ERR_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+     { ISO7816_E_CARD_REMOVAL,       "CARD_REMOVAL" },</span><br><span style="color: hsl(120, 100%, 40%);">+   { ISO7816_E_POWER_DN_IND,       "POWER_DN_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+   { ISO7816_E_RESET_ACT_IND,      "RESET_ACT_IND" },</span><br><span style="color: hsl(120, 100%, 40%);">+  { ISO7816_E_ABORT_REQ,          "ABORT_REQ" },</span><br><span style="color: hsl(120, 100%, 40%);">+      { ISO7816_E_TPDU_CLEAR_REQ,     "TPDU_CLEAR_REQ" },</span><br><span style="color: hsl(120, 100%, 40%);">+ { 0, NULL }</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%);">+struct iso7816_3_priv {</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t slot_nr;</span><br><span style="color: hsl(120, 100%, 40%);">+      /* child FSM instances */</span><br><span style="color: hsl(120, 100%, 40%);">+     struct osmo_fsm_inst *atr_fi;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct osmo_fsm_inst *pps_fi;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct osmo_fsm_inst *tpdu_fi;</span><br><span style="color: hsl(120, 100%, 40%);">+        /* other data */</span><br><span style="color: hsl(120, 100%, 40%);">+      bool convention_convert;/*!< If convention conversion is needed */</span><br><span style="color: hsl(120, 100%, 40%);">+ uint16_t guard_time_ms;</span><br><span style="color: hsl(120, 100%, 40%);">+       /* underlying UART */</span><br><span style="color: hsl(120, 100%, 40%);">+ struct card_uart *uart;</span><br><span style="color: hsl(120, 100%, 40%);">+       iso7816_user_cb user_cb;</span><br><span style="color: hsl(120, 100%, 40%);">+      void *user_priv;</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%);">+/* type-safe method to obtain iso7816_3_priv from fi */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct iso7816_3_priv *get_iso7816_3_priv(struct osmo_fsm_inst *fi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        OSMO_ASSERT(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+      OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+        return (struct iso7816_3_priv *) fi->priv;</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%);">+/* convert from clock cycles of the CLK line to milli-seconds */</span><br><span style="color: hsl(120, 100%, 40%);">+uint32_t fi_cycles2ms(struct osmo_fsm_inst *fi, uint32_t cyclces)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+        /* FIXME */</span><br><span style="color: hsl(120, 100%, 40%);">+   return 1000;</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%);">+/* card UART notifies us: dispatch to (main ISO7816-3) FSM */</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_uart_notification(struct card_uart *cuart, enum card_uart_event evt, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct osmo_fsm_inst *fi = (struct osmo_fsm_inst *) cuart->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      LOGPFSML(fi, LOGL_DEBUG, "UART Notification '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                 get_value_string(card_uart_event_vals, evt));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* FIXME: Set only flags here; Main loop polls flags and dispatches events */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (evt) {</span><br><span style="color: hsl(120, 100%, 40%);">+        case CUART_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_dispatch(fi, ISO7816_E_RX_SINGLE, data);</span><br><span style="color: hsl(120, 100%, 40%);">+                break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case CUART_E_RX_COMPLETE:</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_dispatch(fi, ISO7816_E_RX_COMPL, data);</span><br><span style="color: hsl(120, 100%, 40%);">+         break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case CUART_E_RX_TIMEOUT:</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_dispatch(fi, ISO7816_E_WTIME_EXP, data);</span><br><span style="color: hsl(120, 100%, 40%);">+                break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case CUART_E_TX_COMPLETE:</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_dispatch(fi, ISO7816_E_TX_COMPL, data);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void iso7816_3_reset_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* go back to initial state in child FSMs */</span><br><span style="color: hsl(120, 100%, 40%);">+  osmo_fsm_inst_state_chg(ip->atr_fi, ATR_S_WAIT_TS, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+  //osmo_fsm_inst_state_chg(ip->pps_fi, PPS_S_WAIT_PPSS, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+      osmo_fsm_inst_state_chg(ip->tpdu_fi, TPDU_S_INIT, 0, 0);</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%);">+static void iso7816_3_reset_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RESET_REL_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+         /* TOOD: this should happen before reset is released */</span><br><span style="color: hsl(120, 100%, 40%);">+               card_uart_ctrl(ip->uart, CUART_CTL_RX, true);</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg_ms(fi, ISO7816_S_WAIT_ATR,</span><br><span style="color: hsl(120, 100%, 40%);">+                                       fi_cycles2ms(fi, 40000), T_WAIT_ATR);</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%);">+              OSMO_ASSERT(0);</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%);">+static void iso7816_3_wait_atr_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_state_chg(fi, ISO7816_S_IN_ATR, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+          osmo_fsm_inst_dispatch(ip->atr_fi, event, data);</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%);">+              OSMO_ASSERT(0);</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%);">+static void iso7816_3_in_atr_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   struct msgb *atr;</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+     case ISO7816_E_RX_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+            /* simply pass this through to the child FSM for the ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_dispatch(ip->atr_fi, event, data);</span><br><span style="color: hsl(120, 100%, 40%);">+           break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ISO7816_E_ATR_DONE_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+          atr = data;</span><br><span style="color: hsl(120, 100%, 40%);">+           /* FIXME: verify ATR result: success / failure */</span><br><span style="color: hsl(120, 100%, 40%);">+             osmo_fsm_inst_state_chg(fi, ISO7816_S_WAIT_TPDU, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+               /* notify user about ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+           ip->user_cb(fi, event, 0, atr);</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%);">+              OSMO_ASSERT(0);</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%);">+static void iso7816_3_wait_tpdu_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+        /* reset the TPDU state machine */</span><br><span style="color: hsl(120, 100%, 40%);">+    osmo_fsm_inst_dispatch(ip->tpdu_fi, ISO7816_E_TPDU_CLEAR_REQ, NULL);</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%);">+static void iso7816_3_wait_tpdu_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_XCEIVE_TPDU_CMD:</span><br><span style="color: hsl(120, 100%, 40%);">+               /* "data" contains a msgb-wrapped TPDU */</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_state_chg(fi, ISO7816_S_IN_TPDU, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+         /* pass on to sub-fsm */</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_dispatch(ip->tpdu_fi, event, data);</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%);">+              OSMO_ASSERT(0);</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%);">+static void iso7816_3_in_tpdu_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   struct msgb *apdu;</span><br><span style="color: hsl(120, 100%, 40%);">+    OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+     case ISO7816_E_RX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+    case ISO7816_E_TX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_TX_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+            /* simply pass this through to the child FSM for the ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_dispatch(ip->tpdu_fi, event, data);</span><br><span style="color: hsl(120, 100%, 40%);">+          break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ISO7816_E_TPDU_DONE_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+         apdu = data;</span><br><span style="color: hsl(120, 100%, 40%);">+          osmo_fsm_inst_state_chg(fi, ISO7816_S_WAIT_TPDU, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+               /* hand finished TPDU to user */</span><br><span style="color: hsl(120, 100%, 40%);">+              ip->user_cb(fi, event, 0, apdu);</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%);">+              OSMO_ASSERT(0);</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%);">+static void iso7816_3_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        OSMO_ASSERT(fi->fsm == &iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_WTIME_EXP:</span><br><span style="color: hsl(120, 100%, 40%);">+     case ISO7816_E_HW_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+    case ISO7816_E_CARD_REMOVAL:</span><br><span style="color: hsl(120, 100%, 40%);">+          /* FIXME: power off? */</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_state_chg(fi, ISO7816_S_RESET, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+           break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ISO7816_E_POWER_DN_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+  case ISO7816_E_RESET_ACT_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+         osmo_fsm_inst_state_chg(fi, ISO7816_S_RESET, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+           break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ISO7816_E_ABORT_REQ:</span><br><span style="color: hsl(120, 100%, 40%);">+             /* FIXME */</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%);">+              OSMO_ASSERT(0);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const struct osmo_fsm_state iso7816_3_states[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+     [ISO7816_S_RESET] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "RESET",</span><br><span style="color: hsl(120, 100%, 40%);">+            .in_event_mask =        S(ISO7816_E_RESET_REL_IND),</span><br><span style="color: hsl(120, 100%, 40%);">+           .out_state_mask =       S(ISO7816_S_WAIT_ATR) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                       S(ISO7816_S_RESET),</span><br><span style="color: hsl(120, 100%, 40%);">+           .action = iso7816_3_reset_action,</span><br><span style="color: hsl(120, 100%, 40%);">+             .onenter = iso7816_3_reset_onenter,</span><br><span style="color: hsl(120, 100%, 40%);">+   },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_WAIT_ATR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "WAIT_ATR",</span><br><span style="color: hsl(120, 100%, 40%);">+         .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_IN_ATR),</span><br><span style="color: hsl(120, 100%, 40%);">+          .action = iso7816_3_wait_atr_action,</span><br><span style="color: hsl(120, 100%, 40%);">+  },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_IN_ATR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "IN_ATR",</span><br><span style="color: hsl(120, 100%, 40%);">+           .in_event_mask =        S(ISO7816_E_RX_SINGLE) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_E_RX_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                     S(ISO7816_E_ATR_DONE_IND),</span><br><span style="color: hsl(120, 100%, 40%);">+            .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_IN_ATR) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                 S(ISO7816_S_WAIT_TPDU),</span><br><span style="color: hsl(120, 100%, 40%);">+               .action = iso7816_3_in_atr_action,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_WAIT_TPDU] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "WAIT_TPDU",</span><br><span style="color: hsl(120, 100%, 40%);">+                .in_event_mask =        S(ISO7816_E_XCEIVE_TPDU_CMD),</span><br><span style="color: hsl(120, 100%, 40%);">+         .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_WAIT_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_S_IN_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                        S(ISO7816_S_IN_PPS_REQ),</span><br><span style="color: hsl(120, 100%, 40%);">+              .action = iso7816_3_wait_tpdu_action,</span><br><span style="color: hsl(120, 100%, 40%);">+         .onenter = iso7816_3_wait_tpdu_onenter,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_IN_TPDU] = {</span><br><span style="color: hsl(120, 100%, 40%);">+               .name = "IN_TPDU",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_E_RX_COMPL) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                       S(ISO7816_E_TX_COMPL) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                       S(ISO7816_E_RX_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                     S(ISO7816_E_TX_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                     S(ISO7816_E_TPDU_DONE_IND),</span><br><span style="color: hsl(120, 100%, 40%);">+           .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_WAIT_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_S_IN_TPDU),</span><br><span style="color: hsl(120, 100%, 40%);">+         .action = iso7816_3_in_tpdu_action,</span><br><span style="color: hsl(120, 100%, 40%);">+   },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_IN_PPS_REQ] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "IN_PPS_REQ",</span><br><span style="color: hsl(120, 100%, 40%);">+               .in_event_mask =        0, /* FIXME */</span><br><span style="color: hsl(120, 100%, 40%);">+                .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_WAIT_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_S_IN_PPS_REQ) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                     S(ISO7816_S_WAIT_PPS_RSP),</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_WAIT_PPS_RSP] = {</span><br><span style="color: hsl(120, 100%, 40%);">+          .name = "WAIT_PPS_RESP",</span><br><span style="color: hsl(120, 100%, 40%);">+            .in_event_mask =        0, /* FIXME */</span><br><span style="color: hsl(120, 100%, 40%);">+                .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_WAIT_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_S_WAIT_PPS_RSP) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ISO7816_S_IN_PPS_RSP),</span><br><span style="color: hsl(120, 100%, 40%);">+      },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ISO7816_S_IN_PPS_RSP] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "IN_PPS_RESP",</span><br><span style="color: hsl(120, 100%, 40%);">+              .in_event_mask =        0, /* FIXME */</span><br><span style="color: hsl(120, 100%, 40%);">+                .out_state_mask =       S(ISO7816_S_RESET) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ISO7816_S_WAIT_TPDU) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                      S(ISO7816_S_IN_PPS_RSP),</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%);">+static struct osmo_fsm iso7816_3_fsm = {</span><br><span style="color: hsl(120, 100%, 40%);">+      .name = "ISO7816-3",</span><br><span style="color: hsl(120, 100%, 40%);">+        .states = iso7816_3_states,</span><br><span style="color: hsl(120, 100%, 40%);">+   .num_states = ARRAY_SIZE(iso7816_3_states),</span><br><span style="color: hsl(120, 100%, 40%);">+   .log_subsys = DISO7816,</span><br><span style="color: hsl(120, 100%, 40%);">+       .event_names = iso7816_3_event_names,</span><br><span style="color: hsl(120, 100%, 40%);">+ .allstate_action = iso7816_3_allstate_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ .allstate_event_mask =  S(ISO7816_E_WTIME_EXP) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(ISO7816_E_CARD_REMOVAL) |</span><br><span style="color: hsl(120, 100%, 40%);">+                           S(ISO7816_E_POWER_DN_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                           S(ISO7816_E_RESET_ACT_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                          S(ISO7816_E_HW_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                             S(ISO7816_E_ABORT_REQ),</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%);">+ * ATR FSM</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%);">+struct atr_fsm_priv {</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t hist_len;       /*!< store the number of expected historical bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+      uint8_t y;              /*!< last mask of the upcoming TA, TB, TC, TD interface bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t i;              /*!< interface byte subgroup number */</span><br><span style="color: hsl(120, 100%, 40%);">+     struct msgb *atr;       /*!< ATR data */</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t computed_checksum;</span><br><span style="color: hsl(120, 100%, 40%);">+    uint16_t protocol_support;</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%);">+/* obtain the [software] guard time in milli-seconds from the atr fsm_inst */</span><br><span style="color: hsl(120, 100%, 40%);">+static uint32_t atr_fi_gt_ms(struct osmo_fsm_inst *fi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  OSMO_ASSERT(fi->fsm == &atr_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+      OSMO_ASSERT(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+       ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ip->guard_time_ms;</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%);">+/* obtain the 'byte' parmeter of an ISO7816_E_RX event */</span><br><span style="color: hsl(120, 100%, 40%);">+static uint8_t get_rx_byte_evt(struct osmo_fsm_inst *fi, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t byte = *(uint8_t *)data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    /* apply inverse convention */</span><br><span style="color: hsl(120, 100%, 40%);">+        if (ip->convention_convert)</span><br><span style="color: hsl(120, 100%, 40%);">+                byte = convention_convert_lut[byte];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        return byte;</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%);">+/* append a single byte to the ATR */</span><br><span style="color: hsl(120, 100%, 40%);">+static int atr_append_byte(struct osmo_fsm_inst *fi, uint8_t byte)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct atr_fsm_priv *atp = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!msgb_tailroom(atp->atr)) {</span><br><span style="color: hsl(120, 100%, 40%);">+            LOGPFSML(fi, LOGL_ERROR, "ATR overflow !?!");</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_dispatch(fi->proc.parent, ISO7816_E_SW_ERR_IND, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+               return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+     msgb_put_u8(atp->atr, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+       return 0;</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%);">+static void atr_wait_ts_onenter(struct osmo_fsm_inst *fi, uint32_t old_state)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct atr_fsm_priv *atp = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     /* reset state to its initial value */</span><br><span style="color: hsl(120, 100%, 40%);">+        atp->hist_len = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ atp->y = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+        atp->i = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+        if (!atp->atr)</span><br><span style="color: hsl(120, 100%, 40%);">+             atp->atr = msgb_alloc_c(fi, 33, "ATR"); /* TS + 32 chars */</span><br><span style="color: hsl(120, 100%, 40%);">+      else</span><br><span style="color: hsl(120, 100%, 40%);">+          msgb_reset(atp->atr);</span><br><span style="color: hsl(120, 100%, 40%);">+      atp->computed_checksum = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+        atp->protocol_support = 0;</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%);">+static void atr_wait_ts_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct atr_fsm_priv *atp = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+       struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             OSMO_ASSERT(msgb_length(atp->atr) == 0);</span><br><span style="color: hsl(120, 100%, 40%);">+restart:</span><br><span style="color: hsl(120, 100%, 40%);">+         byte = get_rx_byte_evt(parent_fi, data);</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGPFSML(fi, LOGL_DEBUG, "RX byte '%02x'\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+         switch (byte) {</span><br><span style="color: hsl(120, 100%, 40%);">+               case 0x23:</span><br><span style="color: hsl(120, 100%, 40%);">+                    /* direct convention used, but decoded using inverse</span><br><span style="color: hsl(120, 100%, 40%);">+                   * convention (a parity error should also have occurred) */</span><br><span style="color: hsl(120, 100%, 40%);">+                   /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case 0x30:</span><br><span style="color: hsl(120, 100%, 40%);">+                    /* inverse convention used, but decoded using direct</span><br><span style="color: hsl(120, 100%, 40%);">+                   * convention (a parity error should also have occurred) */</span><br><span style="color: hsl(120, 100%, 40%);">+                   ip->convention_convert = !ip->convention_convert;</span><br><span style="color: hsl(120, 100%, 40%);">+                       goto restart;</span><br><span style="color: hsl(120, 100%, 40%);">+                 break;</span><br><span style="color: hsl(120, 100%, 40%);">+                case 0x3b: /* direct convention used and correctly decoded */</span><br><span style="color: hsl(120, 100%, 40%);">+                 /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case 0x3f: /* inverse convention used and correctly decoded */</span><br><span style="color: hsl(120, 100%, 40%);">+                        atr_append_byte(fi, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                    osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_T0, atr_fi_gt_ms(fi), T_GUARD);</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%);">+                      LOGPFSML(fi, LOGL_ERROR, "Invalid TS received: 0x%02X\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                    /* FIXME: somehow indiicate to user */</span><br><span style="color: hsl(120, 100%, 40%);">+                        osmo_fsm_inst_dispatch(fi->proc.parent, ISO7816_E_SW_ERR_IND, NULL);</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%);">+             atp->i = 0; /* first interface byte sub-group is coming (T0 is kind of TD0) */</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%);">+             OSMO_ASSERT(0);</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%);">+static void atr_wait_tX_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct atr_fsm_priv *atp = fi->priv;</span><br><span style="color: hsl(120, 100%, 40%);">+       uint32_t guard_time_ms = atr_fi_gt_ms(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             byte = get_rx_byte_evt(fi->proc.parent, data);</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSML(fi, LOGL_DEBUG, "RX byte '%02x'\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+         atr_append_byte(fi, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+            switch (fi->state) {</span><br><span style="color: hsl(120, 100%, 40%);">+               case ATR_S_WAIT_T0: /* see ISO/IEC 7816-3:2006 section 8.2.2 */</span><br><span style="color: hsl(120, 100%, 40%);">+               case ATR_S_WAIT_TD: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (fi->state == ATR_S_WAIT_T0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          /* save number of hist. bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+                              atp->hist_len = (byte & 0x0f);</span><br><span style="color: hsl(120, 100%, 40%);">+                 } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              /* remember supported protocol to know if TCK will be present */</span><br><span style="color: hsl(120, 100%, 40%);">+                              atp->protocol_support |= (1<<(byte & 0x0f));</span><br><span style="color: hsl(120, 100%, 40%);">+                     }</span><br><span style="color: hsl(120, 100%, 40%);">+                     atp->y = (byte & 0xf0); /* remember incoming interface bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+                        atp->i++;</span><br><span style="color: hsl(120, 100%, 40%);">+                  if (atp->y & 0x10) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_TA, guard_time_ms, T_GUARD);</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%);">+                     /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case ATR_S_WAIT_TA: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (atp->y & 0x20) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_TB, guard_time_ms, T_GUARD);</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%);">+                     /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case ATR_S_WAIT_TB: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (atp->y & 0x40) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_TC, guard_time_ms, T_GUARD);</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%);">+                     /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case ATR_S_WAIT_TC: /* see ISO/IEC 7816-3:2006 section 8.2.3 */</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (atp->y & 0x80) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_TD, guard_time_ms, T_GUARD);</span><br><span style="color: hsl(120, 100%, 40%);">+                                break;</span><br><span style="color: hsl(120, 100%, 40%);">+                        } else if (atp->hist_len) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_HIST, guard_time_ms, T_GUARD);</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%);">+                     /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case ATR_S_WAIT_HIST: /* see ISO/IEC 7816-3:2006 section 8.2.4 */</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (atp->hist_len)</span><br><span style="color: hsl(120, 100%, 40%);">+                         atp->hist_len--;</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (atp->hist_len == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                          if (atp->protocol_support > 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                        /* wait for check byte */</span><br><span style="color: hsl(120, 100%, 40%);">+                                     osmo_fsm_inst_state_chg_ms(fi, ATR_S_WAIT_TCK,</span><br><span style="color: hsl(120, 100%, 40%);">+                                                                   guard_time_ms, T_GUARD);</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%);">+                     } else {</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%);">+                     /* fall-through */</span><br><span style="color: hsl(120, 100%, 40%);">+            case ATR_S_WAIT_TCK: /* see ISO/IEC 7816-3:2006 section 8.2.5 */</span><br><span style="color: hsl(120, 100%, 40%);">+                      /* verify checksum if present */</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (fi->state == ATR_S_WAIT_TCK) {</span><br><span style="color: hsl(120, 100%, 40%);">+                         uint8_t ui;</span><br><span style="color: hsl(120, 100%, 40%);">+                           uint8_t *atr = msgb_data(atp->atr);</span><br><span style="color: hsl(120, 100%, 40%);">+                                LOGPFSML(fi, LOGL_INFO, "Complete ATR: %s\n", msgb_hexdump(atp->atr));</span><br><span style="color: hsl(120, 100%, 40%);">+                           for (ui = 1; ui < msgb_length(atp->atr)-1; ui++) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      atp->computed_checksum ^= atr[ui];</span><br><span style="color: hsl(120, 100%, 40%);">+                         }</span><br><span style="color: hsl(120, 100%, 40%);">+                             if (atp->computed_checksum != byte) {</span><br><span style="color: hsl(120, 100%, 40%);">+                                      /* checkum error. report to user? */</span><br><span style="color: hsl(120, 100%, 40%);">+                                  LOGPFSML(fi, LOGL_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+                                               "computed checksum %02x doesn't match TCK=%02x\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                                              atp->computed_checksum, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                            }</span><br><span style="color: hsl(120, 100%, 40%);">+                             /* ATR complete; notify parent */</span><br><span style="color: hsl(120, 100%, 40%);">+                             osmo_fsm_inst_state_chg(fi, ATR_S_DONE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                                osmo_fsm_inst_dispatch(fi->proc.parent, ISO7816_E_ATR_DONE_IND, atp->atr);</span><br><span style="color: hsl(120, 100%, 40%);">+                      }</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%);">+                      OSMO_ASSERT(0);</span><br><span style="color: hsl(120, 100%, 40%);">+               }</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%);">+              OSMO_ASSERT(0);</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%);">+static const struct osmo_fsm_state atr_states[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+   [ATR_S_WAIT_TS] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_TS",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TS) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_ts_action,</span><br><span style="color: hsl(120, 100%, 40%);">+         .onenter = atr_wait_ts_onenter,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_T0] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_T0",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TA) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TB) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TC) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_HIST) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_TA] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_TA",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TB) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TC) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_HIST) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_TB] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_TB",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TC) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_HIST) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_TC] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_TC",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_HIST) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_TD] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "WAIT_TD",</span><br><span style="color: hsl(120, 100%, 40%);">+          .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TA) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TB) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TC) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_TD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                    S(ATR_S_WAIT_HIST) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                  S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_HIST] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_HIST",</span><br><span style="color: hsl(120, 100%, 40%);">+                .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_WAIT_TCK) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                   S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_WAIT_TCK] = {</span><br><span style="color: hsl(120, 100%, 40%);">+          .name = "WAIT_TCK",</span><br><span style="color: hsl(120, 100%, 40%);">+         .in_event_mask =        S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask =       S(ATR_S_DONE),</span><br><span style="color: hsl(120, 100%, 40%);">+                .action = atr_wait_tX_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [ATR_S_DONE] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "DONE",</span><br><span style="color: hsl(120, 100%, 40%);">+             .in_event_mask =        0,</span><br><span style="color: hsl(120, 100%, 40%);">+            .out_state_mask =       S(ATR_S_WAIT_T0),</span><br><span style="color: hsl(120, 100%, 40%);">+             //.action = atr_done_action,</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%);">+static struct osmo_fsm atr_fsm = {</span><br><span style="color: hsl(120, 100%, 40%);">+  .name = "ATR",</span><br><span style="color: hsl(120, 100%, 40%);">+      .states = atr_states,</span><br><span style="color: hsl(120, 100%, 40%);">+ .num_states = ARRAY_SIZE(atr_states),</span><br><span style="color: hsl(120, 100%, 40%);">+ .log_subsys = DATR,</span><br><span style="color: hsl(120, 100%, 40%);">+   .event_names = iso7816_3_event_names,</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%);">+ * PPS FSM</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%);">+static const struct osmo_fsm_state pps_states[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+   [PPS_S_WAIT_PPSS] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_PPSS",</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_PPS0] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_PPS0",</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_PPS1] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_PPS1",</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_PPS2] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_PPS2",</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_PPS3] = {</span><br><span style="color: hsl(120, 100%, 40%);">+         .name = "WAIT_PPS3",</span><br><span style="color: hsl(120, 100%, 40%);">+        },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_PCK] = {</span><br><span style="color: hsl(120, 100%, 40%);">+          .name = "WAIT_PCK",</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [PPS_S_WAIT_END] = {</span><br><span style="color: hsl(120, 100%, 40%);">+          .name = "WAIT_END",</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%);">+static struct osmo_fsm pps_fsm = {</span><br><span style="color: hsl(120, 100%, 40%);">+  .name = "PPS",</span><br><span style="color: hsl(120, 100%, 40%);">+      .states = pps_states,</span><br><span style="color: hsl(120, 100%, 40%);">+ .num_states = ARRAY_SIZE(pps_states),</span><br><span style="color: hsl(120, 100%, 40%);">+ .log_subsys = DPPS,</span><br><span style="color: hsl(120, 100%, 40%);">+   .event_names = iso7816_3_event_names,</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%);">+ * TPDU FSM</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%);">+struct tpdu_fsm_priv {</span><br><span style="color: hsl(120, 100%, 40%);">+       struct msgb *tpdu;</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%);">+/* type-safe method to obtain iso7816_3_priv from fi */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct tpdu_fsm_priv *get_tpdu_fsm_priv(struct osmo_fsm_inst *fi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        OSMO_ASSERT(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+      OSMO_ASSERT(fi->fsm == &tpdu_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+     return (struct tpdu_fsm_priv *) fi->priv;</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%);">+static void tpdu_s_init_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osim_apdu_cmd_hdr *tpduh;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_XCEIVE_TPDU_CMD:</span><br><span style="color: hsl(120, 100%, 40%);">+               /* start transmission of a TPDU by sending the 5-byte header */</span><br><span style="color: hsl(120, 100%, 40%);">+               tfp->tpdu = (struct msgb *)data;</span><br><span style="color: hsl(120, 100%, 40%);">+           OSMO_ASSERT(msgb_length(tfp->tpdu) >= sizeof(*tpduh));</span><br><span style="color: hsl(120, 100%, 40%);">+          tfp->tpdu->l2h = msgb_data(tfp->tpdu) + sizeof(*tpduh);</span><br><span style="color: hsl(120, 100%, 40%);">+              tpduh = msgb_data(tfp->tpdu);</span><br><span style="color: hsl(120, 100%, 40%);">+              LOGPFSML(fi, LOGL_DEBUG, "Transmitting TPDU header %s via UART\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                   osmo_hexdump_nospc((uint8_t *) tpduh, sizeof(*tpduh)));</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, TPDU_S_TX_HDR, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+             card_uart_tx(ip->uart, (uint8_t *) tpduh, sizeof(*tpduh), true);</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%);">+              OSMO_ASSERT(0);</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%);">+static void tpdu_s_tx_hdr_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(fi->fsm == &tpdu_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+     switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_TX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* Rx of single byte is already enabled by previous card_uart_tx() call */</span><br><span style="color: hsl(120, 100%, 40%);">+            osmo_fsm_inst_state_chg(fi, TPDU_S_PROCEDURE, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_s_procedure_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osim_apdu_cmd_hdr *tpduh = msgb_data(tfp->tpdu);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             byte = get_rx_byte_evt(fi->proc.parent, data);</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSML(fi, LOGL_DEBUG, "Received 0x%02x from UART\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              if (byte == 0x60) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   /* NULL: wait for another procedure byte */</span><br><span style="color: hsl(120, 100%, 40%);">+                   osmo_fsm_inst_state_chg(fi, TPDU_S_PROCEDURE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+          } else if ((byte >= 0x60 && byte <= 0x6f) || (byte >= 0x90 && byte <= 0x9f)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                    //msgb_apdu_sw(tfp->apdu) = byte << 8;</span><br><span style="color: hsl(120, 100%, 40%);">+                       msgb_put(tfp->tpdu, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+                 /* receive second SW byte (SW2) */</span><br><span style="color: hsl(120, 100%, 40%);">+                    osmo_fsm_inst_state_chg(fi, TPDU_S_SW2, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                        break;</span><br><span style="color: hsl(120, 100%, 40%);">+                } else if (byte == tpduh->ins) {</span><br><span style="color: hsl(120, 100%, 40%);">+                   if (msgb_l2len(tfp->tpdu)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                               /* transmit all remaining bytes */</span><br><span style="color: hsl(120, 100%, 40%);">+                            card_uart_tx(ip->uart, msgb_l2(tfp->tpdu), msgb_l2len(tfp->tpdu), true);</span><br><span style="color: hsl(120, 100%, 40%);">+                             osmo_fsm_inst_state_chg(fi, TPDU_S_TX_REMAINING, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                       } else {</span><br><span style="color: hsl(120, 100%, 40%);">+                              card_uart_set_rx_threshold(ip->uart, tpduh->p3);</span><br><span style="color: hsl(120, 100%, 40%);">+                                osmo_fsm_inst_state_chg(fi, TPDU_S_RX_REMAINING, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                       }</span><br><span style="color: hsl(120, 100%, 40%);">+             } else if (byte == (tpduh->ins ^ 0xFF)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  osmo_panic("unsupported single-byte T=0 case");</span><br><span style="color: hsl(120, 100%, 40%);">+                     /* FIXME: transmit single byte then wait for proc */</span><br><span style="color: hsl(120, 100%, 40%);">+                  //osmo_fsm_inst_state_chg(fi, TPDU_S_xX_SINGLE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                        //osmo_fsm_inst_state_chg(fi, TPDU_S_PROCEDURE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                } else</span><br><span style="color: hsl(120, 100%, 40%);">+                        OSMO_ASSERT(0);</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%);">+              OSMO_ASSERT(0);</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%);">+/* UART is transmitting remaining data; we wait for ISO7816_E_TX_COMPL */</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_s_tx_remaining_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_TX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+              card_uart_set_rx_threshold(ip->uart, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_state_chg(fi, TPDU_S_SW1, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+/* UART is transmitting single byte of data; we wait for ISO7816_E_TX_COMPL */</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_s_tx_single_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_TX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* TODO: increase pointer/counter? */</span><br><span style="color: hsl(120, 100%, 40%);">+         osmo_fsm_inst_state_chg(fi, TPDU_S_PROCEDURE, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+/* UART is receiving remaining data; we wait for ISO7816_E_RX_COMPL */</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_s_rx_remaining_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osim_apdu_cmd_hdr *tpduh = msgb_data(tfp->tpdu);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_COMPL:</span><br><span style="color: hsl(120, 100%, 40%);">+              /* retrieve pending byte(s) */</span><br><span style="color: hsl(120, 100%, 40%);">+                rc = card_uart_rx(ip->uart, msgb_l2(tfp->tpdu), tpduh->p3);</span><br><span style="color: hsl(120, 100%, 40%);">+          if (rc != tpduh->p3)</span><br><span style="color: hsl(120, 100%, 40%);">+                       LOGPFSML(fi, LOGL_ERROR, "expected %u bytes; read %d\n", tpduh->p3, rc);</span><br><span style="color: hsl(120, 100%, 40%);">+         card_uart_set_rx_threshold(ip->uart, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_state_chg(fi, TPDU_S_SW1, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+/* UART is receiving single byte of data; we wait for ISO7816_E_RX_SINGLE */</span><br><span style="color: hsl(120, 100%, 40%);">+static void tpdu_s_rx_single_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             byte = get_rx_byte_evt(fi->proc.parent, data);</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSML(fi, LOGL_DEBUG, "Received 0x%02x from UART\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              /* TODO: record byte */</span><br><span style="color: hsl(120, 100%, 40%);">+               /* FIXME: determine if number of expected bytes received */</span><br><span style="color: hsl(120, 100%, 40%);">+           if (0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                      osmo_fsm_inst_state_chg(fi, TPDU_S_SW1, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+                } else</span><br><span style="color: hsl(120, 100%, 40%);">+                        osmo_fsm_inst_state_chg(fi, TPDU_S_PROCEDURE, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+static void tpdu_s_sw1_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             byte = get_rx_byte_evt(fi->proc.parent, data);</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSML(fi, LOGL_DEBUG, "Received 0x%02x from UART\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              /* record byte */</span><br><span style="color: hsl(120, 100%, 40%);">+             //msgb_apdu_sw(tfp->apdu) = byte << 8;</span><br><span style="color: hsl(120, 100%, 40%);">+               msgb_put_u8(tfp->tpdu, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              card_uart_set_rx_threshold(ip->uart, 1);</span><br><span style="color: hsl(120, 100%, 40%);">+           osmo_fsm_inst_state_chg(fi, TPDU_S_SW2, 0, 0);</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%);">+              OSMO_ASSERT(0);</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%);">+static void tpdu_s_sw2_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        struct tpdu_fsm_priv *tfp = get_tpdu_fsm_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *parent_fi = fi->proc.parent;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct iso7816_3_priv *ip = get_iso7816_3_priv(parent_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+    uint8_t byte;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_SINGLE:</span><br><span style="color: hsl(120, 100%, 40%);">+             byte = get_rx_byte_evt(fi->proc.parent, data);</span><br><span style="color: hsl(120, 100%, 40%);">+             LOGPFSML(fi, LOGL_DEBUG, "Received 0x%02x from UART\n", byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              /* record SW2 byte */</span><br><span style="color: hsl(120, 100%, 40%);">+         //msgb_apdu_sw(tfp->apdu) &= 0xFF00;</span><br><span style="color: hsl(120, 100%, 40%);">+           //msgb_apdu_sw(tfp->apdu) |= byte;</span><br><span style="color: hsl(120, 100%, 40%);">+         msgb_put_u8(tfp->tpdu, byte);</span><br><span style="color: hsl(120, 100%, 40%);">+              osmo_fsm_inst_state_chg(fi, TPDU_S_DONE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+               /* Notify parent FSM */</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_dispatch(fi->proc.parent, ISO7816_E_TPDU_DONE_IND, tfp->tpdu);</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%);">+              OSMO_ASSERT(0);</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%);">+static void tpdu_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     OSMO_ASSERT(fi->fsm == &tpdu_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   switch (event) {</span><br><span style="color: hsl(120, 100%, 40%);">+      case ISO7816_E_RX_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+    case ISO7816_E_TX_ERR_IND:</span><br><span style="color: hsl(120, 100%, 40%);">+            /* FIXME: handle this in some different way */</span><br><span style="color: hsl(120, 100%, 40%);">+                osmo_fsm_inst_state_chg(fi, TPDU_S_DONE, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+               osmo_fsm_inst_dispatch(fi->proc.parent, ISO7816_E_TPDU_DONE_IND, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+            break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ISO7816_E_TPDU_CLEAR_REQ:</span><br><span style="color: hsl(120, 100%, 40%);">+                osmo_fsm_inst_state_chg(fi, TPDU_S_INIT, 0, 0);</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%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const struct osmo_fsm_state tpdu_states[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+  [TPDU_S_INIT] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "INIT",</span><br><span style="color: hsl(120, 100%, 40%);">+             .in_event_mask = S(ISO7816_E_XCEIVE_TPDU_CMD) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                S(ISO7816_E_TX_COMPL),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_TX_HDR),</span><br><span style="color: hsl(120, 100%, 40%);">+           .action = tpdu_s_init_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_TX_HDR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "TX_HDR",</span><br><span style="color: hsl(120, 100%, 40%);">+           .in_event_mask = S(ISO7816_E_TX_COMPL),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_PROCEDURE),</span><br><span style="color: hsl(120, 100%, 40%);">+                .action = tpdu_s_tx_hdr_action,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_PROCEDURE] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "PROCEDURE",</span><br><span style="color: hsl(120, 100%, 40%);">+                .in_event_mask = S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+              .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_PROCEDURE) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                 S(TPDU_S_RX_REMAINING) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_RX_SINGLE) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                 S(TPDU_S_TX_REMAINING) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_TX_SINGLE) |</span><br><span style="color: hsl(120, 100%, 40%);">+                                 S(TPDU_S_SW2),</span><br><span style="color: hsl(120, 100%, 40%);">+              .action = tpdu_s_procedure_action,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_TX_REMAINING] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "TX_REMAINING",</span><br><span style="color: hsl(120, 100%, 40%);">+             .in_event_mask = S(ISO7816_E_TX_COMPL),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_SW1),</span><br><span style="color: hsl(120, 100%, 40%);">+              .action = tpdu_s_tx_remaining_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_TX_SINGLE] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "TX_SINGLE",</span><br><span style="color: hsl(120, 100%, 40%);">+                .in_event_mask = S(ISO7816_E_TX_COMPL),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_PROCEDURE),</span><br><span style="color: hsl(120, 100%, 40%);">+                .action = tpdu_s_tx_single_action,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_RX_REMAINING] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "RX_REMAINING",</span><br><span style="color: hsl(120, 100%, 40%);">+             .in_event_mask = S(ISO7816_E_RX_COMPL),</span><br><span style="color: hsl(120, 100%, 40%);">+               .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_SW1),</span><br><span style="color: hsl(120, 100%, 40%);">+              .action = tpdu_s_rx_remaining_action,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_RX_SINGLE] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "RX_SINGLE",</span><br><span style="color: hsl(120, 100%, 40%);">+                .in_event_mask = S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+              .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_PROCEDURE),</span><br><span style="color: hsl(120, 100%, 40%);">+                .action = tpdu_s_rx_single_action,</span><br><span style="color: hsl(120, 100%, 40%);">+    },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_SW1] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "SW1",</span><br><span style="color: hsl(120, 100%, 40%);">+              .in_event_mask = S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+              .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_SW2),</span><br><span style="color: hsl(120, 100%, 40%);">+              .action = tpdu_s_sw1_action,</span><br><span style="color: hsl(120, 100%, 40%);">+  },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_SW2] = {</span><br><span style="color: hsl(120, 100%, 40%);">+              .name = "SW2",</span><br><span style="color: hsl(120, 100%, 40%);">+              .in_event_mask = S(ISO7816_E_RX_SINGLE),</span><br><span style="color: hsl(120, 100%, 40%);">+              .out_state_mask = S(TPDU_S_INIT) |</span><br><span style="color: hsl(120, 100%, 40%);">+                              S(TPDU_S_DONE),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = tpdu_s_sw2_action,</span><br><span style="color: hsl(120, 100%, 40%);">+  },</span><br><span style="color: hsl(120, 100%, 40%);">+    [TPDU_S_DONE] = {</span><br><span style="color: hsl(120, 100%, 40%);">+             .name = "DONE",</span><br><span style="color: hsl(120, 100%, 40%);">+             .in_event_mask = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+           .out_state_mask = S(TPDU_S_INIT),</span><br><span style="color: hsl(120, 100%, 40%);">+             .action = NULL,</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%);">+static struct osmo_fsm tpdu_fsm = {</span><br><span style="color: hsl(120, 100%, 40%);">+   .name = "TPDU",</span><br><span style="color: hsl(120, 100%, 40%);">+     .states = tpdu_states,</span><br><span style="color: hsl(120, 100%, 40%);">+        .num_states = ARRAY_SIZE(tpdu_states),</span><br><span style="color: hsl(120, 100%, 40%);">+        .allstate_event_mask =  S(ISO7816_E_RX_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                             S(ISO7816_E_TX_ERR_IND) |</span><br><span style="color: hsl(120, 100%, 40%);">+                             S(ISO7816_E_TPDU_CLEAR_REQ),</span><br><span style="color: hsl(120, 100%, 40%);">+  .allstate_action = tpdu_allstate_action,</span><br><span style="color: hsl(120, 100%, 40%);">+      .log_subsys = DTPDU,</span><br><span style="color: hsl(120, 100%, 40%);">+  .event_names = iso7816_3_event_names,</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%);">+struct osmo_fsm_inst *iso7816_fsm_alloc(void *ctx, int log_level, const char *id,</span><br><span style="color: hsl(120, 100%, 40%);">+                                        struct card_uart *cuart, iso7816_user_cb user_cb,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     void *user_priv)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   struct iso7816_3_priv *ip;</span><br><span style="color: hsl(120, 100%, 40%);">+    struct osmo_fsm_inst *fi;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   fi = osmo_fsm_inst_alloc(&iso7816_3_fsm, ctx, NULL, log_level, id);</span><br><span style="color: hsl(120, 100%, 40%);">+       ip = talloc_zero(fi, struct iso7816_3_priv);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (!ip)</span><br><span style="color: hsl(120, 100%, 40%);">+              goto out_fi;</span><br><span style="color: hsl(120, 100%, 40%);">+  fi->priv = ip;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   ip->uart = cuart;</span><br><span style="color: hsl(120, 100%, 40%);">+  cuart->priv = fi;</span><br><span style="color: hsl(120, 100%, 40%);">+  cuart->handle_event = tpdu_uart_notification;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    ip->user_cb = user_cb;</span><br><span style="color: hsl(120, 100%, 40%);">+     ip->user_priv = user_priv;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       ip->atr_fi = osmo_fsm_inst_alloc_child(&atr_fsm, fi, ISO7816_E_SW_ERR_IND);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!ip->atr_fi)</span><br><span style="color: hsl(120, 100%, 40%);">+           goto out_fi;</span><br><span style="color: hsl(120, 100%, 40%);">+  ip->atr_fi->priv = talloc_zero(ip->atr_fi, struct atr_fsm_priv);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!ip->atr_fi->priv)</span><br><span style="color: hsl(120, 100%, 40%);">+          goto out_atr;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       ip->tpdu_fi = osmo_fsm_inst_alloc_child(&tpdu_fsm, fi, ISO7816_E_SW_ERR_IND);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (!ip->tpdu_fi)</span><br><span style="color: hsl(120, 100%, 40%);">+          goto out_atr;</span><br><span style="color: hsl(120, 100%, 40%);">+ ip->tpdu_fi->priv = talloc_zero(ip->tpdu_fi, struct tpdu_fsm_priv);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (!ip->tpdu_fi->priv)</span><br><span style="color: hsl(120, 100%, 40%);">+         goto out_tpdu;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if 0</span><br><span style="color: hsl(120, 100%, 40%);">+       ip->pps_fi = osmo_fsm_inst_alloc_child(&pps_fsm, fi, ISO7816_E_SW_ERR_IND);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (!ip->pps_fi)</span><br><span style="color: hsl(120, 100%, 40%);">+           goto out_tpdu;</span><br><span style="color: hsl(120, 100%, 40%);">+        ip->pps_fi->priv = talloc_zero(ip->pps_fi, struct pps_fsm_priv);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (!ip->pps_fi->priv)</span><br><span style="color: hsl(120, 100%, 40%);">+          goto out_pps;</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* This ensures the 'onenter' function of the initial state is called */</span><br><span style="color: hsl(120, 100%, 40%);">+      osmo_fsm_inst_state_chg(fi, ISO7816_S_RESET, 0, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return fi;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#if 0</span><br><span style="color: hsl(120, 100%, 40%);">+out_pps:</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_fsm_inst_free(ip->pps_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+out_tpdu:</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_fsm_inst_free(ip->tpdu_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+out_atr:</span><br><span style="color: hsl(120, 100%, 40%);">+ osmo_fsm_inst_free(ip->atr_fi);</span><br><span style="color: hsl(120, 100%, 40%);">+out_fi:</span><br><span style="color: hsl(120, 100%, 40%);">+   osmo_fsm_inst_free(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+       cuart->priv = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+        return NULL;</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%);">+void *iso7816_fsm_get_user_priv(struct osmo_fsm_inst *fi)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       struct iso7816_3_priv *ip = get_iso7816_3_priv(fi);</span><br><span style="color: hsl(120, 100%, 40%);">+   return ip->user_priv;</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%);">+static __attribute__((constructor)) void on_dso_load_iso7816(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        osmo_fsm_register(&iso7816_3_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+        osmo_fsm_register(&atr_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+      osmo_fsm_register(&tpdu_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+     osmo_fsm_register(&pps_fsm);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/ccid_common/iso7816_fsm.h b/ccid_common/iso7816_fsm.h</span><br><span>new file mode 100644</span><br><span>index 0000000..9a6b24e</span><br><span>--- /dev/null</span><br><span>+++ b/ccid_common/iso7816_fsm.h</span><br><span>@@ -0,0 +1,40 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/fsm.h></span><br><span style="color: hsl(120, 100%, 40%);">+struct card_uart;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum iso7816_3_event {</span><br><span style="color: hsl(120, 100%, 40%);">+       ISO7816_E_RX_SINGLE,            /*!< single-byte data received on UART */</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_E_RX_COMPL,             /*!< data receive complete on UART */</span><br><span style="color: hsl(120, 100%, 40%);">+      ISO7816_E_TX_COMPL,             /*!< data transmit complete on UART */</span><br><span style="color: hsl(120, 100%, 40%);">+     ISO7816_E_POWER_UP_IND,         /*!< Card powered up */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_E_RESET_REL_IND,        /*!< Reset released */</span><br><span style="color: hsl(120, 100%, 40%);">+     ISO7816_E_RX_ERR_IND,           /*!< Uncorrectable Rx [parity] error */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_E_TX_ERR_IND,           /*!< Uncorrectable Rx [parity] error */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_E_XCEIVE_TPDU_CMD,      /*!< Ask for start of TPDU transmission */</span><br><span style="color: hsl(120, 100%, 40%);">+ /* allstate events */</span><br><span style="color: hsl(120, 100%, 40%);">+ ISO7816_E_WTIME_EXP,            /*!< WTIME expired */</span><br><span style="color: hsl(120, 100%, 40%);">+      ISO7816_E_HW_ERR_IND,           /*!< Hardware error (overcurrent, ...) */</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_E_SW_ERR_IND,           /*!< Software error */</span><br><span style="color: hsl(120, 100%, 40%);">+     ISO7816_E_CARD_REMOVAL,         /*!< card has been removed from slot */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_E_POWER_DN_IND,         /*!< Card powered down */</span><br><span style="color: hsl(120, 100%, 40%);">+  ISO7816_E_RESET_ACT_IND,        /*!< Reset activated */</span><br><span style="color: hsl(120, 100%, 40%);">+    ISO7816_E_ABORT_REQ,            /*!< Abort request (e.g. from CCID) */</span><br><span style="color: hsl(120, 100%, 40%);">+     /* TODO: PPS request */</span><br><span style="color: hsl(120, 100%, 40%);">+       /* TODO: Clock stop request */</span><br><span style="color: hsl(120, 100%, 40%);">+        /* TODO: Rx FIFO overrun */</span><br><span style="color: hsl(120, 100%, 40%);">+   /* TODO: Rx buffer overrun */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* internal events between FSMs in this file */</span><br><span style="color: hsl(120, 100%, 40%);">+       ISO7816_E_ATR_DONE_IND,         /*!< ATR Done indication from ATR child FSM */</span><br><span style="color: hsl(120, 100%, 40%);">+     ISO7816_E_TPDU_DONE_IND,        /*!< TPDU Done indication from TPDU child FSM */</span><br><span style="color: hsl(120, 100%, 40%);">+   ISO7816_E_TPDU_CLEAR_REQ,       /*!< Return TPDU FSM to TPDU_S_INIT */</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%);">+typedef void (*iso7816_user_cb)(struct osmo_fsm_inst *fi, int event, int cause, void *data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct osmo_fsm_inst *iso7816_fsm_alloc(void *ctx, int log_level, const char *id,</span><br><span style="color: hsl(120, 100%, 40%);">+                                    struct card_uart *cuart, iso7816_user_cb user_cb,</span><br><span style="color: hsl(120, 100%, 40%);">+                                     void *ussr_priv);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void *iso7816_fsm_get_user_priv(struct osmo_fsm_inst *fi);</span><br><span>diff --git a/ccid_host/Makefile b/ccid_host/Makefile</span><br><span>index c8862a2..2ed6c83 100644</span><br><span>--- a/ccid_host/Makefile</span><br><span>+++ b/ccid_host/Makefile</span><br><span>@@ -3,6 +3,7 @@</span><br><span> </span><br><span> ccid_functionfs: ccid_main_functionfs.o \</span><br><span>               ccid_slot_sim.o \</span><br><span style="color: hsl(120, 100%, 40%);">+             logging.o \</span><br><span>                  ../ccid_common/ccid_proto.o \</span><br><span>                ../ccid_common/ccid_device.o</span><br><span>        $(CC) $(CFLAGS) -o $@ $^ $(LIBS) -laio</span><br><span>diff --git a/ccid_host/ccid_main_functionfs.c b/ccid_host/ccid_main_functionfs.c</span><br><span>index 26bb525..5c0bc3b 100644</span><br><span>--- a/ccid_host/ccid_main_functionfs.c</span><br><span>+++ b/ccid_host/ccid_main_functionfs.c</span><br><span>@@ -7,6 +7,7 @@</span><br><span> #include <linux/usb/functionfs.h></span><br><span> </span><br><span> #include "ccid_proto.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "logging.h"</span><br><span> </span><br><span> #if __BYTE_ORDER == __LITTLE_ENDIAN</span><br><span> #define cpu_to_le16(x)  (x)</span><br><span>@@ -504,27 +505,6 @@</span><br><span>   .send_int = ccid_ops_send_int,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-static const struct log_info_cat log_info_cat[] = {</span><br><span style="color: hsl(0, 100%, 40%);">-     [DUSB] = {</span><br><span style="color: hsl(0, 100%, 40%);">-              .name = "USB",</span><br><span style="color: hsl(0, 100%, 40%);">-                .description = "USB Transport",</span><br><span style="color: hsl(0, 100%, 40%);">-               .enabled = 1,</span><br><span style="color: hsl(0, 100%, 40%);">-           .loglevel = LOGL_NOTICE,</span><br><span style="color: hsl(0, 100%, 40%);">-        },</span><br><span style="color: hsl(0, 100%, 40%);">-      [DCCID] = {</span><br><span style="color: hsl(0, 100%, 40%);">-             .name = "CCID",</span><br><span style="color: hsl(0, 100%, 40%);">-               .description = "CCID Core",</span><br><span style="color: hsl(0, 100%, 40%);">-           .color = "\033[1;35m",</span><br><span style="color: hsl(0, 100%, 40%);">-                .enabled = 1,</span><br><span style="color: hsl(0, 100%, 40%);">-           .loglevel = LOGL_DEBUG,</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%);">-static const struct log_info log_info = {</span><br><span style="color: hsl(0, 100%, 40%);">-   .cat = log_info_cat,</span><br><span style="color: hsl(0, 100%, 40%);">-    .num_cat = ARRAY_SIZE(log_info_cat),</span><br><span style="color: hsl(0, 100%, 40%);">-};</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> static void *tall_main_ctx;</span><br><span> </span><br><span> static void signal_handler(int signal)</span><br><span>diff --git a/ccid_host/logging.c b/ccid_host/logging.c</span><br><span>new file mode 100644</span><br><span>index 0000000..4e14815</span><br><span>--- /dev/null</span><br><span>+++ b/ccid_host/logging.c</span><br><span>@@ -0,0 +1,47 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/utils.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/logging.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include "logging.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static const struct log_info_cat log_info_cat[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+    [DUSB] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "USB",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "USB Transport",</span><br><span style="color: hsl(120, 100%, 40%);">+             .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_NOTICE,</span><br><span style="color: hsl(120, 100%, 40%);">+      },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DCCID] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "CCID",</span><br><span style="color: hsl(120, 100%, 40%);">+             .description = "USB-CCID Protocol",</span><br><span style="color: hsl(120, 100%, 40%);">+         .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DISO7816] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "ISO7816",</span><br><span style="color: hsl(120, 100%, 40%);">+          .description = "ISO7816-3 State machines",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DATR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "ATR",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "ATR (Answer To Reset) FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+         .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DTPDU] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "TPDU",</span><br><span style="color: hsl(120, 100%, 40%);">+             .description = "TPDU FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DPPS] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "PPS",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "PPS (Protocol and Parameter Selection) FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+                .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</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%);">+const struct log_info log_info = {</span><br><span style="color: hsl(120, 100%, 40%);">+  .cat = log_info_cat,</span><br><span style="color: hsl(120, 100%, 40%);">+  .num_cat = ARRAY_SIZE(log_info_cat),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span>diff --git a/ccid_host/logging.h b/ccid_host/logging.h</span><br><span>new file mode 100644</span><br><span>index 0000000..0a2d36a</span><br><span>--- /dev/null</span><br><span>+++ b/ccid_host/logging.h</span><br><span>@@ -0,0 +1,13 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/logging.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum {</span><br><span style="color: hsl(120, 100%, 40%);">+     DCCID,</span><br><span style="color: hsl(120, 100%, 40%);">+        DUSB,</span><br><span style="color: hsl(120, 100%, 40%);">+ DISO7816,</span><br><span style="color: hsl(120, 100%, 40%);">+     DATR,</span><br><span style="color: hsl(120, 100%, 40%);">+ DTPDU,</span><br><span style="color: hsl(120, 100%, 40%);">+        DPPS,</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%);">+extern const struct log_info log_info;</span><br><span>diff --git a/sysmoOCTSIM/libosmo_emb.c b/sysmoOCTSIM/libosmo_emb.c</span><br><span>index b100560..1914568 100644</span><br><span>--- a/sysmoOCTSIM/libosmo_emb.c</span><br><span>+++ b/sysmoOCTSIM/libosmo_emb.c</span><br><span>@@ -46,8 +46,37 @@</span><br><span>    },</span><br><span>   [DCCID] = {</span><br><span>          .name = "CCID",</span><br><span style="color: hsl(0, 100%, 40%);">-               .description = "CCID Core",</span><br><span style="color: hsl(0, 100%, 40%);">-           .color = "\033[1;35m",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "USB-CCID Protocol",</span><br><span style="color: hsl(120, 100%, 40%);">+         .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DISO7816] = {</span><br><span style="color: hsl(120, 100%, 40%);">+                .name = "ISO7816",</span><br><span style="color: hsl(120, 100%, 40%);">+          .description = "ISO7816-3 State machines",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DATR] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "ATR",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "ATR (Answer To Reset) FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+         .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DTPDU] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "TPDU",</span><br><span style="color: hsl(120, 100%, 40%);">+             .description = "TPDU FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+          .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DPPS] = {</span><br><span style="color: hsl(120, 100%, 40%);">+            .name = "PPS",</span><br><span style="color: hsl(120, 100%, 40%);">+              .description = "PPS (Protocol and Parameter Selection) FSM",</span><br><span style="color: hsl(120, 100%, 40%);">+                .enabled = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+         .loglevel = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+       },</span><br><span style="color: hsl(120, 100%, 40%);">+    [DCARD] = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .name = "CARD",</span><br><span style="color: hsl(120, 100%, 40%);">+             .description = "Card FSM",</span><br><span>                 .enabled = 1,</span><br><span>                .loglevel = LOGL_DEBUG,</span><br><span>      },</span><br><span>diff --git a/sysmoOCTSIM/logging.h b/sysmoOCTSIM/logging.h</span><br><span>index 900f88e..896b166 100644</span><br><span>--- a/sysmoOCTSIM/logging.h</span><br><span>+++ b/sysmoOCTSIM/logging.h</span><br><span>@@ -3,6 +3,11 @@</span><br><span> #include <osmocom/core/logging.h></span><br><span> </span><br><span> enum {</span><br><span style="color: hsl(120, 100%, 40%);">+       DCCID,</span><br><span>       DUSB,</span><br><span style="color: hsl(0, 100%, 40%);">-   DCCID</span><br><span style="color: hsl(120, 100%, 40%);">+ DISO7816,</span><br><span style="color: hsl(120, 100%, 40%);">+     DATR,</span><br><span style="color: hsl(120, 100%, 40%);">+ DTPDU,</span><br><span style="color: hsl(120, 100%, 40%);">+        DPPS,</span><br><span style="color: hsl(120, 100%, 40%);">+ DCARD,</span><br><span> };</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-ccid-firmware/+/15690">change 15690</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/c/osmo-ccid-firmware/+/15690"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-ccid-firmware </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I0145b77e6165d36d33f18ef3a452f2c37913bd73 </div>
<div style="display:none"> Gerrit-Change-Number: 15690 </div>
<div style="display:none"> Gerrit-PatchSet: 5 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>