- uint8_t foundCnt=0;
- for (int idx=0; idx < *size - pLen; idx++){
- if (memcmp(BitStream+idx, preamble, pLen) == 0){
- //first index found
- foundCnt++;
- if (foundCnt == 1){
- *startIdx = idx;
- }
- if (foundCnt == 2){
- *size = idx - *startIdx;
- return 1;
- }
- }
- }
- return 0;
-}
-
-//by marshmellow
-//takes 1s and 0s and searches for EM410x format - output EM ID
-uint8_t Em410xDecode(uint8_t *BitStream, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo)
-{
- //no arguments needed - built this way in case we want this to be a direct call from "data " cmds in the future
- // otherwise could be a void with no arguments
- //set defaults
- uint32_t i = 0;
- if (BitStream[1]>1){ //allow only 1s and 0s
- // PrintAndLog("no data found");
- return 0;
- }
- // 111111111 bit pattern represent start of frame
- // include 0 in front to help get start pos
- uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1};
- uint32_t idx = 0;
- uint32_t parityBits = 0;
- uint8_t errChk = 0;
- uint8_t FmtLen = 10;
- *startIdx = 0;
- errChk = preambleSearch(BitStream, preamble, sizeof(preamble), size, startIdx);
- if (errChk == 0 || *size < 64) return 0;
- if (*size > 64) FmtLen = 22;
- *startIdx += 1; //get rid of 0 from preamble
- idx = *startIdx + 9;
- for (i=0; i<FmtLen; i++){ //loop through 10 or 22 sets of 5 bits (50-10p = 40 bits or 88 bits)
- parityBits = bytebits_to_byte(BitStream+(i*5)+idx,5);
- //check even parity
- if (parityTest(parityBits, 5, 0) == 0){
- //parity failed quit
- return 0;
- }
- //set uint64 with ID from BitStream
- for (uint8_t ii=0; ii<4; ii++){
- *hi = (*hi << 1) | (*lo >> 63);
- *lo = (*lo << 1) | (BitStream[(i*5)+ii+idx]);
- }
- }
- if (errChk != 0) return 1;
- //skip last 5 bit parity test for simplicity.
- // *size = 64 | 128;
- return 0;
-}
-
-//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 askmandemod(uint8_t *BinStream, size_t *size, int *clk, int *invert, int maxErr)
-{
- int i;
- //int clk2=*clk;
- int start = DetectASKClock(BinStream, *size, clk, 20); //clock default
- if (*clk==0) return -3;
- if (start < 0) return -3;
- // if autodetected too low then adjust //MAY NEED ADJUSTMENT
- //if (clk2==0 && *clk<8) *clk =64;
- //if (clk2==0 && *clk<32) *clk=32;
- if (*invert != 0 && *invert != 1) *invert=0;
- uint32_t initLoopMax = 200;
- if (initLoopMax > *size) initLoopMax=*size;
- // Detect high and lows
- // 25% fuzz in case highs and lows aren't clipped [marshmellow]
- int high, low, ans;
- ans = getHiLo(BinStream, initLoopMax, &high, &low, 75, 75);
- if (ans<1) return -2; //just noise
-
- // PrintAndLog("DEBUG - valid high: %d - valid low: %d",high,low);
- int lastBit = 0; //set first clock check
- uint32_t bitnum = 0; //output counter
- int tol = 0; //clock tolerance adjust - waves will be accepted as within the clock if they fall + or - this value + clock from last valid wave
- if (*clk<=32) tol=1; //clock tolerance may not be needed anymore currently set to + or - 1 but could be increased for poor waves or removed entirely
- int iii = 0;
- uint32_t gLen = *size;
- if (gLen > 3000) gLen=3000;
- //if 0 errors allowed then only try first 2 clock cycles as we want a low tolerance
- if (!maxErr) gLen=*clk*2;
- uint8_t errCnt =0;
- uint16_t MaxBits = 500;
- uint32_t bestStart = *size;
- int bestErrCnt = maxErr+1;
- // PrintAndLog("DEBUG - lastbit - %d",lastBit);
- // loop to find first wave that works
- for (iii=0; iii < gLen; ++iii){
- if ((BinStream[iii] >= high) || (BinStream[iii] <= low)){
- lastBit=iii-*clk;
- errCnt=0;
- // loop through to see if this start location works
- for (i = iii; i < *size; ++i) {
- if ((BinStream[i] >= high) && ((i-lastBit) > (*clk-tol))){
- lastBit+=*clk;
- } else if ((BinStream[i] <= low) && ((i-lastBit) > (*clk-tol))){
- //low found and we are expecting a bar
- lastBit+=*clk;
- } else {
- //mid value found or no bar supposed to be here
- if ((i-lastBit)>(*clk+tol)){
- //should have hit a high or low based on clock!!
-
- //debug
- //PrintAndLog("DEBUG - no wave in expected area - location: %d, expected: %d-%d, lastBit: %d - resetting search",i,(lastBit+(clk-((int)(tol)))),(lastBit+(clk+((int)(tol)))),lastBit);
-
- errCnt++;
- lastBit+=*clk;//skip over until hit too many errors
- if (errCnt>(maxErr)) break; //allow 1 error for every 1000 samples else start over
- }
- }
- if ((i-iii) >(MaxBits * *clk)) break; //got plenty of bits
- }
- //we got more than 64 good bits and not all errors
- if ((((i-iii)/ *clk) > (64)) && (errCnt<=maxErr)) {
- //possible good read
- if (errCnt==0){
- bestStart=iii;
- bestErrCnt=errCnt;
- break; //great read - finish
- }
- if (errCnt<bestErrCnt){ //set this as new best run
- bestErrCnt=errCnt;
- bestStart = iii;
- }