Device cleanup (#592)

- Replaced device_find_\* usage by device_get_\* variants.
- Removed deprecated device_find_\* functions.
- Improved reliability of Bluetooth scanning, pairing, connection, and HID host lifecycle behavior, including peer auto-connect handling.
- Improved Bluetooth/USB status indicators and clarified “Eject failed” alerts with the affected mount path.
This commit is contained in:
Ken Van Hoeylandt
2026-07-27 17:29:12 +02:00
committed by GitHub
parent 3354924359
commit d1f06cb774
20 changed files with 328 additions and 204 deletions
@@ -17,7 +17,7 @@ class BtManage final : public App {
State state;
View view = View(&bindings, &state);
bool isViewEnabled = false;
struct Device* btDevice = nullptr;
Device* btDevice = nullptr;
public:
+3 -7
View File
@@ -54,13 +54,8 @@ class BootApp : public App {
);
static void setupDisplay() {
auto* display = device_find_first_by_type(&DISPLAY_TYPE);
// Boards not yet migrated to the kernel display driver register a placeholder device (so
// the devicetree node resolves) with a NULL api - nothing for this function to act on.
if (display != nullptr && device_get_driver(display)->api == nullptr) {
display = nullptr;
}
if (display != nullptr) {
Device* display = nullptr;
if (device_get_first_by_type(&DISPLAY_TYPE, &display) == ERROR_NONE) {
Device* backlight;
if (display_get_backlight(display, &backlight) == ERROR_NONE) {
if (!device_is_ready(backlight)) {
@@ -83,6 +78,7 @@ class BootApp : public App {
} else {
LOG_I(TAG, "No backlight for %s", display->name);
}
device_put(display);
} else {
LOG_I(TAG, "No kernel display");
}
+37 -13
View File
@@ -18,25 +18,38 @@ extern const AppManifest manifest;
static void onBtToggled(bool requestOn) {
#if defined(CONFIG_BT_NIMBLE_ENABLED)
Device* dev = device_find_first_by_type(&BLUETOOTH_TYPE);
if (!dev) return;
bool radio_on = bluetooth::isRadioOnOrPending(dev);
if (requestOn && !radio_on) {
bluetooth::start(dev);
} else if (!requestOn && radio_on) {
bluetooth::stop(dev);
Device* dev;
if (device_get_first_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bool radio_on = bluetooth::isRadioOnOrPending(dev);
if (requestOn && !radio_on) {
LOG_I(TAG, "Turning on");
bluetooth::start(dev);
} else if (!requestOn && radio_on) {
LOG_I(TAG, "Turning off");
bluetooth::stop(dev);
}
device_put(dev);
} else {
LOG_W(TAG, "Toggle: No bluetooth device found");
}
#endif
}
static void onScanToggled(bool enabled) {
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
if (!dev) return;
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) != ERROR_NONE) {
LOG_W(TAG, "Scan: No bluetooth device found");
return;
}
if (enabled) {
bluetooth_scan_start(dev);
} else {
bluetooth_scan_stop(dev);
}
device_put(dev);
}
static void onConnectPeer(const std::array<uint8_t, 6>& addr, int profileId) {
@@ -86,7 +99,7 @@ void BtManage::requestViewUpdate() {
unlock();
}
void BtManage::onBtEvent(const struct BtEvent& event) {
void BtManage::onBtEvent(const BtEvent& event) {
auto radio_state = bluetooth::getRadioState();
LOG_I(TAG, "Update with state %s", bluetooth::radioStateToString(radio_state));
getState().setRadioState(radio_state);
@@ -112,10 +125,13 @@ void BtManage::onBtEvent(const struct BtEvent& event) {
case BT_EVENT_RADIO_STATE_CHANGED:
if (event.radio_state == BT_RADIO_STATE_ON) {
getState().updatePairedPeers();
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
if (dev && !bluetooth_is_scanning(dev)) {
Device* dev = nullptr;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE && !bluetooth_is_scanning(dev)) {
bluetooth_scan_start(dev);
}
if (dev) {
device_put(dev);
}
}
break;
default:
@@ -141,7 +157,9 @@ void BtManage::onShow(AppContext& app, lv_obj_t* parent) {
// Initialise state and view before subscribing to avoid incoming events
// racing with state initialisation.
state.setRadioState(bluetooth::getRadioState());
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
Device* dev = nullptr;
device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev);
state.setScanning(dev ? bluetooth_is_scanning(dev) : false);
state.updateScanResults();
state.updatePairedPeers();
@@ -152,6 +170,11 @@ void BtManage::onShow(AppContext& app, lv_obj_t* parent) {
view.update();
unlock();
if (btDevice) {
// Decrease refcount before re-ssignment
device_put(btDevice);
}
btDevice = dev;
if (btDevice) {
bluetooth_add_event_callback(btDevice, this, onKernelBtEvent);
@@ -172,6 +195,7 @@ void BtManage::onHide(AppContext& app) {
lock();
if (btDevice) {
bluetooth_remove_event_callback(btDevice, onKernelBtEvent);
device_put(btDevice);
btDevice = nullptr;
}
isViewEnabled = false;
+5 -1
View File
@@ -46,8 +46,12 @@ static void onEnableOnBootParentClicked(lv_event_t* event) {
static void onScanButtonClicked(lv_event_t* event) {
auto bt = std::static_pointer_cast<BtManage>(getCurrentApp());
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
Device* dev = nullptr;
device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev);
bool scanning = dev ? bluetooth_is_scanning(dev) : false;
if (dev) {
device_put(dev);
}
bt->getBindings().onScanToggled(!scanning);
}
@@ -121,8 +121,12 @@ public:
}
void onShow(AppContext& app, lv_obj_t* parent) override {
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
bluetooth_add_event_callback(dev, this, onKernelBtEvent);
{
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_add_event_callback(dev, this, onKernelBtEvent);
device_put(dev);
}
}
// Load stored settings (name, autoConnect)
@@ -189,8 +193,10 @@ public:
}
void onHide(AppContext& app) override {
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_remove_event_callback(dev, onKernelBtEvent);
device_put(dev);
}
viewEnabled = false;
}
+6 -2
View File
@@ -436,12 +436,16 @@ void View::onEjectPressed() {
std::string mount_path = state->getSelectedChildPath();
LOG_I(TAG, "Ejecting %s", mount_path.c_str());
struct Device* msc_dev = device_find_first_active_by_type(&USB_HOST_MSC_TYPE);
if (!msc_dev || !usb_msc_eject(msc_dev, mount_path.c_str())) {
Device* msc_dev = nullptr;
if (device_get_first_active_by_type(&USB_HOST_MSC_TYPE, &msc_dev) != ERROR_NONE || !usb_msc_eject(msc_dev, mount_path.c_str())) {
LOG_W(TAG, "usb_msc_eject: %s not found", mount_path.c_str());
alertdialog::start("Eject failed", "Could not eject \"" + file::getLastPathSegment(mount_path) + "\".");
}
if (msc_dev) {
device_put(msc_dev);
}
onNavigate();
state->setEntriesForPath(state->getCurrentPath());
update();
@@ -25,15 +25,18 @@ namespace tt::app::kerneldisplay {
constexpr auto* TAG = "KernelDisplay";
static Device* getBacklightDevice() {
Device* display = device_find_first_by_type(&DISPLAY_TYPE);
check(display);
Device* display;
check(device_get_first_by_type(&DISPLAY_TYPE, &display) == ERROR_NONE);
// Boards not yet migrated to the kernel display driver register a placeholder device (so the
// devicetree node resolves) with a NULL api - nothing for display_get_backlight() to act on.
if (device_get_driver(display)->api == nullptr) {
device_put(display);
return nullptr;
}
Device* backlight = nullptr;
return display_get_backlight(display, &backlight) == ERROR_NONE ? backlight : nullptr;
display_get_backlight(display, &backlight);
device_put(display);
return backlight;
}
class KernelDisplayApp final : public App {
@@ -28,9 +28,10 @@ static uint32_t timeoutMsToIndex(uint32_t ms) {
static void applyKeyboardBacklight(bool enabled, uint8_t brightness) {
// TODO: Get keyboard backlight from (optional) keyboard child device
Device* backlight = device_find_by_name("keyboard_backlight");
if (backlight != nullptr) {
Device* backlight;
if (device_get_by_name("keyboard_backlight", &backlight) == ERROR_NONE) {
backlight_set_brightness(backlight, enabled ? brightness : 0);
device_put(backlight);
}
}
+26 -10
View File
@@ -124,8 +124,10 @@ static void bt_event_bridge(Device*, void* /*context*/, BtEvent event) {
}
if (has_hid_host_auto) {
LOG_I(TAG, "HID host auto-connect peer found — starting scan");
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_scan_start(dev);
device_put(dev);
}
} else if (has_hid_device_auto) {
LOG_I(TAG, "HID device auto-start (bonded peer found)");
@@ -231,10 +233,12 @@ static void bt_event_bridge(Device*, void* /*context*/, BtEvent event) {
}
}
if (has_auto) {
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
if (!bluetooth_is_scanning(dev)) {
bluetooth_scan_start(dev);
}
device_put(dev);
}
}
});
@@ -337,10 +341,19 @@ const char* radioStateToString(RadioState state) {
}
RadioState getRadioState() {
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
if (dev == nullptr) return RadioState::Off;
BtRadioState state = BT_RADIO_STATE_OFF;
bluetooth_get_radio_state(dev, &state);
// Scoped to safeguard dev usage
{
Device* dev = nullptr;
device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev);
if (dev == nullptr) {
return RadioState::Off;
}
bluetooth_get_radio_state(dev, &state);
device_put(dev);
}
switch (state) {
case BT_RADIO_STATE_OFF: return RadioState::Off;
case BT_RADIO_STATE_ON_PENDING: return RadioState::OnPending;
@@ -405,9 +418,10 @@ void pair(const std::array<uint8_t, 6>& /*addr*/) {
}
void unpair(const std::array<uint8_t, 6>& addr) {
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
if (dev != nullptr) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_unpair(dev, addr.data());
device_put(dev);
}
settings::remove(settings::addrToHex(addr));
}
@@ -442,9 +456,11 @@ void disconnect(const std::array<uint8_t, 6>& addr, int profileId) {
bluetooth_hid_device_stop(dev);
}
} else {
Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
if (dev == nullptr) return;
bluetooth_disconnect(dev, addr.data(), (BtProfileId)profileId);
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_disconnect(dev, addr.data(), (BtProfileId)profileId);
device_put(dev);
}
}
}
@@ -500,12 +500,14 @@ static void hidHostSubscribeNext(HidHostCtx& ctx) {
}
device.name = name;
settings::save(device);
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
BtEvent e = {};
e.type = BT_EVENT_PROFILE_STATE_CHANGED;
e.profile_state.state = BT_PROFILE_STATE_CONNECTED;
e.profile_state.profile = BT_PROFILE_HID_HOST;
bluetooth_fire_event(dev, e);
device_put(dev);
}
});
return;
@@ -660,13 +662,15 @@ static int hidHostGapCb(struct ble_gap_event* event, void* /*arg*/) {
} else {
LOG_W(TAG, "Connect failed status=%d", event->connect.status);
hid_host_ctx.reset();
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_set_hid_host_active(dev, false);
struct BtEvent e = {};
BtEvent e = {};
e.type = BT_EVENT_PROFILE_STATE_CHANGED;
e.profile_state.state = BT_PROFILE_STATE_IDLE;
e.profile_state.profile = BT_PROFILE_HID_HOST;
bluetooth_fire_event(dev, e);
device_put(dev);
}
}
break;
@@ -685,13 +689,15 @@ static int hidHostGapCb(struct ble_gap_event* event, void* /*arg*/) {
hid_host_mouse_btn.store(false);
hid_host_mouse_active.store(false);
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_set_hid_host_active(dev, false);
struct BtEvent e = {};
e.type = BT_EVENT_PROFILE_STATE_CHANGED;
e.profile_state.state = BT_PROFILE_STATE_IDLE;
e.profile_state.profile = BT_PROFILE_HID_HOST;
bluetooth_fire_event(dev, e);
device_put(dev);
}
getMainDispatcher().dispatch([saved_kb, saved_mouse, saved_cursor, saved_queue] {
@@ -793,7 +799,13 @@ void hidHostConnect(const std::array<uint8_t, 6>& addr) {
}
// Notify driver that a HID host central connection is starting.
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) bluetooth_set_hid_host_active(dev, true);
{
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_set_hid_host_active(dev, true);
device_put(dev);
}
}
// Look up the addr_type from the cached scan results.
ble_addr_t ble_addr = {};
@@ -813,7 +825,8 @@ void hidHostConnect(const std::array<uint8_t, 6>& addr) {
if (rc != 0) {
LOG_W(TAG, "ble_gap_connect failed rc=%d", rc);
hid_host_ctx.reset();
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
bluetooth_set_hid_host_active(dev, false);
// Fire IDLE so bt_event_bridge can start a new scan and retry.
BtEvent e = {};
@@ -821,6 +834,7 @@ void hidHostConnect(const std::array<uint8_t, 6>& addr) {
e.profile_state.state = BT_PROFILE_STATE_IDLE;
e.profile_state.profile = BT_PROFILE_HID_HOST;
bluetooth_fire_event(dev, e);
device_put(dev);
}
} else {
LOG_I(TAG, "Connecting...");
@@ -867,11 +881,13 @@ void autoConnectHidHost() {
auto peers = settings::loadAll();
for (const auto& peer : peers) {
if (peer.autoConnect && peer.profileId == BT_PROFILE_HID_HOST) {
if (Device* dev = device_find_first_active_by_type(&BLUETOOTH_TYPE)) {
Device* dev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &dev) == ERROR_NONE) {
if (!bluetooth_is_scanning(dev)) {
LOG_I(TAG, "Auto-connect HID host: device not in scan, retrying scan");
bluetooth_scan_start(dev);
}
device_put(dev);
}
break;
}
+22 -7
View File
@@ -179,8 +179,11 @@ static void usbHidInputTask(void* arg) {
UsbHidEvent hid_evt;
if (xQueueReceive(ctx->hid_queue, &hid_evt, pdMS_TO_TICKS(100)) != pdTRUE) {
if (!ctx->subscribed) {
struct Device* hid_dev = device_find_first_active_by_type(&USB_HOST_HID_TYPE);
if (hid_dev) ctx->subscribed = usb_host_hid_subscribe(hid_dev, ctx->hid_queue);
Device* hid_dev;
if (device_get_first_active_by_type(&USB_HOST_HID_TYPE, &hid_dev) == ERROR_NONE) {
ctx->subscribed = usb_host_hid_subscribe(hid_dev, ctx->hid_queue);
device_put(hid_dev);
}
}
continue;
}
@@ -300,14 +303,23 @@ void startUsbHidInput() {
return;
}
struct Device* hid_dev = device_find_first_active_by_type(&USB_HOST_HID_TYPE);
if (hid_dev) ctx->subscribed = usb_host_hid_subscribe(hid_dev, ctx->hid_queue);
Device* hid_dev = nullptr;
if (device_get_first_active_by_type(&USB_HOST_HID_TYPE, &hid_dev) == ERROR_NONE) {
ctx->subscribed = usb_host_hid_subscribe(hid_dev, ctx->hid_queue);
device_put(hid_dev);
}
ctx->running = true;
if (xTaskCreate(usbHidInputTask, "usb_hid_inp", TASK_STACK, ctx, TASK_PRIORITY, &ctx->task) != pdPASS) {
LOG_E(TAG, "failed to create task");
ctx->running = false;
if (hid_dev) usb_host_hid_unsubscribe(hid_dev, ctx->hid_queue);
if (ctx->subscribed) {
Device* cleanup_dev = nullptr;
if (device_get_first_active_by_type(&USB_HOST_HID_TYPE, &cleanup_dev) == ERROR_NONE) {
usb_host_hid_unsubscribe(cleanup_dev, ctx->hid_queue);
device_put(cleanup_dev);
}
}
vQueueDelete(ctx->hid_queue);
vQueueDelete(ctx->key_queue);
vSemaphoreDelete(ctx->task_done);
@@ -345,8 +357,11 @@ void stopUsbHidInput() {
ctx->task = nullptr;
if (ctx->subscribed) {
struct Device* hid_dev = device_find_first_active_by_type(&USB_HOST_HID_TYPE);
if (hid_dev) usb_host_hid_unsubscribe(hid_dev, ctx->hid_queue);
Device* hid_dev;
if (device_get_first_active_by_type(&USB_HOST_HID_TYPE, &hid_dev) == ERROR_NONE) {
usb_host_hid_unsubscribe(hid_dev, ctx->hid_queue);
device_put(hid_dev);
}
}
vQueueDelete(ctx->hid_queue);
vQueueDelete(ctx->key_queue);
@@ -19,7 +19,7 @@ constexpr auto* TAG = "RtcTime";
Device* RtcTimeService::findRtcDevice() {
if (!rtcDevice) {
rtcDevice = device_find_first_active_by_type(&RTC_TYPE);
device_get_first_active_by_type(&RTC_TYPE, &rtcDevice);
}
return rtcDevice;
}
@@ -130,6 +130,11 @@ void RtcTimeService::onStop(ServiceContext& serviceContext) {
kernel::unsubscribeSystemEvent(timeEventSubscription);
timeEventSubscription = 0;
}
if (rtcDevice) {
device_put(rtcDevice);
rtcDevice = nullptr;
}
}
extern const ServiceManifest manifest = {
@@ -166,8 +166,18 @@ class StatusbarService final : public Service {
void updateBluetoothIcon() {
auto radio_state = bluetooth::getRadioState();
Device* btdev = device_find_first_active_by_type(&BLUETOOTH_TYPE);
bool scanning = btdev ? bluetooth_is_scanning(btdev) : false;
bool scanning;
{
Device* btdev;
if (device_get_first_active_by_type(&BLUETOOTH_TYPE, &btdev) == ERROR_NONE) {
scanning = bluetooth_is_scanning(btdev);
device_put(btdev);
} else {
scanning = false;
}
}
Device* serial_dev = bluetooth_serial_get_device();
Device* midi_dev = bluetooth_midi_get_device();
bool connected = (serial_dev && bluetooth_serial_is_connected(serial_dev)) ||
@@ -211,11 +221,27 @@ class StatusbarService final : public Service {
}
}
void updateUsbIcon() {
Device* hid_dev = device_find_first_active_by_type(&USB_HOST_HID_TYPE);
Device* midi_dev = device_find_first_active_by_type(&USB_HOST_MIDI_TYPE);
static bool isHidOrMidiConnected() {
Device* hid_dev = nullptr;
device_get_first_active_by_type(&USB_HOST_HID_TYPE, &hid_dev);
Device* midi_dev = nullptr;
device_get_first_active_by_type(&USB_HOST_MIDI_TYPE, &midi_dev);
bool connected = (hid_dev && usb_host_hid_is_connected(hid_dev)) ||
(midi_dev && usb_midi_is_connected(midi_dev));
if (hid_dev) {
device_put(hid_dev);
}
if (midi_dev) {
device_put(midi_dev);
}
return connected;
}
void updateUsbIcon() {
bool connected = isHidOrMidiConnected();
if (!connected) {
// MSC: scan filesystems for any mounted /usb* path
file_system_for_each(&connected, [](struct FileSystem* fs, void* ctx) -> bool {