cleanups for py3 migration

legit-fork
informatic 2019-10-18 16:11:14 +02:00
parent c6168dd727
commit 20417bfd6b
6 changed files with 97 additions and 95 deletions

View File

@ -61,6 +61,7 @@ class BLELink(BaseLink):
def scan(self): def scan(self):
res = [] res = []
self._adapter.reset()
devices = self._adapter.scan(timeout=SCAN_TIMEOUT) devices = self._adapter.scan(timeout=SCAN_TIMEOUT)
for dev in devices: for dev in devices:
if dev['name'] and dev['name'].startswith((u'MISc', u'NBSc', u'JP2', u'Seg')): if dev['name'] and dev['name'].startswith((u'MISc', u'NBSc', u'JP2', u'Seg')):
@ -91,13 +92,13 @@ class BLELink(BaseLink):
except queue.Empty: except queue.Empty:
raise LinkTimeoutException raise LinkTimeoutException
if self.dump: if self.dump:
print '<', hexlify(data).upper() print('<', hexlify(data).upper())
return data return data
def write(self, data): def write(self, data):
if self.dump: if self.dump:
print '>', hexlify(data).upper() print('>', hexlify(data).upper())
size = len(data) size = len(data)
ofs = 0 ofs = 0
while size: while size:

View File

@ -48,13 +48,13 @@ class SerialLink(BaseLink):
if len(data)<size: if len(data)<size:
raise LinkTimeoutException raise LinkTimeoutException
if self.dump: if self.dump:
print "<", hexlify(data).upper() print("<", hexlify(data).upper())
return data return data
def write(self, data): def write(self, data):
if self.dump: if self.dump:
print ">", hexlify(data).upper() print(">", hexlify(data).upper())
self.com.write(data) self.com.write(data)

View File

@ -44,7 +44,7 @@ class TCPLink(BaseLink):
p = port.partition(':') p = port.partition(':')
host = p[0] host = p[0]
port = int(p[2], 10) port = int(p[2], 10)
print host, port print(host, port)
try: try:
self.sock.connect((host, port)) self.sock.connect((host, port))
except socket.timeout: except socket.timeout:
@ -61,13 +61,13 @@ class TCPLink(BaseLink):
def read(self, size): def read(self, size):
data = recvall(self.sock, size) data = recvall(self.sock, size)
if data and self.dump: if data and self.dump:
print "<", hexlify(data).upper() print("<", hexlify(data).upper())
return data return data
def write(self, data): def write(self, data):
if self.dump: if self.dump:
print ">", hexlify(data).upper() print(">", hexlify(data).upper())
size = len(data) size = len(data)
ofs = 0 ofs = 0
while size: while size:

View File

@ -3,7 +3,7 @@
def checksum(data): def checksum(data):
s = 0 s = 0
for c in data: for c in data:
s += ord(c) s += c
return (s & 0xFFFF) ^ 0xFFFF return (s & 0xFFFF) ^ 0xFFFF

View File

@ -8,6 +8,7 @@ class BasePacket(object):
self.cmd = cmd self.cmd = cmd
self.arg = arg self.arg = arg
self.data = data self.data = data
print(self.data)
def __str__(self): def __str__(self):
return "%s->%s: %02X @%02X %s" % (BT.GetDeviceName(self.src), BT.GetDeviceName(self.dst), self.cmd, self.arg, hexlify(self.data).upper()) return "%s->%s: %02X @%02X %s" % (BT.GetDeviceName(self.src), BT.GetDeviceName(self.dst), self.cmd, self.arg, hexlify(self.data).upper())

View File

@ -60,13 +60,13 @@ class XiaomiTransport(BT):
while True: while True:
while True: while True:
c = self.link.read(1) c = self.link.read(1)
if c=="\x55": if c==b"\x55":
break break
while True: while True:
c = self.link.read(1) c = self.link.read(1)
if c=="\xAA": if c==b"\xAA":
return True return True
if c!="\x55": if c!=b"\x55":
break # start waiting 55 again, else - this is 55, so wait for AA break # start waiting 55 again, else - this is 55, so wait for AA
@ -74,21 +74,21 @@ class XiaomiTransport(BT):
self._wait_pre() self._wait_pre()
pkt = self.link.read(1) pkt = self.link.read(1)
l = ord(pkt)+3 l = ord(pkt)+3
for i in xrange(l): for i in range(l):
pkt += self.link.read(1) pkt.extend(self.link.read(1))
ck_calc = checksum(pkt[0:-2]) ck_calc = checksum(pkt[0:-2])
ck_pkt = unpack("<H", pkt[-2:])[0] ck_pkt = unpack("<H", pkt[-2:])[0]
if ck_pkt!=ck_calc: if ck_pkt!=ck_calc:
print "Checksum mismatch !" print("Checksum mismatch !")
return None return None
sa, da = self._split_addr(ord(pkt[1])) sa, da = self._split_addr(pkt[1])
return BasePacket(sa, da, ord(pkt[2]), ord(pkt[3]), pkt[4:-2]) # sa, da, cmd, arg, data return BasePacket(sa, da, pkt[2], pkt[3], pkt[4:-2]) # sa, da, cmd, arg, data
def send(self, packet): def send(self, packet):
dev = self._make_addr(packet.src, packet.dst) dev = self._make_addr(packet.src, packet.dst)
pkt = pack("<BBBB", len(packet.data)+2, dev, packet.cmd, packet.arg)+packet.data pkt = pack("<BBBB", len(packet.data)+2, dev, packet.cmd, packet.arg)+packet.data
pkt = "\x55\xAA" + pkt + pack("<H", checksum(pkt)) pkt = b"\x55\xAA" + pkt + pack("<H", checksum(pkt))
self.link.write(pkt) self.link.write(pkt)