Replace bankd_client_fsm with rspro_client_fsm

There was a lot of similarity between the bankd_client_fsm (for the
client->bankd RSPRO connection) and the rspro_client_fsm (for the
client->server and the bankd->server RSPRO connections).

With the last few commits introducing the missing features to
rspro_client_fsm, we can completely obsolete bankd_client_fsm and
further simplify the codebase.

Change-Id: Icbe9881a0391fcd0c47e5d930dc764fc0cb1dfbf
changes/87/16587/2
Harald Welte 3 years ago
parent a3b14d1890
commit 3e9860bb77
  1. 4
      src/Makefile.am
  2. 278
      src/bankd_client_fsm.c
  3. 24
      src/client.h
  4. 85
      src/simtrace2-remsim_client.c

@ -28,12 +28,12 @@ noinst_HEADERS = debug.h client.h rspro_util.h slotmap.h rspro_client_fsm.h \
bin_PROGRAMS = osmo-remsim-client-st2
remsim_client_SOURCES = remsim_client.c rspro_client_fsm.c bankd_client_fsm.c debug.c
remsim_client_SOURCES = remsim_client.c rspro_client_fsm.c debug.c
remsim_client_LDADD = $(OSMOCORE_LIBS) $(OSMOGSM_LIBS) $(OSMOABIS_LIBS) \
libosmo-rspro.la
osmo_remsim_client_st2_SOURCES = simtrace2-remsim_client.c \
bankd_client_fsm.c rspro_client_fsm.c debug.c \
rspro_client_fsm.c debug.c \
simtrace2/apdu_dispatch.c \
simtrace2/simtrace2-discovery.c \
simtrace2/libusb_util.c

