client: Work without global g_client variable

We may want to develop programs that include multiple instances of
a remsim_client.  Let's remove the global variable 'g_client' and
instead de-reference the bankd_client using container_of() macros

Change-Id: I456fb633561b88912be2f78c3e0264794d921255
This commit is contained in:
Harald Welte 2020-02-14 12:55:43 +01:00
parent 1200c820f0
commit 8e46ab6755
3 changed files with 28 additions and 21 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/fsm.h>
#include <osmocom/abis/ipa.h>
#include <osmocom/rspro/RsproPDU.h>
@ -57,8 +58,9 @@ struct bankd_client {
struct cardem_inst *cardem;
};
#define srvc2bankd_client(srvc) container_of(srvc, struct bankd_client, srv_conn)
#define bankdc2bankd_client(bdc) container_of(bdc, struct bankd_client, bankd_conn)
extern struct bankd_client *g_client;
extern int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu);

View File

@ -62,7 +62,7 @@ static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *p
return 0;
}
struct bankd_client *g_client;
static struct bankd_client *g_client;
static void *g_tall_ctx;
void __thread *talloc_asn1_ctx;
int asn_debug;
@ -70,6 +70,7 @@ int asn_debug;
/* handle incoming messages from server */
static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
{
struct bankd_client *bc = srvc2bankd_client(srvc);
RsproPDU_t *resp;
switch (pdu->msg.present) {
@ -80,24 +81,24 @@ static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
break;
case RsproPDUchoice_PR_configClientIdReq:
/* store/set the clientID as instructed by the server */
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;
if (!srvc->clslot)
srvc->clslot = talloc_zero(srvc, ClientSlot_t);
*srvc->clslot = pdu->msg.choice.configClientIdReq.clientSlot;
if (!bc->bankd_conn.clslot)
bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
*bc->bankd_conn.clslot = *bc->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_conn.server_host,
osmo_talloc_replace_string(bc, &bc->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_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
rspro2bank_slot(&bc->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
bc->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
/* instruct bankd FSM to connect */
osmo_fsm_inst_dispatch(g_client->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
/* send response to server */
resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
server_conn_send_rspro(srvc, resp);

View File

@ -48,12 +48,13 @@ static int bankd_handle_setAtrReq(struct bankd_client *bc, const RsproPDU_t *pdu
int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
{
struct bankd_client *client = bankdc2bankd_client(bankdc);
switch (pdu->msg.present) {
case RsproPDUchoice_PR_tpduCardToModem:
bankd_handle_tpduCardToModem(g_client, pdu);
bankd_handle_tpduCardToModem(client, pdu);
break;
case RsproPDUchoice_PR_setAtrReq:
bankd_handle_setAtrReq(g_client, pdu);
bankd_handle_setAtrReq(client, pdu);
break;
default:
OSMO_ASSERT(0);
@ -68,17 +69,19 @@ int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU
struct stdin_state {
struct osmo_fd ofd;
struct msgb *rx_msg;
struct bankd_client *bc;
};
/* called every time a command on stdin was received */
static void handle_stdin_command(struct stdin_state *ss, char *cmd)
{
struct bankd_client *bc = ss->bc;
RsproPDU_t *pdu;
BankSlot_t bslot;
uint8_t buf[1024];
int rc;
bank_slot2rspro(&bslot, &g_client->bankd_slot);
bank_slot2rspro(&bslot, &bc->bankd_slot);
OSMO_ASSERT(ss->rx_msg);
@ -86,9 +89,9 @@ static void handle_stdin_command(struct stdin_state *ss, char *cmd)
if (!strcasecmp(cmd, "RESET")) {
/* reset the [remote] card */
pdu = rspro_gen_ClientSlotStatusInd(g_client->srv_conn.clslot, &bslot,
pdu = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
true, false, false, true);
server_conn_send_rspro(&g_client->bankd_conn, pdu);
server_conn_send_rspro(&bc->bankd_conn, pdu);
} else {
/* we assume the user has entered a C-APDU as hex string. parse + send */
rc = osmo_hexparse(cmd, buf, sizeof(buf));
@ -96,14 +99,14 @@ static void handle_stdin_command(struct stdin_state *ss, char *cmd)
fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd);
return;
}
if (!g_client->srv_conn.clslot) {
if (!bc->srv_conn.clslot) {
fprintf(stderr, "Cannot send command; no client slot\n");
return;
}
/* Send CMD APDU to [remote] card */
pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, buf, rc);
server_conn_send_rspro(&g_client->bankd_conn, pdu);
pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc);
server_conn_send_rspro(&bc->bankd_conn, pdu);
}
}
@ -144,7 +147,7 @@ static int stdin_fd_cb(struct osmo_fd *ofd, unsigned int what)
/* main function */
int client_user_main(struct bankd_client *g_client)
int client_user_main(struct bankd_client *bc)
{
struct stdin_state ss;
@ -152,6 +155,7 @@ int client_user_main(struct bankd_client *g_client)
memset(&ss, 0, sizeof(ss));
osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
osmo_fd_register(&ss.ofd);
ss.bc = bc;
while (1) {
osmo_select_main(0);