#include "magstripe.h" #include #include "board.h" #include "debug.h" #include "timer.h" #include #include "util.h" #include "config.h" #include "avr_task.h" #include #include "string.h" #include "avr_cunit.h" #include "uart.h" //#include "kbd.h" //#undef _DBG //#define _DBG struct avrDataStruct *avrDataPtr; struct trackVar { WORD cnt; DWORD tmp; WORD prev; BYTE last_data; WORD allBitCnt; BYTE bitCnt8; WORD byteCntRaw; WORD dataCnt; }; struct trackVar t2Var; struct trackVar t1Var; #define BCD_BIT_PER_CHAR 4 #define BCD_BUFFER_SIZE 50 #define BAD_CHAR 0xff #define ALPHA_BIT_PER_CHAR 6 #define ALPHA_BUFFER_SIZE 110 BYTE buffer[GLOBAL_BUFFER_SIZE]; BYTE bcdRaw[BCD_BUFFER_SIZE]; //BYTE bcdDecoded[BCD_BUFFER_SIZE]; BYTE alphaRaw[ALPHA_BUFFER_SIZE]; //BYTE alphaDecoded[ALPHA_BUFFER_SIZE]; //BCD SS ES RLC ALPHA SS ES RLC BYTE dataDecoded[ALPHA_BUFFER_SIZE+BCD_BUFFER_SIZE]; static inline void f2f(WORD tc, struct trackVar *tVar, BYTE *rawDest, WORD rawCnt); BOOL decode(BYTE *raw_src, WORD bitCnt, BYTE *dest, WORD *bytesCnt, BYTE bitPerChar, BYTE *parityArr, BYTE startSentinel, BYTE endSentinel); static BYTE getBit(BYTE *bytePtr, WORD bitIndx); static void bitsRever(BYTE *src, WORD cnt); static BYTE parity4[16]= {1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1}; static BYTE parity6[64]= {1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1}; //static BYTE bitcount4[16]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; void track1_init()//ALPHA data INT1 { cbi(TRACK1_DDR,TRACK1_PIN); //set track 1 pin as input EIMSK |= _BV(INT1); EICRA |= _BV(ISC10); // Any logical changes on INT1 } void track2_init()//BCD data INT0 { cbi(TRACK2_DDR,TRACK2_PIN); //set track 2 pin as input EIMSK |= _BV(INT0); EICRA |= _BV(ISC00); // Any logical changes on INT1 } ISR(TIMER0_OVF_vect)//approximately every 35 ms { static BYTE cnt=0; if(++cnt < 17) return; cnt=0; BYTE t1=0,t2=0; //track 2 BCD if(t2Var.allBitCnt>0) { _DBG("DECODING BCD\n"); if(!decode(bcdRaw, t2Var.allBitCnt, dataDecoded+0,&t2Var.dataCnt,BCD_BIT_PER_CHAR,parity4,BCD_START_SENTINEL,BCD_END_SENTINEL)) { bitsRever(bcdRaw,t2Var.allBitCnt); if(!decode(bcdRaw, t2Var.allBitCnt, dataDecoded+0,&t2Var.dataCnt,BCD_BIT_PER_CHAR,parity4,BCD_START_SENTINEL,BCD_END_SENTINEL)) _DBG("BCD Error\n"); else t2=1; } else t2=1; } //track 1 ALPHA if(t2) if(t1Var.allBitCnt>0) { _DBG("DECODING ALPHA\n"); //offset BCD data if(!decode(alphaRaw,t1Var.allBitCnt,dataDecoded+t2Var.dataCnt,&t1Var.dataCnt,ALPHA_BIT_PER_CHAR,parity6,ALPHA_START_SENTINEL,ALPHA_END_SENTINEL)) { bitsRever(alphaRaw,t1Var.allBitCnt); //offset BCD data if(!decode(alphaRaw,t1Var.allBitCnt,dataDecoded+t2Var.dataCnt,&t1Var.dataCnt,ALPHA_BIT_PER_CHAR,parity6,ALPHA_START_SENTINEL,ALPHA_END_SENTINEL)) _DBG("ALPHA Error\n"); else t1=1; } else t1=1; } if(t1&&t2) { avrDataPtr->eventCode = AVR_EVENT_READ_MAGSTRIPE; avrDataPtr->datasize = t1Var.dataCnt + t2Var.dataCnt; avrDataPtr->dataptr = dataDecoded; _DBG("Size %u\n",avrDataPtr->datasize); } if(t2)//ALPHA { _DBG("ALPHA code\n"); for(int i=0;i> (7-(bitIndx%8))) & 1; } void setBit(BYTE *bytePtr, WORD bitIndx, BYTE bit) { if(bit) sbi(bytePtr[bitIndx/8],(7-(bitIndx%8))); else cbi(bytePtr[bitIndx/8],(7-(bitIndx%8))); } static void bitsRever(BYTE *src, WORD cnt) { BYTE b=0; for(WORD i=0;i<(cnt/2);i++){ b = getBit(src,i); setBit(src,i,getBit(src,cnt-1-i)); setBit(src,cnt-1-i,b); } } static BYTE getByte(BYTE *bytePtr, WORD bitIndx, BYTE bitPerChar, BYTE *parityArr) { BYTE result=0; for(int i=0;i>3 && !raw_src[offset++]); --offset; offset *= 8; //bits offset while(offsetbyteCntRaw > rawCnt) return; ++tVar->cnt; if(tVar->cnt<=LEADING_ZEROS) { if(tVar->cnt==1) return; tVar->tmp += tc; return; } if(tVar->cnt == LEADING_ZEROS+1) { tVar->prev = (tVar->tmp+tc) / LEADING_ZEROS; return; } else { if(tVar->bitCnt8 > 7){ tVar->bitCnt8 = 0; tVar->byteCntRaw++; } if(tVar->last_data){ tVar->last_data=0; return; } // if(tc > ( (tVar->prev>>1)+(tVar->prev>>2)+(tVar->prev>>6)+10))*/ if(tc > ((tVar->prev>>1)+ (tVar->prev>>2) + (tVar->prev>>5) ))// > 145 { tVar->last_data = 0; tVar->prev = tc; tVar->allBitCnt++; tVar->bitCnt8++; //_DBG("0"); return; } else { tVar->last_data=1; tVar->allBitCnt++; rawDest[tVar->byteCntRaw] |= 1<<(7-tVar->bitCnt8); tVar->bitCnt8++; //_DBG("1"); return; } } }