summaryrefslogtreecommitdiffstats
path: root/src/host/layer23/src/common/sap_fsm.c
blob: 9591dabd2a05f74ff29ea95ace3e5418d95d1a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * SAP (SIM Access Profile) FSM definition
 * based on Bluetooth SAP specification
 *
 * (C) 2018-2019 by Vadim Yanitskiy <axilirator@gmail.com>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <osmocom/core/socket.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/fsm.h>

#include <osmocom/bb/common/osmocom_data.h>
#include <osmocom/bb/common/logging.h>

#include <osmocom/bb/common/sap_interface.h>
#include <osmocom/bb/common/sap_proto.h>
#include <osmocom/bb/common/sap_fsm.h>

/*! Send encoded SAP message to the Server.
 * \param[in] ms MS instance with active SAP connection
 * \param[in] msg encoded SAP message buffer
 * \returns 0 in case of success, negative in case of error
 */
static int sap_send_msgb(struct osmocom_ms *ms, struct msgb *msg)
{
	int rc;

	rc = osmo_wqueue_enqueue(&ms->sap_wq, msg);
	if (rc) {
		LOGP(DSAP, LOGL_ERROR, "Failed to enqueue SAP message\n");
		msgb_free(msg);
		return rc;
	}

	return 0;
}

static void sap_fsm_disconnect(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
}

static void sap_fsm_connect(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;
	struct msgb *msg;
	uint16_t size;

	/* Section 5.1.1, CONNECT_REQ */
	msg = sap_msgb_alloc(SAP_CONNECT_REQ);
	if (!msg)
		return;

	/* Section 4.1.1, start MaxMsgSize negotiation */
	size = htons(ms->sap_entity.max_msg_size);
	sap_msgb_add_param(msg, SAP_MAX_MSG_SIZE,
		sizeof(size), (uint8_t *) &size);

	sap_send_msgb(ms, msg);
}

static void sap_negotiate_msg_size(struct osmosap_entity *sap,
	const struct sap_message *sap_msg)
{
	uint16_t size, param_len;
	const char *cause = NULL;
	struct sap_param *param;

	param = sap_get_param(sap_msg, SAP_MAX_MSG_SIZE, &param_len);
	if (!param) {
		cause = "missing expected MaxMsgSize parameter";
		goto error;
	}
	if (param_len != sizeof(size)) {
		cause = "MaxMsgSize parameter has wrong length";
		goto error;
	}

	/* Parse MaxMsgSize suggested by server */
	size = ntohs((uint16_t *) param->value);
	if (size > SAP_MAX_MSG_SIZE) {
		cause = "suggested MaxMsgSize is too big for us";
		goto error;
	}

	/* Attempt to initiate connection again */
	sap->max_msg_size = size;
	sap_fsm_connect(sap->fi, sap->fi->state);
	return;

error:
	LOGP(DSAP, LOGL_ERROR, "MaxMsgSize negotiation failed: %s\n", cause);
	osmo_fsm_inst_state_chg(sap->fi, SAP_STATE_NOT_CONNECTED, 0, 0);
}

static void sap_fsm_conn_handler(struct osmo_fsm_inst *fi,
	uint32_t event, void *data)
{
	struct sap_message *sap_msg = (struct sap_message *) data;
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;
	struct sap_param *param;
	uint16_t param_len;
	uint8_t status;

	/* Section 5.1.2, CONNECT_RESP */
	param = sap_get_param(sap_msg, SAP_CONNECTION_STATUS, &param_len);
	if (!param || param_len != sizeof(status)) {
		LOGP(DSAP, LOGL_ERROR, "Missing mandatory connection status\n");
		osmo_fsm_inst_state_chg(fi, SAP_STATE_NOT_CONNECTED, 0, 0);
		return;
	}

	/* Parse connection status */
	status = param->value[0];

	LOGP(DSAP, LOGL_INFO, "SAP connection status (0x%02x): %s\n",
		status, get_value_string(sap_conn_status_names, status));

	switch ((enum sap_conn_status_type) status) {
	case SAP_CONN_STATUS_OK_CALL:
		ms->sap_entity.card_status = SAP_CARD_STATUS_NOT_ACC;
		/* fall-through */
	case SAP_CONN_STATUS_OK_READY:
		osmo_fsm_inst_state_chg(fi, SAP_STATE_WAIT_FOR_CARD, 0, 0);
		break;

	case SAP_CONN_STATUS_ERROR_SMALL_MSG_SIZE:
	case SAP_CONN_STATUS_ERROR_CONN:
		osmo_fsm_inst_state_chg(fi, SAP_STATE_NOT_CONNECTED, 0, 0);
		break;

	/* Section 4.1.1, MaxMsgSize negotiation */
	case SAP_CONN_STATUS_ERROR_MAX_MSG_SIZE:
		sap_negotiate_msg_size(&ms->sap_entity, sap_msg);
		break;
	}
}

