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

313 lines
7.9 KiB
C
Raw Normal View History

2020-07-02 12:27:01 +10:00
/*
* 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>
#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
2020-07-14 13:37:25 +10:00
#include <init.h>
2020-07-02 12:27:01 +10:00
static int start_scan(void);
2020-07-21 12:52:37 +10:00
#define POSITION_STATE_DATA_LEN 16
2020-07-02 12:27:01 +10:00
static struct bt_conn *default_conn;
static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);
2020-07-02 12:27:01 +10:00
static struct bt_gatt_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_params;
static u8_t split_central_notify_func(struct bt_conn *conn,
2020-07-02 12:27:01 +10:00
struct bt_gatt_subscribe_params *params,
const void *data, u16_t length)
{
2020-07-21 12:52:37 +10:00
static u8_t position_state[POSITION_STATE_DATA_LEN];
2020-07-21 12:52:37 +10:00
u8_t changed_positions[POSITION_STATE_DATA_LEN];
2020-07-02 12:27:01 +10:00
if (!data) {
LOG_DBG("[UNSUBSCRIBED]");
params->value_handle = 0U;
return BT_GATT_ITER_STOP;
}
LOG_DBG("[NOTIFICATION] data %p length %u", data, length);
2020-07-21 12:52:37 +10:00
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];
}
2020-07-21 12:52:37 +10:00
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;
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);
}
}
}
2020-07-02 12:27:01 +10:00
return BT_GATT_ITER_CONTINUE;
}
static u8_t split_central_discovery_func(struct bt_conn *conn,
2020-07-02 12:27:01 +10:00
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, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) {
memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid));
2020-07-02 12:27:01 +10:00
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,
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) {
2020-07-02 12:27:01 +10:00
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 = split_central_notify_func;
2020-07-02 12:27:01 +10:00
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 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 = split_central_discovery_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;
}
}
2020-08-03 05:51:42 +10:00
struct bt_conn_info info;
bt_conn_get_info(conn, &info);
LOG_DBG("New connection params: Interval: %d, Latency: %d, PHY: %d", info.le.interval, info.le.latency, info.le.phy->rx_phy);
}
static bool split_central_eir_found(struct bt_data *data, void *user_data)
2020-07-02 12:27:01 +10:00
{
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, 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));
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));
2020-07-02 12:27:01 +10:00
continue;
}
LOG_DBG("Found the split service");
2020-07-02 12:27:01 +10:00
err = bt_le_scan_stop();
if (err) {
LOG_ERR("Stop LE scan failed (err %d)", err);
continue;
}
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 {
2020-08-03 05:51:42 +10:00
param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400);
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();
}
2020-08-03 05:51:42 +10:00
err = bt_conn_le_phy_update(default_conn, BT_CONN_LE_PHY_PARAM_2M);
if (err) {
LOG_ERR("Update phy conn failed (err %d)", err);
start_scan();
}
2020-07-02 12:27:01 +10:00
}
return false;
}
}
return true;
}
static void split_central_device_found(const bt_addr_le_t *addr, s8_t rssi, u8_t type,
2020-07-02 12:27:01 +10:00
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",
log_strdup(dev), type, ad->len, rssi);
2020-07-02 12:27:01 +10:00
/* 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, split_central_eir_found, (void *)addr);
2020-07-02 12:27:01 +10:00
}
}
static int start_scan(void)
{
int err;
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, split_central_device_found);
2020-07-02 12:27:01 +10:00
if (err) {
LOG_ERR("Scanning failed to start (err %d)", err);
return err;
}
LOG_DBG("Scanning successfully started");
return 0;
}
static void split_central_connected(struct bt_conn *conn, u8_t conn_err)
2020-07-02 12:27:01 +10:00
{
char addr[BT_ADDR_LE_STR_LEN];
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", log_strdup(addr));
2020-07-02 12:27:01 +10:00
split_central_process_connection(conn);
2020-07-02 12:27:01 +10:00
}
static void split_central_disconnected(struct bt_conn *conn, u8_t reason)
2020-07-02 12:27:01 +10:00
{
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 %d)", log_strdup(addr), reason);
2020-07-02 12:27:01 +10:00
if (default_conn != conn) {
return;
}
bt_conn_unref(default_conn);
default_conn = NULL;
start_scan();
}
static struct bt_conn_cb conn_callbacks = {
.connected = split_central_connected,
.disconnected = split_central_disconnected,
2020-07-02 12:27:01 +10:00
};
2020-07-14 13:37:25 +10:00
int zmk_split_bt_central_init(struct device *_arg)
2020-07-02 12:27:01 +10:00
{
bt_conn_cb_register(&conn_callbacks);
return start_scan();
}
2020-07-14 13:37:25 +10:00
SYS_INIT(zmk_split_bt_central_init,
APPLICATION,
CONFIG_ZMK_BLE_INIT_PRIORITY);