Bluetooth (#518)

* Bluetooth LE addition

* fixes

* use the psram!

helps a little on S3 (t-deck)

* custom device name

* Update symbols.c

* Feedback + fixes

Fixes external app start/stop server (child devices)
Fixes BtManage causing a full system hang upon disabling bt when a device is connected to the host.

* updoot

* more updoot

* move back!

* Revert "move back!"

This reverts commit d3694365c634acc5db62ac59771c496cb971a727.

* fix some of the things

* Addressing feedback? hmm

* Fixes

Bug 1 — Reconnect loop / Reconnect not working fixed
Bug 2 — Name-only advertising overwrites HID advertising
Bug 3 — BleHidDeviceCtx leak on re-enable
Enhancement — HID device auto-start on radio re-enable

* stuff...

* update for consistency with others

* fix crashes

and some bonus symbols

* a few symbols, i2c speed, cdn message

100kHz i2c speed seems to be more compatible with m5stack modules...and probably in general.
cdn message no longer applies

* Hide BT Settings when bt not enabled

* Addressing things and device fixes

* Missed one!

* stuff
This commit is contained in:
Shadowtrance
2026-05-23 06:49:37 +10:00
committed by GitHub
parent 895e6bc50d
commit 5c78d55b04
72 changed files with 7155 additions and 352 deletions
@@ -0,0 +1,103 @@
#include <tactility/drivers/bluetooth.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#define BT_API(device) ((const struct BluetoothApi*)device_get_driver(device)->api)
extern "C" {
// ---- Device lookup ----
struct Device* bluetooth_find_first_ready_device() {
struct Device* found = nullptr;
device_for_each_of_type(&BLUETOOTH_TYPE, &found, [](struct Device* dev, void* ctx) -> bool {
if (device_is_ready(dev)) {
*static_cast<struct Device**>(ctx) = dev;
return false;
}
return true;
});
return found;
}
// ---- Core radio / scan ----
error_t bluetooth_get_radio_state(struct Device* device, enum BtRadioState* state) {
return BT_API(device)->get_radio_state(device, state);
}
error_t bluetooth_set_radio_enabled(struct Device* device, bool enabled) {
return BT_API(device)->set_radio_enabled(device, enabled);
}
error_t bluetooth_scan_start(struct Device* device) {
return BT_API(device)->scan_start(device);
}
error_t bluetooth_scan_stop(struct Device* device) {
return BT_API(device)->scan_stop(device);
}
bool bluetooth_is_scanning(struct Device* device) {
return BT_API(device)->is_scanning(device);
}
// ---- Pairing ----
error_t bluetooth_pair(struct Device* device, const BtAddr addr) {
return BT_API(device)->pair(device, addr);
}
error_t bluetooth_unpair(struct Device* device, const BtAddr addr) {
return BT_API(device)->unpair(device, addr);
}
error_t bluetooth_get_paired_peers(struct Device* device, struct BtPeerRecord* out, size_t* count) {
return BT_API(device)->get_paired_peers(device, out, count);
}
// ---- Connect / disconnect ----
error_t bluetooth_connect(struct Device* device, const BtAddr addr, enum BtProfileId profile) {
return BT_API(device)->connect(device, addr, profile);
}
error_t bluetooth_disconnect(struct Device* device, const BtAddr addr, enum BtProfileId profile) {
return BT_API(device)->disconnect(device, addr, profile);
}
// ---- Event callbacks ----
error_t bluetooth_add_event_callback(struct Device* device, void* context, BtEventCallback callback) {
return BT_API(device)->add_event_callback(device, context, callback);
}
error_t bluetooth_remove_event_callback(struct Device* device, BtEventCallback callback) {
return BT_API(device)->remove_event_callback(device, callback);
}
error_t bluetooth_set_device_name(struct Device* device, const char* name) {
return BT_API(device)->set_device_name(device, name);
}
error_t bluetooth_get_device_name(struct Device* device, char* buf, size_t buf_len) {
return BT_API(device)->get_device_name(device, buf, buf_len);
}
// ---- HID host active flag ----
void bluetooth_set_hid_host_active(struct Device* device, bool active) {
BT_API(device)->set_hid_host_active(device, active);
}
void bluetooth_fire_event(struct Device* device, struct BtEvent event) {
BT_API(device)->fire_event(device, event);
}
// ---- Device type ----
const struct DeviceType BLUETOOTH_TYPE = {
.name = "bluetooth",
};
} // extern "C"
@@ -0,0 +1,57 @@
#include <tactility/drivers/bluetooth_hid_device.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#define BT_HID_DEVICE_API(device) ((const struct BtHidDeviceApi*)device_get_driver(device)->api)
extern "C" {
const struct DeviceType BLUETOOTH_HID_DEVICE_TYPE = {
.name = "bluetooth-hid-device",
};
struct Device* bluetooth_hid_device_get_device() {
struct Device* found = nullptr;
device_for_each_of_type(&BLUETOOTH_HID_DEVICE_TYPE, &found, [](struct Device* dev, void* ctx) -> bool {
if (device_is_ready(dev)) {
*static_cast<struct Device**>(ctx) = dev;
return false;
}
return true;
});
return found;
}
error_t bluetooth_hid_device_start(struct Device* device, enum BtHidDeviceMode mode) {
return BT_HID_DEVICE_API(device)->start(device, mode);
}
error_t bluetooth_hid_device_stop(struct Device* device) {
return BT_HID_DEVICE_API(device)->stop(device);
}
error_t bluetooth_hid_device_send_key(struct Device* device, uint8_t keycode, bool pressed) {
return BT_HID_DEVICE_API(device)->send_key(device, keycode, pressed);
}
error_t bluetooth_hid_device_send_keyboard(struct Device* device, const uint8_t* report, size_t len) {
return BT_HID_DEVICE_API(device)->send_keyboard(device, report, len);
}
error_t bluetooth_hid_device_send_consumer(struct Device* device, const uint8_t* report, size_t len) {
return BT_HID_DEVICE_API(device)->send_consumer(device, report, len);
}
error_t bluetooth_hid_device_send_mouse(struct Device* device, const uint8_t* report, size_t len) {
return BT_HID_DEVICE_API(device)->send_mouse(device, report, len);
}
error_t bluetooth_hid_device_send_gamepad(struct Device* device, const uint8_t* report, size_t len) {
return BT_HID_DEVICE_API(device)->send_gamepad(device, report, len);
}
bool bluetooth_hid_device_is_connected(struct Device* device) {
return BT_HID_DEVICE_API(device)->is_connected(device);
}
} // extern "C"
@@ -0,0 +1,41 @@
#include <tactility/drivers/bluetooth_midi.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#define BT_MIDI_API(device) ((const struct BtMidiApi*)device_get_driver(device)->api)
extern "C" {
const struct DeviceType BLUETOOTH_MIDI_TYPE = {
.name = "bluetooth-midi",
};
struct Device* bluetooth_midi_get_device() {
struct Device* found = nullptr;
device_for_each_of_type(&BLUETOOTH_MIDI_TYPE, &found, [](struct Device* dev, void* ctx) -> bool {
if (device_is_ready(dev)) {
*static_cast<struct Device**>(ctx) = dev;
return false;
}
return true;
});
return found;
}
error_t bluetooth_midi_start(struct Device* device) {
return BT_MIDI_API(device)->start(device);
}
error_t bluetooth_midi_stop(struct Device* device) {
return BT_MIDI_API(device)->stop(device);
}
error_t bluetooth_midi_send(struct Device* device, const uint8_t* msg, size_t len) {
return BT_MIDI_API(device)->send(device, msg, len);
}
bool bluetooth_midi_is_connected(struct Device* device) {
return BT_MIDI_API(device)->is_connected(device);
}
} // extern "C"
@@ -0,0 +1,45 @@
#include <tactility/drivers/bluetooth_serial.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#define BT_SERIAL_API(device) ((const struct BtSerialApi*)device_get_driver(device)->api)
extern "C" {
const struct DeviceType BLUETOOTH_SERIAL_TYPE = {
.name = "bluetooth-serial",
};
struct Device* bluetooth_serial_get_device() {
struct Device* found = nullptr;
device_for_each_of_type(&BLUETOOTH_SERIAL_TYPE, &found, [](struct Device* dev, void* ctx) -> bool {
if (device_is_ready(dev)) {
*static_cast<struct Device**>(ctx) = dev;
return false;
}
return true;
});
return found;
}
error_t bluetooth_serial_start(struct Device* device) {
return BT_SERIAL_API(device)->start(device);
}
error_t bluetooth_serial_stop(struct Device* device) {
return BT_SERIAL_API(device)->stop(device);
}
error_t bluetooth_serial_write(struct Device* device, const uint8_t* data, size_t len, size_t* written) {
return BT_SERIAL_API(device)->write(device, data, len, written);
}
error_t bluetooth_serial_read(struct Device* device, uint8_t* data, size_t max_len, size_t* read_out) {
return BT_SERIAL_API(device)->read(device, data, max_len, read_out);
}
bool bluetooth_serial_is_connected(struct Device* device) {
return BT_SERIAL_API(device)->is_connected(device);
}
} // extern "C"
+50
View File
@@ -4,6 +4,10 @@
#include <tactility/concurrent/timer.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/bluetooth.h>
#include <tactility/drivers/bluetooth_serial.h>
#include <tactility/drivers/bluetooth_midi.h>
#include <tactility/drivers/bluetooth_hid_device.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/i2c_controller.h>
#include <tactility/drivers/i2s_controller.h>
@@ -50,6 +54,7 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(device_for_each_child),
DEFINE_MODULE_SYMBOL(device_for_each_of_type),
DEFINE_MODULE_SYMBOL(device_exists_of_type),
DEFINE_MODULE_SYMBOL(device_find_by_name),
// driver
DEFINE_MODULE_SYMBOL(driver_construct),
DEFINE_MODULE_SYMBOL(driver_destruct),
@@ -117,6 +122,51 @@ const struct ModuleSymbol KERNEL_SYMBOLS[] = {
DEFINE_MODULE_SYMBOL(uart_controller_get_available),
DEFINE_MODULE_SYMBOL(uart_controller_flush_input),
DEFINE_MODULE_SYMBOL(UART_CONTROLLER_TYPE),
// drivers/bluetooth
DEFINE_MODULE_SYMBOL(bluetooth_find_first_ready_device),
DEFINE_MODULE_SYMBOL(bluetooth_get_radio_state),
DEFINE_MODULE_SYMBOL(bluetooth_set_radio_enabled),
DEFINE_MODULE_SYMBOL(bluetooth_scan_start),
DEFINE_MODULE_SYMBOL(bluetooth_scan_stop),
DEFINE_MODULE_SYMBOL(bluetooth_is_scanning),
DEFINE_MODULE_SYMBOL(bluetooth_pair),
DEFINE_MODULE_SYMBOL(bluetooth_unpair),
DEFINE_MODULE_SYMBOL(bluetooth_get_paired_peers),
DEFINE_MODULE_SYMBOL(bluetooth_connect),
DEFINE_MODULE_SYMBOL(bluetooth_disconnect),
DEFINE_MODULE_SYMBOL(bluetooth_add_event_callback),
DEFINE_MODULE_SYMBOL(bluetooth_remove_event_callback),
DEFINE_MODULE_SYMBOL(bluetooth_set_device_name),
DEFINE_MODULE_SYMBOL(bluetooth_get_device_name),
DEFINE_MODULE_SYMBOL(bluetooth_set_hid_host_active),
DEFINE_MODULE_SYMBOL(bluetooth_fire_event),
DEFINE_MODULE_SYMBOL(BLUETOOTH_TYPE),
// drivers/bluetooth_serial
DEFINE_MODULE_SYMBOL(bluetooth_serial_get_device),
DEFINE_MODULE_SYMBOL(bluetooth_serial_start),
DEFINE_MODULE_SYMBOL(bluetooth_serial_stop),
DEFINE_MODULE_SYMBOL(bluetooth_serial_write),
DEFINE_MODULE_SYMBOL(bluetooth_serial_read),
DEFINE_MODULE_SYMBOL(bluetooth_serial_is_connected),
DEFINE_MODULE_SYMBOL(BLUETOOTH_SERIAL_TYPE),
// drivers/bluetooth_midi
DEFINE_MODULE_SYMBOL(bluetooth_midi_get_device),
DEFINE_MODULE_SYMBOL(bluetooth_midi_start),
DEFINE_MODULE_SYMBOL(bluetooth_midi_stop),
DEFINE_MODULE_SYMBOL(bluetooth_midi_send),
DEFINE_MODULE_SYMBOL(bluetooth_midi_is_connected),
DEFINE_MODULE_SYMBOL(BLUETOOTH_MIDI_TYPE),
// drivers/bluetooth_hid_device
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_get_device),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_start),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_stop),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_send_key),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_send_keyboard),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_send_consumer),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_send_mouse),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_send_gamepad),
DEFINE_MODULE_SYMBOL(bluetooth_hid_device_is_connected),
DEFINE_MODULE_SYMBOL(BLUETOOTH_HID_DEVICE_TYPE),
// concurrent/dispatcher
DEFINE_MODULE_SYMBOL(dispatcher_alloc),
DEFINE_MODULE_SYMBOL(dispatcher_free),