static void sap_fsm_conn_release(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct msgb *msg;

	LOGP(DSAP, LOGL_DEBUG, "Initiating connection release\n");

	/* We don't care about possible allocating / sending errors */
	msg = sap_msgb_alloc(SAP_DISCONNECT_REQ);
	if (msg != NULL)
		sap_send_msgb((struct osmocom_ms *) fi->priv, msg);
}

static void sap_fsm_release_handler(struct osmo_fsm_inst *fi,
	uint32_t event, void *data)
{
	LOGP(DSAP, LOGL_DEBUG, "Connection release complete\n");
	osmo_fsm_inst_state_chg(fi, SAP_STATE_NOT_CONNECTED, 0, 0);
}

static void sap_fsm_idle_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;

	switch ((enum sap_fsm_state) prev_state) {
	case SAP_STATE_CONNECTING:
	case SAP_STATE_WAIT_FOR_CARD:
		/* According to 4.1, if a subscription module is inserted
		 * in the Server and powered on (i.e. STATUS_IND message
		 * indicates "Card reset" state), the Client shall request
		 * the ATR of the subscription module. */
		if (ms->sap_entity.card_status == SAP_CARD_STATUS_RESET)
			sap_send_atr_req(ms);
		break;
	default:
		/* Do nothing, suppress compiler warning */
		break;
	}
}

static void sap_fsm_idle_handler(struct osmo_fsm_inst *fi,
	uint32_t event, void *data)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;
	struct msgb *msg = (struct msgb *) data;
	enum sap_fsm_state state;
	int rc;

	/* Map event to the corresponding state */
	switch ((enum sap_msg_type) event) {
	case SAP_TRANSFER_ATR_REQ:
		state = SAP_STATE_PROC_ATR_REQ;
		break;
	case SAP_TRANSFER_APDU_REQ:
		state = SAP_STATE_PROC_APDU_REQ;
		break;
	case SAP_RESET_SIM_REQ:
		state = SAP_STATE_PROC_RESET_REQ;
		break;
	case SAP_TRANSFER_CARD_READER_STATUS_REQ:
		state = SAP_STATE_PROC_STATUS_REQ;
		break;
	case SAP_SET_TRANSPORT_PROTOCOL_REQ:
		state = SAP_STATE_PROC_SET_TP_REQ;
		break;
	case SAP_POWER_SIM_ON_REQ:
		state = SAP_STATE_PROC_POWERON_REQ;
		break;
	case SAP_POWER_SIM_OFF_REQ:
		state = SAP_STATE_PROC_POWEROFF_REQ;
		break;
	default:
		/* Shall not happen */
		OSMO_ASSERT(0);
	}

	rc = sap_send_msgb(ms, msg);
	if (rc)
		return;

	osmo_fsm_inst_state_chg(fi, state,
		SAP_FSM_PROC_REQ_TIMEOUT, SAP_FSM_PROC_REQ_T);
}

