diff --git a/app/src/rgb_underglow.c b/app/src/rgb_underglow.c index 026b99fdbea..b7a1c99ea20 100644 --- a/app/src/rgb_underglow.c +++ b/app/src/rgb_underglow.c @@ -161,9 +161,9 @@ static struct led_rgb hsb_to_rgb(struct zmk_led_hsb hsb) { #define LED_RGB(hex) \ ((struct led_rgb){ \ - r : LED_RGB_SCALING_MULTIPLE * (((hex)&0xFF0000) >> 16), \ - g : LED_RGB_SCALING_MULTIPLE * (((hex)&0x00FF00) >> 8), \ - b : LED_RGB_SCALING_MULTIPLE * (((hex)&0x0000FF) >> 0) \ + r : LED_RGB_SCALING_MULTIPLE * (((hex) & 0xFF0000) >> 16), \ + g : LED_RGB_SCALING_MULTIPLE * (((hex) & 0x00FF00) >> 8), \ + b : LED_RGB_SCALING_MULTIPLE * (((hex) & 0x0000FF) >> 0) \ }) int zmk_rgb_underglow_set_periph(struct zmk_periph_led periph) { @@ -505,16 +505,9 @@ static int zmk_rgb_underglow_init(void) { return -ENODEV; } #endif + led_data.indicators = 0; #if IS_ENABLED(CONFIG_SETTINGS) - settings_subsys_init(); - - int err = settings_register(&rgb_conf); - if (err) { - LOG_ERR("Failed to register the ext_power settings handler (err %d)", err); - return err; - } - led_data.indicators = 0; k_work_init_delayable(&underglow_save_work, zmk_rgb_underglow_save_state_work); #endif @@ -742,60 +735,61 @@ static int rgb_underglow_auto_state(bool target_wake_state) { sleep_state.rgb_state_before_sleeping = state.on; return zmk_rgb_underglow_off(); } - +} #endif - static int rgb_underglow_event_listener(const zmk_event_t *eh) { + +int rgb_underglow_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) - if (as_zmk_activity_state_changed(eh)) { - return rgb_underglow_auto_state(zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); - } + if (as_zmk_activity_state_changed(eh)) { + return rgb_underglow_auto_state(zmk_activity_get_state() == ZMK_ACTIVITY_ACTIVE); + } #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) - if (as_zmk_usb_conn_state_changed(eh)) { - led_data.indicators = zmk_hid_indicators_get_current_profile(); - led_data.layer = zmk_keymap_highest_layer_active(); - int err = zmk_split_bt_update_led(&led_data); - if (err) { - LOG_ERR("send failed (err %d)", err); - } - static bool prev_state = false; - return rgb_underglow_auto_state(zmk_usb_is_powered()); + if (as_zmk_usb_conn_state_changed(eh)) { + led_data.indicators = zmk_hid_indicators_get_current_profile(); + led_data.layer = zmk_keymap_highest_layer_active(); + int err = zmk_split_bt_update_led(&led_data); + if (err) { + LOG_ERR("send failed (err %d)", err); } + static bool prev_state = false; + return rgb_underglow_auto_state(zmk_usb_is_powered()); + } #endif #if ZMK_BLE_IS_CENTRAL - if (as_zmk_split_peripheral_status_changed(eh)) { - LOG_DBG("event called"); - const struct zmk_split_peripheral_status_changed *ev; - ev = as_zmk_split_peripheral_status_changed(eh); - if (ev->connected) { - k_work_reschedule(&led_update_work, K_MSEC(2500)); - return 0; - } else { - k_work_cancel_delayable(&led_update_work); - return 0; - } + if (as_zmk_split_peripheral_status_changed(eh)) { + LOG_DBG("event called"); + const struct zmk_split_peripheral_status_changed *ev; + ev = as_zmk_split_peripheral_status_changed(eh); + if (ev->connected) { + k_work_reschedule(&led_update_work, K_MSEC(2500)); + return 0; + } else { + k_work_cancel_delayable(&led_update_work); + return 0; } + } #endif - return -ENOTSUP; - } + return -ENOTSUP; +} - ZMK_LISTENER(rgb_underglow, rgb_underglow_event_listener); - // IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) +ZMK_LISTENER(rgb_underglow, rgb_underglow_event_listener); +// IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_IDLE) - ZMK_SUBSCRIPTION(rgb_underglow, zmk_activity_state_changed); +ZMK_SUBSCRIPTION(rgb_underglow, zmk_activity_state_changed); #endif #if IS_ENABLED(CONFIG_ZMK_RGB_UNDERGLOW_AUTO_OFF_USB) - ZMK_SUBSCRIPTION(rgb_underglow, zmk_usb_conn_state_changed); +ZMK_SUBSCRIPTION(rgb_underglow, zmk_usb_conn_state_changed); #endif #if ZMK_BLE_IS_CENTRAL - ZMK_SUBSCRIPTION(rgb_underglow, zmk_split_peripheral_status_changed); +ZMK_SUBSCRIPTION(rgb_underglow, zmk_split_peripheral_status_changed); #endif - SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); +SYS_INIT(zmk_rgb_underglow_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index b2843589e93..899c9f3f365 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -520,32 +520,25 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn, slot->batt_lvl_read_params.single.offset = 0; bt_gatt_read(conn, &slot->batt_lvl_read_params); #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */ + } else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_UPDATE_LED_UUID))) { + LOG_DBG("Found update led handle"); + slot->update_led_handle = bt_gatt_attr_value_handle(attr); + } else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_UPDATE_BL_UUID))) { + LOG_DBG("Found update bl handle"); + slot->update_bl_handle = bt_gatt_attr_value_handle(attr); } bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle && - slot->selected_physical_layout_handle; -} -else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, - BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_UPDATE_LED_UUID))) { - LOG_DBG("Found update led handle"); - slot->update_led_handle = bt_gatt_attr_value_handle(attr); -} -else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, - BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_UPDATE_BL_UUID))) { - LOG_DBG("Found update bl handle"); - slot->update_bl_handle = bt_gatt_attr_value_handle(attr); -} - -bool subscribed = subscribed && (slot->update_bl_handle && slot->update_led_handle); + slot->selected_physical_layout_handle && slot->update_bl_handle && + slot->update_led_handle; #if ZMK_KEYMAP_HAS_SENSORS subscribed = subscribed && slot->sensor_subscribe_params.value_handle; #endif /* ZMK_KEYMAP_HAS_SENSORS */ -#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) - subscribed = subscribed && slot->update_hid_indicators; -#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) -subscribed = subscribed && slot->batt_lvl_subscribe_params.value_handle; + subscribed = subscribed && slot->batt_lvl_subscribe_params.value_handle; #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */ return subscribed ? BT_GATT_ITER_STOP : BT_GATT_ITER_CONTINUE; @@ -989,13 +982,18 @@ void split_central_split_led_callback(struct k_work *work) { while (k_msgq_get(&zmk_split_central_split_led_msgq, &payload, K_NO_WAIT) == 0) { if (peripherals[0].state != PERIPHERAL_SLOT_STATE_CONNECTED) { - LOG_ERR("Source not connected"); - continue; + return; } - if (!peripherals[0].update_led_handle) { - LOG_ERR("handle not discovered"); - continue; + if (peripherals[0].update_led_handle == 0) { + // It appears that sometimes the peripheral is considered connected + // before the GATT characteristics have been discovered. If this is + // the case, the selected_physical_layout_handle will not yet be set. + return; + } + + if (bt_conn_get_security(peripherals[0].conn) < BT_SECURITY_L2) { + return; } int err = bt_gatt_write_without_response(peripherals[0].conn, @@ -1055,13 +1053,15 @@ void split_central_split_bl_callback(struct k_work *work) { while (k_msgq_get(&zmk_split_central_split_bl_msgq, &payload, K_NO_WAIT) == 0) { if (peripherals[0].state != PERIPHERAL_SLOT_STATE_CONNECTED) { - LOG_ERR("Source not connected"); - continue; + return; } if (!peripherals[0].update_bl_handle) { - LOG_ERR("handle not discovered"); - continue; + return; + } + + if (bt_conn_get_security(peripherals[0].conn) < BT_SECURITY_L2) { + return; } int err =