+ uint8_t first, second;
+ ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
+ data[datalen++] = first;
+ data[datalen++] = second;
+ }
+
+ if(active || active_select)
+ {
+ c.arg[0] |= ISO14A_CONNECT;
+ if(active)
+ c.arg[0] |= ISO14A_NO_SELECT;
+ }
+ if(bTimeout){
+ #define MAX_TIMEOUT 624*105 // max timeout is 624 ms
+ c.arg[0] |= ISO14A_SET_TIMEOUT;
+ c.arg[2] = timeout * 105; // each bit is about 9.4 us
+ if(c.arg[2]>MAX_TIMEOUT) {
+ c.arg[2] = MAX_TIMEOUT;
+ PrintAndLog("Set timeout to 624 ms. The max we can wait for response");
+ }
+ }
+ if(power)
+ c.arg[0] |= ISO14A_NO_DISCONNECT;
+ if(datalen>0)
+ c.arg[0] |= ISO14A_RAW;
+
+ // Max buffer is USB_CMD_DATA_SIZE
+ c.arg[1] = (datalen & 0xFFFF) | (numbits << 16);
+ memcpy(c.d.asBytes,data,datalen);
+
+ SendCommand(&c);
+
+ if (reply) {
+ if(active_select)
+ waitCmd(1);
+ if(datalen>0)
+ waitCmd(0);
+ } // if reply
+ return 0;
+}
+
+static void waitCmd(uint8_t iSelect)
+{
+ uint8_t *recv;
+ UsbCommand resp;
+ char *hexout;
+
+ if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
+ recv = resp.d.asBytes;
+ uint8_t iLen = iSelect ? resp.arg[1] : resp.arg[0];
+ PrintAndLog("received %i octets",iLen);
+ if(!iLen)
+ return;
+ hexout = (char *)malloc(iLen * 3 + 1);
+ if (hexout != NULL) {
+ for (int i = 0; i < iLen; i++) { // data in hex
+ sprintf(&hexout[i * 3], "%02X ", recv[i]);
+ }
+ PrintAndLog("%s", hexout);
+ free(hexout);
+ } else {
+ PrintAndLog("malloc failed your client has low memory?");
+ }
+ } else {
+ PrintAndLog("timeout while waiting for reply.");