static void sap_fsm_response_handler(struct osmo_fsm_inst *fi,
	uint32_t event, void *data)
{
	struct sap_message *sap_msg = (struct sap_message *) data;
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;
	struct sap_param *param = NULL;
	uint16_t param_len = 0;
	int param_id, rc;

	switch ((enum sap_msg_type) event) {
	/* Both POWER_SIM_OFF_REQ and RESET_SIM_REQ can be sent in nearly
	 * any state, in order to allow the Client to reactivate
	 * a not accessible subscription module card. */
	case SAP_POWER_SIM_OFF_REQ:
	case SAP_RESET_SIM_REQ:
		OSMO_ASSERT(data != NULL);
		goto request;

	/* Messages without parameters */
	case SAP_SET_TRANSPORT_PROTOCOL_RESP:
	case SAP_POWER_SIM_OFF_RESP:
	case SAP_POWER_SIM_ON_RESP:
	case SAP_RESET_SIM_RESP:
		param_id = -1;
		break;

	case SAP_TRANSFER_CARD_READER_STATUS_RESP:
		param_id = SAP_CARD_READER_STATUS;
		break;
	case SAP_TRANSFER_APDU_RESP:
		param_id = SAP_RESPONSE_APDU;
		break;
	case SAP_TRANSFER_ATR_RESP:
		param_id = SAP_ATR;
		break;

	default:
		/* Shall not happen */
		OSMO_ASSERT(0);
	}

	/* We're done with request now */
	osmo_fsm_inst_state_chg(fi, SAP_STATE_IDLE, 0, 0);

	/* Check the ResultCode */
	rc = sap_check_result_code(sap_msg);
	if (rc != SAP_RESULT_OK_REQ_PROC_CORR) {
		LOGP(DSAP, LOGL_NOTICE, "Bad ResultCode: '%s'\n",
			get_value_string(sap_result_names, rc));
		goto response;
	}

	if (param_id < 0)
		goto response;

	param = sap_get_param(sap_msg, param_id, &param_len);
	if (!param) {
		LOGP(DSAP, LOGL_ERROR, "Message '%s' missing "
			"mandatory parameter '%s'\n",
			get_value_string(sap_msg_names, sap_msg->msg_id),
			get_value_string(sap_param_names, param_id));
		rc = -EINVAL;
		goto response;
	}

response:
	/* Poke optional response handler */
	if (ms->sap_entity.sap_rsp_cb != NULL) {
		if (param != NULL) {
			ms->sap_entity.sap_rsp_cb(ms, rc,
				sap_msg->msg_id, param_len, param->value);
		} else {
			ms->sap_entity.sap_rsp_cb(ms, rc,
				sap_msg->msg_id, 0, NULL);
		}
	}

	return;

request:
	rc = sap_send_msgb(ms, (struct msgb *) data);
	if (rc)
		return;

	osmo_fsm_inst_state_chg(fi, event == SAP_RESET_SIM_REQ ?
		SAP_STATE_PROC_RESET_REQ : SAP_STATE_PROC_POWEROFF_REQ,
		SAP_FSM_PROC_REQ_TIMEOUT, SAP_FSM_PROC_REQ_T);
}

/* Generates mask for a single state or event */
#define S(x) (1 << x)

