+//by marshmellow
+//attempt to psk1 demod graph buffer
+int PSKDemod(const char *Cmd, bool verbose)
+{
+ int invert=0;
+ int clk=0;
+ int maxErr=100;
+ sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
+ if (clk==1){
+ invert=1;
+ clk=0;
+ }
+ if (invert != 0 && invert != 1) {
+ if (verbose) PrintAndLog("Invalid argument: %s", Cmd);
+ return -1;
+ }
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ size_t BitLen = getFromGraphBuf(BitStream);
+ if (BitLen==0) return 0;
+ int errCnt=0;
+ errCnt = pskRawDemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt > maxErr){
+ if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return -1;
+ }
+ if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return -1;
+ }
+ if (verbose) PrintAndLog("Tried PSK Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+ //prime demod buffer for output
+ setDemodBuf(BitStream,BitLen,0);
+ return errCnt;
+}
+
+// Indala 26 bit decode
+// by marshmellow
+// optional arguments - same as CmdpskNRZrawDemod (clock & invert)
+int CmdIndalaDecode(const char *Cmd)
+{
+ int ans;
+ if (strlen(Cmd)>0){
+ ans = PSKDemod(Cmd, 0);
+ } else{ //default to RF/32
+ ans = PSKDemod("32", 0);
+ }
+
+ if (ans < 0){
+ if (g_debugMode==1)
+ PrintAndLog("Error1: %d",ans);
+ return 0;
+ }
+ uint8_t invert=0;
+ ans = indala26decode(DemodBuffer,(size_t *) &DemodBufferLen, &invert);
+ if (ans < 1) {
+ if (g_debugMode==1)
+ PrintAndLog("Error2: %d",ans);
+ return -1;
+ }
+ char showbits[251]={0x00};
+ if (invert)
+ if (g_debugMode==1)
+ PrintAndLog("Had to invert bits");
+
+ //convert UID to HEX
+ uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
+ int idx;
+ uid1=0;
+ uid2=0;
+ PrintAndLog("BitLen: %d",DemodBufferLen);
+ if (DemodBufferLen==64){
+ for( idx=0; idx<64; idx++) {
+ uid1=(uid1<<1)|(uid2>>31);
+ if (DemodBuffer[idx] == 0) {
+ uid2=(uid2<<1)|0;
+ showbits[idx]='0';
+ } else {
+ uid2=(uid2<<1)|1;
+ showbits[idx]='1';
+ }
+ }
+ showbits[idx]='\0';
+ PrintAndLog("Indala UID=%s (%x%08x)", showbits, uid1, uid2);
+ }
+ else {
+ uid3=0;
+ uid4=0;
+ uid5=0;
+ uid6=0;
+ uid7=0;
+ for( idx=0; idx<DemodBufferLen; idx++) {
+ uid1=(uid1<<1)|(uid2>>31);
+ uid2=(uid2<<1)|(uid3>>31);
+ uid3=(uid3<<1)|(uid4>>31);
+ uid4=(uid4<<1)|(uid5>>31);
+ uid5=(uid5<<1)|(uid6>>31);
+ uid6=(uid6<<1)|(uid7>>31);
+ if (DemodBuffer[idx] == 0) {
+ uid7=(uid7<<1)|0;
+ showbits[idx]='0';
+ }
+ else {
+ uid7=(uid7<<1)|1;
+ showbits[idx]='1';
+ }
+ }
+ showbits[idx]='\0';
+ PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)", showbits, uid1, uid2, uid3, uid4, uid5, uid6, uid7);
+ }
+ if (g_debugMode){
+ PrintAndLog("DEBUG: printing demodbuffer:");
+ printDemodBuff();
+ }
+ return 1;
+}
+
+// by marshmellow
+// takes 3 arguments - clock, invert, maxErr as integers
+// attempts to demodulate nrz only
+// prints binary found and saves in demodbuffer for further commands
+
+int NRZrawDemod(const char *Cmd, bool verbose)
+{
+ int invert=0;
+ int clk=0;
+ int maxErr=100;
+ sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
+ if (clk==1){
+ invert=1;
+ clk=0;
+ }
+ if (invert != 0 && invert != 1) {
+ PrintAndLog("Invalid argument: %s", Cmd);
+ return 0;
+ }
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ size_t BitLen = getFromGraphBuf(BitStream);
+ if (BitLen==0) return 0;
+ int errCnt=0;
+ errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, maxErr);
+ if (errCnt > maxErr){
+ if (g_debugMode==1 && verbose) PrintAndLog("Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return 0;
+ }
+ if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ if (g_debugMode==1 && verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return 0;
+ }
+ PrintAndLog("Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+ //prime demod buffer for output
+ setDemodBuf(BitStream,BitLen,0);
+
+ if (errCnt>0 && verbose){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ if (verbose) {
+ PrintAndLog("NRZ demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ return 1;
+}
+
+int CmdNRZrawDemod(const char *Cmd)
+{
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
+ PrintAndLog("Usage: data rawdemod nr [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 nrzrawdemod = demod a nrz/direct tag from GraphBuffer");
+ PrintAndLog(" : data nrzrawdemod 32 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32");
+ PrintAndLog(" : data nrzrawdemod 32 1 = demod a nrz/direct tag from GraphBuffer using a clock of RF/32 and inverting data");
+ PrintAndLog(" : data nrzrawdemod 1 = demod a nrz/direct tag from GraphBuffer while inverting data");
+ PrintAndLog(" : data nrzrawdemod 64 1 0 = demod a nrz/direct tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
+ return 0;
+ }
+ return NRZrawDemod(Cmd, TRUE);
+}
+
+// by marshmellow
+// takes 3 arguments - clock, invert, maxErr as integers
+// attempts to demodulate psk only
+// prints binary found and saves in demodbuffer for further commands
+int CmdPSK1rawDemod(const char *Cmd)
+{
+ int errCnt;
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
+ PrintAndLog("Usage: data rawdemod p1 [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 psk1rawdemod = demod a psk1 tag from GraphBuffer");
+ PrintAndLog(" : data psk1rawdemod 32 = demod a psk1 tag from GraphBuffer using a clock of RF/32");
+ PrintAndLog(" : data psk1rawdemod 32 1 = demod a psk1 tag from GraphBuffer using a clock of RF/32 and inverting data");
+ PrintAndLog(" : data psk1rawdemod 1 = demod a psk1 tag from GraphBuffer while inverting data");
+ PrintAndLog(" : data psk1rawdemod 64 1 0 = demod a psk1 tag from GraphBuffer using a clock of RF/64, inverting data and allowing 0 demod errors");
+ return 0;
+ }
+ errCnt = PSKDemod(Cmd, TRUE);
+ //output
+ if (errCnt<0){
+ if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
+ return 0;
+ }
+ if (errCnt>0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ PrintAndLog("PSK demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ return 1;
+}
+
+// by marshmellow
+// takes same args as cmdpsk1rawdemod
+int CmdPSK2rawDemod(const char *Cmd)
+{
+ int errCnt=0;
+ char cmdp = param_getchar(Cmd, 0);
+ if (strlen(Cmd) > 10 || cmdp == 'h' || cmdp == 'H') {
+ PrintAndLog("Usage: data rawdemod p2 [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 psk2rawdemod = demod a psk2 tag from GraphBuffer, autodetect clock");
+ PrintAndLog(" : data psk2rawdemod 32 = demod a psk2 tag from GraphBuffer using a clock of RF/32");
+ PrintAndLog(" : data psk2rawdemod 32 1 = demod a psk2 tag from GraphBuffer using a clock of RF/32 and inverting output");
+ PrintAndLog(" : data psk2rawdemod 1 = demod a psk2 tag from GraphBuffer, autodetect clock and invert output");
+ PrintAndLog(" : data psk2rawdemod 64 1 0 = demod a psk2 tag from GraphBuffer using a clock of RF/64, inverting output and allowing 0 demod errors");
+ return 0;
+ }
+ errCnt=PSKDemod(Cmd, 1);
+ if (errCnt<0){
+ if (g_debugMode) PrintAndLog("Error demoding: %d",errCnt);
+ return 0;
+ }
+ psk1TOpsk2(DemodBuffer, DemodBufferLen);
+ if (errCnt>0){
+ if (g_debugMode){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ PrintAndLog("PSK2 demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ }else{
+ PrintAndLog("PSK2 demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+ }
+ return 1;
+}
+
+// by marshmellow - combines all raw demod functions into one menu command
+int CmdRawDemod(const char *Cmd)
+{
+ char cmdp = Cmd[0]; //param_getchar(Cmd, 0);
+
+ if (strlen(Cmd) > 14 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd)<2) {
+ PrintAndLog("Usage: data rawdemod [modulation] <help>|<options>");
+ PrintAndLog(" [modulation] as 2 char, 'am' for ask/manchester, 'ar' for ask/raw, 'fs' for fsk, 'nr' for nrz/direct, 'p1' for psk1, 'p2' for psk2");
+ PrintAndLog(" <help> as 'h', prints the help for the specific modulation");
+ PrintAndLog(" <options> see specific modulation help for optional parameters");
+ PrintAndLog("");
+ PrintAndLog(" sample: data rawdemod fs h = print help for ask/raw demod");
+ PrintAndLog(" : data rawdemod fs = demod GraphBuffer using: fsk - autodetect");
+ PrintAndLog(" : data rawdemod am = demod GraphBuffer using: ask/manchester - autodetect");
+ PrintAndLog(" : data rawdemod ar = demod GraphBuffer using: ask/raw - autodetect");
+ PrintAndLog(" : data rawdemod nr = demod GraphBuffer using: nrz/direct - autodetect");
+ PrintAndLog(" : data rawdemod p1 = demod GraphBuffer using: psk1 - autodetect");
+ PrintAndLog(" : data rawdemod p2 = demod GraphBuffer using: psk2 - autodetect");
+ return 0;
+ }
+ char cmdp2 = Cmd[1];
+ int ans = 0;
+ if (cmdp == 'f' && cmdp2 == 's'){
+ ans = CmdFSKrawdemod(Cmd+3);
+ } else if(cmdp == 'a' && cmdp2 == 'm'){
+ ans = Cmdaskmandemod(Cmd+3);
+ } else if(cmdp == 'a' && cmdp2 == 'r'){
+ ans = Cmdaskrawdemod(Cmd+3);
+ } else if(cmdp == 'n' && cmdp2 == 'r'){
+ ans = CmdNRZrawDemod(Cmd+3);
+ } else if(cmdp == 'p' && cmdp2 == '1'){
+ ans = CmdPSK1rawDemod(Cmd+3);
+ } else if(cmdp == 'p' && cmdp2 == '2'){
+ ans = CmdPSK2rawDemod(Cmd+3);
+ } else {
+ PrintAndLog("unknown modulation entered - see help ('h') for parameter structure");
+ }
+ return ans;
+}
+