-// Manchester
-//=============================================================================
-
-static struct {
- enum {
- DEMOD_UNSYNCD,
- DEMOD_START_OF_COMMUNICATION,
- DEMOD_START_OF_COMMUNICATION2,
- DEMOD_START_OF_COMMUNICATION3,
- DEMOD_SOF_COMPLETE,
- DEMOD_MANCHESTER_D,
- DEMOD_MANCHESTER_E,
- DEMOD_END_OF_COMMUNICATION,
- DEMOD_END_OF_COMMUNICATION2,
- DEMOD_MANCHESTER_F,
- DEMOD_ERROR_WAIT
- } state;
- int bitCount;
- int posCount;
- int syncBit;
- uint16_t shiftReg;
- int buffer;
- int buffer2;
- int buffer3;
- int buff;
- int samples;
- int len;
- enum {
- SUB_NONE,
- SUB_FIRST_HALF,
- SUB_SECOND_HALF,
- SUB_BOTH
- } sub;
- uint8_t *output;
-} Demod;
-
-static RAMFUNC int ManchesterDecoding(int v)
-{
- int bit;
- int modulation;
- int error = 0;
-
- bit = Demod.buffer;
- Demod.buffer = Demod.buffer2;
- Demod.buffer2 = Demod.buffer3;
- Demod.buffer3 = v;
-
- if(Demod.buff < 3) {
- Demod.buff++;
- return false;
- }
-
- if(Demod.state==DEMOD_UNSYNCD) {
- Demod.output[Demod.len] = 0xfa;
- Demod.syncBit = 0;
- //Demod.samples = 0;
- Demod.posCount = 1; // This is the first half bit period, so after syncing handle the second part
-
- if(bit & 0x08) {
- Demod.syncBit = 0x08;
- }
-
- if(bit & 0x04) {
- if(Demod.syncBit) {
- bit <<= 4;
- }
- Demod.syncBit = 0x04;
- }
-
- if(bit & 0x02) {
- if(Demod.syncBit) {
- bit <<= 2;
- }
- Demod.syncBit = 0x02;
- }
-
- if(bit & 0x01 && Demod.syncBit) {
- Demod.syncBit = 0x01;
- }
-
- if(Demod.syncBit) {
- Demod.len = 0;
- Demod.state = DEMOD_START_OF_COMMUNICATION;
- Demod.sub = SUB_FIRST_HALF;
- Demod.bitCount = 0;
- Demod.shiftReg = 0;
- Demod.samples = 0;
- if(Demod.posCount) {
- //if(trigger) LED_A_OFF(); // Not useful in this case...
- switch(Demod.syncBit) {
- case 0x08: Demod.samples = 3; break;
- case 0x04: Demod.samples = 2; break;
- case 0x02: Demod.samples = 1; break;
- case 0x01: Demod.samples = 0; break;
- }
- // SOF must be long burst... otherwise stay unsynced!!!
- if(!(Demod.buffer & Demod.syncBit) || !(Demod.buffer2 & Demod.syncBit)) {
- Demod.state = DEMOD_UNSYNCD;
- }
- }
- else {
- // SOF must be long burst... otherwise stay unsynced!!!
- if(!(Demod.buffer2 & Demod.syncBit) || !(Demod.buffer3 & Demod.syncBit)) {
- Demod.state = DEMOD_UNSYNCD;
- error = 0x88;
- }
-
- }
- error = 0;
-
- }
- }
- else {
- modulation = bit & Demod.syncBit;
- modulation |= ((bit << 1) ^ ((Demod.buffer & 0x08) >> 3)) & Demod.syncBit;
-
- Demod.samples += 4;
-
- if(Demod.posCount==0) {
- Demod.posCount = 1;
- if(modulation) {
- Demod.sub = SUB_FIRST_HALF;
- }
- else {
- Demod.sub = SUB_NONE;
- }
- }
- else {
- Demod.posCount = 0;
- /*(modulation && (Demod.sub == SUB_FIRST_HALF)) {
- if(Demod.state!=DEMOD_ERROR_WAIT) {
- Demod.state = DEMOD_ERROR_WAIT;
- Demod.output[Demod.len] = 0xaa;
- error = 0x01;
- }
- }*/
- //else if(modulation) {
- if(modulation) {
- if(Demod.sub == SUB_FIRST_HALF) {
- Demod.sub = SUB_BOTH;
- }
- else {
- Demod.sub = SUB_SECOND_HALF;
- }
- }
- else if(Demod.sub == SUB_NONE) {
- if(Demod.state == DEMOD_SOF_COMPLETE) {
- Demod.output[Demod.len] = 0x0f;
- Demod.len++;
- Demod.state = DEMOD_UNSYNCD;
-// error = 0x0f;
- return true;
- }
- else {
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0x33;
- }
- /*if(Demod.state!=DEMOD_ERROR_WAIT) {
- Demod.state = DEMOD_ERROR_WAIT;
- Demod.output[Demod.len] = 0xaa;
- error = 0x01;
- }*/
- }
-
- switch(Demod.state) {
- case DEMOD_START_OF_COMMUNICATION:
- if(Demod.sub == SUB_BOTH) {
- //Demod.state = DEMOD_MANCHESTER_D;
- Demod.state = DEMOD_START_OF_COMMUNICATION2;
- Demod.posCount = 1;
- Demod.sub = SUB_NONE;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd2;
- }
- break;
- case DEMOD_START_OF_COMMUNICATION2:
- if(Demod.sub == SUB_SECOND_HALF) {
- Demod.state = DEMOD_START_OF_COMMUNICATION3;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd3;
- }
- break;
- case DEMOD_START_OF_COMMUNICATION3:
- if(Demod.sub == SUB_SECOND_HALF) {
-// Demod.state = DEMOD_MANCHESTER_D;
- Demod.state = DEMOD_SOF_COMPLETE;
- //Demod.output[Demod.len] = Demod.syncBit & 0xFF;
- //Demod.len++;
- }
- else {
- Demod.output[Demod.len] = 0xab;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0xd4;
- }
- break;
- case DEMOD_SOF_COMPLETE:
- case DEMOD_MANCHESTER_D:
- case DEMOD_MANCHESTER_E:
- // OPPOSITE FROM ISO14443 - 11110000 = 0 (1 in 14443)
- // 00001111 = 1 (0 in 14443)
- if(Demod.sub == SUB_SECOND_HALF) { // SUB_FIRST_HALF
- Demod.bitCount++;
- Demod.shiftReg = (Demod.shiftReg >> 1) ^ 0x100;
- Demod.state = DEMOD_MANCHESTER_D;
- }
- else if(Demod.sub == SUB_FIRST_HALF) { // SUB_SECOND_HALF
- Demod.bitCount++;
- Demod.shiftReg >>= 1;
- Demod.state = DEMOD_MANCHESTER_E;
- }
- else if(Demod.sub == SUB_BOTH) {
- Demod.state = DEMOD_MANCHESTER_F;
- }
- else {
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0x55;
- }
- break;
-
- case DEMOD_MANCHESTER_F:
- // Tag response does not need to be a complete byte!
- if(Demod.len > 0 || Demod.bitCount > 0) {
- if(Demod.bitCount > 1) { // was > 0, do not interpret last closing bit, is part of EOF
- Demod.shiftReg >>= (9 - Demod.bitCount); // right align data
- Demod.output[Demod.len] = Demod.shiftReg & 0xff;
- Demod.len++;
- }
-
- Demod.state = DEMOD_UNSYNCD;
- return true;
- }
- else {
- Demod.output[Demod.len] = 0xad;
- Demod.state = DEMOD_ERROR_WAIT;
- error = 0x03;
- }
- break;
-
- case DEMOD_ERROR_WAIT:
- Demod.state = DEMOD_UNSYNCD;
- break;
-
- default:
- Demod.output[Demod.len] = 0xdd;
- Demod.state = DEMOD_UNSYNCD;
- break;
- }
-
- /*if(Demod.bitCount>=9) {
- Demod.output[Demod.len] = Demod.shiftReg & 0xff;
- Demod.len++;
-
- Demod.parityBits <<= 1;
- Demod.parityBits ^= ((Demod.shiftReg >> 8) & 0x01);
-
- Demod.bitCount = 0;
- Demod.shiftReg = 0;
- }*/
- if(Demod.bitCount>=8) {
- Demod.shiftReg >>= 1;
- Demod.output[Demod.len] = (Demod.shiftReg & 0xff);
- Demod.len++;
- Demod.bitCount = 0;
- Demod.shiftReg = 0;
- }
-
- if(error) {
- Demod.output[Demod.len] = 0xBB;
- Demod.len++;
- Demod.output[Demod.len] = error & 0xFF;
- Demod.len++;
- Demod.output[Demod.len] = 0xBB;
- Demod.len++;
- Demod.output[Demod.len] = bit & 0xFF;
- Demod.len++;
- Demod.output[Demod.len] = Demod.buffer & 0xFF;
- Demod.len++;
- // Look harder ;-)
- Demod.output[Demod.len] = Demod.buffer2 & 0xFF;
- Demod.len++;
- Demod.output[Demod.len] = Demod.syncBit & 0xFF;
- Demod.len++;
- Demod.output[Demod.len] = 0xBB;
- Demod.len++;
- return true;
- }
-
- }
-
- } // end (state != UNSYNCED)
-
- return false;
-}
-
-//=============================================================================
-// Finally, a `sniffer' for iClass communication