/* Figure 4.13: Simplified State Machine */
static const struct osmo_fsm_state sap_fsm_states[] = {
	[SAP_STATE_NOT_CONNECTED] = {
		.name = "NOT_CONNECTED",
		.out_state_mask = S(SAP_STATE_CONNECTING),
		.onenter = &sap_fsm_disconnect,
	},
	[SAP_STATE_CONNECTING] = {
		.name = "CONNECTING",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_WAIT_FOR_CARD),
		.in_event_mask  = S(SAP_CONNECT_RESP),
		.onenter = &sap_fsm_connect,
		.action = &sap_fsm_conn_handler,
	},
	/* NOTE: this is a custom state (i.e. not defined by the specs).
	 * We need it in order to do release procedure correctly. */
	[SAP_STATE_DISCONNECTING] = {
		.name = "DISCONNECTING",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED),
		.in_event_mask  = S(SAP_DISCONNECT_RESP),
		.onenter = &sap_fsm_conn_release,
		.action = &sap_fsm_release_handler,
	},
	/* NOTE: this is a custom state (i.e. not defined by the specs).
	 * We need it in order to wait until SIM card becomes available.
	 * SAP_STATUS_IND event is handled by sap_fsm_allstate_action(). */
	[SAP_STATE_WAIT_FOR_CARD] = {
		.name = "WAIT_FOR_CARD",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_IDLE),
	},
	[SAP_STATE_IDLE] = {
		.name = "IDLE",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_PROC_APDU_REQ)
				| S(SAP_STATE_PROC_ATR_REQ)
				| S(SAP_STATE_PROC_RESET_REQ)
				| S(SAP_STATE_PROC_STATUS_REQ)
				| S(SAP_STATE_PROC_SET_TP_REQ)
				| S(SAP_STATE_PROC_POWERON_REQ)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_TRANSFER_ATR_REQ)
				| S(SAP_TRANSFER_APDU_REQ)
				| S(SAP_RESET_SIM_REQ)
				| S(SAP_TRANSFER_CARD_READER_STATUS_REQ)
				| S(SAP_SET_TRANSPORT_PROTOCOL_REQ)
				| S(SAP_POWER_SIM_ON_REQ)
				| S(SAP_POWER_SIM_OFF_REQ),
		.onenter = &sap_fsm_idle_enter,
		.action = &sap_fsm_idle_handler,
	},
	[SAP_STATE_PROC_ATR_REQ] = {
		.name = "PROC_ATR_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE)
				| S(SAP_STATE_PROC_RESET_REQ)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_TRANSFER_ATR_RESP)
				| S(SAP_RESET_SIM_REQ)
				| S(SAP_POWER_SIM_OFF_REQ),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_APDU_REQ] = {
		.name = "PROC_APDU_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE)
				| S(SAP_STATE_PROC_RESET_REQ)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_TRANSFER_APDU_RESP)
				| S(SAP_RESET_SIM_REQ)
				| S(SAP_POWER_SIM_OFF_REQ),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_RESET_REQ] = {
		.name = "PROC_RESET_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_RESET_SIM_RESP)
				| S(SAP_POWER_SIM_OFF_REQ),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_STATUS_REQ] = {
		.name = "PROC_STATUS_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE)
				| S(SAP_STATE_PROC_RESET_REQ)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_TRANSFER_CARD_READER_STATUS_RESP)
				| S(SAP_RESET_SIM_REQ)
				| S(SAP_POWER_SIM_OFF_REQ),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_SET_TP_REQ] = {
		.name = "PROC_SET_TP_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE),
		.in_event_mask  = S(SAP_SET_TRANSPORT_PROTOCOL_RESP),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_POWERON_REQ] = {
		.name = "PROC_POWERON_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE)
				| S(SAP_STATE_PROC_POWEROFF_REQ),
		.in_event_mask  = S(SAP_POWER_SIM_ON_RESP)
				| S(SAP_POWER_SIM_OFF_REQ),
		.action = &sap_fsm_response_handler,
	},
	[SAP_STATE_PROC_POWEROFF_REQ] = {
		.name = "PROC_POWEROFF_REQ",
		.out_state_mask = S(SAP_STATE_NOT_CONNECTED)
				| S(SAP_STATE_DISCONNECTING)
				| S(SAP_STATE_WAIT_FOR_CARD)
				| S(SAP_STATE_IDLE),
		.in_event_mask  = S(SAP_POWER_SIM_OFF_RESP),
		.action = &sap_fsm_response_handler,
	},
};

static void sap_fsm_tear_down(struct osmo_fsm_inst *fi,
	enum osmo_fsm_term_cause cause)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;

	/* Flush buffers, close socket */
	_sap_close_sock(ms);

	/* Reset SAP state */
	ms->sap_entity.card_status = SAP_CARD_STATUS_NOT_ACC;
	ms->sap_entity.max_msg_size = GSM_SAP_LENGTH;
	ms->sap_entity.fi = NULL;
}

