def make_full(hexinput): pdf1 = decodefunctions.hextobin(hexinput) b = pdf1[:204] + '0' * 48 print(b, len(b)) ecc = calcBCH(b, 0, 202, 250) newhex = decodefunctions.bin2hex(pdf1[:204] + ecc) print(newhex) print(ecc, decodefunctions.bin2hex(ecc)) BCH_POLYNOMIAL = 285 # 285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(pdf1[:204])) necc = bch.encode(data) bchstring = '' for e in necc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar print(bchstring, decodefunctions.bin2hex(bchstring)) newhex = decodefunctions.bin2hex(pdf1[:204] + bchstring) return newhex
def correct_bchsgb(testhex): pdf = decodefunctions.hextobin(testhex)[:204] bchsgb = (decodefunctions.hextobin(testhex))[204:] binary_data_pdf = newdata = pdf.zfill(204) correctedbch = bchsgb BCH_POLYNOMIAL = 285 BCH_BITS = 6 bitflips = 0 bch = bchlibcaronoff.BCH(BCH_BITS, m=8, prim_poly=BCH_POLYNOMIAL) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 + 1 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) rebuildpdf = '' for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment rebuildpdf = rebuildpdf.zfill(204) ecc = bch.encode(data) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) bchstring = bchstring + binchar ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) packet = data + ecc_provided if ecc != ecc_provided: data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bch.data_len = max_data_len nerr = bch.decode(data, ecc) bitflips = nerr bch.correct(data, ecc) newdata = decodefunctions.dec2bin(data[0]) for e in data[1:]: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar newdata = newdata.zfill(204) correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar newhex = decodefunctions.bin2hex(newdata + correctedbch) else: bitflips = 0 newdata = pdf correctedbch = bchsgb newhex = decodefunctions.bin2hex(pdf + bchsgb) return (bitflips, newdata, correctedbch, newhex)
def bch_check(inputhex): errors = [inputhex] preamble ='' if len(inputhex)==36: strhex = inputhex[6:] preamble = inputhex[:6] elif len(inputhex)==30: strhex = inputhex preamble ='' else: return False ## Error correction attempt for when BCH portion does not match recomputed try: _pdf1 = (Fcn.hextobin(strhex))[:61] _bch1 = (Fcn.hextobin(strhex))[61:82] bitflips1, newpdf1, newbch1 = bch1.pdf1_to_bch1(_pdf1, _bch1) _pdf2 = (Fcn.hextobin(strhex))[82:108] _bch2 = (Fcn.hextobin(strhex))[108:] bitflips2, newpdf2, newbch2 = bch2.pdf2_to_bch2(_pdf2, _bch2) if bitflips1 == -1 or bitflips2 == -1: errors.append('Too many bit errors to correct') elif bitflips1 > 0 or bitflips2 > 0: _newbin = newpdf1 + newbch1 + newpdf2 + newbch2 fixhex= preamble+ Fcn.bin2hex(_newbin) errors.append(fixhex) errors.append(' {} bad pdf1 bit and {} bad pdf2 bit'.format(bitflips1, bitflips2)) errors.append('Corrected Message: {} '.format(fixhex)) except: return False return errors
def fgbcompute(f, r): for n in range(r): ident = randombinary(49) while ident[:2] == '101': ident = randombinary(49) pdf1 = preamble + short_long + user_location + countrycode + ident bch1 = Fcn.calcbch(('_' + pdf1 + '0' * 59), "1001101101100111100011", 25, 86, 107) newbin = pdf1 + bch1 supdata = randombinary(26) bch2 = '' try: bch2 = Fcn.calcbch('_' + newbin + supdata + '0' * 12, '1010100111001', 107, 133, 145) except IndexError: print('error') print(len('_' + newbin + supdata + '0' * 12)) finalbin = newbin + supdata + bch2 hexvalid = Fcn.bin2hex(finalbin[24:]) r1 = 0 # str(random.randint(0,4)) r2 = 3 # str(random.randint(0,3)) hexbad = random_error(hexvalid, r1, r2, 0) f.writelines(['{},{},{},{}'.format(hexvalid, r1, r2, hexbad), '\n'])
def encodelongFGB(hex_code, latitude, southnorth, longitude, eastwest, suppdata): latitude = round(float(latitude) * 1000, 0) longitude = round(float(longitude) * 1000, 0) c = decodehex2.BeaconFGB() try: c.processHex(str(hex_code.strip())) if c.protocolflag() == 'User': binstr = c.bin[0:25] + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata binstr = binstr + str(southnorth) + dec2bin(int(float(latitude/1000)),7) + \ dec2bin( round(( float(latitude/1000) - int(float(latitude/1000))) * 15,0) ,4) + str(eastwest) + \ dec2bin(int(float(longitude / 1000)), 8) + \ dec2bin(round((float(longitude / 1000) - int(float(longitude / 1000))) * 15, 0), 4) bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in [ 'Standard Location', 'Standard Location Protocol - Test' ]: bincoord = stdloc(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:65] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in [ 'ELT-DT Location', 'RLS Location' ]: bincoord = eltdt_rls(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:67] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype( ) == 'National Location': bincoord = natloc(latitude, longitude) binstr = c.bin[0:25] + '1' + c.bin[26:59] + str( southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[ 3] + '000000' bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 return bin2hex(binstr[1:]) except decodehex2.HexError as e: print(e.value, e.message)
def sgbcompute(f, r): for n in range(r): pdf = randombinary(202).zfill(202) bchcalc = sgb.calcBCH(pdf + '0' * 48, 0, 202, 250) finalbin = (pdf + bchcalc).zfill(252) hexvalid = Fcn.bin2hex(finalbin) r1 = random.randint(0, 8) r1 = 6 hexbad = random_error(hexvalid, 0, 0, r1) f.writelines(['{},{},{}'.format(hexvalid, str(r1), hexbad), '\n'])
def test63hex(): data = bytearray(b'\x03' + os.urandom(25)) bch = bchlibcaronoff.BCH(6, m=8, prim_poly=285) ecc = bch.encode(data) rebuildpdf = '' for e in data: segment = decodefunctions.dec2bin(e).zfill(8) rebuildpdf = rebuildpdf + segment rebuildbch = '' for e in ecc: segment = decodefunctions.dec2bin(e).zfill(8) rebuildbch = rebuildbch + segment b = rebuildpdf + rebuildbch #print(decodefunctions.bin2hex(b)) return decodefunctions.bin2hex(b)
def bch_recalc(inputhex): results = [inputhex] preamble = '' if len(inputhex) == 36: strhex = inputhex[6:] preamble = inputhex[:6] elif len(inputhex) == 30: strhex = inputhex preamble = '' else: type = 'Hex length of ' + str( len(inputhex) ) + '.' + ' Length of First Generation Beacon Hex Code to test BCH must be 30 or 36' return [type] try: bin = Fcn.hextobin(strhex) _pdf1 = (bin)[:61] _bch1 = (bin)[61:82] _pdf2 = (bin)[82:108] _bch2 = (bin)[108:] bch1calc = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82) bch2calc = Fcn.calcbch(bin, '1010100111001', 82, 108, 120) if _pdf1 + bch1calc + _pdf2 + bch2calc == bin: results.append( 'bch1 and bch2 recomputed from provided pdf1 and pdf2 match') else: if bch1calc != _bch1: results.append( 'bch1 recomputed from provided pdf1 in input message {}'. format(bch1calc)) if bch2calc != _bch2: results.append( 'bch2 recomputed from provided pdf2 in input message {}'. format(bch2calc)) results.append(preamble + Fcn.bin2hex(_pdf1 + bch1calc + _pdf2 + bch2calc)) return results except TypeError as err: return [inputhex + ' Is not a valid hex']
def random_error(hexvalid, pdf1err, pdf2err, sgberr): #pdf1/bc1 errors correctible 3 #pdf2/bc2 errors correctible 2 scramble = list(Fcn.hextobin(hexvalid)) #print(scramble) b = Fcn.hextobin(hexvalid) if pdf1err or pdf2err: for ipos in random.sample(range(0, 82), pdf1err): scramble[ipos] = str(int(not int(scramble[ipos]))) for j in random.sample(range(82, 120), pdf2err): scramble[j] = str(int(not int(scramble[j]))) elif sgberr: for i in range(int(sgberr)): epos = random.randint(0, 251) scramble[epos] = str(int(not int(scramble[epos]))) return Fcn.bin2hex(''.join(scramble))
def test_hex(testhex): pdf = decodefunctions.hextobin(testhex)[:204] print(pdf) print(len(pdf)) bch = (decodefunctions.hextobin(testhex))[204:] print(bch, len(bch)) bitflips, newpdf, newbch = pdf_to_bchsgb(pdf, bch) print('errors result: ', bitflips) if bitflips == -1: print('fail') elif bitflips == 0: print('match') elif bitflips > 0: print(20 * '-' + 'ORIGINAL MESSAGE' + 25 * '-') print(testhex) print(20 * '-' + 'CORRECTED MESSAGE' + 25 * '-') print(decodefunctions.bin2hex(newpdf + newbch)) print(newpdf + newbch)
def userProtocol(self): self.hex15 = Fcn.bin2hex(self.bin[26:86]) self.tablebin.append( ['26-85', self.bin[26:86], 'Beacon UIN', self.hex15]) self._loctype = "User" self.encpos = 'na' btype = 'Unknown Beacon' tano = 'na' ############################################################################# # # Bit 37-39: 011: Serial User Protocol # # 011: Serial User Protocol # ############################################################################# typeuserprotbin = self.bin[37:40] self._loctype = definitions.userprottype[typeuserprotbin] self._protocol = ('Protocol Flag (Bit 26) :' + self.bin[26], definitions.protocol[self.bin[26]], 'User Protocol Type (bits 37-39) : ' + typeuserprotbin, definitions.userprottype[typeuserprotbin]) self._protocold = { 'pflag': definitions.protocol[self.bin[26]], 'ptype': definitions.userprottype[typeuserprotbin], 'serial': '' } if typeuserprotbin == '011': # Bit 37-39: 011: Serial User Protocol (see bits 40 to 42) susertype = self.bin[40:43] serialtype = definitions.serialusertype[susertype] self._protocold['serial'] = serialtype self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', 'Serial user' ]) self.tablebin.append( ['40-42', str(self.bin[40:43]), 'Serial type', serialtype]) # Bit 43 value 1 - Yes for type approval certificate if self.bin[43] == '1': tacert = 'Bit 43 assisgned. Type Approval at bits 74-84.' # Bits 64-73 all 0 or national use # Bits 74-83 is the Type Approval Certificate Number tano = str(Fcn.bin2dec((self.bin)[74:84])) else: # Bits 64-83 is national use or as defined by other protocl tacert = 'Bit 43 not assisgned - no type approval number in Hex' tano = 'na' self.tablebin.append(['43', str(self.bin[43]), 'TAC', tacert]) # Bits 40-42 : 000 : ELT with Serial Identification # Bits 40-42 : 010 : Float free EPIRB with Serial Identification # Bits 40-42 : 100 : Nonfloat free EPIRB with Serial Identification # Bits 40-42 : 110 : PLB with Serial Identification # Serial ID is from bit 44-63 if susertype in ['000', '010', '100', '110']: s1, s2 = ('Bits 64-73 : ' + (self.bin[64:74]), 'Serial ID Decimal Value: ' + str(Fcn.bin2dec(self.bin[44:64]))) auxradiodevice = 'Aux Radio Device: ' + self.bin[ 84:86] + ' ' + definitions.auxlocdevice[self.bin[84:86]] if susertype in ['010', '100']: btype = 'EPIRB' elif susertype == '000': btype = 'ELT' elif susertype == '110': btype = 'PLB' self.tablebin.append([ '44-63', str(self.bin[44:64]), 'Serial Number', str(Fcn.bin2dec(self.bin[44:64])) ]) self.tablebin.append([ '64-73', str(self.bin[64:74]), 'National use', str(Fcn.bin2dec(self.bin[64:74])) ]) # For Serial User Protocol # Bit 40-42 : 011: ELT with Aircraft 24-bit Address if susertype == '011': btype, s1, s2 = ( 'ELT', 'Aircraft 24 bit Address (bits 44-67) :Dec value: ' + str(Fcn.bin2dec(self.bin[44:68])) + ' Hex Value : ' + Fcn.bin2hex(self.bin[44:68]), 'Number of Additional ELTs (bits 68-73):' + str(Fcn.bin2dec(self.bin[68:74]))) auxradiodevice = 'Aux Radio Device: ' + self.bin[ 84:86] + ' ' + definitions.auxlocdevice[self.bin[84:86]] emergencycode = 'Emergency Code (109-112): ' self.tablebin.append([ '44-67', str(self.bin[44:68]), 'AirCraft 24 bit identification', str(Fcn.bin2dec(self.bin[44:68])) ]) # Bit 40-42 : 001 : Aircraft operator designator and serial number # Aircraft Operator is in bits 44-61 # Serial ID is from bit 62-73 if susertype == '001': btype, s1, s2 = ('ELT', 'AirCraft Operator Designator : ' + Fcn.baudot(self.bin, 44, 62), 'Serial # Assigned by Operator: ' + str(Fcn.bin2dec(self.bin[62:74]))) auxradiodevice = 'Aux Radio Device: ' + self.bin[ 84:86] + ' ' + definitions.auxlocdevice[self.bin[84:86]] self.tablebin.append([ '44-61', str(self.bin[44:62]), 'AirCraft Operator Designator', Fcn.baudot(self.bin, 44, 62) ]) self.tablebin.append([ '62-73', str(self.bin[62:74]), 'Serial No Assigned by Operator', str(Fcn.bin2dec(self.bin[62:74])) ]) if susertype in ['111', '101']: self.tablebin.append( ['44-73', str(self.bin[44:74]), 'Unknown Serial type', '']) self._protocol = ('Protocol Flag (Bit 26) :' + self.bin[26], definitions.protocol[self.bin[26]], typeuserprotbin, definitions.userprottype[typeuserprotbin], susertype, serialtype) self.typeapproval = (tacert, 'Type Approval ', str(tano)) self.tablebin.append([ '74-83', str(self.bin[74:84]), 'Type approval certificate No', tano ]) self.tablebin.append([ '84-85', str(self.bin[84:86]), 'Auxiliary radio device', definitions.auxlocdevice[self.bin[84:86]] ]) ############################################################################# # Bit 37-39: 000 Orbitography User Protocol # ############################################################################# elif typeuserprotbin == '000': self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', definitions.userprottype[typeuserprotbin] ]) btype = 'Orbitography' self.tablebin.append([ '40-85', str(self.bin[40:86]), 'Identification', str(Fcn.bin2hex(self.bin[40:88])) ]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) self.tablebin.append([ '107-132', str(self.bin[107:133]), 'Reserved', 'Reserved for national use' ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) ############################################################################# # Bit 37-39: 001 ELT Aviation User Protocol # ############################################################################# elif typeuserprotbin == '001': self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', 'ELT Aviation User' ]) aircraftid = Fcn.baudot(self.bin, 40, 82) self.tablebin.append( ['40-81', str(self.bin[40:82]), 'Aircraft ID', aircraftid]) self.tablebin.append([ '82-83', str(self.bin[82:84]), 'ELT No', str(Fcn.bin2dec(self.bin[82:84])) ]) self.tablebin.append([ '84-85', str(self.bin[84:86]), 'Auxiliary radio device', definitions.auxlocdevice[self.bin[84:86]] ]) btype = 'ELT' ############################################################################# # Bit 37-39: 111 : Test User protocol # ############################################################################# elif typeuserprotbin == '111': self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', 'Test user' ]) self.tablebin.append( ['40-85', str(self.bin[40:86]), 'National use', '']) btype = 'Test' ############################################################################# # Bit 37-39: 110 : Radio Call Sign xxx # ############################################################################# elif typeuserprotbin == '110': btype = 'EPIRB' mmsi = bcd = emergencycode = '' m = self.bin[40:76] pad = '' if Fcn.bin2dec(self.bin[72:76]) < 10: pad = str(Fcn.bin2dec(self.bin[72:76])) radiocallsign = Fcn.baudot(self.bin, 40, 64) + str( Fcn.bin2dec(self.bin[64:68])) + str( Fcn.bin2dec(self.bin[68:72])) + pad self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', definitions.userprottype[typeuserprotbin] ]) self.tablebin.append([ '40-75', str(self.bin[40:76]), 'Radio call sign', radiocallsign ]) self.tablebin.append([ '76-81', str(self.bin[76:82]), 'Beacon No', self.bin[76:82] + ': ' + Fcn.baudot(self.bin, 76, 82) ]) self.tablebin.append([ '82-83', str(self.bin[82:84]), 'Spare No', str(Fcn.bin2dec(self.bin[82:84])) ]) self.tablebin.append([ '84-85', str(self.bin[84:86]), 'Auxiliary radio device', definitions.auxlocdevice[self.bin[84:86]] ]) ############################################################################# # Bit 37-39: 010 Maritime User Protocol # ############################################################################# elif typeuserprotbin == '010': mmsi = 'MMSI: ' + Fcn.baudot(self.bin, 40, 76) btype = 'EPIRB' self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', definitions.userprottype[typeuserprotbin] ]) self.tablebin.append([ '40-75', str(self.bin[40:76]), 'MMSI', Fcn.baudot(self.bin, 40, 76) ]) self.tablebin.append([ '76-81', str(self.bin[76:82]), 'Beacon No', self.bin[76:82] + ': ' + Fcn.baudot(self.bin, 76, 82) ]) self.tablebin.append([ '82-83', str(self.bin[82:84]), 'Spare No', str(Fcn.bin2dec(self.bin[82:84])) ]) self.tablebin.append([ '84-85', str(self.bin[84:86]), 'Auxiliary radio device', definitions.auxlocdevice[self.bin[84:86]] ]) ############################################################################## # Bit 37-39: 100 National User Protocol # ############################################################################## elif typeuserprotbin == '100': self._protocol = (self.bin[26], definitions.protocol[self.bin[26]], typeuserprotbin, definitions.userprottype[typeuserprotbin]) self.tablebin.append([ '37-39', str(self.bin[37:40]), 'User protocol type', definitions.userprottype[typeuserprotbin] ]) self.tablebin.append([ '40-85', str(self.bin[84:86]), 'Reserved', 'Reserved for national use' ]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) self.tablebin.append([ '107-112', str(self.bin[107:113]), 'Reserved', 'Reserved for national use' ]) self.tablebin.append([ '113-132', str(self.bin[113:133]), 'Reserved', 'Reserved for national use' ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) if typeuserprotbin not in ['100', '000']: # and self.bch.complete=='1': location_data = 'Check for location' self.encpos = str(self.bin[107]) lat, declat, latdir, ltminutes = Fcn.latitude( self.bin[108], self.bin[109:116], self.bin[116:120]) lg, declng, lngdir, lgminutes = Fcn.longitude( self.bin[120], self.bin[121:129], self.bin[129:133]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) self.tablebin.append([ '107', str(self.bin[107]), 'Encoded location source', definitions.enc_delta_posflag[self.encpos] ]) if Fcn.is_number(declat) and Fcn.is_number(declng): self._loc = True a = self.update_locd(declat, latdir) b = self.update_locd(declng, lngdir) else: a = declat b = declng self.location = (a, b) self.tablebin.append([ '108-119', str(self.bin[108:120]), 'Latitude', '{} (decimal: {})'.format(lat, a) ]) self.tablebin.append([ '120-132', str(self.bin[120:133]), 'Longitude', '{} (decimal: {})'.format(lg, b) ]) self.tablebin.append( ['', '', 'Resolved location', '{} {}'.format(a, b)]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) self._btype = btype
def pdf_to_bchsgb(pdf, bchsgb): binary_data_pdf = newdata = pdf.zfill(204) print(len(binary_data_pdf)) correctedbch = bchsgb bchsgb = bchsgb BCH_POLYNOMIAL = 285 #285 BCH_BITS = 6 bitflips = 0 bch = bchlib.BCH(BCH_POLYNOMIAL, BCH_BITS) max_data_len = bch.n // 8 - (bch.ecc_bits + 7) // 8 data = bytearray(bch1correct.bitstring_to_bytes(binary_data_pdf)) print('len in bytes of data', len(data)) print('m------------:', bch.m) print('ecc_bits: {} ecc_bytes: {} '.format(bch.ecc_bits, bch.ecc_bytes)) print('m:', bch.m) print('n: {} bytes: {}'.format(bch.n, bch.n // 8)) print('max data length:', max_data_len) print('t: {}'.format(bch.t)) print('length of data in bytes:', len(data)) rebuildpdf = '' print('---Per Byte' + '-' * 40) for e in range(len(data)): segment = decodefunctions.dec2bin(data[e]).zfill(8) rebuildpdf = rebuildpdf + segment print(e, data[e], segment) print('-' * 50) print('pdf as input', len(pdf), pdf) print('bch as input', len(bchsgb), bchsgb) print('-' * 50) rebuildpdf = rebuildpdf.zfill(202) print(rebuildpdf, len(rebuildpdf)) print(binary_data_pdf == rebuildpdf) print('inputed data len to encode bch: ', len(data)) ecc = bch.encode(data) print('computed ecc bytes:', len(ecc)) bchstring = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) print(e, binchar) bchstring = bchstring + binchar print(bchsgb) print(bchstring) print(bchsgb == bchstring) print(decodefunctions.bin2hex(rebuildpdf + bchstring)) # create array of ecc provide by bch ecc_provided = bytearray(bch1correct.bitstring_to_bytes(bchsgb)) print(ecc) print('ecc included:', ecc_provided, len(ecc_provided), type(ecc_provided)) print('ecc calc:', ecc, len(ecc), type(ecc)) print('provided', bchsgb, len(bchsgb), decodefunctions.bin2hex(bchsgb)) print('calculated', bchstring, len(bchstring), decodefunctions.bin2hex(bchstring)) packet = data + ecc_provided print('ecc equiv', ecc == ecc_provided) if ecc != ecc_provided: data, ecc = packet[1:-bch.ecc_bytes], packet[-bch.ecc_bytes:] print('cropped len data', len(data)) #data, ecc = packet[:-6], packet[-6:] print(data, ecc) bitflips = bch.decode_inplace(data, ecc) print('bitflips: %d' % (bitflips)) newdata = pdf[:4] #decodefunctions.dec2bin(byte_one) for e in data: binchar = decodefunctions.dec2bin(e).zfill(8) newdata = newdata + binchar # correctedbch = '' for e in ecc: binchar = decodefunctions.dec2bin(e).zfill(8) correctedbch = correctedbch + binchar return (bitflips, newdata.zfill(204), correctedbch[:48])
b = '0' else: b = '1' binstr = binstr + b return binstr preamble = '000000000000000000101111' short_long = '1' user_location = '1' countrycode = '0100111100' #316 - Canada for n in range(10000): ident = randombinary(49) pdf1 = preamble + short_long + user_location + countrycode + ident bch1 = Fcn.calcbch(('_' + pdf1 + '0' * 59), "1001101101100111100011", 25, 86, 107) newbin = pdf1 + bch1 supdata = randombinary(26) bch2 = Fcn.calcbch('_' + newbin + supdata + '0' * 12, '1010100111001', 107, 133, 145) finalbin = newbin + supdata + bch2 # print(Fcn.bin2hex(finalbin)) hexcheck = Fcn.bin2hex(finalbin) if decodehex2.BeaconFGB(hexcheck).errors == []: decoded.write(hexcheck + '\n') decoded.close()
def encodelongFGB(hex_code, formdata): for e in formdata: print('form control' , e, formdata[e]) c = decodehex2.BeaconFGB() pad24='1'*16 +'000101111' try: c.processHex(str(hex_code.strip())) if c.loctype()!='National User': latitude = round(float(formdata['latitude']) * 1000, 0) longitude = round(float(formdata['longitude']) * 1000, 0) southnorth = formdata['northsouth'] eastwest = formdata['eastwest'] if c.protocolflag() == 'User' and c.loctype()!='National User': suppdata= formdata['encodepos'] binstr = pad24 + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata binstr = binstr + str(southnorth) + dec2bin(int(float(latitude/1000)),7) + \ dec2bin( round(( float(latitude/1000) - int(float(latitude/1000))) * 15,0) ,4) + str(eastwest) + \ dec2bin(int(float(longitude / 1000)), 8) + \ dec2bin(round((float(longitude / 1000) - int(float(longitude / 1000))) * 15, 0), 4) bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.loctype() == 'National User': binstr = '_'+ '1'*15 + '000101111' + '1' + c.bin[26:86] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 +'0' *26 bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() in \ ['Location: Standard Location Protocol - ELT (Serial)','Standard Location','Standard Location Protocol - Test','Standard Location Protocol - PLB (Serial)','Standard Location Protocol - EPIRB (Serial)','Std Loc. Serial ELT - Aircraft Operator Designator Protocol']: bincoord = stdloc(latitude, longitude) suppdata = '1101' + formdata['encodepos'] + formdata['auxdevice'] binstr = pad24 + '1' + c.bin[26:65] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == 'RLS Location Protocol': # bit 115=132 are 18 bit location offset suppdata = formdata['encodepos'] + \ formdata['auxdevice'] + \ formdata['rlmtypeone'] + \ formdata['rlmtypetwo'] + \ formdata['feedbacktype1'] + \ formdata['feedbacktype2'] + \ formdata['rlsprovider'] bincoord = eltdt_rls(latitude, longitude) binstr = pad24 + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == definitions.ELT_DT_LOC: suppdata = formdata['meansactivation'] + formdata['encodedaltitude'] + formdata['freshness'] aircraft3letter = formdata['aircraft_3ld'] if suppdata[-2:] != '00': # bit 115=132 are 18 bit location offset bincoord= eltdt_rls(latitude, longitude) binstr ='_'+ '1'*15 + '000101111' + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) binstr = binstr + bch1 + suppdata + bincoord[2] + bincoord[3] bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 else: # bits 115-132 are aircraft 3 letter designators 000 and 3 segments of 5 bit modified baudot for each letter designator bincoord = eltdt_rls(latitude, longitude) binstr = '_' + '1'*15 + '000101111' + '1' + c.bin[26:67] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) aircraft3letterbin='' for letter in aircraft3letter.strip(): try: key = next(key for key, value in definitions.baudot.items() if value == letter.upper()) except StopIteration: key = '111111' key = key[1:] aircraft3letterbin = aircraft3letterbin + key aircraft3letterbin = aircraft3letterbin + '11111' * (3 - len(aircraft3letter.strip())) binstr = binstr + bch1 + suppdata + '000' + aircraft3letterbin bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 elif c.protocolflag() == 'Location' and c.loctype() == 'National Location': bincoord= natloc(latitude, longitude) suppdata = '110' + formdata['nationalassign']+ formdata['encodepos'] + formdata['auxdevice'] binstr = pad24 + '1' + c.bin[26:59] + str(southnorth) + bincoord[0] + str(eastwest) + bincoord[1] bch1 = calcbch(binstr, "1001101101100111100011", 25, 86, 107) if formdata['nationalassign'] == '0': refine = '0'*14 else: refine = bincoord[2] + bincoord[3] binstr = binstr + bch1 + suppdata + refine + '000000' bch2 = calcbch(binstr, '1010100111001', 107, 133, 145) binstr = binstr + bch2 return bin2hex(binstr[1:]) except decodehex2.HexError as e: print(e.value, e.message)
def locationProtocol(self): typelocprotbin = self.bin[37:41] self._locd = dict(lat='not provided', long='not provided', comp='') tano = 'na' self.encpos = 'na' if typelocprotbin in ['0010', '0110', '1010', '1100']: btype = 'EPIRB' elif typelocprotbin in ['0011', '0101', '0100', '1000', '1001']: btype = 'ELT' elif typelocprotbin in ['0111', '1011']: btype = 'PLB' elif typelocprotbin == '1110': btype = 'Std Loc Test' elif typelocprotbin == '1111': btype = 'Nat Loc Test' elif typelocprotbin == '1101': if self.bin[41:43] == '00': btype = 'ELT' elif self.bin[41:43] == '01': btype = 'EPIRB' elif self.bin[41:43] == '10': btype = 'PLB' elif self.bin[41:43] == '11': btype = 'RLS Loc Test' else: btype = 'Unknown' self._protocold = { 'pflag': definitions.protocol[self.bin[26]], 'ptype': definitions.locprottype[typelocprotbin], 'serial': '' } self._protocol = ('Protocol Flag (Bit 26) :' + self.bin[26], definitions.protocol[self.bin[26]], 'Location Protocol type (bits 37-40) : ' + typelocprotbin, definitions.locprottype[typelocprotbin], typelocprotbin) ident = ('') #Standard Location protocols if typelocprotbin in definitions.stdloctypes: #['0010','0011','0100','0101','0110','0111','1100','1110'] default = '011111111101111111111' self._loctype = 'Standard Location' self.hex15 = Fcn.bin2hex(self.bin[26:65] + default) self.tablebin.append([ '37-40', str(self.bin[37:41]), 'Location protocol', definitions.locprottype[typelocprotbin] ]) self.tablebin.append( ['26-85', self.bin[26:65] + default, 'Beacon UIN', self.hex15]) latdelta, longdelta, ltoffset, lgoffset = Fcn.latlongresolution( self.bin, 113, 133) lat, declat, latdir, ltminutes = Fcn.latitude( self.bin[65], self.bin[66:73], self.bin[73:75]) lng, declng, lngdir, lgminutes = Fcn.longitude( self.bin[75], self.bin[76:84], self.bin[84:86]) self.courseloc = (declat, declng) # EPIRB MMSI if typelocprotbin == '0010': ident = ('MMSI ID Number: ', str(Fcn.bin2dec(self.bin[41:61])), 'Specific Beacon :', str(Fcn.bin2dec(self.bin[61:65]))) self.tablebin.append([ '41-60', str(self.bin[41:61]), 'MMSI ID No', str(Fcn.bin2dec(self.bin[41:61])) ]) self.tablebin.append([ '61-64', str(self.bin[61:65]), 'Specific beacon No', str(Fcn.bin2dec(self.bin[61:65])) ]) # ELT 24 bit address elif typelocprotbin == '0011': self.tablebin.append([ '41-64', str(self.bin[41:65]), 'Aircraft ID No', '{} ({})'.format(str(Fcn.bin2dec(self.bin[41:65])), str(Fcn.bin2hex(self.bin[41:65]))) ]) # ELT - Aircraft Operator Designator Standard Location Protocol elif typelocprotbin == '0101': self.tablebin.append([ '41-64', str(self.bin[41:65]), 'ELT Operator ID', '{} ELT No:{}'.format( str(Fcn.baudot(self.bin, 41, 55, True)), str(Fcn.bin2dec(self.bin[56:65]))) ]) # PLB, ELT and EBIRB Serial elif typelocprotbin in ['0100', '0110', '0111']: self.tablebin.append([ '37-40', str(self.bin[37:41]), 'Location protocol', 'Serial {}'.format(btype) ]) self.tablebin.append([ '41-50', str(self.bin[41:51]), 'Type approval certificate', str(Fcn.bin2dec(self.bin[41:51])) ]) self.tablebin.append([ '51-64', str(self.bin[51:65]), 'Serial No', str(Fcn.bin2dec(self.bin[51:65])) ]) elif typelocprotbin == '1110': self.tablebin.append([ '41-65', str(self.bin[41:66]), 'No decode identification', definitions.locprottype[typelocprotbin] ]) if self.type != '15 Hex ID': self.tablebin.append([ '65-74', str(self.bin[65:75]), 'Latitude', '{} ({})'.format(lat, declat) ]) self.tablebin.append([ '75-85', str(self.bin[75:86]), 'Longitude', '{} ({})'.format(lng, declng) ]) if self.bin[107:111] == '1101': computed = '107-110 should be 1101. Passed.' else: computed = '107-110 :' + self.bin[ 107:111] + '. Not 1101. Failed' self.fixedbits = computed self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) self.tablebin.append( ['107-110', str(self.bin[107:111]), 'Validity', computed]) self.tablebin.append([ '111', str(self.bin[111]), 'Encoded position', definitions.enc_delta_posflag[self.bin[111]] ]) self.tablebin.append([ '112', str(self.bin[112]), 'Aux device', definitions.homer[self.bin[112]] ]) self.encpos = str(self.bin[111]) self.tablebin.append([ '113-122', str(self.bin[113:123]), 'Latitude offset', ltoffset ]) self.tablebin.append([ '123-132', str(self.bin[123:133]), 'Longitude offset', lgoffset ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) elif self.type == '15 Hex ID': self.tablebin.append(['65-85', default, 'Default bits', '']) # National Location protocols - PLB, ELT and EPIRB elif typelocprotbin in definitions.natloctypes: #['1000','1010','1011','1111']: self._loctype = 'National Location' self.tablebin.append([ '37-40', str(self.bin[37:41]), 'Location protocol', '{} {}'.format(btype, self._loctype) ]) default = '011111110000001111111100000' #59-85 default data 27 bit binary (to construct 15 Hex UIN when no location present) self.hex15 = Fcn.bin2hex(self.bin[26:59] + default) self.tablebin.append( ['26-85', self.bin[26:59] + default, 'Beacon UIN', self.hex15]) ident = ('Serial Number :', str(Fcn.bin2dec(self.bin[41:59]))) self.tablebin.append([ '41-58', str(self.bin[41:59]), 'Serial No', '#{}'.format(str(Fcn.bin2dec(self.bin[41:59]))) ]) latdelta, longdelta, ltmin, ltsec, lgmin, lgsec, ltoffset, lgoffset = ( 0, 0, 0, 0, 0, 0, 0, 0) lat, declat, latdir, ltminutes = Fcn.latitude( self.bin[59], self.bin[60:67], self.bin[67:72]) lng, declng, lngdir, lgminutes = Fcn.longitude( self.bin[72], self.bin[73:81], self.bin[81:86]) self.courseloc = (declat, declng) if self.type != '15 Hex ID': self.tablebin.append([ '59-71', str(self.bin[59:72]), 'Latitude', '{} ({})'.format(lat, declat) ]) self.tablebin.append([ '72-85', str(self.bin[72:86]), 'Longitude', '{} ({})'.format(lng, declng) ]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) if self.bin[107:110] == '110': computed = '107-109 should be 110. Passed.' else: computed = '107-109 :' + self.bin[ 107:110] + '. Not 110. Failed' self.tablebin.append( ['107-109', str(self.bin[107:110]), 'Validity', computed]) self.fixedbits = computed finallat = finallng = 'Not Used' self._locd['encpos'] = definitions.enc_delta_posflag[ self.bin[111]] if self.bin[110] == '0': self._locd[ 'comp'] = 'Value 0: bits 113-132 for national use' latdelta = longdelta = 0 self.tablebin.append([ '110', str(self.bin[110]), 'Location check', self._locd['comp'] ]) self.tablebin.append([ '111', str(self.bin[111]), 'Location source', self._locd['encpos'] ]) self.tablebin.append([ '112', str(self.bin[112]), 'Aux device', definitions.homer[self.bin[112]] ]) self.tablebin.append([ '113-132', str(self.bin[113:133]), 'National use', '' ]) else: latdelta, longdelta, ltoffset, lgoffset = Fcn.latlongresolution( self.bin, 113, 127) self.tablebin.append([ '110', str(self.bin[110]), 'Location check', 'bits 113-126 for location.\n 127-132 for national use' ]) self.tablebin.append([ '111', str(self.bin[111]), 'Location source', definitions.enc_delta_posflag[self.bin[111]] ]) self.encpos = str(self.bin[111]) self.tablebin.append([ '112', str(self.bin[112]), 'Aux device', definitions.homer[self.bin[112]] ]) self.tablebin.append([ '113-119', str(self.bin[113:120]), 'Latitude offset', ltoffset ]) self.tablebin.append([ '120-126', str(self.bin[120:127]), 'Longitude offset', lgoffset ]) self.tablebin.append([ '127-132', str(self.bin[127:133]), 'National use', '' ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) elif self.type == '15 Hex ID': self.tablebin.append(['59-85', default, 'Default bits', '']) # RLS Location Protocol elif typelocprotbin == '1101': default = '0111111110111111111' #67-85 default 19 bit binary (to construct 15 Hex UIN when no location present) self.hex15 = Fcn.bin2hex(self.bin[26:67] + default) self.tablebin.append( ['26-85', self.bin[26:59] + default, 'Beacon UIN', self.hex15]) self._loctype = 'RLS Location' self.tablebin.append([ '37-40', str(self.bin[37:41]), 'Location protocol', '{} {}'.format(btype, self._loctype) ]) tano = str(Fcn.bin2dec(self.bin[43:53])) self.tablebin.append( ['41-42', str(self.bin[41:43]), 'Beacon type', btype]) self.tablebin.append([ '43-52', str(self.bin[43:53]), 'RLS Tac No', '#{}'.format(tano) ]) self.tablebin.append([ '53-66', str(self.bin[53:67]), 'Serial No', '#{}'.format(str(Fcn.bin2dec(self.bin[53:67]))) ]) latdelta, longdelta, ltmin, ltsec, lgmin, lgsec, ltoffset, lgoffset = ( 0, 0, 0, 0, 0, 0, 0, 0) lat, declat, latdir = Fcn.latitudeRLS(self.bin[67], self.bin[68:76]) lng, declng, lngdir = Fcn.longitudeRLS(self.bin[76], self.bin[77:86]) self.courseloc = (declat, declng) if self.type != '15 Hex ID': self.tablebin.append([ '67-75', str(self.bin[67:76]), 'Latitude', '{} ({})'.format(lat, declat) ]) self.tablebin.append([ '76-85', str(self.bin[76:86]), 'Longitude', '{} ({})'.format(lng, declng) ]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) #self.tablebin.append(['107-108',str(self.bin[107:109]),'supplementary','supplementary']) self._locd['encpos'] = definitions.enc_delta_posflag[ self.bin[107]] self.encpos = str(self.bin[107]) self._locd['homer'] = definitions.homer[self.bin[108]] self.tablebin.append([ '107', str(self.bin[107]), 'encoded position source', self._locd['encpos'] ]) self.tablebin.append( ['108', str(self.bin[108]), 'homer', self._locd['homer']]) self.tablebin.append([ '109-114', str(self.bin[109:115]), 'reserved', 'reserved for RLS data' ]) self.tablebin.append([ '109', str(self.bin[109]), 'Process automatically RLM (Type-1)', [ 'Acknowledgement Type-1 not requested and not accepted by this beacon', 'Acknowledgement Type-1 accepted by this beacon' ][int(self.bin[109])] ]) self.tablebin.append([ '110', str(self.bin[110]), 'Process manually RLM (Type-2)', [ 'Manually generated RLM not accepted by this beacon', 'Manually generated RLM accepted by this beacon' ][int(self.bin[110])] ]) self.tablebin.append([ '111', str(self.bin[111]), 'Feedback on RLM Type-1', [ 'Acknowledgement Type-1 not (yet) received by this beacon', 'Acknowledgement Type-1 (automatic acknowledgement) received by this beacon' ][int(self.bin[111])] ]) self.tablebin.append([ '112', str(self.bin[112]), 'Feedback on RLM Type-2', [ 'Acknowledgement Type-2 not (yet) received by this beacon', 'Acknowledgement Type-2 received by this beacon' ][int(self.bin[112])] ]) self.tablebin.append([ '113-114', str(self.bin[113:115]), 'RLS Provider', { '00': 'Spare', '11': 'Spare', '01': 'GALILEO Return Link Service Provider', '10': 'GLONASS Return Link Service Provider' }[str(self.bin[113:115])] ]) finallat = finallng = 'Not Used' latdelta, longdelta, ltoffset, lgoffset = Fcn.latlongresolution( self.bin, 115, 133) self.tablebin.append([ '115-123', str(self.bin[115:124]), 'Latitude offset', ltoffset ]) self.tablebin.append([ '124-132', str(self.bin[124:133]), 'Longitude offset', lgoffset ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) elif self.type == '15 Hex ID': self.tablebin.append(['67-85', default, 'Default bits', '']) # ELT-DT Location Protocol elif typelocprotbin == '1001': default = '0111111110111111111' #67-85 default 19 bit binary (to construct 15 Hex UIN when no location present) self.hex15 = Fcn.bin2hex(self.bin[26:67] + default) self.tablebin.append( ['26-85', self.bin[26:59] + default, 'Beacon UIN', self.hex15]) self._loctype = 'ELT-DT Location' self.tablebin.append([ '37-40', str(self.bin[37:41]), 'Location protocol', '{} {}'.format(btype, self._loctype) ]) self.tablebin.append([ '41-42', str(self.bin[41:43]), 'ELT Type', definitions.eltdt[str(self.bin[41:43])] ]) if str(self.bin[41:43]) == '10': tano = str(Fcn.bin2dec(self.bin[43:53])) self.tablebin.append([ '43-52', str(self.bin[43:53]), 'Tac No', '#{}'.format(tano) ]) self.tablebin.append([ '53-66', str(self.bin[53:67]), 'Serial No', '#{}'.format(str(Fcn.bin2dec(self.bin[53:67]))) ]) elif str(self.bin[41:43]) == '00': self.tablebin.append([ '43-66', str(self.bin[43:67]), 'Aircraft 24 bit address', '#{}'.format(str(Fcn.bin2dec(self.bin[43:67]))) ]) elif str(self.bin[41:43]) == '01': self.tablebin.append([ '43-60', str(self.bin[43:61]), 'AirCraft Operator Designator', Fcn.baudot(self.bin, 43, 61) ]) self.tablebin.append([ '61-66', str(self.bin[61:67]), 'Serial No Assigned by Operator', str(Fcn.bin2dec(self.bin[61:67])) ]) elif str(self.bin[41:43]) == '11': self.tablebin.append([ '43-66', str(self.bin[43:67]), 'ELT(DT) Location Test Protocol', 'reserved' ]) latdelta, longdelta, ltmin, ltsec, lgmin, lgsec, ltoffset, lgoffset = ( 0, 0, 0, 0, 0, 0, 0, 0) lat, declat, latdir = Fcn.latitudeRLS(self.bin[67], self.bin[68:76]) lng, declng, lngdir = Fcn.longitudeRLS(self.bin[76], self.bin[77:86]) self.courseloc = (declat, declng) if self.type != '15 Hex ID': self.tablebin.append([ '67-75', str(self.bin[67:76]), 'Latitude', '{} ({})'.format(lat, declat) ]) self.tablebin.append([ '76-85', str(self.bin[76:86]), 'Longitude', '{} ({})'.format(lng, declng) ]) self.tablebin.append([ '86-106', str(self.bin[86:107]), 'BCH 1', str(self.bch.bch1calc()) ]) means = { '01': 'automatic', '11': 'manual', '00': 'spare', '10': 'spare' } meansbin = str(self.bin[107:109]) self.tablebin.append([ '107-108', meansbin, 'means of activation', means[meansbin] ]) enc_altbin = str(self.bin[109:113]) enc_altstr = 'altitude is between {} and {}'.format( definitions.enc_alt[enc_altbin][0], definitions.enc_alt[enc_altbin][1]) self.tablebin.append( ['109-112', enc_altbin, 'encoded altitude', enc_altstr]) finallat = finallng = 'Not Used' enc_loc_fresh = { '01': 'old', '11': 'current', '00': 'old', '10': 'old' } enc_freshbin = str(self.bin[113:115]) self.tablebin.append([ '113-114', enc_freshbin, 'encoded location freshness', enc_loc_fresh[enc_freshbin] ]) latdelta, longdelta, ltoffset, lgoffset = Fcn.latlongresolution( self.bin, 115, 133) self.tablebin.append([ '115-123', str(self.bin[115:124]), 'Latitude offset', ltoffset ]) self.tablebin.append([ '124-132', str(self.bin[124:133]), 'Longitude offset', lgoffset ]) self.tablebin.append([ '133-144', str(self.bin[133:145]), 'BCH 2', str(self.bch.bch2calc()) ]) elif self.type == '15 Hex ID': self.tablebin.append(['67-85', default, 'Default bits', '']) if Fcn.is_number(declat) and Fcn.is_number(latdelta) and Fcn.is_number( declng) and Fcn.is_number(longdelta): self._loc = True a = self.update_locd((abs(declat) + latdelta), latdir) b = self.update_locd((abs(declng) + longdelta), lngdir) else: self._loc = False a = declat b = declng self.tablebin.append( ['', '', 'Composite location', '{} {}'.format(a, b)]) self.location = (a, b) self._btype = btype self.tac = tano
import decodefunctions as Fcn import random decoded=open('samplehex.csv','w') # generate 154 bit hex codes for n in range(10000): binstr='' for i in range(154): x=random.random() if x<.5: b='0' else: b='1' binstr=binstr+b decoded.write(Fcn.bin2hex(binstr)+'\n') decoded.close() #print binstr,len(binstr),Fcn.bin2hex(binstr)
#calculatedBCH = Sgb.calcBCH(m, 0, 202, 250) #print(calculatedBCH,len(calculatedBCH)) #print(len(mainmsg),len(bchsgb)) #print(calculatedBCH=='010010010010101001001111110001010111101001001001') #data=bytearray(os.urandom(26)) #rebuildmsg='' #for e in range(len(data)): # segment = Fcn.dec2bin(data[e]).zfill(8) # rebuildmsg = rebuildmsg + segment #print(rebuildmsg,len(rebuildmsg)) mainmsg='0001110000111101111010001001111111000011111111100111100011111001110010001111101111000100010010000011101101111001001011110000101110110010001000100011001001110110100101000001010001110100010001011110110110' bchsgb='011001011000001000000101110011100111001101100010' print(mainmsg,bchsgb) calculatedBCH = Sgb.calcBCH(mainmsg, 0, 202, 250) print(calculatedBCH, len(calculatedBCH),bchsgb==calculatedBCH) print(Fcn.bin2hex('00'+mainmsg+calculatedBCH)) print(len(mainmsg)) h='070F7A27F0FF9E3E723EF1120EDE4BC2EC888C9DA5051D117B6658205CE7362' bitflips,newmsg,newbch = msgbits_to_bch(mainmsg,bchsgb) #print(mainmsg) #print(newmsg,len(newmsg)) #print(bchsgb) #print(newbch,len(newbch)) #print(bitflips) #if bitflips==-1: # print('fail') testhex='00039A3D32618658622811F000000000001FFFF004030680258AB06AAA95EB9' m=(Fcn.hextobin(testhex))[2:204] b=(Fcn.hextobin(testhex))[204:] #print(testhex)
bch1='001000100000000101001' bitflips,newpdf,newbch = pdf1_to_bch1(pdf1,bch1) print(pdf1) print(newpdf,len(newpdf)) print print(bch1) print(newbch,len(newbch)) print(bitflips) if bitflips==-1: print('fail') pdf1=(decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[:61] bch1=(decodefunctions.hextobin('D3C4EB28140AA681100A444154C224'))[61:82] #print('93CB0242508F3BC928BCB407180EC6',pdf1,len(pdf1)) #print('bch1',bch1,len(bch1)) pdf1= (decodefunctions.hextobin('9D6940000045DF533687000452A622'))[:61] bch1 = (decodefunctions.hextobin('9D6940000045DF533687000452A622'))[61:82] bch1corrected = decodefunctions.calcbch(pdf1,"1001101101100111100011", 0, 61, 82) pdf2bch2=(decodefunctions.hextobin('9D6940000045DF533687000452A622'))[82:] print(pdf1+bch1,len(pdf1+bch1)) print(bch1corrected) print(pdf1 + bch1 + pdf2bch2, len(pdf1 + bch1+pdf2bch2)) print(decodefunctions.bin2hex((pdf1 + bch1corrected+pdf2bch2)))
if __name__ == "__main__": strhex = input("Hex message: ") errors = [] #errors = bch_recalc(strhex) if errors: print(errors) _pdf1 = (Fcn.hextobin(strhex))[:61] print(len(_pdf1)) _bch1 = (Fcn.hextobin(strhex))[61:82] bitflips1, newpdf1, newbch1 = bch1.pdf1_to_bch1(_pdf1, _bch1) _pdf2 = (Fcn.hextobin(strhex))[82:108] _bch2 = (Fcn.hextobin(strhex))[108:] bitflips2, newpdf2, newbch2 = bch2.pdf2_to_bch2(_pdf2, _bch2) if bitflips1 == -1 or bitflips2 == -1: print('Too many bit errors to correct') elif bitflips1 > 0 or bitflips2 > 0: _newbin = newpdf1 + newbch1 + newpdf2 + newbch2 print(' {} bad pdf1 bit and {} bad pdf2 bit'.format( bitflips1, bitflips2)) print('Corrected Message: {} '.format(Fcn.bin2hex(_newbin))) print(_newbin, len(_newbin), len(newpdf1), len(newbch1), len(newpdf2), len(newbch2)) else: print('bch1 and bch2 encoded match recalculated') print('-' * 50) print(bch1_binarycalc(strhex)) print(bch2_binarycalc(strhex))