def read_tags(self): ''' Send request for tags and parse response. ''' tag_list = [] # prepare frame frm = [0xFF, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00, 0x00] ba = bytearray(frm) c = crc.calc_crc(frm, len(frm)) ba.append((c & 0xFF00) >> 8) ba.append(c & 0xFF) # send it to reader self.send(ba) # get ack data = self.rec(2) if data[3] != 0x61: return tag_list # get tags md_flg = 1 while md_flg == 1: data = self.rec(2) if data[3] != 0xA0: break md_flg = data[4] & (1 << 0) cnt = 0 idx = 9 while cnt < data[1]: if 0x1F == data[idx]: ant = data[idx + 1] & 0x0F count = data[idx + 2] time = "" for i in range(4): time = "%02X" % data[idx + 3 + i] + time time = int(time, 16) rssi = data[idx + 7] if rssi > 0x7F: rssi -= 0x100 length = data[idx + 9] epc_crc = 0 for i in range(2): epc_crc <<= 8 epc_crc |= data[idx + 10 + i] epc = "" for i in range(length): epc += "%02X" % data[idx + 12 + i] cnt += 12 + length idx += 12 + length tag = Tag(ant, count, time, rssi, length, epc_crc, epc) tag_list.append(tag) # print("Tag: ", end='') # print("%d %d %s %d %d 0x%02X %s" % (ant, count, time, rssi, length, epc_crc, epc) ) return tag_list
def cmd(self, opc, context=None): # build header frm = [0xFF, 0x00, 0x00, opc, 0x00, 0x00, 0x00, 0x00] # calculate and add crc to the frame ba = bytearray(frm) # call opcode's callback for data field if TxOpcClb[opc] is not None: data = TxOpcClb[opc](context) else: data = b'' ba.extend(data) ba[1] = len(data) c = crc.calc_crc(ba, len(ba)) ba.append((c & 0xFF00) >> 8) ba.append(c & 0xFF) # send the frame self.send(ba) # if one of hidden commands - return if opc == OP.Rainbow: return [] # get response resp = self.rec(2) # check SOF if resp[0] != 0xFF: return False # check CRC cc = crc.calc_crc(resp, len(resp) - 2) ccs = '%04X' % cc ccb = binascii.a2b_hex(ccs) if ccb != resp[-2:]: return False # check opcode if opc != resp[3]: return False # run Rx Opcode if len(resp) > 11 and RxOpcClb[resp[3]] is not None: return RxOpcClb[resp[3]](resp[9:-2]) else: return []
def test_upload(self): log = logging.getLogger("test_program_upload") self._start() # erase entry self.system.comm.put_frame(EraseBootTableEntry([0, 1, 2])) # wait for response f = self.system.comm.get_frame(20, filter_type=EntryEraseSuccessFrame) self.assertIsInstance(f, EntryEraseSuccessFrame) # upload entries 0...n program_path = config['BINARIES_DIR'] + '/fm_terminal.bin' with open(program_path, 'rb') as f: content = f.read() content = pad(content, multiply_of=128, pad_with=0x1A) crc = calc_crc(content) length = len(content) parts = int(ceil(length / float(WriteProgramPart.MAX_PART_SIZE))) for i in xrange(0, length, WriteProgramPart.MAX_PART_SIZE): part = content[i:i + WriteProgramPart.MAX_PART_SIZE] self.system.comm.put_frame( WriteProgramPart(entries=[0, 1, 2], offset=i, content=part)) log.info("Total parts {}\nlength: {}\nCRC: {:X}".format( parts, length, crc)) f = [ self.system.comm.get_frame( 20, filter_type=EntryProgramPartWriteSuccess) for _ in xrange(0, parts) ] for i in f: self.assertIsInstance(i, EntryProgramPartWriteSuccess) # finalize entry name = 'Uploaded2' self.system.comm.put_frame( FinalizeProgramEntry([0, 1, 2], length, crc, name)) f = self.system.comm.get_frame(20, filter_type=EntryFinalizeSuccess) self.assertIsInstance(f, EntryFinalizeSuccess)
def write_sel_tag(self, mem, pwd, sel_data_len, epc_len, sel_data_addr, sel_data, new_epc): # prepare frame frm = [0xFF, 11 + 24, 0x00, 0x6A, 0x00, 0x00, 0x00, 0x00] data = [0x01, 0x00, 0x00, 0x00, 0x00, 0x60, 0x0C, 0x00, 0x00, 0x00, 0x00] frm += data for i in range(0, 12): tmp = sel_data[i * 2:i * 2 + 2] val = int(tmp, 16) frm.append(val) for i in range(0, 12): tmp = new_epc[i * 2:i * 2 + 2] val = int(tmp, 16) frm.append(val) ba = bytearray(frm) c = crc.calc_crc(frm, len(frm)) ba.append((c & 0xFF00) >> 8) ba.append(c & 0xFF) self.send(ba) resp = self.rec(10) resp = self.rec(10)
def check_integrity(data: List[int]): return data[-1] == crc.calc_crc(data[0:-1])
print 'Ignoring {}'.format(frame) except Empty: pass return None file = sys.argv[1] slots = [int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])] description = sys.argv[5] with open(file, 'rb') as f: program_data = f.read() program_data = pad(program_data, multiply_of=128, pad_with=0x1A) crc = calc_crc(program_data) length = len(program_data) parts = int(ceil(length / float(WriteProgramPart.MAX_PART_SIZE))) print 'Will upload {} bytes of program (CRC: {:4X}), {} parts into slots {}'.format( length, crc, parts, slots) print 'Erasing boot slots' sender.send(EraseBootTableEntry(slots)) response = wait_for_frame(EntryEraseSuccessFrame, 50) if response is None: print 'Failed to erase' sys.exit(1) print 'Boot slots erased'
def checksum(data: List[int]) -> int: return crc.calc_crc(data) & 255
assert 0 <= i <= 5, "Incorrect boot slot" if boot_slots != [3, 4, 5]: print "================ WARNING ==============" print "=== Boot slots not on fallback! ===" print "=======================================" print "" return boot_slots args = parse_args() PART_SIZE = telecommand.WriteProgramPart.MAX_PART_SIZE binary_data_org = open(args.file, 'rb').read() binary_data = copy.copy(binary_data_org) binary_data = list(pad(binary_data, multiply_of=128, pad_with=0x1A)) crc = calc_crc(binary_data) boot_slots = process_boot_slots(args.boot_slots) chunks = int(math.ceil(len(binary_data) * 1.0 / PART_SIZE)) print "Generating:" print " - file {}".format(args.file) print " - length {} bytes, {} chunks".format(len(binary_data), chunks) print " - bootslots {}".format(boot_slots) print " - Sample: tc.WriteProgramPart(" + str( boot_slots) + ", 0xA5, [0x00, 0x01])" print " - CRC: {:04X}".format(crc) output_file = open(args.output, 'w') output_file.write('tasks = [\n')