Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
advertising.cpp
Go to the documentation of this file.
1#if defined(ARDUINO_RASPBERRY_PI_PICO_W) && ENABLE_BLE
2
3#include <btstack.h>
4
5#include "advertising.hpp"
6
7#include <cstring>
8
9namespace cs::midi_ble_btstack {
10
11namespace {
12
13uint8_t adv_data[] {
14 // Flags general discoverable
15 0x02, BLUETOOTH_DATA_TYPE_FLAGS, 0x06,
16 // Connection interval range
17 0x05, BLUETOOTH_DATA_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE, 0x0c, 0x00, 0x0c,
18 0x00,
19 // Service UUID
20 0x11, BLUETOOTH_DATA_TYPE_COMPLETE_LIST_OF_128_BIT_SERVICE_CLASS_UUIDS,
21 0x00, 0xc7, 0xc4, 0x4e, 0xe3, 0x6c, 0x51, 0xa7, 0x33, 0x4b, 0xe8, 0xed,
22 0x5a, 0x0e, 0xb8, 0x03};
23static_assert(sizeof(adv_data) <= LE_ADVERTISING_DATA_SIZE);
24uint8_t adv_rsp_data[LE_ADVERTISING_DATA_SIZE] {
25 // Name header
26 0x15, BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME,
27 // Name value
28 'C', 'o', 'n', 't', 'r', 'o', 'l', ' ', 'S', 'u', 'r', 'f', 'a', 'c', 'e',
29 ' ', 'M', 'I', 'D', 'I'};
30uint8_t adv_rsp_data_len() { return adv_rsp_data[0] + 1; }
31
32void set_adv_connection_interval(uint16_t min_itvl, uint16_t max_itvl) {
33 uint8_t *slave_itvl_range = adv_data + 5;
34 slave_itvl_range[0] = (min_itvl >> 0) & 0xFF;
35 slave_itvl_range[1] = (min_itvl >> 8) & 0xFF;
36 slave_itvl_range[2] = (max_itvl >> 0) & 0xFF;
37 slave_itvl_range[3] = (max_itvl >> 8) & 0xFF;
38}
39
40void set_adv_name(const char *name) {
41 auto len = std::min(std::strlen(name), sizeof(adv_rsp_data) - 2);
42 uint8_t *adv_name_len = adv_rsp_data;
43 uint8_t *adv_name = adv_rsp_data + 2;
44 std::memcpy(adv_name, name, len);
45 *adv_name_len = static_cast<uint8_t>(len + 1);
46}
47
48} // namespace
49
50void le_midi_setup_adv(const BLESettings &ble_settings) {
51 set_adv_name(ble_settings.device_name);
52 set_adv_connection_interval(ble_settings.connection_interval.minimum,
53 ble_settings.connection_interval.maximum);
54 uint16_t adv_int_min = 0x0020; // 20 ms (multiple of 0.625ms)
55 uint16_t adv_int_max = 0x0040; // 40 ms (multiple of 0.625ms)
56 uint8_t adv_type = 0;
57 bd_addr_t null_addr {};
58 uint8_t channel_map = 0x07; // All channels
59 uint8_t filter_policy = 0x00; // Allow any
60 gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0,
61 null_addr, channel_map, filter_policy);
62 gap_advertisements_set_data(sizeof(adv_data), adv_data);
63 gap_scan_response_set_data(adv_rsp_data_len(), adv_rsp_data);
64 gap_advertisements_enable(1);
65}
66
67} // namespace cs::midi_ble_btstack
68
69#endif
void le_midi_setup_adv(const BLESettings &ble_settings)
Configuration options for the low-level BLE code.
Definition BLEAPI.hpp:150
struct BLESettings::@4 connection_interval
Connection intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms).
uint16_t maximum
Definition BLEAPI.hpp:157
const char * device_name
Device name (used for advertising)
Definition BLEAPI.hpp:152
uint16_t minimum
Definition BLEAPI.hpp:156