3#if CONFIG_BT_BLE_ENABLED
17#include <esp32-hal-bt.h>
18#include <esp_bt_main.h>
19#include <esp_gap_ble_api.h>
20#include <esp_gatt_common_api.h>
24const char *midi_ble_name =
"Control Surface (BLE)";
29 if (!btStarted() && !btStart())
34 esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
35 if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
36 ret = esp_bluedroid_init();
38 ESP_LOGE(
"MIDIBLE",
"Init bluetooth failed: %s",
39 esp_err_to_name(ret));
44 if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
45 ret = esp_bluedroid_enable();
47 ESP_LOGE(
"MIDIBLE",
"Enable bluetooth failed: %s",
48 esp_err_to_name(ret));
53 ret = esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE);
55 ESP_LOGE(
"MIDIBLE",
"set local MTU failed: %s", esp_err_to_name(ret));
59 ret = esp_ble_gap_set_device_name(midi_ble_name);
61 ESP_LOGE(
"MIDIBLE",
"set device name failed: %s", esp_err_to_name(ret));
67 ESP_LOGE(
"MIDIBLE",
"gap register error: %s", esp_err_to_name(ret));
73 ESP_LOGE(
"MIDIBLE",
"GATTS register error: %s", esp_err_to_name(ret));
79 ESP_LOGE(
"MIDIBLE",
"GATTS app register error: %s",
80 esp_err_to_name(ret));
87 esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
89 esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
90 uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
91 uint8_t key_size = 16;
92 uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
93 uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
94 uint8_t oob_support = ESP_BLE_OOB_DISABLE;
96 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
99 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
100 esp_err_to_name(ret));
104 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
107 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
108 esp_err_to_name(ret));
112 ret = esp_ble_gap_set_security_param(
113 ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
114 sizeof(auth_option));
117 "Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
118 esp_err_to_name(ret));
122 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size,
125 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_MAX_KEY_SIZE: %s",
126 esp_err_to_name(ret));
130 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support,
131 sizeof(oob_support));
133 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_OOB_SUPPORT: %s",
134 esp_err_to_name(ret));
138 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key,
141 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_SET_INIT_KEY: %s",
142 esp_err_to_name(ret));
146 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key,
149 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_SET_RSP_KEY: %s",
150 esp_err_to_name(ret));
154 vTaskDelay(100 / portTICK_PERIOD_MS);
159bool midi_deinit(
void) { assert(!
"Not implemented"); }
Handlers for Bluetooth and BLE events.
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Declarations of internal functions for the MIDI over BLE system, used in the midi-*....
uint16_t midi_get_app_id(void)
bool midi_deinit()
Cleanup the MIDI BLE application and de-initialize the Bluetooth stack.
bool midi_init()
Initialize the Bluetooth stack and register the MIDI BLE application with the Bluedroid driver.
void set_midi_ble_name(const char *name)
Set the name of the BLE device. Must be set before calling midi_init().