+//takes 3 arguments - clock, invert and maxErr as integers
+//attempts to demodulate ask while decoding manchester
+//prints binary found and saves in graphbuffer for further commands
+int CmdAskEM410xDemod(const char *Cmd)
+{
+ int invert=0;
+ int clk=0;
+ int maxErr=100;
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') {
+ PrintAndLog("Usage: data askem410xdemod [clock] <0|1> [maxError]");
+ PrintAndLog(" [set clock as integer] optional, if not set, autodetect.");
+ PrintAndLog(" <invert>, 1 for invert output");
+ PrintAndLog(" [set maximum allowed errors], default = 100.");
+ PrintAndLog("");
+ PrintAndLog(" sample: data askem410xdemod = demod an EM410x Tag ID from GraphBuffer");
+ PrintAndLog(" : data askem410xdemod 32 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32");
+ PrintAndLog(" : data askem410xdemod 32 1 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/32 and inverting data");
+ PrintAndLog(" : data askem410xdemod 1 = demod an EM410x Tag ID from GraphBuffer while inverting data");
+ PrintAndLog(" : data askem410xdemod 64 1 0 = demod an EM410x Tag ID from GraphBuffer using a clock of RF/64 and inverting data and allowing 0 demod errors");
+
+ return 0;
+ }
+
+
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
+ 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);
+ if (BitLen==0) return 0;
+ int errCnt=0;
+ errCnt = askmandemod(BitStream, &BitLen, &clk, &invert, maxErr);
+ 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
+//takes 3 arguments - clock, invert, maxErr as integers