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