static void sap_fsm_handle_card_status_ind(struct osmo_fsm_inst *fi,
	const struct sap_message *sap_msg)
{
	struct osmocom_ms *ms = (struct osmocom_ms *) fi->priv;
	struct sap_param *param;
	uint16_t param_len;
	uint8_t status;

	param = sap_get_param(sap_msg, SAP_STATUS_CHANGE, &param_len);
	if (!param || param_len != sizeof(status)) {
		LOGP(DSAP, LOGL_ERROR, "Missing mandatory '%s' parameter\n",
			get_value_string(sap_param_names, SAP_STATUS_CHANGE));
		return;
	}

	status = param->value[0];

	if (ms->sap_entity.card_status != status) {
		LOGP(DSAP, LOGL_NOTICE, "(SIM) card status change '%s' -> '%s'\n",
			get_value_string(sap_card_status_names, ms->sap_entity.card_status),
			get_value_string(sap_card_status_names, status));
		ms->sap_entity.card_status = status;
	}

	switch ((enum sap_card_status_type) status) {
	/* SIM card is ready */
	case SAP_CARD_STATUS_RESET:
		if (fi->state != SAP_STATE_IDLE)
			osmo_fsm_inst_state_chg(fi, SAP_STATE_IDLE, 0, 0);
		break;

	/* SIM card has recovered after unaccessful state */
	case SAP_CARD_STATUS_RECOVERED:
		if (fi->state != SAP_STATE_IDLE)
			osmo_fsm_inst_state_chg(fi, SAP_STATE_IDLE, 0, 0);
		break;

	/* SIM card inserted, we need to power it on */
	case SAP_CARD_STATUS_INSERTED:
		if (fi->state != SAP_STATE_IDLE)
			osmo_fsm_inst_state_chg(fi, SAP_STATE_IDLE, 0, 0);
		sap_send_poweron_req(ms);
		break;

	case SAP_CARD_STATUS_UNKNOWN_ERROR:
	case SAP_CARD_STATUS_NOT_ACC:
	case SAP_CARD_STATUS_REMOVED:
	default: /* Unknown card status */
		if (fi->state != SAP_STATE_WAIT_FOR_CARD)
			osmo_fsm_inst_state_chg(fi, SAP_STATE_WAIT_FOR_CARD, 0, 0);
		break;
	}
}

static void sap_fsm_allstate_action(struct osmo_fsm_inst *fi,
	uint32_t event, void *data)
{
	struct sap_message *sap_msg = (struct sap_message *) data;

	switch ((enum sap_msg_type) event) {
	/* Disconnect indication initiated by the Server.
	 * FIXME: at the moment, immediate release is always assumed,
	 * but ideally we should check type of release (using *data) */
	case SAP_DISCONNECT_IND:
		/* This message may arrive in any of the sub-states of
		 * the "Connected" state (i.e. connection shall exist) */
		if (!SAP_STATE_IS_ACTIVE(fi->state))
			goto not_peritted;

		sap_msg = NULL;
		/* fall-through */

	/* Disconnect initiated by the Client */
	case SAP_DISCONNECT_REQ:
		/* DISCONNECT_REQ has no parameters, so the caller
		 * shall not allocate the message manually. */
		OSMO_ASSERT(sap_msg == NULL);

		/* If we have no active connection, tear-down immediately */
		if (!SAP_STATE_IS_ACTIVE(fi->state)) {
			osmo_fsm_inst_state_chg(fi,
				SAP_STATE_NOT_CONNECTED, 0, 0);
			break;
		}

		/* Trigger Client-initiated connection release */
		osmo_fsm_inst_state_chg(fi, SAP_STATE_DISCONNECTING,
			SAP_FSM_CONN_REL_TIMEOUT, SAP_FSM_CONN_REL_T);
		break;

	/* SIM status indication (inserted or ejected) */
	case SAP_STATUS_IND:
		/* This message may arrive in any of the sub-states of
		 * the "Connected" state (i.e. connection shall exist) */
		if (!SAP_STATE_IS_ACTIVE(fi->state))
			goto not_peritted;

		sap_fsm_handle_card_status_ind(fi, sap_msg);
		break;

	case SAP_ERROR_RESP:
		LOGP(DSAP, LOGL_NOTICE, "RX Error Response from Server\n");

		if (fi->state == SAP_STATE_CONNECTING) {
			/* Connection establishment error */
			osmo_fsm_inst_state_chg(fi,
				SAP_STATE_NOT_CONNECTED, 0, 0);
		} else if (fi->state > SAP_STATE_IDLE) {
			/* Error replaces any Request message */
			osmo_fsm_inst_state_chg(fi,
				SAP_STATE_IDLE, 0, 0);
		} else {
			/* Should not happen in general */
			goto not_peritted;
		}
		break;

	default:
		/* Shall not happen */
		OSMO_ASSERT(0);
	}

	return;

not_peritted:
	LOGPFSML(fi, LOGL_NOTICE, "Event '%s' is not "
		"permitted in state '%s', please fix!\n",
		osmo_fsm_event_name(fi->fsm, event),
		osmo_fsm_state_name(fi->fsm, fi->state));
}

