blob: dd78c8e74671e847cf33faac84340a6dcc062e81 [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
23#include <errno.h>
24#include <string.h>
Harald Welte228af8a2019-03-08 22:20:21 +010025
Harald Welte2ff0ab92018-08-17 22:10:49 +020026#include <talloc.h>
27
Harald Welte2ff0ab92018-08-17 22:10:49 +020028#include <osmocom/core/fsm.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/core/logging.h>
Harald Welte2ff0ab92018-08-17 22:10:49 +020031
32#include "rspro_util.h"
Harald Welte24173fb2018-08-24 20:37:28 +020033#include "client.h"
Harald Welte61d98e92019-03-03 15:43:07 +010034#include "debug.h"
Harald Welte2ff0ab92018-08-17 22:10:49 +020035
Harald Welted5a87292020-02-13 16:44:56 +010036static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu)
Harald Welte2ff0ab92018-08-17 22:10:49 +020037{
Harald Welte24173fb2018-08-24 20:37:28 +020038 switch (pdu->msg.present) {
39 case RsproPDUchoice_PR_connectClientRes:
Harald Weltee56f2b92019-03-02 17:02:13 +010040 /* Store 'identity' of bankd to in peer_comp_id */
Harald Welted5a87292020-02-13 16:44:56 +010041 rspro_comp_id_retrieve(&bankdc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
42 osmo_fsm_inst_dispatch(bankdc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020043 break;
Harald Welted5a87292020-02-13 16:44:56 +010044 case RsproPDUchoice_PR_tpduCardToModem:
45 case RsproPDUchoice_PR_setAtrReq:
Harald Welte1200c822020-02-13 20:43:27 +010046 return client_user_bankd_handle_rx(bankdc, pdu);
Harald Welte24173fb2018-08-24 20:37:28 +020047 default:
Harald Welted5a87292020-02-13 16:44:56 +010048 LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU %s\n",
49 rspro_msgt_name(pdu));
Harald Welte24173fb2018-08-24 20:37:28 +020050 return -1;
51 }
52
53 return 0;
Harald Welte2ff0ab92018-08-17 22:10:49 +020054}
55
Harald Weltee56f2b92019-03-02 17:02:13 +010056/* handle incoming messages from server */
57static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu)
58{
Harald Welte8e46ab62020-02-14 12:55:43 +010059 struct bankd_client *bc = srvc2bankd_client(srvc);
Harald Weltee56f2b92019-03-02 17:02:13 +010060 RsproPDU_t *resp;
61
62 switch (pdu->msg.present) {
63 case RsproPDUchoice_PR_connectClientRes:
64 /* Store 'identity' of server in srvc->peer_comp_id */
65 rspro_comp_id_retrieve(&srvc->peer_comp_id, &pdu->msg.choice.connectClientRes.identity);
66 osmo_fsm_inst_dispatch(srvc->fi, SRVC_E_CLIENT_CONN_RES, (void *) pdu);
67 break;
Harald Welted571a3e2019-03-11 22:09:50 +010068 case RsproPDUchoice_PR_configClientIdReq:
Harald Weltee56f2b92019-03-02 17:02:13 +010069 /* store/set the clientID as instructed by the server */
Harald Welte8e46ab62020-02-14 12:55:43 +010070 if (!srvc->clslot)
71 srvc->clslot = talloc_zero(srvc, ClientSlot_t);
72 *srvc->clslot = pdu->msg.choice.configClientIdReq.clientSlot;
73 if (!bc->bankd_conn.clslot)
74 bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
75 *bc->bankd_conn.clslot = *bc->srv_conn.clslot;
Harald Welted571a3e2019-03-11 22:09:50 +010076 /* send response to server */
77 resp = rspro_gen_ConfigClientIdRes(ResultCode_ok);
78 server_conn_send_rspro(srvc, resp);
79 break;
80 case RsproPDUchoice_PR_configClientBankReq:
Harald Weltee56f2b92019-03-02 17:02:13 +010081 /* store/set the bankd ip/port as instructed by the server */
Harald Welte8e46ab62020-02-14 12:55:43 +010082 osmo_talloc_replace_string(bc, &bc->bankd_conn.server_host,
Harald Welted571a3e2019-03-11 22:09:50 +010083 rspro_IpAddr2str(&pdu->msg.choice.configClientBankReq.bankd.ip));
Harald Welte8e46ab62020-02-14 12:55:43 +010084 rspro2bank_slot(&bc->bankd_slot, &pdu->msg.choice.configClientBankReq.bankSlot);
85 bc->bankd_conn.server_port = pdu->msg.choice.configClientBankReq.bankd.port;
Harald Welte1b6696f2020-02-16 15:27:15 +010086 /* bankd port 0 is a magic value to indicate "no bankd" */
87 if (bc->bankd_conn.server_port == 0)
88 osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_DISCONNECT, NULL);
89 else
90 osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
Harald Weltee56f2b92019-03-02 17:02:13 +010091 /* send response to server */
Harald Welted571a3e2019-03-11 22:09:50 +010092 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
Harald Weltea844bb02019-03-09 13:38:50 +010093 server_conn_send_rspro(srvc, resp);
Harald Weltee56f2b92019-03-02 17:02:13 +010094 break;
95 default:
Harald Welte8d8d4f12019-03-27 22:50:39 +010096 LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n",
97 rspro_msgt_name(pdu));
Harald Weltee56f2b92019-03-02 17:02:13 +010098 return -1;
99 }
100
101 return 0;
102}
103
Harald Welte879ee412020-02-14 14:21:33 +0100104struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software)
105{
106 struct bankd_client *bc = talloc_zero(ctx, struct bankd_client);
107 struct rspro_server_conn *srvc, *bankdc;
108 int rc;
109
110 if (!bc)
111 return NULL;
112
113 /* create and [attempt to] establish connection to remsim-server */
114 srvc = &bc->srv_conn;
115 srvc->server_host = "localhost";
116 srvc->server_port = 9998;
117 srvc->handle_rx = srvc_handle_rx;
118 srvc->own_comp_id.type = ComponentType_remsimClient;
119 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.name, name);
120 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.software, software);
121 OSMO_STRLCPY_ARRAY(srvc->own_comp_id.sw_version, PACKAGE_VERSION);
122
123 rc = server_conn_fsm_alloc(bc, srvc);
124 if (rc < 0) {
125 fprintf(stderr, "Unable to create Server conn FSM: %s\n", strerror(errno));
126 exit(1);
127 }
128
129 bankdc = &bc->bankd_conn;
130 /* server_host / server_port are configured from remsim-server */
131 bankdc->handle_rx = bankd_handle_rx;
132 memcpy(&bankdc->own_comp_id, &srvc->own_comp_id, sizeof(bankdc->own_comp_id));
133 rc = server_conn_fsm_alloc(bc, bankdc);
134 if (rc < 0) {
135 fprintf(stderr, "Unable to connect bankd conn FSM: %s\n", strerror(errno));
136 exit(1);
137 }
138 osmo_fsm_inst_update_id(bankdc->fi, "bankd");
139
140 return bc;
141}
142
143void remsim_client_set_clslot(struct bankd_client *bc, int client_id, int slot_nr)
144{
145 if (!bc->srv_conn.clslot) {
146 bc->srv_conn.clslot = talloc_zero(bc, ClientSlot_t);
147 OSMO_ASSERT(bc->srv_conn.clslot);
148 }
149
150 if (!bc->bankd_conn.clslot) {
151 bc->bankd_conn.clslot = talloc_zero(bc, ClientSlot_t);
152 OSMO_ASSERT(bc->bankd_conn.clslot);
153 }
154
155 if (client_id >= 0) {
156 bc->srv_conn.clslot->clientId = client_id;
157 bc->bankd_conn.clslot->clientId = client_id;
158 }
159
160 if (slot_nr >= 0) {
161 bc->srv_conn.clslot->slotNr = slot_nr;
162 bc->bankd_conn.clslot->slotNr = slot_nr;
163 }
164}