cleanups for py3 migration
parent
c6168dd727
commit
20417bfd6b
|
@ -61,6 +61,7 @@ class BLELink(BaseLink):
|
|||
|
||||
def scan(self):
|
||||
res = []
|
||||
self._adapter.reset()
|
||||
devices = self._adapter.scan(timeout=SCAN_TIMEOUT)
|
||||
for dev in devices:
|
||||
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:
|
||||
raise LinkTimeoutException
|
||||
if self.dump:
|
||||
print '<', hexlify(data).upper()
|
||||
print('<', hexlify(data).upper())
|
||||
return data
|
||||
|
||||
|
||||
def write(self, data):
|
||||
if self.dump:
|
||||
print '>', hexlify(data).upper()
|
||||
print('>', hexlify(data).upper())
|
||||
size = len(data)
|
||||
ofs = 0
|
||||
while size:
|
||||
|
|
|
@ -48,13 +48,13 @@ class SerialLink(BaseLink):
|
|||
if len(data)<size:
|
||||
raise LinkTimeoutException
|
||||
if self.dump:
|
||||
print "<", hexlify(data).upper()
|
||||
print("<", hexlify(data).upper())
|
||||
return data
|
||||
|
||||
|
||||
def write(self, data):
|
||||
if self.dump:
|
||||
print ">", hexlify(data).upper()
|
||||
print(">", hexlify(data).upper())
|
||||
self.com.write(data)
|
||||
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ class TCPLink(BaseLink):
|
|||
p = port.partition(':')
|
||||
host = p[0]
|
||||
port = int(p[2], 10)
|
||||
print host, port
|
||||
print(host, port)
|
||||
try:
|
||||
self.sock.connect((host, port))
|
||||
except socket.timeout:
|
||||
|
@ -61,13 +61,13 @@ class TCPLink(BaseLink):
|
|||
def read(self, size):
|
||||
data = recvall(self.sock, size)
|
||||
if data and self.dump:
|
||||
print "<", hexlify(data).upper()
|
||||
print("<", hexlify(data).upper())
|
||||
return data
|
||||
|
||||
|
||||
def write(self, data):
|
||||
if self.dump:
|
||||
print ">", hexlify(data).upper()
|
||||
print(">", hexlify(data).upper())
|
||||
size = len(data)
|
||||
ofs = 0
|
||||
while size:
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
def checksum(data):
|
||||
s = 0
|
||||
for c in data:
|
||||
s += ord(c)
|
||||
s += c
|
||||
return (s & 0xFFFF) ^ 0xFFFF
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ class BasePacket(object):
|
|||
self.cmd = cmd
|
||||
self.arg = arg
|
||||
self.data = data
|
||||
print(self.data)
|
||||
|
||||
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())
|
||||
|
|
|
@ -60,13 +60,13 @@ class XiaomiTransport(BT):
|
|||
while True:
|
||||
while True:
|
||||
c = self.link.read(1)
|
||||
if c=="\x55":
|
||||
if c==b"\x55":
|
||||
break
|
||||
while True:
|
||||
c = self.link.read(1)
|
||||
if c=="\xAA":
|
||||
if c==b"\xAA":
|
||||
return True
|
||||
if c!="\x55":
|
||||
if c!=b"\x55":
|
||||
break # start waiting 55 again, else - this is 55, so wait for AA
|
||||
|
||||
|
||||
|
@ -74,21 +74,21 @@ class XiaomiTransport(BT):
|
|||
self._wait_pre()
|
||||
pkt = self.link.read(1)
|
||||
l = ord(pkt)+3
|
||||
for i in xrange(l):
|
||||
pkt += self.link.read(1)
|
||||
for i in range(l):
|
||||
pkt.extend(self.link.read(1))
|
||||
ck_calc = checksum(pkt[0:-2])
|
||||
ck_pkt = unpack("<H", pkt[-2:])[0]
|
||||
if ck_pkt!=ck_calc:
|
||||
print "Checksum mismatch !"
|
||||
print("Checksum mismatch !")
|
||||
return None
|
||||
sa, da = self._split_addr(ord(pkt[1]))
|
||||
return BasePacket(sa, da, ord(pkt[2]), ord(pkt[3]), pkt[4:-2]) # sa, da, cmd, arg, data
|
||||
sa, da = self._split_addr(pkt[1])
|
||||
return BasePacket(sa, da, pkt[2], pkt[3], pkt[4:-2]) # sa, da, cmd, arg, data
|
||||
|
||||
|
||||
def send(self, packet):
|
||||
dev = self._make_addr(packet.src, packet.dst)
|
||||
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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue