+#include "mifare.h"
+#include "proxmark3.h"
+#include "iso14443crc.h"
+#include "protocols.h"
+
+
+
+static void topaz_switch_on_field(void)
+{
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_SELECT | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE, 0, 0}};
+ SendCommand(&c);
+
+ UsbCommand resp;
+ WaitForResponse(CMD_ACK, &resp);
+}
+
+
+static void topaz_switch_off_field(void)
+{
+ UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
+ SendCommand(&c);
+
+ UsbCommand resp;
+ WaitForResponse(CMD_ACK, &resp);
+}
+
+
+static void topaz_send_cmd_raw(uint8_t *cmd, uint8_t len, uint8_t *response)
+{
+ UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_RAW | ISO14A_NO_DISCONNECT | ISO14A_TOPAZMODE, len, 0}};
+ memcpy(c.d.asBytes, cmd, len);
+ SendCommand(&c);
+
+ UsbCommand resp;
+ WaitForResponse(CMD_ACK, &resp);
+
+ memcpy(response, resp.d.asBytes, resp.arg[0]);
+}
+
+
+static void topaz_send_cmd(uint8_t *cmd, uint8_t len, uint8_t *response)
+{
+ if (len > 1) {
+ uint8_t first, second;
+ ComputeCrc14443(CRC_14443_B, cmd, len, &first, &second);
+ cmd[len] = first;
+ cmd[len+1] = second;
+ }
+
+ topaz_send_cmd_raw(cmd, len+2, response);
+}
+
+
+static void topaz_select(uint8_t *atqa, uint8_t *uid)
+{
+ // ToDo: implement anticollision
+ uint8_t rid_cmd[] = {TOPAZ_RID, 0, 0, 0, 0, 0, 0, 0, 0};
+ uint8_t wupa_cmd[] = {TOPAZ_WUPA};
+
+ topaz_switch_on_field();
+ topaz_send_cmd(wupa_cmd, sizeof(wupa_cmd), atqa);
+ topaz_send_cmd(rid_cmd, sizeof(rid_cmd) - 2, uid);
+ topaz_switch_off_field();
+}
+