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

@ -68,6 +68,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE src/events/layer_state_changed.c) target_sources(app PRIVATE src/events/layer_state_changed.c)
target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c)
target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_HID_INDICATORS app PRIVATE src/hid_indicators.c)
if (CONFIG_ZMK_BLE) if (CONFIG_ZMK_BLE)
target_sources(app PRIVATE src/events/ble_active_profile_changed.c) target_sources(app PRIVATE src/events/ble_active_profile_changed.c)
@ -83,6 +84,8 @@ target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_bac
target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c) target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c)
target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c) target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c)
target_sources_ifdef(CONFIG_ZMK_HID_INDICATORS app PRIVATE src/events/hid_indicators_changed.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT app PRIVATE src/events/split_peripheral_status_changed.c) target_sources_ifdef(CONFIG_ZMK_SPLIT app PRIVATE src/events/split_peripheral_status_changed.c)
add_subdirectory(src/split) add_subdirectory(src/split)

View file

@ -87,6 +87,12 @@ config ZMK_HID_CONSUMER_REPORT_USAGES_BASIC
endchoice endchoice
config ZMK_HID_INDICATORS
bool "HID Indicators"
help
Enable HID indicators, used for detecting state of Caps/Scroll/Num Lock,
Kata, and Compose.
menu "Output Types" menu "Output Types"
config ZMK_USB config ZMK_USB

View file

@ -27,6 +27,7 @@ int zmk_ble_prof_select(uint8_t index);
int zmk_ble_prof_disconnect(uint8_t index); int zmk_ble_prof_disconnect(uint8_t index);
int zmk_ble_active_profile_index(); int zmk_ble_active_profile_index();
int zmk_ble_profile_index(const bt_addr_le_t *addr);
bt_addr_le_t *zmk_ble_active_profile_addr(); bt_addr_le_t *zmk_ble_active_profile_addr();
bool zmk_ble_active_profile_is_open(); bool zmk_ble_active_profile_is_open();
bool zmk_ble_active_profile_is_connected(); bool zmk_ble_active_profile_is_connected();

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <zmk/hid_indicators_types.h>
#include <zmk/event_manager.h>
struct zmk_hid_indicators_changed {
zmk_hid_indicators indicators;
};
ZMK_EVENT_DECLARE(zmk_hid_indicators_changed);

View file

@ -50,6 +50,7 @@
#define ZMK_HID_MAIN_VAL_BUFFERED_BYTES (0x01 << 8) #define ZMK_HID_MAIN_VAL_BUFFERED_BYTES (0x01 << 8)
#define ZMK_HID_REPORT_ID_KEYBOARD 0x01 #define ZMK_HID_REPORT_ID_KEYBOARD 0x01
#define ZMK_HID_REPORT_ID_LEDS 0x01
#define ZMK_HID_REPORT_ID_CONSUMER 0x02 #define ZMK_HID_REPORT_ID_CONSUMER 0x02
#define ZMK_HID_REPORT_ID_MOUSE 0x03 #define ZMK_HID_REPORT_ID_MOUSE 0x03
@ -73,6 +74,22 @@ static const uint8_t zmk_hid_report_desc[] = {
HID_REPORT_COUNT(0x01), HID_REPORT_COUNT(0x01),
HID_INPUT(ZMK_HID_MAIN_VAL_CONST | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS), HID_INPUT(ZMK_HID_MAIN_VAL_CONST | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS),
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
HID_USAGE_PAGE(HID_USAGE_LED),
HID_USAGE_MIN8(HID_USAGE_LED_NUM_LOCK),
HID_USAGE_MAX8(HID_USAGE_LED_KANA),
HID_REPORT_SIZE(0x01),
HID_REPORT_COUNT(0x05),
HID_OUTPUT(ZMK_HID_MAIN_VAL_DATA | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS),
HID_USAGE_PAGE(HID_USAGE_LED),
HID_REPORT_SIZE(0x03),
HID_REPORT_COUNT(0x01),
HID_OUTPUT(ZMK_HID_MAIN_VAL_CONST | ZMK_HID_MAIN_VAL_VAR | ZMK_HID_MAIN_VAL_ABS),
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
HID_USAGE_PAGE(HID_USAGE_KEY), HID_USAGE_PAGE(HID_USAGE_KEY),
#if IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_NKRO) #if IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_NKRO)
@ -189,6 +206,19 @@ struct zmk_hid_keyboard_report {
struct zmk_hid_keyboard_report_body body; struct zmk_hid_keyboard_report_body body;
} __packed; } __packed;
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
struct zmk_hid_led_report_body {
uint8_t leds;
} __packed;
struct zmk_hid_led_report {
uint8_t report_id;
struct zmk_hid_led_report_body body;
} __packed;
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
struct zmk_hid_consumer_report_body { struct zmk_hid_consumer_report_body {
#if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) #if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC)
uint8_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE]; uint8_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE];

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <zmk/endpoints_types.h>
#include <zmk/hid.h>
#include <zmk/hid_indicators_types.h>
zmk_hid_indicators zmk_hid_indicators_get_current_profile(void);
zmk_hid_indicators zmk_hid_indicators_get_profile(struct zmk_endpoint_instance endpoint);
void zmk_hid_indicators_set_profile(zmk_hid_indicators indicators,
struct zmk_endpoint_instance endpoint);
void zmk_hid_indicators_process_report(struct zmk_hid_led_report_body *report,
struct zmk_endpoint_instance endpoint);

