+//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 (g_debugMode || verbose) 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;
+ uint8_t carrier=countFC(BitStream, BitLen, 0);
+ if (carrier!=2 && carrier!=4 && carrier!=8){
+ //invalid carrier
+ return 0;
+ }
+ if (g_debugMode){
+ PrintAndLog("Carrier: rf/%d",carrier);
+ }
+ int errCnt=0;
+ errCnt = pskRawDemod(BitStream, &BitLen, &clk, &invert);
+ if (errCnt > maxErr){
+ if (g_debugMode || 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 || verbose) PrintAndLog("no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
+ return 0;
+ }
+ if (verbose || g_debugMode){
+ PrintAndLog("\nUsing Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen);
+ if (errCnt>0){
+ PrintAndLog("# Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
+ }
+ }
+ //prime demod buffer for output
+ setDemodBuf(BitStream,BitLen,0);
+ return 1;
+}
+
+// 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){
+ if (g_debugMode)
+ PrintAndLog("Error1: %d",ans);
+ return 0;
+ }
+ uint8_t invert=0;
+ size_t size = DemodBufferLen;
+ int startIdx = indala26decode(DemodBuffer, &size, &invert);
+ if (startIdx < 0 || size > 224) {
+ if (g_debugMode)
+ PrintAndLog("Error2: %d",ans);
+ return -1;
+ }
+ setDemodBuf(DemodBuffer, size, (size_t)startIdx);
+ if (invert)
+ if (g_debugMode)
+ PrintAndLog("Had to invert bits");
+
+ PrintAndLog("BitLen: %d",DemodBufferLen);
+ //convert UID to HEX
+ uint32_t uid1, uid2, uid3, uid4, uid5, uid6, uid7;
+ uid1=bytebits_to_byte(DemodBuffer,32);
+ uid2=bytebits_to_byte(DemodBuffer+32,32);
+ if (DemodBufferLen==64) {
+ PrintAndLog("Indala UID=%s (%x%08x)", sprint_bin_break(DemodBuffer,DemodBufferLen,16), uid1, uid2);
+ } else {
+ uid3=bytebits_to_byte(DemodBuffer+64,32);
+ uid4=bytebits_to_byte(DemodBuffer+96,32);
+ uid5=bytebits_to_byte(DemodBuffer+128,32);
+ uid6=bytebits_to_byte(DemodBuffer+160,32);
+ uid7=bytebits_to_byte(DemodBuffer+192,32);
+ PrintAndLog("Indala UID=%s (%x%08x%08x%08x%08x%08x%08x)",
+ sprint_bin_break(DemodBuffer,DemodBufferLen,16), uid1, uid2, uid3, uid4, uid5, uid6, uid7);
+ }
+ if (g_debugMode){
+ PrintAndLog("DEBUG: printing demodbuffer:");
+ printDemodBuff();
+ }
+ return 1;
+}
+
+int CmdPSKNexWatch(const char *Cmd)
+{
+ if (!PSKDemod("", false)) return 0;
+ uint8_t preamble[28] = {0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ size_t startIdx = 0, size = DemodBufferLen;
+ bool invert = false;
+ if (!preambleSearch(DemodBuffer, preamble, sizeof(preamble), &size, &startIdx)){
+ // if didn't find preamble try again inverting
+ if (!PSKDemod("1", false)) return 0;
+ size = DemodBufferLen;
+ if (!preambleSearch(DemodBuffer, preamble, sizeof(preamble), &size, &startIdx)) return 0;
+ invert = true;
+ }
+ if (size != 128) return 0;
+ setDemodBuf(DemodBuffer, size, startIdx+4);
+ startIdx = 8+32; //4 = extra i added, 8 = preamble, 32 = reserved bits (always 0)
+ //get ID
+ uint32_t ID = 0;
+ for (uint8_t wordIdx=0; wordIdx<4; wordIdx++){
+ for (uint8_t idx=0; idx<8; idx++){
+ ID = (ID << 1) | DemodBuffer[startIdx+wordIdx+(idx*4)];
+ }
+ }
+ //parity check (TBD)
+
+ //checksum check (TBD)
+
+ //output
+ PrintAndLog("NexWatch ID: %d", ID);
+ if (invert){
+ PrintAndLog("Had to Invert - probably NexKey");
+ for (uint8_t idx=0; idx<size; idx++)
+ DemodBuffer[idx] ^= 1;
+ }
+
+ CmdPrintDemodBuff("x");
+ 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)