feat(ble): Add event for inactive BLE profile changes

This commit is contained in:
snoyer 2025-08-12 09:53:44 +01:00
parent 118359c83e
commit 6eab60f293
5 changed files with 66 additions and 0 deletions

View File

@ -82,6 +82,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
if (CONFIG_ZMK_BLE)
target_sources(app PRIVATE src/events/ble_active_profile_changed.c)
target_sources(app PRIVATE src/events/ble_inactive_profile_changed.c)
target_sources(app PRIVATE src/behaviors/behavior_bt.c)
target_sources(app PRIVATE src/ble.c)
target_sources(app PRIVATE src/hog.c)

View File

@ -17,6 +17,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/event_manager.h>
#include <zmk/events/battery_state_changed.h>
#include <zmk/events/ble_active_profile_changed.h>
#include <zmk/events/ble_inactive_profile_changed.h>
#include <zmk/events/endpoint_changed.h>
#include <zmk/events/wpm_state_changed.h>
#include <zmk/events/layer_state_changed.h>
@ -282,6 +283,7 @@ ZMK_SUBSCRIPTION(widget_output_status, zmk_usb_conn_state_changed);
#endif
#if defined(CONFIG_ZMK_BLE)
ZMK_SUBSCRIPTION(widget_output_status, zmk_ble_active_profile_changed);
ZMK_SUBSCRIPTION(widget_output_status, zmk_ble_inactive_profile_changed);
#endif
static void set_layer_status(struct zmk_widget_status *widget, struct layer_status_state state) {

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2025 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <zephyr/kernel.h>
#include <zmk/event_manager.h>
#include <zephyr/device.h>
#include <zmk/ble/profile.h>
struct zmk_ble_inactive_profile_changed {
uint8_t index;
struct zmk_ble_profile *profile;
};
ZMK_EVENT_DECLARE(zmk_ble_inactive_profile_changed);

View File

@ -36,6 +36,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/split/bluetooth/uuid.h>
#include <zmk/event_manager.h>
#include <zmk/events/ble_active_profile_changed.h>
#include <zmk/events/ble_inactive_profile_changed.h>
#if IS_ENABLED(CONFIG_ZMK_BLE_PASSKEY_ENTRY)
#include <zmk/events/keycode_state_changed.h>
@ -96,6 +97,22 @@ static void raise_profile_changed_event_callback(struct k_work *work) {
K_WORK_DEFINE(raise_profile_changed_event_work, raise_profile_changed_event_callback);
static void raise_inactive_profile_changed_event(uint8_t index) {
raise_zmk_ble_inactive_profile_changed(
(struct zmk_ble_inactive_profile_changed){.index = index, .profile = &profiles[index]});
}
struct raise_inactive_profile_changed_event_work {
struct k_work work;
uint8_t index;
} raise_inactive_profile_changed_event_work;
static void raise_inactive_profile_changed_event_callback(struct k_work *work) {
struct raise_inactive_profile_changed_event_work *info =
CONTAINER_OF(work, struct raise_inactive_profile_changed_event_work, work);
raise_inactive_profile_changed_event(info->index);
}
bool zmk_ble_active_profile_is_open(void) { return zmk_ble_profile_is_open(active_profile); }
bool zmk_ble_profile_is_open(uint8_t index) {
@ -527,6 +544,13 @@ static void connected(struct bt_conn *conn, uint8_t err) {
if (is_conn_active_profile(conn)) {
LOG_DBG("Active profile connected");
k_work_submit(&raise_profile_changed_event_work);
} else {
const int profile_index = zmk_ble_profile_index(bt_conn_get_dst(conn));
if (profile_index > -1) {
LOG_DBG("Other profile connected");
raise_inactive_profile_changed_event_work.index = profile_index;
k_work_submit(&raise_inactive_profile_changed_event_work.work);
}
}
}
@ -552,6 +576,13 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) {
if (is_conn_active_profile(conn)) {
LOG_DBG("Active profile disconnected");
k_work_submit(&raise_profile_changed_event_work);
} else {
const int profile_index = zmk_ble_profile_index(bt_conn_get_dst(conn));
if (profile_index > -1) {
LOG_DBG("Other profile disconnected");
raise_inactive_profile_changed_event_work.index = profile_index;
k_work_submit(&raise_inactive_profile_changed_event_work.work);
}
}
}
@ -742,6 +773,8 @@ static int zmk_ble_init(void) {
#if IS_ENABLED(CONFIG_SETTINGS)
settings_register(&profiles_handler);
k_work_init_delayable(&ble_save_work, ble_save_profile_work);
k_work_init(&raise_inactive_profile_changed_event_work.work,
raise_inactive_profile_changed_event_callback);
#else
zmk_ble_complete_startup();
#endif

View File

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