@ -1,278 +0,0 @@
/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
*
* All Rights Reserved
*
* SPDX-License-Identifier: GPL-2.0+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <talloc.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/fsm.h>
#include <osmocom/abis/ipa.h>
#include <osmocom/gsm/protocol/ipaccess.h>
#include "client.h"
#include "rspro_util.h"
#define S(x) (1 << (x))
static void bankd_updown_cb(struct ipa_client_conn *conn, int up)
{
struct bankd_client *bc = conn->data;
printf("RSPRO link to %s:%d %s\n", conn->addr, conn->port, up ? "UP" : "DOWN");
osmo_fsm_inst_dispatch(bc->bankd_fi, up ? BDC_E_TCP_UP: BDC_E_TCP_DOWN, 0);
}
/* internal function, bypassing FSM state */
static int _bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
{
return ipa_client_conn_send_rspro(bc->bankd_conn, rspro);
}
int bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *rspro)
{
if (osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_RSPRO_TX, rspro) < 0) {
ASN_STRUCT_FREE(asn_DEF_RsproPDU, rspro);
return -1;
}
return 0;
}
/***********************************************************************
* client-side FSM for RSPRO connection to remsim-bankd
*
* This is part of remsim-client and manages the connection to remsim-bankd,
* over which actual TPDU exchanges happen.
***********************************************************************/
enum bankd_conn_fsm_state {
/* waiting for initial connection to remsim-bankd */
BDC_ST_INIT,
/* bankd connection established, waiting for ClientConnectRes */
BDC_ST_ESTABLISHED,
/* bankd connection established, ClientConnect succeeded */
BDC_ST_CONNECTED,
/* connection lost, we're waiting for a re-establish */
BDC_ST_REESTABLISH,
};
static const struct value_string remsim_client_bankd_fsm_event_names[] = {
OSMO_VALUE_STRING(BDC_E_ESTABLISH),
OSMO_VALUE_STRING(BDC_E_TCP_UP),
OSMO_VALUE_STRING(BDC_E_TCP_DOWN),
OSMO_VALUE_STRING(BDC_E_CLIENT_CONN_RES),
OSMO_VALUE_STRING(BDC_E_RSPRO_TX),
{ 0, NULL }
};
#define T1_WAIT_CLIENT_CONN_RES 10
#define T2_RECONNECT 10
static void bdc_st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case BDC_E_ESTABLISH:
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
default:
OSMO_ASSERT(0);
}
}
static void bdc_st_established_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
struct bankd_client *bc = (struct bankd_client *) fi->priv;
RsproPDU_t *pdu;
/* Send ClientConnReq */
pdu = rspro_gen_ConnectClientReq(&bc->srv_conn.own_comp_id, bc->srv_conn.clslot);
_bankd_conn_send_rspro(bc, pdu);
}
static void bdc_st_established(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case BDC_E_TCP_DOWN:
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
case BDC_E_CLIENT_CONN_RES:
/* somehow notify the main code? */
osmo_fsm_inst_state_chg(fi, BDC_ST_CONNECTED, 0, 0);
break;
default:
OSMO_ASSERT(0);
}
}
static void bdc_st_connected(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct bankd_client *bc = (struct bankd_client *) fi->priv;
RsproPDU_t *pdu = NULL;
switch (event) {
case BDC_E_TCP_DOWN:
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
case BDC_E_RSPRO_TX:
pdu = data;
_bankd_conn_send_rspro(bc, pdu);
break;
default:
OSMO_ASSERT(0);
}
}
static void bdc_st_reestablish_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
struct bankd_client *bc = (struct bankd_client *) fi->priv;
int rc;
/* re-create bankd_conn */
if (bc->bankd_conn) {
LOGPFSML(fi, LOGL_INFO, "Destroying existing connection to bankd\n");
ipa_client_conn_close(bc->bankd_conn);
ipa_client_conn_destroy(bc->bankd_conn);
bc->bankd_conn = NULL;
}
LOGPFSML(fi, LOGL_INFO, "Creating TCP connection to bankd at %s:%u\n",
bc->bankd_host, bc->bankd_port);
bc->bankd_conn = ipa_client_conn_create(bc, NULL, 0, bc->bankd_host, bc->bankd_port,
bankd_updown_cb, bankd_read_cb, NULL, bc);
if (!bc->bankd_conn) {
fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
exit(1);
}
/* Attempt to connect TCP socket */
rc = ipa_client_conn_open(bc->bankd_conn);
if (rc < 0) {
fprintf(stderr, "Unable to connect RSPRO to %s:%d - %s\n",
bc->bankd_conn->addr, bc->bankd_conn->port, strerror(errno));
/* FIXME: retry? Timer? Abort? */
OSMO_ASSERT(0);
}
}
static void bdc_st_reestablish(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case BDC_E_TCP_UP:
osmo_fsm_inst_state_chg(fi, BDC_ST_ESTABLISHED, T1_WAIT_CLIENT_CONN_RES, 1);
break;
case BDC_E_TCP_DOWN:
/* wait for normal T2 timeout */
break;
default:
OSMO_ASSERT(0);
}
}
static void bdc_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
switch (event) {
case BDC_E_ESTABLISH:
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
default:
OSMO_ASSERT(0);
}
}
static int remsim_client_bankd_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
switch (fi->T) {
case 2:
/* TCP reconnect failed: retry */
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
case 1:
/* no ClientConnectRes received: disconnect + reconnect */
osmo_fsm_inst_state_chg(fi, BDC_ST_REESTABLISH, T2_RECONNECT, 2);
break;
default:
OSMO_ASSERT(0);
}
return 0;
}
static const struct osmo_fsm_state bankd_conn_fsm_states[] = {
[BDC_ST_INIT] = {
.name = "INIT",
.in_event_mask = 0, /* S(BDC_E_ESTABLISH) via allstate */
.out_state_mask = S(BDC_ST_REESTABLISH),
.action = bdc_st_init,
},
[BDC_ST_ESTABLISHED] = {
.name = "ESTABLISHED",
.in_event_mask = S(BDC_E_TCP_DOWN) | S(BDC_E_CLIENT_CONN_RES),
.out_state_mask = S(BDC_ST_CONNECTED) | S(BDC_ST_REESTABLISH),
.action = bdc_st_established,
.onenter = bdc_st_established_onenter,
},
[BDC_ST_CONNECTED] = {
.name = "CONNECTED",
.in_event_mask = S(BDC_E_TCP_DOWN) | S(BDC_E_RSPRO_TX),
.out_state_mask = S(BDC_ST_REESTABLISH),
.action = bdc_st_connected,
},
[BDC_ST_REESTABLISH] = {
.name = "REESTABLISH",
.in_event_mask = S(BDC_E_TCP_UP) | S(BDC_E_TCP_DOWN),
.out_state_mask = S(BDC_ST_ESTABLISHED) | S(BDC_ST_REESTABLISH),
.action = bdc_st_reestablish,
.onenter = bdc_st_reestablish_onenter,
},
};
struct osmo_fsm remsim_client_bankd_fsm = {
.name = "BANKD_CONN",
.states = bankd_conn_fsm_states,
.num_states = ARRAY_SIZE(bankd_conn_fsm_states),
.allstate_event_mask = S(BDC_E_ESTABLISH),
.allstate_action = bdc_allstate_action,
.timer_cb = remsim_client_bankd_fsm_timer_cb,
.log_subsys = DMAIN,
.event_names = remsim_client_bankd_fsm_event_names,
};
int bankd_conn_fsm_alloc(struct bankd_client *bc)
{
struct osmo_fsm_inst *fi;
fi = osmo_fsm_inst_alloc(&remsim_client_bankd_fsm, bc, bc, LOGL_DEBUG, "bankd");
if (!fi)
return -1;
bc->bankd_fi = fi;
return 0;
}
static __attribute__((constructor)) void on_dso_load(void)
{
OSMO_ASSERT(osmo_fsm_register(&remsim_client_bankd_fsm) == 0);
}

