xref: /btstack/src/classic/avrcp.h (revision dc01ccc6b23fb84d78b2e6c1f4a02f3559d9bd6a)
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 #define AVRCP_MAX_ATTRIBUTTE_SIZE 100
59 #define AVRCP_ATTRIBUTE_HEADER_LEN  8
60 
61 typedef enum {
62     AVRCP_STATUS_INVALID_COMMAND = 0,           // sent if TG received a PDU that it did not understand.
63     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.
64     AVRCP_STATUS_SPECIFIED_PARAMETER_NOT_FOUND, // sent if the parameter ID is understood, but content is wrong or corrupted.
65     AVRCP_STATUS_INTERNAL_ERROR,                // sent if there are error conditions not covered by a more specific error code.
66     AVRCP_STATUS_SUCCESS,                       // sent if the operation was successful.
67     AVRCP_STATUS_UID_CHANGED,                   // sent if the UIDs on the device have changed.
68     AVRCP_STATUS_RESERVED_6,
69     AVRCP_STATUS_INVALID_DIRECTION,             // The Direction parameter is invalid. Valid for command: Change Path
70     AVRCP_STATUS_NOT_A_DIRECTORY,               // The UID provided does not refer to a folder item. Valid for command: Change Path
71     AVRCP_STATUS_DOES_NOT_EXIST,                // The UID provided does not refer to any currently valid. Valid for command: Change Path, PlayItem, AddToNowPlaying, GetItemAttributes
72     AVRCP_STATUS_INVALID_SCOPE,                 // The scope parameter is invalid. Valid for command: GetFolderItems, PlayItem, AddToNowPlayer, GetItemAttributes,
73     AVRCP_STATUS_RANGE_OUT_OF_BOUNDS,           // The start of range provided is not valid. Valid for command: GetFolderItems
74     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
75     AVRCP_STATUS_MEDIA_IN_USE,                  // The media is not able to be used for this operation at this time. Valid for command: PlayItem, AddToNowPlaying
76     AVRCP_STATUS_NOW_PLAYING_LIST_FULL,         // No more items can be added to the Now Playing List. Valid for command: AddToNowPlaying
77     AVRCP_STATUS_SEARCH_NOT_SUPPORTED,          // The Browsed Media Player does not support search. Valid for command: Search
78     AVRCP_STATUS_SEARCH_IN_PROGRESS,            // A search operation is already in progress. Valid for command: Search
79     AVRCP_STATUS_INVALID_PLAYER_ID,             // The specified Player Id does not refer to a valid player. Valid for command: SetAddressedPlayer, SetBrowsedPlayer
80     AVRCP_STATUS_PLAYER_NOT_BROWSABLE,          // The Player Id supplied refers to a Media Player which does not support browsing. Valid for command: SetBrowsedPlayer
81     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
82     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
83     AVRCP_STATUS_NO_AVAILABLE_PLAYERS,
84     AVRCP_STATUS_ADDRESSED_PLAYER_CHANGED,       // Valid for command: Register Notification
85     AVRCP_STATUS_RESERVED                       // 0x17 - 0xFF
86 } avrcp_status_code_t;
87 
88 typedef enum {
89     AVRCP_SINGLE_PACKET= 0,
90     AVRCP_START_PACKET    ,
91     AVRCP_CONTINUE_PACKET ,
92     AVRCP_END_PACKET
93 } avrcp_packet_type_t;
94 
95 typedef enum {
96     AVRCP_COMMAND_FRAME = 0,
97     AVRCP_RESPONSE_FRAME
98 } avrcp_frame_type_t;
99 
100 
101 typedef enum {
102     AVRCP_CAPABILITY_ID_COMPANY = 0x02,
103     AVRCP_CAPABILITY_ID_EVENT = 0x03
104 } avrcp_capability_id_t;
105 
106 typedef enum {
107     AVRCP_MEDIA_ATTR_NONE = 0,
108     AVRCP_MEDIA_ATTR_TITLE,
109     AVRCP_MEDIA_ATTR_ARTIST,
110     AVRCP_MEDIA_ATTR_ALBUM,
111     AVRCP_MEDIA_ATTR_TRACK,
112     AVRCP_MEDIA_ATTR_TOTAL_TRACKS,
113     AVRCP_MEDIA_ATTR_GENRE,
114     AVRCP_MEDIA_ATTR_SONG_LENGTH
115 } avrcp_media_attribute_id_t;
116 
117 typedef enum {
118     AVRCP_PDU_ID_GET_CAPABILITIES = 0x10,
119     AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue = 0x13,
120     AVRCP_PDU_ID_SetPlayerApplicationSettingValue = 0x14,
121     AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,
122     AVRCP_PDU_ID_GET_PLAY_STATUS = 0x30,
123     AVRCP_PDU_ID_REGISTER_NOTIFICATION = 0x31,
124     AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE = 0x40,
125     AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE = 0x41,
126     AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50,
127     AVRCP_PDU_ID_UNDEFINED = 0xFF
128 } avrcp_pdu_id_t;
129 
130 typedef enum {
131     AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED = 0x01,            // Change in playback status of the current track.
132     AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED = 0x02,                      // Change of current track
133     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END = 0x03,                  // Reached end of a track
134     AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START = 0x04,                // Reached start of a track
135     AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED = 0x05,               // Change in playback position. Returned after the specified playback notification change notification interval
136     AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED = 0x06,                // Change in battery status
137     AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED = 0x07,              // Change in system status
138     AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED = 0x08, // Change in player application setting
139     AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED = 0x09,        // The content of the Now Playing list has changed, see 6.9.5.
140     AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED = 0x0a,          // The available players have changed, see 6.9.4.
141     AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED = 0x0b,           // The Addressed Player has been changed, see 6.9.2.
142     AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED = 0x0c,                       // The UIDs have changed, see 6.10.3.3.
143     AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED = 0x0d                      // The volume has been changed locally on the TG, see 6.13.3.
144 } avrcp_notification_event_id_t;
145 
146 // control command response: accepted, rejected, interim
147 // status  command response: not implemented, rejected, in transition, stable
148 // notify  command response: not implemented, rejected, changed
149 
150 typedef enum {
151     AVRCP_CTYPE_CONTROL = 0,
152     AVRCP_CTYPE_STATUS,
153     AVRCP_CTYPE_SPECIFIC_INQUIRY,
154     AVRCP_CTYPE_NOTIFY,
155     AVRCP_CTYPE_GENERAL_INQUIRY,
156     AVRCP_CTYPE_RESERVED5,
157     AVRCP_CTYPE_RESERVED6,
158     AVRCP_CTYPE_RESERVED7,
159     AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED = 8,
160     AVRCP_CTYPE_RESPONSE_ACCEPTED,
161     AVRCP_CTYPE_RESPONSE_REJECTED,
162     AVRCP_CTYPE_RESPONSE_IN_TRANSITION, // target state is in transition. A subsequent STATUS command, may result in the return of a STABLE status
163     AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE,
164     AVRCP_CTYPE_RESPONSE_CHANGED_STABLE,
165     AVRCP_CTYPE_RESPONSE_RESERVED,
166     AVRCP_CTYPE_RESPONSE_INTERIM            // target is unable to respond with either ACCEPTED or REJECTED within 100 millisecond
167 } avrcp_command_type_t;
168 
169 typedef enum {
170     AVRCP_SUBUNIT_TYPE_MONITOR = 0,
171     AVRCP_SUBUNIT_TYPE_AUDIO = 1,
172     AVRCP_SUBUNIT_TYPE_PRINTER,
173     AVRCP_SUBUNIT_TYPE_DISC,
174     AVRCP_SUBUNIT_TYPE_TAPE_RECORDER_PLAYER,
175     AVRCP_SUBUNIT_TYPE_TUNER,
176     AVRCP_SUBUNIT_TYPE_CA,
177     AVRCP_SUBUNIT_TYPE_CAMERA,
178     AVRCP_SUBUNIT_TYPE_RESERVED,
179     AVRCP_SUBUNIT_TYPE_PANEL = 9,
180     AVRCP_SUBUNIT_TYPE_BULLETIN_BOARD,
181     AVRCP_SUBUNIT_TYPE_CAMERA_STORAGE,
182     AVRCP_SUBUNIT_TYPE_VENDOR_UNIQUE = 0x1C,
183     AVRCP_SUBUNIT_TYPE_RESERVED_FOR_ALL_SUBUNIT_TYPES,
184     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].
185                                               // Further extension is possible when the value of extended_unit_type is FF16, in which case another byte will be added.
186     AVRCP_SUBUNIT_TYPE_UNIT = 0x1F
187 } avrcp_subunit_type_t;
188 
189 typedef enum {
190     AVRCP_SUBUNIT_ID = 0,
191     AVRCP_SUBUNIT_ID_IGNORE = 7
192 } avrcp_subunit_id_t;
193 
194 typedef enum {
195     AVRCP_CMD_OPCODE_VENDOR_DEPENDENT = 0x00,
196     // AVRCP_CMD_OPCODE_RESERVE = 0x01,
197     AVRCP_CMD_OPCODE_UNIT_INFO = 0x30,
198     AVRCP_CMD_OPCODE_SUBUNIT_INFO = 0x31,
199     AVRCP_CMD_OPCODE_PASS_THROUGH = 0x7C,
200     // AVRCP_CMD_OPCODE_VERSION = 0xB0,
201     // AVRCP_CMD_OPCODE_POWER = 0xB2,
202     AVRCP_CMD_OPCODE_UNDEFINED = 0xFF
203 } avrcp_command_opcode_t;
204 
205 typedef enum {
206     AVRCP_OPERATION_ID_SKIP = 0x3C,
207     AVRCP_OPERATION_ID_VOLUME_UP = 0x41,
208     AVRCP_OPERATION_ID_VOLUME_DOWN = 0x42,
209     AVRCP_OPERATION_ID_MUTE = 0x43,
210 
211     AVRCP_OPERATION_ID_PLAY = 0x44,
212     AVRCP_OPERATION_ID_STOP = 0x45,
213     AVRCP_OPERATION_ID_PAUSE = 0x46,
214     AVRCP_OPERATION_ID_REWIND = 0x48,
215     AVRCP_OPERATION_ID_FAST_FORWARD = 0x49,
216     AVRCP_OPERATION_ID_FORWARD = 0x4B,
217     AVRCP_OPERATION_ID_BACKWARD = 0x4C,
218     AVRCP_OPERATION_ID_UNDEFINED = 0xFF
219 } avrcp_operation_id_t;
220 
221 typedef enum{
222     AVRCP_PLAYBACK_STATUS_STOPPED = 0x00,
223     AVRCP_PLAYBACK_STATUS_PLAYING,
224     AVRCP_PLAYBACK_STATUS_PAUSED,
225     AVRCP_PLAYBACK_STATUS_FWD_SEEK,
226     AVRCP_PLAYBACK_STATUS_REV_SEEK,
227     AVRCP_PLAYBACK_STATUS_ERROR = 0xFF
228 } avrcp_playback_status_t;
229 
230 typedef enum{
231     AVRCP_BATTERY_STATUS_NORMAL = 0x00, // Battery operation is in normal state
232     AVRCP_BATTERY_STATUS_WARNING,       // unable to operate soon. Is provided when the battery level is going down.
233     AVRCP_BATTERY_STATUS_CRITICAL,      // can not operate any more. Is provided when the battery level is going down.
234     AVRCP_BATTERY_STATUS_EXTERNAL,      // Plugged to external power supply
235     AVRCP_BATTERY_STATUS_FULL_CHARGE    // when the device is completely charged from the external power supply
236 } avrcp_battery_status_t;
237 
238 
239 typedef enum {
240     AVCTP_CONNECTION_IDLE,
241     AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE,
242     AVCTP_CONNECTION_W4_L2CAP_CONNECTED,
243     AVCTP_CONNECTION_OPENED,
244     AVCTP_W2_SEND_PRESS_COMMAND,
245     AVCTP_W2_SEND_RELEASE_COMMAND,
246     AVCTP_W4_STOP,
247     AVCTP_W2_SEND_COMMAND,
248     AVCTP_W2_SEND_RESPONSE,
249     AVCTP_W2_RECEIVE_PRESS_RESPONSE,
250     AVCTP_W2_RECEIVE_RESPONSE
251 } avctp_connection_state_t;
252 
253 typedef struct {
254     uint16_t len;
255     uint8_t  * value;
256 } avrcp_now_playing_info_item_t;
257 
258 typedef struct {
259     uint8_t track_id[8];
260     uint16_t track_nr;
261     char * title;
262     char * artist;
263     char * album;
264     char * genre;
265     uint32_t song_length_ms;
266     uint32_t song_position_ms;
267 } avrcp_track_t;
268 
269 typedef enum {
270     AVRCP_PARSER_IDLE = 0,
271     AVRCP_PARSER_GET_ATTRIBUTE_HEADER,       // 8 bytes
272     AVRCP_PARSER_GET_ATTRIBUTE_VALUE,
273     AVRCP_PARSER_IGNORE_ATTRIBUTE_VALUE
274 } avrcp_parser_state_t;
275 
276 typedef struct {
277     btstack_linked_item_t    item;
278     bd_addr_t remote_addr;
279     uint16_t l2cap_signaling_cid;
280     uint16_t avrcp_cid;
281 
282     avctp_connection_state_t state;
283     uint8_t wait_to_send;
284 
285     // command
286     uint8_t transaction_label;
287     avrcp_command_opcode_t command_opcode;
288     avrcp_command_type_t command_type;
289     avrcp_subunit_type_t subunit_type;
290     avrcp_subunit_id_t   subunit_id;
291     avrcp_packet_type_t  packet_type;
292 
293     uint8_t cmd_operands[20];
294     uint8_t cmd_operands_length;
295     btstack_timer_source_t press_and_hold_cmd_timer;
296     uint8_t  continuous_fast_forward_cmd;
297     uint16_t notifications_enabled;
298     uint16_t notifications_to_register;
299     uint16_t notifications_to_deregister;
300 
301     avrcp_subunit_type_t unit_type;
302     uint32_t company_id;
303     avrcp_subunit_type_t subunit_info_type;
304     const uint8_t * subunit_info_data;
305     uint16_t subunit_info_data_size;
306 
307     avrcp_now_playing_info_item_t now_playing_info[AVRCP_MEDIA_ATTR_COUNT];
308     uint8_t  track_id[8];
309     uint32_t song_length_ms;
310     uint32_t song_position_ms;
311     int total_tracks;
312     int track_nr;
313     uint8_t track_selected;
314     uint8_t track_changed;
315 
316     avrcp_playback_status_t playback_status;
317     uint8_t playback_status_changed;
318 
319     uint8_t playing_content_changed;
320 
321     avrcp_battery_status_t battery_status;
322     uint8_t battery_status_changed;
323     uint8_t volume_percentage;
324     uint8_t volume_percentage_changed;
325     uint8_t now_playing_info_response;
326     uint8_t now_playing_info_attr_bitmap;
327     uint8_t abort_continue_response;
328 
329     // used for fragmentation
330     avrcp_media_attribute_id_t next_attr_id;
331 
332     avrcp_parser_state_t parser_state;
333     uint8_t  parser_attribute_header[AVRCP_ATTRIBUTE_HEADER_LEN];
334     uint8_t  parser_attribute_header_pos;
335 
336     uint16_t list_size;
337     uint16_t list_offset;
338     uint8_t  attribute_value[AVRCP_MAX_ATTRIBUTTE_SIZE];
339     uint16_t attribute_value_len;
340     uint16_t attribute_value_offset;
341 
342     uint32_t attribute_id;
343 
344     uint8_t  num_attributes;
345     uint8_t  num_parsed_attributes;
346 } avrcp_connection_t;
347 
348 typedef enum {
349     AVRCP_SHUFFLE_MODE_INVALID,
350     AVRCP_SHUFFLE_MODE_OFF,
351     AVRCP_SHUFFLE_MODE_ALL_TRACKS,
352     AVRCP_SHUFFLE_MODE_GROUP
353 } avrcp_shuffle_mode_t;
354 
355 typedef enum {
356     AVRCP_REPEAT_MODE_INVALID,
357     AVRCP_REPEAT_MODE_OFF,
358     AVRCP_REPEAT_MODE_SINGLE_TRACK,
359     AVRCP_REPEAT_MODE_ALL_TRACKS,
360     AVRCP_REPEAT_MODE_GROUP
361 } avrcp_repeat_mode_t;
362 
363 typedef enum{
364     AVRCP_CONTROLLER = 0,
365     AVRCP_TARGET
366 } avrcp_role_t;
367 
368 typedef enum {
369     UTF8 = 106
370 } rfc2978_charset_mib_enumid_t;
371 
372 typedef struct {
373     avrcp_role_t role;
374     btstack_linked_list_t connections;
375     btstack_packet_handler_t avrcp_callback;
376     btstack_packet_handler_t packet_handler;
377 
378     // SDP query
379     uint16_t avrcp_cid;
380     uint16_t avrcp_l2cap_psm;
381     uint16_t avrcp_version;
382     uint16_t avrcp_browsing_l2cap_psm;
383     uint16_t avrcp_browsing_version;
384     uint8_t  role_supported;
385 } avrcp_context_t;
386 
387 const char * avrcp_subunit2str(uint16_t index);
388 const char * avrcp_event2str(uint16_t index);
389 const char * avrcp_operation2str(uint8_t index);
390 const char * avrcp_attribute2str(uint8_t index);
391 const char * avrcp_play_status2str(uint8_t index);
392 const char * avrcp_ctype2str(uint8_t index);
393 const char * avrcp_repeat2str(uint8_t index);
394 const char * avrcp_shuffle2str(uint8_t index);
395 
396 void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context);
397 
398 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);
399 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid);
400 void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status);
401 void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid);
402 
403 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size);
404 avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context);
405 avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context);
406 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid);
407 
408 #if defined __cplusplus
409 }
410 #endif
411 
412 #endif // __AVRCP_H