Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
173 lines (159 loc) · 7.96 KB
/
Copy pathmain.py
File metadata and controls
173 lines (159 loc) · 7.96 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from lib.pins import TOTKeyPins
from lib.inputclasses import InputEvent, ButtonPress
import asyncio
from lib.computer_comms import ComputerComms
import time
import adafruit_datetime
import lib.totpmanager
import displayio
from adafruit_display_text import label
import terminalio
import lib.displaymenu
import storage
class TOTKey:
LONG_THRESHOLD = 0.5
def __init__(self, pins, comms):
self.pins: TOTKeyPins = pins
self.comms: ComputerComms = comms
self.running = True
self.button_presses = []
self.button_states = [True, True, True]
self.inputs = []
self.comp_commands = []
self.current_menu = None
filepath = "/keys.json"
self.totp_manager = lib.totpmanager.TOTPManager(comms, filepath if not storage.getmount("/").readonly else None)
self.menus = { # key: menu name, value: DisplayScreenBase descendent
"Main": lib.displaymenu.ListMenu(comms, ["Keys", "Info", "Help"]),
"Info": lib.displaymenu.InfoMenu(comms, pins, "Main"),
"Keys": lib.displaymenu.KeyListMenu(comms, self.totp_manager, "Main"),
"KeyShowMenu": lib.displaymenu.KeyShowMenu(comms, pins, self.totp_manager, "Keys"),
"Help": lib.displaymenu.HelpMenu("Main")
}
asyncio.run(self.do_tasks())
async def do_tasks(self):
self.comms.log("TOTKey starting up...")
self.input_task = asyncio.create_task(self.process_inputs())
self.usb_task = asyncio.create_task(self.handle_computer())
self.display_task = asyncio.create_task(self.run_display())
await self.wait_for_finish()
self.comms.log("All tasks finished. Thanks for using TOTKey.")
async def wait_for_finish(self):
await asyncio.gather(self.input_task, self.usb_task, self.display_task)
def input_from_button_press(self, button):
for event in self.button_presses:
if event.button == button:
self.inputs.append(InputEvent(button, time.monotonic() - event.time > self.LONG_THRESHOLD))
self.button_presses.remove(event)
break
async def process_inputs(self):
while self.running:
if self.button_states[0] != self.pins.upButton.value:
self.button_states[0] = self.pins.upButton.value
if not self.button_states[0]:
self.button_presses.append(ButtonPress("UP"))
else:
self.input_from_button_press("UP")
if self.button_states[1] != self.pins.centerButton.value:
self.button_states[1] = self.pins.centerButton.value
if not self.button_states[1]:
self.button_presses.append(ButtonPress("CENTER"))
else:
self.input_from_button_press("CENTER")
if self.button_states[2] != self.pins.downButton.value:
self.button_states[2] = self.pins.downButton.value
if not self.button_states[2]:
self.button_presses.append(ButtonPress("DOWN"))
else:
self.input_from_button_press("DOWN")
for button_press in self.button_presses:
if time.monotonic() - button_press.time > self.LONG_THRESHOLD:
self.inputs.append(InputEvent(button_press.button, True))
self.button_presses.remove(button_press)
await asyncio.sleep(0)
async def handle_computer(self):
self.computer_input_task = asyncio.create_task(self.handle_computer_input())
self.computer_output_task = asyncio.create_task(self.handle_computer_output())
while self.running:
await asyncio.sleep(0)
self.computer_input_task.cancel() # don't get stuck in ainput()
await asyncio.gather(self.computer_output_task)
async def handle_computer_input(self):
while self.running:
command = await self.comms.get_command()
if command is not None:
self.comp_commands.append(command)
async def handle_computer_output(self):
self.comms.send_command("BOARD_READY", {})
while self.running:
if len(self.comp_commands) > 0:
command = self.comp_commands.pop(0)
await self.handle_command(command)
await asyncio.sleep(0)
async def handle_command(self, command):
if command["command"] == "INIT_COMMS":
self.comms.send_command("COMMS_START_ACK", {"current_time": self.pins.rtc.get_unixtime()})
elif command["command"] == "SET_TIME":
self.pins.rtc.set_datetime(adafruit_datetime.datetime.fromtimestamp(command["args"]["current_time"]))
self.comms.send_command("TIME_SET_ACK", {"current_time": self.pins.rtc.get_unixtime()})
elif command["command"] == "GET_TIME":
self.comms.send_command("TIME_GET_RES", {"current_time": self.pins.rtc.get_unixtime()})
elif command["command"] == "BLINK_LED":
self.comms.send_command("BLINK_ACK", {})
self.pins.led2.value = not self.pins.led2.value
await asyncio.sleep(0.5)
self.pins.led2.value = not self.pins.led2.value
elif command["command"] == "ADD_KEY":
try:
self.totp_manager.add_key(command["args"]["service_name"], command["args"]["secret"], command["args"]["digits"])
self.comms.send_command("KEY_ADD_ACK", {})
except ValueError as e:
self.comms.log(f"Error adding key: {e}", "ERROR")
elif command["command"] == "REMOVE_KEY":
try:
self.totp_manager.remove_key(command["args"]["service_name"])
self.comms.send_command("KEY_REMOVE_ACK", {})
except ValueError as e:
self.comms.log(f"Error removing key: {e}", "ERROR")
elif command["command"] == "LIST_KEYS":
self.comms.send_command("KEY_LIST_RESPONSE", {"keys": list(self.totp_manager.keys.keys())})
elif command["command"] == "GET_VOLTAGE":
self.comms.send_command("VOLTAGE_GET_RESPONSE", {"voltage": str(self.pins.get_voltage())})
elif command["command"] == "HALT":
self.running = False
else:
self.comms.log(f"Unknown command: {command['command']}", "WARNING")
async def run_display(self):
if self.pins.oled is None:
return
self.splash = displayio.Group()
current_time = pins.rtc.get_datestring()
self.splash.append(label.Label(terminalio.FONT, text=f"TOTKey booting up...\n{current_time}", y=4))
self.pins.oled.show(self.splash)
await asyncio.sleep(1)
self.splash = displayio.Group()
self.current_menu = self.menus["Main"]
self.current_menu.display_on(self.splash)
self.pins.oled.show(self.splash)
while self.running:
self.current_menu.update_display()
if len(self.inputs) > 0:
event = self.inputs.pop(0)
if event.isLongPress and event.button in ["UP", "DOWN"]:
self.pins.oled.rotation = 180 if self.pins.oled.rotation == 0 else 0
else:
if self.pins.oled.rotation == 180:
if event.button == "UP":
event.button = "DOWN"
elif event.button == "DOWN":
event.button = "UP"
new_menu = self.current_menu.take_input(event)
if new_menu is not None:
self.current_menu = self.menus[new_menu]
while len(self.splash) > 0:
self.splash.pop()
self.current_menu.display_on(self.splash)
self.pins.oled.show(self.splash)
await asyncio.sleep(0)
pins = TOTKeyPins()
totKey = TOTKey(pins, ComputerComms(pins)) # start and run TOTKey firmware