def acquire_bus_pkt(self): # returns a full bus packet buf = [] # Read 1 byte from SPI device until the sync marker is found sync_index = 0 while (sync_index < (len(self.ccsds_sync))): b = self.read_data(1) if (b[0] == self.ccsds_sync[sync_index]): # buf.append(b) sync_index += 1 elif (b[0] == self.ccsds_sync[0]): # buf = [b] sync_index = 1 else: sync_index = 0 # Read 6 CCSDS header bytes buf = self.read_data(CCSDS_HEADER_LEN) pkt_len = (buf[PKT_LEN_INDEX] << 8) | buf[PKT_LEN_INDEX + 1] + 1 # Read payload data bytes and crc bytes buf.extend(self.read_data(pkt_len)) # Check crc crc_index = CCSDS_HEADER_LEN + pkt_len - 2 crc = (buf[crc_index] << 8) | buf[crc_index + 1] # Calculate CRC over the entire packet crc_check = crc16.calc(buf[:crc_index]) if (crc == crc_check): self.bus_pkts_buffer.append(buf) else: print('CRC error')
def create_from_code(cls, code: str): code = code.strip() payload = CodeSection(code=code) if not payload.tag_type == Tag.payload: raise not_bank_slip("invalid `payload` code section") country_code = CodeSection(code=payload.rest) if not country_code.tag_type == Tag.country_code: raise not_bank_slip("invalid `country code` code section") crc = CodeSection(code=country_code.rest) if not crc.tag_type == Tag.crc: raise not_bank_slip("invalid `crc` code section") if not crc.rest is None: raise not_bank_slip("unexpected extend code section") data_part = code[: -crc.length] calc_crc = Crc16CcittFalse.calchex( data_part.encode(encoding="ascii") ).upper() if not calc_crc == crc.data: raise not_bank_slip("calculated crc and provided crc are unmatched") return cls( payload=QrPayload.create_from_code(payload.data), country_code=country_code.data, crc=crc.data, )
def ck_g1f7_14c367(blocks): c = Crc16CcittFalse.calc(blocks[0][2][0x100:]) c1 = c & 0xff c2 = c >> 8 print("\t[+] Checksum 0x{:02x}{:02x}. ".format(c1, c2), end='') if c1 == blocks[0][2][0xa4] and c2 == blocks[0][2][0xa5]: print("Correct!") else: blocks[0][2][0xa4] = c1 blocks[0][2][0xa5] = c2 print("Fixed!")
def create_command(command, data): temp = bytearray.fromhex('5669564F746563683200' + command + data) crc = Crc16CcittFalse() crc.process(temp) crchex = crc.finalhex() i = len(crchex) - 1 cheksum = '' while i > 0: cheksum = cheksum + crchex[i - 1] + crchex[i] i -= 2 print(temp) print(cheksum) return temp + cheksum
def _check_rc_16(self): """ checksum packet CRC-16\n _check_rc_16("00, ff")\n ARG: String of bytes in hex. """ sources = self.telegramm r_c = ''.join(sources[len(sources) - 2:]) self.telegramm_decode["RC"] = r_c body_packet = bytearray.fromhex(''.join(sources[:len(sources) - 2])) get_check_rc = Crc16CcittFalse.calchex(body_packet) if r_c == get_check_rc.upper(): return True else: print("Wrong checksum CRC-16 !!!") return False
def preparePCMFrame(frame, offset): pcm = [] for j in range(offset + 1, int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)), 2): line = frame[j, :][:, 1] linePCM = [] for i in range(0, 128): if line[int(i * pixelSize + start + 2)]: linePCM.append(1) else: linePCM.append(0) dataBits = np.split(np.array(linePCM[:112]), 8) crcControlData = np.packbits(dataBits) data = [] for x in dataBits: data.append(np.packbits(x)) crc = np.packbits(linePCM[112:]) crc = crc[0] << 8 | crc[1] #crc = linePCM[14:].view(np.uint16) crcControlValue = Crc16CcittFalse.calc(bytearray(crcControlData)) if crc != crcControlValue: if crc == 0: continue crc = 0 print("Error CRC: %d, %d, %d" % (j, crc, crcControlValue)) #todo .view(np.uint16) linePCM = [] linePCM.append(np.uint16(data[0][0]) << 8 | data[0][1] & 0xffff) linePCM.append(np.uint16(data[1][0]) << 8 | data[1][1] & 0xffff) linePCM.append(np.uint16(data[2][0]) << 8 | data[2][1] & 0xffff) linePCM.append(np.uint16(data[3][0]) << 8 | data[3][1] & 0xffff) linePCM.append(np.uint16(data[4][0]) << 8 | data[4][1] & 0xffff) linePCM.append(np.uint16(data[5][0]) << 8 | data[5][1] & 0xffff) linePCM.append(np.uint16(data[6][0]) << 8 | data[6][1] & 0xffff) linePCM.append(np.uint16(data[7][0]) << 8 | data[7][1] & 0xffff) linePCM.append(crc) pcm.append(linePCM) #for line in range(0, len(pcm)): # print(pcm[line]); return (pcm)
def acquire_bus_pkt(self): # returns a full bus packet without the sync marker buf = [] # Read 1 byte from SPI device until the sync marker is found sync_index = 0 while (sync_index < (len(self.ccsds_sync))): b = self.read_data(1) if (b[0] == self.ccsds_sync[sync_index]): # buf.append(b) sync_index += 1 elif (b[0] == self.ccsds_sync[0]): # buf = [b] sync_index = 1 else: sync_index = 0 #print('found sync!') # Read 6 CCSDS header bytes buf = self.read_data(CCSDS_HEADER_LEN) apid = buf[APID_INDEX] pkt_len = (buf[PKT_LEN_INDEX] << 8) | buf[PKT_LEN_INDEX + 1] + 1 # Read payload data bytes and crc bytes pkt = self.read_data(pkt_len) # Assuming crc is included in the packet length buf.extend(pkt) # Check crc crc_index = CCSDS_HEADER_LEN + pkt_len - 2 crc = (buf[crc_index] << 8) | buf[crc_index + 1] #Calculate CRC over the entire packet crcinst = crc16() crc_check = crc16.calc(buf[:crc_index]) if (crc == crc_check): self.bus_pkts_buffer.append(buf) else: # print('crc did not work') err_pkt = TxPacket() err_pkt_pl = struct.pack('!H%dB' % len(buf), len(buf), *buf) raw_err_pkt = err_pkt.encode(ERR_DPKT_CRC_INVALID, err_pkt_pl) self.tx_socket.send(raw_err_pkt)
def create_command(self, command, data='0000'): """ Конструктор комманд для вивы command - команда по дукоментации data - данные, для команд где неиспользуется '0000', длинна подаваемых данных считается отдельно :return команду + CRC """ if data != '0000': temp0 = hex(len(data) // 2)[2::].upper().zfill(4) data = temp0 + data temp1 = ('5669564F746563683200' + command + data) crc = Crc16CcittFalse() crc.process(bytearray.fromhex(temp1)) crchex = crc.finalhex() i = len(crchex) - 1 cheksum = '' while i > 0: cheksum = cheksum + crchex[i - 1] + crchex[i] i -= 2 return temp1 + cheksum
def create_pkt(data): payload_len = 6 + len(data) + trash_len + 2 buf = bytearray() buf.append(0x02) buf.append(0x00) buf.append(0x00) buf.append(0x00) buf.append(((payload_len - 1) >> 8) & 0xFF) buf.append((payload_len - 1) & 0xFF) buf.extend(bytearray([0] * 6)) buf.extend(data) buf.extend(bytearray([0] * trash_len)) crc = crc16.calc(buf) pkt = bytearray() pkt.extend(sync) pkt.extend(buf) pkt.append((crc >> 8) & 0xFF) pkt.append(crc & 0xFF) return pkt
def echo_pkt(self): try: buf = self.bus_pkts_buffer[0] except IndexError as e: # Empty buffer, but that's ok return # Change APID buf[0] = 0x3 buf[1] = 0xFF # Update CRC crc_index = len(buf) - 2 crc_new = crc16.calc(buf[:crc_index]) buf[crc_index] = crc_new >> 8 buf[crc_index + 1] = crc_new & 0xFF # Add sync marker and send pkt = [] pkt.extend(self.ccsds_sync) pkt.extend(buf) self.spi.write(bytearray(pkt))
def handle_tx_pkts(self): try: raw_ipc_pkt = self.ipc_pkts_buffer.pop(0) except IndexError as e: # Empty buffer, but that's ok return ipc_pkt = TxPacket() apid, pkt_data = ipc_pkt.decode(raw_ipc_pkt) # print('apid: ', apid) # print('pkt_data: ', pkt_data) # 0b00 - continuation, 0b01 - first of group, 0b10 - last of group, 0b11 - standalone seq_cnt = 0 seq_flag = 0b01 while (len(pkt_data) > (BUS_DATA_LEN)): sync = [] sync.append(0x35) sync.append(0x2E) sync.append(0xF8) sync.append(0x53) pkt = [] pkt.append((apid >> 8) & 0b00000111) pkt.append(apid & 0xFF) pkt.append((seq_flag << 6) | ((seq_cnt >> 8) & 0b00111111)) pkt.append(seq_cnt & 0xFF) pkt.append(((BUS_DATA_LEN + 1) >> 8) & 0xFF) pkt.append((BUS_DATA_LEN + 1) & 0xFF) pkt.extend(bytearray(pkt_data[:BUS_DATA_LEN])) crc = crc16.calc(pkt) pkt.extend([crc >> 8, crc & 0xFF]) bus_tx_pkt = [] bus_tx_pkt.extend(sync) bus_tx_pkt.extend(pkt) # print('bus_tx_pkt: ', bus_tx_pkt) #for debug self.bus_pkts_buffer.append(bus_tx_pkt) del pkt_data[:BUS_DATA_LEN] seq_cnt += 1 seq_flag = 0b00 #Last or only packet if (seq_flag == 0b01): seq_flag = 0b11 else: seq_flag = 0b10 ###Start Revised Packet Definition - Tested and Works### sync = [] sync.append(0x35) sync.append(0x2E) sync.append(0xF8) sync.append(0x53) pkt = [] pkt.append((apid >> 8) & 0b00000111) pkt.append(apid & 0xFF) pkt.append((seq_flag << 6) | ((seq_cnt >> 8) & 0b00111111)) pkt.append(seq_cnt & 0xFF) pkt.append( ((len(pkt_data) + 1) >> 8) & 0xFF) #include length of CRC = 2 pkt.append((len(pkt_data) + 1) & 0xFF) #include length of CRC = 2 pkt.extend(bytearray(pkt_data[:BUS_DATA_LEN])) crc = crc16.calc( pkt ) #do not include sync bytes in this calculation (hence the separation of sync and pkt) pkt.extend([crc >> 8, crc & 0xFF]) bus_tx_pkt = [] bus_tx_pkt.extend(sync) bus_tx_pkt.extend(pkt) # print('bus_tx_pkt: ', bus_tx_pkt) #for debug ###End Revised Packet Definition### self.bus_pkts_buffer.append(bus_tx_pkt)
def get_packet_crc(data): crc = hex(Crc16CcittFalse.calc(data[18:510])) # CCITT False crc = crc[4] + crc[5] + crc[2] + crc[3] # revers due to HARD realization return bytes.fromhex( crc) == data[510:512], crc, data[510:512], data[18:510]
def check_header_crc(data): # crc = Crc16Cdma2000.calc(data[8:14]) #CDMA2000 crc = hex(Crc16CcittFalse.calc(data[8:14])) # CCITT False crc = crc[4] + crc[5] + crc[2] + crc[3] # revers due to HARD realization return bytes.fromhex(crc) == data[14:16]
e += "{{ 0x{:08x}, 0x{:08x} }},".format(b[0], b[1]) if args.erase_memory: for n in args.erase_memory.split(','): a = n.split(':')[0] b = n.split(':')[1] e += "{{ {}, {} }},".format(a, b) if e: body += "erase = {{ {} }};".format(e[:-1]) header = header.format(args.sw, args.type, args.can, args.ecu, args.frame_format, body) if args.fix_checksum: fix_checksum(args.sw, blocks) print("\n[ ] Writing {} ...".format(args.out), end='') with open(args.out, 'wb') as f: f.write(header.encode("ASCII")) for b in blocks: crc = Crc16CcittFalse.calc(b[2]) print("\n\t[ ] 0x{:x} bytes block (CRC 0x{:04x}) at 0x{:08x}... ". format(b[1], crc, b[0]), end='') f.write(pack('>II', b[0], b[1])) f.write(b[2]) f.write(pack('>H', crc)) print("OK\r\t[+", end='') print('\n[+] Done')