View file

@ -0,0 +1,9 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
typedef uint8_t zmk_hid_indicators;

View file

@ -224,6 +224,15 @@ int zmk_ble_clear_bonds() {
int zmk_ble_active_profile_index() { return active_profile; } int zmk_ble_active_profile_index() { return active_profile; }
int zmk_ble_profile_index(const bt_addr_le_t *addr) {
for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) {
if (bt_addr_le_cmp(addr, &profiles[i].peer) == 0) {
return i;
}
}
return -ENODEV;
}
#if IS_ENABLED(CONFIG_SETTINGS) #if IS_ENABLED(CONFIG_SETTINGS)
static void ble_save_profile_work(struct k_work *work) { static void ble_save_profile_work(struct k_work *work) {
settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile));

View file

@ -0,0 +1,10 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/kernel.h>
#include <zmk/events/hid_indicators_changed.h>
ZMK_EVENT_IMPL(zmk_hid_indicators_changed);

63
app/src/hid_indicators.c Normal file
View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zmk/ble.h>
#include <zmk/endpoints.h>
#include <zmk/hid_indicators.h>
#include <zmk/events/hid_indicators_changed.h>
#include <zmk/events/endpoint_changed.h>
#include <zmk/split/bluetooth/central.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static zmk_hid_indicators hid_indicators[ZMK_ENDPOINT_COUNT];
zmk_hid_indicators zmk_hid_indicators_get_current_profile(void) {
return zmk_hid_indicators_get_profile(zmk_endpoints_selected());
}
zmk_hid_indicators zmk_hid_indicators_get_profile(struct zmk_endpoint_instance endpoint) {
int profile = zmk_endpoint_instance_to_index(endpoint);
return hid_indicators[profile];
}
static void raise_led_changed_event(struct k_work *_work) {
ZMK_EVENT_RAISE(new_zmk_hid_indicators_changed((struct zmk_hid_indicators_changed){
.indicators = zmk_hid_indicators_get_current_profile()}));
}
static K_WORK_DEFINE(led_changed_work, raise_led_changed_event);
void zmk_hid_indicators_set_profile(zmk_hid_indicators indicators,
struct zmk_endpoint_instance endpoint) {
int profile = zmk_endpoint_instance_to_index(endpoint);
// This write is not happening on the main thread. To prevent potential data races, every
// operation involving hid_indicators must be atomic. Currently, each function either reads
// or writes only one entry at a time, so it is safe to do these operations without a lock.
hid_indicators[profile] = indicators;
k_work_submit(&led_changed_work);
}
void zmk_hid_indicators_process_report(struct zmk_hid_led_report_body *report,
struct zmk_endpoint_instance endpoint) {
uint8_t indicators = report->leds;
zmk_hid_indicators_set_profile(indicators, endpoint);
LOG_DBG("Update HID indicators: endpoint=%d, indicators=%x", endpoint.transport, indicators);
}
static int profile_listener(const zmk_event_t *eh) {
raise_led_changed_event(NULL);
return 0;
}
static ZMK_LISTENER(profile_listener, profile_listener);
static ZMK_SUBSCRIPTION(profile_listener, zmk_endpoint_changed);

View file

@ -15,8 +15,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zephyr/bluetooth/gatt.h> #include <zephyr/bluetooth/gatt.h>
#include <zmk/ble.h> #include <zmk/ble.h>
#include <zmk/endpoints_types.h>
#include <zmk/hog.h> #include <zmk/hog.h>
#include <zmk/hid.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 { enum {
HIDS_REMOTE_WAKE = BIT(0), HIDS_REMOTE_WAKE = BIT(0),
@ -51,6 +55,15 @@ static struct hids_report input = {
.type = HIDS_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 = { static struct hids_report consumer_input = {
.id = ZMK_HID_REPORT_ID_CONSUMER, .id = ZMK_HID_REPORT_ID_CONSUMER,
.type = HIDS_INPUT, .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)); 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, static ssize_t read_hids_consumer_input_report(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf, const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset) { 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_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, BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref,
NULL, &input), NULL, &input),
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, 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_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), 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), NULL, &mouse_input),
#endif // IS_ENABLED(CONFIG_ZMK_MOUSE) #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_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point)); 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 = { struct bt_gatt_notify_params notify_params = {
.attr = &hog_svc.attrs[10], .attr = &hog_svc.attrs[9],
.data = &report, .data = &report,
.len = sizeof(report), .len = sizeof(report),
}; };

