+int CmdEm410xDecode(const char *Cmd)
+{
+ uint64_t id = 0;
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ uint32_t len = GetFromGraphBuf(bits);
+ id = Em410xDecode(bits, len);
+ 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;
+
+ sscanf(Cmd, "%i %i", &clk, &invert);
+
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ uint32_t len = GetFromGraphBuf(bits);
+
+ int errCnt = askmandemod(bits, &len, &clk, &invert);
+
+ if (errCnt < 0) return 0;
+ if (len < 16) return 0;
+
+ PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,len);
+
+ if (errCnt > 0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+
+ PrintAndLog("ASK/Manchester decoded bitstream:");
+
+ printBitStream(bits, len);
+
+ uint64_t tagid = Em410xDecode(bits, len);
+
+ if (tagid > 0){
+ SetGraphBuf(bits, len);
+ printEM410x(tagid);
+ 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 bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ int high = 0, low = 0;
+
+ for (; i < GraphTraceLen; ++i){
+ if (GraphBuffer[i] > high) high = GraphBuffer[i];
+ else if (GraphBuffer[i] < low) low = GraphBuffer[i];
+ bits[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(bits, &bitnum);
+
+ if (errCnt>=20){
+ PrintAndLog("Too many errors: %d",errCnt);
+ return 0;
+ }
+
+ PrintAndLog("Manchester Decoded - # errors:%d - data:",errCnt);
+ printBitStream(bits,bitnum);
+
+ if (errCnt==0){
+ //put back in graphbuffer
+ SetGraphBuf(bits, bitnum);
+
+ uint64_t id = Em410xDecode(bits,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 bits[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];
+ bits[i] = GraphBuffer[i];
+ }
+ if (high > 1 || low < 0){
+ PrintAndLog("Error: please raw demod the wave first then decode");
+ return 0;
+ }
+ bitnum = i;
+ errCnt = BiphaseRawDecode(bits, &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(bits, 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)
+{
+ int invert = 0;
+ int clk = 0;
+
+ sscanf(Cmd, "%i %i", &clk, &invert);
+
+ if (invert != 0 && invert != 1 ) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+
+ if ( clock < 0 ) {
+ PrintAndLog("Wrong clock argument");
+ return 0;
+ }
+
+ uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0x00};
+ int len = GetFromGraphBuf(bits);
+ int errCnt = 0;
+
+ errCnt = askrawdemod(bits, &len, &clk, &invert);
+
+ //throw away static - allow 1 and -1 (in case of threshold command first)
+ if (errCnt == -1) {
+ PrintAndLog("no data found");
+ return 0;
+ }
+
+ if (len < 16) return 0;
+
+ PrintAndLog("Using Clock: %d - invert: %d - Bits Found: %d",clk,invert,len);
+
+ //move BitStream back to GraphBuffer
+ SetGraphBuf(bits, len);
+
+ 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(bits,len);
+ return 1;