34#if CONFIG_BT_NIMBLE_ENABLED
35 #include "nvs_flash.h"
37 #include "nimble/nimble_port.h"
38 #include "nimble/nimble_port_freertos.h"
39 #include "host/ble_hs.h"
40 #include "services/gap/ble_svc_gap.h"
41 #include "services/gatt/ble_svc_gatt.h"
49static const char * TAG =
"BLEMIDI";
51static BLEMIDI * s_instance =
nullptr;
54#if CONFIG_BT_NIMBLE_ENABLED
58static const ble_uuid128_t MIDI_SERVICE_UUID = BLE_UUID128_INIT(0x00, 0xC7, 0xC4, 0x4E, 0xE3, 0x6C, 0x51, 0xA7,
59 0x33, 0x4B, 0xE8, 0xED, 0x5A, 0x0E, 0xB8, 0x03);
62static const ble_uuid128_t MIDI_CHAR_UUID = BLE_UUID128_INIT(0xF3, 0x6B, 0x10, 0x9D, 0x66, 0xF2, 0xA9, 0xA1,
63 0x12, 0x41, 0x68, 0x38, 0xDB, 0xE5, 0x72, 0x77);
65static uint16_t s_charHandle;
66static uint8_t s_ownAddrType;
67static ble_gatt_chr_def s_chars[2];
68static ble_gatt_svc_def s_services[2];
71static void startAdvertising();
75static int characteristicAccess(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt * ctxt,
void * arg)
77 if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR && s_instance) {
80 if (ble_hs_mbuf_to_flat(ctxt->om, packet,
sizeof(packet), &len) == 0)
81 s_instance->packetReceived(packet, len);
87static int gapEvent(ble_gap_event * event,
void * arg)
89 switch (event->type) {
90 case BLE_GAP_EVENT_CONNECT:
91 if (event->connect.status == 0) {
92 ESP_LOGI(TAG,
"connected");
94 s_instance->setConnected(
true);
98 case BLE_GAP_EVENT_DISCONNECT:
99 ESP_LOGI(TAG,
"disconnected, reason 0x%03X", event->disconnect.reason);
101 s_instance->setConnected(
false);
104 case BLE_GAP_EVENT_ADV_COMPLETE:
115static void startAdvertising()
117 ble_hs_adv_fields fields = { };
118 fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
119 fields.uuids128 = (ble_uuid128_t *) &MIDI_SERVICE_UUID;
120 fields.num_uuids128 = 1;
121 fields.uuids128_is_complete = 1;
122 ble_gap_adv_set_fields(&fields);
124 ble_hs_adv_fields response = { };
125 const char * name = ble_svc_gap_device_name();
126 response.name = (uint8_t *) name;
127 response.name_len = strlen(name);
128 response.name_is_complete = 1;
129 ble_gap_adv_rsp_set_fields(&response);
131 ble_gap_adv_params params = { };
132 params.conn_mode = BLE_GAP_CONN_MODE_UND;
133 params.disc_mode = BLE_GAP_DISC_MODE_GEN;
134 int rc = ble_gap_adv_start(s_ownAddrType,
nullptr, BLE_HS_FOREVER, ¶ms, gapEvent,
nullptr);
135 if (rc != 0 && rc != BLE_HS_EALREADY)
136 ESP_LOGE(TAG,
"ble_gap_adv_start failed (%d)", rc);
140static void hostSynced()
142 ble_hs_id_infer_auto(0, &s_ownAddrType);
147static void hostTask(
void * arg)
150 nimble_port_freertos_deinit();
172 if (s_instance ==
this)
173 s_instance =
nullptr;
179 #if CONFIG_BT_NIMBLE_ENABLED
180 static bool released =
false;
182 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
191 #if CONFIG_BT_NIMBLE_ENABLED
194 ESP_LOGE(TAG,
"already started");
198 m_queue = xQueueCreate(queueSize,
sizeof(
MIDIMessage));
209 if (nimble_port_init() != ESP_OK) {
210 ESP_LOGE(TAG,
"nimble_port_init failed");
211 s_instance =
nullptr;
212 vQueueDelete(m_queue);
217 ble_hs_cfg.sync_cb = hostSynced;
222 memset(s_chars, 0,
sizeof(s_chars));
223 memset(s_services, 0,
sizeof(s_services));
224 s_chars[0].uuid = &MIDI_CHAR_UUID.u;
225 s_chars[0].access_cb = characteristicAccess;
226 s_chars[0].flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_NOTIFY;
227 s_chars[0].val_handle = &s_charHandle;
228 s_services[0].type = BLE_GATT_SVC_TYPE_PRIMARY;
229 s_services[0].uuid = &MIDI_SERVICE_UUID.u;
230 s_services[0].characteristics = s_chars;
231 ble_gatts_count_cfg(s_services);
232 ble_gatts_add_svcs(s_services);
234 ble_svc_gap_device_name_set(deviceName);
236 nimble_port_freertos_init(hostTask);
241 ESP_LOGE(TAG,
"NimBLE is not enabled in sdkconfig (CONFIG_BT_NIMBLE_ENABLED)");
250 return m_queue && xQueueReceive(m_queue, msg, 0) == pdTRUE;
254void BLEMIDI::putMessage(uint8_t status, uint8_t data1, uint8_t data2)
257 if (xQueueSendToBack(m_queue, &msg, 0) == pdTRUE)
264void BLEMIDI::setConnected(
bool value)
271 for (
int channel = 0; channel < 16; ++channel)
272 putMessage(0xB0 | channel, 123, 0);
278int BLEMIDI::dataBytes(uint8_t status)
280 switch (status & 0xF0) {
285 return status == 0xF2 ? 2 : (status == 0xF1 || status == 0xF3 ? 1 : 0);
296void BLEMIDI::packetReceived(uint8_t
const * data,
int len)
298 if (len < 2 || !(data[0] & 0x80))
301 int putCount = m_putCount;
331 m_runningStatus = b < 0xF0 ? b : 0;
332 int n = dataBytes(b);
339 if (i + n <= len && !(data[i] & 0x80) && (n == 1 || !(data[i + 1] & 0x80)))
340 putMessage(b, data[i], n == 2 ? data[i + 1] : 0);
341 while (i < len && !(data[i] & 0x80))
348 if (m_inSysEx || !m_runningStatus) {
352 int n = dataBytes(m_runningStatus);
353 if (i + n > len || (data[i] & 0x80) || (n == 2 && (data[i + 1] & 0x80))) {
355 while (i < len && !(data[i] & 0x80))
359 putMessage(m_runningStatus, data[i], n == 2 ? data[i + 1] : 0);
363 if (m_putCount != putCount)
This file contains the fabgl::BLEMIDI definition.
bool getMessage(MIDIMessage *msg)
Gets the next received message (non blocking).
static void releaseClassicBTMemory()
Releases the internal RAM reserved for classic Bluetooth (BR/EDR), about 15 KB.
bool begin(char const *deviceName, int queueSize=64)
Starts the Bluetooth stack and advertises the BLE-MIDI service.
MIDI input over Bluetooth Low Energy (BLE-MIDI peripheral).
A MIDI message: status byte and up to two data bytes.