fsm completion event handling from main loop

Change-Id: I14062c51494d3abb08e9237ace17726c53deeccc
diff --git a/ccid_common/ccid_device.h b/ccid_common/ccid_device.h
index 38a71db..41222d4 100644
--- a/ccid_common/ccid_device.h
+++ b/ccid_common/ccid_device.h
@@ -57,6 +57,8 @@
 	struct ccid_pars_decoded proposed_pars;
 	/* default parameters; applied on ResetParameters */
 	const struct ccid_pars_decoded *default_pars;
+	uint32_t event;
+	void* event_data;
 };
 
 /* CCID operations provided by USB transport layer */
@@ -86,6 +88,7 @@
 			  const struct ccid_pars_decoded *pars_dec);
 	int (*set_rate_and_clock)(struct ccid_slot *cs, uint32_t freq_hz, uint32_t rate_bps);
 	void (*icc_set_insertion_status)(struct ccid_slot *cs, bool present);
+	int (*handle_fsm_events)(struct ccid_slot *cs, bool enable);
 };
 
 /* An instance of CCID (i.e. a card reader device) */
diff --git a/ccid_common/ccid_slot_fsm.c b/ccid_common/ccid_slot_fsm.c
index 8d38f29..d1b028c 100644
--- a/ccid_common/ccid_slot_fsm.c
+++ b/ccid_common/ccid_slot_fsm.c
@@ -110,12 +110,35 @@
 {
 	struct iso_fsm_slot *ss = iso7816_fsm_get_user_priv(fi);
 	struct ccid_slot *cs = ss->cs;
-	struct msgb *tpdu, *resp;
 
 	switch (event) {
 	case ISO7816_E_ATR_DONE_IND:
+	case ISO7816_E_TPDU_DONE_IND:
+	case ISO7816_E_TPDU_FAILED_IND:
+	case ISO7816_E_PPS_DONE_IND:
+	case ISO7816_E_PPS_FAILED_IND:
+		cs->event = event;
+		cs->event_data = data;
+		delay_us(100);
+		break;
+	default:
+		LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, cause=%d, data=%p) unhandled\n",
+			__func__, event, cause, data);
+		break;
+	}
+}
+
+static int iso_handle_fsm_events(struct ccid_slot *cs, bool enable){
+	struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
+	struct msgb *tpdu, *resp;
+	uint32_t event = cs->event;
+	void* data = cs->event_data;
+
+	cs->event = 0;
+	switch (event) {
+	case ISO7816_E_ATR_DONE_IND:
 		tpdu = data;
-		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, cause=%d, data=%s)\n", __func__, event, cause,
+		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
 			msgb_hexdump(tpdu));
 		resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0,
 					   msgb_data(tpdu), msgb_length(tpdu));
@@ -124,7 +147,7 @@
 		break;
 	case ISO7816_E_TPDU_DONE_IND:
 		tpdu = data;
-		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, cause=%d, data=%s)\n", __func__, event, cause,
+		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
 			msgb_hexdump(tpdu));
 		resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_OK, 0, msgb_l2(tpdu), msgb_l2len(tpdu));
 		ccid_slot_send_unbusy(cs, resp);
@@ -132,7 +155,7 @@
 		break;
 	case ISO7816_E_TPDU_FAILED_IND:
 		tpdu = data;
-		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, cause=%d, data=%s)\n", __func__, event, cause,
+		LOGPCS(cs, LOGL_DEBUG, "%s(event=%d, data=%s)\n", __func__, event,
 			msgb_hexdump(tpdu));
 		/* FIXME: other error causes than card removal?*/
 		resp = ccid_gen_data_block(cs, ss->seq, CCID_CMD_STATUS_FAILED, CCID_ERR_ICC_MUTE, msgb_l2(tpdu), 0);
@@ -167,8 +190,8 @@
 		msgb_free(tpdu);
 		break;
 	default:
