Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
midi-init.c
Go to the documentation of this file.
1#ifdef ESP32
2#include <sdkconfig.h>
3#if CONFIG_BT_BLE_ENABLED
4
14#include "events.h"
15#include "midi-private.h"
16
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>
21
22#include <assert.h>
23
24const char *midi_ble_name = "Control Surface (BLE)";
25
26void set_midi_ble_name(const char *name) { midi_ble_name = name; }
27
28bool midi_init(void) {
29 if (!btStarted() && !btStart())
30 return false;
31
32 esp_err_t ret;
33
34 esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
35 if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
36 ret = esp_bluedroid_init();
37 if (ret != ESP_OK) {
38 ESP_LOGE("MIDIBLE", "Init bluetooth failed: %s",
39 esp_err_to_name(ret));
40 return false;
41 }
42 }
43
44 if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
45 ret = esp_bluedroid_enable();
46 if (ret != ESP_OK) {
47 ESP_LOGE("MIDIBLE", "Enable bluetooth failed: %s",
48 esp_err_to_name(ret));
49 return false;
50 }
51 }
52
53 ret = esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE);
54 if (ret != ESP_OK) {
55 ESP_LOGE("MIDIBLE", "set local MTU failed: %s", esp_err_to_name(ret));
56 return false;
57 }
58
59 ret = esp_ble_gap_set_device_name(midi_ble_name);
60 if (ret != ESP_OK) {
61 ESP_LOGE("MIDIBLE", "set device name failed: %s", esp_err_to_name(ret));
62 return false;
63 }
64
65 ret = esp_ble_gap_register_callback(gap_event_handler);
66 if (ret != ESP_OK) {
67 ESP_LOGE("MIDIBLE", "gap register error: %s", esp_err_to_name(ret));
68 return false;
69 }
70
71 ret = esp_ble_gatts_register_callback(gatts_event_handler);
72 if (ret != ESP_OK) {
73 ESP_LOGE("MIDIBLE", "GATTS register error: %s", esp_err_to_name(ret));
74 return false;
75 }
76
77 ret = esp_ble_gatts_app_register(midi_get_app_id());
78 if (ret != ESP_OK) {
79 ESP_LOGE("MIDIBLE", "GATTS app register error: %s",
80 esp_err_to_name(ret));
81 return false;
82 }
83
84 // Authentication and security
85
86 // Bonding with peer device after authentication
87 esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
88 // Set the IO capability to No output No input
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;
95
96 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
97 sizeof(auth_req));
98 if (ret != ESP_OK) {
99 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
100 esp_err_to_name(ret));
101 return false;
102 }
103
104 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
105 sizeof(iocap));
106 if (ret != ESP_OK) {
107 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
108 esp_err_to_name(ret));
109 return false;
110 }
111
112 ret = esp_ble_gap_set_security_param(
113 ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
114 sizeof(auth_option));
115 if (ret != ESP_OK) {
116 ESP_LOGE("MIDIBLE",
117 "Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
118 esp_err_to_name(ret));
119 return false;
120 }
121
122 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size,
123 sizeof(key_size));
124 if (ret != ESP_OK) {
125 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_MAX_KEY_SIZE: %s",
126 esp_err_to_name(ret));
127 return false;
128 }
129
130 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support,
131 sizeof(oob_support));
132 if (ret != ESP_OK) {
133 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_OOB_SUPPORT: %s",
134 esp_err_to_name(ret));
135 return false;
136 }
137
138 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key,
139 sizeof(init_key));
140 if (ret != ESP_OK) {
141 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_SET_INIT_KEY: %s",
142 esp_err_to_name(ret));
143 return false;
144 }
145
146 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key,
147 sizeof(rsp_key));
148 if (ret != ESP_OK) {
149 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_SET_RSP_KEY: %s",
150 esp_err_to_name(ret));
151 return false;
152 }
153
154 vTaskDelay(100 / portTICK_PERIOD_MS);
155
156 return true;
157}
158
159bool midi_deinit(void) { assert(!"Not implemented"); }
160
161#endif
162#endif
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().