Skip to content

Commit 31a4645

Browse files
committed
elbpcom: maintain data as bytes, not a string containing hex
struct now works in bytes. This change is needed as part of the python2-to-python3 transition.
1 parent 9b83e3c commit 31a4645

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

src/hal/utils/elbpcom.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@
5858
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
5959
s.settimeout(options.timeout)
6060

61-
def transact(sdata, quiet=False, response=True):
62-
sdata = "".join(sdata.split()).decode("hex")
61+
def transact(sdata:bytes, quiet=False, response=True):
6362
s.sendto(sdata, (options.sip, options.sport))
6463
if not response: return
6564
try:
6665
data, daddr = s.recvfrom(1280)
67-
if not quiet: print("<", data.encode("hex"))
68-
if not quiet: print(" ", re.sub('[^ -~]', '.', data))
66+
if not quiet: print("<", data.hex())
67+
if not quiet: print(" ", re.sub('[^ -~]', '.', data.decode('utf-7', errors='replace')))
6968
return data
7069
except socket.timeout:
7170
if not quiet: print("! no response")
@@ -80,21 +79,21 @@ def interact():
8079
except KeyboardInterrupt:
8180
pass
8281

83-
def make_read_request(space, info, size, increment, address, nbytes):
82+
def make_read_request(space:int, info:bool, size:int, increment:bool, address:int, nbytes:int) -> bytes:
8483
return struct.pack("<HH",
8584
(1<<14) | (space << 10) | (info << 13)
86-
| (sizemap[size] << 8) | (increment << 7) | (nbytes/size),
85+
| (sizemap[size] << 8) | (increment << 7) | int(nbytes/size),
8786
address)
8887

89-
def make_write_request(space, info, size, increment, address, bytes):
88+
def make_write_request(space:int, info:bool, size:int, increment:bool, address:int, data:bytes) -> bytes:
9089
return struct.pack("<HH",
9190
(1<<15) | (1<<14) | (space << 10) | (info << 13)
92-
| (sizemap[size] << 8) | (increment << 7) | (len(bytes) / size),
93-
address) + bytes
91+
| (sizemap[size] << 8) | (increment << 7) | int(len(data) / size),
92+
address) + data
9493

95-
def optimal_size(space, info, address, nbytes):
94+
def optimal_size(space:int, info:bool, address:int, nbytes:int):
9695
if info: return 2
97-
info = transact(make_read_request(space, True, 2, True, 2, 4).encode("hex"), quiet=True)
96+
info = transact(make_read_request(space, True, 2, True, 2, 4), quiet=True)
9897
if info is None:
9998
raise RuntimeError("Failed to get information about memory space %d" % space)
10099
memsizes, memranges = struct.unpack("<HH", info)
@@ -112,17 +111,15 @@ def optimal_size(space, info, address, nbytes):
112111
if options.address is None: raise SystemExit("--read must specify --address")
113112
size = optimal_size(options.space, options.info, options.address, options.read if options.increment else 0)
114113
command = make_read_request(options.space, options.info, size, options.increment, options.address, options.read)
115-
command = command.encode("hex")
116-
print(">", command)
114+
print(">", command.hex())
117115
transact(command)
118116

119117
elif options.write:
120118
if options.address is None: raise SystemExit("--write must specify --address")
121-
write = options.write.decode("hex")
119+
write = bytes.fromhex(options.write)
122120
size = optimal_size(options.space, options.info, options.address, len(write) if options.increment else 0)
123121
command = make_write_request(options.space, options.info, size, options.increment, options.address, write)
124-
command = command.encode("hex")
125-
print(">", command)
122+
print(">", command.hex())
126123
transact(command, response=False)
127124

128125
elif args:

0 commit comments

Comments
 (0)