Browse Source

Initial work on split central support.

xmkb
Pete Johanson 5 years ago
parent
commit
a165db6358
  1. 1
      app/CMakeLists.txt
  2. 11
      app/include/zmk/split/bluetooth/service.h
  3. 14
      app/include/zmk/split/bluetooth/uuid.h
  4. 2
      app/src/ble.c
  5. 241
      app/src/split/bluetooth/central.c
  6. 1
      app/src/split/bluetooth/service.c

1
app/CMakeLists.txt

@ -44,6 +44,7 @@ target_sources(app PRIVATE src/behaviors/behavior_transparent.c) @@ -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)

11
app/include/zmk/split/bluetooth/service.h

@ -2,16 +2,5 @@ @@ -2,16 +2,5 @@
#include <bluetooth/uuid.h>
#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);

14
app/include/zmk/split/bluetooth/uuid.h

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
#pragma once
#include <bluetooth/uuid.h>
#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)

2
app/src/ble.c

@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
#include <bluetooth/gatt.h>
#include <zmk/keys.h>
#include <zmk/split/bluetooth/service.h>
#include <zmk/split/bluetooth/uuid.h>
static struct bt_conn *auth_passkey_entry_conn;
static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0};

241
app/src/split/bluetooth/central.c

@ -0,0 +1,241 @@ @@ -0,0 +1,241 @@
/*
* Copyright (c) 2020 Peter Johanson
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/types.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <sys/byteorder.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/split/bluetooth/uuid.h>
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();
}

1
app/src/split/bluetooth/service.c

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
#include <bluetooth/gatt.h>
#include <zmk/matrix.h>
#include <zmk/split/bluetooth/uuid.h>
#include <zmk/split/bluetooth/service.h>
static u8_t num_of_positions = ZMK_KEYMAP_LEN;

Loading…
Cancel
Save