blob: 0674f6c422e416f357d6fec9f40c7331b4f70b0f [file] [log] [blame]
Harald Welte0e968cc2020-02-22 18:16:16 +01001/* (C) 2020 by Harald Welte <laforge@gnumonks.org>
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 */
22
23#include <stdint.h>
24#include <string.h>
25#include <errno.h>
26
27#include <osmocom/core/talloc.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/core/fsm.h>
31#include <osmocom/core/exec.h>
32
33#include "rspro_util.h"
34#include "client.h"
35#include "debug.h"
36
37#define S(x) (1 << (x))
38
39/***********************************************************************/
40
41/* build the (additional) environment for executing a script */
42static char **build_script_env(struct bankd_client *bc, const char *cause)
43{
44 char **env = talloc_zero_size(bc, 256*sizeof(char *));
45 int rc, i = 0;
46
47 if (!env)
48 return NULL;
49
50 env[i++] = talloc_asprintf(env, "REMSIM_CLIENT_VERSION=%s", VERSION);
51
52 env[i++] = talloc_asprintf(env, "REMSIM_SERVER_ADDR=%s:%u",
53 bc->srv_conn.server_host, bc->srv_conn.server_port);
54 env[i++] = talloc_asprintf(env, "REMSIM_SERVER_STATE=%s",
55 osmo_fsm_inst_state_name(bc->srv_conn.fi));
56
57 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_ADDR=%s:%u",
58 bc->bankd_conn.server_host, bc->bankd_conn.server_port);
59 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_STATE=%s",
60 osmo_fsm_inst_state_name(bc->bankd_conn.fi));
61
62
63 if (bc->srv_conn.clslot) {
64 env[i++] = talloc_asprintf(env, "REMSIM_CLIENT_SLOT=%lu:%lu",
65 bc->srv_conn.clslot->clientId,
66 bc->srv_conn.clslot->slotNr);
67 }
68 env[i++] = talloc_asprintf(env, "REMSIM_BANKD_SLOT=%u:%u",
69 bc->bankd_slot.bank_id, bc->bankd_slot.slot_nr);
70
71 env[i++] = talloc_asprintf(env, "REMSIM_SIM_VCC=%u", bc->last_status.flags.vcc_present);
72 env[i++] = talloc_asprintf(env, "REMSIM_SIM_RST=%u", bc->last_status.flags.reset_active);
Harald Welte9629ba32020-03-03 22:51:55 +010073 env[i++] = talloc_asprintf(env, "REMSIM_SIM_CLK=%u", bc->last_status.flags.clk_active);
Harald Welte0e968cc2020-02-22 18:16:16 +010074
75 env[i++] = talloc_asprintf(env, "REMSIM_CAUSE=%s", cause);
76
77 /* ask frontend to append any frontend-speccific additional environment vars */
78 rc = frontend_append_script_env(bc, env+i, 256-i);
79 if (rc > 0)
80 i += rc;
81
82 /* terminate last entry */
83 env[i++] = NULL;
84 return env;
85}
86
87static int call_script(struct bankd_client *bc, const char *cause)
88{
89 char **env, *cmd;
90 int rc;
91
92 if (!bc->cfg->event_script)
93 return 0;
94
95 env = build_script_env(bc, cause);
96 if (!env)
97 return -ENOMEM;
98
99 cmd = talloc_asprintf(env, "%s %s", bc->cfg->event_script, cause);
100 if (!cmd) {
101 talloc_free(env);
102 return -ENOMEM;
103 }
104
105 rc = osmo_system_nowait(cmd, osmo_environment_whitelist, env);
106 talloc_free(env);
107
108 return rc;
109}
110
111
112/***********************************************************************/
113
114
115enum main_fsm_state {
116 MF_ST_INIT,
117 MF_ST_UNCONFIGURED, /* waiting for configuration from server */
118 MF_ST_WAIT_BANKD, /* configured; waiting for bankd conn */
119 MF_ST_OPERATIONAL, /* fully operational (configured + bankd conn live */
120};
121
122static const struct value_string main_fsm_event_names[] = {
123 OSMO_VALUE_STRING(MF_E_SRVC_CONNECTED),
124 OSMO_VALUE_STRING(MF_E_SRVC_LOST),
125 OSMO_VALUE_STRING(MF_E_SRVC_CONFIG_BANK),
126 OSMO_VALUE_STRING(MF_E_SRVC_RESET_REQ),
127 OSMO_VALUE_STRING(MF_E_BANKD_CONNECTED),
128 OSMO_VALUE_STRING(MF_E_BANKD_LOST),
129 OSMO_VALUE_STRING(MF_E_BANKD_TPDU),
130 OSMO_VALUE_STRING(MF_E_BANKD_ATR),
131 OSMO_VALUE_STRING(MF_E_BANKD_SLOT_STATUS),
132 OSMO_VALUE_STRING(MF_E_MDM_STATUS_IND),
133 OSMO_VALUE_STRING(MF_E_MDM_PTS_IND),
134 OSMO_VALUE_STRING(MF_E_MDM_TPDU),
135 { 0, NULL }
136};
137
138static void main_st_operational(struct osmo_fsm_inst *fi, uint32_t event, void *data);
139
140static void main_st_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
141{
142 struct bankd_client *bc = (struct bankd_client *) fi->priv;
143
144 switch (event) {
145 case MF_E_SRVC_CONNECTED:
146 osmo_fsm_inst_state_chg(fi, MF_ST_UNCONFIGURED, 0, 0);
147 call_script(bc, "event-server-connect");
148 break;
149 default:
150 OSMO_ASSERT(0);
151 }
152}
153
154static void main_st_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
155{
156 struct bankd_client *bc = (struct bankd_client *) fi->priv;
157 /* we might be called from a 'higher' state such as operational; clean up */
158 osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_DISCONNECT, NULL);
159}
160
161static void main_st_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data)
162{
163 switch (event) {
164 case MF_E_SRVC_CONFIG_BANK:
165 /* same treatment as below */
166 main_st_operational(fi, event, data);
167 break;
168 default:
169 OSMO_ASSERT(0);
170 }
171}
172
173static void main_st_wait_bankd(struct osmo_fsm_inst *fi, uint32_t event, void *data)
174{
175 struct bankd_client *bc = (struct bankd_client *) fi->priv;
176
177 switch (event) {
178 case MF_E_SRVC_CONFIG_BANK:
179 /* same treatment as below */
180 main_st_operational(fi, event, data);
181 break;
182 case MF_E_BANKD_CONNECTED:
183 osmo_fsm_inst_state_chg(fi, MF_ST_OPERATIONAL, 0, 0);
184 call_script(bc, "event-bankd-connect");
185 break;
186 default:
187 OSMO_ASSERT(0);
188 }
189}
190
191static void main_st_operational_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
192{
193 struct bankd_client *bc = (struct bankd_client *) fi->priv;
194
195 /* Simulate card-insert to modem */
196 frontend_request_card_insert(bc);
197 call_script(bc, "request-card-insert");
198
199 /* Select remote (forwarded) SIM */
200 frontend_request_sim_remote(bc);
201 call_script(bc, "request-sim-remote");
202
203 /* Set the ATR */
204 frontend_handle_set_atr(bc, bc->cfg->atr.data, bc->cfg->atr.len);
205
206 /* Reset the modem */
207 frontend_request_modem_reset(bc);
208 call_script(bc, "request-modem-reset");
209}
210
211static void main_st_operational(struct osmo_fsm_inst *fi, uint32_t event, void *data)
212{
213 struct bankd_client *bc = (struct bankd_client *) fi->priv;
214 struct frontend_phys_status *pstatus = NULL;
215 struct frontend_pts *pts = NULL;
216 struct frontend_tpdu *tpdu = NULL;
217 RsproPDU_t *pdu_rx = NULL;
218 RsproPDU_t *resp;
219 BankSlot_t bslot;
220
221 switch (event) {
222 case MF_E_BANKD_LOST:
223 osmo_fsm_inst_state_chg(fi, MF_ST_WAIT_BANKD, 0, 0);
224 break;
225 case MF_E_SRVC_CONFIG_BANK:
226 pdu_rx = data;
227 OSMO_ASSERT(pdu_rx);
228 OSMO_ASSERT(pdu_rx->msg.present == RsproPDUchoice_PR_configClientBankReq);
229 /* store/set the bankd ip/port as instructed by the server */
230 osmo_talloc_replace_string(bc, &bc->bankd_conn.server_host,
231 rspro_IpAddr2str(&pdu_rx->msg.choice.configClientBankReq.bankd.ip));
232 bc->bankd_conn.server_port = pdu_rx->msg.choice.configClientBankReq.bankd.port;
233 rspro2bank_slot(&bc->bankd_slot, &pdu_rx->msg.choice.configClientBankReq.bankSlot);
234 /* bankd port 0 is a magic value to indicate "no bankd" */
235 if (bc->bankd_conn.server_port == 0)
236 osmo_fsm_inst_state_chg(fi, MF_ST_UNCONFIGURED, 0, 0);
237 else {
238 osmo_fsm_inst_state_chg(fi, MF_ST_WAIT_BANKD, 0, 0);
239 /* TODO: do we need to disconnect before? */
240 osmo_fsm_inst_dispatch(bc->bankd_conn.fi, SRVC_E_ESTABLISH, NULL);
241 }
242 /* send response to server */
243 resp = rspro_gen_ConfigClientBankRes(ResultCode_ok);
244 server_conn_send_rspro(&bc->srv_conn, resp);
245 call_script(bc, "event-config-bankd");
246 break;
247 case MF_E_BANKD_TPDU:
248 pdu_rx = data;
249 OSMO_ASSERT(pdu_rx);
250 OSMO_ASSERT(pdu_rx->msg.present == RsproPDUchoice_PR_tpduCardToModem);
251 /* forward to modem/cardem (via API) */
252 frontend_handle_card2modem(bc, pdu_rx->msg.choice.tpduCardToModem.data.buf,
253 pdu_rx->msg.choice.tpduCardToModem.data.size);
254 /* response happens indirectly via tpduModemToCard */
255 break;
256 case MF_E_BANKD_ATR:
257 pdu_rx = data;
258 OSMO_ASSERT(pdu_rx);
259 OSMO_ASSERT(pdu_rx->msg.present == RsproPDUchoice_PR_setAtrReq);
260 /* forward to modem/cardem (via API) */
261 frontend_handle_set_atr(bc, pdu_rx->msg.choice.setAtrReq.atr.buf,
262 pdu_rx->msg.choice.setAtrReq.atr.size);
263 /* send response to bankd */
264 resp = rspro_gen_SetAtrRes(ResultCode_ok);
265 server_conn_send_rspro(&bc->bankd_conn, resp);
266 break;
267 case MF_E_BANKD_SLOT_STATUS:
268 pdu_rx = data;
269 OSMO_ASSERT(pdu_rx);
270 OSMO_ASSERT(pdu_rx->msg.present == RsproPDUchoice_PR_bankSlotStatusInd);
271 /* forward to modem/cardem (via API) */
272 frontend_handle_slot_status(bc, &pdu_rx->msg.choice.bankSlotStatusInd.slotPhysStatus);
273 break;
274 case MF_E_MDM_STATUS_IND:
275 pstatus = data;
276 OSMO_ASSERT(pstatus);
277 /* forward to bankd */
278 bank_slot2rspro(&bslot, &bc->bankd_slot);
279 resp = rspro_gen_ClientSlotStatusInd(bc->srv_conn.clslot, &bslot,
280 pstatus->flags.reset_active,
281 pstatus->flags.vcc_present,
282 pstatus->flags.clk_active,
283 pstatus->flags.card_present);
284 server_conn_send_rspro(&bc->bankd_conn, resp);
285 if (!memcmp(&bc->last_status.flags, &pstatus->flags, sizeof(pstatus->flags)))
286 call_script(bc, "event-modem-status");
287 bc->last_status = *pstatus;
288 break;
289 case MF_E_MDM_PTS_IND:
290 pts = data;
291 OSMO_ASSERT(pts);
292 /* forward to bankd? */
293 break;
294 case MF_E_MDM_TPDU:
295 tpdu = data;
296 OSMO_ASSERT(tpdu);
297 /* forward to bankd */
298 bank_slot2rspro(&bslot, &bc->bankd_slot);
299 resp = rspro_gen_TpduModem2Card(bc->srv_conn.clslot, &bslot, tpdu->buf, tpdu->len);
300 server_conn_send_rspro(&bc->bankd_conn, resp);
301 break;
302 default:
303 OSMO_ASSERT(0);
304 }
305}
306
307static void main_allstate_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
308{
309 switch (event) {
310 case MF_E_SRVC_LOST:
311 /* should we do anything? The SRVC fsm will take care of reconnect, and we
312 * can continue to talk to the bankd without any trouble... */
313 break;
314 case MF_E_SRVC_RESET_REQ:
315 osmo_fsm_inst_state_chg(fi, MF_ST_UNCONFIGURED, 0, 0);
316 break;
317 default:
318 OSMO_ASSERT(0);
319 }
320}
321
322
323static const struct osmo_fsm_state main_fsm_states[] = {
324 [MF_ST_INIT] = {
325 .name = "INIT",
326 .in_event_mask = S(MF_E_SRVC_CONNECTED),
327 .out_state_mask = S(MF_ST_UNCONFIGURED),
328 .action = main_st_init,
329 },
330 [MF_ST_UNCONFIGURED] = {
331 .name = "UNCONFIGURED",
332 .in_event_mask = S(MF_E_SRVC_CONFIG_BANK),
333 .out_state_mask = S(MF_ST_INIT) | S(MF_ST_WAIT_BANKD),
334 .action = main_st_unconfigured,
335 .onenter = main_st_unconfigured_onenter,
336 },
337 [MF_ST_WAIT_BANKD] = {
338 .name = "WAIT_BANKD",
339 .in_event_mask = S(MF_E_SRVC_CONFIG_BANK) | S(MF_E_BANKD_CONNECTED),
340 .out_state_mask = S(MF_ST_INIT) | S(MF_ST_UNCONFIGURED) | S(MF_ST_OPERATIONAL),
341 .action = main_st_wait_bankd,
342 },
343 [MF_ST_OPERATIONAL] = {
344 .name = "OPERATIONAL",
345 .in_event_mask = S(MF_E_SRVC_CONFIG_BANK) |
346 S(MF_E_BANKD_LOST) |
347 S(MF_E_BANKD_TPDU) |
348 S(MF_E_BANKD_ATR) |
349 S(MF_E_BANKD_SLOT_STATUS) |
350 S(MF_E_MDM_STATUS_IND) |
351 S(MF_E_MDM_PTS_IND) |
352 S(MF_E_MDM_TPDU),
353 .out_state_mask = S(MF_ST_INIT) | S(MF_ST_UNCONFIGURED) | S(MF_ST_WAIT_BANKD),
354 .action = main_st_operational,
355 .onenter = main_st_operational_onenter,
356 },
357};
358
359static struct osmo_fsm client_main_fsm = {
360 .name = "CLIENT_MAIN",
361 .states = main_fsm_states,
362 .num_states = ARRAY_SIZE(main_fsm_states),
363 .allstate_event_mask = S(MF_E_SRVC_LOST) | S(MF_E_SRVC_RESET_REQ),
364 .allstate_action = main_allstate_action,
365 .log_subsys = DMAIN,
366 .event_names = main_fsm_event_names,
367};
368
369struct osmo_fsm_inst *main_fsm_alloc(void *ctx, struct bankd_client *bc)
370{
371 return osmo_fsm_inst_alloc(&client_main_fsm, ctx, bc, LOGL_DEBUG, "main");
372}
373
374static __attribute((constructor)) void on_dso_load_main_fsm(void)
375{
376 OSMO_ASSERT(osmo_fsm_register(&client_main_fsm) == 0);
377}