remsim_client: Move body of main() loop to separate function

I rally don't like the existing spaghetti-style code.

Change-Id: I85c166e8aa95bb3f2e5d60d14f60caa94f3116fb
This commit is contained in:
Harald Welte 2019-12-16 16:51:34 +01:00
parent f918748208
commit a86e6b73d8
1 changed files with 115 additions and 109 deletions

View File

@ -992,97 +992,12 @@ static void handle_options(struct client_config *cfg, int argc, char **argv)
}
int main(int argc, char **argv)
static void main_body(struct client_config *cfg)
{
struct rspro_server_conn *srvc, *bankdc;
struct st_transport *transp = ci->slot->transp;
struct client_config *cfg;
int rc;
int ret = 1;
print_welcome();
talloc_enable_null_tracking();
g_tall_ctx = talloc_named_const(NULL, 0, "global");
talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
msgb_talloc_ctx_init(g_tall_ctx, 0);
osmo_init_logging2(g_tall_ctx, &log_info);
cfg = client_config_init(g_tall_ctx);
handle_options(cfg, argc, argv);
if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
fprintf(stderr, "You have to specify the vendor and product ID\n");
goto do_exit;
}
signal(SIGUSR1, handle_sig_usr1);
rc = osmo_libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto do_exit;
}
g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
if (!g_gti) {
perror("unable to open GSMTAP");
goto close_exit;
}
gsmtap_source_add_sink(g_gti);
signal(SIGINT, &signal_handler);
// initialize remote SIM client
g_client = talloc_zero(g_tall_ctx, struct bankd_client);
if (cfg->client_id != -1) {
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
g_client->srv_conn.clslot->clientId = cfg->client_id;
/* default to client slot 0 */
if (cfg->client_slot == -1)
g_client->srv_conn.clslot->slotNr = 0;
else
g_client->srv_conn.clslot->slotNr = cfg->client_slot;
g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
*g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
}
/* create and [attempt to] establish connection to remsim-server */
srvc = &g_client->srv_conn;
srvc->server_host = cfg->server_host;
srvc->server_port = cfg->server_port;
srvc->handle_rx = srvc_handle_rx;
srvc->own_comp_id.type = ComponentType_remsimClient;
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
rc = server_conn_fsm_alloc(g_client, srvc);
if (rc < 0) {
fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
exit(1);
}
osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
/* create and not yet establish connection to remsim-bankd */
srvc = &g_client->srv_conn;
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);
}
osmo_fsm_inst_update_id(bankdc->fi, "bankd");
asn_debug = 0;
// connect to SIMtrace2 cardem
do {
struct usb_interface_match _ifm, *ifm = &_ifm;
int rc;
ifm->vendor = cfg->usb.vendor_id;
ifm->product = cfg->usb.product_id;
ifm->configuration = cfg->usb.config_id;
@ -1094,7 +1009,7 @@ int main(int argc, char **argv)
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
if (!transp->usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
return;
}
rc = libusb_claim_interface(transp->usb_devh, cfg->usb.if_num);
@ -1181,17 +1096,108 @@ int main(int argc, char **argv)
osmo_select_main(false);
}
ret = 0;
libusb_release_interface(transp->usb_devh, 0);
close_exit:
if (transp->usb_devh)
libusb_close(transp-> usb_devh);
if (cfg->keep_running)
}
int main(int argc, char **argv)
{
struct rspro_server_conn *srvc, *bankdc;
struct client_config *cfg;
int rc;
int ret = 1;
print_welcome();
talloc_enable_null_tracking();
g_tall_ctx = talloc_named_const(NULL, 0, "global");
talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
msgb_talloc_ctx_init(g_tall_ctx, 0);
osmo_init_logging2(g_tall_ctx, &log_info);
cfg = client_config_init(g_tall_ctx);
handle_options(cfg, argc, argv);
if (cfg->usb.vendor_id < 0 || cfg->usb.product_id < 0) {
fprintf(stderr, "You have to specify the vendor and product ID\n");
goto do_exit;
}
signal(SIGUSR1, handle_sig_usr1);
rc = osmo_libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto do_exit;
}
g_gti = gsmtap_source_init(cfg->gsmtap_host, GSMTAP_UDP_PORT, 0);
if (!g_gti) {
perror("unable to open GSMTAP");
goto close_exit;
}
gsmtap_source_add_sink(g_gti);
signal(SIGINT, &signal_handler);
// initialize remote SIM client
g_client = talloc_zero(g_tall_ctx, struct bankd_client);
if (cfg->client_id != -1) {
g_client->srv_conn.clslot = talloc_zero(g_client, ClientSlot_t);
g_client->srv_conn.clslot->clientId = cfg->client_id;
/* default to client slot 0 */
if (cfg->client_slot == -1)
g_client->srv_conn.clslot->slotNr = 0;
else
g_client->srv_conn.clslot->slotNr = cfg->client_slot;
g_client->bankd_conn.clslot = talloc_zero(g_client, ClientSlot_t);
*g_client->bankd_conn.clslot = *g_client->srv_conn.clslot;
}
/* create and [attempt to] establish connection to remsim-server */
srvc = &g_client->srv_conn;
srvc->server_host = cfg->server_host;
srvc->server_port = cfg->server_port;
srvc->handle_rx = srvc_handle_rx;
srvc->own_comp_id.type = ComponentType_remsimClient;
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, "simtrace2-remsim-client");
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, "remsim-client");
OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
rc = server_conn_fsm_alloc(g_client, srvc);
if (rc < 0) {
fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
exit(1);
}
osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_ESTABLISH, NULL);
/* create and not yet establish connection to remsim-bankd */
srvc = &g_client->srv_conn;
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);
}
osmo_fsm_inst_update_id(bankdc->fi, "bankd");
asn_debug = 0;
// connect to SIMtrace2 cardem
do {
main_body(cfg);
sleep(1);
} while (cfg->keep_running);
libusb_exit(NULL);
close_exit:
osmo_libusb_exit(NULL);
do_exit:
return ret;
}