mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-13 01:58:08 +00:00
src/apps/fido_u2f: add async write
This commit is contained in:
parent
d323aa88f7
commit
c568071177
@ -229,7 +229,7 @@ class Cmd:
|
|||||||
async def read_cmd(iface: io.HID) -> Cmd:
|
async def read_cmd(iface: io.HID) -> Cmd:
|
||||||
desc_init = frame_init()
|
desc_init = frame_init()
|
||||||
desc_cont = frame_cont()
|
desc_cont = frame_cont()
|
||||||
read = loop.select(iface.iface_num())
|
read = loop.select(iface.iface_num() | io.POLL_READ)
|
||||||
|
|
||||||
buf = await read
|
buf = await read
|
||||||
# log.debug(__name__, 'read init %s', buf)
|
# log.debug(__name__, 'read init %s', buf)
|
||||||
@ -267,14 +267,14 @@ async def read_cmd(iface: io.HID) -> Cmd:
|
|||||||
if cfrm.cid != ifrm.cid:
|
if cfrm.cid != ifrm.cid:
|
||||||
# cont frame for a different channel, reply with BUSY and skip
|
# cont frame for a different channel, reply with BUSY and skip
|
||||||
log.warning(__name__, '_ERR_CHANNEL_BUSY')
|
log.warning(__name__, '_ERR_CHANNEL_BUSY')
|
||||||
send_cmd(cmd_error(cfrm.cid, _ERR_CHANNEL_BUSY), iface)
|
await send_cmd(cmd_error(cfrm.cid, _ERR_CHANNEL_BUSY), iface)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if cfrm.seq != seq:
|
if cfrm.seq != seq:
|
||||||
# cont frame for this channel, but incorrect seq number, abort
|
# cont frame for this channel, but incorrect seq number, abort
|
||||||
# current msg
|
# current msg
|
||||||
log.warning(__name__, '_ERR_INVALID_SEQ')
|
log.warning(__name__, '_ERR_INVALID_SEQ')
|
||||||
send_cmd(cmd_error(cfrm.cid, _ERR_INVALID_SEQ), iface)
|
await send_cmd(cmd_error(cfrm.cid, _ERR_INVALID_SEQ), iface)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
datalen += utils.memcpy(data, datalen, cfrm.data, 0, bcnt - datalen)
|
datalen += utils.memcpy(data, datalen, cfrm.data, 0, bcnt - datalen)
|
||||||
@ -283,7 +283,7 @@ async def read_cmd(iface: io.HID) -> Cmd:
|
|||||||
return Cmd(ifrm.cid, ifrm.cmd, data)
|
return Cmd(ifrm.cid, ifrm.cmd, data)
|
||||||
|
|
||||||
|
|
||||||
def send_cmd(cmd: Cmd, iface: io.HID) -> None:
|
async def send_cmd(cmd: Cmd, iface: io.HID) -> None:
|
||||||
init_desc = frame_init()
|
init_desc = frame_init()
|
||||||
cont_desc = frame_cont()
|
cont_desc = frame_cont()
|
||||||
offset = 0
|
offset = 0
|
||||||
@ -302,11 +302,14 @@ def send_cmd(cmd: Cmd, iface: io.HID) -> None:
|
|||||||
if offset < datalen:
|
if offset < datalen:
|
||||||
frm = overlay_struct(buf, cont_desc)
|
frm = overlay_struct(buf, cont_desc)
|
||||||
|
|
||||||
|
write = loop.select(iface.iface_num() | io.POLL_WRITE)
|
||||||
while offset < datalen:
|
while offset < datalen:
|
||||||
frm.seq = seq
|
frm.seq = seq
|
||||||
offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen)
|
offset += utils.memcpy(frm.data, 0, cmd.data, offset, datalen)
|
||||||
utime.sleep_ms(1) # FIXME: async write
|
while True:
|
||||||
iface.write(buf)
|
await write
|
||||||
|
if iface.write(buf) > 0:
|
||||||
|
break
|
||||||
# log.debug(__name__, 'send cont %s', buf)
|
# log.debug(__name__, 'send cont %s', buf)
|
||||||
seq += 1
|
seq += 1
|
||||||
|
|
||||||
@ -322,7 +325,7 @@ async def handle_reports(iface: io.HID):
|
|||||||
if req is None:
|
if req is None:
|
||||||
continue
|
continue
|
||||||
resp = dispatch_cmd(req)
|
resp = dispatch_cmd(req)
|
||||||
send_cmd(resp, iface)
|
await send_cmd(resp, iface)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.exception(__name__, e)
|
log.exception(__name__, e)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user