28 #ifdef _I2S_H_INCLUDED
29 using namespace esp_i2s;
32 #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
33 defined(CONFIG_IDF_TARGET_ESP32C3) || \
34 defined(CONFIG_IDF_TARGET_ESP32C5) || \
35 defined(CONFIG_IDF_TARGET_ESP32C6) || \
36 defined(CONFIG_IDF_TARGET_ESP32S2) || \
37 defined(CONFIG_IDF_TARGET_ESP32S3) || \
38 defined(CONFIG_IDF_TARGET_ESP32H2) || \
39 defined(CONFIG_IDF_TARGET_ESP32P4)
40 #error "ESP32C3, ESP32S2, ESP32S3... do not support A2DP"
53 #include "esp_idf_version.h"
54 #include "freertos/FreeRTOS.h"
55 #include "freertos/FreeRTOSConfig.h"
56 #include "freertos/queue.h"
57 #include "freertos/task.h"
58 #include "freertos/timers.h"
59 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0)
60 #include "freertos/xtensa_api.h"
62 #include "xtensa_api.h"
64 #include "A2DPVolumeControl.h"
65 #include "esp_a2dp_api.h"
66 #include "esp_avrc_api.h"
68 #include "esp_bt_device.h"
69 #include "esp_bt_main.h"
70 #include "esp_gap_bt_api.h"
71 #include "esp_spp_api.h"
72 #include "esp_task_wdt.h"
73 #include "esp_timer.h"
75 #include "nvs_flash.h"
77 #ifdef ARDUINO_ARCH_ESP32
78 #include "esp32-hal-bt.h"
79 #include "esp32-hal-log.h"
84 #if !defined(ESP_IDF_VERSION)
85 #error Unsupported ESP32 Version: Upgrade the ESP32 version in the Board Manager
89 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0) && \
90 !defined(I2S_COMM_FORMAT_STAND_I2S)
92 #define I2S_COMM_FORMAT_STAND_I2S \
93 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
94 #define I2S_COMM_FORMAT_STAND_MSB \
95 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
96 #define I2S_COMM_FORMAT_STAND_PCM_LONG \
97 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
98 #define I2S_COMM_FORMAT_STAND_PCM_SHORT \
99 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_SHORT)
103 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
104 #define TaskHandle_t xTaskHandle
105 #define QueueHandle_t xQueueHandle
106 #define TickType_t portTickType
109 #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0)
110 #define esp_bt_gap_set_device_name esp_bt_dev_set_device_name
113 #define A2DP_DEPRECATED __attribute__((deprecated))
115 #define BT_APP_SIG_WORK_DISPATCH (0x01)
131 #define BT_AV_TAG "BT_AV"
132 #define BT_RC_CT_TAG "RCCT"
133 #define BT_APP_TAG "BT_API"
136 #define APP_RC_CT_TL_GET_CAPS (0)
137 #define APP_RC_CT_TL_GET_META_DATA (1)
138 #define APP_RC_CT_TL_RN_TRACK_CHANGE (2)
139 #define APP_RC_CT_TL_RN_PLAYBACK_CHANGE (3)
140 #define APP_RC_CT_TL_RN_PLAY_POS_CHANGE (4)
143 extern "C" void ccall_bt_app_task_handler(
void *arg);
144 extern "C" void ccall_app_gap_callback(esp_bt_gap_cb_event_t event,
145 esp_bt_gap_cb_param_t *param);
146 extern "C" void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event,
147 esp_avrc_ct_cb_param_t *param);
148 extern "C" void ccall_app_a2d_callback(esp_a2d_cb_event_t event,
149 esp_a2d_cb_param_t *param);
150 extern "C" void ccall_av_hdl_stack_evt(uint16_t event,
void *p_param);
152 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
153 extern "C" void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event,
154 esp_avrc_tg_cb_param_t *param);
155 extern "C" void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
171 friend void ccall_bt_app_task_handler(
void *arg);
172 friend void ccall_app_gap_callback(esp_bt_gap_cb_event_t event,
173 esp_bt_gap_cb_param_t *param);
174 friend void ccall_app_rc_ct_callback(esp_avrc_ct_cb_event_t event,
175 esp_avrc_ct_cb_param_t *param);
176 friend void ccall_app_a2d_callback(esp_a2d_cb_event_t event,
177 esp_a2d_cb_param_t *param);
178 friend void ccall_av_hdl_stack_evt(uint16_t event,
void *p_param);
180 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
182 friend void ccall_app_rc_tg_callback(esp_avrc_tg_cb_event_t event,
183 esp_avrc_tg_cb_param_t *param);
185 friend void ccall_av_hdl_avrc_tg_evt(uint16_t event,
void *p_param);
196 void set_auto_reconnect(
bool active);
199 virtual void disconnect();
202 virtual bool reconnect();
208 virtual void set_connected(
bool active);
211 virtual void end(
bool releaseMemory =
false);
220 volume_value = std::min((
int)volume, 0x7F);
221 ESP_LOGI(BT_AV_TAG,
"set_volume: %d", volume_value);
222 volume_control()->set_volume(volume_value);
223 volume_control()->set_enabled(
true);
224 is_volume_used =
true;
228 virtual int get_volume() {
return is_volume_used ? volume_value : 0; }
232 volume_control_ptr = ptr;
243 virtual void set_on_connection_state_changed(
245 void *obj =
nullptr);
249 virtual void set_on_audio_state_changed_post(
251 void *obj =
nullptr);
254 virtual void set_on_audio_state_changed(
256 void *obj =
nullptr);
260 virtual void debounce(
void (*cb)(
void),
int ms);
263 void log_free_heap();
274 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
295 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
302 set_scan_mode_connectable(connectable);
309 virtual void clean_last_connection();
315 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 1)
318 bluedroid_config = cfg;
322 void delay_ms(uint32_t millis);
324 unsigned long get_millis();
335 avrc_rn_events = events;
339 const char *bt_name = {0};
342 unsigned long reconnect_timout = 0;
343 unsigned int default_reconnect_timout = 10000;
344 bool is_autoreconnect_allowed =
false;
345 uint32_t debounce_ms = 0;
349 bool is_start_disabled =
false;
350 bool is_target_status_active =
true;
352 void *obj) =
nullptr;
354 void *obj) =
nullptr;
356 void *obj) =
nullptr;
357 void *connection_state_obj =
nullptr;
358 void *audio_state_obj =
nullptr;
359 void *audio_state_obj_post =
nullptr;
360 const char *m_a2d_conn_state_str[4] = {
"Disconnected",
"Connecting",
361 "Connected",
"Disconnecting"};
362 const char *m_a2d_audio_state_str[4] = {
"Suspended",
"Started",
"Suspended",
"Suspended"};
363 const char *m_avrc_playback_state_str[5] = {
"stopped",
"playing",
"paused",
364 "forward seek",
"reverse seek"};
368 UBaseType_t task_priority = configMAX_PRIORITIES - 10;
370 uint8_t volume_value = 0;
371 bool is_volume_used =
false;
372 BaseType_t task_core = 1;
374 int event_queue_size = 20;
375 int event_stack_size = 3072;
377 std::vector<esp_avrc_rn_event_ids_t> avrc_rn_events = {
378 ESP_AVRC_RN_VOLUME_CHANGE};
380 QueueHandle_t app_task_queue =
nullptr;
381 TaskHandle_t app_task_handle =
nullptr;
383 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 1)
384 esp_bluedroid_config_t bluedroid_config{.ssp_en =
true};
387 virtual void init_nvs();
389 virtual const char *last_bda_nvs_name() = 0;
390 virtual void get_last_connection();
392 virtual bool has_last_connection();
393 virtual bool read_address(
const char *name,
esp_bd_addr_t &bda);
394 virtual bool write_address(
const char *name,
esp_bd_addr_t bda);
397 virtual void set_scan_mode_connectable(
bool connectable);
398 virtual void set_scan_mode_connectable_default() = 0;
402 return volume_control_ptr !=
nullptr ? volume_control_ptr
403 : &default_volume_control;
406 virtual bool bt_start();
407 virtual esp_err_t bluedroid_init();
408 virtual esp_err_t esp_a2d_disconnect(
esp_bd_addr_t remote_bda) = 0;
409 virtual void app_task_start_up();
410 virtual void app_task_shut_down();
412 virtual void app_task_handler(
void *arg);
414 virtual bool isSource() = 0;
416 virtual void app_gap_callback(esp_bt_gap_cb_event_t event,
417 esp_bt_gap_cb_param_t *param) = 0;
420 esp_avrc_ct_cb_param_t *param) = 0;
423 esp_a2d_cb_param_t *param) = 0;
425 virtual void av_hdl_stack_evt(uint16_t event,
void *p_param) = 0;
427 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
429 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event,
430 esp_avrc_tg_cb_param_t *param) = 0;
431 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param) = 0;
void(* app_callback_t)(uint16_t event, void *param)
handler for the dispatched work
Definition: BluetoothA2DPCommon.h:120
Default implementation for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:105
Abstract class for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:45
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:169
void set_event_stack_size(int size)
Defines the stack size of the event task (in bytes)
Definition: BluetoothA2DPCommon.h:290
virtual void set_volume(uint8_t volume)
Sets the volume (range 0 - 127)
Definition: BluetoothA2DPCommon.h:219
virtual int get_volume()
Determines the actual volume.
Definition: BluetoothA2DPCommon.h:228
void set_bluedroid_config_t(esp_bluedroid_config_t cfg)
Defines the esp_bluedroid_config_t: Available from IDF 5.2.1.
Definition: BluetoothA2DPCommon.h:317
virtual void set_avrc_rn_events(std::vector< esp_avrc_rn_event_ids_t > events)
Definition: BluetoothA2DPCommon.h:334
virtual void set_default_bt_mode(esp_bt_mode_t mode)
Definition: BluetoothA2DPCommon.h:313
virtual ~BluetoothA2DPCommon()=default
Destructor.
virtual bool is_connected()
Checks if A2DP is connected.
Definition: BluetoothA2DPCommon.h:214
void set_task_priority(UBaseType_t priority)
defines the task priority (the default value is configMAX_PRIORITIES - 10)
Definition: BluetoothA2DPCommon.h:280
void set_task_core(BaseType_t core)
Definition: BluetoothA2DPCommon.h:284
virtual void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)=0
callback function for A2DP source
virtual A2DPVolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:401
virtual const char * get_name()
Provides the actual SSID name.
Definition: BluetoothA2DPCommon.h:306
virtual void set_volume_control(A2DPVolumeControl *ptr)
you can define a custom VolumeControl implementation
Definition: BluetoothA2DPCommon.h:231
virtual void set_connectable(bool connectable)
Bluetooth connectable.
Definition: BluetoothA2DPCommon.h:301
virtual esp_bd_addr_t * get_last_peer_address()
Provides the address of the last device.
Definition: BluetoothA2DPCommon.h:293
virtual void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)=0
callback function for AVRCP controller
void set_event_queue_size(int size)
Defines the queue size of the event task.
Definition: BluetoothA2DPCommon.h:287
ReconnectStatus
Buetooth A2DP Reconnect Status.
Definition: BluetoothA2DPCommon.h:162
esp_a2d_audio_state_t
Buetooth A2DP datapath states.
Definition: external_lists.h:5
esp_a2d_connection_state_t
Buetooth A2DP connection states.
Definition: external_lists.h:16
esp_bt_discovery_mode_t
AVRCP discovery mode.
Definition: external_lists.h:85
esp_avrc_playback_stat_t
AVRCP current status of playback.
Definition: external_lists.h:72
esp_bt_mode_t
Bluetooth Controller mode.
Definition: external_lists.h:96
uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]
Bluetooth address.
Definition: external_lists.h:107
@ ESP_A2D_AUDIO_STATE_STOPPED
Definition: external_lists.h:8
@ ESP_A2D_CONNECTION_STATE_CONNECTED
Definition: external_lists.h:19
@ ESP_A2D_CONNECTION_STATE_DISCONNECTED
Definition: external_lists.h:17
@ ESP_BT_GENERAL_DISCOVERABLE
Definition: external_lists.h:88
@ ESP_BT_MODE_CLASSIC_BT
Definition: external_lists.h:99
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:124
uint16_t sig
Definition: BluetoothA2DPCommon.h:125
app_callback_t cb
Definition: BluetoothA2DPCommon.h:127
uint16_t event
Definition: BluetoothA2DPCommon.h:126
void * param
Definition: BluetoothA2DPCommon.h:128