+int CmdDetectNRZpskClockRate(const char *Cmd)
+{
+ GetNRZpskClock("",0,0);
+ return 0;
+}
+
+int PSKnrzDemod(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 -1;
+ }
+ uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
+ int BitLen = getFromGraphBuf(BitStream);
+ int errCnt=0;
+ errCnt = pskNRZrawDemod(BitStream, &BitLen,&clk,&invert);
+ if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
+ //PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return -1;
+ }
+ PrintAndLog("Tried PSK/NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
+
+ //prime demod buffer for output
+ setDemodBuf(BitStream,BitLen);
+ return errCnt;
+}
+// Indala 26 bit decode
+// by marshmellow
+// optional arguments - same as CmdpskNRZrawDemod (clock & invert)
+int CmdIndalaDecode(const char *Cmd)
+{
+
+ int ans=PSKnrzDemod(Cmd);
+ if (ans < 0){
+ PrintAndLog("Error1: %d",ans);
+ return 0;
+ }
+ uint8_t invert=0;
+ ans = indala26decode(DemodBuffer, &DemodBufferLen, &invert);
+ if (ans < 1) {
+ PrintAndLog("Error2: %d",ans);
+ return -1;
+ }
+ char showbits[251];
+ if(invert==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);
+ }
+ return 1;
+}
+
+/*
+//by marshmellow (attempt to get rid of high immediately after a low)
+void pskCleanWave2(uint8_t *bitStream, int bitLen)
+{
+ int i;
+ int low=128;
+ int gap = 4;
+ // int loopMax = 2048;
+ int newLow=0;
+
+ for (i=0; i<bitLen; ++i)
+ if (bitStream[i]<low) low=bitStream[i];
+
+ low = (int)(((low-128)*.80)+128);
+ PrintAndLog("low: %d",low);
+ for (i=0; i<bitLen; ++i){
+ if (newLow==1){
+ bitStream[i]=low+5;
+ gap--;
+ if (gap==0){
+ newLow=0;
+ gap=4;
+ }
+ }
+ if (bitStream[i]<=low) newLow=1;
+ }
+ return;
+}
+*/
+int CmdPskClean(const char *Cmd)
+{
+ uint8_t bitStream[MAX_GRAPH_TRACE_LEN]={0};
+ int bitLen = getFromGraphBuf(bitStream);
+ pskCleanWave(bitStream, bitLen);
+ setGraphBuf(bitStream, bitLen);
+ return 0;
+}
+
+//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 CmdpskNRZrawDemod(const char *Cmd)
+{
+ int errCnt= PSKnrzDemod(Cmd);
+ //output
+ if (errCnt<0) return 0;
+ if (errCnt>0){
+ PrintAndLog("# Errors during Demoding (shown as 77 in bit stream): %d",errCnt);
+ }
+ PrintAndLog("PSK or NRZ demoded bitstream:");
+ // Now output the bitstream to the scrollback by line of 16 bits
+ printDemodBuff();
+
+ return 1;
+}
+
+
+