File size: 7,930 Bytes
f5dbfe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from test1 import *




def crc_noref(bitstring, poly, init, xorout, width=5):
    """
    Computes a non-reflected CRC over the given bitstring.
    
    Parameters:
      bitstring (str): A string of '0' and '1'.
      poly (int): The CRC polynomial (e.g. 0x09 for CRC-5/EPC).
      init (int): The initial CRC value.
      xorout (int): The final XOR value.
      width (int): The CRC width (default 5).
    
    Returns:
      int: The computed CRC value.
    """
    crc = init
    mask = (1 << width) - 1
    topbit = 1 << (width - 1)
    for bit in bitstring:
        b = int(bit)
        # Grab the MSB of crc.
        msb = (crc & topbit) >> (width - 1)
        # Shift left and add the new bit.
        crc = ((crc << 1) & mask) | b
        if msb:
            crc ^= poly
    return crc ^ xorout


def crc_ref(bitstring, poly, init, xorout, width=5):
    """
    Computes a reflected CRC over the given bitstring.
    
    Parameters:
      bitstring (str): A string of '0' and '1'.
      poly (int): The CRC polynomial. For reflected CRCs, this is usually the reflected version.
      init (int): The initial CRC value.
      xorout (int): The final XOR value.
      width (int): The CRC width (default 5).
    
    Returns:
      int: The computed CRC value.
    """
    crc = init
    mask = (1 << width) - 1
    for bit in bitstring:
        b = int(bit)
        # XOR in the new bit into the LSB of crc.
        # (Note: for a fully bitwise algorithm, there are several approaches.
        # Here we process one bit at a time, shifting right.)
        lsb = crc & 1
        crc = (crc >> 1) | (b << (width - 1))
        if lsb:
            crc ^= poly
    return crc ^ xorout


# Define a dictionary of common CRC-5 variants.
# The parameters below are examples; please verify them against your protocol.
crc5_variants = {
    # CRC-5/EPC: Commonly used in RFID applications.
    "CRC-5/EPC": {"poly": 0x09, "init": 0x09, "refin": False, "xorout": 0x00},
    # CRC-5/ITU: Used in certain telecommunication protocols.
    "CRC-5/ITU": {"poly": 0x15, "init": 0x00, "refin": True,  "xorout": 0x00},
    # CRC-5/USB: Used in USB protocols.
    "CRC-5/USB": {"poly": 0x05, "init": 0x1F, "refin": True,  "xorout": 0x1F},
    # CRC-5/ROHC: Used in ROHC (Robust Header Compression).
    "CRC-5/ROHC": {"poly": 0x15, "init": 0x1F, "refin": True,  "xorout": 0x00},
    # CRC-5/BBC: Often used in older BBC/ARC protocols.
    "CRC-5/BBC": {"poly": 0x09, "init": 0x00, "refin": False, "xorout": 0x00},
}


def find_crc5_candidates(rx_packet, variant_parameters, start_range, end_range):
    """
    Given an RX packet as a binary string, this function will try various candidate slices
    (i.e. different assumed boundaries for the data+CRC) and run each through all defined
    CRC-5 variants. If the computed CRC equals zero, it is considered a candidate match.
    
    Parameters:
      rx_packet (str): The entire RX packet as a binary string (e.g., "110101001...")
      variant_parameters (dict): A dictionary of CRC-5 variants (like crc5_variants).
      start_range (tuple): A tuple (min_start, max_start) for candidate start indices.
      end_range (tuple): A tuple (min_end, max_end) for candidate end indices. (The candidate
                         is rx_packet[start:end]. Typically you need at least 5 bits at the end.)
    
    Returns:
      list of dicts: Each dict contains keys: 'variant', 'start', 'end', 'candidate' (the binary
                     substring tested), and 'crc' (which should be zero for a valid candidate).
    """
    results = []
    # Iterate over each CRC-5 variant.
    for variant_name, params in variant_parameters.items():
        poly = params["poly"]
        init = params["init"]
        refin = params["refin"]
        xorout = params["xorout"]
        
        # Try various candidate boundaries.
        for start in range(start_range[0], min(start_range[1], len(rx_packet)) + 1):
            for end in range(end_range[0], min(end_range[1], len(rx_packet)) + 1):
                # Make sure the candidate is long enough to include a 5-bit CRC.
                if end - start < 5:
                    continue
                candidate = rx_packet[start:end]
                # Compute the CRC using the appropriate algorithm.
                if not refin:
                    crc_val = crc_noref(candidate, poly, init, xorout, width=5)
                else:
                    crc_val = crc_ref(candidate, poly, init, xorout, width=5)
                if crc_val == 0:
                    results.append({
                        "variant": variant_name,
                        "start": start,
                        "end": end,
                        "candidate": candidate,
                        "crc": crc_val
                    })
    return results


# =============================================================================
# Example usage:
#
# Suppose you have an RX packet from your decoded data as a binary string.
# You are not 100% sure where the CRC portion starts/ends, so you decide to try
# candidate segments with different boundaries. For example:
#
#   - Try candidate segments starting between indices 0 and 5.
#   - Try candidate segments ending between (len(rx_packet) - 10) and len(rx_packet).
#
# Replace 'rx_packet' below with your actual binary data.
# =============================================================================
if __name__ == "__main__":
    # Example RX packet (replace with your actual decoded binary string).
    #rx_packet = "010101101100000111001101101110001001111111111111000000111000000011100101001000001001110101"

    all_candidates = {}

    total_valid = 0

    for idx in range(0, min(len(decoded_packets), 1000)):
        rx_packet = decoded_packets[idx][1]


        # Define the candidate ranges.
        # start_range = (0, 0)
        # end_range = (len(rx_packet) - 1, len(rx_packet))
        # end_range = (100, 100)
        
        # candidates = find_crc5_candidates(rx_packet, crc5_variants, start_range, end_range)
        
        # if candidates:
        #     #print("Found candidate(s) with a zero CRC:")
        #     for candidate in candidates:
        #         #print(f"Variant: {candidate['variant']}, "
        #         #    f"start index: {candidate['start']}, end index: {candidate['end']}, "
        #         #    f"candidate bits: {candidate['candidate']}, computed CRC: {candidate['crc']}")
                
        #         name = f"{candidate['variant']}_{candidate['start']}_{candidate['end']}"
        #         if name in all_candidates:
        #             all_candidates[name] += 1
        #         else:
        #             all_candidates[name] = 1
        #     total_valid += 1
        # else:
        #     pass
        #     #print("No valid CRC candidate found in the specified range.")


        # add bad bit, find any bit between 1 and 99 and flip it
        if 0:
            for bit in range(1, 100):
                rx_packet = rx_packet[:bit] + str(int(rx_packet[bit]) ^ 1) + rx_packet[bit + 1:]


        found = False
        for poly in range(11, 12):
            for init in range(0, 1):
                for xorout in range(0, 1):
                    crc = crc_noref(rx_packet[0:100], poly, init, xorout, width=5)
                    if(crc == 0):
                        name = f"{poly}_{init}_{xorout}"
                        #print(f"Found candidate with a zero CRC: {name}")
                        if name in all_candidates:
                            all_candidates[name] += 1
                        else:
                            all_candidates[name] = 1
                        found = True

        if found:
            total_valid += 1

    print(f"Total valid packets: {total_valid}")

    for candidate, count in sorted(all_candidates.items(), key=lambda item: item[1], reverse=True):
        print(f"{candidate}: {count}")