-		LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, cause=%d, data=%p) unhandled\n",
-			__func__, event, cause, data);
+		LOGPCS(cs, LOGL_NOTICE, "%s(event=%d, data=%p) unhandled\n",
+			__func__, event, data);
 		break;
 	}
 }
@@ -177,7 +200,7 @@
 				const struct ccid_pc_to_rdr_xfr_block *xfb)
 {
 	struct iso_fsm_slot *ss = ccid_slot2iso_fsm_slot(cs);
-	struct msgb *tpdu;
+
 
 	if(!cs->icc_present)
 		return -CCID_ERR_ICC_MUTE;
@@ -188,17 +211,10 @@
 	OSMO_ASSERT(xfb->wLevelParameter == 0x0000);
 	OSMO_ASSERT(msgb_length(msg) > xfb->hdr.dwLength);
 
-	/* 'msg' contains the raw CCID message as received from USB. We could create
-	 * a new message buffer for the ISO7816 side here or we could 'strip the CCID
-	 * header off the start of the message. Let's KISS and do a copy here */
-	tpdu = msgb_alloc(512, "TPDU");
-	OSMO_ASSERT(tpdu);
-	memcpy(msgb_data(tpdu), xfb->abData, xfb->hdr.dwLength);
-	msgb_put(tpdu, xfb->hdr.dwLength);
-	msgb_free(msg);
+	msgb_pull(msg, 10);
 
-	LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer: %s\n", msgb_hexdump(tpdu));
-	osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, tpdu);
+	LOGPCS(cs, LOGL_DEBUG, "scheduling TPDU transfer: %s\n", msgb_hexdump(msg));
+	osmo_fsm_inst_dispatch(ss->fi, ISO7816_E_XCEIVE_TPDU_CMD, msg);
 	/* continues in iso_fsm_clot_user_cb once response/error/timeout is received */
 	return 0;
 }
@@ -332,4 +348,5 @@
 	.set_clock = iso_fsm_slot_set_clock,
 	.set_params = iso_fsm_slot_set_params,
 	.set_rate_and_clock = iso_fsm_slot_set_rate_and_clock,
+	.handle_fsm_events = iso_handle_fsm_events,
 };
diff --git a/sysmoOCTSIM/main.c b/sysmoOCTSIM/main.c
index b44e362..de19cae 100644
--- a/sysmoOCTSIM/main.c
+++ b/sysmoOCTSIM/main.c
@@ -316,7 +316,7 @@
 {
 	uint8_t statusbytes[2] = {0};
 	//struct msgb *msg = ccid_msgb_alloc();
-	struct msgb *msg = msgb_alloc(64, "IRQ");
+	struct msgb *msg = msgb_alloc(300,"IRQ");
 	struct ccid_rdr_to_pc_notify_slot_change *nsc = msgb_put(msg, sizeof(*nsc) + sizeof(statusbytes));
 	nsc->bMessageType = RDR_to_PC_NotifySlotChange;
 
@@ -1191,6 +1191,9 @@
 		command_try_recv();
 		poll_card_detect();
 		submit_next_irq();
+		for (int i = 0; i < usb_fs_descs.ccid.class.bMaxSlotIndex; i++){
+			g_ci.slot_ops->handle_fsm_events(&g_ci.slot[i], true);
+		}
 		feed_ccid();
 		osmo_timers_update();
 		int qs = llist_count_at(&g_ccid_s.free_q);
@@ -1201,13 +1204,12 @@
 				msgb_free(msg);
 			}
 		if(qs < NUM_OUT_BUF)
-			for (int i= 0; i < qs-NUM_OUT_BUF; i++){
+			for (int i= 0; i < NUM_OUT_BUF-qs; i++){
 				struct msgb *msg = msgb_alloc(300,"ccid");
 				OSMO_ASSERT(msg);
 				/* return the message back to the queue of free message buffers */
 				llist_add_tail_at(&msg->list, &g_ccid_s.free_q);
 			}
 
-
 	}
 }