xref: /btstack/src/classic/avrcp.h (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
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 #define AVRCP_MEDIA_ATTR_COUNT 7
58 
59 typedef enum {
60     AVRCP_STATUS_INVALID_COMMAND = 0,           // sent if TG received a PDU that it did not understand.
61     AVRCP_STATUS_INVALID_PARAMETER,             // Sent if the TG received a PDU with a parameter ID that it did not understand, or, if there is only one parameter ID in the PDU.
62     AVRCP_STATUS_SPECIFIED_PARAMETER_NOT_FOUND, // sent if the parameter ID is understood, but content is wrong or corrupted.
63     AVRCP_STATUS_INTERNAL_ERROR,                // sent if there are error conditions not covered by a more specific error code.
64     AVRCP_STATUS_SUCCESS,                       // sent if the operation was successful.
65     AVRCP_STATUS_UID_CHANGED,                   // sent if the UIDs on the device have changed.
66     AVRCP_STATUS_RESERVED_6,
67     AVRCP_STATUS_INVALID_DIRECTION,             // The Direction parameter is invalid. Valid for command: Change Path
68     AVRCP_STATUS_NOT_A_DIRECTORY,               // The UID provided does not refer to a folder item. Valid for command: Change Path
69     AVRCP_STATUS_DOES_NOT_EXIST,                // The UID provided does not refer to any currently valid. Valid for command: Change Path, PlayItem, AddToNowPlaying, GetItemAttributes
70     AVRCP_STATUS_INVALID_SCOPE,                 // The scope parameter is invalid. Valid for command: GetFolderItems, PlayItem, AddToNowPlayer, GetItemAttributes,
71     AVRCP_STATUS_RANGE_OUT_OF_BOUNDS,           // The start of range provided is not valid. Valid for command: GetFolderItems
72     AVRCP_STATUS_UID_IS_A_DIRECTORY,            // The UID provided refers to a directory, which cannot be handled by this media player. Valid for command: PlayItem, AddToNowPlaying
73     AVRCP_STATUS_MEDIA_IN_USE,                  // The media is not able to be used for this operation at this time. Valid for command: PlayItem, AddToNowPlaying
74     AVRCP_STATUS_NOW_PLAYING_LIST_FULL,         // No more items can be added to the Now Playing List. Valid for command: AddToNowPlaying
75     AVRCP_STATUS_SEARCH_NOT_SUPPORTED,          // The Browsed Media Player does not support search. Valid for command: Search
76     AVRCP_STATUS_SEARCH_IN_PROGRESS,            // A search operation is already in progress. Valid for command: Search
77     AVRCP_STATUS_INVALID_PLAYER_ID,             // The specified Player Id does not refer to a valid player. Valid for command: SetAddressedPlayer, SetBrowsedPlayer
78     AVRCP_STATUS_PLAYER_NOT_BROWSABLE,          // The Player Id supplied refers to a Media Player which does not support browsing. Valid for command: SetBrowsedPlayer
79     AVRCP_STATUS_PLAYER_NOT_ADDRESSED,          // The Player Id supplied refers to a player which is not currently addressed, and the command is not able to be performed if the player is not set as addressed. Valid for command: Search SetBrowsedPlayer
80     AVRCP_STATUS_NO_VALID_SEARCH_RESULTS,       // The Search result list does not contain valid entries, e.g. after being invalidated due to change of browsed player. Valid for command: GetFolderItems
81     AVRCP_STATUS_NO_AVAILABLE_PLAYERS,
82     AVRCP_STATUS_ADDRESSED_PLAYER_CHANGED,       // Valid for command: Register Notification
83     AVRCP_STATUS_RESERVED                       // 0x17 - 0xFF
84 } avrcp_status_code_t;
85 
86 typedef enum {
87     AVRCP_SINGLE_PACKET= 0,
88     AVRCP_START_PACKET    ,
89     AVRCP_CONTINUE_PACKET ,
90     AVRCP_END_PACKET
91 } avrcp_packet_type_t;
92 
93 typedef enum {
94     AVRCP_COMMAND_FRAME = 0,
95     AVRCP_RESPONSE_FRAME
96 } avrcp_frame_type_t;
97 
98 
99 typedef enum {
100     AVRCP_CAPABILITY_ID_COMPANY = 0x02,
101     AVRCP_CAPABILITY_ID_EVENT = 0x03
102 } avrcp_capability_id_t;
103 
104 typedef enum {
105     AVRCP_MEDIA_ATTR_TITLE = 1,
106     AVRCP_MEDIA_ATTR_ARTIST,
107     AVRCP_MEDIA_ATTR_ALBUM,
108     AVRCP_MEDIA_ATTR_TRACK,
109     AVRCP_MEDIA_ATTR_TOTAL_TRACKS,
110     AVRCP_MEDIA_ATTR_GENRE,
111     AVRCP_MEDIA_ATTR_SONG_LENGTH
112 } avrcp_media_attribute_id_t;
113 
114 typedef enum {
115     AVRCP_PDU_ID_GET_CAPABILITIES = 0x10,
116     AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue = 0x13,
117     AVRCP_PDU_ID_SetPlayerApplicationSettingValue = 0x14,
118     AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,
119     AVRCP_PDU_ID_GET_PLAY_STATUS = 0x30,
120     AVRCP_PDU_ID_REGISTER_NOTIFICATION = 0x31,
121     AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50,
122     AVRCP_PDU_ID_UNDEFINED = 0xFF
123 } avrcp_pdu_id_t;
124 
125 typedef enum {
126     AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED = 0x01,            // Change in playback status of the current track.
127     AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED = 0x02,                      // Change of current track
128     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END = 0x03,                  // Reached end of a track
129     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START = 0x04,                // Reached start of a track
130     AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED = 0x05,               // Change in playback position. Returned after the specified playback notification change notification interval
131     AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED = 0x06,                // Change in battery status
132     AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED = 0x07,              // Change in system status
133     AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED = 0x08, // Change in player application setting
134     AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED = 0x09,        // The content of the Now Playing list has changed, see 6.9.5.
135     AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED = 0x0a,          // The available players have changed, see 6.9.4.
136     AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED = 0x0b,           // The Addressed Player has been changed, see 6.9.2.
137     AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED = 0x0c,                       // The UIDs have changed, see 6.10.3.3.
138     AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED = 0x0d                      // The volume has been changed locally on the TG, see 6.13.3.
139 } avrcp_notification_event_id_t;
140 
141 typedef enum {
142     AVRCP_CTYPE_CONTROL = 0,
143     AVRCP_CTYPE_STATUS,
144     AVRCP_CTYPE_SPECIFIC_INQUIRY,
145     AVRCP_CTYPE_NOTIFY,
146     AVRCP_CTYPE_GENERAL_INQUIRY,
147     AVRCP_CTYPE_RESERVED5,
148     AVRCP_CTYPE_RESERVED6,
149     AVRCP_CTYPE_RESERVED7,
150     AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED = 8,
151     AVRCP_CTYPE_RESPONSE_ACCEPTED,
152     AVRCP_CTYPE_RESPONSE_REJECTED,
153     AVRCP_CTYPE_RESPONSE_IN_TRANSITION, // target state is in transition. A subsequent STATUS command, may result in the return of a STABLE status
154     AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
155     AVRCP_CTYPE_RESPONSE_CHANGED_STABLE,
156     AVRCP_CTYPE_RESPONSE_RESERVED,
157     AVRCP_CTYPE_RESPONSE_INTERIM            // target is unable to respond with either ACCEPTED or REJECTED within 100 millisecond
158 } avrcp_command_type_t;
159 
160 // control command response: accepted, rejected, interim
161 // status command response: not implemented, rejected, in transiiton, stable
162 // notify command response: not implemented, rejected, changed
163 typedef enum {
164     AVRCP_SUBUNIT_TYPE_MONITOR = 0,
165     AVRCP_SUBUNIT_TYPE_AUDIO = 1,
166     AVRCP_SUBUNIT_TYPE_PRINTER,
167     AVRCP_SUBUNIT_TYPE_DISC,
168     AVRCP_SUBUNIT_TYPE_TAPE_RECORDER_PLAYER,
169     AVRCP_SUBUNIT_TYPE_TUNER,
170     AVRCP_SUBUNIT_TYPE_CA,
171     AVRCP_SUBUNIT_TYPE_CAMERA,
172     AVRCP_SUBUNIT_TYPE_RESERVED,
173     AVRCP_SUBUNIT_TYPE_PANEL = 9,
174     AVRCP_SUBUNIT_TYPE_BULLETIN_BOARD,
175     AVRCP_SUBUNIT_TYPE_CAMERA_STORAGE,
176     AVRCP_SUBUNIT_TYPE_VENDOR_UNIQUE = 0x1C,
177     AVRCP_SUBUNIT_TYPE_RESERVED_FOR_ALL_SUBUNIT_TYPES,
178     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].
179                                               // Further extension is possible when the value of extended_unit_type is FF16, in which case another byte will be added.
180     AVRCP_SUBUNIT_TYPE_UNIT = 0x1F
181 } avrcp_subunit_type_t;
182 
183 typedef enum {
184     AVRCP_SUBUNIT_ID = 0,
185     AVRCP_SUBUNIT_ID_IGNORE = 7
186 } avrcp_subunit_id_t;
187 
188 typedef enum {
189     AVRCP_CMD_OPCODE_VENDOR_DEPENDENT = 0x00,
190     // AVRCP_CMD_OPCODE_RESERVE = 0x01,
191     AVRCP_CMD_OPCODE_UNIT_INFO = 0x30,
192     AVRCP_CMD_OPCODE_SUBUNIT_INFO = 0x31,
193     AVRCP_CMD_OPCODE_PASS_THROUGH = 0x7C,
194     // AVRCP_CMD_OPCODE_VERSION = 0xB0,
195     // AVRCP_CMD_OPCODE_POWER = 0xB2,
196     AVRCP_CMD_OPCODE_UNDEFINED = 0xFF
197 } avrcp_command_opcode_t;
198 
199 typedef enum {
200     AVRCP_OPERATION_ID_SKIP = 0x3C,
201     AVRCP_OPERATION_ID_VOLUME_UP = 0x41,
202     AVRCP_OPERATION_ID_VOLUME_DOWN = 0x42,
203     AVRCP_OPERATION_ID_MUTE = 0x43,
204 
205     AVRCP_OPERATION_ID_PLAY = 0x44,
206     AVRCP_OPERATION_ID_STOP = 0x45,
207     AVRCP_OPERATION_ID_PAUSE = 0x46,
208     AVRCP_OPERATION_ID_REWIND = 0x48,
209     AVRCP_OPERATION_ID_FAST_FORWARD = 0x49,
210     AVRCP_OPERATION_ID_FORWARD = 0x4B,
211     AVRCP_OPERATION_ID_BACKWARD = 0x4C,
212     AVRCP_OPERATION_ID_UNDEFINED = 0xFF
213 } avrcp_operation_id_t;
214 
215 typedef enum{
216     AVRCP_PLAY_STATUS_STOPPED = 0x00,
217     AVRCP_PLAY_STATUS_PLAYING,
218     AVRCP_PLAY_STATUS_PAUSED,
219     AVRCP_PLAY_STATUS_FWD_SEEK,
220     AVRCP_PLAY_STATUS_REV_SEEK,
221     AVRCP_PLAY_STATUS_ERROR = 0xFF
222 } avrcp_play_status_t;
223 
224 typedef enum {
225     AVCTP_CONNECTION_IDLE,
226     AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE,
227     AVCTP_CONNECTION_W4_L2CAP_CONNECTED,
228     AVCTP_CONNECTION_OPENED,
229     AVCTP_W2_SEND_PRESS_COMMAND,
230     AVCTP_W2_SEND_RELEASE_COMMAND,
231     AVCTP_W4_STOP,
232     AVCTP_W2_SEND_COMMAND,
233     AVCTP_W2_SEND_RESPONSE,
234     AVCTP_W2_RECEIVE_PRESS_RESPONSE,
235     AVCTP_W2_RECEIVE_RESPONSE
236 } avctp_connection_state_t;
237 
238 typedef struct {
239     uint16_t len;
240     uint8_t  * value;
241 } avrcp_now_playing_info_item_t;
242 
243 typedef struct {
244     btstack_linked_item_t    item;
245     bd_addr_t remote_addr;
246     uint16_t l2cap_signaling_cid;
247     uint16_t avrcp_cid;
248 
249     avctp_connection_state_t state;
250     uint8_t wait_to_send;
251 
252     // command
253     uint8_t transaction_label;
254     avrcp_command_opcode_t command_opcode;
255     avrcp_command_type_t command_type;
256     avrcp_subunit_type_t subunit_type;
257     avrcp_subunit_id_t   subunit_id;
258     uint8_t cmd_operands[20];
259     uint8_t cmd_operands_length;
260     btstack_timer_source_t press_and_hold_cmd_timer;
261     uint8_t  continuous_fast_forward_cmd;
262     uint16_t notifications_enabled;
263     uint16_t notifications_to_register;
264     uint16_t notifications_to_deregister;
265 
266     avrcp_now_playing_info_item_t now_playing_info[AVRCP_MEDIA_ATTR_COUNT];
267     uint32_t song_length_ms;
268     int total_tracks;
269     int track_nr;
270     // used for fragmentation
271     int offset;
272     int total_num_bytes;
273     uint8_t now_playing_info_response;
274 } avrcp_connection_t;
275 
276 typedef enum {
277     AVRCP_SHUFFLE_MODE_INVALID,
278     AVRCP_SHUFFLE_MODE_OFF,
279     AVRCP_SHUFFLE_MODE_ALL_TRACKS,
280     AVRCP_SHUFFLE_MODE_GROUP
281 } avrcp_shuffle_mode_t;
282 
283 typedef enum {
284     AVRCP_REPEAT_MODE_INVALID,
285     AVRCP_REPEAT_MODE_OFF,
286     AVRCP_REPEAT_MODE_SINGLE_TRACK,
287     AVRCP_REPEAT_MODE_ALL_TRACKS,
288     AVRCP_REPEAT_MODE_GROUP
289 } avrcp_repeat_mode_t;
290 
291 typedef enum{
292     AVRCP_CONTROLLER = 0,
293     AVRCP_TARGET
294 } avrcp_role_t;
295 
296 typedef enum {
297     UTF8 = 106
298 } rfc2978_charset_mib_enumid_t;
299 
300 typedef struct {
301     avrcp_role_t role;
302     btstack_linked_list_t connections;
303     btstack_packet_handler_t avrcp_callback;
304     btstack_packet_handler_t packet_handler;
305 
306     // SDP query
307     uint16_t avrcp_cid;
308     uint16_t avrcp_l2cap_psm;
309     uint16_t avrcp_version;
310     uint16_t avrcp_browsing_l2cap_psm;
311     uint16_t avrcp_browsing_version;
312     uint8_t  role_supported;
313 } avrcp_context_t;
314 
315 const char * avrcp_subunit2str(uint16_t index);
316 const char * avrcp_event2str(uint16_t index);
317 const char * avrcp_operation2str(uint8_t index);
318 const char * avrcp_attribute2str(uint8_t index);
319 const char * avrcp_play_status2str(uint8_t index);
320 const char * avrcp_ctype2str(uint8_t index);
321 const char * avrcp_repeat2str(uint8_t index);
322 const char * avrcp_shuffle2str(uint8_t index);
323 
324 void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context);
325 
326 void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name);
327 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid);
328 void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status);
329 void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid);
330 
331 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size);
332 avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context);
333 avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context);
334 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid);
335 
336 #if defined __cplusplus
337 }
338 #endif
339 
340 #endif // __AVRCP_H