+int ASKmanDemod(const char *Cmd, bool verbose, bool emSearch)
+{
+ int invert=0;
+ int clk=0;
+ int maxErr=100;
+
+ 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;
+ }
+ if (clk==1){
+ invert=1;
+ clk=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;
+ }
+ if (verbose) PrintAndLog("\nUsing Clock: %d - Invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ //output
+ if (errCnt>0){
+ if (verbose) PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ if (verbose) PrintAndLog("ASK/Manchester decoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ setDemodBuf(BitStream,BitLen,0);
+ if (verbose) printDemodBuff();
+ uint64_t lo =0;
+ size_t idx=0;
+ if (emSearch){
+ 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();
+ }
+ if (verbose) PrintAndLog("EM410x pattern found: ");
+ if (verbose) printEM410x(lo);
+ return 1;
+ }
+ }
+ return 1;
+}
+
+//by marshmellow
+//takes 3 arguments - clock, invert, maxErr as integers
+//attempts to demodulate ask while decoding manchester
+//prints binary found and saves in graphbuffer for further commands
+int Cmdaskmandemod(const char *Cmd)
+{
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
+ PrintAndLog("Usage: data rawdemod am [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 rawdemod am = demod an ask/manchester tag from GraphBuffer");
+ PrintAndLog(" : data rawdemod am 32 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32");
+ PrintAndLog(" : data rawdemod am 32 1 = demod an ask/manchester tag from GraphBuffer using a clock of RF/32 and inverting data");
+ PrintAndLog(" : data rawdemod am 1 = demod an ask/manchester tag from GraphBuffer while inverting data");
+ PrintAndLog(" : data rawdemod am 64 1 0 = demod an ask/manchester tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
+ return 0;
+ }
+ return ASKmanDemod(Cmd, TRUE, TRUE);
+}
+