+void printBitStream(uint8_t BitStream[], uint32_t bitLen){
+ uint32_t i = 0;
+ if (bitLen<16) {
+ PrintAndLog("Too few bits found: %d",bitLen);
+ return;
+ }
+ if (bitLen>512) bitLen=512;
+ for (i = 0; i <= (bitLen-16); i+=16) {
+ PrintAndLog("%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i%i",
+ BitStream[i],
+ BitStream[i+1],
+ BitStream[i+2],
+ BitStream[i+3],
+ BitStream[i+4],
+ BitStream[i+5],
+ BitStream[i+6],
+ BitStream[i+7],
+ BitStream[i+8],
+ BitStream[i+9],
+ BitStream[i+10],
+ BitStream[i+11],
+ BitStream[i+12],
+ BitStream[i+13],
+ BitStream[i+14],
+ BitStream[i+15]);
+ }
+ return;
+}
+void printEM410x(uint64_t id)
+{
+ if (id !=0){
+ uint64_t iii=1;
+ uint64_t id2lo=0; //id2hi=0,
+ uint32_t ii=0;
+ uint32_t i=0;
+ for (ii=5; ii>0;ii--){
+ for (i=0;i<8;i++){
+ id2lo=(id2lo<<1LL)|((id & (iii<<(i+((ii-1)*8))))>>(i+((ii-1)*8)));
+ }
+ }
+ //output em id
+ PrintAndLog("EM TAG ID : %010llx", id);
+ PrintAndLog("Unique TAG ID: %010llx", id2lo); //id2hi,
+ PrintAndLog("DEZ 8 : %08lld",id & 0xFFFFFF);
+ PrintAndLog("DEZ 10 : %010lld",id & 0xFFFFFF);
+ PrintAndLog("DEZ 5.5 : %05lld.%05lld",(id>>16LL) & 0xFFFF,(id & 0xFFFF));
+ PrintAndLog("DEZ 3.5A : %03lld.%05lld",(id>>32ll),(id & 0xFFFF));
+ PrintAndLog("DEZ 14/IK2 : %014lld",id);
+ PrintAndLog("DEZ 15/IK3 : %015lld",id2lo);
+ PrintAndLog("Other : %05lld_%03lld_%08lld",(id&0xFFFF),((id>>16LL) & 0xFF),(id & 0xFFFFFF));
+ }
+ return;
+}
+
+int CmdEm410xDecode(const char *Cmd)
+{
+ uint64_t id=0;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ uint32_t i=0;
+ i=getFromGraphBuf(BitStream);
+ id = Em410xDecode(BitStream,i);
+ printEM410x(id);
+ if (id>0) return 1;
+ return 0;
+}
+
+//by marshmellow
+//takes 2 arguments - clock and invert both as integers
+//attempts to demodulate ask while decoding manchester
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskmandemod(const char *Cmd)
+{
+ int invert=0;
+ int clk=0;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ sscanf(Cmd, "%i %i", &clk, &invert);
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+ uint32_t BitLen = getFromGraphBuf(BitStream);
+
+ int errCnt=0;
+ errCnt = askmandemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt<0){ //if fatal error (or -1)
+ // PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
+ return 0;
+ }
+ if (BitLen<16) return 0;
+ PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ if (errCnt>0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ PrintAndLog("ASK/Manchester decoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printBitStream(BitStream,BitLen);
+ uint64_t lo =0;
+ lo = Em410xDecode(BitStream,BitLen);
+ if (lo>0){
+ //set GraphBuffer for clone or sim command
+ setGraphBuf(BitStream,BitLen);
+ PrintAndLog("EM410x pattern found: ");
+ printEM410x(lo);
+ }
+ //if (BitLen>16) return 1;
+ return 0;
+}
+
+//by marshmellow
+//manchester decode
+//stricktly take 10 and 01 and convert to 0 and 1
+int Cmdmandecoderaw(const char *Cmd)
+{
+ int i =0;
+ int errCnt=0;
+ int bitnum=0;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ int high = 0, low = 0;
+ for (;i<GraphTraceLen;++i){
+ if (GraphBuffer[i]>high) high=GraphBuffer[i];
+ else if(GraphBuffer[i]<low) low=GraphBuffer[i];
+ BitStream[i]=GraphBuffer[i];
+ }
+ if (high>1 || low <0 ){
+ PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
+ return 0;
+ }
+ bitnum=i;
+ errCnt=manrawdecode(BitStream,&bitnum);
+ if (errCnt>=20){
+ PrintAndLog("Too many errors: %d",errCnt);
+ return 0;
+ }
+ PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
+ printBitStream(BitStream,bitnum);
+ if (errCnt==0){
+ //put back in graphbuffer
+ ClearGraph(0);
+ for (i=0; i < bitnum; ++i){
+ GraphBuffer[i]=BitStream[i];
+ }
+ GraphTraceLen=bitnum;
+ RepaintGraphWindow();
+ uint64_t id = 0;
+ id = Em410xDecode(BitStream,i);
+ printEM410x(id);
+ }
+ return 1;
+}
+
+//by marshmellow
+//biphase decode
+//take 01 or 10 = 0 and 11 or 00 = 1
+//takes 1 argument "offset" default = 0 if 1 it will shift the decode by one bit
+// since it is not like manchester and doesn't have an incorrect bit pattern we
+// cannot determine if our decode is correct or if it should be shifted by one bit
+// the argument offset allows us to manually shift if the output is incorrect
+// (better would be to demod and decode at the same time so we can distinguish large
+// width waves vs small width waves to help the decode positioning) or askbiphdemod
+int CmdBiphaseDecodeRaw(const char *Cmd)
+{
+ int i = 0;
+ int errCnt=0;
+ int bitnum=0;
+ int offset=0;
+ int high=0, low=0;
+ sscanf(Cmd, "%i", &offset);
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ //get graphbuffer & high and low
+ for (;i<GraphTraceLen;++i){
+ if(GraphBuffer[i]>high)high=GraphBuffer[i];
+ else if(GraphBuffer[i]<low)low=GraphBuffer[i];
+ BitStream[i]=GraphBuffer[i];
+ }
+ if (high>1 || low <0){
+ PrintAndLog("Error: please raw demod the wave first then decode");
+ return 0;
+}
+ bitnum=i;
+ errCnt=BiphaseRawDecode(BitStream,&bitnum, offset);
+ if (errCnt>=20){
+ PrintAndLog("Too many errors attempting to decode: %d",errCnt);
+ return 0;
+ }
+ PrintAndLog("Biphase Decoded using offset: %d - # errors:%d - data:",offset,errCnt);
+ printBitStream(BitStream,bitnum);
+ PrintAndLog("\nif bitstream does not look right try offset=1");
+ return 1;
+}
+
+
+//by marshmellow
+//takes 2 arguments - clock and invert both as integers
+//attempts to demodulate ask only
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskrawdemod(const char *Cmd)
+{
+ uint32_t i;
+ int invert=0;
+ int clk=0;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ sscanf(Cmd, "%i %i", &clk, &invert);
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+ int BitLen = getFromGraphBuf(BitStream);
+ int errCnt=0;
+ errCnt = askrawdemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt==-1){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ PrintAndLog("no data found");
+ return 0;
+ }
+ if (BitLen<16) return 0;
+ PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+ //PrintAndLog("Data start pos:%d, lastBit:%d, stop pos:%d, numBits:%d",iii,lastBit,i,bitnum);
+ //move BitStream back to GraphBuffer
+
+ ClearGraph(0);
+ for (i=0; i < BitLen; ++i){
+ GraphBuffer[i]=BitStream[i];
+ }
+ GraphTraceLen=BitLen;
+ RepaintGraphWindow();
+
+ //output
+ if (errCnt>0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ PrintAndLog("ASK demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printBitStream(BitStream,BitLen);
+
+ return 1;
+}
+