-
Notifications
You must be signed in to change notification settings - Fork 676
/
bluetooth_service.h
249 lines (222 loc) · 7.09 KB
/
bluetooth_service.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _BLUETOOTH_SERVICE_H_
#define _BLUETOOTH_SERVICE_H_
#include "freertos/event_groups.h"
#include "audio_error.h"
#include "audio_element.h"
#include "esp_peripherals.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "bt_keycontrol.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ESP_A2DP_SAMPLE_RATE 44100
/**
* brief Bluetooth service working mode
*/
typedef enum {
BLUETOOTH_A2DP_SINK, /*!< A2DP Bluetooth sink audio, ESP32 will receive audio data from other bluetooth devices */
BLUETOOTH_A2DP_SOURCE, /*!< A2DP Bluetooth source audio, ESP32 can send audio data to other bluetooth devices */
} bluetooth_service_mode_t;
/**
* brief Bluetooth service user callback
*/
typedef struct {
esp_a2d_cb_t user_a2d_cb; /*!< callback for a2dp */
esp_a2d_sink_data_cb_t user_a2d_sink_data_cb; /*!< callback for a2dp sink data */
esp_a2d_source_data_cb_t user_a2d_source_data_cb; /*!< callback for a2dp source data */
esp_avrc_ct_cb_t user_avrc_ct_cb; /*!< callback for avrc ct */
} bluetooth_service_user_cb_t;
/**
*brief Bluetooth service configuration
*/
typedef struct {
const char *device_name; /*!< Bluetooth local device name */
const char *remote_name; /*!< Bluetooth remote device name */
bluetooth_service_mode_t mode; /*!< Bluetooth working mode */
bluetooth_service_user_cb_t user_callback; /*!< Bluetooth user callback */
} bluetooth_service_cfg_t;
/**
*brief Bluetooth address length
*/
#define BLUETOOTH_ADDR_LEN 6
/**
*brief Bluetooth device address
*/
typedef uint8_t bluetooth_addr_t[BLUETOOTH_ADDR_LEN];
/**
* @brief Initialize and start the Bluetooth service. This function can only be called for one time,
* and `bluetooth_service_destroy` must be called after use.
*
* @param config The configuration
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t bluetooth_service_start(bluetooth_service_cfg_t *config);
/**
* @brief Create Bluetooth stream, it is valid when Bluetooth service has started.
* The returned audio stream compatible with existing audio streams and can be used with the Audio Pipeline
*
* @return The Audio Element handle
*/
audio_element_handle_t bluetooth_service_create_stream();
/**
* @brief Create Bluetooth peripheral, it is valid when Bluetooth service has started.
* The returned bluetooth peripheral compatible with existing peripherals and can be used with the ESP Peripherals
*
* @return The Peripheral handle
*/
esp_periph_handle_t bluetooth_service_create_periph();
/**
* @brief Send the AVRC passthrough command (PLAY) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_play(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (PAUSE) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_pause(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (STOP) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_stop(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (NEXT) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_next(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (PREV) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_prev(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (REWIND) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_rewind(esp_periph_handle_t periph);
/**
* @brief Send the AVRC passthrough command (FAST FORWARD) to the Bluetooth device
*
* @param[in] periph The periph
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t periph_bluetooth_fast_forward(esp_periph_handle_t periph);
/**
* @brief Start device discovery.
*
* @param[in] periph The periph
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_ERR_INVALID_ARG: if invalid parameters are provided
* - ESP_FAIL: others
*/
esp_err_t periph_bluetooth_discover(esp_periph_handle_t periph);
/**
* @brief Cancel device discovery.
*
* @param[in] periph The periph
*
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t periph_bluetooth_cancel_discover(esp_periph_handle_t periph);
/**
* @brief Connect remote Device.
*
* @param[in] periph The periph
* @param[in] remote_bda remote Bluetooth device address
* @return
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_STATE: if bluetooth stack is not yet enabled
* - ESP_FAIL: others
*/
esp_err_t periph_bluetooth_connect(esp_periph_handle_t periph, bluetooth_addr_t remote_bda);
/**
* @brief Destroy and cleanup bluetooth service, this function must be called after destroying
* the Bluetoth Stream and Bluetooth Peripheral created by `bluetooth_service_create_stream` and `bluetooth_service_create_periph`
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t bluetooth_service_destroy();
/**
* @brief Get a2dp sample rate.
*
* @return
* - sample rate
*/
int periph_bluetooth_get_a2dp_sample_rate();
#ifdef __cplusplus
}
#endif
#endif