Lots of work on split peripheral/central logic.

This commit is contained in:
Pete Johanson 2020-07-02 23:34:11 -04:00
parent a165db6358
commit be537d0656
8 changed files with 56 additions and 18 deletions

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,2 @@
int zmk_split_bt_central_init();

View file

@ -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)

View file

@ -11,6 +11,11 @@
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/keys.h>
#include <zmk/split/bluetooth/uuid.h>
@ -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;
}

View file

@ -16,6 +16,10 @@ LOG_MODULE_REGISTER(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/kscan.h>
#include <zmk/endpoints.h>
#ifdef CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL
#include <zmk/split/bluetooth/central.h>
#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

View file

@ -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 ||

View file

@ -2,6 +2,7 @@
#include <zephyr/types.h>
#include <sys/util.h>
#include <bluetooth/gatt.h>
#include <bluetooth/uuid.h>
#include <zmk/matrix.h>
#include <zmk/split/bluetooth/uuid.h>
@ -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));
}