30 #if (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0))
31 #define TIMER_ARG_TYPE void *
33 #define TIMER_ARG_TYPE tmrTimerControl *
36 typedef int32_t (*music_data_cb_t)(uint8_t *data, int32_t len);
37 typedef int32_t (*music_data_frames_cb_t)(Frame *data, int32_t len);
38 typedef void (*bt_app_copy_cb_t)(
bt_app_msg_t *msg,
void *p_dest,
void *p_src);
39 typedef void (*bt_app_cb_t)(uint16_t event,
void *param);
41 extern "C" void ccall_a2d_app_heart_beat(TIMER_ARG_TYPE arg);
42 extern "C" void ccall_bt_app_av_sm_hdlr(uint16_t event,
void *param);
43 extern "C" void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event,
void *param);
44 extern "C" int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
52 APP_AV_STATE_DISCOVERING,
53 APP_AV_STATE_DISCOVERED,
54 APP_AV_STATE_UNCONNECTED,
55 APP_AV_STATE_CONNECTING,
56 APP_AV_STATE_CONNECTED,
57 APP_AV_STATE_DISCONNECTING,
60 static const char *APP_AV_STATE_STR[] = {
61 "IDLE",
"DISCOVERING",
"DISCOVERED",
"UNCONNECTED",
62 "CONNECTING",
"CONNECTED",
"DISCONNECTING"};
72 friend void ccall_a2d_app_heart_beat(TIMER_ARG_TYPE arg);
73 friend void ccall_bt_app_av_sm_hdlr(uint16_t event,
void *param);
74 friend void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event,
void *param);
75 friend int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
90 this->reconnect_status = active ? AutoReconnect : NoReconnect;
96 memcpy(last_connection, addr, ESP_BD_ADDR_LEN);
110 get_data_in_frames_cb = cb;
119 get_next_stream_cb = next_stream;
126 std::vector<const char *> names;
131 virtual void start(
const char *name) {
132 std::vector<const char *> names = {name};
137 virtual void start(std::vector<const char *> names);
140 virtual void start(
const char *name, music_data_frames_cb_t callback) {
142 std::vector<const char *> names = {name};
147 virtual void start(music_data_frames_cb_t callback) {
153 virtual void start(std::vector<const char *> names,
154 music_data_frames_cb_t callback) {
160 virtual void start_raw(
const char *name, music_data_cb_t callback =
nullptr) {
167 music_data_cb_t callback =
nullptr) {
173 virtual void start_raw(music_data_cb_t callback =
nullptr) {
179 void set_pin_code(
const char *pin_code, esp_bt_pin_type_t pin_type);
188 ssid_callback = callback;
193 void (*callback)(esp_bt_gap_discovery_state_t discoveryMode)) {
194 discovery_mode_callback = callback;
205 this->valid_cod_services = filter;
211 passthru_command_callback = cb;
212 is_passthru_active = (cb !=
nullptr);
216 void end(
bool releaseMemory=
false)
override;
221 int32_t (*get_data_in_frames_cb)(Frame *data, int32_t len) =
nullptr;
223 Stream *p_stream =
nullptr;
224 Stream &(*get_next_stream_cb)() =
nullptr;
226 const char *dev_name =
"ESP32_A2DP_SRC";
227 bool ssp_enabled =
false;
228 std::vector<const char *> bt_names;
229 esp_bt_pin_type_t pin_type;
230 esp_bt_pin_code_t pin_code;
231 uint32_t pin_code_len;
232 uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
236 int s_media_state = 0;
238 int s_connecting_heatbeat_count;
243 bool reset_ble =
false;
244 bool discovery_active =
false;
245 uint16_t valid_cod_services = ESP_BT_COD_SRVC_RENDERING |
246 ESP_BT_COD_SRVC_AUDIO |
247 ESP_BT_COD_SRVC_TELEPHONY;
249 bool (*ssid_callback)(
const char *ssid,
esp_bd_addr_t address,
251 void (*discovery_mode_callback)(esp_bt_gap_discovery_state_t discoveryMode) =
253 void (*passthru_command_callback)(uint8_t, bool) =
nullptr;
254 bool is_passthru_active =
false;
256 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
257 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
260 void app_gap_callback(esp_bt_gap_cb_event_t event,
261 esp_bt_gap_cb_param_t *param)
override;
263 esp_avrc_ct_cb_param_t *param)
override;
265 esp_a2d_cb_param_t *param)
override;
266 void av_hdl_stack_evt(uint16_t event,
void *p_param)
override;
274 virtual void process_user_state_callbacks(uint16_t event,
void *param);
276 virtual bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event,
277 void *p_params,
int param_len,
278 bt_app_copy_cb_t p_copy_cback);
279 virtual void bt_app_av_media_proc(uint16_t event,
void *param);
284 virtual void bt_app_av_state_connected_hdlr(uint16_t event,
void *param);
285 virtual void bt_app_av_state_disconnecting_hdlr(uint16_t event,
void *param);
287 virtual bool get_name_from_eir(uint8_t *eir, uint8_t *bdname,
288 uint8_t *bdname_len);
289 virtual void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
291 virtual const char *last_bda_nvs_name() {
return "src_bda"; }
293 virtual void a2d_app_heart_beat(
void *arg);
305 ESP_LOGI(BT_AV_TAG,
"==> a2dp connecting to: %s",
to_str(peer));
307 s_a2d_state = APP_AV_STATE_CONNECTING;
308 return esp_a2d_source_connect(peer);
312 ESP_LOGI(BT_AV_TAG,
"==> a2dp esp_a2d_source_disconnect from: %s",
314 esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
315 return esp_a2d_source_disconnect(peer);
319 const char *
to_state_str(
int state) {
return APP_AV_STATE_STR[state]; }
321 void set_scan_mode_connectable_default()
override {
325 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
326 virtual void bt_av_notify_evt_handler(uint8_t event, esp_avrc_rn_param_t *param);
327 virtual void bt_av_volume_changed(
void);
328 virtual void app_rc_tg_callback(esp_avrc_tg_cb_event_t event,
329 esp_avrc_tg_cb_param_t *param)
override;
330 virtual void av_hdl_avrc_tg_evt(uint16_t event,
void *p_param)
override;
331 virtual bool isSource() {
return true;}
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:169
const char * to_str(esp_a2d_connection_state_t state)
converts esp_a2d_connection_state_t to a string
Definition: BluetoothA2DPCommon.cpp:364
virtual void set_scan_mode_connectable(bool connectable)
Defines if the bluetooth is connectable.
Definition: BluetoothA2DPCommon.cpp:537
A2DP Bluetooth Source.
Definition: BluetoothA2DPSource.h:71
virtual void bt_app_av_state_unconnected_hdlr(uint16_t event, void *param)
A2DP application state machine handler for each state.
Definition: BluetoothA2DPSource.cpp:604
virtual int32_t get_audio_data_volume(uint8_t *data, int32_t len)
provides the audio after applying the volume
Definition: BluetoothA2DPSource.cpp:151
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:166
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:80
virtual void reset_last_connection()
resets the last connectioin so that we can reconnect
Definition: BluetoothA2DPSource.cpp:183
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:153
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:843
virtual void set_valid_cod_service(uint16_t filter)
Definition: BluetoothA2DPSource.h:204
virtual int32_t get_audio_data(uint8_t *data, int32_t len)
provides the audio data to be sent
Definition: BluetoothA2DPSource.cpp:158
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:173
~BluetoothA2DPSource()
Destructor.
Definition: BluetoothA2DPSource.cpp:78
virtual bool is_valid_cod_service(uint32_t cod)
Definition: BluetoothA2DPSource.cpp:259
BluetoothA2DPSource()
Constructor.
Definition: BluetoothA2DPSource.cpp:59
virtual void set_reset_ble(bool doInit)
Defines if the BLE should be reset on start.
Definition: BluetoothA2DPSource.cpp:1003
virtual void set_auto_reconnect(bool active)
Definition: BluetoothA2DPSource.h:89
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:140
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:185
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:209
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:192
virtual void set_data_source(Stream &data)
Defines a single Arduino Stream (e.g. File) as audio source.
Definition: BluetoothA2DPSource.h:116
virtual void set_data_source_callback(Stream &(*next_stream)())
Provide a callback which provides streams.
Definition: BluetoothA2DPSource.h:118
virtual void set_ssp_enabled(bool active)
activate Secure Simple Pairing
Definition: BluetoothA2DPSource.h:85
virtual void start()
Definition: BluetoothA2DPSource.h:125
virtual void set_data_callback(int32_t(cb)(uint8_t *data, int32_t len))
Defines the data callback.
Definition: BluetoothA2DPSource.h:103
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:531
virtual void set_auto_reconnect(esp_bd_addr_t addr)
automatically tries to reconnect to the indicated address
Definition: BluetoothA2DPSource.h:94
virtual void bt_app_av_state_connecting_hdlr(uint16_t event, void *param)
Definition: BluetoothA2DPSource.cpp:644
int32_t(* get_data_cb)(uint8_t *data, int32_t len)
callback for data
Definition: BluetoothA2DPSource.h:220
const char * to_state_str(int state)
converts a APP_AV_STATE_ENUM to a string
Definition: BluetoothA2DPSource.h:319
virtual void set_local_name(const char *name)
Defines the local name.
Definition: BluetoothA2DPSource.h:100
virtual void start(music_data_frames_cb_t callback)
Obsolete: use the start w/o callback and set the callback separately.
Definition: BluetoothA2DPSource.h:147
virtual void bt_app_av_sm_hdlr(uint16_t event, void *param)
A2DP application state machine.
Definition: BluetoothA2DPSource.cpp:576
virtual void set_data_callback_in_frames(int32_t(cb)(Frame *data, int32_t len))
Defines the data callback.
Definition: BluetoothA2DPSource.h:108
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:160
virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
avrc CT event handler
Definition: BluetoothA2DPSource.cpp:900
void end(bool releaseMemory=false) override
Ends the processing and releases the resources.
Definition: BluetoothA2DPSource.cpp:141
virtual bool is_discovery_active()
Definition: BluetoothA2DPSource.h:199
virtual void start(const char *name)
Starts the A2DP source.
Definition: BluetoothA2DPSource.h:131
APP_AV_STATE
Buetooth A2DP global state.
Definition: BluetoothA2DPSource.h:50
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:124