+size_t 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 0;
+ }
+ }
+ return 1;//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
+ */
+uint8_t handshakeIclassTag(uint8_t *card_data)
+{
+ static uint8_t act_all[] = { 0x0a };
+ static uint8_t identify[] = { 0x0c };
+ static uint8_t select[] = { 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+
+ static uint8_t readcheck_cc[]= { 0x88, 0x02,};
+
+ 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)
+ 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;
+}
+
+