1#if defined(ARDUINO_RASPBERRY_PI_PICO_W) && ENABLE_BLE
3#define BTSTACK_FILE__ "gatt_midi.cpp"
26constexpr uint16_t midi_char_value_handle =
28constexpr uint16_t midi_cccd_handle =
33btstack_packet_callback_registration_t hci_event_callback_registration;
34btstack_packet_callback_registration_t sm_event_callback_registration;
39void connection_handler(uint8_t *packet, [[maybe_unused]] uint16_t size) {
42 if (hci_subevent_le_connection_complete_get_status(packet) != 0)
44 uint16_t conn_handle =
45 hci_subevent_le_connection_complete_get_connection_handle(packet);
48 sm_request_pairing(conn_handle);
50 uint16_t conn_latency = 0;
51 uint16_t supervision_timeout = 400;
52 gap_request_connection_parameter_update(
59void connection_update_handler([[maybe_unused]] uint8_t *packet,
60 [[maybe_unused]] uint16_t size) {
62 "Connection update: status="
63 << hci_subevent_le_connection_update_complete_get_status(packet)
64 <<
", connection interval="
65 << hci_subevent_le_connection_update_complete_get_conn_interval(packet)
66 <<
", connection latency="
67 << hci_subevent_le_connection_update_complete_get_conn_latency(packet)
68 <<
", supervision timeout="
69 << hci_subevent_le_connection_update_complete_get_supervision_timeout(packet));
73void connection_param_req_handler([[maybe_unused]] uint8_t *packet,
74 [[maybe_unused]] uint16_t size) {
76 "Connection parameter request: interval min="
77 << hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet)
79 << hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet)
81 << hci_subevent_le_remote_connection_parameter_request_get_latency(packet)
83 << hci_subevent_le_remote_connection_parameter_request_get_timeout(packet));
87void le_packet_handler(uint8_t *packet, uint16_t size) {
88 uint8_t type = hci_event_le_meta_get_subevent_code(packet);
92 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
93 connection_handler(packet, size);
95 case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE:
96 connection_update_handler(packet, size);
98 case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST:
99 connection_param_req_handler(packet, size);
105void gattservice_handler(uint8_t *packet, [[maybe_unused]] uint16_t size) {
106 [[maybe_unused]] uint8_t type =
107 hci_event_gattservice_meta_get_subevent_code(packet);
109 << hex << type << dec <<
")");
112void disconnect_handler(uint8_t *packet, [[maybe_unused]] uint16_t size) {
114 uint16_t conn_handle =
115 hci_event_disconnection_complete_get_connection_handle(packet);
119void mtu_exchange_complete_handler(uint8_t *packet,
120 [[maybe_unused]] uint16_t size) {
122 uint16_t conn_handle = att_event_mtu_exchange_complete_get_handle(packet);
123 uint16_t mtu = att_event_mtu_exchange_complete_get_MTU(packet);
128void gatt_event_mtu_handler(uint8_t *packet, [[maybe_unused]] uint16_t size) {
130 uint16_t conn_handle = gatt_event_mtu_get_handle(packet);
131 uint16_t mtu = gatt_event_mtu_get_MTU(packet);
135void btstack_event_state_handler(uint8_t *packet,
136 [[maybe_unused]] uint16_t size) {
137 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING)
139 bd_addr_t local_addr;
140 gap_local_bd_addr(local_addr);
141 DEBUGREF(
"BTstack up and running on " << bd_addr_to_str(local_addr));
145void packet_handler(uint8_t packet_type, [[maybe_unused]] uint16_t channel,
146 uint8_t *packet, uint16_t size) {
147 if (packet_type != HCI_EVENT_PACKET)
149 auto type = hci_event_packet_get_type(packet);
153 case HCI_EVENT_LE_META: le_packet_handler(packet, size);
break;
154 case HCI_EVENT_GATTSERVICE_META:
155 gattservice_handler(packet, size);
157 case HCI_EVENT_DISCONNECTION_COMPLETE:
158 disconnect_handler(packet, size);
160 case SM_EVENT_JUST_WORKS_REQUEST:
161 sm_just_works_confirm(
162 sm_event_just_works_request_get_handle(packet));
164 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
165 mtu_exchange_complete_handler(packet, size);
168 case GATT_EVENT_MTU: gatt_event_mtu_handler(packet, size);
break;
169 case BTSTACK_EVENT_STATE:
170 btstack_event_state_handler(packet, size);
179uint16_t att_read_callback([[maybe_unused]] hci_con_handle_t connection_handle,
181 [[maybe_unused]] uint16_t offset,
182 [[maybe_unused]] uint8_t *buffer,
183 [[maybe_unused]] uint16_t buffer_size) {
184 if (att_handle == midi_char_value_handle)
189int midi_cccd_write(hci_con_handle_t conn_handle, uint8_t *buffer,
190 [[maybe_unused]] uint16_t buffer_size) {
192 bool notify = (little_endian_read_16(buffer, 0) &
193 GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) != 0;
200int midi_value_write(hci_con_handle_t conn_handle, uint8_t *buffer,
201 uint16_t buffer_size) {
204 auto data_gen = [data {data}]()
mutable {
return std::exchange(data, {}); };
212int att_write_callback(hci_con_handle_t conn_handle, uint16_t att_handle,
213 [[maybe_unused]] uint16_t transaction_mode,
214 uint16_t offset, uint8_t *buffer, uint16_t buffer_size) {
215 DEBUGREF(
"ATT write: handle=" << att_handle <<
", offset=" << offset
216 <<
", size=" << buffer_size);
218 if (transaction_mode != ATT_TRANSACTION_MODE_NONE)
219 return ATT_ERROR_REQUEST_NOT_SUPPORTED;
222 return ATT_ERROR_INVALID_OFFSET;
224 if (att_handle == midi_cccd_handle)
225 return midi_cccd_write(conn_handle, buffer, buffer_size);
227 else if (att_handle == midi_char_value_handle)
228 return midi_value_write(conn_handle, buffer, buffer_size);
232void le_midi_setup(
const BLESettings &ble_settings) {
236 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
237 sm_set_authentication_requirements(SM_AUTHREQ_SECURE_CONNECTION |
240 att_server_init(
profile_data, att_read_callback, att_write_callback);
244 hci_event_callback_registration.callback = &packet_handler;
245 hci_add_event_handler(&hci_event_callback_registration);
247 att_server_register_packet_handler(packet_handler);
249 sm_event_callback_registration.callback = &packet_handler;
250 sm_add_event_handler(&sm_event_callback_registration);
254btstack_context_callback_registration_t create_context_callback(F &f) {
255 btstack_context_callback_registration_t ret {};
256 ret.callback = +[](
void *context) { (*
static_cast<F *
>(context))(); };
264 cs::midi_ble_btstack::instance = &instance;
265 cs::midi_ble_btstack::settings = settings;
266 le_midi_setup(settings);
267 hci_power_control(HCI_POWER_ON);
276 [[maybe_unused]]
const auto t0 = micros();
281 volatile std::atomic_bool notify_done {
false};
285 DEBUGREF(
"notify " << micros() - t0);
288 DEBUGREF(
"notify done " << micros() - t0);
289 notify_done.store(
true, std::memory_order_release);
291 auto send_ctx = create_context_callback(send);
296 DEBUGREF(
"req send " << micros() - t0);
297 auto ret = att_server_request_to_send_notification(&send_ctx,
299 assert(ret == ERROR_CODE_SUCCESS);
301 auto run_ctx = create_context_callback(run);
304 DEBUGREF(
"req main thread " << micros() - t0);
305 btstack_run_loop_execute_on_main_thread(&run_ctx);
307 while (!notify_done.load(std::memory_order_acquire)) tight_loop_contents();
308 DEBUGREF(
"all done " << micros() - t0);
Type definitions and callback interfaces for communication between the low-level BLE stacks and highe...
@ ConsumeImmediately
Buffer is valid only during the callback. Do not keep any pointers to it.
Callable that returns the next chunk of data from a BLE packet when called.
Defines the interface for callback functions registered by the low-level BLE code.
virtual void handleData(BLEConnectionHandle conn_handle, BLEDataGenerator &&data, BLEDataLifetime lifetime)=0
Called by the BLE stack when the central writes data to the MIDI GATT characteristic.
virtual void handleConnect(BLEConnectionHandle conn_handle)=0
Called by the BLE stack when a connection is established.
virtual void handleSubscribe(BLEConnectionHandle conn_handle, BLECharacteristicHandle char_handle, bool notify)=0
Called by the BLE stack when the central subscribes to receive notifications for the MIDI GATT charac...
virtual void handleDisconnect(BLEConnectionHandle conn_handle)=0
Called by the BLE stack when a connection is terminated.
virtual void handleMTU(BLEConnectionHandle conn_handle, uint16_t mtu)=0
Called by the BLE stack when the maximum transmission unit for the connection changes.
const uint8_t profile_data[]
#define ATT_CHARACTERISTIC_7772E5DB_3868_4112_A1A9_F2669D106BF3_01_CLIENT_CONFIGURATION_HANDLE
#define ATT_CHARACTERISTIC_7772E5DB_3868_4112_A1A9_F2669D106BF3_01_VALUE_HANDLE
#define DEBUGREF(x)
Print an expression and its location (file and line number) to the debug output if debugging is enabl...
static in_place_t in_place
bool init(MIDIBLEInstance &instance, BLESettings settings)
constexpr const char * gattservice_event_names[114]
void notify(BLEConnectionHandle conn_handle, BLECharacteristicHandle char_handle, BLEDataView data)
void le_midi_setup_adv(const BLESettings &ble_settings)
constexpr const char * hci_event_names[256]
constexpr const char * le_event_names[42]
Represents a handle to a local GATT characteristic.
Represents a handle to the connection to another device.
Non-owning, std::span-style read-only view of BLE data.
Configuration options for the low-level BLE code.
struct BLESettings::@4 connection_interval
Connection intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms).
bool initiate_security
Set to true if you want the Arduino to always initiate the Bluetooth bonding or secure connection.