@ -9,36 +9,16 @@
#include "slotmap.h"
#include "debug.h"
/* fsm.c */
enum bankd_conn_fsm_event {
BDC_E_ESTABLISH, /* instruct BDC to (re)etablish TCP connection to bankd */
BDC_E_TCP_UP, /* notify BDC that TCP connection is up/connected */
BDC_E_TCP_DOWN, /* notify BDC that TCP connection is down/disconnected */
BDC_E_CLIENT_CONN_RES, /* notify BDC that ClientConnectRes has been received */
BDC_E_RSPRO_TX, /* transmit a RSPRO PDU to the bankd */
};
extern struct osmo_fsm remsim_client_bankd_fsm;
/* main.c */
struct bankd_client {
/* connection to the remsim-server (control) */
struct rspro_server_conn srv_conn;
/* connection to the remsim-bankd (data) */
struct rspro_server_conn bankd_conn;
/* remote component ID */
struct app_comp_id peer_comp_id;
/* connection to the remsim-bankd */
char *bankd_host;
uint16_t bankd_port;
struct bank_slot bankd_slot;
struct ipa_client_conn *bankd_conn;
struct osmo_fsm_inst *bankd_fi;
};
int bankd_conn_send_rspro(struct bankd_client *bc, RsproPDU_t *pdu);
int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg);
int bankd_conn_fsm_alloc(struct bankd_client *bc);

