Beispiel #1
0
static int l_iso15693_crc(lua_State *L)
{
    //    uint16_t Iso15693Crc(uint8_t *v, int n);
    size_t size;
    const char *v = luaL_checklstring(L, 1, &size);
    uint16_t retval = Iso15693Crc((uint8_t *) v, size);
    lua_pushinteger(L, (int) retval);
    return 1;
}
Beispiel #2
0
// Mode 3
int CmdHF15Demod(const char *Cmd)
{
	// The sampling rate is 106.353 ksps/s, for T = 18.8 us
	
	int i, j;
	int max = 0, maxPos = 0;

	int skip = 4;

	if (GraphTraceLen < 1000) return 0;

	// First, correlate for SOF
	for (i = 0; i < 100; i++) {
		int corr = 0;
		for (j = 0; j < arraylen(FrameSOF); j += skip) {
			corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
		}
		if (corr > max) {
			max = corr;
			maxPos = i;
		}
	}
	PrintAndLog("SOF at %d, correlation %d", maxPos,
		max / (arraylen(FrameSOF) / skip));
	
	i = maxPos + arraylen(FrameSOF) / skip;
	int k = 0;
	uint8_t outBuf[20];
	memset(outBuf, 0, sizeof(outBuf));
	uint8_t mask = 0x01;
	for (;;) {
		int corr0 = 0, corr1 = 0, corrEOF = 0;
		for (j = 0; j < arraylen(Logic0); j += skip) {
			corr0 += Logic0[j] * GraphBuffer[i + (j / skip)];
		}
		for (j = 0; j < arraylen(Logic1); j += skip) {
			corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
		}
		for (j = 0; j < arraylen(FrameEOF); j += skip) {
			corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
		}
		// Even things out by the length of the target waveform.
		corr0 *= 4;
		corr1 *= 4;
		
		if (corrEOF > corr1 && corrEOF > corr0) {
			PrintAndLog("EOF at %d", i);
			break;
		} else if (corr1 > corr0) {
			i += arraylen(Logic1) / skip;
			outBuf[k] |= mask;
		} else {
			i += arraylen(Logic0) / skip;
		}
		mask <<= 1;
		if (mask == 0) {
			k++;
			mask = 0x01;
		}
		if ((i + (int)arraylen(FrameEOF)) >= GraphTraceLen) {
			PrintAndLog("ran off end!");
			break;
		}
	}
	if (mask != 0x01) {
		PrintAndLog("error, uneven octet! (discard extra bits!)");
		PrintAndLog("   mask=%02x", mask);
	}
	PrintAndLog("%d octets", k);
	
	for (i = 0; i < k; i++) {
		PrintAndLog("# %2d: %02x ", i, outBuf[i]);
	}
	PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
	return 0;
}