32#if (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0))
33#define TIMER_ARG_TYPE void *
35#define TIMER_ARG_TYPE tmrTimerControl *
38typedef int32_t (*music_data_cb_t)(uint8_t *data, int32_t len);
39typedef int32_t (*music_data_frames_cb_t)(Frame *data, int32_t len);
40typedef void (*bt_app_copy_cb_t)(
bt_app_msg_t *msg,
void *p_dest,
void *p_src);
41typedef void (*bt_app_cb_t)(uint16_t event,
void *param);
43extern "C" void ccall_a2d_app_heart_beat(TIMER_ARG_TYPE arg);
44extern "C" void ccall_bt_app_av_sm_hdlr(uint16_t event,
void *param);
45extern "C" void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event,
void *param);
46extern "C" int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
54 APP_AV_STATE_DISCOVERING,
55 APP_AV_STATE_DISCOVERED,
56 APP_AV_STATE_UNCONNECTED,
57 APP_AV_STATE_CONNECTING,
58 APP_AV_STATE_CONNECTED,
59 APP_AV_STATE_DISCONNECTING,
62static const char *APP_AV_STATE_STR[] = {
63 "IDLE",
"DISCOVERING",
"DISCOVERED",
"UNCONNECTED",
64 "CONNECTING",
"CONNECTED",
"DISCONNECTING"};
74 friend void ccall_a2d_app_heart_beat(TIMER_ARG_TYPE arg);
75 friend void ccall_bt_app_av_sm_hdlr(uint16_t event,
void *param);
76 friend void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event,
void *param);
77 friend int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
92 this->reconnect_status = active ? AutoReconnect : NoReconnect;
98 memcpy(last_connection, addr, ESP_BD_ADDR_LEN);
112 get_data_in_frames_cb = cb;
121 get_next_stream_cb = next_stream;
128 std::vector<const char *> names;
133 virtual void start(
const char *name) {
134 std::vector<const char *> names = {name};
139 virtual void start(std::vector<const char *> names);
142 virtual void start(
const char *name, music_data_frames_cb_t callback) {
144 std::vector<const char *> names = {name};
149 virtual void start(music_data_frames_cb_t callback) {
155 virtual void start(std::vector<const char *> names,
156 music_data_frames_cb_t callback) {
162 virtual void start_raw(
const char *name, music_data_cb_t callback =
nullptr) {
169 music_data_cb_t callback =
nullptr) {
175 virtual void start_raw(music_data_cb_t callback =
nullptr) {
181 void set_pin_code(
const char *pin_code, esp_bt_pin_type_t pin_type);
190 ssid_callback = callback;
195 void (*callback)(esp_bt_gap_discovery_state_t discoveryMode)) {
196 discovery_mode_callback = callback;
207 this->valid_cod_services = filter;
213 passthru_command_callback = cb;
214 is_passthru_active = (cb !=
nullptr);
218 void end(
bool releaseMemory=
false)
override;
223 int32_t (*get_data_in_frames_cb)(Frame *data, int32_t len) =
nullptr;
225 Stream *p_stream =
nullptr;
226 Stream &(*get_next_stream_cb)() =
nullptr;
228 const char *dev_name =
"ESP32_A2DP_SRC";
229 bool ssp_enabled =
false;
230 std::vector<const char *> bt_names;
231 esp_bt_pin_type_t pin_type;
232 esp_bt_pin_code_t pin_code;
233 uint32_t pin_code_len;
234 uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
238 int s_media_state = 0;
240 int s_connecting_heatbeat_count;
245 bool reset_ble =
false;
246 bool discovery_active =
false;
247 uint16_t valid_cod_services = ESP_BT_COD_SRVC_RENDERING |
248 ESP_BT_COD_SRVC_AUDIO |
249 ESP_BT_COD_SRVC_TELEPHONY;
251 bool (*ssid_callback)(
const char *ssid,
esp_bd_addr_t address,
253 void (*discovery_mode_callback)(esp_bt_gap_discovery_state_t discoveryMode) =
255 void (*passthru_command_callback)(uint8_t, bool) =
nullptr;
256 bool is_passthru_active =
false;
259#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
260 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
263 void app_gap_callback(esp_bt_gap_cb_event_t event,
264 esp_bt_gap_cb_param_t *param)
override;
266 esp_avrc_ct_cb_param_t *param)
override;
268 esp_a2d_cb_param_t *param)
override;
269 void av_hdl_stack_evt(uint16_t event,
void *p_param)
override;
277 virtual void process_user_state_callbacks(uint16_t event,
void *param);
279 virtual bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event,
280 void *p_params,
int param_len,
281 bt_app_copy_cb_t p_copy_cback);
282 virtual void bt_app_av_media_proc(uint16_t event,
void *param);
287 virtual void bt_app_av_state_connected_hdlr(uint16_t event,
void *param);
288 virtual void bt_app_av_state_disconnecting_hdlr(uint16_t event,
void *param);
290 virtual bool get_name_from_eir(uint8_t *eir, uint8_t *bdname,
291 uint8_t *bdname_len);
292 virtual void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
294 virtual const char *last_bda_nvs_name() {
return "src_bda"; }
296 virtual void a2d_app_heart_beat(
void *arg);
308 ESP_LOGI(BT_AV_TAG,
"==> a2dp connecting to: %s",
to_str(peer));
310 s_a2d_state = APP_AV_STATE_CONNECTING;
311 return esp_a2d_source_connect(peer);
315 ESP_LOGI(BT_AV_TAG,
"==> a2dp esp_a2d_source_disconnect from: %s",
317 esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
318 return esp_a2d_source_disconnect(peer);
322 const char *
to_state_str(
int state) {
return APP_AV_STATE_STR[state]; }
324 void set_scan_mode_connectable_default()
override {
328#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
329 virtual void bt_av_notify_evt_handler(uint8_t event, esp_avrc_rn_param_t *param);
330 virtual void bt_av_volume_changed(
void);
331 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event,
332 esp_avrc_tg_cb_param_t *param)
override;
333 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param)
override;
334 virtual bool isSource() {
return true;}
Common Bluetooth A2DP functions.
Definition BluetoothA2DPCommon.h:173
const char * to_str(esp_a2d_connection_state_t state)
converts esp_a2d_connection_state_t to a string
Definition BluetoothA2DPCommon.cpp:373
virtual void set_scan_mode_connectable(bool connectable)
Defines if the bluetooth is connectable.
Definition BluetoothA2DPCommon.cpp:552
A2DP Bluetooth Source.
Definition BluetoothA2DPSource.h:73
virtual void bt_app_av_state_unconnected_hdlr(uint16_t event, void *param)
A2DP application state machine handler for each state.
Definition BluetoothA2DPSource.cpp:617
virtual int32_t get_audio_data_volume(uint8_t *data, int32_t len)
provides the audio after applying the volume
Definition BluetoothA2DPSource.cpp:161
virtual void start_raw(std::vector< const char * > names, music_data_cb_t callback=nullptr)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:168
void set_pin_code(const char *pin_code, esp_bt_pin_type_t pin_type)
Defines the pin code. If nothing is defined we use "1234".
Definition BluetoothA2DPSource.cpp:82
virtual void reset_last_connection()
resets the last connectioin so that we can reconnect
Definition BluetoothA2DPSource.cpp:193
virtual void start(std::vector< const char * > names, music_data_frames_cb_t callback)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:155
void app_rc_ct_callback(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param) override
callback function for AVRCP controller
Definition BluetoothA2DPSource.cpp:856
virtual void set_valid_cod_service(uint16_t filter)
Definition BluetoothA2DPSource.h:206
virtual int32_t get_audio_data(uint8_t *data, int32_t len)
provides the audio data to be sent
Definition BluetoothA2DPSource.cpp:168
virtual void start_raw(music_data_cb_t callback=nullptr)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:175
~BluetoothA2DPSource()
Destructor.
Definition BluetoothA2DPSource.cpp:80
const char * to_state_str(int state)
converts a APP_AV_STATE_ENUM to a string
Definition BluetoothA2DPSource.h:322
virtual bool is_valid_cod_service(uint32_t cod)
Definition BluetoothA2DPSource.cpp:269
BluetoothA2DPSource()
Constructor.
Definition BluetoothA2DPSource.cpp:61
virtual void set_reset_ble(bool doInit)
Defines if the BLE should be reset on start.
Definition BluetoothA2DPSource.cpp:1016
virtual void set_auto_reconnect(bool active)
Definition BluetoothA2DPSource.h:91
virtual void start(const char *name, music_data_frames_cb_t callback)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:142
virtual void set_ssid_callback(bool(*callback)(const char *ssid, esp_bd_addr_t address, int rrsi))
Define callback to be notified about the found ssids.
Definition BluetoothA2DPSource.h:187
virtual void set_avrc_passthru_command_callback(void(*cb)(uint8_t key, bool isReleased))
Define the handler fur button presses on the remote bluetooth speaker.
Definition BluetoothA2DPSource.h:211
virtual void set_discovery_mode_callback(void(*callback)(esp_bt_gap_discovery_state_t discoveryMode))
Define callback to be notified about bt discovery mode state changes.
Definition BluetoothA2DPSource.h:194
virtual void set_data_source(Stream &data)
Defines a single Arduino Stream (e.g. File) as audio source.
Definition BluetoothA2DPSource.h:118
virtual void set_data_source_callback(Stream &(*next_stream)())
Provide a callback which provides streams.
Definition BluetoothA2DPSource.h:120
virtual void set_ssp_enabled(bool active)
activate Secure Simple Pairing
Definition BluetoothA2DPSource.h:87
virtual void start()
Definition BluetoothA2DPSource.h:127
virtual void set_data_callback(int32_t(cb)(uint8_t *data, int32_t len))
Defines the data callback.
Definition BluetoothA2DPSource.h:105
void app_a2d_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param) override
callback function for A2DP source
Definition BluetoothA2DPSource.cpp:544
virtual void set_auto_reconnect(esp_bd_addr_t addr)
automatically tries to reconnect to the indicated address
Definition BluetoothA2DPSource.h:96
virtual void bt_app_av_state_connecting_hdlr(uint16_t event, void *param)
Definition BluetoothA2DPSource.cpp:657
int32_t(* get_data_cb)(uint8_t *data, int32_t len)
callback for data
Definition BluetoothA2DPSource.h:222
virtual void set_local_name(const char *name)
Defines the local name.
Definition BluetoothA2DPSource.h:102
virtual void start(music_data_frames_cb_t callback)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:149
virtual void bt_app_av_sm_hdlr(uint16_t event, void *param)
A2DP application state machine.
Definition BluetoothA2DPSource.cpp:589
virtual void set_data_callback_in_frames(int32_t(cb)(Frame *data, int32_t len))
Defines the data callback.
Definition BluetoothA2DPSource.h:110
virtual void start_raw(const char *name, music_data_cb_t callback=nullptr)
Obsolete: use the start w/o callback and set the callback separately.
Definition BluetoothA2DPSource.h:162
virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
avrc CT event handler
Definition BluetoothA2DPSource.cpp:913
void end(bool releaseMemory=false) override
Ends the processing and releases the resources.
Definition BluetoothA2DPSource.cpp:147
virtual bool is_discovery_active()
Definition BluetoothA2DPSource.h:201
virtual void start(const char *name)
Starts the A2DP source.
Definition BluetoothA2DPSource.h:133
APP_AV_STATE
Buetooth A2DP global state.
Definition BluetoothA2DPSource.h:52
uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN]
Bluetooth address.
Definition external_lists.h:107
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition BluetoothA2DPCommon.h:128