Arduino A2DP
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 
24 typedef void (* bt_app_cb_t) (uint16_t event, void *param);
25 typedef int32_t (* music_data_cb_t) (uint8_t *data, int32_t len);
26 typedef int32_t (* music_data_channels_cb_t) (Frame *data, int32_t len);
27 typedef void (* bt_app_copy_cb_t) (app_msg_t *msg, void *p_dest, void *p_src);
28 
29 extern "C" void ccall_bt_av_hdl_stack_evt(uint16_t event, void *p_param);
30 extern "C" void ccall_bt_app_task_handler(void *arg);
31 extern "C" void ccall_bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
32 extern "C" void ccall_bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
33 extern "C" void ccall_a2d_app_heart_beat(void *arg) ;
34 extern "C" void ccall_bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
35 extern "C" void ccall_bt_app_av_sm_hdlr(uint16_t event, void *param);
36 extern "C" void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event, void *param) ;
37 extern "C" int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
38 extern "C" int32_t ccall_get_channel_data_wrapper(uint8_t *data, int32_t len) ;
39 extern "C" int32_t ccall_get_data_default(uint8_t *data, int32_t len) ;
40 
41 
49  friend void ccall_bt_av_hdl_stack_evt(uint16_t event, void *p_param);
50  friend void ccall_bt_app_task_handler(void *arg);
51  friend void ccall_bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
52  friend void ccall_bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
53  friend void ccall_a2d_app_heart_beat(void *arg) ;
54  friend void ccall_bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
55  friend void ccall_bt_app_av_sm_hdlr(uint16_t event, void *param);
56  friend void ccall_bt_av_hdl_avrc_ct_evt(uint16_t event, void *param) ;
57  friend int32_t ccall_bt_app_a2d_data_cb(uint8_t *data, int32_t len);
58  friend int32_t ccall_get_channel_data_wrapper(uint8_t *data, int32_t len) ;
59  friend int32_t ccall_get_data_default(uint8_t *data, int32_t len) ;
60 
61 
62  public:
65 
68 
70  void set_ssp_enabled(bool active){
71  this->ssp_enabled = active;
72  }
73 
75  void set_auto_reconnect(bool active){
76  this->auto_reconnect = active;
77  }
78 
80  void set_auto_reconnect(esp_bd_addr_t addr){
81  this->auto_reconnect = true;
82  memcpy(last_connection,addr,ESP_BD_ADDR_LEN);
83  }
84 
86  virtual void set_local_name(const char* name){
87  bt_name = name;
88  }
89 
96  virtual void start(const char* name, music_data_channels_cb_t callback = NULL);
97 
99  virtual void start(std::vector<const char*> names, music_data_channels_cb_t callback = NULL);
100 
110  virtual void start_raw(const char* name, music_data_cb_t callback = NULL);
111 
113  virtual void start_raw(std::vector<const char*> names, music_data_cb_t callback = NULL);
114 
115 
117  virtual void set_pin_code(const char* pin_code, esp_bt_pin_type_t pin_type=ESP_BT_PIN_TYPE_VARIABLE);
118 
124  virtual bool write_data(SoundData *data);
125 
127  virtual bool is_connected();
128 
132  virtual bool has_sound_data();
133 
137  virtual void set_nvs_init(bool doInit);
138 
142  virtual void set_reset_ble(bool doInit);
143 
145  virtual void set_volume(uint8_t volume){
146  ESP_LOGI(BT_AV_TAG, "set_volume: %d", volume);
147  volume_value = volume;
148  is_volume_used = true;
149  }
150 
152  virtual int get_volume(){
153  return is_volume_used ? volume_value : 0;
154  }
155 
157  virtual int32_t get_data_default(uint8_t *data, int32_t len);
158 
159 
160  protected:
161  music_data_channels_cb_t data_stream_channels_callback;
162  const char *dev_name = "ESP32_A2DP_SRC";
163 
164  bool ssp_enabled=false;
165  const char* bt_name;
166  std::vector<const char*> bt_names;
167 
168  esp_bt_pin_type_t pin_type;
169  esp_bt_pin_code_t pin_code;
170  uint32_t pin_code_len;
171 
172  esp_bd_addr_t s_peer_bda;
173  uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
174  int s_a2d_state; // Next Target Connection State
175  int s_media_state;
176  int s_intv_cnt=0;
177  int s_connecting_intv;
178  uint32_t s_pkt_cnt;
179  TimerHandle_t s_tmr;
180  xQueueHandle s_bt_app_task_queue;
181  xTaskHandle s_bt_app_task_handle;
182  // support for raw data
183  SoundData *sound_data;
184  int32_t sound_data_current_pos;
185  bool hasSoundData;
186 
187  // initialization
188  bool nvs_init = true;
189  bool reset_ble = true;
190  music_data_cb_t data_stream_callback;
191 
192  // volume
193  uint8_t volume_value = 0;
194  bool is_volume_used = false;
195 
196 #ifdef CURRENT_ESP_IDF
197  esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
198 #endif
199 
200  virtual void process_user_state_callbacks(uint16_t event, void *param);
201 
202  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);
203  virtual void bt_app_task_start_up(void);
204  virtual void bt_app_task_shut_down(void);
205  virtual void bt_app_av_media_proc(uint16_t event, void *param);
206 
207  /* A2DP application state machine handler for each state */
208  virtual void bt_app_av_state_unconnected(uint16_t event, void *param);
209  virtual void bt_app_av_state_connecting(uint16_t event, void *param);
210  virtual void bt_app_av_state_connected(uint16_t event, void *param);
211  virtual void bt_app_av_state_disconnecting(uint16_t event, void *param);
212 
213 
214  virtual bool bt_app_send_msg(app_msg_t *msg);
215  virtual void bt_app_work_dispatched(app_msg_t *msg);
216 
217  virtual bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len);
218  virtual void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
219 
220  virtual const char* last_bda_nvs_name() {
221  return "src_bda";
222  }
223 
228  // handler for bluetooth stack enabled events
229  virtual void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
230  virtual void bt_app_task_handler(void *arg);
231  virtual void bt_app_gap_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
233  virtual void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
234  virtual void a2d_app_heart_beat(void *arg);
236  virtual void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
238  virtual void bt_app_av_sm_hdlr(uint16_t event, void *param);
240  virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param);
242  virtual void reset_last_connection();
243 
244 #ifdef CURRENT_ESP_IDF
245  void bt_av_notify_evt_handler(uint8_t event, esp_avrc_rn_param_t *param);
246  void bt_av_volume_changed(void);
247 #endif
248 
249 };
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:95
A2DP Bluetooth Source.
Definition: BluetoothA2DPSource.h:48
void set_auto_reconnect(esp_bd_addr_t addr)
automatically tries to reconnect to the indicated address
Definition: BluetoothA2DPSource.h:80
virtual bool write_data(SoundData *data)
write sound data: In some cases it is very difficult to use the callback function....
Definition: BluetoothA2DPSource.cpp:911
virtual void reset_last_connection()
resets the last connectioin so that we can reconnect
Definition: BluetoothA2DPSource.cpp:234
virtual void set_volume(uint8_t volume)
Sets the volume (range 0 - 255)
Definition: BluetoothA2DPSource.h:145
virtual void start(const char *name, music_data_channels_cb_t callback=NULL)
starts the bluetooth source
Definition: BluetoothA2DPSource.cpp:148
~BluetoothA2DPSource()
Destructor.
Definition: BluetoothA2DPSource.cpp:133
BluetoothA2DPSource()
Constructor.
Definition: BluetoothA2DPSource.cpp:110
virtual bool is_connected()
Returns true if the bluetooth device is connected.
Definition: BluetoothA2DPSource.cpp:137
virtual void set_reset_ble(bool doInit)
Defines if the BLE should be reset on start.
Definition: BluetoothA2DPSource.cpp:950
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:141
void set_ssp_enabled(bool active)
activate Secure Simple Pairing
Definition: BluetoothA2DPSource.h:70
virtual void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
Definition: BluetoothA2DPSource.cpp:511
void set_auto_reconnect(bool active)
activate / deactivate the automatic reconnection to the last address (per default this is on)
Definition: BluetoothA2DPSource.h:75
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:817
virtual int get_volume()
Determines the actual volume.
Definition: BluetoothA2DPSource.h:152
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:566
virtual int32_t get_data_default(uint8_t *data, int32_t len)
callback for data
Definition: BluetoothA2DPSource.cpp:918
virtual void set_nvs_init(bool doInit)
Defines if the Flash NVS should be reset on start.
Definition: BluetoothA2DPSource.cpp:946
virtual void set_local_name(const char *name)
Defines the local name.
Definition: BluetoothA2DPSource.h:86
virtual void bt_app_av_sm_hdlr(uint16_t event, void *param)
A2DP application state machine.
Definition: BluetoothA2DPSource.cpp:609
virtual bool has_sound_data()
Returns true if write_dataRaw has been called with any valid data.
Definition: BluetoothA2DPSource.cpp:907
virtual void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
avrc CT event handler
Definition: BluetoothA2DPSource.cpp:858
virtual void start_raw(const char *name, music_data_cb_t callback=NULL)
starts the bluetooth source
Definition: BluetoothA2DPSource.cpp:165
Sound data as byte stream. We support TwoChannelSoundData (uint16_t + uint16_t) and OneChannelSoundDa...
Definition: SoundData.h:67
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:75