Control Surface disable-pipes
MIDI Control Surface library for Arduino
midi-app.c
Go to the documentation of this file.
1#ifdef ESP32
2
11#include "advertising.h"
12#include "logging.h"
13#include "midi-private.h"
14
15#include <esp_gatt_defs.h>
16#include <stddef.h> // NULL
17#include <string.h> // memcpy
18
21static const uint8_t MIDI_SERVICE_UUID_128[16] = {
22 0x00, 0xc7, 0xc4, 0x4e, 0xe3, 0x6c, //
23 0x51, 0xa7, //
24 0x33, 0x4b, //
25 0xe8, 0xed, //
26 0x5a, 0x0e, 0xb8, 0x03, //
27};
28
31static const uint8_t MIDI_char_uuid128[16] = {
32 0xf3, 0x6b, 0x10, 0x9d, 0x66, 0xf2, //
33 0xa9, 0xa1, //
34 0x12, 0x41, //
35 0x68, 0x38, //
36 0xdb, 0xe5, 0x72, 0x77, //
37};
38
41const esp_gatt_char_prop_t MIDI_properties = ESP_GATT_CHAR_PROP_BIT_READ |
42 ESP_GATT_CHAR_PROP_BIT_WRITE_NR |
43 ESP_GATT_CHAR_PROP_BIT_NOTIFY;
44
46enum {
47 MIDI_SERVICE_INDEX,
48 MIDI_CHARACTERISTIC_DECLARATION_INDEX,
49 MIDI_CHARACTERISTIC_VALUE_INDEX,
50 MIDI_2902_DESCRIPTOR_INDEX,
51 MIDI_ATTRIBUTE_TABLE_SIZE,
52};
53
54// <?> What does this do?
55// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_gatts.html#_CPPv429esp_ble_gatts_create_attr_tabPK19esp_gatts_attr_db_t13esp_gatt_if_t7uint8_t7uint8_t
56static const uint8_t MIDI_SERVICE_INSTANCE_ID = 0;
57
58static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE;
59static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE;
60static const uint16_t character_client_config_uuid =
61 ESP_GATT_UUID_CHAR_CLIENT_CONFIG;
62
63/* Full Database Description - Used to add attributes into the database */
64static const esp_gatts_attr_db_t
65 midi_attribute_table[MIDI_ATTRIBUTE_TABLE_SIZE] = {
66 // Service Declaration
67 [MIDI_SERVICE_INDEX] =
68 {
69 .attr_control = {ESP_GATT_AUTO_RSP},
70 {
71 .uuid_length = sizeof(primary_service_uuid),
72 .uuid_p = (uint8_t *)&primary_service_uuid,
73 .perm = ESP_GATT_PERM_READ,
74 .max_length = sizeof(MIDI_SERVICE_UUID_128),
75 .length = sizeof(MIDI_SERVICE_UUID_128),
76 .value = (uint8_t *)MIDI_SERVICE_UUID_128,
77 },
78 },
79
80 // Characteristic Declaration
81 [MIDI_CHARACTERISTIC_DECLARATION_INDEX] =
82 {
83 .attr_control = {ESP_GATT_AUTO_RSP},
84 {
85 .uuid_length = sizeof(character_declaration_uuid),
86 .uuid_p = (uint8_t *)&character_declaration_uuid,
87 .perm = ESP_GATT_PERM_READ,
88 .max_length = sizeof(MIDI_properties),
89 .length = sizeof(MIDI_properties),
90 .value = (uint8_t *)&MIDI_properties,
91 },
92 },
93
94 // Characteristic Value
95 [MIDI_CHARACTERISTIC_VALUE_INDEX] =
96 {
97 .attr_control = {ESP_GATT_RSP_BY_APP},
98 {
99 .uuid_length = sizeof(MIDI_char_uuid128),
100 .uuid_p = (uint8_t *)&MIDI_char_uuid128,
101 .perm = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
102 .max_length = 0,
103 .length = 0,
104 .value = NULL,
105 },
106 },
107
108 // Client Characteristic Configuration Descriptor (0x2902)
109 [MIDI_2902_DESCRIPTOR_INDEX] =
110 {
111 .attr_control = {ESP_GATT_AUTO_RSP},
112 {
113 .uuid_length = sizeof(character_client_config_uuid),
114 .uuid_p = (uint8_t *)&character_client_config_uuid,
115 .perm = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE,
116 .max_length = 2,
117 .length = 0,
118 .value = NULL,
119 },
120 },
121};
122
123static uint16_t MIDI_handle_table[MIDI_ATTRIBUTE_TABLE_SIZE] = {};
124
125static uint16_t midi_gatts_if = ESP_GATT_IF_NONE;
126static uint16_t midi_conn_id = 0;
127
128void midi_register_interface(esp_gatt_if_t gatts_if) {
129 midi_gatts_if = gatts_if;
130}
131
132void midi_set_connection_id(uint16_t conn_id) { midi_conn_id = conn_id; }
133uint16_t midi_get_connection_id(void) { return midi_conn_id; }
134
135void midi_handle_register_app_event(esp_gatt_if_t gatts_if,
136 esp_ble_gatts_cb_param_t *param) {
137 // Set the Service UUID for advertisement
138 advertising_set_service_uuid(MIDI_SERVICE_UUID_128,
139 sizeof(MIDI_SERVICE_UUID_128));
140 // Configure the advertising data and start advertising when that's done
142
143 // Register the GATTS service table
144 esp_err_t ret = esp_ble_gatts_create_attr_tab(
145 midi_attribute_table, gatts_if, MIDI_ATTRIBUTE_TABLE_SIZE,
146 MIDI_SERVICE_INSTANCE_ID);
147 if (ret) {
148 ESP_LOGE("MIDIBLE", "Create attribute table failed, error code: %d",
149 ret);
150 }
151}
152
153void midi_handle_create_attribute_table_event(esp_gatt_if_t gatts_if,
154 esp_ble_gatts_cb_param_t *param) {
155 if (param->add_attr_tab.num_handle != MIDI_ATTRIBUTE_TABLE_SIZE) {
156 ESP_LOGE("MIDIBLE",
157 "Create attribute table error, num_handle (%d) "
158 "doesn't equal MIDI_ATTRIBUTE_TABLE_SIZE (%d)",
159 param->add_attr_tab.num_handle, MIDI_ATTRIBUTE_TABLE_SIZE);
160 return;
161 }
162 ESP_LOGI("MIDIBLE",
163 "Created attribute table successfully, number of "
164 "handles: %d\n",
165 param->add_attr_tab.num_handle);
166 // Save the handles to the created services, characteristics and
167 // desriptors
168 memcpy(MIDI_handle_table, param->add_attr_tab.handles,
169 sizeof(MIDI_handle_table));
170 // Start the MIDI service.
171 esp_ble_gatts_start_service(midi_get_service_handle());
172}
173
174uint16_t midi_get_service_handle(void) {
175 return MIDI_handle_table[MIDI_SERVICE_INDEX];
176}
177
178uint16_t midi_get_characteristic_handle(void) {
179 return MIDI_handle_table[MIDI_CHARACTERISTIC_VALUE_INDEX];
180}
181
182uint16_t midi_get_descriptor_handle(void) {
183 return MIDI_handle_table[MIDI_2902_DESCRIPTOR_INDEX];
184}
185
186uint16_t midi_get_app_id(void) { return 0x55; }
187uint16_t midi_get_gatts_if(void) { return midi_gatts_if; }
188
189#endif
Advertising the MIDI service for Bluetooth Low Energy.
void advertising_set_service_uuid(const uint8_t uuid[], uint16_t length)
Set the UUID of the service to be advertised.
bool advertising_config(void)
Configure the advertising data, register with the Bluetooth driver.
Declarations of internal functions for the MIDI over BLE system, used in the midi-*....
void midi_handle_register_app_event(esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void midi_register_interface(esp_gatt_if_t gatts_if)
void midi_set_connection_id(uint16_t conn_id)
uint16_t midi_get_app_id(void)
uint16_t midi_get_gatts_if(void)
void midi_handle_create_attribute_table_event(esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
uint16_t midi_get_service_handle(void)
uint16_t midi_get_descriptor_handle(void)
uint16_t midi_get_characteristic_handle(void)
uint16_t midi_get_connection_id(void)