feat: handle LED indicators report

This commit is contained in:
Alessandro Bortolin
2022-09-06 12:29:07 +00:00
committed by Pete Johanson
parent 6276e973d5
commit 4e55c5f6e9
13 changed files with 269 additions and 15 deletions

View File

@@ -15,8 +15,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zephyr/bluetooth/gatt.h>
#include <zmk/ble.h>
#include <zmk/endpoints_types.h>
#include <zmk/hog.h>
#include <zmk/hid.h>
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/hid_indicators.h>
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
enum {
HIDS_REMOTE_WAKE = BIT(0),
@@ -51,6 +55,15 @@ static struct hids_report input = {
.type = HIDS_INPUT,
};
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
static struct hids_report led_indicators = {
.id = ZMK_HID_REPORT_ID_LEDS,
.type = HIDS_OUTPUT,
};
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
static struct hids_report consumer_input = {
.id = ZMK_HID_REPORT_ID_CONSUMER,
.type = HIDS_INPUT,
@@ -94,6 +107,34 @@ static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt
sizeof(struct zmk_hid_keyboard_report_body));
}
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
static ssize_t write_hids_leds_report(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags) {
if (offset != 0) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if (len != sizeof(struct zmk_hid_led_report_body)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
struct zmk_hid_led_report_body *report = (struct zmk_hid_led_report_body *)buf;
int profile = zmk_ble_profile_index(bt_conn_get_dst(conn));
if (profile < 0) {
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
struct zmk_endpoint_instance endpoint = {.transport = ZMK_TRANSPORT_BLE,
.ble = {
.profile_index = profile,
}};
zmk_hid_indicators_process_report(report, endpoint);
return len;
}
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
static ssize_t read_hids_consumer_input_report(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset) {
@@ -152,6 +193,7 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
NULL, &input),
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),
@@ -166,6 +208,15 @@ BT_GATT_SERVICE_DEFINE(
NULL, &mouse_input),
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE)
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT, NULL,
write_hids_leds_report, NULL),
BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
NULL, &led_indicators),
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point));
@@ -251,7 +302,7 @@ void send_consumer_report_callback(struct k_work *work) {
}
struct bt_gatt_notify_params notify_params = {
.attr = &hog_svc.attrs[10],
.attr = &hog_svc.attrs[9],
.data = &report,
.len = sizeof(report),
};