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_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
92 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
93 sizeof(auth_req));
94 if (ret != ESP_OK) {
95 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
96 esp_err_to_name(ret));
97 return false;
98 }
99
100 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
101 sizeof(iocap));
102 if (ret != ESP_OK) {
103 ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
104 esp_err_to_name(ret));
105 return false;
106 }
107
108 ret = esp_ble_gap_set_security_param(
109 ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
110 sizeof(auth_option));
111 if (ret != ESP_OK) {
112 ESP_LOGE("MIDIBLE",
113 "Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
114 esp_err_to_name(ret));
115 return false;
116 }
117
118 vTaskDelay(100 / portTICK_PERIOD_MS);
119
120 return true;
121}
122
123bool midi_deinit(void) { assert(!"Not implemented"); }
124
125#endif
126#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().