3#if CONFIG_BT_BLE_ENABLED || CONFIG_BT_BLUEDROID_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>
25#if __has_include("esp32-hal-bt-mem.h")
26#include "esp32-hal-bt-mem.h"
29const char *midi_ble_name =
"Control Surface (BLE)";
34 ESP_LOGI(
"MIDIBLE",
"Ensure Bluetooth started");
35 if (!btStarted() && !btStart())
37 ESP_LOGI(
"MIDIBLE",
"Bluetooth started");
41 esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
42 if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
43 ESP_LOGI(
"MIDIBLE",
"Initializing Bluedroid");
44 ret = esp_bluedroid_init();
46 ESP_LOGE(
"MIDIBLE",
"Init bluetooth failed: %s",
47 esp_err_to_name(ret));
52 if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
53 ESP_LOGI(
"MIDIBLE",
"Enabling Bluedroid");
54 ret = esp_bluedroid_enable();
56 ESP_LOGE(
"MIDIBLE",
"Enable bluetooth failed: %s",
57 esp_err_to_name(ret));
62 ret = esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE);
64 ESP_LOGE(
"MIDIBLE",
"set local MTU failed: %s", esp_err_to_name(ret));
68 ret = esp_ble_gap_set_device_name(midi_ble_name);
70 ESP_LOGE(
"MIDIBLE",
"set device name failed: %s", esp_err_to_name(ret));
76 ESP_LOGE(
"MIDIBLE",
"gap register error: %s", esp_err_to_name(ret));
82 ESP_LOGE(
"MIDIBLE",
"GATTS register error: %s", esp_err_to_name(ret));
88 ESP_LOGE(
"MIDIBLE",
"GATTS app register error: %s",
89 esp_err_to_name(ret));
96 esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
98 esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
99 uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
100 uint8_t key_size = 16;
101 uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
102 uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
103 uint8_t oob_support = ESP_BLE_OOB_DISABLE;
105 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
108 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
109 esp_err_to_name(ret));
113 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
116 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
117 esp_err_to_name(ret));
121 ret = esp_ble_gap_set_security_param(
122 ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
123 sizeof(auth_option));
126 "Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
127 esp_err_to_name(ret));
131 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size,
134 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_MAX_KEY_SIZE: %s",
135 esp_err_to_name(ret));
139 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support,
140 sizeof(oob_support));
142 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_OOB_SUPPORT: %s",
143 esp_err_to_name(ret));
147 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key,
150 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_SET_INIT_KEY: %s",
151 esp_err_to_name(ret));
155 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key,
158 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_SET_RSP_KEY: %s",
159 esp_err_to_name(ret));
163 vTaskDelay(100 / portTICK_PERIOD_MS);
169 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().