Confirmed pairing works
This commit is contained in:
parent
2f4f3e13aa
commit
70d8dcb6f6
9 changed files with 616 additions and 68 deletions
27
src/demo.c
27
src/demo.c
|
|
@ -136,21 +136,27 @@ int pairing_check(struct NoiseState *st, const char *buf);
|
|||
void pairing_input(uint8_t keycode);
|
||||
void pairing_parse_report(struct hid_report *buf, uint8_t len);
|
||||
|
||||
/* Minimum number of bytes of handshake hash to confirm during pairing */
|
||||
#define MIN_PAIRING_SEQUENCE_LENGTH 8
|
||||
|
||||
int pairing_check(struct NoiseState *st, const char *buf) {
|
||||
LOG_PRINTF("Checking pairing\n");
|
||||
const char *p = buf;
|
||||
int idx = 0;
|
||||
do {
|
||||
const char *found = strchr(p, ' ');
|
||||
size_t plen = found ? (size_t)(found - p) : strlen(p); /* p >= found */
|
||||
int num = -1;
|
||||
/* FIXME ignore "and", ignore commata and dots, handle letter case correctly (currently it's ignored). */
|
||||
for (int i=0; i<256; i++) {
|
||||
if (!strncmp(p, adjectives[i], plen) || !strncmp(p, nouns[i], plen)) {
|
||||
if ((!strncmp(p, adjectives[i], plen)) || (!strncmp(p, nouns[i], plen))) {
|
||||
LOG_PRINTF(" idx=%02d h=%02x i=%02x adj=%s n=%s plen=%d s=%s\n", idx, st->handshake_hash[idx], i, adjectives[i], nouns[i], plen, p);
|
||||
num = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (num == -1) {
|
||||
LOG_PRINTF("Pairing word not found in dictionary\n");
|
||||
LOG_PRINTF("Pairing word \"%s\" not found in dictionary\n", p);
|
||||
return -1;
|
||||
}
|
||||
if (st->handshake_hash[idx] != num) {
|
||||
|
|
@ -159,9 +165,12 @@ int pairing_check(struct NoiseState *st, const char *buf) {
|
|||
}
|
||||
idx++;
|
||||
p = strchr(p, ' ');
|
||||
} while (p != NULL && idx < BLAKE2S_HASH_SIZE);
|
||||
if (!p)
|
||||
break; /* end of string */
|
||||
p++; /* skip space */
|
||||
} while (idx < BLAKE2S_HASH_SIZE);
|
||||
|
||||
if (idx < 8) {
|
||||
if (idx < MIN_PAIRING_SEQUENCE_LENGTH) {
|
||||
LOG_PRINTF("Pairing sequence too short, only %d bytes of hash checked\n", idx);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -180,20 +189,24 @@ void pairing_input(uint8_t keycode) {
|
|||
break;
|
||||
|
||||
case KEY_BACKSPACE:
|
||||
pairing_buf[pairing_buf_pos] = '\0'; /* FIXME debug */
|
||||
if (pairing_buf_pos > 0)
|
||||
pairing_buf_pos--;
|
||||
break;
|
||||
|
||||
default:
|
||||
for (size_t i=0; i<sizeof(keycode_mapping)/sizeof(keycode_mapping[0]); i++) {
|
||||
for (size_t i=0; keycode_mapping[i].kc != KEY_NONE; i++) {
|
||||
if (keycode_mapping[i].kc == keycode) {
|
||||
if (pairing_buf_pos < sizeof(pairing_buf)-1) /* allow for terminating null byte */
|
||||
if (pairing_buf_pos < sizeof(pairing_buf)-1) /* allow for terminating null byte */ {
|
||||
pairing_buf[pairing_buf_pos++] = keycode_mapping[i].ch;
|
||||
pairing_buf[pairing_buf_pos] = '\0'; /* FIXME debug */
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
LOG_PRINTF("Input: %s\n", pairing_buf);
|
||||
}
|
||||
|
||||
void pairing_parse_report(struct hid_report *buf, uint8_t len) {
|
||||
|
|
@ -228,7 +241,7 @@ static void hid_in_message_handler(uint8_t device_id, const uint8_t *data, uint3
|
|||
return;
|
||||
}
|
||||
|
||||
LOG_PRINTF("Sending event %02X %02X %02X %02X\n", data[0], data[1], data[2], data[3]);
|
||||
//LOG_PRINTF("Sending event %02X %02X %02X %02X\n", data[0], data[1], data[2], data[3]);
|
||||
struct hid_report_packet pkt = {
|
||||
.len = length,
|
||||
.report = {0}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,33 @@
|
|||
|
||||
#include "hid_keycodes.h"
|
||||
|
||||
struct keymap_entry keycode_mapping[37] = {
|
||||
{ KEY_A, 'A' },
|
||||
{ KEY_B, 'B' },
|
||||
{ KEY_C, 'C' },
|
||||
{ KEY_D, 'D' },
|
||||
{ KEY_E, 'E' },
|
||||
{ KEY_F, 'F' },
|
||||
{ KEY_G, 'G' },
|
||||
{ KEY_H, 'H' },
|
||||
{ KEY_I, 'I' },
|
||||
{ KEY_J, 'J' },
|
||||
{ KEY_K, 'K' },
|
||||
{ KEY_L, 'L' },
|
||||
{ KEY_M, 'M' },
|
||||
{ KEY_N, 'N' },
|
||||
{ KEY_O, 'O' },
|
||||
{ KEY_P, 'P' },
|
||||
{ KEY_Q, 'Q' },
|
||||
{ KEY_R, 'R' },
|
||||
{ KEY_S, 'S' },
|
||||
{ KEY_T, 'T' },
|
||||
{ KEY_U, 'U' },
|
||||
{ KEY_V, 'V' },
|
||||
{ KEY_W, 'W' },
|
||||
{ KEY_X, 'X' },
|
||||
{ KEY_Y, 'Y' },
|
||||
{ KEY_Z, 'Z' },
|
||||
struct keymap_entry keycode_mapping[] = {
|
||||
{ KEY_A, 'a' },
|
||||
{ KEY_B, 'b' },
|
||||
{ KEY_C, 'c' },
|
||||
{ KEY_D, 'd' },
|
||||
{ KEY_E, 'e' },
|
||||
{ KEY_F, 'f' },
|
||||
{ KEY_G, 'g' },
|
||||
{ KEY_H, 'h' },
|
||||
{ KEY_I, 'i' },
|
||||
{ KEY_J, 'j' },
|
||||
{ KEY_K, 'k' },
|
||||
{ KEY_L, 'l' },
|
||||
{ KEY_M, 'm' },
|
||||
{ KEY_N, 'n' },
|
||||
{ KEY_O, 'o' },
|
||||
{ KEY_P, 'p' },
|
||||
{ KEY_Q, 'q' },
|
||||
{ KEY_R, 'r' },
|
||||
{ KEY_S, 's' },
|
||||
{ KEY_T, 't' },
|
||||
{ KEY_U, 'u' },
|
||||
{ KEY_V, 'v' },
|
||||
{ KEY_W, 'w' },
|
||||
{ KEY_X, 'x' },
|
||||
{ KEY_Y, 'y' },
|
||||
{ KEY_Z, 'z' },
|
||||
{ KEY_1, '1' },
|
||||
{ KEY_2, '2' },
|
||||
{ KEY_3, '3' },
|
||||
|
|
@ -38,6 +38,8 @@ struct keymap_entry keycode_mapping[37] = {
|
|||
{ KEY_8, '8' },
|
||||
{ KEY_9, '9' },
|
||||
{ KEY_0, '0' },
|
||||
{ KEY_MINUS, '-' },
|
||||
{ KEY_SPACE, ' ' },
|
||||
{ KEY_NONE, 0 }, /* end marker */
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ struct keymap_entry {
|
|||
char ch;
|
||||
};
|
||||
|
||||
extern struct keymap_entry keycode_mapping[37];
|
||||
extern struct keymap_entry keycode_mapping[];
|
||||
|
||||
enum hid_keycode {
|
||||
KEY_NONE = 0x00, // No key pressed
|
||||
|
|
|
|||
|
|
@ -142,7 +142,6 @@ enum handshake_state try_continue_noise_handshake(struct NoiseState *st, uint8_t
|
|||
goto errout;
|
||||
}
|
||||
|
||||
|
||||
HANDLE_NOISE_ERROR(noise_dhstate_get_public_key(remote_dh, st->remote_key, sizeof(st->remote_key)), "getting remote pubkey");
|
||||
|
||||
if (!memcmp(st->remote_key, st->remote_key_reference, sizeof(st->remote_key))) { /* keys match */
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ void hid_driver_init(const hid_config_t *config)
|
|||
static void *init(usbh_device_t *usbh_dev)
|
||||
{
|
||||
if (!initialized) {
|
||||
LOG_PRINTF("\n%s/%d : driver not initialized\r\n", __FILE__, __LINE__);
|
||||
LOG_PRINTF("\n%s/%d : driver not initialized\n", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +341,7 @@ static void remove(void *drvdata)
|
|||
bool hid_set_report(uint8_t device_id, uint8_t val)
|
||||
{
|
||||
if (device_id >= USBH_HID_MAX_DEVICES) {
|
||||
LOG_PRINTF("invalid device id");
|
||||
LOG_PRINTF("invalid device id\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -374,7 +374,7 @@ bool hid_set_report(uint8_t device_id, uint8_t val)
|
|||
bool hid_is_connected(uint8_t device_id)
|
||||
{
|
||||
if (device_id >= USBH_HID_MAX_DEVICES) {
|
||||
LOG_PRINTF("is connected: invalid device id");
|
||||
LOG_PRINTF("is connected: invalid device id\n");
|
||||
return false;
|
||||
}
|
||||
return hid_device[device_id].state_next == STATE_INACTIVE;
|
||||
|
|
|
|||
|
|
@ -342,12 +342,13 @@ static void rxflvl_handle(void *drvdata)
|
|||
if ( channels[channel].data_index < channels[channel].packet.datalen) {
|
||||
if (len == channels[channel].packet.endpoint_size_max) {
|
||||
REBASE_CH(OTG_HCCHAR, channel) |= OTG_HCCHAR_CHENA;
|
||||
LOG_PRINTF("CHENA[%d/%d] ", channels[channel].data_index, channels[channel].packet.datalen);
|
||||
//LOG_PRINTF("CHENA[%d/%d] ", channels[channel].data_index, channels[channel].packet.datalen);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} else if ((rxstsp&OTG_GRXSTSP_PKTSTS_MASK) == OTG_GRXSTSP_PKTSTS_IN_COMP) {
|
||||
/*
|
||||
#ifdef USART_DEBUG
|
||||
uint32_t i;
|
||||
LOG_PRINTF("\nDATA: ");
|
||||
|
|
@ -356,6 +357,7 @@ static void rxflvl_handle(void *drvdata)
|
|||
LOG_PRINTF("%02X ", data[i]);
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
} else if ((rxstsp&OTG_GRXSTSP_PKTSTS_MASK) == OTG_GRXSTSP_PKTSTS_CHH) {
|
||||
|
||||
} else {
|
||||
|
|
@ -441,7 +443,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
// To clear interrupt write 0 to PENA
|
||||
// To disable port write 1 to PENCHNG
|
||||
REBASE(OTG_HPRT) &= ~OTG_HPRT_PENA;
|
||||
LOG_PRINTF("PENCHNG");
|
||||
//LOG_PRINTF("PENCHNG");
|
||||
if ((hprt & OTG_HPRT_PENA)) {
|
||||
return USBH_POLL_STATUS_DEVICE_CONNECTED;
|
||||
}
|
||||
|
|
@ -451,13 +453,13 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
if (REBASE(OTG_HPRT) & OTG_HPRT_POCCHNG) {
|
||||
// TODO: Check for functionality
|
||||
REBASE(OTG_HPRT) |= OTG_HPRT_POCCHNG;
|
||||
LOG_PRINTF("POCCHNG");
|
||||
//LOG_PRINTF("POCCHNG");
|
||||
}
|
||||
}
|
||||
|
||||
if (REBASE(OTG_GINTSTS) & OTG_GINTSTS_DISCINT) {
|
||||
REBASE(OTG_GINTSTS) = OTG_GINTSTS_DISCINT;
|
||||
LOG_PRINTF("DISCINT");
|
||||
//LOG_PRINTF("DISCINT");
|
||||
|
||||
/*
|
||||
* When the voltage drops, DISCINT interrupt is generated although
|
||||
|
|
@ -465,7 +467,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
* Often, DISCINT is bad interpreted upon insertion of device
|
||||
*/
|
||||
if (!(REBASE(OTG_HPRT) & OTG_HPRT_PCSTS)) {
|
||||
LOG_PRINTF("discint processsing...");
|
||||
//LOG_PRINTF("discint processsing...");
|
||||
channels_init(dev);
|
||||
}
|
||||
REBASE(OTG_GINTSTS) = REBASE(OTG_GINTSTS);
|
||||
|
|
@ -490,7 +492,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_NAK) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_NAK;
|
||||
LOG_PRINTF("NAK\n");
|
||||
//LOG_PRINTF("NAK\n");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -506,7 +508,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_ACK) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_ACK;
|
||||
LOG_PRINTF("ACK");
|
||||
//LOG_PRINTF("ACK");
|
||||
if (eptyp == USBH_ENDPOINT_TYPE_CONTROL) {
|
||||
channels[channel].packet.toggle[0] = 1;
|
||||
} else {
|
||||
|
|
@ -516,7 +518,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_XFRC) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_XFRC;
|
||||
LOG_PRINTF("XFRC\n");
|
||||
//LOG_PRINTF("XFRC\n");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -532,7 +534,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_FRMOR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_FRMOR;
|
||||
LOG_PRINTF("FRMOR");
|
||||
//LOG_PRINTF("FRMOR");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -547,7 +549,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_TXERR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_TXERR;
|
||||
LOG_PRINTF("TXERR");
|
||||
//LOG_PRINTF("TXERR");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -564,7 +566,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_STALL) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_STALL;
|
||||
LOG_PRINTF("STALL");
|
||||
//LOG_PRINTF("STALL");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -580,7 +582,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_CHH) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_CHH;
|
||||
LOG_PRINTF("CHH");
|
||||
//LOG_PRINTF("CHH");
|
||||
|
||||
free_channel(dev, channel);
|
||||
}
|
||||
|
|
@ -589,7 +591,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
if (hcint & OTG_HCINT_NAK) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_NAK;
|
||||
if (eptyp == USBH_ENDPOINT_TYPE_CONTROL) {
|
||||
LOG_PRINTF("NAK");
|
||||
//LOG_PRINTF("NAK");
|
||||
}
|
||||
|
||||
REBASE_CH(OTG_HCCHAR, channel) |= OTG_HCCHAR_CHENA;
|
||||
|
|
@ -598,12 +600,12 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_DTERR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_DTERR;
|
||||
LOG_PRINTF("DTERR");
|
||||
//LOG_PRINTF("DTERR");
|
||||
}
|
||||
|
||||
if (hcint & OTG_HCINT_ACK) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_ACK;
|
||||
LOG_PRINTF("ACK");
|
||||
//LOG_PRINTF("ACK");
|
||||
|
||||
channels[channel].packet.toggle[0] ^= 1;
|
||||
|
||||
|
|
@ -613,7 +615,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_XFRC) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_XFRC;
|
||||
LOG_PRINTF("XFRC\n");
|
||||
//LOG_PRINTF("XFRC\n");
|
||||
|
||||
free_channel(dev, channel);
|
||||
usbh_packet_callback_data_t cb_data;
|
||||
|
|
@ -633,7 +635,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_BBERR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_BBERR;
|
||||
LOG_PRINTF("BBERR");
|
||||
//LOG_PRINTF("BBERR");
|
||||
free_channel(dev, channel);
|
||||
|
||||
usbh_packet_callback_data_t cb_data;
|
||||
|
|
@ -647,13 +649,13 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_FRMOR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_FRMOR;
|
||||
LOG_PRINTF("FRMOR");
|
||||
//LOG_PRINTF("FRMOR");
|
||||
|
||||
}
|
||||
|
||||
if (hcint & OTG_HCINT_TXERR) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_TXERR;
|
||||
LOG_PRINTF("TXERR");
|
||||
//LOG_PRINTF("TXERR");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -669,7 +671,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (hcint & OTG_HCINT_STALL) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_STALL;
|
||||
LOG_PRINTF("STALL");
|
||||
//LOG_PRINTF("STALL");
|
||||
|
||||
free_channel(dev, channel);
|
||||
|
||||
|
|
@ -684,7 +686,7 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
}
|
||||
if (hcint & OTG_HCINT_CHH) {
|
||||
REBASE_CH(OTG_HCINT, channel) = OTG_HCINT_CHH;
|
||||
LOG_PRINTF("CHH");
|
||||
//LOG_PRINTF("CHH");
|
||||
free_channel(dev, channel);
|
||||
}
|
||||
|
||||
|
|
@ -694,12 +696,12 @@ static enum USBH_POLL_STATUS poll_run(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
|
||||
if (REBASE(OTG_GINTSTS) & OTG_GINTSTS_MMIS) {
|
||||
REBASE(OTG_GINTSTS) = OTG_GINTSTS_MMIS;
|
||||
LOG_PRINTF("Mode mismatch");
|
||||
//LOG_PRINTF("Mode mismatch");
|
||||
}
|
||||
|
||||
if (REBASE(OTG_GINTSTS) & OTG_GINTSTS_IPXFR) {
|
||||
REBASE(OTG_GINTSTS) = OTG_GINTSTS_IPXFR;
|
||||
LOG_PRINTF("IPXFR");
|
||||
//LOG_PRINTF("IPXFR");
|
||||
}
|
||||
|
||||
return USBH_POLL_STATUS_NONE;
|
||||
|
|
@ -847,7 +849,7 @@ static void poll_init(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
if (done) {
|
||||
dev->poll_sequence++;
|
||||
dev->timestamp_us = dev->time_curr_us;
|
||||
LOG_PRINTF("\t\t POLL SEQUENCE %d\n", dev->poll_sequence);
|
||||
//LOG_PRINTF("\t\t POLL SEQUENCE %d\n", dev->poll_sequence);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -859,7 +861,7 @@ static void poll_reset(usbh_lld_stm32f4_driver_data_t *dev)
|
|||
dev->state = dev->state_prev;
|
||||
dev->state_prev = DEVICE_STATE_RESET;
|
||||
|
||||
LOG_PRINTF("RESET");
|
||||
//LOG_PRINTF("RESET");
|
||||
} else {
|
||||
LOG_PRINTF("waiting %d < %d\n",dev->time_curr_us, dev->timestamp_us);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue