blob: 182894ffb1b26beb35718c8d6b484528626b8cc8 [file] [log] [blame]
Harald Welte879ee412020-02-14 14:21:33 +01001/* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org>
Harald Welte3dcdd202019-03-09 13:06:46 +01002 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
Harald Welte2ff0ab92018-08-17 22:10:49 +020022
Harald Welte0e968cc2020-02-22 18:16:16 +010023/* This file contains code shared among all remsim client applications,
24 * including the ifd-handler, which is not an executable program with a main()
25 * function or command line parsing, but a shared library */
26
Harald Welte2ff0ab92018-08-17 22:10:49 +020027#include <errno.h>
28#include <string.h>
Harald Welte228af8a2019-03-08 22:20:21 +010029
Harald Welte2ff0ab92018-08-17 22:10:49 +020030#include <talloc.h>
31
Harald Welte2ff0ab92018-08-17 22:10:49 +020032#include <osmocom/core/fsm.h>
33#include <osmocom/core/utils.h>
34#include <osmocom/core/logging.h>
Harald Welte2ff0ab92018-08-17 22:10:49 +020035
36#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020037#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010038#include "debug.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020039
Harald Welte0e968cc2020-02-22 18:16:16 +010040struct client_config *client_config_init(void *ctx)
41{
42 struct client_config *cfg = talloc_zero(ctx, struct client_config);
43 if (!cfg)
44 return NULL;
45
46 cfg->server_host = talloc_strdup(cfg, "127.0.0.1");
47 cfg->server_port = 9998;
48 cfg->client_id = -1;
49 cfg->client_slot = -1;
50 cfg->gsmtap_host = talloc_strdup(cfg, "127.0.0.1");
51 cfg->keep_running = false;
52
53 cfg->usb.vendor_id = -1;
54 cfg->usb.product_id = -1;
55 cfg->usb.config_id = -1;
56 cfg->usb.if_num = -1;
57 cfg->usb.altsetting = 0;
58 cfg->usb.addr = -1;
59 cfg->usb.path = NULL;
60
61 cfg->atr.data[0] = 0x3B;
62 cfg->atr.data[1] = 0x00; // the shortest simplest ATR possible
63 cfg->atr.len = 2;
64
65 return cfg;
66};
67
Harald Welted5a87292020-02-13 16:44:56 +010068static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Harald Welte2ff0ab92018-08-17 22:10:49 +020069{
Harald Welte0e968cc2020-02-22 18:16:16 +010070 struct bankd_client *bc = bankdc2bankd_client(bankdc);
71
Harald Welte24173fb2018-08-24 20:37:28 +020072 switch (pdu->msg.present) {
73 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010074 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welted5a87292020-02-13 16:44:56 +010075 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
76 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020077 break;
Harald Welted5a87292020-02-13 16:44:56 +010078 case RsproPDUchoice_PR_tpduCardToModem:
Harald Welte0e968cc2020-02-22 18:16:16 +010079 return osmo_fsm_inst_dispatch(bc->main_fi, MF_E_BANKD_TPDU, (void *) pdu);
Harald Welted5a87292020-02-13 16:44:56 +010080 case RsproPDUchoice_PR_setAtrReq:
Harald Welte0e968cc2020-02-22 18:16:16 +010081 return osmo_fsm_inst_dispatch(bc->main_fi, MF_E_BANKD_ATR, (void *) pdu);
82 case RsproPDUchoice_PR_bankSlotStatusInd:
83 return osmo_fsm_inst_dispatch(bc->main_fi, MF_E_BANKD_SLOT_STATUS, (void *) pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020084 default:
Harald Welted5a87292020-02-13 16:44:56 +010085 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU %s\n",
86 rspro_msgt_name(pdu));
Harald Welte24173fb2018-08-24 20:37:28 +020087 return -1;
88 }
89
90 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020091}
92
Harald Weltee56f2b92019-03-02 17:02:13 +010093/* handle incoming messages from server */
94static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
95{
Harald Welte8e46ab62020-02-14 12:55:43 +010096 struct bankd_client *bc = srvc2bankd_client(srvc);
Harald Weltee56f2b92019-03-02 17:02:13 +010097 RsproPDU_t *resp;
98
99 switch (pdu->msg.present) {
100 case RsproPDUchoice_PR_connectClientRes:
101 /* Store 'identity' of server in srvc->peer_comp_id */
102 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
103 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
104 break;
Harald Welted571a3e2019-03-11 22:09:50 +0100105 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +0100106 /* store/set the clientID as instructed by the server */
Harald Welte8e46ab62020-02-14 12:55:43 +0100107 if (!srvc->clslot)
108 srvc->clslot = talloc_zero(srvc, ClientSlot_t);
109 *srvc->clslot = pdu->msg.choice.configClientIdReq.clientSlot;
110 if (!bc->bankd_conn.clslot)
111 bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
112 *bc->bankd_conn.clslot = *bc->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +0100113 /* send response to server */
114 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
115 server_conn_send_rspro(srvc, resp);
116 break;
117 case RsproPDUchoice_PR_configClientBankReq:
Harald Welte0e968cc2020-02-22 18:16:16 +0100118 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_SRVC_CONFIG_BANK, (void *) pdu);
Harald Weltee56f2b92019-03-02 17:02:13 +0100119 break;
120 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +0100121 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
122 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +0100123 return -1;
124 }
125
126 return 0;
127}
128
Harald Welte0e968cc2020-02-22 18:16:16 +0100129struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software,
130 struct client_config *cfg)
Harald Welte879ee412020-02-14 14:21:33 +0100131{
132 struct bankd_client *bc = talloc_zero(ctx, struct bankd_client);
133 struct rspro_server_conn *srvc, *bankdc;
134 int rc;
135
136 if (!bc)
137 return NULL;
138
Harald Welte0e968cc2020-02-22 18:16:16 +0100139 bc->cfg = cfg;
140
141 bc->main_fi = main_fsm_alloc(bc, bc);
142 if (!bc->main_fi) {
143 fprintf(stderr, "Unable to create main client FSM: %s\n", strerror(errno));
144 exit(1);
145 }
146
147 remsim_client_set_clslot(bc, cfg->client_id, cfg->client_slot);
148
Harald Welte879ee412020-02-14 14:21:33 +0100149 /* create and [attempt to] establish connection to remsim-server */
150 srvc = &bc->srv_conn;
Harald Welte0e968cc2020-02-22 18:16:16 +0100151 srvc->server_host = cfg->server_host;
152 srvc->server_port = cfg->server_port;
Harald Welte879ee412020-02-14 14:21:33 +0100153 srvc->handle_rx = srvc_handle_rx;
154 srvc->own_comp_id.type = ComponentType_remsimClient;
155 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, name);
156 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, software);
157 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
158
159 rc = server_conn_fsm_alloc(bc, srvc);
160 if (rc < 0) {
161 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
162 exit(1);
163 }
Harald Welte0e968cc2020-02-22 18:16:16 +0100164 osmo_fsm_inst_change_parent(srvc->fi, bc->main_fi, MF_E_SRVC_LOST);
165 srvc->parent_conn_evt = MF_E_SRVC_CONNECTED;
166 srvc->parent_disc_evt = MF_E_SRVC_LOST;
Harald Welte879ee412020-02-14 14:21:33 +0100167
168 bankdc = &bc->bankd_conn;
169 /* server_host / server_port are configured from remsim-server */
170 bankdc->handle_rx = bankd_handle_rx;
171 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
172 rc = server_conn_fsm_alloc(bc, bankdc);
173 if (rc < 0) {
174 fprintf(stderr, "Unable to connect bankd conn FSM: %s\n", strerror(errno));
175 exit(1);
176 }
177 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
Harald Welte0e968cc2020-02-22 18:16:16 +0100178 osmo_fsm_inst_change_parent(bankdc->fi, bc->main_fi, MF_E_BANKD_LOST);
179 bankdc->parent_conn_evt = MF_E_BANKD_CONNECTED;
180 bankdc->parent_disc_evt = MF_E_BANKD_LOST;
Harald Welte879ee412020-02-14 14:21:33 +0100181
182 return bc;
183}
184
185void remsim_client_set_clslot(struct bankd_client *bc, int client_id, int slot_nr)
186{
187 if (!bc->srv_conn.clslot) {
188 bc->srv_conn.clslot = talloc_zero(bc, ClientSlot_t);
189 OSMO_ASSERT(bc->srv_conn.clslot);
190 }
191
192 if (!bc->bankd_conn.clslot) {
193 bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
194 OSMO_ASSERT(bc->bankd_conn.clslot);
195 }
196
197 if (client_id >= 0) {
198 bc->srv_conn.clslot->clientId = client_id;
199 bc->bankd_conn.clslot->clientId = client_id;
200 }
201
202 if (slot_nr >= 0) {
203 bc->srv_conn.clslot->slotNr = slot_nr;
204 bc->bankd_conn.clslot->slotNr = slot_nr;
205 }
206}