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