+//by marshmellow
+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;
+
+ // ensure equally divided by 16
+ bitLen &= 0xfff0;
+
+
+ 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;
+}
+//by marshmellow
+//print EM410x ID in multiple formats
+void printEM410x(uint64_t id)
+{
+ if (id !=0){
+ uint64_t iii=1;
+ uint64_t id2lo=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);
+ 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;
+}
+
+//by marshmellow
+//take binary from demod buffer and see if we can find an EM410x ID
+int CmdEm410xDecode(const char *Cmd)
+{
+ uint64_t id=0;
+ size_t size = DemodBufferLen, idx=0;
+ id = Em410xDecode(DemodBuffer, &size, &idx);
+ if (id>0){
+ setDemodBuf(DemodBuffer, size, idx);
+ if (g_debugMode){
+ PrintAndLog("DEBUG: Printing demod buffer:");
+ printDemodBuff();
+ }
+ printEM410x(id);
+ 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;
+ }
+
+ size_t BitLen = getFromGraphBuf(BitStream);
+ if (g_debugMode==1) PrintAndLog("DEBUG: Bitlen from grphbuff: %d",BitLen);
+ int errCnt=0;
+ errCnt = askmandemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt<0||BitLen<16){ //if fatal error (or -1)
+ if (g_debugMode==1) PrintAndLog("no data found %d, errors:%d, bitlen:%d, clock:%d",errCnt,invert,BitLen,clk);
+ return 0;
+ }
+ PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ //output
+ 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
+ setDemodBuf(BitStream,BitLen,0);
+ printDemodBuff();
+ uint64_t lo =0;
+ size_t idx=0;
+ lo = Em410xDecode(BitStream, &BitLen, &idx);
+ if (lo>0){
+ //set GraphBuffer for clone or sim command
+ setDemodBuf(BitStream, BitLen, idx);
+ if (g_debugMode){
+ PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen);
+ printDemodBuff();
+ }
+ PrintAndLog("EM410x pattern found: ");
+ printEM410x(lo);
+ 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;
+ size_t size=0;
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ int high=0,low=0;
+ for (;i<DemodBufferLen;++i){
+ if (DemodBuffer[i]>high) high=DemodBuffer[i];
+ else if(DemodBuffer[i]<low) low=DemodBuffer[i];
+ BitStream[i]=DemodBuffer[i];
+ }
+ if (high>1 || low <0 ){
+ PrintAndLog("Error: please raw demod the wave first then mancheseter raw decode");
+ return 0;
+ }
+ size=i;
+ errCnt=manrawdecode(BitStream, &size);
+ if (errCnt>=20){
+ PrintAndLog("Too many errors: %d",errCnt);
+ return 0;
+ }
+ PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
+ printBitStream(BitStream, size);
+ if (errCnt==0){
+ uint64_t id = 0;
+ size_t idx=0;
+ id = Em410xDecode(BitStream, &size, &idx);
+ if (id>0){
+ //need to adjust to set bitstream back to manchester encoded data
+ //setDemodBuf(BitStream, size, idx);
+
+ printEM410x(id);
+ }
+ }
+ return 1;
+}
+
+//by marshmellow
+//biphase decode
+//take 01 or 10 = 0 and 11 or 00 = 1
+//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
+// and "invert" default = 0 if 1 it will invert output
+// 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;
+ size_t size=0;
+ int offset=0;
+ int invert=0;
+ int high=0, low=0;
+ sscanf(Cmd, "%i %i", &offset, &invert);
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ //get graphbuffer & high and low
+ for (;i<DemodBufferLen;++i){
+ if(DemodBuffer[i]>high)high=DemodBuffer[i];
+ else if(DemodBuffer[i]<low)low=DemodBuffer[i];
+ BitStream[i]=DemodBuffer[i];
+ }
+ if (high>1 || low <0){
+ PrintAndLog("Error: please raw demod the wave first then decode");
+ return 0;
+ }
+ size=i;
+ errCnt=BiphaseRawDecode(BitStream, &size, offset, invert);
+ 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, size);
+ 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)
+{
+ 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;
+ }
+ size_t BitLen = getFromGraphBuf(BitStream);
+ int errCnt=0;
+ errCnt = askrawdemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt==-1||BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ PrintAndLog("no data found");
+ if (g_debugMode==1) PrintAndLog("errCnt: %d, BitLen: %d, clk: %d, invert: %d", errCnt, BitLen, clk, invert);
+ return 0;
+ }
+ PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ //move BitStream back to DemodBuffer
+ setDemodBuf(BitStream,BitLen,0);
+
+ //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;
+}
+