-
Notifications
You must be signed in to change notification settings - Fork 141
/
midi-init.c
120 lines (98 loc) · 3.42 KB
/
midi-init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifdef ESP32
/**
* @file
*
* Initialization of the Bluetooth stack and registration of the MIDI BLE
* application.
*
* @todo Implement de-initialization, cleaning up everything we've configured.
*/
#include "events.h"
#include "midi-private.h"
#include <esp32-hal-bt.h>
#include <esp_bt_main.h>
#include <esp_gap_ble_api.h>
#include <esp_gatt_common_api.h>
#include <assert.h>
bool midi_init(void) {
if (!btStarted() && !btStart())
return false;
esp_err_t ret;
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
ret = esp_bluedroid_init();
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "Init bluetooth failed: %s",
esp_err_to_name(ret));
return false;
}
}
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
ret = esp_bluedroid_enable();
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "Enable bluetooth failed: %s",
esp_err_to_name(ret));
return false;
}
}
ret = esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE);
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "set local MTU failed: %s", esp_err_to_name(ret));
return false;
}
const char *name = "Control Surface (BLE)";
ret = esp_ble_gap_set_device_name(name);
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "set device name failed: %s", esp_err_to_name(ret));
return false;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "gap register error: %s", esp_err_to_name(ret));
return false;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "GATTS register error: %s", esp_err_to_name(ret));
return false;
}
ret = esp_ble_gatts_app_register(midi_get_app_id());
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "GATTS app register error: %s",
esp_err_to_name(ret));
return false;
}
// Authentication and security
// Bonding with peer device after authentication
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;
// Set the IO capability to No output No input
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
sizeof(auth_req));
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
esp_err_to_name(ret));
return false;
}
ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
sizeof(iocap));
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE", "Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
esp_err_to_name(ret));
return false;
}
ret = esp_ble_gap_set_security_param(
ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
sizeof(auth_option));
if (ret != ESP_OK) {
ESP_LOGE("MIDIBLE",
"Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
esp_err_to_name(ret));
return false;
}
vTaskDelay(100 / portTICK_PERIOD_MS);
return true;
}
bool midi_deinit(void) { assert(!"Not implemented"); }
#endif