Control Surface disable-pipes
MIDI Control Surface library for Arduino
advertising.c
Go to the documentation of this file.
1#ifdef ESP32
2
3#include "logging.h"
4#include <esp_gap_ble_api.h>
5#include <string.h>
6
7// https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf §36.4
8static esp_ble_adv_data_t adv_data = {
9 .set_scan_rsp = false,
10 .include_name = false,
11 .include_txpower = false,
12 // Intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms)
13 .min_interval = 0x000C,
14 .max_interval = 0x000C,
15 .appearance = 0x00,
16 .manufacturer_len = 0,
17 .p_manufacturer_data = NULL,
18 .service_data_len = 0,
19 .p_service_data = NULL,
20 // Service advertisement will be set later:
21 .service_uuid_len = 0,
22 .p_service_uuid = NULL,
23 .flag = ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT,
24};
25
26static esp_ble_adv_data_t adv_data_rsp = {
27 .set_scan_rsp = true,
28 .include_name = true,
29 .include_txpower = true,
30 // Intervals as multiples of 1.25 milliseconds (e.g.0x000C = 15 ms)
31 .min_interval = 0x000C,
32 .max_interval = 0x000C,
33 .appearance = 0x00,
34 .manufacturer_len = 0,
35 .p_manufacturer_data = NULL,
36 .service_data_len = 0,
37 .p_service_data = NULL,
38 // Service advertisement will be set later:
39 .service_uuid_len = 0,
40 .p_service_uuid = NULL,
41 .flag = ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT,
42};
43
44// https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf §36.5
45static esp_ble_adv_params_t adv_params = {
46 .adv_int_min = 0x20,
47 .adv_int_max = 0x40,
48 .adv_type = ADV_TYPE_IND,
49 .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
50 .peer_addr = {},
51 .peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
52 .channel_map = ADV_CHNL_ALL,
53 .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
54};
55
56uint8_t adv_config_done = 0;
57const uint8_t adv_config_flag = 1 << 0;
58const uint8_t scan_rsp_config_flag = 1 << 1;
59
60void advertising_set_service_uuid(const uint8_t uuid[], uint16_t length) {
61 ESP_LOGI("MIDIBLE", "advertising_set_service_uuid");
62 adv_data.p_service_uuid = (uint8_t *)uuid;
63 adv_data.service_uuid_len = length;
64}
65
66bool advertising_config(void) {
67 ESP_LOGI("MIDIBLE", "advertising_config");
68 esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data);
69 if (ret) {
70 ESP_LOGE("MIDIBLE", "config adv data failed, error code = %x", ret);
71 return false;
72 }
73 adv_config_done |= adv_config_flag;
74 ret = esp_ble_gap_config_adv_data(&adv_data_rsp);
75 if (ret) {
76 ESP_LOGE("MIDIBLE", "config adv rsp data failed, error code = %x", ret);
77 return false;
78 }
79 adv_config_done |= scan_rsp_config_flag;
80 return true;
81}
82
83bool advertising_handle_config_complete_event(esp_ble_gap_cb_param_t *param) {
84 if (param->adv_data_cmpl.status != ESP_BT_STATUS_SUCCESS) {
85 ESP_LOGE("MIDIBLE", "esp_ble_gap_config_adv_data failed: %d",
86 param->adv_data_cmpl.status);
87 return false;
88 }
89 // If this completes the config, start advertising (could be before or
90 // after the response config)
91 adv_config_done &= (~adv_config_flag);
92 if (adv_config_done == 0) {
93 esp_ble_gap_start_advertising(&adv_params);
94 }
95 return true;
96}
97
99 esp_ble_gap_cb_param_t *param) {
100 if (param->scan_rsp_data_cmpl.status != ESP_BT_STATUS_SUCCESS) {
101 ESP_LOGE("MIDIBLE", "esp_ble_gap_config_adv_data response failed: %d",
102 param->scan_rsp_data_cmpl.status);
103 return false;
104 }
105 // If this completes the config, start advertising (could be before or
106 // after the advertising config)
107 adv_config_done &= (~scan_rsp_config_flag);
108 if (adv_config_done == 0) {
109 esp_ble_gap_start_advertising(&adv_params);
110 }
111 return true;
112}
113
114#endif
void advertising_set_service_uuid(const uint8_t uuid[], uint16_t length)
Set the UUID of the service to be advertised.
bool advertising_handle_config_complete_event(esp_ble_gap_cb_param_t *param)
Callback that indicates that the configuration of the advertising data was complete.
bool advertising_config(void)
Configure the advertising data, register with the Bluetooth driver.
bool advertising_handle_config_response_complete_event(esp_ble_gap_cb_param_t *param)
Callback that indicates that the configuration of the advertising response data was complete.