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:
parent
1200c820f0
commit
8e46ab6755
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <osmocom/core/linuxlist.h>
|
||||||
#include <osmocom/core/fsm.h>
|
#include <osmocom/core/fsm.h>
|
||||||
#include <osmocom/abis/ipa.h>
|
#include <osmocom/abis/ipa.h>
|
||||||
#include <osmocom/rspro/RsproPDU.h>
|
#include <osmocom/rspro/RsproPDU.h>
|
||||||
|
@ -57,8 +58,9 @@ struct bankd_client {
|
||||||
struct cardem_inst *cardem;
|
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);
|
extern int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu);
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *p
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct bankd_client *g_client;
|
static struct bankd_client *g_client;
|
||||||
static void *g_tall_ctx;
|
static void *g_tall_ctx;
|
||||||
void __thread *talloc_asn1_ctx;
|
void __thread *talloc_asn1_ctx;
|
||||||
int asn_debug;
|
int asn_debug;
|
||||||
|
@ -70,6 +70,7 @@ int asn_debug;
|
||||||
/* handle incoming messages from server */
|
/* handle incoming messages from server */
|
||||||
static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
|
static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
|
||||||
{
|
{
|
||||||
|
struct bankd_client *bc = srvc2bankd_client(srvc);
|
||||||
RsproPDU_t *resp;
|
RsproPDU_t *resp;
|
||||||
|
|
||||||
switch (pdu->msg.present) {
|
switch (pdu->msg.present) {
|
||||||
|
@ -80,24 +81,24 @@ static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
|
||||||
break;
|
break;
|
||||||
case RsproPDUchoice_PR_configClientIdReq:
|
case RsproPDUchoice_PR_configClientIdReq:
|
||||||
/* store/set the clientID as instructed by the server */
|
/* store/set the clientID as instructed by the server */
|
||||||
if (!g_client->srv_conn.clslot)
|
if (!srvc->clslot)
|
||||||
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
|
srvc->clslot = talloc_zero(srvc, ClientSlot_t);
|
||||||
*g_client->srv_conn.clslot = pdu->msg.choice.configClientIdReq.clientSlot;
|
*srvc->clslot = pdu->msg.choice.configClientIdReq.clientSlot;
|
||||||
if (!g_client->bankd_conn.clslot)
|
if (!bc->bankd_conn.clslot)
|
||||||
g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
|
bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
|
||||||
*g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
|
*bc->bankd_conn.clslot = *bc->srv_conn.clslot;
|
||||||
/* send response to server */
|
/* send response to server */
|
||||||
resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
|
resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
|
||||||
server_conn_send_rspro(srvc, resp);
|
server_conn_send_rspro(srvc, resp);
|
||||||
break;
|
break;
|
||||||
case RsproPDUchoice_PR_configClientBankReq:
|
case RsproPDUchoice_PR_configClientBankReq:
|
||||||
/* store/set the bankd ip/port as instructed by the server */
|
/* 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));
|
rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
|
||||||
rspro2bank_slot(&g_client->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
|
rspro2bank_slot(&bc->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
|
||||||
g_client->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
|
bc->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
|
||||||
/* instruct bankd FSM to connect */
|
/* 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 */
|
/* send response to server */
|
||||||
resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
|
resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
|
||||||
server_conn_send_rspro(srvc, resp);
|
server_conn_send_rspro(srvc, resp);
|
||||||
|
|
|
@ -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)
|
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) {
|
switch (pdu->msg.present) {
|
||||||
case RsproPDUchoice_PR_tpduCardToModem:
|
case RsproPDUchoice_PR_tpduCardToModem:
|
||||||
bankd_handle_tpduCardToModem(g_client, pdu);
|
bankd_handle_tpduCardToModem(client, pdu);
|
||||||
break;
|
break;
|
||||||
case RsproPDUchoice_PR_setAtrReq:
|
case RsproPDUchoice_PR_setAtrReq:
|
||||||
bankd_handle_setAtrReq(g_client, pdu);
|
bankd_handle_setAtrReq(client, pdu);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
OSMO_ASSERT(0);
|
OSMO_ASSERT(0);
|
||||||
|
@ -68,17 +69,19 @@ int client_user_bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU
|
||||||
struct stdin_state {
|
struct stdin_state {
|
||||||
struct osmo_fd ofd;
|
struct osmo_fd ofd;
|
||||||
struct msgb *rx_msg;
|
struct msgb *rx_msg;
|
||||||
|
struct bankd_client *bc;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* called every time a command on stdin was received */
|
/* called every time a command on stdin was received */
|
||||||
static void handle_stdin_command(struct stdin_state *ss, char *cmd)
|
static void handle_stdin_command(struct stdin_state *ss, char *cmd)
|
||||||
{
|
{
|
||||||
|
struct bankd_client *bc = ss->bc;
|
||||||
RsproPDU_t *pdu;
|
RsproPDU_t *pdu;
|
||||||
BankSlot_t bslot;
|
BankSlot_t bslot;
|
||||||
uint8_t buf[1024];
|
uint8_t buf[1024];
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
bank_slot2rspro(&bslot, &g_client->bankd_slot);
|
bank_slot2rspro(&bslot, &bc->bankd_slot);
|
||||||
|
|
||||||
OSMO_ASSERT(ss->rx_msg);
|
OSMO_ASSERT(ss->rx_msg);
|
||||||
|
|
||||||
|
@ -86,9 +89,9 @@ static void handle_stdin_command(struct stdin_state *ss, char *cmd)
|
||||||
|
|
||||||
if (!strcasecmp(cmd, "RESET")) {
|
if (!strcasecmp(cmd, "RESET")) {
|
||||||
/* reset the [remote] card */
|
/* 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);
|
true, false, false, true);
|
||||||
server_conn_send_rspro(&g_client->bankd_conn, pdu);
|
server_conn_send_rspro(&bc->bankd_conn, pdu);
|
||||||
} else {
|
} else {
|
||||||
/* we assume the user has entered a C-APDU as hex string. parse + send */
|
/* we assume the user has entered a C-APDU as hex string. parse + send */
|
||||||
rc = osmo_hexparse(cmd, buf, sizeof(buf));
|
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);
|
fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!g_client->srv_conn.clslot) {
|
if (!bc->srv_conn.clslot) {
|
||||||
fprintf(stderr, "Cannot send command; no client slot\n");
|
fprintf(stderr, "Cannot send command; no client slot\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send CMD APDU to [remote] card */
|
/* Send CMD APDU to [remote] card */
|
||||||
pdu = rspro_gen_TpduModem2Card(g_client->srv_conn.clslot, &bslot, buf, rc);
|
pdu = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, buf, rc);
|
||||||
server_conn_send_rspro(&g_client->bankd_conn, pdu);
|
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 */
|
/* main function */
|
||||||
int client_user_main(struct bankd_client *g_client)
|
int client_user_main(struct bankd_client *bc)
|
||||||
{
|
{
|
||||||
struct stdin_state ss;
|
struct stdin_state ss;
|
||||||
|
|
||||||
|
@ -152,6 +155,7 @@ int client_user_main(struct bankd_client *g_client)
|
||||||
memset(&ss, 0, sizeof(ss));
|
memset(&ss, 0, sizeof(ss));
|
||||||
osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
|
osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
|
||||||
osmo_fd_register(&ss.ofd);
|
osmo_fd_register(&ss.ofd);
|
||||||
|
ss.bc = bc;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
osmo_select_main(0);
|
osmo_select_main(0);
|
||||||
|
|
Loading…
Reference in New Issue