client: major restructuring around new main_fsm

The remsim_client code already used FSMs for the connections
to both remsim-server and remsim-bankd.  However the 'main' part of the
program was not yet implemented as a FSM, making it somewhat difficult
to perform the right actions in every possible situation.

This commit re-structures the code around a central main_fsm, which
gets notified from the per-connection FSMs and which handles the common
processing.  It also handles the execution of external script commands,
and hence further unifies the code base between the different backends
(simtrace2, ifd_handler, shell)

Closes: #4414

Change-Id: I44a430bc5674dea00ed72a0b28729ac8bcb4e022
diff --git a/src/client/client.h b/src/client/client.h
index 0761255..b828882 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -10,9 +10,49 @@
 #include "slotmap.h"
 #include "debug.h"
 
+/***********************************************************************
+ * frontend interface
+ ***********************************************************************/
+
+struct bankd_client;
+
+struct frontend_phys_status {
+	struct {
+		/* all members can be 0 (inactive), 1 (active) or -1 (not supported/known) */
+		int reset_active;
+		int vcc_present;
+		int clk_active;
+		int card_present;
+	} flags;
+	uint16_t voltage_mv;
+	uint8_t fi;
+	uint8_t di;
+	uint8_t wi;
+	uint8_t waiting_time;
+};
+
+struct frontend_pts {
+	const uint8_t *buf;
+	size_t len;
+};
+
+struct frontend_tpdu {
+	const uint8_t *buf;
+	size_t len;
+};
+
+/* API from generic core to frontend (modem/cardem) */
+int frontend_request_card_insert(struct bankd_client *bc);
+int frontend_request_sim_remote(struct bankd_client *bc);
+int frontend_request_modem_reset(struct bankd_client *bc);
+int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, size_t len);
+int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, size_t len);
+int frontend_handle_slot_status(struct bankd_client *bc, const SlotPhysStatus_t *sts);
+int frontend_append_script_env(struct bankd_client *bc, char **env, size_t max_env);
+
 /* main.c */
 
-struct cardem_inst;
+struct osmo_st2_cardem_inst;
 
 #define ATR_SIZE_MAX            55
 struct client_config {
@@ -48,6 +88,8 @@
 	struct rspro_server_conn srv_conn;
 	/* connection to the remsim-bankd (data) */
 	struct rspro_server_conn bankd_conn;
+	/* CLIENT_MAIN fsm */
+	struct osmo_fsm_inst *main_fi;
 
 	/* remote component ID */
 	struct app_comp_id peer_comp_id;
@@ -55,17 +97,43 @@
 	struct bank_slot bankd_slot;
 
 	struct client_config *cfg;
-	struct cardem_inst *cardem;
+	struct osmo_st2_cardem_inst *cardem;
+	struct frontend_phys_status last_status;
 	void *data;
 };
 
 #define srvc2bankd_client(srvc)		container_of(srvc, struct bankd_client, srv_conn)
 #define bankdc2bankd_client(bdc)	container_of(bdc, struct bankd_client, bankd_conn)
 
-struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software);
+struct client_config *client_config_init(void *ctx);
+struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software,
+					  struct client_config *cfg);
 void remsim_client_set_clslot(struct bankd_client *bc, int client_id, int slot_nr);
 
-
-extern int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu);
-
 extern int client_user_main(struct bankd_client *g_client);
+
+
+/***********************************************************************
+ * main FSM
+ ***********************************************************************/
+
+enum main_fsm_event {
+	MF_E_SRVC_CONNECTED,	/* connection to server established (TCP + RSPRO level) */
+	MF_E_SRVC_LOST,		/* connection to server was lost */
+	MF_E_SRVC_CONFIG_BANK,	/* server instructs us to connect to bankd/slot */
+	MF_E_SRVC_RESET_REQ,	/* RsproPDUchoice_PR_ResetStateReq */
+
+	MF_E_BANKD_CONNECTED,	/* connection to bankd established (TCP + RSPRO level) */
+	MF_E_BANKD_LOST,	/* connection to bankd was lost */
+	MF_E_BANKD_TPDU,	/* RsproPDUchoice_PR_tpduCardToModem */
+	MF_E_BANKD_ATR,		/* RsproPDUchoice_PR_setAtrReq */
+	MF_E_BANKD_SLOT_STATUS,	/* bankSlotStatusInd */
+
+	MF_E_MDM_STATUS_IND,	/* status from modem/cardem */
+	MF_E_MDM_PTS_IND,	/* PTS indication from modem/cardem */
+	MF_E_MDM_TPDU,		/* TPDU from modem/cardem */
+};
+struct osmo_fsm_inst *main_fsm_alloc(void *ctx, struct bankd_client *bc);
+
+
+