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 bch1_binarycalc(inputhex): try: bin = Fcn.hextobin(inputhex) except TypeError as err: return [inputhex + ' Is not a valid hex'] result = '' if len(inputhex) == 36: strhex = inputhex[6:] bin = bin[24:] result = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82) elif len(inputhex) == 30: strhex = inputhex result = Fcn.calcbch(bin, "1001101101100111100011", 0, 61, 82) elif len(inputhex) == 63: # SGB recalc bch1 strhex = inputhex result = Fcn2.calcBCH(bin[2:], 0, 202, 250) else: result = 'Invalid Input Hex length of ' + str( len(inputhex)) + '.' + ' Valid length of FGB 30/36 or 63 for SGB' return result
def bch2_binarycalc(inputhex): try: bin = Fcn.hextobin(inputhex) except TypeError as err: return [inputhex + ' Is not a valid hex'] result = '' if len(inputhex) == 36: strhex = inputhex[6:] bin = bin[24:] result = Fcn.calcbch(bin, '1010100111001', 82, 108, 120) elif len(inputhex) == 30: strhex = inputhex result = Fcn.calcbch(bin, '1010100111001', 82, 108, 120) elif len(inputhex) == 63: return "" else: result = 'Invalid Input Hex length of ' + str( len(inputhex)) + '.' + ' Valid lengths of FGB message are 30 or 36' return result
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 __init__(self, testbin, mtype): bch1 = bch2 = bch1error = bch2error = 'na' self.complete = '0' if mtype in [ 'Short Msg', 'Long Msg no Framesynch', 'Long Msg with Framesynch' ]: bch1 = Fcn.calcbch(testbin, "1001101101100111100011", 25, 86, 107) bch1error = self.errors(testbin[86:107], bch1) if mtype in ['Long Msg no Framesynch', 'Long Msg with Framesynch']: bch2 = Fcn.calcbch(testbin, '1010100111001', 107, 133, 145) bch2error = self.errors(testbin[133:145], bch2) if bch2error == 0: self.complete = '1' self.bch = (bch1, bch2, testbin[86:107], testbin[133:145], bch1error, bch2error)
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 samplepdf2(f, r): for n in range(r): pdf2 = randombinary(26).zfill(26) bch2 = Fcn.calcbch('0' * 107 + pdf2, '1010100111001', 107, 133, 145) + '0000' bch = bchlib.BCH(67, 2) data = bytearray(bch1correct.bitstring_to_bytes(pdf2)) ecc = bch.encode(data) bchstring = (Fcn.dec2bin(ecc[0]).zfill(8) + Fcn.dec2bin(ecc[1]).zfill(8))[:12] + '0000' e = random.sample(range(0, 38), 3) scramble = list(pdf2 + bch2) for i in e: scramble[i] = str(int(not int(scramble[i]))) corrupt = ''.join(scramble) ecc_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[26:])) data_provided = bytearray(bch1correct.bitstring_to_bytes(corrupt[:26])) ecc_c = bch.encode(data_provided) print(ecc_c == ecc_provided, ecc, ecc_provided) packet = bytearray(bch1correct.bitstring_to_bytes( corrupt[:26])) + bytearray( bch1correct.bitstring_to_bytes(corrupt[26:])) data, ecc = packet[:-bch.ecc_bytes], packet[-bch.ecc_bytes:] bitflips = bch.decode_inplace(data, ecc) newdata = Fcn.dec2bin(data[0]).zfill(2) for e in data[1:]: binchar = Fcn.dec2bin(e).zfill(8) newdata = newdata + binchar correctedbch2 = '' # decodefunctions.dec2bin(ecc[0]) for e in ecc: binchar = Fcn.dec2bin(e).zfill(8) correctedbch2 = correctedbch2 + binchar correctedbch2 = correctedbch2[:-4] bch2 = bch2[:-4] bchstring = bchstring[:-4] f.writelines([ '{},{},{},{},{},{},{},{}'.format( pdf2 + bch2, bch2 == bchstring, corrupt, len(pdf2 + bch2), corrupt == pdf2 + bch2, newdata + correctedbch2, newdata + correctedbch2 == pdf2 + bch2, bitflips), '\n' ])
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)))
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)