static int sap_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	switch ((enum sap_fsm_state) fi->state) {
	/* Connection establishment / release timeout */
	case SAP_STATE_DISCONNECTING:
	case SAP_STATE_CONNECTING:
		LOGP(DSAP, LOGL_NOTICE, "Connection timeout\n");
		osmo_fsm_inst_state_chg(fi, SAP_STATE_NOT_CONNECTED, 0, 0);
		break;

	/* Request processing timeout */
	case SAP_STATE_PROC_ATR_REQ:
	case SAP_STATE_PROC_APDU_REQ:
	case SAP_STATE_PROC_RESET_REQ:
	case SAP_STATE_PROC_STATUS_REQ:
	case SAP_STATE_PROC_SET_TP_REQ:
	case SAP_STATE_PROC_POWERON_REQ:
	case SAP_STATE_PROC_POWEROFF_REQ:
		LOGP(DSAP, LOGL_NOTICE, "Timeout waiting for '%s' to complete, "
			"going back to IDLE\n", osmo_fsm_inst_state_name(fi));
		osmo_fsm_inst_state_chg(fi, SAP_STATE_IDLE, 0, 0);
		break;

	default:
		LOGP(DSAP, LOGL_ERROR, "Timeout for unhandled state '%s'\n",
			osmo_fsm_inst_state_name(fi));
	}

	/* Do not tear-down FSM */
	return 0;
}

static struct osmo_fsm sap_fsm_def = {
	.name = "sap_fsm",
	.log_subsys = DSAP,
	.states = sap_fsm_states,
	.num_states = ARRAY_SIZE(sap_fsm_states),
	.event_names = sap_msg_names,
	.cleanup = &sap_fsm_tear_down,
	.timer_cb = &sap_fsm_timer_cb,
	.allstate_action = &sap_fsm_allstate_action,
	.allstate_event_mask = 0
		| S(SAP_DISCONNECT_REQ)
		| S(SAP_DISCONNECT_IND)
		| S(SAP_STATUS_IND)
		| S(SAP_ERROR_RESP),
};

/*! Allocate a new SAP state machine for a given ms.
 * \param[in] ms MS instance associated with SAP FSM
 * \returns 0 in case of success, negative in case of error
 */
int sap_fsm_alloc(struct osmocom_ms *ms)
{
	struct osmosap_entity *sap;

	sap = &ms->sap_entity;
	OSMO_ASSERT(sap->fi == NULL);

	/* Register our FSM (if required) */
	if (!osmo_fsm_find_by_name(sap_fsm_def.name))
		OSMO_ASSERT(osmo_fsm_register(&sap_fsm_def) == 0);

	/* Allocate an instance using ms as talloc context */
	sap->fi = osmo_fsm_inst_alloc(&sap_fsm_def, ms,
		ms, LOGL_DEBUG, ms->name);
	if (!sap->fi) {
		LOGP(DSAP, LOGL_ERROR, "Failed to allocate SAP FSM\n");
		return -ENOMEM;
	}

	return 0;
}