Various small and easy cleanup found by inspection in PyCharm

This commit is contained in:
Arvid Norlander 2020-08-05 18:42:34 +02:00
parent a17ad52102
commit 6a5a32d29f
No known key found for this signature in database
GPG key ID: E824A8E5D8D29AA0
16 changed files with 76 additions and 73 deletions

View file

@ -22,9 +22,9 @@ if __name__ == "__main__":
sleep(0.05)
led_script = unhexlify(
'39ff100000ff03000001ff002000000000ffff0000ffff0000ff03000001ff00' \
'200000000000000000ffff0000ff03000001ff002000000000ffff0000000000' \
'0000000000000000000000000000000000000000000000000000000000000000' \
'39ff100000ff03000001ff002000000000ffff0000ffff0000ff03000001ff00'
'200000000000000000ffff0000ff03000001ff002000000000ffff0000000000'
'0000000000000000000000000000000000000000000000000000000000000000'
'0000000000000000000000000000000000000000000000000000000000')
assert_status(tls.app(led_script))

View file

@ -1,12 +1,14 @@
#!/usr/bin/env python3
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
import sys
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
o = bus.get_object('net.reactivated.Fprint', '/net/reactivated/Fprint/Manager', introspect=False)
o = o.GetDefaultDevice()
o = bus.get_object('net.reactivated.Fprint', o, introspect=False)
o = dbus.Interface(o, 'net.reactivated.Fprint.Device')
print(o.RunCmd(sys.argv[1]))
print(o.RunCmd(sys.argv[1]))

View file

@ -3,8 +3,8 @@ from binascii import hexlify
from validitysensor.sensor import sensor
#usb.trace_enabled = True
#tls.trace_enabled = True
# usb.trace_enabled = True
# tls.trace_enabled = True
def identify():

View file

@ -8,7 +8,7 @@ from .util import assert_status
from .winbio_constants import finger_names
class UserStorage():
class UserStorage:
def __init__(self, dbid, name):
self.dbid = dbid
self.name = name
@ -19,7 +19,7 @@ class UserStorage():
self.name), repr(self.users))
class User():
class User:
def __init__(self, dbid, identity):
self.dbid = dbid
self.identity = identity
@ -112,7 +112,7 @@ def identity_to_bytes(identity: str):
raise Exception('Don' 't know how to handle identity %s' % repr(identity))
class DbRecord():
class DbRecord:
def __init__(self):
self.dbid = 0
self.type = 0
@ -125,8 +125,8 @@ class DbRecord():
self.dbid, self.type, self.storage, repr(self.value), repr(self.children))
class Db():
class Info():
class Db:
class Info:
def __init__(self, total, used, free, records, roots):
self.total = total # partition size
self.used = used # used (not deleted)

View file

