From b3babe2505fe4542e18967d90ccfc2783943243f Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sat, 16 May 2020 23:26:26 -0400 Subject: [PATCH] Initial passkey entry support. --- README.md | 8 +++++++ src/ble.c | 58 +++++++++++++++++++++++++++++++++++++++++++------- src/ble.h | 3 ++- src/handlers.c | 6 ++++++ src/keys.h | 1 + 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 311e782b..c2a982ca 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,14 @@ with a less restritive license and better BLE support, built on top of the [Zeph - Update the kscan GPIO driver to use interrupts. - Try disabling callbacks for the read pins temporarily when scanning, then re-enabling them. +# Missing Features + +- Mod Tap +- One Shot +- Shell over BLE? +- Split support + - custom kscan driver? that recieves events from other side over serial/BLE notifications? + ## Long Term - Tool to convert keymap `info.json` files into a DTS keymap file? diff --git a/src/ble.c b/src/ble.c index 172bb4a4..266185fe 100644 --- a/src/ble.c +++ b/src/ble.c @@ -1,4 +1,6 @@ +#include + #include #include #include @@ -6,6 +8,12 @@ #include #include +#include "keys.h" + +static struct bt_conn *auth_passkey_entry_conn; +static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; +static u8_t passkey_digit = 0; + static void connected(struct bt_conn *conn, u8_t err) { char addr[BT_ADDR_LE_STR_LEN]; @@ -75,7 +83,7 @@ static void auth_passkey_entry(struct bt_conn *conn) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Passkey entry requested for %s\n", addr); - // bt_conn_auth_passkey_entry(conn, 1234); + auth_passkey_entry_conn = bt_conn_ref(conn); } static void auth_cancel(struct bt_conn *conn) @@ -84,6 +92,14 @@ static void auth_cancel(struct bt_conn *conn) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + if (auth_passkey_entry_conn) + { + bt_conn_unref(auth_passkey_entry_conn); + auth_passkey_entry_conn = NULL; + } + + passkey_digit = 0; + printk("Pairing cancelled: %s\n", addr); } @@ -132,15 +148,41 @@ int zmk_ble_init() return err; } - // err = bt_passkey_set(1234); - if (err) - { - printk("Set passkey failed: %d\n", err); - return err; - } - bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); return 0; } + +bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) +{ + zmk_key key = key_event->key; + + if (!auth_passkey_entry_conn) + { + return true; + } + + if (key < KC_1 || key > KC_0) + { + return true; + } + + u32_t val = (key == KC_0) ? 0 : (key - KC_1 + 1); + + passkey_entries[passkey_digit++] = val; + + if (passkey_digit == 6) + { + u32_t passkey = 0; + for (int i = 5; i >= 0; i--) + { + passkey = (passkey * 10) + val; + } + bt_conn_auth_passkey_entry(auth_passkey_entry_conn, passkey); + bt_conn_unref(auth_passkey_entry_conn); + auth_passkey_entry_conn = NULL; + } + + return false; +} diff --git a/src/ble.h b/src/ble.h index bec3a24f..03b34fe9 100644 --- a/src/ble.h +++ b/src/ble.h @@ -1,4 +1,5 @@ #pragma once -int zmk_ble_init(); \ No newline at end of file +int zmk_ble_init(); +bool zmk_ble_handle_key_user(struct zmk_key_event *key_event); \ No newline at end of file diff --git a/src/handlers.c b/src/handlers.c index 9383e758..0ab93f69 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -1,6 +1,7 @@ #include "handlers.h" +#include "ble.h" #include "endpoints.h" __attribute__((weak)) bool zmk_handle_key_user(struct zmk_key_event *key_event) @@ -15,5 +16,10 @@ void zmk_handle_key(struct zmk_key_event key_event) return; } + if (!zmk_ble_handle_key_user(&key_event)) + { + return; + } + zmk_endpoints_send_key_event(key_event); }; diff --git a/src/keys.h b/src/keys.h index bfe8dd9a..b61ef811 100644 --- a/src/keys.h +++ b/src/keys.h @@ -1,6 +1,7 @@ #pragma once #include +#include typedef u64_t zmk_key;