2020-09-10 13:57:44 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 The ZMK Contributors
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*/
|
|
|
|
|
2020-05-15 23:41:06 +10:00
|
|
|
#include <settings/settings.h>
|
2020-12-04 08:04:48 +11:00
|
|
|
#include <init.h>
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-09-09 13:26:00 +10:00
|
|
|
#include <logging/log.h>
|
|
|
|
|
|
|
|
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
|
|
|
|
2020-05-15 23:41:06 +10:00
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/gatt.h>
|
|
|
|
|
2020-05-18 23:11:46 +10:00
|
|
|
#include <zmk/ble.h>
|
|
|
|
#include <zmk/hog.h>
|
|
|
|
#include <zmk/hid.h>
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
enum {
|
2020-05-15 23:41:06 +10:00
|
|
|
HIDS_REMOTE_WAKE = BIT(0),
|
|
|
|
HIDS_NORMALLY_CONNECTABLE = BIT(1),
|
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct hids_info {
|
2020-12-03 03:41:57 +11:00
|
|
|
uint16_t version; /* version number of base USB HID Specification */
|
|
|
|
uint8_t code; /* country HID Device hardware is localized for. */
|
|
|
|
uint8_t flags;
|
2020-05-15 23:41:06 +10:00
|
|
|
} __packed;
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
struct hids_report {
|
2020-12-03 03:41:57 +11:00
|
|
|
uint8_t id; /* report id */
|
|
|
|
uint8_t type; /* report type */
|
2020-05-15 23:41:06 +10:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static struct hids_info info = {
|
|
|
|
.version = 0x0000,
|
|
|
|
.code = 0x00,
|
2021-09-24 10:05:50 +10:00
|
|
|
.flags = HIDS_NORMALLY_CONNECTABLE | HIDS_REMOTE_WAKE,
|
2020-05-15 23:41:06 +10:00
|
|
|
};
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
enum {
|
2020-05-15 23:41:06 +10:00
|
|
|
HIDS_INPUT = 0x01,
|
|
|
|
HIDS_OUTPUT = 0x02,
|
|
|
|
HIDS_FEATURE = 0x03,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hids_report input = {
|
|
|
|
.id = 0x01,
|
|
|
|
.type = HIDS_INPUT,
|
|
|
|
};
|
|
|
|
|
2020-06-03 01:23:22 +10:00
|
|
|
static struct hids_report consumer_input = {
|
|
|
|
.id = 0x02,
|
|
|
|
.type = HIDS_INPUT,
|
|
|
|
};
|
|
|
|
|
2020-05-15 23:41:06 +10:00
|
|
|
static bool host_requests_notification = false;
|
2020-12-03 03:41:57 +11:00
|
|
|
static uint8_t ctrl_point;
|
|
|
|
// static uint8_t proto_mode;
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t read_hids_info(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
|
2020-12-03 03:41:57 +11:00
|
|
|
uint16_t len, uint16_t offset) {
|
2020-09-14 12:53:24 +10:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
|
|
|
|
sizeof(struct hids_info));
|
2020-05-15 23:41:06 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t read_hids_report_ref(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2020-12-03 03:41:57 +11:00
|
|
|
void *buf, uint16_t len, uint16_t offset) {
|
2020-09-14 12:53:24 +10:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, attr->user_data,
|
|
|
|
sizeof(struct hids_report));
|
2020-05-15 23:41:06 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t read_hids_report_map(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2020-12-03 03:41:57 +11:00
|
|
|
void *buf, uint16_t len, uint16_t offset) {
|
2020-09-14 12:53:24 +10:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, zmk_hid_report_desc,
|
|
|
|
sizeof(zmk_hid_report_desc));
|
2020-05-15 23:41:06 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2020-12-03 03:41:57 +11:00
|
|
|
void *buf, uint16_t len, uint16_t offset) {
|
2020-11-22 07:09:21 +11:00
|
|
|
struct zmk_hid_keyboard_report_body *report_body = &zmk_hid_get_keyboard_report()->body;
|
2020-09-14 12:53:24 +10:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body,
|
2020-11-22 07:09:21 +11:00
|
|
|
sizeof(struct zmk_hid_keyboard_report_body));
|
2020-06-03 01:23:22 +10:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t read_hids_consumer_input_report(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, void *buf,
|
2020-12-03 03:41:57 +11:00
|
|
|
uint16_t len, uint16_t offset) {
|
2020-06-03 01:23:22 +10:00
|
|
|
struct zmk_hid_consumer_report_body *report_body = &zmk_hid_get_consumer_report()->body;
|
2020-09-14 12:53:24 +10:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, report_body,
|
|
|
|
sizeof(struct zmk_hid_consumer_report_body));
|
2020-05-15 23:41:06 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// static ssize_t write_proto_mode(struct bt_conn *conn,
|
|
|
|
// const struct bt_gatt_attr *attr,
|
2020-12-03 03:41:57 +11:00
|
|
|
// const void *buf, uint16_t len, uint16_t offset,
|
|
|
|
// uint8_t flags)
|
2020-05-15 23:41:06 +10:00
|
|
|
// {
|
|
|
|
// printk("PROTO CHANGED\n");
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
|
2020-12-03 03:41:57 +11:00
|
|
|
static void input_ccc_changed(const struct bt_gatt_attr *attr, uint16_t value) {
|
2020-05-15 23:41:06 +10:00
|
|
|
host_requests_notification = (value == BT_GATT_CCC_NOTIFY) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
static ssize_t write_ctrl_point(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
2020-12-03 03:41:57 +11:00
|
|
|
const void *buf, uint16_t len, uint16_t offset, uint8_t flags) {
|
|
|
|
uint8_t *value = attr->user_data;
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
if (offset + len > sizeof(ctrl_point)) {
|
2020-05-15 23:41:06 +10:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(value + offset, buf, len);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HID Service Declaration */
|
2020-09-14 12:53:24 +10:00
|
|
|
BT_GATT_SERVICE_DEFINE(
|
|
|
|
hog_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_HIDS),
|
|
|
|
// BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_PROTOCOL_MODE, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
|
|
|
// BT_GATT_PERM_WRITE, NULL, write_proto_mode, &proto_mode),
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_INFO, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, read_hids_info,
|
|
|
|
NULL, &info),
|
2021-09-15 23:26:48 +10:00
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT_MAP, BT_GATT_CHRC_READ, BT_GATT_PERM_READ_ENCRYPT,
|
2020-09-14 12:53:24 +10:00
|
|
|
read_hids_report_map, NULL, NULL),
|
|
|
|
|
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT, read_hids_input_report, NULL, NULL),
|
|
|
|
BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
|
2021-09-15 23:26:48 +10:00
|
|
|
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
|
|
|
|
NULL, &input),
|
2020-09-14 12:53:24 +10:00
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT, read_hids_consumer_input_report, NULL, NULL),
|
|
|
|
BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
|
2021-09-15 23:26:48 +10:00
|
|
|
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
|
|
|
|
NULL, &consumer_input),
|
2020-09-14 12:53:24 +10:00
|
|
|
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
|
|
|
BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point));
|
2020-05-15 23:41:06 +10:00
|
|
|
|
2020-09-09 13:26:00 +10:00
|
|
|
struct bt_conn *destination_connection() {
|
|
|
|
struct bt_conn *conn;
|
|
|
|
bt_addr_le_t *addr = zmk_ble_active_profile_addr();
|
|
|
|
LOG_DBG("Address pointer %p", addr);
|
|
|
|
if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
|
|
|
|
LOG_WRN("Not sending, no active address for current profile");
|
|
|
|
return NULL;
|
|
|
|
} else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) {
|
|
|
|
LOG_WRN("Not sending, not connected to active profile");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2020-12-04 08:04:48 +11:00
|
|
|
K_THREAD_STACK_DEFINE(hog_q_stack, CONFIG_ZMK_BLE_THREAD_STACK_SIZE);
|
|
|
|
|
|
|
|
struct k_work_q hog_work_q;
|
|
|
|
|
|
|
|
K_MSGQ_DEFINE(zmk_hog_keyboard_msgq, sizeof(struct zmk_hid_keyboard_report_body),
|
|
|
|
CONFIG_ZMK_BLE_KEYBOARD_REPORT_QUEUE_SIZE, 4);
|
|
|
|
|
|
|
|
void send_keyboard_report_callback(struct k_work *work) {
|
|
|
|
struct zmk_hid_keyboard_report_body report;
|
|
|
|
|
|
|
|
while (k_msgq_get(&zmk_hog_keyboard_msgq, &report, K_NO_WAIT) == 0) {
|
|
|
|
struct bt_conn *conn = destination_connection();
|
|
|
|
if (conn == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bt_gatt_notify_params notify_params = {
|
|
|
|
.attr = &hog_svc.attrs[5],
|
|
|
|
.data = &report,
|
|
|
|
.len = sizeof(report),
|
|
|
|
};
|
|
|
|
|
|
|
|
int err = bt_gatt_notify_cb(conn, ¬ify_params);
|
|
|
|
if (err) {
|
|
|
|
LOG_ERR("Error notifying %d", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_unref(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
K_WORK_DEFINE(hog_keyboard_work, send_keyboard_report_callback);
|
|
|
|
|
2020-11-22 07:09:21 +11:00
|
|
|
int zmk_hog_send_keyboard_report(struct zmk_hid_keyboard_report_body *report) {
|
2020-12-04 08:04:48 +11:00
|
|
|
int err = k_msgq_put(&zmk_hog_keyboard_msgq, report, K_MSEC(100));
|
|
|
|
if (err) {
|
|
|
|
switch (err) {
|
|
|
|
case -EAGAIN: {
|
|
|
|
LOG_WRN("Keyboard message queue full, popping first message and queueing again");
|
|
|
|
struct zmk_hid_keyboard_report_body discarded_report;
|
|
|
|
k_msgq_get(&zmk_hog_keyboard_msgq, &discarded_report, K_NO_WAIT);
|
|
|
|
return zmk_hog_send_keyboard_report(report);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG_WRN("Failed to queue keyboard report to send (%d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
2020-09-09 13:26:00 +10:00
|
|
|
}
|
|
|
|
|
2020-12-04 08:04:48 +11:00
|
|
|
k_work_submit_to_queue(&hog_work_q, &hog_keyboard_work);
|
2020-09-09 13:26:00 +10:00
|
|
|
|
2020-12-04 08:04:48 +11:00
|
|
|
return 0;
|
2020-06-03 01:23:22 +10:00
|
|
|
};
|
|
|
|
|
2020-12-04 08:04:48 +11:00
|
|
|
K_MSGQ_DEFINE(zmk_hog_consumer_msgq, sizeof(struct zmk_hid_consumer_report_body),
|
|
|
|
CONFIG_ZMK_BLE_CONSUMER_REPORT_QUEUE_SIZE, 4);
|
|
|
|
|
|
|
|
void send_consumer_report_callback(struct k_work *work) {
|
|
|
|
struct zmk_hid_consumer_report_body report;
|
|
|
|
|
|
|
|
while (k_msgq_get(&zmk_hog_consumer_msgq, &report, K_NO_WAIT) == 0) {
|
|
|
|
struct bt_conn *conn = destination_connection();
|
|
|
|
if (conn == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bt_gatt_notify_params notify_params = {
|
|
|
|
.attr = &hog_svc.attrs[10],
|
|
|
|
.data = &report,
|
|
|
|
.len = sizeof(report),
|
|
|
|
};
|
|
|
|
|
|
|
|
int err = bt_gatt_notify_cb(conn, ¬ify_params);
|
|
|
|
if (err) {
|
|
|
|
LOG_DBG("Error notifying %d", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
bt_conn_unref(conn);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
K_WORK_DEFINE(hog_consumer_work, send_consumer_report_callback);
|
|
|
|
|
2020-09-14 12:53:24 +10:00
|
|
|
int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *report) {
|
2020-12-04 08:04:48 +11:00
|
|
|
int err = k_msgq_put(&zmk_hog_consumer_msgq, report, K_MSEC(100));
|
|
|
|
if (err) {
|
|
|
|
switch (err) {
|
|
|
|
case -EAGAIN: {
|
|
|
|
LOG_WRN("Consumer message queue full, popping first message and queueing again");
|
|
|
|
struct zmk_hid_consumer_report_body discarded_report;
|
|
|
|
k_msgq_get(&zmk_hog_consumer_msgq, &discarded_report, K_NO_WAIT);
|
|
|
|
return zmk_hog_send_consumer_report(report);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
LOG_WRN("Failed to queue consumer report to send (%d)", err);
|
|
|
|
return err;
|
|
|
|
}
|
2020-09-09 13:26:00 +10:00
|
|
|
}
|
2020-09-14 12:53:24 +10:00
|
|
|
|
2020-12-04 08:04:48 +11:00
|
|
|
k_work_submit_to_queue(&hog_work_q, &hog_consumer_work);
|
|
|
|
|
|
|
|
return 0;
|
2020-05-15 23:41:06 +10:00
|
|
|
};
|
2020-12-04 08:04:48 +11:00
|
|
|
|
|
|
|
int zmk_hog_init(const struct device *_arg) {
|
|
|
|
k_work_q_start(&hog_work_q, hog_q_stack, K_THREAD_STACK_SIZEOF(hog_q_stack),
|
|
|
|
CONFIG_ZMK_BLE_THREAD_PRIORITY);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(zmk_hog_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY);
|