View file

@ -13,6 +13,9 @@
#include <zmk/usb.h> #include <zmk/usb.h>
#include <zmk/hid.h> #include <zmk/hid.h>
#include <zmk/keymap.h> #include <zmk/keymap.h>
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/hid_indicators.h>
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
#include <zmk/event_manager.h> #include <zmk/event_manager.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -83,12 +86,44 @@ static int get_report_cb(const struct device *dev, struct usb_setup_packet *setu
return 0; return 0;
} }
static int set_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len,
uint8_t **data) {
if ((setup->wValue & HID_GET_REPORT_TYPE_MASK) != HID_REPORT_TYPE_OUTPUT) {
LOG_ERR("Unsupported report type %d requested",
(setup->wValue & HID_GET_REPORT_TYPE_MASK) >> 8);
return -ENOTSUP;
}
switch (setup->wValue & HID_GET_REPORT_ID_MASK) {
#if IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
case ZMK_HID_REPORT_ID_LEDS:
if (*len != sizeof(struct zmk_hid_led_report)) {
LOG_ERR("LED set report is malformed: length=%d", *len);
return -EINVAL;
} else {
struct zmk_hid_led_report *report = (struct zmk_hid_led_report *)*data;
struct zmk_endpoint_instance endpoint = {
.transport = ZMK_TRANSPORT_USB,
};
zmk_hid_indicators_process_report(&report->body, endpoint);
}
break;
#endif // IS_ENABLED(CONFIG_ZMK_HID_INDICATORS)
default:
LOG_ERR("Invalid report ID %d requested", setup->wValue & HID_GET_REPORT_ID_MASK);
return -EINVAL;
}
return 0;
}
static const struct hid_ops ops = { static const struct hid_ops ops = {
#if IS_ENABLED(CONFIG_ZMK_USB_BOOT) #if IS_ENABLED(CONFIG_ZMK_USB_BOOT)
.protocol_change = set_proto_cb, .protocol_change = set_proto_cb,
#endif #endif
.int_in_ready = in_ready_cb, .int_in_ready = in_ready_cb,
.get_report = get_report_cb, .get_report = get_report_cb,
.set_report = set_report_cb,
}; };
static int zmk_usb_hid_send_report(const uint8_t *report, size_t len) { static int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {

View file

@ -23,7 +23,8 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/
### HID ### HID
| Config | Type | Description | Default | | Config | Type | Description | Default |
| ------------------------------------- | ---- | ------------------------------------------------- | ------- | | ------------------------------------- | ---- | -------------------------------------------------------------- | ------- |
| `CONFIG_ZMK_HID_INDICATORS` | bool | Enable reciept of HID/LED indicator state from connected hosts | n |
| `CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE` | int | Number of consumer keys simultaneously reportable | 6 | | `CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE` | int | Number of consumer keys simultaneously reportable | 6 |
Exactly zero or one of the following options may be set to `y`. The first is used if none are set. Exactly zero or one of the following options may be set to `y`. The first is used if none are set.
@ -92,8 +93,9 @@ Note that `CONFIG_BT_MAX_CONN` and `CONFIG_BT_MAX_PAIRED` should be set to the s
Following split keyboard settings are defined in [zmk/app/src/split/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/split/Kconfig) (generic) and [zmk/app/src/split/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/split/bluetooth/Kconfig) (bluetooth). Following split keyboard settings are defined in [zmk/app/src/split/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/split/Kconfig) (generic) and [zmk/app/src/split/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/src/split/bluetooth/Kconfig) (bluetooth).
| Config | Type | Description | Default | | Config | Type | Description | Default |
| ----------------------------------------------------- | ---- | ----------------------------------------------------------------------- | ------- | | ----------------------------------------------------- | ---- | ------------------------------------------------------------------------ | ------- |
| `CONFIG_ZMK_SPLIT` | bool | Enable split keyboard support | n | | `CONFIG_ZMK_SPLIT` | bool | Enable split keyboard support | n |
| `CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS` | bool | Enable split keyboard support for passing indicator state to peripherals | n |
| `CONFIG_ZMK_SPLIT_BLE` | bool | Use BLE to communicate between split keyboard halves | y | | `CONFIG_ZMK_SPLIT_BLE` | bool | Use BLE to communicate between split keyboard halves | y |
| `CONFIG_ZMK_SPLIT_ROLE_CENTRAL` | bool | `y` for central device, `n` for peripheral | | | `CONFIG_ZMK_SPLIT_ROLE_CENTRAL` | bool | `y` for central device, `n` for peripheral | |
| `CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE` | int | Max number of key state events to queue when received from peripherals | 5 | | `CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE` | int | Max number of key state events to queue when received from peripherals | 5 |