@ -430,7 +430,7 @@ static int process_do_rx_da(struct cardem_inst *ci, uint8_t *buf, int len)
BankSlot_t bslot;
bank_slot2rspro(&bslot, &g_client->bankd_slot);
RsproPDU_t *pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, apdu_command, sizeof(ac.hdr) + ac.lc.tot); // create RSPRO packet
bankd_conn_send_rspro(g_client, pdu);
server_conn_send_rspro(&g_client->bankd_conn, pdu);
// the response will come separately
} else if (ac.lc.tot > ac.lc.cur) { // there is pending data from the modem
cardem_request_pb_and_rx(ci, ac.hdr.ins, ac.lc.tot - ac.lc.cur); // send procedure byte to get remaining data
@ -532,7 +532,7 @@ static void signal_handler(int signal)
/** remsim_client **/
static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu)
static int bankd_handle_tpduCardToModem(struct bankd_client *bc, const RsproPDU_t *pdu)
{
OSMO_ASSERT(pdu);
OSMO_ASSERT(RsproPDUchoice_PR_tpduCardToModem == pdu->msg.present);
@ -554,7 +554,7 @@ static int bankd_handle_tpduCardToModem(struct bankd_client *bc, RsproPDU_t *pdu
return 0;
}
static int bankd_handle_setAtrReq(struct bankd_client *bc, RsproPDU_t *pdu)
static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu)
{
RsproPDU_t *resp;
int rc;
@ -571,70 +571,35 @@ static int bankd_handle_setAtrReq(struct bankd_client *bc, RsproPDU_t *pdu)
resp = rspro_gen_SetAtrRes(ResultCode_cardTransmissionError);
if (!resp)
return -ENOMEM;
bankd_conn_send_rspro(g_client, resp);
server_conn_send_rspro(&g_client->bankd_conn, resp);
return 0;
}
static int bankd_handle_msg(struct bankd_client *bc, struct msgb *msg)
/* handle incoming message from bankd */
static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
{
RsproPDU_t *pdu = rspro_dec_msg(msg);
if (!pdu) {
LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Error decoding PDU\n");
return -1;
}
switch (pdu->msg.present) {
case RsproPDUchoice_PR_connectClientRes:
/* Store 'identity' of bankd to in peer_comp_id */
rspro_comp_id_retrieve(&bc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
osmo_fsm_inst_dispatch(bc->bankd_fi, BDC_E_CLIENT_CONN_RES, pdu);
rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
break;
case RsproPDUchoice_PR_tpduCardToModem: // APDU response from card received
bankd_handle_tpduCardToModem(bc, pdu);
bankd_handle_tpduCardToModem(g_client, pdu);
break;
case RsproPDUchoice_PR_setAtrReq:
bankd_handle_setAtrReq(bc, pdu);
bankd_handle_setAtrReq(g_client, pdu);
break;
default:
LOGPFSML(bc->bankd_fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s: %s\n",
rspro_msgt_name(pdu), msgb_hexdump(msg));
LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsuppoerted RSPRO PDU %s\n",
rspro_msgt_name(pdu));
return -1;
}
return 0;
}
int bankd_read_cb(struct ipa_client_conn *conn, struct msgb *msg)
{
struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
struct bankd_client *bc = conn->data;
int rc;
if (msgb_length(msg) < sizeof(*hh))
goto invalid;
msg->l2h = &hh->data[0];
if (hh->proto != IPAC_PROTO_OSMO)
goto invalid;
if (!he || msgb_l2len(msg) < sizeof(*he))
goto invalid;
msg->l2h = &he->data[0];
if (he->proto != IPAC_PROTO_EXT_RSPRO)
goto invalid;
LOGPFSML(bc->bankd_fi, LOGL_DEBUG, "Received RSPRO %s\n", msgb_hexdump(msg));
rc = bankd_handle_msg(bc, msg);
msgb_free(msg);
return rc;
invalid:
msgb_free(msg);
return -1;
}
/* handle incoming messages from server */
static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
{
@ -651,18 +616,21 @@ static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
if (!g_client->srv_conn.clslot)
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
*g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
if (!g_client->bankd_conn.clslot)
g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
*g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
/* send response to server */
resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
server_conn_send_rspro(srvc, resp);
break;
case RsproPDUchoice_PR_configClientBankReq:
/* store/set the bankd ip/port as instructed by the server */
osmo_talloc_replace_string(g_client, &g_client->bankd_host,
osmo_talloc_replace_string(g_client, &g_client->bankd_conn.server_host,
rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
g_client->bankd_port = pdu->msg.choice.configClientBankReq.bankd.port;
g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
/* instruct bankd FSM to connect */
osmo_fsm_inst_dispatch(g_client->bankd_fi, BDC_E_ESTABLISH, NULL);
osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
/* send response to server */
resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
server_conn_send_rspro(srvc, resp);
@ -733,7 +701,7 @@ static const struct option opts[] = {
int main(int argc, char **argv)
{
struct rspro_server_conn *srvc;
struct rspro_server_conn *srvc, *bankdc;
struct st_transport *transp = ci->slot->transp;
char *gsmtap_host = "127.0.0.1";
int rc;
@ -853,6 +821,8 @@ int main(int argc, char **argv)
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
g_client->srv_conn.clslot->clientId = client_id;
g_client->srv_conn.clslot->slotNr = client_slot;
g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
*g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
}
srvc = &g_client->srv_conn;
@ -870,13 +840,18 @@ int main(int argc, char **argv)
}
osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
asn_debug = 0;
if (bankd_conn_fsm_alloc(g_client) < 0) {
fprintf(stderr, "Unable to connect: %s\n", strerror(errno));
bankdc = &g_client->bankd_conn;
/* server_host / server_port are configured from remsim-server */
bankdc->handle_rx = bankd_handle_rx;
memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
rc = server_conn_fsm_alloc(g_client, bankdc);
if (rc < 0) {
fprintf(stderr, "Unable to create bankd conn FSM: %s\n", strerror(errno));
exit(1);
}
asn_debug = 0;
// connect to SIMtrace2 cardem
do {
struct usb_interface_match _ifm, *ifm = &_ifm;

Loading…
Cancel
Save