xref: /btstack/src/classic/avrcp.h (revision b28dc8004dd8d4fb9020a6dcd2bc81f05d36a008)
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 BLUEKITCHEN
24  * GMBH 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  * Audio/Video Remote Control Profile (AVRCP)
40  *
41  */
42 
43 #ifndef AVRCP_H
44 #define AVRCP_H
45 
46 #include <stdint.h>
47 
48 #include "btstack_run_loop.h"
49 #include "btstack_linked_list.h"
50 #include "l2cap.h"
51 
52 #if defined __cplusplus
53 extern "C" {
54 #endif
55 
56 #define PSM_AVCTP_BROWSING              0x001b
57 
58 #define AVRCP_BROWSING_ITEM_HEADER_LEN 3
59 #define AVRCP_BROWSING_MAX_NUM_ATTR_IDS 8
60 
61 #define BT_SIG_COMPANY_ID 0x001958
62 #define AVRCP_MEDIA_ATTR_COUNT 7
63 #define AVRCP_MAX_ATTRIBUTTE_SIZE 100
64 #define AVRCP_ATTRIBUTE_HEADER_LEN  8
65 #define AVRCP_MAX_FOLDER_NAME_SIZE      20
66 
67 #define AVRCP_NO_TRACK_SELECTED_PLAYBACK_POSITION_CHANGED    0xFFFFFFFF
68 
69 #define AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER          0x0001u
70 #define AVRCP_FEATURE_MASK_CATEGORY_MONITOR_OR_AMPLIFIER        0x0002u
71 #define AVRCP_FEATURE_MASK_CATEGORY_TUNER                       0x0004u
72 #define AVRCP_FEATURE_MASK_CATEGORY_MENU                        0x0008u
73 #define AVRCP_FEATURE_MASK_PLAYER_APPLICATION_SETTINGS          0x0010u   // AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER must be 1 for this feature to be set
74 #define AVRCP_FEATURE_MASK_GROUP_NAVIGATION                     0x0020u   // AVRCP_FEATURE_MASK_CATEGORY_PLAYER_OR_RECORDER must be 1 for this feature to be set
75 #define AVRCP_FEATURE_MASK_BROWSING                             0x0040u
76 #define AVRCP_FEATURE_MASK_MULTIPLE_MEDIA_PLAYE_APPLICATIONS    0x0080u
77 
78 typedef enum {
79     AVRCP_STATUS_INVALID_COMMAND = 0,           // sent if TG received a PDU that it did not understand.
80     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.
81     AVRCP_STATUS_SPECIFIED_PARAMETER_NOT_FOUND, // sent if the parameter ID is understood, but content is wrong or corrupted.
82     AVRCP_STATUS_INTERNAL_ERROR,                // sent if there are error conditions not covered by a more specific error code.
83     AVRCP_STATUS_SUCCESS,                       // sent if the operation was successful.
84     AVRCP_STATUS_UID_CHANGED,                   // sent if the UIDs on the device have changed.
85     AVRCP_STATUS_RESERVED_6,
86     AVRCP_STATUS_INVALID_DIRECTION,             // The Direction parameter is invalid. Valid for command: Change Path
87     AVRCP_STATUS_NOT_A_DIRECTORY,               // The UID provided does not refer to a folder item. Valid for command: Change Path
88     AVRCP_STATUS_DOES_NOT_EXIST,                // The UID provided does not refer to any currently valid. Valid for command: Change Path, PlayItem, AddToNowPlaying, GetItemAttributes
89     AVRCP_STATUS_INVALID_SCOPE,                 // The scope parameter is invalid. Valid for command: GetFolderItems, PlayItem, AddToNowPlayer, GetItemAttributes,
90     AVRCP_STATUS_RANGE_OUT_OF_BOUNDS,           // The start of range provided is not valid. Valid for command: GetFolderItems
91     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
92     AVRCP_STATUS_MEDIA_IN_USE,                  // The media is not able to be used for this operation at this time. Valid for command: PlayItem, AddToNowPlaying
93     AVRCP_STATUS_NOW_PLAYING_LIST_FULL,         // No more items can be added to the Now Playing List. Valid for command: AddToNowPlaying
94     AVRCP_STATUS_SEARCH_NOT_SUPPORTED,          // The Browsed Media Player does not support search. Valid for command: Search
95     AVRCP_STATUS_SEARCH_IN_PROGRESS,            // A search operation is already in progress. Valid for command: Search
96     AVRCP_STATUS_INVALID_PLAYER_ID,             // The specified Player Id does not refer to a valid player. Valid for command: SetAddressedPlayer, SetBrowsedPlayer
97     AVRCP_STATUS_PLAYER_NOT_BROWSABLE,          // The Player Id supplied refers to a Media Player which does not support browsing. Valid for command: SetBrowsedPlayer
98     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
99     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
100     AVRCP_STATUS_NO_AVAILABLE_PLAYERS,
101     AVRCP_STATUS_ADDRESSED_PLAYER_CHANGED,       // Valid for command: Register Notification
102     AVRCP_STATUS_RESERVED                       // 0x17 - 0xFF
103 } avrcp_status_code_t;
104 
105 typedef enum {
106     AVRCP_SINGLE_PACKET= 0,
107     AVRCP_START_PACKET    ,
108     AVRCP_CONTINUE_PACKET ,
109     AVRCP_END_PACKET
110 } avrcp_packet_type_t;
111 
112 
113 typedef enum {
114     AVCTP_SINGLE_PACKET= 0,
115     AVCTP_START_PACKET    ,
116     AVCTP_CONTINUE_PACKET ,
117     AVCTP_END_PACKET
118 } avctp_packet_type_t;
119 
120 typedef enum {
121     AVRCP_COMMAND_FRAME = 0,
122     AVRCP_RESPONSE_FRAME
123 } avrcp_frame_type_t;
124 
125 
126 typedef enum {
127     AVRCP_CAPABILITY_ID_COMPANY = 0x02,
128     AVRCP_CAPABILITY_ID_EVENT = 0x03
129 } avrcp_capability_id_t;
130 
131 typedef enum {
132     AVRCP_MEDIA_ATTR_ALL = 0x0000,
133     AVRCP_MEDIA_ATTR_TITLE,
134     AVRCP_MEDIA_ATTR_ARTIST,
135     AVRCP_MEDIA_ATTR_ALBUM,
136     AVRCP_MEDIA_ATTR_TRACK,
137     AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS,
138     AVRCP_MEDIA_ATTR_GENRE,
139     AVRCP_MEDIA_ATTR_SONG_LENGTH_MS,
140     AVRCP_MEDIA_ATTR_DEFAULT_COVER_ART,
141     AVRCP_MEDIA_ATTR_RESERVED = 0x0009,
142     AVRCP_MEDIA_ATTR_NONE = 0x7FFF
143 } avrcp_media_attribute_id_t;
144 
145 typedef enum {
146     AVRCP_PDU_ID_GET_CAPABILITIES = 0x10,
147     AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE = 0x13,
148     AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE = 0x14,
149     AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,
150     AVRCP_PDU_ID_GET_PLAY_STATUS = 0x30,
151     AVRCP_PDU_ID_REGISTER_NOTIFICATION = 0x31,
152     AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE = 0x40,
153     AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE = 0x41,
154     AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50,
155     AVRCP_PDU_ID_SET_ADDRESSED_PLAYER = 0x60,
156     AVRCP_PDU_ID_SET_BROWSED_PLAYER = 0x70,
157     AVRCP_PDU_ID_GET_FOLDER_ITEMS = 0x71,
158     AVRCP_PDU_ID_CHANGE_PATH = 0x72,
159     AVRCP_PDU_ID_GET_ITEM_ATTRIBUTES = 0x73,
160     AVRCP_PDU_ID_PLAY_ITEM = 0x74,
161     AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS = 0x75,
162     AVRCP_PDU_ID_SEARCH = 0x80,
163     AVRCP_PDU_ID_ADD_TO_NOW_PLAYING = 0x90,
164     AVRCP_PDU_ID_GENERAL_REJECT = 0xA0,
165 
166     AVRCP_PDU_ID_UNDEFINED = 0xFF
167 } avrcp_pdu_id_t;
168 
169 typedef enum {
170     AVRCP_NOTIFICATION_EVENT_FIRST_INDEX = 0x01,
171     AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED = 0x01,            // Change in playback status of the current track.
172     AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED = 0x02,                      // Change of current track
173     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END = 0x03,                  // Reached end of a track
174     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START = 0x04,                // Reached start of a track
175     AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED = 0x05,               // Change in playback position. Returned after the specified playback notification change notification interval
176     AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED = 0x06,                // Change in battery status
177     AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED = 0x07,              // Change in system status
178     AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED = 0x08, // Change in player application setting
179     AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED = 0x09,        // The content of the Now Playing list has changed, see 6.9.5.
180     AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED = 0x0a,          // The available players have changed, see 6.9.4.
181     AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED = 0x0b,           // The Addressed Player has been changed, see 6.9.2.
182     AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED = 0x0c,                       // The UIDs have changed, see 6.10.3.3.
183     AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED = 0x0d,                     // The volume has been changed locally on the TG, see 6.13.3.
184     AVRCP_NOTIFICATION_EVENT_MAX_VALUE = 0x0e,
185     AVRCP_NOTIFICATION_EVENT_LAST_INDEX = 0x0e
186 } avrcp_notification_event_id_t;
187 
188 
189 // control command response: accepted, rejected, interim
190 // status  command response: not implemented, rejected, in transition, stable
191 // notify  command response: not implemented, rejected, changed
192 
193 typedef enum {
194     AVRCP_CTYPE_CONTROL = 0,
195     AVRCP_CTYPE_STATUS,
196     AVRCP_CTYPE_SPECIFIC_INQUIRY,
197     AVRCP_CTYPE_NOTIFY,
198     AVRCP_CTYPE_GENERAL_INQUIRY,
199     AVRCP_CTYPE_RESERVED5,
200     AVRCP_CTYPE_RESERVED6,
201     AVRCP_CTYPE_RESERVED7,
202     AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED = 8,
203     AVRCP_CTYPE_RESPONSE_ACCEPTED,
204     AVRCP_CTYPE_RESPONSE_REJECTED,
205     AVRCP_CTYPE_RESPONSE_IN_TRANSITION, // target state is in transition. A subsequent STATUS command, may result in the return of a STABLE status
206     AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
207     AVRCP_CTYPE_RESPONSE_CHANGED_STABLE,
208     AVRCP_CTYPE_RESPONSE_RESERVED,
209     AVRCP_CTYPE_RESPONSE_INTERIM            // target is unable to respond with either ACCEPTED or REJECTED within 100 millisecond
210 } avrcp_command_type_t;
211 
212 typedef enum {
213     AVRCP_SUBUNIT_TYPE_MONITOR = 0,
214     AVRCP_SUBUNIT_TYPE_AUDIO = 1,
215     AVRCP_SUBUNIT_TYPE_PRINTER,
216     AVRCP_SUBUNIT_TYPE_DISC,
217     AVRCP_SUBUNIT_TYPE_TAPE_RECORDER_PLAYER,
218     AVRCP_SUBUNIT_TYPE_TUNER,
219     AVRCP_SUBUNIT_TYPE_CA,
220     AVRCP_SUBUNIT_TYPE_CAMERA,
221     AVRCP_SUBUNIT_TYPE_RESERVED,
222     AVRCP_SUBUNIT_TYPE_PANEL = 9,
223     AVRCP_SUBUNIT_TYPE_BULLETIN_BOARD,
224     AVRCP_SUBUNIT_TYPE_CAMERA_STORAGE,
225     AVRCP_SUBUNIT_TYPE_VENDOR_UNIQUE = 0x1C,
226     AVRCP_SUBUNIT_TYPE_RESERVED_FOR_ALL_SUBUNIT_TYPES,
227     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].
228                                               // Further extension is possible when the value of extended_unit_type is FF16, in which case another byte will be added.
229     AVRCP_SUBUNIT_TYPE_UNIT = 0x1F
230 } avrcp_subunit_type_t;
231 
232 typedef enum {
233     AVRCP_SUBUNIT_ID = 0,
234     AVRCP_SUBUNIT_ID_IGNORE = 7
235 } avrcp_subunit_id_t;
236 
237 typedef enum {
238     AVRCP_CMD_OPCODE_VENDOR_DEPENDENT = 0x00,
239     // AVRCP_CMD_OPCODE_RESERVE = 0x01,
240     AVRCP_CMD_OPCODE_UNIT_INFO = 0x30,
241     AVRCP_CMD_OPCODE_SUBUNIT_INFO = 0x31,
242     AVRCP_CMD_OPCODE_PASS_THROUGH = 0x7C,
243     // AVRCP_CMD_OPCODE_VERSION = 0xB0,
244     // AVRCP_CMD_OPCODE_POWER = 0xB2,
245     AVRCP_CMD_OPCODE_UNDEFINED = 0xFF
246 } avrcp_command_opcode_t;
247 
248 // See "AVC-Panel Subunit.pdf", Chapter 9.4 "PASS THROUGH control command"
249 // Using subset defined in "AVRCP_v1.5.pdf", Chapter 4.6.1 "Support Level in TG"
250 typedef enum {
251     AVRCP_OPERATION_ID_SELECT = 0x00,
252     AVRCP_OPERATION_ID_UP = 0x01,
253     AVRCP_OPERATION_ID_DOWN = 0x02,
254     AVRCP_OPERATION_ID_LEFT = 0x03,
255     AVRCP_OPERATION_ID_RIGHT = 0x04,
256     AVRCP_OPERATION_ID_RIGHT_UP = 0x05,
257     AVRCP_OPERATION_ID_RIGHT_DOWN = 0x06,
258     AVRCP_OPERATION_ID_LEFT_UP = 0x07,
259     AVRCP_OPERATION_ID_LEFT_DOWN = 0x07,
260     AVRCP_OPERATION_ID_ROOT_MENU = 0x09,
261     AVRCP_OPERATION_ID_SETUP_MENU = 0x0A,
262     AVRCP_OPERATION_ID_CONTENTS_MENU = 0x0B,
263     AVRCP_OPERATION_ID_FAVORITE_MENU = 0x0C,
264     AVRCP_OPERATION_ID_EXIT = 0x0D,
265     AVRCP_OPERATION_ID_RESERVED_1 = 0x0E,
266 
267     AVRCP_OPERATION_ID_0 = 0x20,
268     AVRCP_OPERATION_ID_1 = 0x21,
269     AVRCP_OPERATION_ID_2 = 0x22,
270     AVRCP_OPERATION_ID_3 = 0x23,
271     AVRCP_OPERATION_ID_4 = 0x24,
272     AVRCP_OPERATION_ID_5 = 0x25,
273     AVRCP_OPERATION_ID_6 = 0x26,
274     AVRCP_OPERATION_ID_7 = 0x27,
275     AVRCP_OPERATION_ID_8 = 0x28,
276     AVRCP_OPERATION_ID_9 = 0x29,
277     AVRCP_OPERATION_ID_DOT   = 0x2A,
278     AVRCP_OPERATION_ID_ENTER = 0x2B,
279     AVRCP_OPERATION_ID_CLEAR = 0x2C,
280     AVRCP_OPERATION_ID_RESERVED_2 = 0x2D,
281 
282     AVRCP_OPERATION_ID_CHANNEL_UP = 0x30,
283     AVRCP_OPERATION_ID_CHANNEL_DOWN = 0x31,
284     AVRCP_OPERATION_ID_PREVIOUS_CHANNEL = 0x32,
285     AVRCP_OPERATION_ID_SOUND_SELECT = 0x33,
286     AVRCP_OPERATION_ID_INPUT_SELECT = 0x34,
287     AVRCP_OPERATION_ID_DISPLAY_INFORMATION = 0x35,
288     AVRCP_OPERATION_ID_HELP = 0x36,
289     AVRCP_OPERATION_ID_PAGE_UP = 0x37,
290     AVRCP_OPERATION_ID_PAGE_DOWN = 0x38,
291     AVRCP_OPERATION_ID_RESERVED_3 = 0x39,
292 
293     AVRCP_OPERATION_ID_SKIP = 0x3C,
294 
295     AVRCP_OPERATION_ID_POWER = 0x40,
296     AVRCP_OPERATION_ID_VOLUME_UP = 0x41,
297     AVRCP_OPERATION_ID_VOLUME_DOWN = 0x42,
298     AVRCP_OPERATION_ID_MUTE = 0x43,
299     AVRCP_OPERATION_ID_PLAY = 0x44,
300     AVRCP_OPERATION_ID_STOP = 0x45,
301     AVRCP_OPERATION_ID_PAUSE = 0x46,
302     AVRCP_OPERATION_ID_RECORD = 0x47,
303     AVRCP_OPERATION_ID_REWIND = 0x48,
304     AVRCP_OPERATION_ID_FAST_FORWARD = 0x49,
305     AVRCP_OPERATION_ID_EJECT = 0x4A,
306     AVRCP_OPERATION_ID_FORWARD = 0x4B,
307     AVRCP_OPERATION_ID_BACKWARD = 0x4C,
308     AVRCP_OPERATION_ID_RESERVED_4 = 0x4D,
309 
310     AVRCP_OPERATION_ID_ANGLE = 0x50,
311     AVRCP_OPERATION_ID_SUBPICTURE = 0x51,
312     AVRCP_OPERATION_ID_RESERVED_5 = 0x52,
313 
314     AVRCP_OPERATION_ID_F1 = 0x71,
315     AVRCP_OPERATION_ID_F2 = 0x72,
316     AVRCP_OPERATION_ID_F3 = 0x73,
317     AVRCP_OPERATION_ID_F4 = 0x74,
318     AVRCP_OPERATION_ID_F5 = 0x75,
319     AVRCP_OPERATION_ID_RESERVED_6 = 0x76
320 } avrcp_operation_id_t;
321 
322 typedef enum{
323     AVRCP_PLAYBACK_STATUS_STOPPED = 0x00,
324     AVRCP_PLAYBACK_STATUS_PLAYING,
325     AVRCP_PLAYBACK_STATUS_PAUSED,
326     AVRCP_PLAYBACK_STATUS_FWD_SEEK,
327     AVRCP_PLAYBACK_STATUS_REV_SEEK,
328     AVRCP_PLAYBACK_STATUS_ERROR = 0xFF
329 } avrcp_playback_status_t;
330 
331 typedef enum {
332     AVRCP_BATTERY_STATUS_NORMAL = 0x00, // Battery operation is in normal state
333     AVRCP_BATTERY_STATUS_WARNING,       // unable to operate soon. Is provided when the battery level is going down.
334     AVRCP_BATTERY_STATUS_CRITICAL,      // can not operate any more. Is provided when the battery level is going down.
335     AVRCP_BATTERY_STATUS_EXTERNAL,      // Plugged to external power supply
336     AVRCP_BATTERY_STATUS_FULL_CHARGE    // when the device is completely charged from the external power supply
337 } avrcp_battery_status_t;
338 
339 typedef enum {
340     AVRCP_SYSTEM_STATUS_POWER_ON = 0x00,
341     AVRCP_SYSTEM_STATUS_POWER_OFF,
342     AVRCP_SYSTEM_STATUS_UNPLUGGED
343 } avrcp_system_status_t;
344 
345 typedef enum {
346     AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_ILLEGAL = 0x00,       // ValueIDs with descriptions:
347     AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_EQUALIZER_STATUS,     // 1 - off, 2 - on
348     AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_REPEAT_MODE_STATUS,   // 1 - off, 2 - single track repeat, 3 - all tracks repeat, 4 - group repeat
349     AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_SHUFFLE_STATUS,       // 1 - off, 2 - all tracks shuffle , 3 - group shuffle
350     AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_SCAN_STATUS           // 1 - off, 2 - all tracks scan    , 3 - group scan
351 } avrcp_player_application_setting_attribute_id_t;
352 
353 typedef enum {
354     AVCTP_CONNECTION_IDLE,
355     AVCTP_CONNECTION_W2_SEND_SDP_QUERY,
356     AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE,
357     AVCTP_CONNECTION_W4_ERTM_CONFIGURATION,
358     AVCTP_CONNECTION_W4_L2CAP_CONNECTED,
359     AVCTP_CONNECTION_W2_L2CAP_RETRY,
360     AVCTP_CONNECTION_OPENED,
361     AVCTP_W2_SEND_PRESS_COMMAND,
362     AVCTP_W2_SEND_RELEASE_COMMAND,
363     AVCTP_W4_STOP,
364     AVCTP_W2_SEND_COMMAND,
365     AVCTP_W2_SEND_RESPONSE,
366     AVCTP_W2_RECEIVE_PRESS_RESPONSE,
367     AVCTP_W2_RECEIVE_RESPONSE,
368     AVCTP_W2_SEND_FRAGMENTED_COMMAND,
369 } avctp_connection_state_t;
370 
371 typedef struct {
372     uint16_t len;
373     uint8_t  * value;
374 } avrcp_now_playing_info_item_t;
375 
376 typedef struct {
377     uint8_t track_id[8];
378     uint16_t track_nr;
379     char * title;
380     char * artist;
381     char * album;
382     char * genre;
383     uint32_t song_length_ms;
384     uint32_t song_position_ms;
385 } avrcp_track_t;
386 
387 typedef enum {
388     AVRCP_PARSER_GET_ATTRIBUTE_HEADER = 0,       // 8 bytes
389     AVRCP_PARSER_GET_ATTRIBUTE_VALUE,
390     AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE
391 } avrcp_parser_state_t;
392 
393 
394 typedef enum{
395     AVRCP_CONTROLLER = 0,
396     AVRCP_TARGET
397 } avrcp_role_t;
398 
399 typedef enum {
400     AVRCP_SHUFFLE_MODE_INVALID,
401     AVRCP_SHUFFLE_MODE_OFF,
402     AVRCP_SHUFFLE_MODE_ALL_TRACKS,
403     AVRCP_SHUFFLE_MODE_GROUP
404 } avrcp_shuffle_mode_t;
405 
406 typedef enum {
407     AVRCP_REPEAT_MODE_INVALID,
408     AVRCP_REPEAT_MODE_OFF,
409     AVRCP_REPEAT_MODE_SINGLE_TRACK,
410     AVRCP_REPEAT_MODE_ALL_TRACKS,
411     AVRCP_REPEAT_MODE_GROUP
412 } avrcp_repeat_mode_t;
413 
414 typedef enum {
415     RFC2978_CHARSET_MIB_UTF8 = 106
416 } rfc2978_charset_mib_enumid_t;
417 
418 typedef enum {
419     AVRCP_BROWSING_MEDIA_PLAYER_LIST = 0x00,
420     AVRCP_BROWSING_MEDIA_PLAYER_VIRTUAL_FILESYSTEM,
421     AVRCP_BROWSING_SEARCH,
422     AVRCP_BROWSING_NOW_PLAYING
423 } avrcp_browsing_scope_t;
424 
425 
426 
427 
428 // BROWSING
429 typedef struct {
430     uint16_t l2cap_browsing_cid;
431 
432     avctp_connection_state_t state;
433     bool     wait_to_send;
434     uint8_t  transaction_label;
435     // used for fragmentation
436     uint8_t  num_packets;
437     uint16_t bytes_to_send;
438 
439     uint8_t *ertm_buffer;
440     uint32_t ertm_buffer_size;
441     l2cap_ertm_config_t ertm_config;
442 
443     // players
444     uint8_t  set_browsed_player_id;
445     uint16_t browsed_player_id;
446 
447     avrcp_browsing_scope_t  scope;
448     uint8_t  folder_uid[8]; // or media element
449     uint16_t uid_counter;
450 
451     // get folder item
452     uint8_t  get_folder_items;
453     uint32_t start_item;
454     uint32_t end_item;
455     uint32_t attr_bitmap;
456 
457     // item attrs
458     uint8_t get_item_attributes;
459 
460     // change_path
461     uint8_t  change_path;
462     uint8_t  direction;
463 
464     // search str
465     uint16_t search_str_len;
466     uint8_t  search_str[20];
467     uint8_t  search;
468 
469     // get_item_attributes
470     uint8_t  get_total_nr_items;
471     avrcp_browsing_scope_t get_total_nr_items_scope;
472 
473     avrcp_pdu_id_t pdu_id;
474     uint8_t browsing_status;
475     uint16_t num_items;
476 
477     avrcp_parser_state_t parser_state;
478     uint8_t  parser_attribute_header[AVRCP_BROWSING_ITEM_HEADER_LEN];
479     uint8_t  parser_attribute_header_pos;
480     uint8_t  parsed_attribute_value[AVRCP_MAX_ATTRIBUTTE_SIZE];
481     uint16_t parsed_attribute_value_len;
482     uint16_t parsed_attribute_value_offset;
483     uint8_t  parsed_num_attributes;
484 
485     // get folder items data
486     uint8_t * attr_list;
487     uint16_t attr_list_size;
488     // command
489     // uint8_t transaction_label;
490     avrcp_command_opcode_t command_opcode;
491     avrcp_command_type_t command_type;
492     avrcp_subunit_type_t subunit_type;
493     avrcp_subunit_id_t   subunit_id;
494     avrcp_packet_type_t  packet_type;
495     uint8_t cmd_operands[200];
496     uint8_t cmd_operands_length;
497 
498     bool incoming_declined;
499 } avrcp_browsing_connection_t;
500 
501 typedef struct {
502     btstack_linked_item_t    item;
503 
504     avrcp_role_t role;
505     bd_addr_t remote_addr;
506     uint16_t avrcp_l2cap_psm;
507     uint16_t l2cap_signaling_cid;
508     uint16_t l2cap_mtu;
509     uint16_t avrcp_cid;
510     hci_con_handle_t con_handle;
511 
512     bool incoming_declined;
513 
514     uint16_t avrcp_browsing_cid;
515     uint16_t browsing_l2cap_psm;
516     uint16_t browsing_version;
517 
518     avrcp_browsing_connection_t * browsing_connection;
519 
520     avctp_connection_state_t state;
521     bool wait_to_send;
522 
523     // PID check
524     uint8_t reject_transport_header;
525     uint8_t transport_header;
526     uint16_t invalid_pid;
527 
528     // transaction id
529     uint8_t transaction_id_counter;
530 
531     // limit number of pending commands to transaction id window size
532     uint8_t last_confirmed_transaction_id;
533 
534     // command
535     uint8_t transaction_id;
536 
537     avrcp_command_opcode_t command_opcode;
538     avrcp_command_type_t command_type;
539     avrcp_subunit_type_t subunit_type;
540     avrcp_subunit_id_t   subunit_id;
541     avrcp_packet_type_t  packet_type;
542 
543     // regular commands
544     uint8_t cmd_operands[20];
545     uint8_t cmd_operands_length;
546 
547     // long/fragmented commands
548     const uint8_t * cmd_operands_fragmented_buffer;
549     uint16_t  cmd_operands_fragmented_pos;
550     uint16_t  cmd_operands_fragmented_len;
551 
552     btstack_timer_source_t retry_timer;
553     btstack_timer_source_t press_and_hold_cmd_timer;
554     bool     press_and_hold_cmd_active;
555     bool     press_and_hold_cmd_release;
556 
557     uint16_t notifications_enabled;
558     uint16_t initial_status_reported;
559 
560     uint16_t target_supported_notifications;
561     bool     target_supported_notifications_queried;
562     bool     target_supported_notifications_suppress_emit_result;
563     const uint32_t *target_supported_companies;
564     uint8_t  target_supported_companies_num;
565 
566     uint16_t notifications_to_register;
567     uint16_t notifications_to_deregister;
568     uint8_t  notifications_transaction_label[AVRCP_NOTIFICATION_EVENT_MAX_VALUE+1];
569 
570     avrcp_subunit_type_t unit_type;
571     uint32_t company_id;
572     avrcp_subunit_type_t subunit_info_type;
573     const uint8_t * subunit_info_data;
574     uint16_t subunit_info_data_size;
575 
576     avrcp_now_playing_info_item_t now_playing_info[AVRCP_MEDIA_ATTR_COUNT];
577     uint8_t  track_id[8];
578     uint32_t song_length_ms;
579     uint32_t song_position_ms;
580     int total_tracks;
581     int track_nr;
582     uint8_t track_selected;
583     uint8_t track_changed;
584 
585     avrcp_playback_status_t playback_status;
586     uint8_t playback_status_changed;
587 
588     uint8_t playing_content_changed;
589 
590     avrcp_battery_status_t battery_status;
591     uint8_t battery_status_changed;
592     uint8_t absolute_volume;
593     uint8_t notify_absolute_volume_changed;
594 
595     uint8_t now_playing_info_response;
596     uint8_t now_playing_info_attr_bitmap;
597     uint8_t abort_continue_response;
598 
599     // used for fragmentation
600     avrcp_media_attribute_id_t next_attr_id;
601 
602     avrcp_parser_state_t parser_state;
603     uint8_t  parser_attribute_header[AVRCP_ATTRIBUTE_HEADER_LEN];
604     uint8_t  parser_attribute_header_pos;
605 
606     uint16_t list_size;
607     uint16_t list_offset;
608     uint8_t  attribute_value[AVRCP_MAX_ATTRIBUTTE_SIZE];
609     uint16_t attribute_value_len;
610     uint16_t attribute_value_offset;
611 
612     uint32_t attribute_id;
613 
614     uint8_t  num_attributes;
615     uint8_t  num_parsed_attributes;
616 
617     uint8_t addressed_player_changed;
618     uint16_t addressed_player_id;
619     uint16_t uid_counter;
620     // PTS requires definition of max num fragments
621     uint8_t max_num_fragments;
622     uint8_t num_received_fragments;
623 
624     uint8_t accept_response;
625 
626 #ifdef ENABLE_AVCTP_FRAGMENTATION
627     uint16_t avctp_reassembly_size;
628     uint8_t  avctp_reassembly_buffer[200];
629 #endif
630 
631 } avrcp_connection_t;
632 
633 typedef struct {
634     avrcp_role_t role;
635     btstack_packet_handler_t avrcp_callback;
636     btstack_packet_handler_t packet_handler;
637 
638     bool (*set_addressed_player_callback)(uint16_t player_id);
639 
640     btstack_packet_handler_t browsing_avrcp_callback;
641     btstack_packet_handler_t browsing_packet_handler;
642 } avrcp_context_t;
643 
644 
645 const char * avrcp_subunit2str(uint16_t index);
646 const char * avrcp_event2str(uint16_t index);
647 const char * avrcp_operation2str(uint8_t index);
648 const char * avrcp_attribute2str(uint8_t index);
649 const char * avrcp_play_status2str(uint8_t index);
650 const char * avrcp_ctype2str(uint8_t index);
651 const char * avrcp_repeat2str(uint8_t index);
652 const char * avrcp_shuffle2str(uint8_t index);
653 
654 
655 void avrcp_register_controller_packet_handler(btstack_packet_handler_t avrcp_controller_packet_handler);
656 void avrcp_register_target_packet_handler(btstack_packet_handler_t avrcp_target_packet_handler);
657 
658 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size);
659 
660 avrcp_connection_t * avrcp_get_connection_for_l2cap_signaling_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid);
661 avrcp_connection_t * avrcp_get_connection_for_avrcp_cid_for_role(avrcp_role_t role, uint16_t avrcp_cid);
662 avrcp_connection_t * avrcp_get_connection_for_bd_addr_for_role(avrcp_role_t role, bd_addr_t addr);
663 
664 avrcp_connection_t * avrcp_get_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid);
665 avrcp_connection_t * avrcp_get_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid);
666 avrcp_browsing_connection_t * avrcp_get_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid);
667 
668 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid);
669 uint16_t avrcp_get_next_cid(avrcp_role_t role);
670 btstack_linked_list_t avrcp_get_connections(void);
671 
672 uint16_t avrcp_sdp_query_browsing_l2cap_psm(void);
673 void avrcp_handle_sdp_client_query_attribute_value(uint8_t *packet);
674 avrcp_connection_t * get_avrcp_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid);
675 avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid);
676 avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid);
677 // SDP query
678 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);
679 
680 
681 /* API_START */
682 
683 /**
684  * @brief Set up AVRCP service
685  */
686 void avrcp_init(void);
687 
688 /**
689  * @brief Register callback for the AVRCP Controller client.
690  * @param callback
691  */
692 void avrcp_register_packet_handler(btstack_packet_handler_t callback);
693 
694 /**
695  * @brief   Connect to AVRCP service on a remote device, emits AVRCP_SUBEVENT_CONNECTION_ESTABLISHED with status
696  * @param   remote_addr
697  * @param   avrcp_cid  outgoing parameter, valid if status == ERROR_CODE_SUCCESS
698  * @return status
699  */
700 uint8_t avrcp_connect(bd_addr_t remote_addr, uint16_t * avrcp_cid);
701 
702 /**
703  * @brief   Disconnect from AVRCP service
704  * @param   avrcp_cid
705  * @return status
706  */
707 uint8_t avrcp_disconnect(uint16_t avrcp_cid);
708 
709 /**
710  * @brief De-Init AVRCP
711  */
712 void avrcp_deinit(void);
713 
714 /* API_END */
715 
716 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
717 void avrcp_init_fuzz(void);
718 void avrcp_packet_handler_fuzz(uint8_t *packet, uint16_t size);
719 #endif
720 
721 
722 #if defined __cplusplus
723 }
724 #endif
725 
726 #endif // AVRCP_H
727