summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/CMakeLists.txt1
-rw-r--r--app/include/zmk/split/bluetooth/service.h11
-rw-r--r--app/include/zmk/split/bluetooth/uuid.h14
-rw-r--r--app/src/ble.c2
-rw-r--r--app/src/split/bluetooth/central.c241
-rw-r--r--app/src/split/bluetooth/service.c1
6 files changed, 258 insertions, 12 deletions
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index 3f2065f..81c8ac4 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 694afc1..76af898 100644
--- a/app/include/zmk/split/bluetooth/service.h
+++ b/app/include/zmk/split/bluetooth/service.h
@@ -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); \ 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 0000000..4fa7f40
--- /dev/null
+++ b/app/include/zmk/split/bluetooth/uuid.h
@@ -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)
diff --git a/app/src/ble.c b/app/src/ble.c
index 94a23da..8b43cdd 100644
--- a/app/src/ble.c
+++ b/app/src/ble.c
@@ -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};
diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c
new file mode 100644
index 0000000..1adc5c8
--- /dev/null
+++ b/app/src/split/bluetooth/central.c
@@ -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();
+}
diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c
index a669723..81fbfb5 100644
--- a/app/src/split/bluetooth/service.c
+++ b/app/src/split/bluetooth/service.c
@@ -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;