Browse Source

More split implementation.

* Propogate key position state changes on central.
* Various BLE tweaks.
xmkb
Pete Johanson 4 years ago
parent
commit
d4afd989f3
  1. 2
      app/Kconfig
  2. 8
      app/src/main.c
  3. 113
      app/src/split/bluetooth/central.c

2
app/Kconfig

@ -126,7 +126,7 @@ config ZMK_ACTION_MOD_TAP
endmenu endmenu
config HEAP_MEM_POOL_SIZE config HEAP_MEM_POOL_SIZE
default 200 default 1024
module = ZMK module = ZMK
module-str = zmk module-str = zmk

8
app/src/main.c

@ -31,14 +31,6 @@ void main(void)
return; 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 #ifdef CONFIG_SETTINGS
settings_load(); settings_load();
#endif #endif

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

@ -17,13 +17,15 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/split/bluetooth/uuid.h> #include <zmk/split/bluetooth/uuid.h>
#include <zmk/event-manager.h>
#include <zmk/events/position-state-changed.h>
#include <init.h> #include <init.h>
static int start_scan(void); static int start_scan(void);
static struct bt_conn *default_conn; 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_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_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, struct bt_gatt_subscribe_params *params,
const void *data, u16_t length) const void *data, u16_t length)
{ {
static u8_t position_state[16];
u8_t changed_positions[16];
if (!data) { if (!data) {
LOG_DBG("[UNSUBSCRIBED]"); LOG_DBG("[UNSUBSCRIBED]");
params->value_handle = 0U; 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); 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; return BT_GATT_ITER_CONTINUE;
} }
@ -96,6 +123,32 @@ static u8_t discover_func(struct bt_conn *conn,
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 = 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) static bool eir_found(struct bt_data *data, void *user_data)
{ {
bt_addr_le_t *addr = 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) { for (i = 0; i < data->data_len; i += 16) {
struct bt_le_conn_param *param; struct bt_le_conn_param *param;
struct bt_uuid uuid; struct bt_uuid uuid;
char uuid_str[BT_UUID_STR_LEN];
int err; int err;
if (!bt_uuid_create(&uuid, &data->data[i], 16)) { 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; 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)); 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; continue;
} }
LOG_DBG("Found the split service");
err = bt_le_scan_stop(); err = bt_le_scan_stop();
if (err) { if (err) {
LOG_ERR("Stop LE scan failed (err %d)", err); LOG_ERR("Stop LE scan failed (err %d)", err);
continue; continue;
} }
param = BT_LE_CONN_PARAM_DEFAULT; default_conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr);
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, if (default_conn) {
param, &default_conn); LOG_DBG("Found existing connection");
if (err) { split_central_process_connection(default_conn);
LOG_ERR("Create conn failed (err %d)", err); } else {
start_scan(); 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 false;
@ -179,7 +243,7 @@ static int start_scan(void)
return 0; 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]; char addr[BT_ADDR_LE_STR_LEN];
int err; int err;
@ -196,31 +260,20 @@ static void connected(struct bt_conn *conn, u8_t conn_err)
return; 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); split_central_process_connection(conn);
if (err) {
LOG_ERR("Discover failed(err %d)", err);
return;
}
}
} }
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]; char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); 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) { if (default_conn != conn) {
return; return;
@ -233,8 +286,8 @@ static void disconnected(struct bt_conn *conn, u8_t reason)
} }
static struct bt_conn_cb conn_callbacks = { static struct bt_conn_cb conn_callbacks = {
.connected = connected, .connected = split_central_connected,
.disconnected = disconnected, .disconnected = split_central_disconnected,
}; };
int zmk_split_bt_central_init(struct device *_arg) 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, SYS_INIT(zmk_split_bt_central_init,
APPLICATION, APPLICATION,
CONFIG_APPLICATION_INIT_PRIORITY); CONFIG_ZMK_BLE_INIT_PRIORITY);
Loading…
Cancel
Save