Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_bt.h"
#include "bt_app_core.h"
#include "bt_app_av.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
local_device_name
<anonymous enum>
bda2str(uint8_t *, char *, size_t)
bt_app_dev_cb(esp_bt_dev_cb_event_t, esp_bt_dev_cb_param_t *)
bt_app_gap_cb(esp_bt_gap_cb_event_t, esp_bt_gap_cb_param_t *)
bt_av_hdl_stack_evt(uint16_t, void *)
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesa2dp_sink samplemain/main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <inttypes.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs.h" #include "nvs_flash.h" #include "esp_system.h" #include "esp_log.h" #include "esp_bt.h" #include "bt_app_core.h" #include "bt_app_av.h" #include "esp_bt_main.h" #include "esp_bt_device.h" #include "esp_gap_bt_api.h" #include "esp_a2dp_api.h" #include "esp_avrc_api.h"19 includes /* device name */ static const char local_device_name[] = CONFIG_EXAMPLE_LOCAL_DEVICE_NAME; /* event for stack up */ enum { BT_APP_EVT_STACK_UP = 0, }{ ... }; /******************************** * STATIC FUNCTION DECLARATIONS *******************************//* ... */ /* Device callback function */ static void bt_app_dev_cb(esp_bt_dev_cb_event_t event, esp_bt_dev_cb_param_t *param); /* GAP callback function */ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param); /* handler for bluetooth stack enabled events */ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param); /******************************* * STATIC FUNCTION DEFINITIONS ******************************//* ... */ static char *bda2str(uint8_t * bda, char *str, size_t size) { if (bda == NULL || str == NULL || size < 18) { return NULL; }{...} uint8_t *p = bda; sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], p[3], p[4], p[5]); return str; }{ ... } static void bt_app_dev_cb(esp_bt_dev_cb_event_t event, esp_bt_dev_cb_param_t *param) { switch (event) { case ESP_BT_DEV_NAME_RES_EVT: { if (param->name_res.status == ESP_BT_STATUS_SUCCESS) { ESP_LOGI(BT_AV_TAG, "Get local device name success: %s", param->name_res.name); }{...} else { ESP_LOGE(BT_AV_TAG, "Get local device name failed, status: %d", param->name_res.status); }{...} break; }{...} ... default: { ESP_LOGI(BT_AV_TAG, "event: %d", event); break; }{...} ... }{...} }{ ... } static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) { uint8_t *bda = NULL; switch (event) { /* when authentication completed, this event comes */ case ESP_BT_GAP_AUTH_CMPL_EVT: { if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name); ESP_LOG_BUFFER_HEX(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN); }{...} else { ESP_LOGE(BT_AV_TAG, "authentication failed, status: %d", param->auth_cmpl.stat); }{...} ESP_LOGI(BT_AV_TAG, "link key type of current link is: %d", param->auth_cmpl.lk_type); break; }{...} ... case ESP_BT_GAP_ENC_CHG_EVT: { char *str_enc[3] = {"OFF", "E0", "AES"}; bda = (uint8_t *)param->enc_chg.bda; ESP_LOGI(BT_AV_TAG, "Encryption mode to [%02x:%02x:%02x:%02x:%02x:%02x] changed to %s", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], str_enc[param->enc_chg.enc_mode]); break; }{...} #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true) /* when Security Simple Pairing user confirmation requested, this event comes */ case ESP_BT_GAP_CFM_REQ_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %"PRIu32, param->cfm_req.num_val); esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); break; /* when Security Simple Pairing passkey notified, this event comes */... case ESP_BT_GAP_KEY_NOTIF_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey: %"PRIu32, param->key_notif.passkey); break; /* when Security Simple Pairing passkey requested, this event comes */... case ESP_BT_GAP_KEY_REQ_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!"); break;/* ... */ #endif /* when GAP mode changed, this event comes */ case ESP_BT_GAP_MODE_CHG_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_MODE_CHG_EVT mode: %d, interval: %.2f ms", param->mode_chg.mode, param->mode_chg.interval * 0.625); break; /* when ACL connection completed, this event comes */... case ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT: bda = (uint8_t *)param->acl_conn_cmpl_stat.bda; ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT Connected to [%02x:%02x:%02x:%02x:%02x:%02x], status: 0x%x", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], param->acl_conn_cmpl_stat.stat); break; /* when ACL disconnection completed, this event comes */... case ESP_BT_GAP_ACL_DISCONN_CMPL_STAT_EVT: bda = (uint8_t *)param->acl_disconn_cmpl_stat.bda; ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_ACL_DISC_CMPL_STAT_EVT Disconnected from [%02x:%02x:%02x:%02x:%02x:%02x], reason: 0x%x", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], param->acl_disconn_cmpl_stat.reason); break; /* others */... default: { ESP_LOGI(BT_AV_TAG, "event: %d", event); break; }{...} ... }{...} }{ ... } static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) { ESP_LOGD(BT_AV_TAG, "%s event: %d", __func__, event); switch (event) { /* when do the stack up, this event comes */ case BT_APP_EVT_STACK_UP: { esp_bt_gap_set_device_name(local_device_name); esp_bt_dev_register_callback(bt_app_dev_cb); esp_bt_gap_register_callback(bt_app_gap_cb); assert(esp_avrc_ct_init() == ESP_OK); esp_avrc_ct_register_callback(bt_app_rc_ct_cb); assert(esp_avrc_tg_init() == ESP_OK); esp_avrc_tg_register_callback(bt_app_rc_tg_cb); esp_avrc_rn_evt_cap_mask_t evt_set = {0}; esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE); assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK); assert(esp_a2d_sink_init() == ESP_OK); esp_a2d_register_callback(&bt_app_a2d_cb); esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb); /* Get the default value of the delay value */ esp_a2d_sink_get_delay_value(); /* Get local device name */ esp_bt_gap_get_device_name(); /* set discoverable and connectable mode, wait to be connected */ esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); break; }{...} /* others */... default: ESP_LOGE(BT_AV_TAG, "%s unhandled event: %d", __func__, event); break;... }{...} }{ ... } /******************************* * MAIN ENTRY POINT ******************************//* ... */ void app_main(void) { char bda_str[18] = {0}; /* initialize NVS — it is used to store PHY calibration data */ esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); }{...} ESP_ERROR_CHECK(err); /* * This example only uses the functions of Classical Bluetooth. * So release the controller memory for Bluetooth Low Energy. *//* ... */ ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(err)); return; }{...} if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(err)); return; }{...} esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT(); #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == false) bluedroid_cfg.ssp_en = false; #endif if ((err = esp_bluedroid_init_with_cfg(&bluedroid_cfg)) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s", __func__, esp_err_to_name(err)); return; }{...} if ((err = esp_bluedroid_enable()) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s", __func__, esp_err_to_name(err)); return; }{...} #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true) /* set default parameters for Secure Simple Pairing */ esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO; esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));/* ... */ #endif /* set default parameters for Legacy Pairing (use fixed pin code 1234) */ esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED; esp_bt_pin_code_t pin_code; pin_code[0] = '1'; pin_code[1] = '2'; pin_code[2] = '3'; pin_code[3] = '4'; esp_bt_gap_set_pin(pin_type, 4, pin_code); ESP_LOGI(BT_AV_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str))); bt_app_task_start_up(); /* bluetooth device name, connection mode and profile set up */ bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.