From 3796f76c56d42ca9b4fd36edae7f6bf6656009b9 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 29 Jun 2020 00:37:11 -0400 Subject: [PATCH 01/20] Initial exploration of split BLE service. * Service for split peripheral to report position state to split central. * Updated advertising info. * Behavior for split BT until we have a proper event system. --- app/CMakeLists.txt | 2 + app/Kconfig | 19 +++++++ app/boards/shields/kyria/Kconfig.defconfig | 3 ++ app/boards/shields/kyria/kyria.dtsi | 1 + app/dts/behaviors/split_bt.dtsi | 9 ++++ .../behaviors/zmk,behavior-split-bt.yaml | 8 +++ app/include/zmk/split/bluetooth/service.h | 17 +++++++ app/src/ble.c | 7 ++- app/src/split/bluetooth/service.c | 49 +++++++++++++++++++ app/src/split_listener.c | 36 ++++++++++++++ 10 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 app/dts/behaviors/split_bt.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml create mode 100644 app/include/zmk/split/bluetooth/service.h create mode 100644 app/src/split/bluetooth/service.c create mode 100644 app/src/split_listener.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 5be628b7..3f2065f6 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -42,6 +42,8 @@ target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split_listener.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split/bluetooth/service.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c) target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) diff --git a/app/Kconfig b/app/Kconfig index e6dc3bd5..10d98e6c 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -72,6 +72,25 @@ endif endmenu + +menu "Split Support" + +config ZMK_SPLIT + bool "Split keyboard support" + default n + +if ZMK_SPLIT + +config ZMK_SPLIT_BLE + bool "Split keyboard support via BLE transport" + depends on ZMK_BLE + default y + +endif + +endmenu + + config ZMK_KSCAN_MOCK_DRIVER bool "Enable mock kscan driver to simulate key presses" default n diff --git a/app/boards/shields/kyria/Kconfig.defconfig b/app/boards/shields/kyria/Kconfig.defconfig index 25af5376..9ce12d96 100644 --- a/app/boards/shields/kyria/Kconfig.defconfig +++ b/app/boards/shields/kyria/Kconfig.defconfig @@ -4,4 +4,7 @@ if SHIELD_KYRIA_LEFT || SHIELD_KYRIA_RIGHT config ZMK_KEYBOARD_NAME default "Kyria" +config ZMK_SPLIT + default y + endif diff --git a/app/boards/shields/kyria/kyria.dtsi b/app/boards/shields/kyria/kyria.dtsi index f96adf0f..f2f2d750 100644 --- a/app/boards/shields/kyria/kyria.dtsi +++ b/app/boards/shields/kyria/kyria.dtsi @@ -4,6 +4,7 @@ * SPDX-License-Identifier: MIT */ +#include #include / { diff --git a/app/dts/behaviors/split_bt.dtsi b/app/dts/behaviors/split_bt.dtsi new file mode 100644 index 00000000..c31d9579 --- /dev/null +++ b/app/dts/behaviors/split_bt.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + split_behavior: behavior_split_bt { + compatible = "zmk,behavior-split-bt", "zmk,behavior-global"; + label = "SPLIT_BT"; + #binding-cells = <0>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml b/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml new file mode 100644 index 00000000..66d9630f --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2020, Pete Johanson +# SPDX-License-Identifier: MIT + +description: Split Bluetooth Behavior + +compatible: "zmk,behavior-split-bt" + +include: zero_param.yaml diff --git a/app/include/zmk/split/bluetooth/service.h b/app/include/zmk/split/bluetooth/service.h new file mode 100644 index 00000000..694afc16 --- /dev/null +++ b/app/include/zmk/split/bluetooth/service.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#ifndef BT_UUID_NUM_OF_DIGITALS +#define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) +#endif + +#define ZMK_SPLIT_BT_BASE_UUID 0x2a, 0x48, 0xc2, 0xb1, 0xcf, 0xc5, 0x67, 0xc9, 0x07, 0x71, 0x96, 0x00 +#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x00, 0x00, 0x00, 0x00 +#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x01, 0x00, 0x00, 0x00 + +#define ZMK_BT_UUID_SPLIT BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID) +#define ZMK_BT_UUID_SPLIT_POS_STATE BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID) + +int zmk_split_bt_position_pressed(u8_t position); +int zmk_split_bt_position_released(u8_t position); \ No newline at end of file diff --git a/app/src/ble.c b/app/src/ble.c index c0e81a96..94a23da9 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -12,6 +12,7 @@ #include #include +#include static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; @@ -121,9 +122,13 @@ static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { static const struct bt_data zmk_ble_ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), - BT_DATA_BYTES(BT_DATA_UUID16_ALL, + BT_DATA_BYTES(BT_DATA_UUID16_SOME, 0x12, 0x18, /* HID Service */ 0x0f, 0x18), /* Battery Service */ +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE) + BT_DATA_BYTES(BT_DATA_UUID128_SOME, + ZMK_SPLIT_BT_SERVICE_UUID) +#endif }; static void zmk_ble_ready(int err) diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c new file mode 100644 index 00000000..a669723d --- /dev/null +++ b/app/src/split/bluetooth/service.c @@ -0,0 +1,49 @@ + +#include +#include +#include + +#include +#include + +static u8_t num_of_positions = ZMK_KEYMAP_LEN; +static u8_t position_state[16]; + +static ssize_t split_svc_pos_state(struct bt_conn *conn, const struct bt_gatt_attr *attrs, void *buf, u16_t len, u16_t offset) +{ + return bt_gatt_attr_read(conn, attrs, buf, len, offset, &position_state, sizeof(position_state)); +} + +static ssize_t split_svc_num_of_positions(struct bt_conn *conn, const struct bt_gatt_attr *attrs, void *buf, u16_t len, u16_t offset) +{ + return bt_gatt_attr_read(conn, attrs, buf, len, offset, attrs->user_data, sizeof(u8_t)); +} + +static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, u16_t value) +{ +} + + +BT_GATT_SERVICE_DEFINE(split_svc, + BT_GATT_PRIMARY_SERVICE(ZMK_BT_UUID_SPLIT), + BT_GATT_CHARACTERISTIC(ZMK_BT_UUID_SPLIT_POS_STATE, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, + BT_GATT_PERM_READ_ENCRYPT, + split_svc_pos_state, NULL, &position_state), + BT_GATT_CCC(split_svc_pos_state_ccc, + BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), + BT_GATT_DESCRIPTOR(BT_UUID_NUM_OF_DIGITALS, BT_GATT_PERM_READ, + split_svc_num_of_positions, NULL, &num_of_positions), +); + +int zmk_split_bt_position_pressed(u8_t position) +{ + WRITE_BIT(position_state[position / 8], position % 8, true); + return bt_gatt_notify(NULL, &split_svc.attrs[1], &position_state, sizeof(position_state)); +} + +int zmk_split_bt_position_released(u8_t position) +{ + WRITE_BIT(position_state[position / 8], position % 8, false); + // WRITE_BIT(position_state, position, false); + return bt_gatt_notify(NULL, &split_svc.attrs[1], &position_state, sizeof(position_state)); +} \ No newline at end of file diff --git a/app/src/split_listener.c b/app/src/split_listener.c new file mode 100644 index 00000000..65f835a2 --- /dev/null +++ b/app/src/split_listener.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_split_listener + +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include +#include +#include +#include + +int split_listener(const struct zmk_event_header *eh) +{ + if (is_position_state_changed(eh)) { + const struct position_state_changed *ev = cast_position_state_changed(eh); + if (ev->state) { + zmk_split_bt_position_pressed(ev->position); + } else { + zmk_split_bt_position_released(ev->position); + } + } + return 0; +} + +ZMK_LISTENER(split_listener, split_listener); +ZMK_SUBSCRIPTION(split_listener, position_state_changed); \ No newline at end of file From a165db63586f37531d5050623bc5a7bc96793d49 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 1 Jul 2020 22:27:01 -0400 Subject: [PATCH 02/20] Initial work on split central support. --- app/CMakeLists.txt | 1 + app/include/zmk/split/bluetooth/service.h | 11 - app/include/zmk/split/bluetooth/uuid.h | 14 ++ app/src/ble.c | 2 +- app/src/split/bluetooth/central.c | 241 ++++++++++++++++++++++ app/src/split/bluetooth/service.c | 1 + 6 files changed, 258 insertions(+), 12 deletions(-) create mode 100644 app/include/zmk/split/bluetooth/uuid.h create mode 100644 app/src/split/bluetooth/central.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 3f2065f6..81c8ac46 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split/bluetooth/service.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split/bluetooth/central.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c) target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) diff --git a/app/include/zmk/split/bluetooth/service.h b/app/include/zmk/split/bluetooth/service.h index 694afc16..76af8980 100644 --- a/app/include/zmk/split/bluetooth/service.h +++ b/app/include/zmk/split/bluetooth/service.h @@ -2,16 +2,5 @@ #include -#ifndef BT_UUID_NUM_OF_DIGITALS -#define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) -#endif - -#define ZMK_SPLIT_BT_BASE_UUID 0x2a, 0x48, 0xc2, 0xb1, 0xcf, 0xc5, 0x67, 0xc9, 0x07, 0x71, 0x96, 0x00 -#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x00, 0x00, 0x00, 0x00 -#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x01, 0x00, 0x00, 0x00 - -#define ZMK_BT_UUID_SPLIT BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID) -#define ZMK_BT_UUID_SPLIT_POS_STATE BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID) - int zmk_split_bt_position_pressed(u8_t position); int zmk_split_bt_position_released(u8_t position); \ No newline at end of file diff --git a/app/include/zmk/split/bluetooth/uuid.h b/app/include/zmk/split/bluetooth/uuid.h new file mode 100644 index 00000000..4fa7f40c --- /dev/null +++ b/app/include/zmk/split/bluetooth/uuid.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#ifndef BT_UUID_NUM_OF_DIGITALS +#define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) +#endif + +#define ZMK_SPLIT_BT_BASE_UUID 0x2a, 0x48, 0xc2, 0xb1, 0xcf, 0xc5, 0x67, 0xc9, 0x07, 0x71, 0x96, 0x00 +#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x00, 0x00, 0x00, 0x00 +#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x01, 0x00, 0x00, 0x00 + +#define ZMK_BT_UUID_SPLIT BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID) +#define ZMK_BT_UUID_SPLIT_POS_STATE BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID) diff --git a/app/src/ble.c b/app/src/ble.c index 94a23da9..8b43cdd9 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -12,7 +12,7 @@ #include #include -#include +#include static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c new file mode 100644 index 00000000..1adc5c84 --- /dev/null +++ b/app/src/split/bluetooth/central.c @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include + +#include +#include +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +static int start_scan(void); + +static struct bt_conn *default_conn; + +static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); +static struct bt_gatt_discover_params discover_params; +static struct bt_gatt_subscribe_params subscribe_params; + +static u8_t notify_func(struct bt_conn *conn, + struct bt_gatt_subscribe_params *params, + const void *data, u16_t length) +{ + if (!data) { + LOG_DBG("[UNSUBSCRIBED]"); + params->value_handle = 0U; + return BT_GATT_ITER_STOP; + } + + LOG_DBG("[NOTIFICATION] data %p length %u", data, length); + + return BT_GATT_ITER_CONTINUE; +} + +static u8_t discover_func(struct bt_conn *conn, + const struct bt_gatt_attr *attr, + struct bt_gatt_discover_params *params) +{ + int err; + + if (!attr) { + LOG_DBG("Discover complete"); + (void)memset(params, 0, sizeof(*params)); + return BT_GATT_ITER_STOP; + } + + LOG_DBG("[ATTRIBUTE] handle %u", attr->handle); + + if (!bt_uuid_cmp(discover_params.uuid, ZMK_BT_UUID_SPLIT)) { + memcpy(&uuid, ZMK_BT_UUID_SPLIT_POS_STATE, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 1; + discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; + + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_ERR("Discover failed (err %d)", err); + } + } else if (!bt_uuid_cmp(discover_params.uuid, + ZMK_BT_UUID_SPLIT_POS_STATE)) { + memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.start_handle = attr->handle + 2; + discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR; + subscribe_params.value_handle = bt_gatt_attr_value_handle(attr); + + err = bt_gatt_discover(conn, &discover_params); + if (err) { + LOG_ERR("Discover failed (err %d)", err); + } + } else { + subscribe_params.notify = notify_func; + subscribe_params.value = BT_GATT_CCC_NOTIFY; + subscribe_params.ccc_handle = attr->handle; + + err = bt_gatt_subscribe(conn, &subscribe_params); + if (err && err != -EALREADY) { + LOG_ERR("Subscribe failed (err %d)", err); + } else { + LOG_DBG("[SUBSCRIBED]"); + } + + return BT_GATT_ITER_STOP; + } + + return BT_GATT_ITER_STOP; +} + +static bool eir_found(struct bt_data *data, void *user_data) +{ + bt_addr_le_t *addr = user_data; + int i; + + LOG_DBG("[AD]: %u data_len %u", data->type, data->data_len); + + switch (data->type) { + case BT_DATA_UUID128_SOME: + case BT_DATA_UUID128_ALL: + if (data->data_len % 16 != 0U) { + LOG_ERR("AD malformed"); + return true; + } + + for (i = 0; i < data->data_len; i += 16) { + struct bt_le_conn_param *param; + struct bt_uuid uuid; + int err; + + if (!bt_uuid_create(&uuid, &data->data[i], 16)) { + LOG_ERR("Unable to load UUID"); + continue; + } + + if (bt_uuid_cmp(&uuid, ZMK_BT_UUID_SPLIT)) { + continue; + } + + err = bt_le_scan_stop(); + if (err) { + LOG_ERR("Stop LE scan failed (err %d)", err); + continue; + } + + param = BT_LE_CONN_PARAM_DEFAULT; + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, + param, &default_conn); + if (err) { + LOG_ERR("Create conn failed (err %d)", err); + start_scan(); + } + + return false; + } + } + + return true; +} + +static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, + struct net_buf_simple *ad) +{ + char dev[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(addr, dev, sizeof(dev)); + LOG_DBG("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i", + dev, type, ad->len, rssi); + + /* We're only interested in connectable events */ + if (type == BT_GAP_ADV_TYPE_ADV_IND || + type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) { + bt_data_parse(ad, eir_found, (void *)addr); + } +} + +static int start_scan(void) +{ + int err; + + err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); + if (err) { + LOG_ERR("Scanning failed to start (err %d)", err); + return err; + } + + LOG_DBG("Scanning successfully started"); + return 0; +} + +static void connected(struct bt_conn *conn, u8_t conn_err) +{ + char addr[BT_ADDR_LE_STR_LEN]; + int err; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + if (conn_err) { + LOG_ERR("Failed to connect to %s (%u)", addr, conn_err); + + bt_conn_unref(default_conn); + default_conn = NULL; + + start_scan(); + return; + } + + LOG_DBG("Connected: %s", addr); + + if (conn == default_conn) { + memcpy(&uuid, BT_UUID_HRS, sizeof(uuid)); + discover_params.uuid = &uuid.uuid; + discover_params.func = discover_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; + + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; + } + } +} + +static void disconnected(struct bt_conn *conn, u8_t reason) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + LOG_DBG("Disconnected: %s (reason 0x%02x)", addr, reason); + + if (default_conn != conn) { + return; + } + + bt_conn_unref(default_conn); + default_conn = NULL; + + start_scan(); +} + +static struct bt_conn_cb conn_callbacks = { + .connected = connected, + .disconnected = disconnected, +}; + +int zmk_split_bt_central_init() +{ + bt_conn_cb_register(&conn_callbacks); + + return start_scan(); +} diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index a669723d..81fbfb52 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -4,6 +4,7 @@ #include #include +#include #include static u8_t num_of_positions = ZMK_KEYMAP_LEN; From be537d06565a38f5fcca8d5a1d0a7b7350b35d51 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 2 Jul 2020 23:34:11 -0400 Subject: [PATCH 03/20] Lots of work on split peripheral/central logic. --- app/CMakeLists.txt | 6 +++--- app/Kconfig | 16 ++++++++++++++++ app/include/zmk/split/bluetooth/central.h | 2 ++ app/include/zmk/split/bluetooth/uuid.h | 8 +++----- app/src/ble.c | 12 ++++++++++-- app/src/main.c | 11 +++++++++++ app/src/split/bluetooth/central.c | 13 ++++++++----- app/src/split/bluetooth/service.c | 6 +++--- 8 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 app/include/zmk/split/bluetooth/central.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 81c8ac46..1cee6b3e 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -42,9 +42,9 @@ target_sources(app PRIVATE src/behaviors/behavior_mod_tap.c) target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) -target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split_listener.c) -target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split/bluetooth/service.c) -target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE app PRIVATE src/split/bluetooth/central.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) +target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL app PRIVATE src/split/bluetooth/central.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_MOCK_DRIVER app PRIVATE src/kscan_mock.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_COMPOSITE_DRIVER app PRIVATE src/kscan_composite.c) target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) diff --git a/app/Kconfig b/app/Kconfig index 10d98e6c..2309d2d9 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -86,6 +86,22 @@ config ZMK_SPLIT_BLE depends on ZMK_BLE default y +if ZMK_SPLIT_BLE + +choice ZMK_SPLIT_BLE_ROLE + bool "BLE Role For Split Communication" + default ZMK_SPLIT_BLE_ROLE_CENTRAL + +config ZMK_SPLIT_BLE_ROLE_CENTRAL + bool "Central" + +config ZMK_SPLIT_BLE_ROLE_PERIPHERAL + bool "Peripheral" + +endchoice + +endif + endif endmenu diff --git a/app/include/zmk/split/bluetooth/central.h b/app/include/zmk/split/bluetooth/central.h new file mode 100644 index 00000000..09a7161b --- /dev/null +++ b/app/include/zmk/split/bluetooth/central.h @@ -0,0 +1,2 @@ + +int zmk_split_bt_central_init(); \ No newline at end of file diff --git a/app/include/zmk/split/bluetooth/uuid.h b/app/include/zmk/split/bluetooth/uuid.h index 4fa7f40c..59f2f712 100644 --- a/app/include/zmk/split/bluetooth/uuid.h +++ b/app/include/zmk/split/bluetooth/uuid.h @@ -6,9 +6,7 @@ #define BT_UUID_NUM_OF_DIGITALS BT_UUID_DECLARE_16(0x2909) #endif -#define ZMK_SPLIT_BT_BASE_UUID 0x2a, 0x48, 0xc2, 0xb1, 0xcf, 0xc5, 0x67, 0xc9, 0x07, 0x71, 0x96, 0x00 -#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x00, 0x00, 0x00, 0x00 -#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_SPLIT_BT_BASE_UUID, 0x01, 0x00, 0x00, 0x00 +#define ZMK_BT_SPLIT_UUID(num) BT_UUID_128_ENCODE(num, 0x0096, 0x7107, 0xc967, 0xc5cfb1c2482a) +#define ZMK_SPLIT_BT_SERVICE_UUID ZMK_BT_SPLIT_UUID(0x00000000) +#define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001) -#define ZMK_BT_UUID_SPLIT BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID) -#define ZMK_BT_UUID_SPLIT_POS_STATE BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID) diff --git a/app/src/ble.c b/app/src/ble.c index 8b43cdd9..3cbb14de 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -11,6 +11,11 @@ #include #include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + #include #include @@ -126,13 +131,14 @@ static const struct bt_data zmk_ble_ad[] = { 0x12, 0x18, /* HID Service */ 0x0f, 0x18), /* Battery Service */ #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE) - BT_DATA_BYTES(BT_DATA_UUID128_SOME, + BT_DATA_BYTES(BT_DATA_UUID128_ALL, ZMK_SPLIT_BT_SERVICE_UUID) #endif }; static void zmk_ble_ready(int err) { + LOG_DBG("ready? %d", err); if (err) { printk("Bluetooth init failed (err %d)\n", err); @@ -153,7 +159,7 @@ static int zmk_ble_init(struct device *_arg) { settings_load(); } - int err = bt_enable(zmk_ble_ready); + int err = bt_enable(NULL); if (err) { @@ -164,6 +170,8 @@ static int zmk_ble_init(struct device *_arg) bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); + zmk_ble_ready(0); + return 0; } diff --git a/app/src/main.c b/app/src/main.c index 92ecc8b1..4a5bd851 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -16,6 +16,10 @@ LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#ifdef CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL +#include +#endif /* CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL */ + #define ZMK_KSCAN_DEV DT_LABEL(ZMK_MATRIX_NODE_ID) void main(void) @@ -28,6 +32,13 @@ void main(void) } +#ifdef CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL + if (zmk_split_bt_central_init()) { + LOG_ERR("Failed to start BLE split central"); + return; + } +#endif /* CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL */ + #ifdef CONFIG_SETTINGS settings_load(); #endif diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 1adc5c84..9cad29d0 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -55,8 +55,8 @@ static u8_t discover_func(struct bt_conn *conn, LOG_DBG("[ATTRIBUTE] handle %u", attr->handle); - if (!bt_uuid_cmp(discover_params.uuid, ZMK_BT_UUID_SPLIT)) { - memcpy(&uuid, ZMK_BT_UUID_SPLIT_POS_STATE, sizeof(uuid)); + if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid)); discover_params.uuid = &uuid.uuid; discover_params.start_handle = attr->handle + 1; discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; @@ -66,7 +66,7 @@ static u8_t discover_func(struct bt_conn *conn, LOG_ERR("Discover failed (err %d)", err); } } else if (!bt_uuid_cmp(discover_params.uuid, - ZMK_BT_UUID_SPLIT_POS_STATE)) { + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) { memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid)); discover_params.uuid = &uuid.uuid; discover_params.start_handle = attr->handle + 2; @@ -113,6 +113,7 @@ static bool eir_found(struct bt_data *data, void *user_data) for (i = 0; i < data->data_len; i += 16) { struct bt_le_conn_param *param; struct bt_uuid uuid; + char uuid_str[BT_UUID_STR_LEN]; int err; if (!bt_uuid_create(&uuid, &data->data[i], 16)) { @@ -120,7 +121,9 @@ static bool eir_found(struct bt_data *data, void *user_data) continue; } - if (bt_uuid_cmp(&uuid, ZMK_BT_UUID_SPLIT)) { + if (bt_uuid_cmp(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + bt_uuid_to_str(&uuid, uuid_str, sizeof(uuid_str)); + LOG_DBG("UUID does not match split UUID: %s", log_strdup(uuid_str)); continue; } @@ -152,7 +155,7 @@ static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, bt_addr_le_to_str(addr, dev, sizeof(dev)); LOG_DBG("[DEVICE]: %s, AD evt type %u, AD data len %u, RSSI %i", - dev, type, ad->len, rssi); + log_strdup(dev), type, ad->len, rssi); /* We're only interested in connectable events */ if (type == BT_GAP_ADV_TYPE_ADV_IND || diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index 81fbfb52..e1d232a4 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -26,8 +27,8 @@ static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, u16_t value BT_GATT_SERVICE_DEFINE(split_svc, - BT_GATT_PRIMARY_SERVICE(ZMK_BT_UUID_SPLIT), - BT_GATT_CHARACTERISTIC(ZMK_BT_UUID_SPLIT_POS_STATE, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, + BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)), + BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_READ_ENCRYPT, split_svc_pos_state, NULL, &position_state), BT_GATT_CCC(split_svc_pos_state_ccc, @@ -45,6 +46,5 @@ int zmk_split_bt_position_pressed(u8_t position) int zmk_split_bt_position_released(u8_t position) { WRITE_BIT(position_state[position / 8], position % 8, false); - // WRITE_BIT(position_state, position, false); return bt_gatt_notify(NULL, &split_svc.attrs[1], &position_state, sizeof(position_state)); } \ No newline at end of file From d1a5c7ee5ab7b3541ac45c4eef4c17d71475d645 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 13 Jul 2020 23:37:25 -0400 Subject: [PATCH 04/20] Swtich to SYS_INIT. --- app/src/split/bluetooth/central.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 9cad29d0..c370ea0f 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -17,6 +17,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#include static int start_scan(void); @@ -236,9 +237,13 @@ static struct bt_conn_cb conn_callbacks = { .disconnected = disconnected, }; -int zmk_split_bt_central_init() +int zmk_split_bt_central_init(struct device *_arg) { bt_conn_cb_register(&conn_callbacks); return start_scan(); } + +SYS_INIT(zmk_split_bt_central_init, + APPLICATION, + CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file From d74efb331ac32c1a65d20193d06b2cabb858b594 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 14 Jul 2020 12:14:15 -0400 Subject: [PATCH 05/20] Kconfig fixes for split roles. --- app/Kconfig | 2 ++ app/boards/shields/kyria/kyria_left.conf | 2 ++ app/boards/shields/kyria/kyria_right.conf | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 app/boards/shields/kyria/kyria_left.conf create mode 100644 app/boards/shields/kyria/kyria_right.conf diff --git a/app/Kconfig b/app/Kconfig index 2309d2d9..5881aaeb 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -94,6 +94,8 @@ choice ZMK_SPLIT_BLE_ROLE config ZMK_SPLIT_BLE_ROLE_CENTRAL bool "Central" + select BT_CENTRAL + select BT_GATT_CLIENT config ZMK_SPLIT_BLE_ROLE_PERIPHERAL bool "Peripheral" diff --git a/app/boards/shields/kyria/kyria_left.conf b/app/boards/shields/kyria/kyria_left.conf new file mode 100644 index 00000000..e51dee44 --- /dev/null +++ b/app/boards/shields/kyria/kyria_left.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL=y \ No newline at end of file diff --git a/app/boards/shields/kyria/kyria_right.conf b/app/boards/shields/kyria/kyria_right.conf new file mode 100644 index 00000000..a835adc1 --- /dev/null +++ b/app/boards/shields/kyria/kyria_right.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y \ No newline at end of file From d4afd989f35c46e43468dab1a19bfab9dcf34996 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 15 Jul 2020 23:26:24 -0400 Subject: [PATCH 06/20] More split implementation. * Propogate key position state changes on central. * Various BLE tweaks. --- app/Kconfig | 2 +- app/src/main.c | 8 --- app/src/split/bluetooth/central.c | 113 ++++++++++++++++++++++-------- 3 files changed, 84 insertions(+), 39 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 5881aaeb..8ae10ad2 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -126,7 +126,7 @@ config ZMK_ACTION_MOD_TAP endmenu config HEAP_MEM_POOL_SIZE - default 200 + default 1024 module = ZMK module-str = zmk diff --git a/app/src/main.c b/app/src/main.c index 4a5bd851..b7d3a4b8 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -31,14 +31,6 @@ void main(void) return; } - -#ifdef CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL - if (zmk_split_bt_central_init()) { - LOG_ERR("Failed to start BLE split central"); - return; - } -#endif /* CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL */ - #ifdef CONFIG_SETTINGS settings_load(); #endif diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index c370ea0f..7595ee26 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -17,13 +17,15 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#include +#include #include static int start_scan(void); static struct bt_conn *default_conn; -static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0); +static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; @@ -31,6 +33,10 @@ static u8_t notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, u16_t length) { + static u8_t position_state[16]; + + u8_t changed_positions[16]; + if (!data) { LOG_DBG("[UNSUBSCRIBED]"); params->value_handle = 0U; @@ -39,6 +45,27 @@ static u8_t notify_func(struct bt_conn *conn, LOG_DBG("[NOTIFICATION] data %p length %u", data, length); + for (int i = 0; i < 16; i++) { + changed_positions[i] = ((u8_t *)data)[i] ^ position_state[i]; + position_state[i] = ((u8_t *)data)[i]; + } + + for (int i = 0; i < 16; i++) { + for (int j = 0; j < 8; j++) { + if (changed_positions[i] & BIT(j)) { + u32_t position = (i * 8) + j; + bool pressed = position_state[i] & BIT(j); + struct position_state_changed *pos_ev = new_position_state_changed(); + pos_ev->position = position; + pos_ev->state = pressed; + + LOG_DBG("Trigger key position state change for %d", position); + ZMK_EVENT_RAISE(pos_ev); + } + } + } + + return BT_GATT_ITER_CONTINUE; } @@ -96,6 +123,32 @@ static u8_t discover_func(struct bt_conn *conn, return BT_GATT_ITER_STOP; } +static void split_central_process_connection(struct bt_conn *conn) { + int err; + + LOG_DBG("Current security for connection: %d", bt_conn_get_security(conn)); + + err = bt_conn_set_security(conn, BT_SECURITY_L2); + if (err) { + LOG_ERR("Failed to set security (reason %d)", err); + return; + } + + if (conn == default_conn) { + discover_params.uuid = &uuid.uuid; + discover_params.func = discover_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; + + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; + } + } +} + static bool eir_found(struct bt_data *data, void *user_data) { bt_addr_le_t *addr = user_data; @@ -114,7 +167,6 @@ static bool eir_found(struct bt_data *data, void *user_data) for (i = 0; i < data->data_len; i += 16) { struct bt_le_conn_param *param; struct bt_uuid uuid; - char uuid_str[BT_UUID_STR_LEN]; int err; if (!bt_uuid_create(&uuid, &data->data[i], 16)) { @@ -122,24 +174,36 @@ static bool eir_found(struct bt_data *data, void *user_data) continue; } - if (bt_uuid_cmp(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + if (!bt_uuid_cmp(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { + char uuid_str[BT_UUID_STR_LEN]; + char service_uuid_str[BT_UUID_STR_LEN]; + bt_uuid_to_str(&uuid, uuid_str, sizeof(uuid_str)); - LOG_DBG("UUID does not match split UUID: %s", log_strdup(uuid_str)); + bt_uuid_to_str(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID), service_uuid_str, sizeof(service_uuid_str)); + LOG_DBG("UUID %s does not match split UUID: %s", log_strdup(uuid_str), log_strdup(service_uuid_str)); continue; } + LOG_DBG("Found the split service"); + err = bt_le_scan_stop(); if (err) { LOG_ERR("Stop LE scan failed (err %d)", err); continue; } - param = BT_LE_CONN_PARAM_DEFAULT; - err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, - param, &default_conn); - if (err) { - LOG_ERR("Create conn failed (err %d)", err); - start_scan(); + default_conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr); + if (default_conn) { + LOG_DBG("Found existing connection"); + split_central_process_connection(default_conn); + } else { + param = BT_LE_CONN_PARAM_DEFAULT; + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, + param, &default_conn); + if (err) { + LOG_ERR("Create conn failed (err %d)", err); + start_scan(); + } } return false; @@ -179,7 +243,7 @@ static int start_scan(void) return 0; } -static void connected(struct bt_conn *conn, u8_t conn_err) +static void split_central_connected(struct bt_conn *conn, u8_t conn_err) { char addr[BT_ADDR_LE_STR_LEN]; int err; @@ -196,31 +260,20 @@ static void connected(struct bt_conn *conn, u8_t conn_err) return; } - LOG_DBG("Connected: %s", addr); + LOG_DBG("Connected: %s", log_strdup(addr)); - if (conn == default_conn) { - memcpy(&uuid, BT_UUID_HRS, sizeof(uuid)); - discover_params.uuid = &uuid.uuid; - discover_params.func = discover_func; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; + - err = bt_gatt_discover(default_conn, &discover_params); - if (err) { - LOG_ERR("Discover failed(err %d)", err); - return; - } - } + split_central_process_connection(conn); } -static void disconnected(struct bt_conn *conn, u8_t reason) +static void split_central_disconnected(struct bt_conn *conn, u8_t reason) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - LOG_DBG("Disconnected: %s (reason 0x%02x)", addr, reason); + LOG_DBG("Disconnected: %s (reason %d)", log_strdup(addr), reason); if (default_conn != conn) { return; @@ -233,8 +286,8 @@ static void disconnected(struct bt_conn *conn, u8_t reason) } static struct bt_conn_cb conn_callbacks = { - .connected = connected, - .disconnected = disconnected, + .connected = split_central_connected, + .disconnected = split_central_disconnected, }; int zmk_split_bt_central_init(struct device *_arg) @@ -246,4 +299,4 @@ int zmk_split_bt_central_init(struct device *_arg) SYS_INIT(zmk_split_bt_central_init, APPLICATION, - CONFIG_APPLICATION_INIT_PRIORITY); \ No newline at end of file + CONFIG_ZMK_BLE_INIT_PRIORITY); \ No newline at end of file From 0321c490b5d0d087e76c06185a2940420cf7ab4c Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 15 Jul 2020 23:26:35 -0400 Subject: [PATCH 07/20] Disable USB on the peripheral size. --- app/boards/shields/kyria/kyria_right.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/boards/shields/kyria/kyria_right.conf b/app/boards/shields/kyria/kyria_right.conf index a835adc1..99d6c4e2 100644 --- a/app/boards/shields/kyria/kyria_right.conf +++ b/app/boards/shields/kyria/kyria_right.conf @@ -1,2 +1,3 @@ CONFIG_ZMK_SPLIT=y -CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y \ No newline at end of file +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y +CONFIG_ZMK_USB=n \ No newline at end of file From 6701b7babc68cb8090a9d16105bd3876025aa0e8 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 16 Jul 2020 15:50:41 -0400 Subject: [PATCH 08/20] Working BT settings. --- app/Kconfig | 22 +++++++++++--------- app/boards/arm/nice_nano/nice_nano_defconfig | 7 +++++++ app/boards/shields/kyria/Kconfig.defconfig | 16 ++++++++++++-- app/src/ble.c | 14 ++++++++----- app/src/main.c | 8 ------- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 8ae10ad2..c2a753f2 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -40,6 +40,8 @@ menuconfig ZMK_BLE select BT_PERIPHERAL select BT_GATT_DIS select BT_GATT_BAS + select SETTINGS + select BT_SETTINGS if ZMK_BLE @@ -47,6 +49,9 @@ config ZMK_BLE_INIT_PRIORITY int "Init Priority" default 50 +config SYSTEM_WORKQUEUE_STACK_SIZE + default 2048 + # HID GATT notifications sent this way are *not* picked up by Linux, and possibly others. config BT_GATT_NOTIFY_MULTIPLE default n @@ -58,16 +63,6 @@ config ZMK_BLE_PASSKEY_ENTRY bool "Experimental: Requiring typing passkey from host to pair BLE connection" default n -# Incresed stack due to settings API usage -# CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 -# -# CONFIG_BT_SETTINGS=y -# CONFIG_FLASH=y -# CONFIG_FLASH_PAGE_LAYOUT=y -# CONFIG_FLASH_MAP=y -# CONFIG_NVS=y -# CONFIG_SETTINGS=y - endif endmenu @@ -97,6 +92,13 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL select BT_CENTRAL select BT_GATT_CLIENT +if ZMK_SPLIT_BLE_ROLE_CENTRAL + +config BT_MAX_CONN + default 2 + +endif + config ZMK_SPLIT_BLE_ROLE_PERIPHERAL bool "Peripheral" diff --git a/app/boards/arm/nice_nano/nice_nano_defconfig b/app/boards/arm/nice_nano/nice_nano_defconfig index f9be0ee8..393d61fe 100644 --- a/app/boards/arm/nice_nano/nice_nano_defconfig +++ b/app/boards/arm/nice_nano/nice_nano_defconfig @@ -11,3 +11,10 @@ CONFIG_ARM_MPU=y CONFIG_GPIO=y CONFIG_USE_DT_CODE_PARTITION=y + +CONFIG_MPU_ALLOW_FLASH_WRITE=y +CONFIG_NVS=y +CONFIG_SETTINGS_NVS=y +CONFIG_FLASH=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_FLASH_MAP=y \ No newline at end of file diff --git a/app/boards/shields/kyria/Kconfig.defconfig b/app/boards/shields/kyria/Kconfig.defconfig index 9ce12d96..bc0a7b82 100644 --- a/app/boards/shields/kyria/Kconfig.defconfig +++ b/app/boards/shields/kyria/Kconfig.defconfig @@ -1,8 +1,20 @@ -if SHIELD_KYRIA_LEFT || SHIELD_KYRIA_RIGHT +if SHIELD_KYRIA_LEFT config ZMK_KEYBOARD_NAME - default "Kyria" + default "Kyria Left" + +endif + + +if SHIELD_KYRIA_RIGHT + +config ZMK_KEYBOARD_NAME + default "Kyria Right" + +endif + +if SHIELD_KYRIA_LEFT || SHIELD_KYRIA_RIGHT config ZMK_SPLIT default y diff --git a/app/src/ble.c b/app/src/ble.c index 3cbb14de..809575d2 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -128,8 +128,11 @@ static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { static const struct bt_data zmk_ble_ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_SOME, +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE) 0x12, 0x18, /* HID Service */ - 0x0f, 0x18), /* Battery Service */ +#endif + 0x0f, 0x18 /* Battery Service */ + ), #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE) BT_DATA_BYTES(BT_DATA_UUID128_ALL, ZMK_SPLIT_BT_SERVICE_UUID) @@ -155,10 +158,6 @@ static void zmk_ble_ready(int err) static int zmk_ble_init(struct device *_arg) { - if (IS_ENABLED(CONFIG_SETTINGS)) - { - settings_load(); - } int err = bt_enable(NULL); if (err) @@ -167,6 +166,11 @@ static int zmk_ble_init(struct device *_arg) return err; } + if (IS_ENABLED(CONFIG_BT_SETTINGS)) + { + settings_load(); + } + bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); diff --git a/app/src/main.c b/app/src/main.c index b7d3a4b8..e7553042 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -16,10 +16,6 @@ LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include -#ifdef CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL -#include -#endif /* CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL */ - #define ZMK_KSCAN_DEV DT_LABEL(ZMK_MATRIX_NODE_ID) void main(void) @@ -30,8 +26,4 @@ void main(void) { return; } - -#ifdef CONFIG_SETTINGS - settings_load(); -#endif } From f4cb3a7cba8f4593f3103470b0c790cf68449ea0 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 16 Jul 2020 16:01:04 -0400 Subject: [PATCH 09/20] Clean up old global behavior logic. --- app/boards/shields/kyria/kyria.dtsi | 1 - app/dts/behaviors/split_bt.dtsi | 9 --------- 2 files changed, 10 deletions(-) delete mode 100644 app/dts/behaviors/split_bt.dtsi diff --git a/app/boards/shields/kyria/kyria.dtsi b/app/boards/shields/kyria/kyria.dtsi index f2f2d750..f96adf0f 100644 --- a/app/boards/shields/kyria/kyria.dtsi +++ b/app/boards/shields/kyria/kyria.dtsi @@ -4,7 +4,6 @@ * SPDX-License-Identifier: MIT */ -#include #include / { diff --git a/app/dts/behaviors/split_bt.dtsi b/app/dts/behaviors/split_bt.dtsi deleted file mode 100644 index c31d9579..00000000 --- a/app/dts/behaviors/split_bt.dtsi +++ /dev/null @@ -1,9 +0,0 @@ -/ { - behaviors { - split_behavior: behavior_split_bt { - compatible = "zmk,behavior-split-bt", "zmk,behavior-global"; - label = "SPLIT_BT"; - #binding-cells = <0>; - }; - }; -}; From 7778ceec9214a32be5a22848e9d4978f452e41ba Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 16 Jul 2020 18:50:08 -0400 Subject: [PATCH 10/20] One more lingering global behavior bit. --- app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml diff --git a/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml b/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml deleted file mode 100644 index 66d9630f..00000000 --- a/app/dts/bindings/behaviors/zmk,behavior-split-bt.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# Copyright (c) 2020, Pete Johanson -# SPDX-License-Identifier: MIT - -description: Split Bluetooth Behavior - -compatible: "zmk,behavior-split-bt" - -include: zero_param.yaml From eb0bf2337ee988cb5b094db814beb0db8271e908 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 16 Jul 2020 18:52:04 -0400 Subject: [PATCH 11/20] More cleanup of BLE split code. --- app/include/zmk/split/bluetooth/central.h | 2 -- app/include/zmk/split/bluetooth/service.h | 2 -- 2 files changed, 4 deletions(-) delete mode 100644 app/include/zmk/split/bluetooth/central.h diff --git a/app/include/zmk/split/bluetooth/central.h b/app/include/zmk/split/bluetooth/central.h deleted file mode 100644 index 09a7161b..00000000 --- a/app/include/zmk/split/bluetooth/central.h +++ /dev/null @@ -1,2 +0,0 @@ - -int zmk_split_bt_central_init(); \ No newline at end of file diff --git a/app/include/zmk/split/bluetooth/service.h b/app/include/zmk/split/bluetooth/service.h index 76af8980..954e0cdd 100644 --- a/app/include/zmk/split/bluetooth/service.h +++ b/app/include/zmk/split/bluetooth/service.h @@ -1,6 +1,4 @@ #pragma once -#include - int zmk_split_bt_position_pressed(u8_t position); int zmk_split_bt_position_released(u8_t position); \ No newline at end of file From 339a15881a07c5b6e05c39ccdb8a312ba53042ff Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 17 Jul 2020 16:42:02 -0400 Subject: [PATCH 12/20] Tweaks for turning off USB for split peripherals. --- app/Kconfig | 7 +++++++ app/boards/shields/kyria/kyria_right.conf | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index c2a753f2..aa8be605 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -102,6 +102,13 @@ endif config ZMK_SPLIT_BLE_ROLE_PERIPHERAL bool "Peripheral" +if ZMK_SPLIT_BLE_ROLE_PERIPHERAL + +config ZMK_USB + default n + +endif + endchoice endif diff --git a/app/boards/shields/kyria/kyria_right.conf b/app/boards/shields/kyria/kyria_right.conf index 99d6c4e2..a835adc1 100644 --- a/app/boards/shields/kyria/kyria_right.conf +++ b/app/boards/shields/kyria/kyria_right.conf @@ -1,3 +1,2 @@ CONFIG_ZMK_SPLIT=y -CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y -CONFIG_ZMK_USB=n \ No newline at end of file +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y \ No newline at end of file From 5b61c211eaace2174fd68a51a1972e29b14cd5a6 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 17 Jul 2020 22:44:35 -0400 Subject: [PATCH 13/20] Tweaks for split Lily58 sync. --- app/boards/shields/lily58/lily58_left.conf | 2 ++ app/boards/shields/lily58/lily58_right.conf | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 app/boards/shields/lily58/lily58_left.conf create mode 100644 app/boards/shields/lily58/lily58_right.conf diff --git a/app/boards/shields/lily58/lily58_left.conf b/app/boards/shields/lily58/lily58_left.conf new file mode 100644 index 00000000..e51dee44 --- /dev/null +++ b/app/boards/shields/lily58/lily58_left.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL=y \ No newline at end of file diff --git a/app/boards/shields/lily58/lily58_right.conf b/app/boards/shields/lily58/lily58_right.conf new file mode 100644 index 00000000..990cf7c0 --- /dev/null +++ b/app/boards/shields/lily58/lily58_right.conf @@ -0,0 +1,2 @@ +CONFIG_ZMK_SPLIT=y +CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL=y From 64b9963bca66ec0d3d919e7a8d35d485fa2002e9 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 17 Jul 2020 23:02:00 -0400 Subject: [PATCH 14/20] Name each Lily58 side differently. --- app/boards/shields/lily58/Kconfig.defconfig | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/boards/shields/lily58/Kconfig.defconfig b/app/boards/shields/lily58/Kconfig.defconfig index 9ec58344..8f278b03 100644 --- a/app/boards/shields/lily58/Kconfig.defconfig +++ b/app/boards/shields/lily58/Kconfig.defconfig @@ -1,7 +1,14 @@ -if SHIELD_LILY58_LEFT || SHIELD_LILY58_RIGHT +if SHIELD_LILY58_LEFT config ZMK_KEYBOARD_NAME - default "Lily58" + default "Lily58 Left" + +endif + +if SHIELD_LILY58_RIGHT + +config ZMK_KEYBOARD_NAME + default "Lily58 Right" endif From 1951883def78d9771221a347a4a61c176907433c Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 17 Jul 2020 23:04:47 -0400 Subject: [PATCH 15/20] Bump max BT connections for testing. --- app/Kconfig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index aa8be605..997409ef 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -95,7 +95,7 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL if ZMK_SPLIT_BLE_ROLE_CENTRAL config BT_MAX_CONN - default 2 + default 5 endif @@ -107,6 +107,10 @@ if ZMK_SPLIT_BLE_ROLE_PERIPHERAL config ZMK_USB default n + +config BT_MAX_CONN + default 5 + endif endchoice From 879fd5b8e7e13e18c35bfe8a21a1919d0d1b1a5b Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 19 Jul 2020 22:20:42 -0400 Subject: [PATCH 16/20] Connection params tweaks. --- app/src/split/bluetooth/central.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 7595ee26..cf77e0fd 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -197,7 +197,7 @@ static bool eir_found(struct bt_data *data, void *user_data) LOG_DBG("Found existing connection"); split_central_process_connection(default_conn); } else { - param = BT_LE_CONN_PARAM_DEFAULT; + param = BT_LE_CONN_PARAM(0x0005, 0x000a, 5, 400); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { From 2a6b9ec86e99a873b1ecebbb66955b5f93a899b1 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 19 Jul 2020 22:33:15 -0400 Subject: [PATCH 17/20] Fixed min/max. --- app/src/split/bluetooth/central.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index cf77e0fd..1fce9db6 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -197,7 +197,7 @@ static bool eir_found(struct bt_data *data, void *user_data) LOG_DBG("Found existing connection"); split_central_process_connection(default_conn); } else { - param = BT_LE_CONN_PARAM(0x0005, 0x000a, 5, 400); + param = BT_LE_CONN_PARAM(0x0006, 0x000c, 5, 400); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { From fd407c4876ddb3af8f0490051cd81620c545ebad Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 19 Jul 2020 22:40:49 -0400 Subject: [PATCH 18/20] Update connectin params once we're connected to. --- app/src/ble.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/ble.c b/app/src/ble.c index 809575d2..af3c25d2 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -37,6 +37,8 @@ static void connected(struct bt_conn *conn, u8_t err) printk("Connected %s\n", addr); + bt_conn_le_param_update(conn, BT_LE_CONN_PARAM(0x0006, 0x000c, 5, 400)); + if (bt_conn_set_security(conn, BT_SECURITY_L2)) { printk("Failed to set security\n"); From 542a9de48a39c08586cb72aa975b5da0dd9a4bad Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 20 Jul 2020 22:52:37 -0400 Subject: [PATCH 19/20] Replace magic 16 with sane constant. --- app/src/split/bluetooth/central.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 1fce9db6..066835f3 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -23,6 +23,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static int start_scan(void); +#define POSITION_STATE_DATA_LEN 16 + static struct bt_conn *default_conn; static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID); @@ -33,9 +35,9 @@ static u8_t notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, u16_t length) { - static u8_t position_state[16]; + static u8_t position_state[POSITION_STATE_DATA_LEN]; - u8_t changed_positions[16]; + u8_t changed_positions[POSITION_STATE_DATA_LEN]; if (!data) { LOG_DBG("[UNSUBSCRIBED]"); @@ -45,12 +47,12 @@ static u8_t notify_func(struct bt_conn *conn, LOG_DBG("[NOTIFICATION] data %p length %u", data, length); - for (int i = 0; i < 16; i++) { + for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { changed_positions[i] = ((u8_t *)data)[i] ^ position_state[i]; position_state[i] = ((u8_t *)data)[i]; } - for (int i = 0; i < 16; i++) { + for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { for (int j = 0; j < 8; j++) { if (changed_positions[i] & BIT(j)) { u32_t position = (i * 8) + j; From c4d3c03eb069c814321ab49a809dc584f1819b34 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 20 Jul 2020 23:02:27 -0400 Subject: [PATCH 20/20] Improved function naming for central functions. --- app/src/split/bluetooth/central.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 066835f3..b6d72221 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -31,7 +31,7 @@ static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; -static u8_t notify_func(struct bt_conn *conn, +static u8_t split_central_notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, u16_t length) { @@ -71,7 +71,7 @@ static u8_t notify_func(struct bt_conn *conn, return BT_GATT_ITER_CONTINUE; } -static u8_t discover_func(struct bt_conn *conn, +static u8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params) { @@ -108,7 +108,7 @@ static u8_t discover_func(struct bt_conn *conn, LOG_ERR("Discover failed (err %d)", err); } } else { - subscribe_params.notify = notify_func; + subscribe_params.notify = split_central_notify_func; subscribe_params.value = BT_GATT_CCC_NOTIFY; subscribe_params.ccc_handle = attr->handle; @@ -138,7 +138,7 @@ static void split_central_process_connection(struct bt_conn *conn) { if (conn == default_conn) { discover_params.uuid = &uuid.uuid; - discover_params.func = discover_func; + discover_params.func = split_central_discovery_func; discover_params.start_handle = 0x0001; discover_params.end_handle = 0xffff; discover_params.type = BT_GATT_DISCOVER_PRIMARY; @@ -151,7 +151,7 @@ static void split_central_process_connection(struct bt_conn *conn) { } } -static bool eir_found(struct bt_data *data, void *user_data) +static bool split_central_eir_found(struct bt_data *data, void *user_data) { bt_addr_le_t *addr = user_data; int i; @@ -215,7 +215,7 @@ static bool eir_found(struct bt_data *data, void *user_data) return true; } -static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, +static void split_central_device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, struct net_buf_simple *ad) { char dev[BT_ADDR_LE_STR_LEN]; @@ -227,7 +227,7 @@ static void device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type, /* We're only interested in connectable events */ if (type == BT_GAP_ADV_TYPE_ADV_IND || type == BT_GAP_ADV_TYPE_ADV_DIRECT_IND) { - bt_data_parse(ad, eir_found, (void *)addr); + bt_data_parse(ad, split_central_eir_found, (void *)addr); } } @@ -235,7 +235,7 @@ static int start_scan(void) { int err; - err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); + err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, split_central_device_found); if (err) { LOG_ERR("Scanning failed to start (err %d)", err); return err; @@ -248,7 +248,6 @@ static int start_scan(void) static void split_central_connected(struct bt_conn *conn, u8_t conn_err) { char addr[BT_ADDR_LE_STR_LEN]; - int err; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); @@ -262,9 +261,7 @@ static void split_central_connected(struct bt_conn *conn, u8_t conn_err) return; } - LOG_DBG("Connected: %s", log_strdup(addr)); - - + LOG_DBG("Connected: %s", log_strdup(addr)); split_central_process_connection(conn); }