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:
@@ -0,0 +1,307 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tactility/error.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Device;
|
||||
struct DeviceType;
|
||||
|
||||
// ---- Device name ----
|
||||
|
||||
/**
|
||||
* Maximum BLE device name length in bytes, excluding the NUL terminator.
|
||||
* Must match CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN (set in device.py for BT devices).
|
||||
* ble_svc_gap_device_name_set() returns BLE_HS_EINVAL for names longer than this.
|
||||
*/
|
||||
#define BLE_DEVICE_NAME_MAX 64
|
||||
|
||||
// ---- Address ----
|
||||
|
||||
#define BT_ADDR_LEN 6
|
||||
|
||||
typedef uint8_t BtAddr[BT_ADDR_LEN];
|
||||
|
||||
// ---- Radio ----
|
||||
|
||||
enum BtRadioState {
|
||||
BT_RADIO_STATE_OFF,
|
||||
BT_RADIO_STATE_ON_PENDING,
|
||||
BT_RADIO_STATE_ON,
|
||||
BT_RADIO_STATE_OFF_PENDING,
|
||||
};
|
||||
|
||||
// ---- Peer record ----
|
||||
|
||||
#define BT_NAME_MAX 248
|
||||
|
||||
struct BtPeerRecord {
|
||||
BtAddr addr;
|
||||
/** BLE address type (BLE_ADDR_PUBLIC=0, BLE_ADDR_RANDOM=1, etc.) */
|
||||
uint8_t addr_type;
|
||||
char name[BT_NAME_MAX + 1];
|
||||
int8_t rssi;
|
||||
bool paired;
|
||||
bool connected;
|
||||
};
|
||||
|
||||
// ---- Profile identifiers ----
|
||||
|
||||
enum BtProfileId {
|
||||
/** Connect to a BLE HID device (keyboard, mouse, gamepad) */
|
||||
BT_PROFILE_HID_HOST,
|
||||
/** Present this device as a BLE HID peripheral (keyboard, gamepad) */
|
||||
BT_PROFILE_HID_DEVICE,
|
||||
/** BLE SPP serial port (Nordic UART Service / custom GATT) */
|
||||
BT_PROFILE_SPP,
|
||||
/** BLE MIDI (GATT-based) */
|
||||
BT_PROFILE_MIDI,
|
||||
};
|
||||
|
||||
enum BtProfileState {
|
||||
BT_PROFILE_STATE_IDLE,
|
||||
BT_PROFILE_STATE_CONNECTING,
|
||||
BT_PROFILE_STATE_CONNECTED,
|
||||
BT_PROFILE_STATE_DISCONNECTING,
|
||||
};
|
||||
|
||||
// ---- Events ----
|
||||
|
||||
enum BtEventType {
|
||||
/** Radio state changed */
|
||||
BT_EVENT_RADIO_STATE_CHANGED,
|
||||
/** Started scanning for peers */
|
||||
BT_EVENT_SCAN_STARTED,
|
||||
/** Finished scanning for peers */
|
||||
BT_EVENT_SCAN_FINISHED,
|
||||
/** A new peer was found during scan */
|
||||
BT_EVENT_PEER_FOUND,
|
||||
/** Pairing requires user confirmation (passkey displayed or entry required) */
|
||||
BT_EVENT_PAIR_REQUEST,
|
||||
/** Pairing attempt completed */
|
||||
BT_EVENT_PAIR_RESULT,
|
||||
/** A peer's connection state changed */
|
||||
BT_EVENT_CONNECT_STATE_CHANGED,
|
||||
/** A profile's state changed */
|
||||
BT_EVENT_PROFILE_STATE_CHANGED,
|
||||
/** Data was received on the BLE SPP (NUS) RX characteristic */
|
||||
BT_EVENT_SPP_DATA_RECEIVED,
|
||||
/** Data was received on the BLE MIDI I/O characteristic */
|
||||
BT_EVENT_MIDI_DATA_RECEIVED,
|
||||
};
|
||||
|
||||
enum BtPairResult {
|
||||
BT_PAIR_RESULT_SUCCESS,
|
||||
BT_PAIR_RESULT_FAILED,
|
||||
BT_PAIR_RESULT_REJECTED,
|
||||
/** Stale bond detected and removed; fresh pairing will follow */
|
||||
BT_PAIR_RESULT_BOND_LOST,
|
||||
};
|
||||
|
||||
struct BtPairRequestData {
|
||||
BtAddr addr;
|
||||
uint32_t passkey; /**< Passkey to display (0 if not applicable) */
|
||||
bool needs_confirmation; /**< true: just confirm, false: user must enter passkey */
|
||||
};
|
||||
|
||||
struct BtPairResultData {
|
||||
BtAddr addr;
|
||||
enum BtPairResult result;
|
||||
/** Profile active when pairing completed (BtProfileId value) */
|
||||
int profile;
|
||||
};
|
||||
|
||||
struct BtProfileStateData {
|
||||
BtAddr addr;
|
||||
enum BtProfileId profile;
|
||||
enum BtProfileState state;
|
||||
};
|
||||
|
||||
struct BtEvent {
|
||||
enum BtEventType type;
|
||||
union {
|
||||
enum BtRadioState radio_state;
|
||||
struct BtPeerRecord peer;
|
||||
struct BtPairRequestData pair_request;
|
||||
struct BtPairResultData pair_result;
|
||||
struct BtProfileStateData profile_state;
|
||||
};
|
||||
};
|
||||
|
||||
typedef void (*BtEventCallback)(struct Device* device, void* context, struct BtEvent event);
|
||||
|
||||
// ---- Top-level Bluetooth API ----
|
||||
|
||||
struct BluetoothApi {
|
||||
/**
|
||||
* Get the radio state.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[out] state the current radio state
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*get_radio_state)(struct Device* device, enum BtRadioState* state);
|
||||
|
||||
/**
|
||||
* Enable or disable the Bluetooth radio.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] enabled true to enable, false to disable
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*set_radio_enabled)(struct Device* device, bool enabled);
|
||||
|
||||
/**
|
||||
* Start scanning for nearby BLE devices.
|
||||
* @param[in] device the bluetooth device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*scan_start)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Stop an active scan.
|
||||
* @param[in] device the bluetooth device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*scan_stop)(struct Device* device);
|
||||
|
||||
/**
|
||||
* @param[in] device the bluetooth device
|
||||
* @return true when a scan is in progress
|
||||
*/
|
||||
bool (*is_scanning)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Initiate pairing with a peer.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] addr the peer address
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*pair)(struct Device* device, const BtAddr addr);
|
||||
|
||||
/**
|
||||
* Remove a previously paired peer.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] addr the peer address
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*unpair)(struct Device* device, const BtAddr addr);
|
||||
|
||||
/**
|
||||
* Get the list of currently paired peers.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[out] out the buffer to write records into (may be NULL to query count only)
|
||||
* @param[in, out] count in: capacity of out, out: actual number of paired peers
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*get_paired_peers)(struct Device* device, struct BtPeerRecord* out, size_t* count);
|
||||
|
||||
/**
|
||||
* Connect to a peer using the specified profile.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] addr the peer address
|
||||
* @param[in] profile the profile to connect with
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*connect)(struct Device* device, const BtAddr addr, enum BtProfileId profile);
|
||||
|
||||
/**
|
||||
* Disconnect a peer from the specified profile.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] addr the peer address
|
||||
* @param[in] profile the profile to disconnect from
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*disconnect)(struct Device* device, const BtAddr addr, enum BtProfileId profile);
|
||||
|
||||
/**
|
||||
* Add an event callback.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] context context pointer passed to the callback
|
||||
* @param[in] callback the callback function
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*add_event_callback)(struct Device* device, void* context, BtEventCallback callback);
|
||||
|
||||
/**
|
||||
* Remove a previously added event callback.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] callback the callback to remove
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*remove_event_callback)(struct Device* device, BtEventCallback callback);
|
||||
|
||||
/**
|
||||
* Set the BLE device name used in advertising and the GAP service.
|
||||
* Can be called before or after the radio is enabled.
|
||||
* If called while advertising is active, advertising restarts with the new name.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] name NUL-terminated name (max BLE_DEVICE_NAME_MAX bytes)
|
||||
* @return ERROR_NONE on success, ERROR_INVALID_ARGUMENT if name is too long or NULL
|
||||
*/
|
||||
error_t (*set_device_name)(struct Device* device, const char* name);
|
||||
|
||||
/**
|
||||
* Get the current BLE device name.
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[out] buf buffer to write the name into
|
||||
* @param[in] buf_len size of buf (must be >= BLE_DEVICE_NAME_MAX + 1)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*get_device_name)(struct Device* device, char* buf, size_t buf_len);
|
||||
|
||||
/**
|
||||
* Notify the driver that a HID host connection is in progress or complete.
|
||||
* Called by the Tactility HID host module to prevent name resolution from
|
||||
* initiating a simultaneous central connection (BLE_HS_EALREADY).
|
||||
* @param[in] device the bluetooth device
|
||||
* @param[in] active true when HID host is connecting/connected, false when idle
|
||||
*/
|
||||
void (*set_hid_host_active)(struct Device* device, bool active);
|
||||
|
||||
/**
|
||||
* Fire an event through all registered event callbacks.
|
||||
* Used by the Tactility HID host module to inject profile-state events that
|
||||
* originate outside the platform driver (e.g. HID host connect/disconnect).
|
||||
*/
|
||||
void (*fire_event)(struct Device* device, struct BtEvent event);
|
||||
};
|
||||
|
||||
extern const struct DeviceType BLUETOOTH_TYPE;
|
||||
|
||||
// ---- Public C API ----
|
||||
// These are the only functions external code should call.
|
||||
// The BluetoothApi struct above is the internal driver interface only.
|
||||
|
||||
/**
|
||||
* Find the first ready Bluetooth device.
|
||||
* Use this instead of referencing BLUETOOTH_TYPE directly from external apps,
|
||||
* since data symbols may not be exported by the ELF loader.
|
||||
* @return the first ready Device of BLUETOOTH_TYPE, or NULL if none found.
|
||||
*/
|
||||
struct Device* bluetooth_find_first_ready_device(void);
|
||||
|
||||
error_t bluetooth_get_radio_state(struct Device* device, enum BtRadioState* state);
|
||||
error_t bluetooth_set_radio_enabled(struct Device* device, bool enabled);
|
||||
error_t bluetooth_scan_start(struct Device* device);
|
||||
error_t bluetooth_scan_stop(struct Device* device);
|
||||
bool bluetooth_is_scanning(struct Device* device);
|
||||
error_t bluetooth_pair(struct Device* device, const BtAddr addr);
|
||||
error_t bluetooth_unpair(struct Device* device, const BtAddr addr);
|
||||
error_t bluetooth_get_paired_peers(struct Device* device, struct BtPeerRecord* out, size_t* count);
|
||||
error_t bluetooth_connect(struct Device* device, const BtAddr addr, enum BtProfileId profile);
|
||||
error_t bluetooth_disconnect(struct Device* device, const BtAddr addr, enum BtProfileId profile);
|
||||
error_t bluetooth_add_event_callback(struct Device* device, void* context, BtEventCallback callback);
|
||||
error_t bluetooth_remove_event_callback(struct Device* device, BtEventCallback callback);
|
||||
error_t bluetooth_set_device_name(struct Device* device, const char* name);
|
||||
error_t bluetooth_get_device_name(struct Device* device, char* buf, size_t buf_len);
|
||||
void bluetooth_set_hid_host_active(struct Device* device, bool active);
|
||||
void bluetooth_fire_event(struct Device* device, struct BtEvent event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,124 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tactility/error.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Device;
|
||||
struct DeviceType;
|
||||
|
||||
// ---- HID device mode ----
|
||||
|
||||
/**
|
||||
* Selects the HID report descriptor and appearance used when this device
|
||||
* operates as a BLE HID peripheral.
|
||||
*/
|
||||
enum BtHidDeviceMode {
|
||||
/** Keyboard (report ID 1, 8 bytes) + Consumer (report ID 2, 2 bytes). */
|
||||
BT_HID_DEVICE_MODE_KEYBOARD,
|
||||
/** Mouse only (report ID 1, 4 bytes). */
|
||||
BT_HID_DEVICE_MODE_MOUSE,
|
||||
/** Keyboard + Consumer + Mouse (report IDs 1, 2, 3). */
|
||||
BT_HID_DEVICE_MODE_KEYBOARD_MOUSE,
|
||||
/** Gamepad (report ID 1, 8 bytes: 2-byte buttons + 6-byte axes). */
|
||||
BT_HID_DEVICE_MODE_GAMEPAD,
|
||||
};
|
||||
|
||||
// ---- BLE HID Device child device ----
|
||||
|
||||
/**
|
||||
* BLE HID device profile API (present this device as a BLE HID peripheral).
|
||||
* This API is exposed by a child device of the Bluetooth device.
|
||||
*/
|
||||
struct BtHidDeviceApi {
|
||||
/**
|
||||
* Start advertising as a BLE HID device with the given mode.
|
||||
* Sets up the appropriate GATT report descriptor and starts advertising.
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] mode the HID device mode (keyboard, mouse, gamepad, etc.)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*start)(struct Device* device, enum BtHidDeviceMode mode);
|
||||
|
||||
/**
|
||||
* Stop advertising as a BLE HID device and close any active connection.
|
||||
* @param[in] device the HID device child device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*stop)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Send a single key event when operating as a HID keyboard device.
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] keycode the HID keycode
|
||||
* @param[in] pressed true for key down, false for key up
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send_key)(struct Device* device, uint8_t keycode, bool pressed);
|
||||
|
||||
/**
|
||||
* Send a full keyboard HID report (8 bytes: modifier, reserved, keycodes[6]).
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] report pointer to the 8-byte keyboard report
|
||||
* @param[in] len number of bytes (up to 8)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send_keyboard)(struct Device* device, const uint8_t* report, size_t len);
|
||||
|
||||
/**
|
||||
* Send a consumer control HID report (2 bytes: 16-bit usage code, little-endian).
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] report pointer to the 2-byte consumer report
|
||||
* @param[in] len number of bytes (up to 2)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send_consumer)(struct Device* device, const uint8_t* report, size_t len);
|
||||
|
||||
/**
|
||||
* Send a mouse HID report (4 bytes: buttons, X, Y, wheel).
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] report pointer to the 4-byte mouse report
|
||||
* @param[in] len number of bytes (up to 4)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send_mouse)(struct Device* device, const uint8_t* report, size_t len);
|
||||
|
||||
/**
|
||||
* Send a gamepad HID report (8 bytes: buttons[2] + axes[6]).
|
||||
* @param[in] device the HID device child device
|
||||
* @param[in] report pointer to the 8-byte gamepad report
|
||||
* @param[in] len number of bytes (up to 8)
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send_gamepad)(struct Device* device, const uint8_t* report, size_t len);
|
||||
|
||||
/**
|
||||
* @param[in] device the HID device child device
|
||||
* @return true when a remote host is connected to this HID device
|
||||
*/
|
||||
bool (*is_connected)(struct Device* device);
|
||||
};
|
||||
|
||||
extern const struct DeviceType BLUETOOTH_HID_DEVICE_TYPE;
|
||||
|
||||
/** Find the first ready BLE HID device child device. Returns NULL if unavailable. */
|
||||
struct Device* bluetooth_hid_device_get_device(void);
|
||||
|
||||
error_t bluetooth_hid_device_start(struct Device* device, enum BtHidDeviceMode mode);
|
||||
error_t bluetooth_hid_device_stop(struct Device* device);
|
||||
error_t bluetooth_hid_device_send_key(struct Device* device, uint8_t keycode, bool pressed);
|
||||
error_t bluetooth_hid_device_send_keyboard(struct Device* device, const uint8_t* report, size_t len);
|
||||
error_t bluetooth_hid_device_send_consumer(struct Device* device, const uint8_t* report, size_t len);
|
||||
error_t bluetooth_hid_device_send_mouse(struct Device* device, const uint8_t* report, size_t len);
|
||||
error_t bluetooth_hid_device_send_gamepad(struct Device* device, const uint8_t* report, size_t len);
|
||||
bool bluetooth_hid_device_is_connected(struct Device* device);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tactility/error.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Device;
|
||||
struct DeviceType;
|
||||
|
||||
// ---- BLE MIDI child device ----
|
||||
|
||||
/**
|
||||
* BLE MIDI profile API (MIDI over Bluetooth Low Energy specification).
|
||||
* This API is exposed by a child device of the Bluetooth device.
|
||||
*/
|
||||
struct BtMidiApi {
|
||||
/**
|
||||
* Start advertising the BLE MIDI service and accept incoming connections.
|
||||
* @param[in] device the MIDI child device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*start)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Stop the BLE MIDI service and close any active connections.
|
||||
* @param[in] device the MIDI child device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*stop)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Send MIDI message bytes over the BLE MIDI connection.
|
||||
* @param[in] device the MIDI child device
|
||||
* @param[in] msg the raw MIDI bytes
|
||||
* @param[in] len the number of bytes
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*send)(struct Device* device, const uint8_t* msg, size_t len);
|
||||
|
||||
/**
|
||||
* @param[in] device the MIDI child device
|
||||
* @return true when a remote device is connected
|
||||
*/
|
||||
bool (*is_connected)(struct Device* device);
|
||||
};
|
||||
|
||||
extern const struct DeviceType BLUETOOTH_MIDI_TYPE;
|
||||
|
||||
/** Find the first ready BLE MIDI child device. Returns NULL if unavailable. */
|
||||
struct Device* bluetooth_midi_get_device(void);
|
||||
|
||||
error_t bluetooth_midi_start(struct Device* device);
|
||||
error_t bluetooth_midi_stop(struct Device* device);
|
||||
error_t bluetooth_midi_send(struct Device* device, const uint8_t* msg, size_t len);
|
||||
bool bluetooth_midi_is_connected(struct Device* device);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <tactility/error.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct Device;
|
||||
struct DeviceType;
|
||||
|
||||
// ---- BLE Serial (Nordic UART Service) child device ----
|
||||
|
||||
/**
|
||||
* BLE serial port profile API (Nordic UART Service or equivalent GATT-based SPP).
|
||||
* This API is exposed by a child device of the Bluetooth device.
|
||||
*/
|
||||
struct BtSerialApi {
|
||||
/**
|
||||
* Start advertising the BLE serial service and accept incoming connections.
|
||||
* @param[in] device the serial child device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*start)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Stop the BLE serial service and close any active connections.
|
||||
* @param[in] device the serial child device
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*stop)(struct Device* device);
|
||||
|
||||
/**
|
||||
* Write data over the BLE serial connection.
|
||||
* @param[in] device the serial child device
|
||||
* @param[in] data the data to send
|
||||
* @param[in] len the number of bytes to send
|
||||
* @param[out] written the number of bytes actually written
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*write)(struct Device* device, const uint8_t* data, size_t len, size_t* written);
|
||||
|
||||
/**
|
||||
* Read data from the BLE serial receive buffer.
|
||||
* @param[in] device the serial child device
|
||||
* @param[out] data the buffer to read into
|
||||
* @param[in] max_len the maximum number of bytes to read
|
||||
* @param[out] read_out the number of bytes actually read
|
||||
* @return ERROR_NONE on success
|
||||
*/
|
||||
error_t (*read)(struct Device* device, uint8_t* data, size_t max_len, size_t* read_out);
|
||||
|
||||
/**
|
||||
* @param[in] device the serial child device
|
||||
* @return true when a remote device is connected
|
||||
*/
|
||||
bool (*is_connected)(struct Device* device);
|
||||
};
|
||||
|
||||
extern const struct DeviceType BLUETOOTH_SERIAL_TYPE;
|
||||
|
||||
/** Find the first ready BLE serial child device. Returns NULL if unavailable. */
|
||||
struct Device* bluetooth_serial_get_device(void);
|
||||
|
||||
error_t bluetooth_serial_start(struct Device* device);
|
||||
error_t bluetooth_serial_stop(struct Device* device);
|
||||
error_t bluetooth_serial_write(struct Device* device, const uint8_t* data, size_t len, size_t* written);
|
||||
error_t bluetooth_serial_read(struct Device* device, uint8_t* data, size_t max_len, size_t* read_out);
|
||||
bool bluetooth_serial_is_connected(struct Device* device);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -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"
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user