@ -6,9 +6,10 @@ from .tls import tls
from .util import assert_status, unhex
class FlashInfo():
class FlashInfo:
def __init__(self, ic, blocks, unknown0, blocksize, unknown1, partitions):
self.ic, self.blocks, self.unknown0, self.blocksize, self.unknown1, self.partitions = ic, blocks, unknown0, blocksize, unknown1, partitions
self.ic, self.blocks, self.unknown0, self.blocksize, self.unknown1, self.partitions = \
ic, blocks, unknown0, blocksize, unknown1, partitions
def __repr__(self):
return 'FlashInfo(%s, 0x%x, 0x%x, 0x%x, 0x%x, %s)' % (repr(
@ -20,7 +21,7 @@ class FlashInfo():
# access lvl:
# 2 -- write only
# 7 -- can read/write even without TLS
class PartitionInfo():
class PartitionInfo:
def __init__(self, id, type, access_lvl, offset, size):
self.id, self.type, self.access_lvl, self.offset, self.size = id, type, access_lvl, offset, size
@ -39,7 +40,7 @@ def get_flash_info():
ic = flash_ic_table_lookup(jid0, jid1, blocks * blocksize)
if ic == None:
if ic is None:
raise Exception('Unknown flash IC. JEDEC id=%x:%x, size=%dx%d' %
(jid0, jid1, blocks, blocksize))
@ -63,7 +64,7 @@ def get_flash_info():
# 0200 2377 0000 0100 802f0000
# 0200 6637 0100 0c00 f0220200
# 0100 2556 0000 0100 60040000
class ModuleInfo():
class ModuleInfo:
def __init__(self, type, subtype, major, minor, size):
self.type, self.subtype, self.major, self.minor, self.size = type, subtype, major, minor, size
@ -72,7 +73,7 @@ class ModuleInfo():
self.minor, self.size)
class FirmwareInfo():
class FirmwareInfo:
def __init__(self, major, minor, buildtime, modules):
self.major, self.minor, self.buildtime, self.modules = major, minor, buildtime, modules

View file

@ -1,4 +1,4 @@
class DeviceInfo():
class DeviceInfo:
def __init__(self, major, type, version, version_mask, name):
self.major, self.type, self.version, self.version_mask, self.name = major, type, version, version_mask, name
@ -445,7 +445,7 @@ def dev_info_lookup(major, ver):
return fuzzy_match
class FlashIcInfo():
class FlashIcInfo:
def __init__(self, name, size, f18, jid0, jid1, f1b, f1c, f1e, secror_size, sector_erase_cmd,
f25, f26):
self.name, self.size, self.f18, self.jid0, self.jid1, self.f1b, self.f1c, self.f1e, self.secror_size, self.sector_erase_cmd, self.f25, self.f26 = name, size, f18, jid0, jid1, f1b, f1c, f1e, secror_size, sector_erase_cmd, f25, f26

View file

@ -28,7 +28,7 @@ def close():
def open_common():
init_flash()
usb.send_init()
tls.parseTlsFlash(read_tls_flash())
tls.parse_tls_flash(read_tls_flash())
tls.open()
upload_fwext()
sensor.open()

View file

@ -32,8 +32,8 @@ def init_machine_guid(machine_guid='e7260876-58db-4d27-8c40-8d13110d6a71'):
def init_db():
stg = db.get_user_storage(name='StgWindsor')
if stg == None:
if stg is None:
logging.info('Creating a new user storage object')
db.new_user_storate()
#init_machine_guid()
# init_machine_guid()

View file

@ -157,7 +157,7 @@ def init_flash():
erase_flash(4)
# Persist certs and keys on cert partition.
write_flash(1, 0, tls.makeTlsFlash())
write_flash(1, 0, tls.make_tls_flash())
# Reboot.
# The device will disconnect and our service will be started by udev as soon as it is connected again.

View file

@ -87,7 +87,7 @@ def factory_reset():
reboot()
class RomInfo():
class RomInfo:
@classmethod
def get(cls):
rsp = tls.cmd(b'\x01')
@ -173,10 +173,10 @@ def bitpack(b):
# convert back to bytes
b = b.to_bytes((u * l + 7) // 8, 'little')
return (u, m, b)
return u, m, b
class Line():
class Line:
mask = None
flags = None
data = None
@ -217,7 +217,7 @@ class CaptureMode(Enum):
ENROLL = 3
class Sensor():
class Sensor:
calib_data = b''
def open(self):
@ -417,7 +417,7 @@ class Sensor():
tst = self.patch_timeslot_again(tst)
c[1] = self.get_key_line() + tst[self.type_info.line_width:]
#---------------- Reply Configuration ---------------
# ---------------- Reply Configuration ---------------
chunks += [[0x17, b'']]
if mode == CaptureMode.IDENTIFY:
@ -448,7 +448,7 @@ class Sensor():
0x2e, unhexlify('0200180023000000700070004d010000a0008c003c32321e3c0a0202')
]]
#---------------- Interleave ---------------
# ---------------- Interleave ---------------
chunks += [[0x44, pack('<L', 1)]]
lines = []
@ -494,14 +494,14 @@ class Sensor():
if pad > 0:
l.data += b'\0' * (4 - pad)
#---------------- Line Update ---------------
# ---------------- Line Update ---------------
line_update = pack('<L', len(lines))
line_update += b''.join([pack('<LL', l.mask, l.flags) for l in lines])
line_update += b''.join([l.data for l in lines if ((l.flags & 0x00f00000) >> 0x14) <= 1])
chunks += [[0x30, line_update]]
#---------------- Line Update Transform ---------------
# ---------------- Line Update Transform ---------------
update_transform = b''.join([
pack('<BBH', l.v0, l.v1, l.v2) + l.data for l in lines
if ((l.flags & 0x00f00000) >> 0x14) > 1
@ -525,7 +525,7 @@ class Sensor():
tst = self.patch_timeslot_again(tst)
c[1] = tst
#---------------- Reply Configuration ---------------
# ---------------- Reply Configuration ---------------
chunks += [[0x17, b'']]
if mode == CaptureMode.IDENTIFY:
@ -728,7 +728,7 @@ class Sensor():
if error != 0:
raise Exception('Scanning problem: %04x' % error)
return (x, y, w1, w2)
return x, y, w1, w2
finally:
tls.app(unhexlify('04')) # capture stop if still running, cleanup
@ -788,7 +788,7 @@ class Sensor():
res = res[magic_len + l:]
return (header, template, tid)
return header, template, tid
def make_finger_data(self, subtype, template, tid):
template = pack('<HH', 1, len(template)) + template
@ -808,7 +808,7 @@ class Sensor():
tinfo = self.make_finger_data(subtype, final_template, tid)
usr = db.lookup_user(identity)
if usr == None:
if usr is None:
usr = db.new_user(identity)
else:
usr = usr.dbid
@ -885,7 +885,7 @@ class Sensor():
usrid, = unpack('<L', usrid)
subtype, = unpack('<H', subtype)
return (usrid, subtype, hsh)
return usrid, subtype, hsh
finally:
# cleanup, ignore any errors
tls.app(unhexlify('6200000000'))

View file

@ -2,7 +2,7 @@ import typing
from struct import unpack, pack
class SidIdentity():
class SidIdentity:
def __init__(self, revision: int, auth: int, subauth: typing.Sequence[int]):
self.revision = revision
self.auth = auth

View file

@ -100,37 +100,37 @@ insn_to_string = [
def decode_insn(b):
if b[0] == 0:
return (0, 1)
return 0, 1
elif b[0] == 1:
return (1, 1)
return 1, 1
elif b[0] == 2:
return (2, 1)
return 2, 1
elif b[0] == 3:
return (3, 1)
return 3, 1
elif b[0] == 4:
return (4, 1)
return 4, 1
elif b[0] == 5:
return (5, 2, b[1])
return 5, 2, b[1]
elif b[0] == 6:
return (6, 2, b[1])
return 6, 2, b[1]
elif b[0] == 7:
return (7, 2, 0x100 if b[1] == 0 else b[1])
return 7, 2, 0x100 if b[1] == 0 else b[1]
elif b[0] & 0xfe == 8:
return (8, 2, (b[0] & 1) << 8 | b[1])
return 8, 2, (b[0] & 1) << 8 | b[1]
elif b[0] & 0xfe == 0xa:
return (9, 2, (b[0] & 1) << 8 | b[1])
return 9, 2, (b[0] & 1) << 8 | b[1]
elif b[0] & 0xfc == 0xc:
return (10, 1, b[0] & 3)
return 10, 1, b[0] & 3
elif b[0] & 0xf8 == 0x10:
return (11, 3, b[0] & 7, b[1] << 2, 0x100 if b[2] == 0 else b[2])
return 11, 3, b[0] & 7, b[1] << 2, 0x100 if b[2] == 0 else b[2]
elif b[0] & 0xe0 == 0x20:
return (12, 1, b[0] & 0x1f) # TODO check how features are converted to op args
return 12, 1, b[0] & 0x1f # TODO check how features are converted to op args
elif b[0] & 0xc0 == 0x40:
return (13, 3, (b[0] & 0x3f) * 4 + 0x80002000, b[1] | (b[2] << 8))
return 13, 3, (b[0] & 0x3f) * 4 + 0x80002000, b[1] | (b[2] << 8)
elif b[0] & 0xc0 == 0x80:
return (14, 1, (b[0] & 0x38) >> 3, b[0] & 7)
return 14, 1, (b[0] & 0x38) >> 3, b[0] & 7
elif b[0] & 0xc0 == 0xc0:
return (15, 2, (b[0] & 0x38) >> 3, b[0] & 7, 0x100 if b[1] == 0 else b[1])
return 15, 2, (b[0] & 0x38) >> 3, b[0] & 7, 0x100 if b[1] == 0 else b[1]
else:
raise Exception('Unhandled instruction %02x' % b)
@ -158,7 +158,7 @@ def find_nth_insn(b, opcode, n):
if op == opcode:
n -= 1
if n == 0:
return (pc, b[:sz])
return pc, b[:sz]
b = b[sz:]
pc += sz
@ -177,7 +177,7 @@ def find_nth_regwrite(b, reg_addr, n):
if addr == reg_addr:
n -= 1
if n == 0:
return (pc, b[:sz])
return pc, b[:sz]
b = b[sz:]
pc += sz

View file

@ -86,7 +86,7 @@ def unpad(b):
# TODO assert the right state transitions
class Tls():
class Tls:
def __init__(self, usb):
self.usb = usb
self.reset()
@ -407,7 +407,7 @@ class Tls():
def make_ext(self, id, b):
return pack('>H', id) + with_2bytes_size(b)
def parseTlsFlash(self, reply):
def parse_tls_flash(self, reply):
while len(reply) > 0:
hdr, reply = reply[:4], reply[4:]
hs, reply = reply[:0x20], reply[0x20:]
@ -440,20 +440,20 @@ class Tls():
else:
self.trace('unhandled block id %04x (%d bytes): %s' % (id, sz, hexlify(body)))
def makeTlsFlashBlock(self, id, body):
def make_tls_flash_block(self, id, body):
m = sha256()
m.update(body)
hdr = pack('<HH', id, len(body))
return hdr + m.digest() + body
def makeTlsFlash(self):
b = self.makeTlsFlashBlock(0, b'\0')
b += self.makeTlsFlashBlock(4, self.priv_blob)
b += self.makeTlsFlashBlock(3, self.tls_cert)
b += self.makeTlsFlashBlock(5, crt_hardcoded)
b += self.makeTlsFlashBlock(1, b'\0' * 0x100)
b += self.makeTlsFlashBlock(2, b'\0' * 0x100)
b += self.makeTlsFlashBlock(6, self.ecdh_blob)
def make_tls_flash(self):
b = self.make_tls_flash_block(0, b'\0')
b += self.make_tls_flash_block(4, self.priv_blob)
b += self.make_tls_flash_block(3, self.tls_cert)
b += self.make_tls_flash_block(5, crt_hardcoded)
b += self.make_tls_flash_block(1, b'\0' * 0x100)
b += self.make_tls_flash_block(2, b'\0' * 0x100)
b += self.make_tls_flash_block(6, self.ecdh_blob)
b += b'\xff' * (0x1000 - len(b))
return b

View file

@ -23,7 +23,7 @@ def default_fwext_name():
def upload_fwext(fw_path=None):
fwi = get_fw_info(2)
if fwi != None:
if fwi is not None:
logging.info('Detected firmware version %d.%d (%s))' %
(fwi.major, fwi.minor, ctime(fwi.buildtime)))
return
@ -56,7 +56,7 @@ def upload_fwext(fw_path=None):
write_fw_signature(2, signature)
fwi = get_fw_info(2)
if fwi == None:
if fwi is None:
raise Exception('No firmware detected')
logging.info('Loaded FWExt version %d.%d (%s), %d modules' %

View file

@ -20,7 +20,7 @@ class CancelledException(Exception):
pass
class Usb():
class Usb:
def __init__(self):
self.interrupt_cb = None
self.trace_enabled = False
@ -66,7 +66,7 @@ class Usb():
return self.dev
def send_init(self):
#self.dev.set_configuration()
# self.dev.set_configuration()
# TODO analyse responses, detect hardware type
assert_status(self.cmd(unhexlify('01'))) # RomInfo.get()

View file

@ -13,4 +13,4 @@ def assert_status(b):
def unhex(x):
return unhexlify(re.sub('\W', '', x))
return unhexlify(re.sub(r'\W', '', x))