#include "string.h"
#include "iso15693tools.h"
#include "protocols.h"
-#include "cmd.h"
+#include "usb_cdc.h"
#include "BigBuf.h"
#include "fpgaloader.h"
#define DELAY_TAG_TO_ARM_SNOOP 32
#define DELAY_READER_TO_ARM_SNOOP 32
+// times in samples @ 212kHz when acting as reader
+//#define ISO15693_READER_TIMEOUT 80 // 80/212kHz = 378us, nominal t1_max=313,9us
+#define ISO15693_READER_TIMEOUT 330 // 330/212kHz = 1558us, should be even enough for iClass tags responding to ACTALL
+#define ISO15693_READER_TIMEOUT_WRITE 4700 // 4700/212kHz = 22ms, nominal 20ms
+
+
static int DEBUG = 0;
///////////////////////////////////////////////////////////////////////
// buffers
-#define ISO15693_DMA_BUFFER_SIZE 128 // must be a power of 2
+#define ISO15693_DMA_BUFFER_SIZE 256 // must be a power of 2
#define ISO15693_MAX_RESPONSE_LENGTH 36 // allows read single block with the maximum block size of 256bits. Read multiple blocks not supported yet
#define ISO15693_MAX_COMMAND_LENGTH 45 // allows write single block with the maximum block size of 256bits. Write multiple blocks not supported yet
ToSendMax++;
}
+
+// Encode EOF only
+static void CodeIso15693AsReaderEOF() {
+ ToSendReset();
+ ToSend[++ToSendMax] = 0x20;
+ ToSendMax++;
+}
+
+
// encode data using "1 out of 256" scheme
// data rate is 1,66 kbit/s (fc/8192)
// is designed for more robust communication over longer distances
}
-static void jam(void) {
- // send a short burst to jam the reader signal
-}
-
-
//=============================================================================
// An ISO 15693 decoder for tag responses (one subcarrier only).
// Uses cross correlation to identify each bit and EOF.
} DecodeTag_t;
-static int inline __attribute__((always_inline)) Handle15693SamplesFromTag(uint16_t amplitude, DecodeTag_t *restrict DecodeTag) {
+static int inline __attribute__((always_inline)) Handle15693SamplesFromTag(uint16_t amplitude, DecodeTag_t *DecodeTag) {
switch (DecodeTag->state) {
case STATE_TAG_SOF_LOW:
// waiting for a rising edge
STATE_READER_AWAIT_2ND_RISING_EDGE_OF_SOF,
STATE_READER_AWAIT_END_OF_SOF_1_OUT_OF_4,
STATE_READER_RECEIVE_DATA_1_OUT_OF_4,
- STATE_READER_RECEIVE_DATA_1_OUT_OF_256
+ STATE_READER_RECEIVE_DATA_1_OUT_OF_256,
+ STATE_READER_RECEIVE_JAMMING
} state;
enum {
CODING_1_OUT_OF_4,
}
-static int inline __attribute__((always_inline)) Handle15693SampleFromReader(bool bit, DecodeReader_t *restrict DecodeReader) {
+static int inline __attribute__((always_inline)) Handle15693SampleFromReader(bool bit, DecodeReader_t *DecodeReader) {
switch (DecodeReader->state) {
case STATE_READER_UNSYNCD:
// wait for unmodulated carrier
}
if (DecodeReader->bitCount == 15) { // we have a full byte
DecodeReader->output[DecodeReader->byteCount++] = DecodeReader->shiftReg;
- if (DecodeReader->byteCount == DecodeReader->jam_search_len) {
- if (!memcmp(DecodeReader->output, DecodeReader->jam_search_string, DecodeReader->jam_search_len)) {
- jam(); // send a jamming signal
- Dbprintf("JAMMING!");
- }
- }
if (DecodeReader->byteCount > DecodeReader->byteCountMax) {
// buffer overflow, give up
LED_B_OFF();
}
DecodeReader->bitCount = 0;
DecodeReader->shiftReg = 0;
+ if (DecodeReader->byteCount == DecodeReader->jam_search_len) {
+ if (!memcmp(DecodeReader->output, DecodeReader->jam_search_string, DecodeReader->jam_search_len)) {
+ LED_D_ON();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_JAM);
+ DecodeReader->state = STATE_READER_RECEIVE_JAMMING;
+ }
+ }
} else {
DecodeReader->bitCount++;
}
LED_B_OFF();
DecodeReaderReset(DecodeReader);
}
+ if (DecodeReader->byteCount == DecodeReader->jam_search_len) {
+ if (!memcmp(DecodeReader->output, DecodeReader->jam_search_string, DecodeReader->jam_search_len)) {
+ LED_D_ON();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SEND_JAM);
+ DecodeReader->state = STATE_READER_RECEIVE_JAMMING;
+ }
+ }
}
DecodeReader->bitCount++;
}
break;
+ case STATE_READER_RECEIVE_JAMMING:
+ DecodeReader->posCount++;
+ if (DecodeReader->Coding == CODING_1_OUT_OF_4) {
+ if (DecodeReader->posCount == 7*16) { // 7 bits jammed
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE); // stop jamming
+ // FpgaDisableTracing();
+ LED_D_OFF();
+ } else if (DecodeReader->posCount == 8*16) {
+ DecodeReader->posCount = 0;
+ DecodeReader->output[DecodeReader->byteCount++] = 0x00;
+ DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_4;
+ }
+ } else {
+ if (DecodeReader->posCount == 7*256) { // 7 bits jammend
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE); // stop jamming
+ LED_D_OFF();
+ } else if (DecodeReader->posCount == 8*256) {
+ DecodeReader->posCount = 0;
+ DecodeReader->output[DecodeReader->byteCount++] = 0x00;
+ DecodeReader->state = STATE_READER_RECEIVE_DATA_1_OUT_OF_256;
+ }
+ }
+ break;
+
default:
LED_B_OFF();
DecodeReaderReset(DecodeReader);
}
-// Encode (into the ToSend buffers) an identify request, which is the first
+// Construct an identify (Inventory) request, which is the first
// thing that you must send to a tag to get a response.
-static void BuildIdentifyRequest(void)
-{
- uint8_t cmd[5];
-
+static void BuildIdentifyRequest(uint8_t *cmd) {
uint16_t crc;
// one sub-carrier, inventory, 1 slot, fast rate
- // AFI is at bit 5 (1<<4) when doing an INVENTORY
- cmd[0] = (1 << 2) | (1 << 5) | (1 << 1);
+ cmd[0] = ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1 | ISO15693_REQ_DATARATE_HIGH;
// inventory command code
cmd[1] = 0x01;
// no mask
crc = Iso15693Crc(cmd, 3);
cmd[3] = crc & 0xff;
cmd[4] = crc >> 8;
-
- CodeIso15693AsReader(cmd, sizeof(cmd));
}
// for the response. The response is not demodulated, just left in the buffer
// so that it can be downloaded to a PC and processed there.
//-----------------------------------------------------------------------------
-void AcquireRawAdcSamplesIso15693(void)
-{
+void AcquireRawAdcSamplesIso15693(void) {
LED_A_ON();
uint8_t *dest = BigBuf_get_addr();
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
- BuildIdentifyRequest();
+ uint8_t cmd[5];
+ BuildIdentifyRequest(cmd);
+ CodeIso15693AsReader(cmd, sizeof(cmd));
// Give the tags time to energize
SpinDelay(100);
if (upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
upTo = dmaBuf; // start reading the circular buffer from the beginning
if (behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
- FpgaDisableTracing();
+ // FpgaDisableTracing();
Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy, samples);
break;
}
FpgaDisableSscDma();
- LEDsoff();
-
DbpString("Snoop statistics:");
Dbprintf(" ExpectTagAnswer: %d, TagIsActive: %d, ReaderIsActive: %d", ExpectTagAnswer, TagIsActive, ReaderIsActive);
Dbprintf(" DecodeTag State: %d", DecodeTag.state);
// uid is in transmission order (which is reverse of display order)
-static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber )
-{
- uint8_t cmd[13];
-
+static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber, uint8_t *cmd) {
uint16_t crc;
// If we set the Option_Flag in this request, the VICC will respond with the security status of the block
// followed by the block data
cmd[11] = crc & 0xff;
cmd[12] = crc >> 8;
- CodeIso15693AsReader(cmd, sizeof(cmd));
}
// Now the VICC>VCD responses when we are simulating a tag
-static void BuildInventoryResponse(uint8_t *uid)
-{
+static void BuildInventoryResponse(uint8_t *uid) {
uint8_t cmd[12];
uint16_t crc;
// speed ... 0 low speed, 1 hi speed
// *recv will contain the tag's answer
// return: length of received data, or -1 for timeout
-int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint32_t *eof_time) {
+int SendDataTag(uint8_t *send, int sendlen, bool init, bool speed_fast, uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint16_t timeout, uint32_t *eof_time) {
if (init) {
Iso15693InitReader();
int answerLen = 0;
- if (!speed) {
- // low speed (1 out of 256)
- CodeIso15693AsReader256(send, sendlen);
- } else {
+ if (speed_fast) {
// high speed (1 out of 4)
CodeIso15693AsReader(send, sendlen);
+ } else {
+ // low speed (1 out of 256)
+ CodeIso15693AsReader256(send, sendlen);
}
TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
+ uint32_t end_time = start_time + 32*(8*ToSendMax-4); // substract the 4 padding bits after EOF
+ LogTrace_ISO15693(send, sendlen, start_time*4, end_time*4, NULL, true);
// Now wait for a response
if (recv != NULL) {
- answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, ISO15693_READER_TIMEOUT, eof_time);
+ answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, timeout, eof_time);
+ }
+
+ return answerLen;
+}
+
+
+int SendDataTagEOF(uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint16_t timeout, uint32_t *eof_time) {
+
+ int answerLen = 0;
+
+ CodeIso15693AsReaderEOF();
+
+ TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
+ uint32_t end_time = start_time + 32*(8*ToSendMax-4); // substract the 4 padding bits after EOF
+ LogTrace_ISO15693(NULL, 0, start_time*4, end_time*4, NULL, true);
+
+ // Now wait for a response
+ if (recv != NULL) {
+ answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, timeout, eof_time);
}
return answerLen;
set_tracing(true);
- int answerLen = 0;
uint8_t TagUID[8] = {0x00};
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
// THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
// Now send the IDENTIFY command
- BuildIdentifyRequest();
+ uint8_t cmd[5];
+ BuildIdentifyRequest(cmd);
uint32_t start_time = 0;
- TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
-
- // Now wait for a response
uint32_t eof_time;
- answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time) ;
+ int answerLen = SendDataTag(cmd, sizeof(cmd), true, true, answer, sizeof(answer), start_time, ISO15693_READER_TIMEOUT, &eof_time);
start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
- if (answerLen >=12) // we should do a better check than this
- {
+ if (answerLen >= 12) { // we should do a better check than this
TagUID[0] = answer[2];
TagUID[1] = answer[3];
TagUID[2] = answer[4];
TagUID[5] = answer[7];
TagUID[6] = answer[8]; // IC Manufacturer code
TagUID[7] = answer[9]; // always E0
-
}
Dbprintf("%d octets read from IDENTIFY request:", answerLen);
TagUID[7],TagUID[6],TagUID[5],TagUID[4],
TagUID[3],TagUID[2],TagUID[1],TagUID[0]);
-
- // Dbprintf("%d octets read from SELECT request:", answerLen2);
- // DbdecodeIso15693Answer(answerLen2,answer2);
- // Dbhexdump(answerLen2,answer2,true);
-
- // Dbprintf("%d octets read from XXX request:", answerLen3);
- // DbdecodeIso15693Answer(answerLen3,answer3);
- // Dbhexdump(answerLen3,answer3,true);
-
// read all pages
if (answerLen >= 12 && DEBUG) {
for (int i = 0; i < 32; i++) { // sanity check, assume max 32 pages
- BuildReadBlockRequest(TagUID, i);
- TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
- int answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time);
+ uint8_t cmd[13];
+ BuildReadBlockRequest(TagUID, i, cmd);
+ answerLen = SendDataTag(cmd, sizeof(cmd), false, true, answer, sizeof(answer), start_time, ISO15693_READER_TIMEOUT, &eof_time);
start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
if (answerLen > 0) {
Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i, answerLen);
}
}
- // for the time being, switch field off to protect rdv4.0
+ // for the time being, switch field off to protect RDV4
// note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
LED_D_OFF();
// Since there is no standardized way of reading the AFI out of a tag, we will brute force it
// (some manufactures offer a way to read the AFI, though)
-void BruteforceIso15693Afi(uint32_t speed)
-{
+void BruteforceIso15693Afi(uint32_t speed) {
LED_A_ON();
uint8_t data[6];
data[2] = 0; // mask length
datalen = Iso15693AddCrc(data,3);
uint32_t start_time = GetCountSspClk();
- recvlen = SendDataTag(data, datalen, true, speed, recv, sizeof(recv), 0, &eof_time);
+ recvlen = SendDataTag(data, datalen, true, speed, recv, sizeof(recv), 0, ISO15693_READER_TIMEOUT, &eof_time);
start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
WDT_HIT();
if (recvlen>=12) {
for (int i = 0; i < 256; i++) {
data[2] = i & 0xFF;
datalen = Iso15693AddCrc(data,4);
- recvlen = SendDataTag(data, datalen, false, speed, recv, sizeof(recv), start_time, &eof_time);
+ recvlen = SendDataTag(data, datalen, false, speed, recv, sizeof(recv), start_time, ISO15693_READER_TIMEOUT, &eof_time);
start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
WDT_HIT();
if (recvlen >= 12) {
uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH];
uint32_t eof_time;
+ uint16_t timeout;
+ bool request_answer = false;
+
+ switch (data[1]) {
+ case ISO15693_WRITEBLOCK:
+ case ISO15693_LOCKBLOCK:
+ case ISO15693_WRITE_MULTI_BLOCK:
+ case ISO15693_WRITE_AFI:
+ case ISO15693_LOCK_AFI:
+ case ISO15693_WRITE_DSFID:
+ case ISO15693_LOCK_DSFID:
+ timeout = ISO15693_READER_TIMEOUT_WRITE;
+ request_answer = data[0] & ISO15693_REQ_OPTION;
+ break;
+ default:
+ timeout = ISO15693_READER_TIMEOUT;
+ }
+
if (DEBUG) {
Dbprintf("SEND:");
Dbhexdump(datalen, data, false);
}
- recvlen = SendDataTag(data, datalen, true, speed, (recv?recvbuf:NULL), sizeof(recvbuf), 0, &eof_time);
+ recvlen = SendDataTag(data, datalen, true, speed, (recv?recvbuf:NULL), sizeof(recvbuf), 0, timeout, &eof_time);
+ if (request_answer) { // send a single EOF to get the tag response
+ recvlen = SendDataTagEOF((recv?recvbuf:NULL), sizeof(recvbuf), 0, ISO15693_READER_TIMEOUT, &eof_time);
+ }
+
// for the time being, switch field off to protect rdv4.0
// note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
//
//-----------------------------------------------------------------------------
-// Set the UID to the tag (based on Iceman work).
+// Set the UID on Magic ISO15693 tag (based on Iceman's LUA-script).
void SetTag15693Uid(uint8_t *uid) {
LED_A_ON();
uint8_t cmd[4][9] = {
- {0x02, 0x21, 0x3e, 0x00, 0x00, 0x00, 0x00},
- {0x02, 0x21, 0x3f, 0x69, 0x96, 0x00, 0x00},
- {0x02, 0x21, 0x38},
- {0x02, 0x21, 0x39}
+ {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3e, 0x00, 0x00, 0x00, 0x00},
+ {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3f, 0x69, 0x96, 0x00, 0x00},
+ {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x38},
+ {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x39}
};
uint16_t crc;
cmd[3][5] = uid[1];
cmd[3][6] = uid[0];
+ uint32_t start_time = 0;
+
for (int i = 0; i < 4; i++) {
// Add the CRC
crc = Iso15693Crc(cmd[i], 7);
cmd[i][7] = crc & 0xff;
cmd[i][8] = crc >> 8;
+ recvlen = SendDataTag(cmd[i], sizeof(cmd[i]), i==0?true:false, true, recvbuf, sizeof(recvbuf), start_time, ISO15693_READER_TIMEOUT_WRITE, &eof_time);
+ start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
if (DEBUG) {
Dbprintf("SEND:");
Dbhexdump(sizeof(cmd[i]), cmd[i], false);
- }
-
- recvlen = SendDataTag(cmd[i], sizeof(cmd[i]), true, 1, recvbuf, sizeof(recvbuf), 0, &eof_time);
-
- if (DEBUG) {
Dbprintf("RECV:");
if (recvlen > 0) {
Dbhexdump(recvlen, recvbuf, false);
DbdecodeIso15693Answer(recvlen, recvbuf);
}
}
-
- cmd_send(CMD_ACK, recvlen>ISO15693_MAX_RESPONSE_LENGTH?ISO15693_MAX_RESPONSE_LENGTH:recvlen, 0, 0, recvbuf, ISO15693_MAX_RESPONSE_LENGTH);
+ // Note: need to know if we expect an answer from one of the magic commands
+ // if (recvlen < 0) {
+ // break;
+ // }
}
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ LED_D_OFF();
+
+ cmd_send(CMD_ACK, recvlen, 0, 0, recvbuf, recvlen);
LED_A_OFF();
}