Harald Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 1 | /* (C) 2018-2020 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 2 | * |
| 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 Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 22 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 23 | /* 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 Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <string.h> |
Harald Welte | 228af8a | 2019-03-08 22:20:21 +0100 | [diff] [blame] | 29 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 30 | #include <talloc.h> |
| 31 | |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 32 | #include <osmocom/core/fsm.h> |
| 33 | #include <osmocom/core/utils.h> |
| 34 | #include <osmocom/core/logging.h> |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 35 | |
| 36 | #include "rspro_util.h" |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 37 | #include "client.h" |
Harald Welte | 61d98e9 | 2019-03-03 15:43:07 +0100 | [diff] [blame] | 38 | #include "debug.h" |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 39 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 40 | struct 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 Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 68 | static int bankd_handle_rx(struct rspro_server_conn *bankdc, const RsproPDU_t *pdu) |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 69 | { |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 70 | struct bankd_client *bc = bankdc2bankd_client(bankdc); |
| 71 | |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 72 | switch (pdu->msg.present) { |
| 73 | case RsproPDUchoice_PR_connectClientRes: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 74 | /* Store 'identity' of bankd to in peer_comp_id */ |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 75 | 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 Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 77 | break; |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 78 | case RsproPDUchoice_PR_tpduCardToModem: |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 79 | return osmo_fsm_inst_dispatch(bc->main_fi, MF_E_BANKD_TPDU, (void *) pdu); |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 80 | case RsproPDUchoice_PR_setAtrReq: |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 81 | 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 Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 84 | default: |
Harald Welte | d5a8729 | 2020-02-13 16:44:56 +0100 | [diff] [blame] | 85 | LOGPFSML(bankdc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU %s\n", |
| 86 | rspro_msgt_name(pdu)); |
Harald Welte | 24173fb | 2018-08-24 20:37:28 +0200 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | return 0; |
Harald Welte | 2ff0ab9 | 2018-08-17 22:10:49 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 93 | /* handle incoming messages from server */ |
| 94 | static int srvc_handle_rx(struct rspro_server_conn *srvc, const RsproPDU_t *pdu) |
| 95 | { |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 96 | struct bankd_client *bc = srvc2bankd_client(srvc); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 97 | 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 Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 105 | case RsproPDUchoice_PR_configClientIdReq: |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 106 | /* store/set the clientID as instructed by the server */ |
Harald Welte | 8e46ab6 | 2020-02-14 12:55:43 +0100 | [diff] [blame] | 107 | 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 Welte | d571a3e | 2019-03-11 22:09:50 +0100 | [diff] [blame] | 113 | /* 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 Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 118 | osmo_fsm_inst_dispatch(bc->main_fi, MF_E_SRVC_CONFIG_BANK, (void *) pdu); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 119 | break; |
| 120 | default: |
Harald Welte | 8d8d4f1 | 2019-03-27 22:50:39 +0100 | [diff] [blame] | 121 | LOGPFSML(srvc->fi, LOGL_ERROR, "Unknown/Unsupported RSPRO PDU type: %s\n", |
| 122 | rspro_msgt_name(pdu)); |
Harald Welte | e56f2b9 | 2019-03-02 17:02:13 +0100 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 129 | struct bankd_client *remsim_client_create(void *ctx, const char *name, const char *software, |
| 130 | struct client_config *cfg) |
Harald Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 131 | { |
| 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 Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 139 | 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 Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 149 | /* create and [attempt to] establish connection to remsim-server */ |
| 150 | srvc = &bc->srv_conn; |
Harald Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 151 | srvc->server_host = cfg->server_host; |
| 152 | srvc->server_port = cfg->server_port; |
Harald Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 153 | 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 Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 164 | 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 Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 167 | |
| 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 Welte | 0e968cc | 2020-02-22 18:16:16 +0100 | [diff] [blame] | 178 | 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 Welte | 879ee41 | 2020-02-14 14:21:33 +0100 | [diff] [blame] | 181 | |
| 182 | return bc; |
| 183 | } |
| 184 | |
| 185 | void 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 | } |