diff --git a/ADSB_Encoder.py b/ADSB_Encoder.py index 418f890..30b97b7 100755 --- a/ADSB_Encoder.py +++ b/ADSB_Encoder.py @@ -64,5 +64,6 @@ if __name__ == "__main__": samples_array = hackrf.hackrf_raw_IQ_format(df17_array) SamplesFile = open("Samples.iq8s", "wb") # TODO make this a function and take the file name. Also have the option to run dd on it. + # TODO make it empty the file first. SamplesFile.write(samples_array) os.system("dd if=Samples.iq8s of=Samples_256K.iq8s bs=4k seek=63") # TODO make this a flag, also make it take the file name diff --git a/ModeS.py b/ModeS.py index 813b9f1..21b612f 100644 --- a/ModeS.py +++ b/ModeS.py @@ -1,5 +1,5 @@ from location import * -from conversions import * + class ModeS: """This class handles the ModeS ADSB manipulation """ @@ -38,7 +38,7 @@ class ModeS: df17_str = "{0:02x}{1:02x}{2:02x}{3:02x}{4:02x}{5:02x}{6:02x}{7:02x}{8:02x}{9:02x}{10:02x}".format(*df17_even_bytes[0:11]) #print df17_str , "%X" % bin2int(crc(df17_str+"000000", encode=True)) , "%X" % get_parity(hex2bin(df17_str+"000000"), extended=True) - df17_crc = bin2int(crc(df17_str+"000000", encode=True)) + df17_crc = self.bin2int(self.modes_crc(df17_str+"000000", encode=True)) df17_even_bytes.append((df17_crc>>16) & 0xff) df17_even_bytes.append((df17_crc>> 8) & 0xff) @@ -60,10 +60,72 @@ class ModeS: df17_odd_bytes.append((oddenclon ) & 0xff) df17_str = "{0:02x}{1:02x}{2:02x}{3:02x}{4:02x}{5:02x}{6:02x}{7:02x}{8:02x}{9:02x}{10:02x}".format(*df17_odd_bytes[0:11]) - df17_crc = bin2int(crc(df17_str+"000000", encode=True)) + df17_crc = self.bin2int(self.modes_crc(df17_str+"000000", encode=True)) df17_odd_bytes.append((df17_crc>>16) & 0xff) df17_odd_bytes.append((df17_crc>> 8) & 0xff) df17_odd_bytes.append((df17_crc ) & 0xff) return (df17_even_bytes, df17_odd_bytes) + +############################################################### + +# Copyright (C) 2015 Junzi Sun (TU Delft) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# the polynominal generattor code for CRC + + + def modes_crc(self, msg, encode=False): + """Mode-S Cyclic Redundancy Check + Detect if bit error occurs in the Mode-S message + Args: + msg (string): 28 bytes hexadecimal message string + encode (bool): True to encode the date only and return the checksum + Returns: + string: message checksum, or partity bits (encoder) + """ + + GENERATOR = "1111111111111010000001001" # Currently don't know what is magic about this number + + msgbin = list(self.hex2bin(msg)) + + if encode: + msgbin[-24:] = ['0'] * 24 + + # loop all bits, except last 24 piraty bits + for i in range(len(msgbin)-24): + # if 1, perform modulo 2 multiplication, + if msgbin[i] == '1': + for j in range(len(GENERATOR)): + # modulo 2 multiplication = XOR + msgbin[i+j] = str((int(msgbin[i+j]) ^ int(GENERATOR[j]))) + + # last 24 bits + reminder = ''.join(msgbin[-24:]) + return reminder + + + + def hex2bin(self, hexstr): + """Convert a hexdecimal string to binary string, with zero fillings. """ + scale = 16 + num_of_bits = len(hexstr) * math.log(scale, 2) + binstr = bin(int(hexstr, scale))[2:].zfill(int(num_of_bits)) + return binstr + + def bin2int(self, binstr): + """Convert a binary string to integer. """ + return int(binstr, 2) diff --git a/conversions.py b/conversions.py index b5ca69f..547aef4 100644 --- a/conversions.py +++ b/conversions.py @@ -1,59 +1,6 @@ -############################################################### -# Copyright (C) 2015 Junzi Sun (TU Delft) - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -# the polynominal generattor code for CRC import math -GENERATOR = "1111111111111010000001001" -def hex2bin(hexstr): - """Convert a hexdecimal string to binary string, with zero fillings. """ - scale = 16 - num_of_bits = len(hexstr) * math.log(scale, 2) - binstr = bin(int(hexstr, scale))[2:].zfill(int(num_of_bits)) - return binstr -def bin2int(binstr): - """Convert a binary string to integer. """ - return int(binstr, 2) -def crc(msg, encode=False): - """Mode-S Cyclic Redundancy Check - Detect if bit error occurs in the Mode-S message - Args: - msg (string): 28 bytes hexadecimal message string - encode (bool): True to encode the date only and return the checksum - Returns: - string: message checksum, or partity bits (encoder) - """ - - msgbin = list(hex2bin(msg)) - - if encode: - msgbin[-24:] = ['0'] * 24 - - # loop all bits, except last 24 piraty bits - for i in range(len(msgbin)-24): - # if 1, perform modulo 2 multiplication, - if msgbin[i] == '1': - for j in range(len(GENERATOR)): - # modulo 2 multiplication = XOR - msgbin[i+j] = str((int(msgbin[i+j]) ^ int(GENERATOR[j]))) - - # last 24 bits - reminder = ''.join(msgbin[-24:]) - return reminder