+}
+
+static bool sendCmdGetResponseWithRetries(uint8_t* command, size_t cmdsize, uint8_t* resp, uint8_t expected_size, uint8_t retries) {
+ while (retries-- > 0) {
+ ReaderTransmitIClass(command, cmdsize);
+ if (expected_size == ReaderReceiveIClass(resp)) {
+ return true;
+ }
+ }
+ return false;//Error
+}
+
+/**
+ * @brief Talks to an iclass tag, sends the commands to get CSN and CC.
+ * @param card_data where the CSN and CC are stored for return
+ * @return 0 = fail
+ * 1 = Got CSN
+ * 2 = Got CSN and CC
+ */
+static uint8_t handshakeIclassTag_ext(uint8_t *card_data, bool use_credit_key) {
+ static uint8_t act_all[] = { 0x0a };
+ //static uint8_t identify[] = { 0x0c };
+ static uint8_t identify[] = { 0x0c, 0x00, 0x73, 0x33 };
+ static uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+ static uint8_t readcheck_cc[]= { 0x88, 0x02 };
+ if (use_credit_key)
+ readcheck_cc[0] = 0x18;
+ else
+ readcheck_cc[0] = 0x88;
+
+ uint8_t resp[ICLASS_BUFFER_SIZE];
+
+ uint8_t read_status = 0;
+
+ // Send act_all
+ ReaderTransmitIClass(act_all, 1);
+ // Card present?
+ if (!ReaderReceiveIClass(resp)) return read_status;//Fail
+ //Send Identify
+ ReaderTransmitIClass(identify, 1);
+ //We expect a 10-byte response here, 8 byte anticollision-CSN and 2 byte CRC
+ uint8_t len = ReaderReceiveIClass(resp);
+ if (len != 10) return read_status;//Fail
+
+ //Copy the Anti-collision CSN to our select-packet
+ memcpy(&select[1], resp, 8);
+ //Select the card
+ ReaderTransmitIClass(select, sizeof(select));
+ //We expect a 10-byte response here, 8 byte CSN and 2 byte CRC
+ len = ReaderReceiveIClass(resp);
+ if (len != 10) return read_status;//Fail
+
+ //Success - level 1, we got CSN
+ //Save CSN in response data
+ memcpy(card_data, resp, 8);
+
+ //Flag that we got to at least stage 1, read CSN
+ read_status = 1;
+
+ // Card selected, now read e-purse (cc) (only 8 bytes no CRC)
+ ReaderTransmitIClass(readcheck_cc, sizeof(readcheck_cc));
+ if (ReaderReceiveIClass(resp) == 8) {
+ //Save CC (e-purse) in response data
+ memcpy(card_data+8, resp, 8);
+ read_status++;
+ }
+
+ return read_status;
+}
+
+static uint8_t handshakeIclassTag(uint8_t *card_data) {
+ return handshakeIclassTag_ext(card_data, false);
+}
+
+
+// Reader iClass Anticollission
+void ReaderIClass(uint8_t arg0) {
+
+ uint8_t card_data[6 * 8] = {0};
+ memset(card_data, 0xFF, sizeof(card_data));
+ uint8_t last_csn[8] = {0,0,0,0,0,0,0,0};
+ uint8_t resp[ICLASS_BUFFER_SIZE];
+ memset(resp, 0xFF, sizeof(resp));
+ //Read conf block CRC(0x01) => 0xfa 0x22
+ uint8_t readConf[] = { ICLASS_CMD_READ_OR_IDENTIFY, 0x01, 0xfa, 0x22};
+ //Read App Issuer Area block CRC(0x05) => 0xde 0x64
+ uint8_t readAA[] = { ICLASS_CMD_READ_OR_IDENTIFY, 0x05, 0xde, 0x64};
+
+ int read_status= 0;
+ uint8_t result_status = 0;
+ // flag to read until one tag is found successfully
+ bool abort_after_read = arg0 & FLAG_ICLASS_READER_ONLY_ONCE;
+ // flag to only try 5 times to find one tag then return
+ bool try_once = arg0 & FLAG_ICLASS_READER_ONE_TRY;
+ // if neither abort_after_read nor try_once then continue reading until button pressed.
+
+ bool use_credit_key = arg0 & FLAG_ICLASS_READER_CEDITKEY;
+ // test flags for what blocks to be sure to read
+ uint8_t flagReadConfig = arg0 & FLAG_ICLASS_READER_CONF;
+ uint8_t flagReadCC = arg0 & FLAG_ICLASS_READER_CC;
+ uint8_t flagReadAA = arg0 & FLAG_ICLASS_READER_AA;
+
+ set_tracing(true);
+ setupIclassReader();
+
+ uint16_t tryCnt = 0;
+ bool userCancelled = BUTTON_PRESS() || usb_poll_validate_length();
+ while (!userCancelled) {
+ // if only looking for one card try 2 times if we missed it the first time
+ if (try_once && tryCnt > 2) {
+ break;
+ }
+ tryCnt++;
+ if (!get_tracing()) {