python code to parse multiple replies
This commit is contained in:
parent
9581c0bed7
commit
b9d68bde1e
|
@ -40,15 +40,19 @@ class zcash_fpga:
|
||||||
def reset_fpga(self):
|
def reset_fpga(self):
|
||||||
self.s.write(self.codecs.decode('0800000000000000', 'hex'))
|
self.s.write(self.codecs.decode('0800000000000000', 'hex'))
|
||||||
# Parse reply - should be reset
|
# Parse reply - should be reset
|
||||||
res = self.get_reply()
|
res = self.get_reply()[0]
|
||||||
if (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['RESET_FPGA_RPL']):
|
if (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['RESET_FPGA_RPL']):
|
||||||
print("ERROR: Reply type was not RESET_FPGA_RPL")
|
print("ERROR: Reply type was not RESET_FPGA_RPL")
|
||||||
|
|
||||||
def get_reply(self):
|
def get_reply(self):
|
||||||
res = self.s.read(1024)
|
res = self.s.read(1024)
|
||||||
if len(res) > 0:
|
print(res)
|
||||||
self.print_reply(res)
|
msg_list = self.parse_reply(self, res)
|
||||||
return res
|
print(msg_list)
|
||||||
|
if len(msg_list) > 0:
|
||||||
|
for msg in msg_list:
|
||||||
|
self.print_reply(msg)
|
||||||
|
return msg_list
|
||||||
else:
|
else:
|
||||||
print ("INFO: No reply received")
|
print ("INFO: No reply received")
|
||||||
return None
|
return None
|
||||||
|
@ -65,7 +69,7 @@ class zcash_fpga:
|
||||||
cmd = "".join(reversed([cmd[i:i+2] for i in range(0, len(cmd), 2)]))
|
cmd = "".join(reversed([cmd[i:i+2] for i in range(0, len(cmd), 2)]))
|
||||||
|
|
||||||
self.s.write(self.codecs.decode(cmd, 'hex'))
|
self.s.write(self.codecs.decode(cmd, 'hex'))
|
||||||
res = self.get_reply()
|
res = self.get_reply()[0] # Just look at the first reply
|
||||||
if res is not None and (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']):
|
if res is not None and (self.struct.unpack('<I', res[4:8])[0] != self.fpga_msg_type_dict['VERIFY_SECP256K1_SIG_RPL']):
|
||||||
print("ERROR: Reply type was not VERIFY_SECP256K1_SIG_RPL")
|
print("ERROR: Reply type was not VERIFY_SECP256K1_SIG_RPL")
|
||||||
return False
|
return False
|
||||||
|
@ -82,13 +86,14 @@ class zcash_fpga:
|
||||||
self.s.close()
|
self.s.close()
|
||||||
print("Closed...")
|
print("Closed...")
|
||||||
|
|
||||||
def print_reply(self, msg):
|
def parse_reply(self, msg, msg_list = []):
|
||||||
if (len(msg) < 8):
|
if (len(msg) < 8):
|
||||||
print("ERROR: Message too small")
|
return msg_list
|
||||||
return None
|
|
||||||
length = (self.struct.unpack('<I', msg[0:4])[0])
|
length = (self.struct.unpack('<I', msg[0:4])[0])
|
||||||
if (len(msg) != length):
|
msg_list.append(msg[0:length])
|
||||||
print("ERROR: Message length mismatch")
|
self.parse_reply(msg[length:len(msg)], msg_list)
|
||||||
|
|
||||||
|
def print_reply(self, msg):
|
||||||
cmd = (self.struct.unpack('<I', msg[4:8])[0])
|
cmd = (self.struct.unpack('<I', msg[4:8])[0])
|
||||||
if (cmd not in self.fpga_msg_dict):
|
if (cmd not in self.fpga_msg_dict):
|
||||||
print("ERROR: Unknown message type:", cmd)
|
print("ERROR: Unknown message type:", cmd)
|
||||||
|
|
Loading…
Reference in New Issue