xref: /btstack/src/classic/avrcp.h (revision 6b7adbe5dd4b0af3c7d33d07ce150d2f4a3a18e0)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  * avrcp.h
40  *
41  * Audio/Video Remote Control Profile
42  *
43  */
44 
45 #ifndef __AVRCP_H
46 #define __AVRCP_H
47 
48 #include <stdint.h>
49 #include "btstack_run_loop.h"
50 #include "btstack_linked_list.h"
51 
52 #if defined __cplusplus
53 extern "C" {
54 #endif
55 
56 #define BT_SIG_COMPANY_ID 0x001958
57 /* API_START */
58 
59 typedef enum {
60     AVRCP_CAPABILITY_ID_COMPANY = 0x02,
61     AVRCP_CAPABILITY_ID_EVENT = 0x03
62 } avrcp_capability_id_t;
63 
64 typedef enum {
65     AVRCP_MEDIA_ATTR_TITLE = 1,
66     AVRCP_MEDIA_ATTR_ARTIST,
67     AVRCP_MEDIA_ATTR_ALBUM,
68     AVRCP_MEDIA_ATTR_TRACK,
69     AVRCP_MEDIA_ATTR_TOTAL_TRACKS,
70     AVRCP_MEDIA_ATTR_GENRE,
71     AVRCP_MEDIA_ATTR_SONG_LENGTH
72 } avrcp_media_attribute_id_t;
73 
74 #define AVRCP_MEDIA_ATTR_COUNT 7
75 
76 typedef enum {
77     AVRCP_PDU_ID_GET_CAPABILITIES = 0x10,
78     AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue = 0x13,
79     AVRCP_PDU_ID_SetPlayerApplicationSettingValue = 0x14,
80     AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,
81     AVRCP_PDU_ID_GET_PLAY_STATUS = 0x30,
82     AVRCP_PDU_ID_REGISTER_NOTIFICATION = 0x31,
83     AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50
84 } avrcp_pdu_id_t;
85 
86 typedef enum {
87     AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED = 0x01,            // Change in playback status of the current track.
88     AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED = 0x02,                      // Change of current track
89     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END = 0x03,                  // Reached end of a track
90     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START = 0x04,                // Reached start of a track
91     AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED = 0x05,               // Change in playback position. Returned after the specified playback notification change notification interval
92     AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED = 0x06,                // Change in battery status
93     AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED = 0x07,              // Change in system status
94     AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED = 0x08, // Change in player application setting
95     AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED = 0x09,        // The content of the Now Playing list has changed, see 6.9.5.
96     AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED = 0x0a,          // The available players have changed, see 6.9.4.
97     AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED = 0x0b,           // The Addressed Player has been changed, see 6.9.2.
98     AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED = 0x0c,                       // The UIDs have changed, see 6.10.3.3.
99     AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED = 0x0d                      // The volume has been changed locally on the TG, see 6.13.3.
100 } avrcp_notification_event_id_t;
101 
102 typedef enum {
103     AVRCP_CTYPE_CONTROL = 0,
104     AVRCP_CTYPE_STATUS,
105     AVRCP_CTYPE_SPECIFIC_INQUIRY,
106     AVRCP_CTYPE_NOTIFY,
107     AVRCP_CTYPE_GENERAL_INQUIRY,
108     AVRCP_CTYPE_RESERVED5,
109     AVRCP_CTYPE_RESERVED6,
110     AVRCP_CTYPE_RESERVED7,
111     AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED = 8,
112     AVRCP_CTYPE_RESPONSE_ACCEPTED,
113     AVRCP_CTYPE_RESPONSE_REJECTED,
114     AVRCP_CTYPE_RESPONSE_IN_TRANSITION, // target state is in transition. A subsequent STATUS command, may result in the return of a STABLE status
115     AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
116     AVRCP_CTYPE_RESPONSE_CHANGED_STABLE,
117     AVRCP_CTYPE_RESPONSE_RESERVED,
118     AVRCP_CTYPE_RESPONSE_INTERIM            // target is unable to respond with either ACCEPTED or REJECTED within 100 millisecond
119 } avrcp_command_type_t;
120 
121 // control command response: accepted, rejected, interim
122 // status command response: not implemented, rejected, in transiiton, stable
123 // notify command response: not implemented, rejected, changed
124 typedef enum {
125     AVRCP_SUBUNIT_TYPE_MONITOR = 0,
126     AVRCP_SUBUNIT_TYPE_AUDIO = 1,
127     AVRCP_SUBUNIT_TYPE_PRINTER,
128     AVRCP_SUBUNIT_TYPE_DISC,
129     AVRCP_SUBUNIT_TYPE_TAPE_RECORDER_PLAYER,
130     AVRCP_SUBUNIT_TYPE_TUNER,
131     AVRCP_SUBUNIT_TYPE_CA,
132     AVRCP_SUBUNIT_TYPE_CAMERA,
133     AVRCP_SUBUNIT_TYPE_RESERVED,
134     AVRCP_SUBUNIT_TYPE_PANEL = 9,
135     AVRCP_SUBUNIT_TYPE_BULLETIN_BOARD,
136     AVRCP_SUBUNIT_TYPE_CAMERA_STORAGE,
137     AVRCP_SUBUNIT_TYPE_VENDOR_UNIQUE = 0x1C,
138     AVRCP_SUBUNIT_TYPE_RESERVED_FOR_ALL_SUBUNIT_TYPES,
139     AVRCP_SUBUNIT_TYPE_EXTENDED_TO_NEXT_BYTE, // The unit_type field may take value 1E16, which means that the field is extended to the following byte. In that case, an additional byte for extended_unit_type will be added immediately following operand[1].
140                                               // Further extension is possible when the value of extended_unit_type is FF16, in which case another byte will be added.
141     AVRCP_SUBUNIT_TYPE_UNIT = 0x1F
142 } avrcp_subunit_type_t;
143 
144 typedef enum {
145     AVRCP_SUBUNIT_ID = 0,
146     AVRCP_SUBUNIT_ID_IGNORE = 7
147 } avrcp_subunit_id_t;
148 
149 typedef enum {
150     AVRCP_CMD_OPCODE_VENDOR_DEPENDENT = 0x00,
151     // AVRCP_CMD_OPCODE_RESERVE = 0x01,
152     AVRCP_CMD_OPCODE_UNIT_INFO = 0x30,
153     // AVRCP_CMD_OPCODE_SUBUNIT_INFO = 0x31,
154     AVRCP_CMD_OPCODE_PASS_THROUGH = 0x7C,
155     // AVRCP_CMD_OPCODE_VERSION = 0xB0,
156     // AVRCP_CMD_OPCODE_POWER = 0xB2,
157     AVRCP_CMD_OPCODE_UNDEFINED = 0xFF
158 } avrcp_command_opcode_t;
159 
160 typedef enum {
161     AVRCP_OPERATION_ID_SKIP = 0x3C,
162     AVRCP_OPERATION_ID_VOLUME_UP = 0x41,
163     AVRCP_OPERATION_ID_VOLUME_DOWN = 0x42,
164     AVRCP_OPERATION_ID_MUTE = 0x43,
165 
166     AVRCP_OPERATION_ID_PLAY = 0x44,
167     AVRCP_OPERATION_ID_STOP = 0x45,
168     AVRCP_OPERATION_ID_PAUSE = 0x46,
169     AVRCP_OPERATION_ID_REWIND = 0x48,
170     AVRCP_OPERATION_ID_FAST_FORWARD = 0x49,
171     AVRCP_OPERATION_ID_FORWARD = 0x4B,
172     AVRCP_OPERATION_ID_BACKWARD = 0x4C,
173     AVRCP_OPERATION_ID_UNDEFINED = 0xFF
174 } avrcp_operation_id_t;
175 
176 typedef enum {
177     AVCTP_CONNECTION_IDLE,
178     AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE,
179     AVCTP_CONNECTION_W4_L2CAP_CONNECTED,
180     AVCTP_CONNECTION_OPENED,
181     AVCTP_W2_SEND_PRESS_COMMAND,
182     AVCTP_W2_SEND_RELEASE_COMMAND,
183     AVCTP_W4_STOP,
184     AVCTP_W2_SEND_COMMAND,
185     AVCTP_W2_RECEIVE_PRESS_RESPONSE,
186     AVCTP_W2_RECEIVE_RESPONSE
187 } avctp_connection_state_t;
188 
189 typedef struct {
190     btstack_linked_item_t    item;
191     bd_addr_t remote_addr;
192     uint16_t l2cap_signaling_cid;
193     uint16_t avrcp_cid;
194 
195     avctp_connection_state_t state;
196     uint8_t wait_to_send;
197 
198     // command
199     uint8_t transaction_label;
200     avrcp_command_opcode_t command_opcode;
201     avrcp_command_type_t command_type;
202     avrcp_subunit_type_t subunit_type;
203     avrcp_subunit_id_t   subunit_id;
204     uint8_t cmd_operands[20];
205     uint8_t cmd_operands_length;
206     btstack_timer_source_t press_and_hold_cmd_timer;
207     uint8_t  continuous_fast_forward_cmd;
208     uint16_t notifications_enabled;
209     uint16_t notifications_to_register;
210     uint16_t notifications_to_deregister;
211 } avrcp_connection_t;
212 
213 typedef enum {
214     AVRCP_PLAY_STATUS_STOPPED = 0x00,
215     AVRCP_PLAY_STATUS_PLAYING,
216     AVRCP_PLAY_STATUS_PAUSED,
217     AVRCP_PLAY_STATUS_FWD_SEEK,
218     AVRCP_PLAY_STATUS_REV_SEEK,
219     AVRCP_PLAY_STATUS_ERROR = 0xFF
220 } avrcp_play_status_t;
221 
222 typedef enum {
223     AVRCP_SHUFFLE_MODE_INVALID,
224     AVRCP_SHUFFLE_MODE_OFF,
225     AVRCP_SHUFFLE_MODE_ALL_TRACKS,
226     AVRCP_SHUFFLE_MODE_GROUP
227 } avrcp_shuffle_mode_t;
228 
229 typedef enum {
230     AVRCP_REPEAT_MODE_INVALID,
231     AVRCP_REPEAT_MODE_OFF,
232     AVRCP_REPEAT_MODE_SINGLE_TRACK,
233     AVRCP_REPEAT_MODE_ALL_TRACKS,
234     AVRCP_REPEAT_MODE_GROUP
235 } avrcp_repeat_mode_t;
236 
237 typedef enum{
238     AVRCP_CONTROLLER = 0,
239     AVRCP_TARGET
240 } avrcp_role_t;
241 
242 typedef struct {
243     avrcp_role_t role;
244     btstack_linked_list_t connections;
245     btstack_packet_handler_t avrcp_callback;
246     btstack_packet_handler_t packet_handler;
247 } avrcp_context_t;
248 
249 typedef struct {
250     avrcp_connection_t * connection;
251     avrcp_context_t * avrcp_context;
252     uint16_t avrcp_l2cap_psm;
253     uint16_t avrcp_version;
254     uint16_t avrcp_browsing_l2cap_psm;
255     uint16_t avrcp_browsing_version;
256     uint8_t  role_supported;
257 } avrcp_sdp_query_context_t;
258 
259 
260 /**
261  * @brief AVDTP Sink service record.
262  * @param service
263  * @param service_record_handle
264  * @param browsing  1 - supported, 0 - not supported
265  * @param supported_features 16-bit bitmap, see AVDTP_SINK_SF_* values in avdtp.h
266  * @param service_name
267  * @param service_provider_name
268  */
269 void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name);
270 
271 /**
272  * @brief AVDTP Sink service record.
273  * @param service
274  * @param service_record_handle
275  * @param browsing  1 - supported, 0 - not supported
276  * @param supported_features 16-bit bitmap, see AVDTP_SINK_SF_* values in avdtp.h
277  * @param service_name
278  * @param service_provider_name
279  */
280 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name);
281 
282 
283 /**
284  * @brief Set up AVDTP Sink device.
285  */
286 void avrcp_controller_init(void);
287 
288 /**
289  * @brief Register callback for the AVRCP Sink client.
290  * @param callback
291  */
292 void avrcp_register_packet_handler(btstack_packet_handler_t callback);
293 
294 /**
295  * @brief Connect to device with a Bluetooth address.
296  * @param bd_addr
297  * @param avrcp_cid
298  * @returns status
299  */
300 
301 
302 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid);
303 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid);
304 
305 /**
306  * @brief Disconnect from AVRCP target
307  * @param avrcp_cid
308  * @returns status
309  */
310 uint8_t avrcp_disconnect(uint16_t avrcp_cid);
311 
312 /**
313  * @brief Unit info.
314  * @param avrcp_cid
315  * @returns status
316  */
317 uint8_t avrcp_unit_info(uint16_t avrcp_cid);
318 
319 /**
320  * @brief Get capabilities.
321  * @param avrcp_cid
322  * @returns status
323  */
324 uint8_t avrcp_get_supported_company_ids(uint16_t avrcp_cid);
325 
326 /**
327  * @brief Get supported Events.
328  * @param avrcp_cid
329  * @returns status
330  */
331 uint8_t avrcp_get_supported_events(uint16_t avrcp_cid);
332 
333 /**
334  * @brief Play. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
335  * @param avrcp_cid
336  * @returns status
337  */
338 uint8_t avrcp_play(uint16_t avrcp_cid);
339 
340 /**
341  * @brief Stop. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
342  * @param avrcp_cid
343  * @returns status
344  */
345 uint8_t avrcp_stop(uint16_t avrcp_cid);
346 
347 /**
348  * @brief Pause. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
349  * @param avrcp_cid
350  * @returns status
351  */
352 uint8_t avrcp_pause(uint16_t avrcp_cid);
353 
354 /**
355  * @brief Start Fast Forward. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
356  * @param avrcp_cid
357  * @returns status
358  */
359 uint8_t avrcp_start_fast_forward(uint16_t avrcp_cid);
360 
361 /**
362  * @brief Stop Fast Forward. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
363  * @param avrcp_cid
364  * @returns status
365  */
366 uint8_t avrcp_stop_fast_forward(uint16_t avrcp_cid);
367 
368 /**
369  * @brief Single step - fast forward. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
370  * @param avrcp_cid
371  * @returns status
372  */
373 uint8_t avrcp_fast_forward(uint16_t avrcp_cid);
374 
375 /**
376  * @brief Stop Rewind. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
377  * @param avrcp_cid
378  * @returns status
379  */
380 uint8_t avrcp_start_rewind(uint16_t avrcp_cid);
381 
382 /**
383  * @brief Stop Rewind. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
384  * @param avrcp_cid
385  * @returns status
386  */
387 uint8_t avrcp_stop_rewind(uint16_t avrcp_cid);
388 
389 /**
390  * @brief Single step rewind. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
391  * @param avrcp_cid
392  * @returns status
393  */
394 uint8_t avrcp_rewind(uint16_t avrcp_cid);
395 
396 /**
397  * @brief Forward. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
398  * @param avrcp_cid
399  * @returns status
400  */
401 uint8_t avrcp_forward(uint16_t avrcp_cid);
402 
403 /**
404  * @brief Backward. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
405  * @param avrcp_cid
406  * @returns status
407  */
408 uint8_t avrcp_backward(uint16_t avrcp_cid);
409 
410 
411 /**
412  * @brief Get play status. Returns event of type AVRCP_SUBEVENT_PLAY_STATUS (length, position, play_status).
413  * If TG does not support SongLength And SongPosition on TG, then TG shall return 0xFFFFFFFF.
414  * @param avrcp_cid
415  * @returns status
416  */
417 uint8_t avrcp_get_play_status(uint16_t avrcp_cid);
418 
419 /**
420  * @brief Enable notification. Response via AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE.
421  * @param avrcp_cid
422  * @param event_id
423  * @returns status
424  */
425 uint8_t avrcp_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id);
426 
427 /**
428  * @brief Disable notification. Response via AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE.
429  * @param avrcp_cid
430  * @param event_id
431  * @returns status
432  */
433 uint8_t avrcp_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id);
434 
435 /**
436  * @brief Get info on now playing media.
437  * @param avrcp_cid
438  * @returns status
439  */
440 uint8_t avrcp_get_now_playing_info(uint16_t avrcp_cid);
441 
442 /**
443  * @brief Set absolute volume 0-127 (corresponds to 0-100%). Response via AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE
444  * @param avrcp_cid
445  * @returns status
446  */
447 uint8_t avrcp_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume);
448 
449 /**
450  * @brief Turns the volume to high. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
451  * @param avrcp_cid
452  * @returns status
453  */
454 uint8_t avrcp_volume_up(uint16_t avrcp_cid);
455 
456 /**
457  * @brief Turns the volume to low. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
458  * @param avrcp_cid
459  * @returns status
460  */
461 uint8_t avrcp_volume_down(uint16_t avrcp_cid);
462 
463 /**
464  * @brief Puts the sound out. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
465  * @param avrcp_cid
466  * @returns status
467  */
468 uint8_t avrcp_mute(uint16_t avrcp_cid);
469 
470 /**
471  * @brief Skip to next playing media. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
472  * @param avrcp_cid
473  * @returns status
474  */
475 uint8_t avrcp_skip(uint16_t avrcp_cid);
476 
477 /**
478  * @brief Query repeat and shuffle mode. Response via AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE.
479  * @param avrcp_cid
480  * @returns status
481  */
482 uint8_t avrcp_query_shuffle_and_repeat_modes(uint16_t avrcp_cid);
483 
484 /**
485  * @brief Set shuffle mode. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
486  * @param avrcp_cid
487  * @returns status
488  */
489 uint8_t avrcp_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode);
490 
491 /**
492  * @brief Set repeat mode. Event AVRCP_SUBEVENT_OPERATION_COMPLETE returns operation id and status.
493  * @param avrcp_cid
494  * @returns status
495  */
496 uint8_t avrcp_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode);
497 
498 const char * avrcp_subunit2str(uint16_t index);
499 const char * avrcp_event2str(uint16_t index);
500 const char * avrcp_operation2str(uint8_t index);
501 const char * avrcp_attribute2str(uint8_t index);
502 const char * avrcp_play_status2str(uint8_t index);
503 const char * avrcp_ctype2str(uint8_t index);
504 const char * avrcp_repeat2str(uint8_t index);
505 const char * avrcp_shuffle2str(uint8_t index);
506 
507 /* API_END */
508 #if defined __cplusplus
509 }
510 #endif
511 
512 #endif // __AVRCP_H