+static void ul_switch_off_field(void) {
+ UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
+ SendCommand(&c);
+}
+
+static int ul_send_cmd_raw( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength ) {
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_APPEND_CRC, cmdlen, 0}};
+ memcpy(c.d.asBytes, cmd, cmdlen);
+ SendCommand(&c);
+ UsbCommand resp;
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
+
+ uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
+ if (resp.arg[0] > 0) {
+ memcpy(response, resp.d.asBytes, resplen);
+ return resplen;
+ } else return -1;
+}
+/*
+static int ul_send_cmd_raw_crc( uint8_t *cmd, uint8_t cmdlen, uint8_t *response, uint16_t responseLength, bool append_crc ) {
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT , cmdlen, 0}};
+ if (append_crc)
+ c.arg[0] |= ISO14A_APPEND_CRC;
+
+ memcpy(c.d.asBytes, cmd, cmdlen);
+ SendCommand(&c);
+ UsbCommand resp;
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
+
+ uint16_t resplen = (resp.arg[0] < responseLength) ? resp.arg[0] : responseLength;
+ if (resp.arg[0] > 0) {
+ memcpy(response, resp.d.asBytes, resplen);
+ return resplen;
+ } else return -1;
+}
+*/
+static int ul_select( iso14a_card_select_t *card ){
+
+ ul_switch_on_field();
+
+ UsbCommand resp;
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) return -1;
+ if (resp.arg[0] < 1) return -1;
+
+ memcpy(card, resp.d.asBytes, sizeof(iso14a_card_select_t));
+ return resp.arg[0];
+}
+
+// This read command will at least return 16bytes.
+static int ul_read( uint8_t page, uint8_t *response, uint16_t responseLength ){
+
+ uint8_t cmd[] = {ISO14443A_CMD_READBLOCK, page};
+ int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
+ if ( len == -1 )
+ ul_switch_off_field();
+ return len;
+}
+
+static int ulc_requestAuthentication( uint8_t blockNo, uint8_t *nonce, uint16_t nonceLength ){
+
+ uint8_t cmd[] = {MIFARE_ULC_AUTH_1, blockNo};
+ int len = ul_send_cmd_raw(cmd, sizeof(cmd), nonce, nonceLength);
+ if ( len == -1 )
+ ul_switch_off_field();
+ return len;
+}
+
+static int ulev1_requestAuthentication( uint8_t *pwd, uint8_t *pack, uint16_t packLength ){
+
+ uint8_t cmd[] = {MIFARE_ULEV1_AUTH, pwd[0], pwd[1], pwd[2], pwd[3]};
+ int len = ul_send_cmd_raw(cmd, sizeof(cmd), pack, packLength);
+ if ( len == -1)
+ ul_switch_off_field();
+ return len;
+}
+
+static int ulev1_getVersion( uint8_t *response, uint16_t responseLength ){
+
+ uint8_t cmd[] = {MIFARE_ULEV1_VERSION};
+ int len = ul_send_cmd_raw(cmd, sizeof(cmd), response, responseLength);
+ if ( len == -1 )
+ ul_switch_off_field();
+ return len;
+}
+
+// static int ulev1_fastRead( uint8_t startblock, uint8_t endblock, uint8_t *response ){