Arduino A2DP
Loading...
Searching...
No Matches
BluetoothA2DPSource.h
1// C++ Class Implementation for a A2DP Source to be used as Arduino Library
2// The original ESP32 implementation can be found at https://github.com/espressif/esp-idf/tree/master/examples/bluetooth/bluedroid/classic_bt/a2dp_source
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// Copyright 2020 Phil Schatzmann
17// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
18
19#pragma once
20
21#include <vector>
22#include "BluetoothA2DPCommon.h"
23
24typedef void (* bt_app_cb_t) (uint16_t event, void *param);
25typedef int32_t (* music_data_cb_t) (uint8_t *data, int32_t len);
26typedef int32_t (* music_data_channels_cb_t) (Frame *data, int32_t len);
27typedef void (* bt_app_copy_cb_t) (app_msg_t *msg, void *p_dest, void *p_src);
28
29extern "C" void ccall_bt_av_hdl_stack_evt(uint16_t event, void *p_param);
30extern "C" void ccall_bt_app_task_handler(void *arg);
31extern "C" void ccall_bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
32extern "C" void ccall_bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
33extern "C" void ccall_a2d_app_heart_beat(void *arg) ;
34extern "C" void ccall_bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
35extern "C" void ccall_bt_app_av_sm_hdlr(uint16_t event, void *param);
36extern "C" void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event, void *param) ;
37extern "C" int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
38extern "C" int32_t ccall_get_channel_data_wrapper(uint8_t *data, int32_t len) ;
39extern "C" int32_t ccall_get_data_default(uint8_t *data, int32_t len) ;
40
41
50 friend void ccall_bt_av_hdl_stack_evt(uint16_t event, void *p_param);
51 friend void ccall_bt_app_task_handler(void *arg);
52 friend void ccall_bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
53 friend void ccall_bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
54 friend void ccall_a2d_app_heart_beat(void *arg) ;
55 friend void ccall_bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
56 friend void ccall_bt_app_av_sm_hdlr(uint16_t event, void *param);
57 friend void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event, void *param) ;
58 friend int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
59 friend int32_t ccall_get_channel_data_wrapper(uint8_t *data, int32_t len) ;
60 friend int32_t ccall_get_data_default(uint8_t *data, int32_t len) ;
61
62
63 public:
66
69
71 virtual void set_ssp_enabled(bool active){
72 this->ssp_enabled = active;
73 }
74
76 virtual void set_auto_reconnect(bool active){
77 this->reconnect_status = active ? AutoReconnect:NoReconnect;
78 }
79
81 virtual void set_auto_reconnect(esp_bd_addr_t addr){
83 memcpy(last_connection,addr,ESP_BD_ADDR_LEN);
84 }
85
87 virtual void set_local_name(const char* name){
88 dev_name = name;
89 }
90
96 virtual void start(const char* name, music_data_channels_cb_t callback = NULL);
97
99 virtual void start(music_data_channels_cb_t callback = NULL) {
100 std::vector<const char*> names; // empty vector
101 start(names, callback);
102 }
103
105 virtual void start(std::vector<const char*> names, music_data_channels_cb_t callback = NULL);
106
116 virtual void start_raw(const char* name, music_data_cb_t callback = NULL);
117
119 virtual void start_raw(music_data_cb_t callback = NULL) {
120 std::vector<const char*> names; // empty vector
121 start_raw(names, callback);
122 }
123
125 virtual void start_raw(std::vector<const char*> names, music_data_cb_t callback = NULL);
126
127
129 virtual void set_pin_code(const char* pin_code, esp_bt_pin_type_t pin_type=ESP_BT_PIN_TYPE_VARIABLE);
130
136 virtual bool write_data(SoundData *data);
137
139 virtual bool is_connected();
140
144 virtual bool has_sound_data();
145
149 virtual void set_nvs_init(bool doInit);
150
154 virtual void set_reset_ble(bool doInit);
155
157 virtual int32_t get_data_default(uint8_t *data, int32_t len);
158
160 void set_ssid_callback(bool(*callback)(const char*ssid, esp_bd_addr_t address, int rrsi)){
161 ssid_callback = callback;
162 }
163
164
165 protected:
166 music_data_channels_cb_t data_stream_channels_callback;
167 const char *dev_name = "ESP32_A2DP_SRC";
168
169 bool ssp_enabled=false;
170 bool is_connecting = false;
171 std::vector<const char*> bt_names;
172
173 esp_bt_pin_type_t pin_type;
174 esp_bt_pin_code_t pin_code;
175 uint32_t pin_code_len;
176
177 uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
178 int s_a2d_state=0; // Next Target Connection State
179 int s_media_state=0;
180 int s_intv_cnt=0;
181 int s_connecting_heatbeat_count;
182 uint32_t s_pkt_cnt;
183 TimerHandle_t s_tmr;
184 xQueueHandle s_bt_app_task_queue = nullptr;
185 xTaskHandle s_bt_app_task_handle = nullptr;
186 // support for raw data
187 SoundData *sound_data = nullptr;
188 int32_t sound_data_current_pos = 0;
189 bool has_sound_data_flag = false;
190
191 // initialization
192 bool nvs_init = true;
193 bool reset_ble = true;
194 music_data_cb_t data_stream_callback;
195
196 bool(*ssid_callback)(const char*ssid, esp_bd_addr_t address, int rrsi) = nullptr;
197
198#ifdef ESP_IDF_4
199 esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
200#endif
201
202 virtual void process_user_state_callbacks(uint16_t event, void *param);
203
204 virtual bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback);
205 virtual void bt_app_task_start_up(void);
206 virtual void bt_app_task_shut_down(void);
207 virtual void bt_app_av_media_proc(uint16_t event, void *param);
208
209 /* A2DP application state machine handler for each state */
210 virtual void bt_app_av_state_unconnected(uint16_t event, void *param);
211 virtual void bt_app_av_state_connecting(uint16_t event, void *param);
212 virtual void bt_app_av_state_connected(uint16_t event, void *param);
213 virtual void bt_app_av_state_disconnecting(uint16_t event, void *param);
214
215
216 virtual bool bt_app_send_msg(app_msg_t *msg);
217 virtual void bt_app_work_dispatched(app_msg_t *msg);
218
219 virtual bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len);
220 virtual void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
221
222 virtual const char* last_bda_nvs_name() {
223 return "src_bda";
224 }
225
230 // handler for bluetooth stack enabled events
231 virtual void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
232 virtual void bt_app_task_handler(void *arg);
233 virtual void bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
235 virtual void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
236 virtual void a2d_app_heart_beat(void *arg);
238 virtual void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
240 virtual void bt_app_av_sm_hdlr(uint16_t event, void *param);
242 virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param);
244 virtual void reset_last_connection();
245
246 virtual esp_err_t esp_a2d_connect(esp_bd_addr_t peer) {
247 return esp_a2d_source_connect(peer);
248 }
249
250#ifdef ESP_IDF_4
251 void bt_av_notify_evt_handler(uint8_t event, esp_avrc_rn_param_t *param);
252 void bt_av_volume_changed(void);
253#endif
254
255};
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:111
A2DP Bluetooth Source.
Definition: BluetoothA2DPSource.h:49
virtual bool write_data(SoundData *data)
write sound data: In some cases it is very difficult to use the callback function....
Definition: BluetoothA2DPSource.cpp:950
virtual void reset_last_connection()
resets the last connectioin so that we can reconnect
Definition: BluetoothA2DPSource.cpp:234
virtual void start(const char *name, music_data_channels_cb_t callback=NULL)
starts the bluetooth source
Definition: BluetoothA2DPSource.cpp:147
~BluetoothA2DPSource()
Destructor.
Definition: BluetoothA2DPSource.cpp:132
BluetoothA2DPSource()
Constructor.
Definition: BluetoothA2DPSource.cpp:110
virtual bool is_connected()
Returns true if the bluetooth device is connected.
Definition: BluetoothA2DPSource.cpp:136
virtual void set_reset_ble(bool doInit)
Defines if the BLE should be reset on start.
Definition: BluetoothA2DPSource.cpp:989
virtual void set_pin_code(const char *pin_code, esp_bt_pin_type_t pin_type=ESP_BT_PIN_TYPE_VARIABLE)
Defines the pin code. If nothing is defined we use "1234".
Definition: BluetoothA2DPSource.cpp:140
virtual void set_auto_reconnect(bool active)
activate / deactivate the automatic reconnection to the last address (per default this is on)
Definition: BluetoothA2DPSource.h:76
virtual void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
Definition: BluetoothA2DPSource.cpp:534
virtual void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
callback function for AVRCP controller
Definition: BluetoothA2DPSource.cpp:854
virtual void set_ssp_enabled(bool active)
activate Secure Simple Pairing
Definition: BluetoothA2DPSource.h:71
virtual void start(music_data_channels_cb_t callback=NULL)
Starts w/o indicating the name. Connections will be managed via set_ssid_callback()
Definition: BluetoothA2DPSource.h:99
virtual void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
callback function for A2DP source
Definition: BluetoothA2DPSource.cpp:587
virtual int32_t get_data_default(uint8_t *data, int32_t len)
callback for data
Definition: BluetoothA2DPSource.cpp:957
virtual void set_auto_reconnect(esp_bd_addr_t addr)
automatically tries to reconnect to the indicated address
Definition: BluetoothA2DPSource.h:81
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:160
virtual void set_nvs_init(bool doInit)
Defines if the Flash NVS should be reset on start.
Definition: BluetoothA2DPSource.cpp:985
virtual void set_local_name(const char *name)
Defines the local name.
Definition: BluetoothA2DPSource.h:87
virtual void bt_app_av_sm_hdlr(uint16_t event, void *param)
A2DP application state machine.
Definition: BluetoothA2DPSource.cpp:630
virtual bool has_sound_data()
Returns true if write_dataRaw has been called with any valid data.
Definition: BluetoothA2DPSource.cpp:946
virtual void start_raw(music_data_cb_t callback=NULL)
Starts w/o indicating the name. Connections will be managed via set_ssid_callback()
Definition: BluetoothA2DPSource.h:119
virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
avrc CT event handler
Definition: BluetoothA2DPSource.cpp:897
virtual void start_raw(const char *name, music_data_cb_t callback=NULL)
starts the bluetooth source
Definition: BluetoothA2DPSource.cpp:164
Sound data as byte stream. We support TwoChannelSoundData (uint16_t + uint16_t) and OneChannelSoundDa...
Definition: SoundData.h:71
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:83