laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-remsim/+/17157 )


Change subject: client: Work without global g_client variable
......................................................................

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
---
M src/client/client.h
M src/client/remsim_client.c
M src/client/user_shell.c
3 files changed, 28 insertions(+), 21 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/57/17157/1

diff --git a/src/client/client.h b/src/client/client.h
index b3001ee..0239b55 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -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 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);

diff --git a/src/client/remsim_client.c b/src/client/remsim_client.c
index dbe983b..496e1f4 100644
--- a/src/client/remsim_client.c
+++ b/src/client/remsim_client.c
@@ -62,7 +62,7 @@
        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 @@
 /* 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 @@
                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);
diff --git a/src/client/user_shell.c b/src/client/user_shell.c
index db36aab..d5cad7b 100644
--- a/src/client/user_shell.c
+++ b/src/client/user_shell.c
@@ -48,12 +48,13 @@

 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 @@
 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 @@

        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 @@
                        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 @@


 /* 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 @@
        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);

--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/17157
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I456fb633561b88912be2f78c3e0264794d921255
Gerrit-Change-Number: 17157
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <[email protected]>
Gerrit-MessageType: newchange

Reply via email to