Make mouse demo work

This commit is contained in:
jaseg 2021-03-23 20:19:24 +01:00
parent 32da9c4e8c
commit 4fcc3337e2
3 changed files with 49 additions and 6 deletions

View file

@ -6,6 +6,7 @@ import sys
from contextlib import contextmanager, suppress, wraps
import hashlib
import secrets
import struct
import serial
from cobs import cobs
@ -336,14 +337,19 @@ class NoiseEngine:
raise ProtocolError('Invalid report type')
def uinput_passthrough(self):
with uinput.Device(KeyMapper.ALL_KEYS) as ui:
mouse_foo = [uinput.BTN_LEFT, uinput.BTN_RIGHT, uinput.BTN_MIDDLE,
uinput.REL_X, uinput.REL_Y, uinput.REL_WHEEL]
with uinput.Device(KeyMapper.ALL_KEYS + mouse_foo) as ui:
old_kcs = set()
for msg_type, payload in self.receive_loop():
report_len, *report = payload
if report_len != 8:
raise ValueError('Unsupported report length', report_len)
report = bytes(report[:report_len])
if msg_type is ReportType.KEYBOARD:
if report_len != 8:
raise ValueError('Unsupported report length', report_len)
modbyte, _reserved, *keycodes = report
import binascii
keys = { *KeyMapper.map_modifiers(modbyte), *KeyMapper.map_regulars(keycodes) }
@ -358,8 +364,26 @@ class NoiseEngine:
old_kcs = keys
elif msg_type is ReportType.MOUSE:
# FIXME unhandled
pass
if report_len < 3 or report_len > 8:
raise ValueError('Unsupported report length', report_len)
import binascii
_report_type, buttons, a, b, c, w, _2 = report
x = ((b&0x0f)<<8) | a
y = ((b&0xf0) >> 4) | (c<<4)
if x >= 2048:
x -= 4096
if y >= 2048:
y -= 4096
if w >= 128:
w -= 256
#print('got mouse report', binascii.hexlify(report), buttons, x, y, w)
ui.emit(uinput.BTN_LEFT, bool(buttons&1), syn=False)
ui.emit(uinput.BTN_MIDDLE, bool(buttons&4), syn=False)
ui.emit(uinput.BTN_RIGHT, bool(buttons&2), syn=False)
ui.emit(uinput.REL_X, x, syn=False)
ui.emit(uinput.REL_Y, y, syn=False)
ui.emit(uinput.REL_WHEEL, w, syn=False)
ui.syn()
if __name__ == '__main__':
import argparse

View file

@ -296,6 +296,7 @@ void pairing_input(uint8_t modbyte, uint8_t keycode) {
case KEY_ENTER:
pairing_buf[pairing_buf_pos++] = '\0';
if (!pairing_check(&noise_state, pairing_buf)) {
LOG_PRINTF("Pairing success, persisting remote key.\n");
persist_remote_key(&noise_state);
/* FIXME write key to backup memory */
@ -303,6 +304,8 @@ void pairing_input(uint8_t modbyte, uint8_t keycode) {
if (send_encrypted_message(&noise_state, &response, sizeof(response)))
LOG_PRINTF("Error sending pairing response packet\n");
noise_state.failed_handshakes = 0;
} else {
/* FIXME sound alarm */

View file

@ -188,7 +188,7 @@ void uninit_handshake(struct NoiseState *st, enum handshake_state new_state) {
noise_handshakestate_free(st->handshake);
st->handshake_state = new_state;
st->handshake = NULL;
arm_key_scrubber();
//arm_key_scrubber(); FIXME DEBUG
}
/*@
@ -291,9 +291,19 @@ int handshake_phase2(struct NoiseState * const st, uint8_t *buf, size_t len) {
BLAKE2s_update(&bc, st->remote_key, sizeof(st->remote_key));
BLAKE2s_finish(&bc, remote_fp);
LOG_PRINTF("Key in memory: ");
for (int i=0; i<BLAKE2S_HASH_SIZE; i++)
LOG_PRINTF("%02x ", remote_fp[i]);
LOG_PRINTF("\n");
LOG_PRINTF("Key in storage: ");
for (int i=0; i<BLAKE2S_HASH_SIZE; i++)
LOG_PRINTF("%02x ", st->remote_key_reference[i]);
LOG_PRINTF("\n");
//@ ghost key_checked_trace = 1;
if (!fc_memcmp_uint8(remote_fp, st->remote_key_reference, sizeof(remote_fp))) { /* keys match */
//@ ghost key_match_trace = 1;
LOG_PRINTF("Keys match, accepting peer.\n");
uint8_t response = REPORT_PAIRING_SUCCESS;
if (send_encrypted_message(st, &response, sizeof(response)))
LOG_PRINTF("Error sending pairing response packet\n");
@ -303,6 +313,7 @@ int handshake_phase2(struct NoiseState * const st, uint8_t *buf, size_t len) {
return 1;
} else { /* keys don't match */
LOG_PRINTF("Keys don't match, requiring pairing.\n");
uint8_t response = REPORT_PAIRING_START;
if (send_encrypted_message(st, &response, sizeof(response)))
LOG_PRINTF("Error sending pairing response packet\n");
@ -403,6 +414,11 @@ void persist_remote_key(struct NoiseState *st) {
BLAKE2s_update(&bc, st->remote_key, sizeof(st->remote_key));
BLAKE2s_finish(&bc, st->remote_key_reference);
st->handshake_state = HANDSHAKE_DONE_KNOWN_HOST;
LOG_PRINTF("Key in memory: ");
for (int i=0; i<BLAKE2S_HASH_SIZE; i++)
LOG_PRINTF("%02x ", st->remote_key_reference[i]);
LOG_PRINTF("\n");
}
/*@