-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathinit.py
More file actions
53 lines (43 loc) · 1.59 KB
/
init.py
File metadata and controls
53 lines (43 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import atexit
import logging
from validitysensor.flash import read_tls_flash
from validitysensor.init_db import init_db
from validitysensor.init_flash import init_flash
from validitysensor.sensor import sensor, reboot, RebootException
from validitysensor.tls import tls
from validitysensor.upload_fwext import upload_fwext
from validitysensor.usb import usb
def close():
logging.debug('In atexit handler')
if usb.dev is not None:
# Send the reboot command before closing the device.
# Without it the sensor seems to keep creating new TLS sessions and eventually runs out of memory.
# The reboot command may fail if we're shutting down because the device is already gone.
try:
logging.debug('atexit: forcing reboot')
reboot()
except RebootException:
pass
finally:
usb.close()
def open_common():
init_flash()
usb.send_init()
tls.parse_tls_flash(read_tls_flash())
tls.open()
upload_fwext()
sensor.open()
init_db()
# We must register atexit only after we opened usb device,
# so that our atexit handler is called before pyusb's one and we can still talk to the device
#
# Don't attempt to autoreboot unless we finished initializing the sensor successfully.
# Otherwise if there is a bug in initialization, we may end up in a loop constantly rebooting the device.
atexit.register(close)
logging.debug('atexit registered')
def open():
usb.open()
open_common()
def open_devpath(busnum: int, address: int):
usb.open_devpath(busnum, address)
open_common()