text
[RRG-proxmark3.git] / common / lfdemod.h
bloba33752b31fa3b3aacd7ea36ecb4315c6eb521860
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Low frequency demod related commands
17 // note that many of these demods are not the slickest code and they often rely
18 // on peaks and clock instead of converting to clean signal.
19 //-----------------------------------------------------------------------------
21 #ifndef LFDEMOD_H__
22 #define LFDEMOD_H__
24 #include "common.h"
26 // might not be high enough for noisy environments
27 #define NOISE_AMPLITUDE_THRESHOLD 8
28 // ignore buffer with less than x samples
29 #define SIGNAL_MIN_SAMPLES 100
30 // ignore first x samples of the buffer
31 #define SIGNAL_IGNORE_FIRST_SAMPLES 10
33 // Max number of bits when demodulating a signal
34 #define MAX_DEMODULATION_BITS 4096
36 // generic
37 typedef struct {
38 int low;
39 int high;
40 int mean;
41 int amplitude;
42 bool isnoise;
43 } signal_t;
44 signal_t *getSignalProperties(void);
46 void computeSignalProperties(const uint8_t *samples, uint32_t size);
47 void removeSignalOffset(uint8_t *samples, uint32_t size);
48 void getNextLow(const uint8_t *samples, size_t size, int low, size_t *i);
49 void getNextHigh(const uint8_t *samples, size_t size, int high, size_t *i);
50 bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low);
51 size_t pskFindFirstPhaseShift(const uint8_t *samples, size_t size, uint8_t *curPhase, size_t waveStart, uint16_t fc, uint16_t *fullWaveLen);
53 size_t addParity(const uint8_t *src, uint8_t *dest, uint8_t sourceLen, uint8_t pLen, uint8_t pType);
54 int askdemod(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType);
55 int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr, uint8_t amp, uint8_t askType, int *startIdx);
56 void askAmp(uint8_t *bits, size_t size);
57 int BiphaseRawDecode(uint8_t *bits, size_t *size, int *offset, int invert);
58 int bits_to_array(const uint8_t *bits, size_t size, uint8_t *dest);
59 uint32_t bytebits_to_byte(uint8_t *src, size_t numbits);
60 uint32_t bytebits_to_byteLSBF(uint8_t *src, size_t numbits);
61 uint16_t countFC(const uint8_t *bits, size_t size, bool fskAdj);
62 int DetectASKClock(uint8_t *dest, size_t size, int *clock, int maxErr);
63 bool DetectCleanAskWave(const uint8_t *dest, size_t size, uint8_t high, uint8_t low);
64 uint8_t detectFSKClk(const uint8_t *bits, size_t size, uint8_t fcHigh, uint8_t fcLow, int *firstClockEdge);
65 int DetectNRZClock(uint8_t *dest, size_t size, int clock, size_t *clockStartIdx);
66 int DetectPSKClock(uint8_t *dest, size_t size, int clock, size_t *firstPhaseShift, uint8_t *curPhase, uint8_t *fc);
67 int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clock);
68 int DetectStrongNRZClk(const uint8_t *dest, size_t size, int peak, int low, bool *strong);
69 bool DetectST(uint8_t *buffer, size_t *size, int *foundclock, size_t *ststart, size_t *stend);
70 size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *start_idx);
71 // void getHiLo(uint8_t *bits, size_t size, int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
72 void getHiLo(int *high, int *low, uint8_t fuzzHi, uint8_t fuzzLo);
73 uint32_t manchesterEncode2Bytes(uint16_t datain);
74 void manchesterEncodeUint32(uint32_t data_in, uint8_t bitlen_in, uint8_t *bits_out, uint16_t *index);
75 int ManchesterEncode(uint8_t *bits, size_t size);
76 uint16_t manrawdecode(uint8_t *bits, size_t *size, uint8_t invert, uint8_t *alignPos);
77 int nrzRawDemod(uint8_t *dest, size_t *size, int *clk, const int *invert, int *startIdx);
78 bool parityTest(uint32_t bits, uint8_t bitLen, uint8_t pType);
79 bool preambleSearch(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx);
80 bool preambleSearchEx(uint8_t *bits, uint8_t *preamble, size_t pLen, size_t *size, size_t *startIdx, bool findone);
81 int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert);
82 int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, const int *invert, int *startIdx);
83 void psk2TOpsk1(uint8_t *bits, size_t size);
84 void psk1TOpsk2(uint8_t *bits, size_t size);
85 size_t removeParity(uint8_t *bits, size_t startIdx, uint8_t pLen, uint8_t pType, size_t bLen);
87 // tag specific
88 int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx);
89 int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, uint64_t *lo);
90 int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32_t *lo, int *waveStartIdx);
91 int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx);
93 #endif