Skip to content

Commit

Permalink
feat(core): Optionally disable endpoint fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Oct 19, 2024
1 parent 022ede9 commit 5205e9b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ config USB_HID_POLL_INTERVAL_MS
#ZMK_USB
endif

config ZMK_ENDPOINT_DISABLE_FALLBACK
bool "Disable automatic endpoint fallback when preferred endpoint is unavailable"

menuconfig ZMK_BLE
bool "BLE (HID over GATT)"
select BT
Expand Down
9 changes: 7 additions & 2 deletions app/include/zmk/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Note that this value may change between firmware versions, so it should not
* be used in any persistent storage.
*/
#define ZMK_ENDPOINT_COUNT (ZMK_ENDPOINT_USB_COUNT + ZMK_ENDPOINT_BLE_COUNT)
#define ZMK_ENDPOINT_COUNT (ZMK_ENDPOINT_USB_COUNT + ZMK_ENDPOINT_BLE_COUNT + 1)

bool zmk_endpoint_instance_eq(struct zmk_endpoint_instance a, struct zmk_endpoint_instance b);

Expand Down Expand Up @@ -64,10 +64,15 @@ int zmk_endpoints_select_transport(enum zmk_transport transport);
int zmk_endpoints_toggle_transport(void);

/**
* Gets the currently-selected endpoint.
* Gets the currently in use endpoint.
*/
struct zmk_endpoint_instance zmk_endpoints_selected(void);

/**
* Gets the preferred endpoint.
*/
struct zmk_endpoint_instance zmk_endpoints_preferred(void);

int zmk_endpoints_send_report(uint16_t usage_page);

#if IS_ENABLED(CONFIG_ZMK_MOUSE)
Expand Down
1 change: 1 addition & 0 deletions app/include/zmk/endpoints_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* The method by which data is sent.
*/
enum zmk_transport {
ZMK_TRANSPORT_NONE,
ZMK_TRANSPORT_USB,
ZMK_TRANSPORT_BLE,
};
Expand Down
50 changes: 50 additions & 0 deletions app/src/endpoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

#if IS_ENABLED(CONFIG_ZMK_ENDPOINT_DISABLE_FALLBACK)
#define DEFAULT_TRANSPORT ZMK_TRANSPORT_NONE
#else
#define DEFAULT_TRANSPORT \
COND_CODE_1(IS_ENABLED(CONFIG_ZMK_BLE), (ZMK_TRANSPORT_BLE), (ZMK_TRANSPORT_USB))
#endif

static struct zmk_endpoint_instance current_instance = {};
static enum zmk_transport preferred_transport =
Expand Down Expand Up @@ -59,6 +63,9 @@ bool zmk_endpoint_instance_eq(struct zmk_endpoint_instance a, struct zmk_endpoin

case ZMK_TRANSPORT_BLE:
return a.ble.profile_index == b.ble.profile_index;

case ZMK_TRANSPORT_NONE:
return true;
}

LOG_ERR("Invalid transport %d", a.transport);
Expand All @@ -73,13 +80,17 @@ int zmk_endpoint_instance_to_str(struct zmk_endpoint_instance endpoint, char *st
case ZMK_TRANSPORT_BLE:
return snprintf(str, len, "BLE:%d", endpoint.ble.profile_index);

case ZMK_TRANSPORT_NONE:
return snprintf(str, len, "None");

default:
return snprintf(str, len, "Invalid");
}
}

#define INSTANCE_INDEX_OFFSET_USB 0
#define INSTANCE_INDEX_OFFSET_BLE ZMK_ENDPOINT_USB_COUNT
#define INSTANCE_INDEX_OFFSET_NONE ZMK_ENDPOINT_USB_COUNT + ZMK_ENDPOINT_BLE_COUNT

int zmk_endpoint_instance_to_index(struct zmk_endpoint_instance endpoint) {
switch (endpoint.transport) {
Expand All @@ -88,6 +99,9 @@ int zmk_endpoint_instance_to_index(struct zmk_endpoint_instance endpoint) {

case ZMK_TRANSPORT_BLE:
return INSTANCE_INDEX_OFFSET_BLE + endpoint.ble.profile_index;

case ZMK_TRANSPORT_NONE:
return INSTANCE_INDEX_OFFSET_NONE;
}

LOG_ERR("Invalid transport %d", endpoint.transport);
Expand Down Expand Up @@ -118,6 +132,24 @@ int zmk_endpoints_toggle_transport(void) {

struct zmk_endpoint_instance zmk_endpoints_selected(void) { return current_instance; }

struct zmk_endpoint_instance zmk_endpoints_preferred(void) {
struct zmk_endpoint_instance instance = {.transport = preferred_transport};

switch (instance.transport) {
#if IS_ENABLED(CONFIG_ZMK_BLE)
case ZMK_TRANSPORT_BLE:
instance.ble.profile_index = zmk_ble_active_profile_index();
break;
#endif // IS_ENABLED(CONFIG_ZMK_BLE)

default:
// No extra data for this transport.
break;
}

return instance;
}

static int send_keyboard_report(void) {
switch (current_instance.transport) {
case ZMK_TRANSPORT_USB: {
Expand Down Expand Up @@ -146,6 +178,8 @@ static int send_keyboard_report(void) {
return -ENOTSUP;
#endif /* IS_ENABLED(CONFIG_ZMK_BLE) */
}
case ZMK_TRANSPORT_NONE:
return 0;
}

LOG_ERR("Unhandled endpoint transport %d", current_instance.transport);
Expand Down Expand Up @@ -180,6 +214,8 @@ static int send_consumer_report(void) {
return -ENOTSUP;
#endif /* IS_ENABLED(CONFIG_ZMK_BLE) */
}
case ZMK_TRANSPORT_NONE:
return 0;
}

LOG_ERR("Unhandled endpoint transport %d", current_instance.transport);
Expand Down Expand Up @@ -230,6 +266,8 @@ int zmk_endpoints_send_mouse_report() {
return -ENOTSUP;
#endif /* IS_ENABLED(CONFIG_ZMK_BLE) */
}
case ZMK_TRANSPORT_NONE:
return 0;
}

LOG_ERR("Unhandled endpoint transport %d", current_instance.transport);
Expand Down Expand Up @@ -282,6 +320,17 @@ static bool is_ble_ready(void) {
}

static enum zmk_transport get_selected_transport(void) {
#if IS_ENABLED(CONFIG_ZMK_ENDPOINT_DISABLE_FALLBACK)
switch (preferred_transport) {
case ZMK_TRANSPORT_BLE:
return is_ble_ready() ? ZMK_TRANSPORT_BLE : ZMK_TRANSPORT_NONE;
case ZMK_TRANSPORT_USB:
return is_usb_ready() ? ZMK_TRANSPORT_USB : ZMK_TRANSPORT_NONE;

default:
break;
}
#else
if (is_ble_ready()) {
if (is_usb_ready()) {
LOG_DBG("Both endpoint transports are ready. Using %d", preferred_transport);
Expand All @@ -296,6 +345,7 @@ static enum zmk_transport get_selected_transport(void) {
LOG_DBG("Only USB is ready.");
return ZMK_TRANSPORT_USB;
}
#endif

LOG_DBG("No endpoint transports are ready.");
return DEFAULT_TRANSPORT;
Expand Down

0 comments on commit 5205e9b

Please sign in to comment.