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_NONE = 0, 106 AVRCP_MEDIA_ATTR_TITLE, 107 AVRCP_MEDIA_ATTR_ARTIST, 108 AVRCP_MEDIA_ATTR_ALBUM, 109 AVRCP_MEDIA_ATTR_TRACK, 110 AVRCP_MEDIA_ATTR_TOTAL_TRACKS, 111 AVRCP_MEDIA_ATTR_GENRE, 112 AVRCP_MEDIA_ATTR_SONG_LENGTH 113 } avrcp_media_attribute_id_t; 114 115 typedef enum { 116 AVRCP_PDU_ID_GET_CAPABILITIES = 0x10, 117 AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue = 0x13, 118 AVRCP_PDU_ID_SetPlayerApplicationSettingValue = 0x14, 119 AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20, 120 AVRCP_PDU_ID_GET_PLAY_STATUS = 0x30, 121 AVRCP_PDU_ID_REGISTER_NOTIFICATION = 0x31, 122 AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE = 0x40, 123 AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50, 124 AVRCP_PDU_ID_UNDEFINED = 0xFF 125 } avrcp_pdu_id_t; 126 127 typedef enum { 128 AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED = 0x01, // Change in playback status of the current track. 129 AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED = 0x02, // Change of current track 130 AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END = 0x03, // Reached end of a track 131 AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START = 0x04, // Reached start of a track 132 AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED = 0x05, // Change in playback position. Returned after the specified playback notification change notification interval 133 AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED = 0x06, // Change in battery status 134 AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED = 0x07, // Change in system status 135 AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED = 0x08, // Change in player application setting 136 AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED = 0x09, // The content of the Now Playing list has changed, see 6.9.5. 137 AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED = 0x0a, // The available players have changed, see 6.9.4. 138 AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED = 0x0b, // The Addressed Player has been changed, see 6.9.2. 139 AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED = 0x0c, // The UIDs have changed, see 6.10.3.3. 140 AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED = 0x0d // The volume has been changed locally on the TG, see 6.13.3. 141 } avrcp_notification_event_id_t; 142 143 // control command response: accepted, rejected, interim 144 // status command response: not implemented, rejected, in transition, stable 145 // notify command response: not implemented, rejected, changed 146 147 typedef enum { 148 AVRCP_CTYPE_CONTROL = 0, 149 AVRCP_CTYPE_STATUS, 150 AVRCP_CTYPE_SPECIFIC_INQUIRY, 151 AVRCP_CTYPE_NOTIFY, 152 AVRCP_CTYPE_GENERAL_INQUIRY, 153 AVRCP_CTYPE_RESERVED5, 154 AVRCP_CTYPE_RESERVED6, 155 AVRCP_CTYPE_RESERVED7, 156 AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED = 8, 157 AVRCP_CTYPE_RESPONSE_ACCEPTED, 158 AVRCP_CTYPE_RESPONSE_REJECTED, 159 AVRCP_CTYPE_RESPONSE_IN_TRANSITION, // target state is in transition. A subsequent STATUS command, may result in the return of a STABLE status 160 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 161 AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, 162 AVRCP_CTYPE_RESPONSE_RESERVED, 163 AVRCP_CTYPE_RESPONSE_INTERIM // target is unable to respond with either ACCEPTED or REJECTED within 100 millisecond 164 } avrcp_command_type_t; 165 166 typedef enum { 167 AVRCP_SUBUNIT_TYPE_MONITOR = 0, 168 AVRCP_SUBUNIT_TYPE_AUDIO = 1, 169 AVRCP_SUBUNIT_TYPE_PRINTER, 170 AVRCP_SUBUNIT_TYPE_DISC, 171 AVRCP_SUBUNIT_TYPE_TAPE_RECORDER_PLAYER, 172 AVRCP_SUBUNIT_TYPE_TUNER, 173 AVRCP_SUBUNIT_TYPE_CA, 174 AVRCP_SUBUNIT_TYPE_CAMERA, 175 AVRCP_SUBUNIT_TYPE_RESERVED, 176 AVRCP_SUBUNIT_TYPE_PANEL = 9, 177 AVRCP_SUBUNIT_TYPE_BULLETIN_BOARD, 178 AVRCP_SUBUNIT_TYPE_CAMERA_STORAGE, 179 AVRCP_SUBUNIT_TYPE_VENDOR_UNIQUE = 0x1C, 180 AVRCP_SUBUNIT_TYPE_RESERVED_FOR_ALL_SUBUNIT_TYPES, 181 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]. 182 // Further extension is possible when the value of extended_unit_type is FF16, in which case another byte will be added. 183 AVRCP_SUBUNIT_TYPE_UNIT = 0x1F 184 } avrcp_subunit_type_t; 185 186 typedef enum { 187 AVRCP_SUBUNIT_ID = 0, 188 AVRCP_SUBUNIT_ID_IGNORE = 7 189 } avrcp_subunit_id_t; 190 191 typedef enum { 192 AVRCP_CMD_OPCODE_VENDOR_DEPENDENT = 0x00, 193 // AVRCP_CMD_OPCODE_RESERVE = 0x01, 194 AVRCP_CMD_OPCODE_UNIT_INFO = 0x30, 195 AVRCP_CMD_OPCODE_SUBUNIT_INFO = 0x31, 196 AVRCP_CMD_OPCODE_PASS_THROUGH = 0x7C, 197 // AVRCP_CMD_OPCODE_VERSION = 0xB0, 198 // AVRCP_CMD_OPCODE_POWER = 0xB2, 199 AVRCP_CMD_OPCODE_UNDEFINED = 0xFF 200 } avrcp_command_opcode_t; 201 202 typedef enum { 203 AVRCP_OPERATION_ID_SKIP = 0x3C, 204 AVRCP_OPERATION_ID_VOLUME_UP = 0x41, 205 AVRCP_OPERATION_ID_VOLUME_DOWN = 0x42, 206 AVRCP_OPERATION_ID_MUTE = 0x43, 207 208 AVRCP_OPERATION_ID_PLAY = 0x44, 209 AVRCP_OPERATION_ID_STOP = 0x45, 210 AVRCP_OPERATION_ID_PAUSE = 0x46, 211 AVRCP_OPERATION_ID_REWIND = 0x48, 212 AVRCP_OPERATION_ID_FAST_FORWARD = 0x49, 213 AVRCP_OPERATION_ID_FORWARD = 0x4B, 214 AVRCP_OPERATION_ID_BACKWARD = 0x4C, 215 AVRCP_OPERATION_ID_UNDEFINED = 0xFF 216 } avrcp_operation_id_t; 217 218 typedef enum{ 219 AVRCP_PLAYBACK_STATUS_STOPPED = 0x00, 220 AVRCP_PLAYBACK_STATUS_PLAYING, 221 AVRCP_PLAYBACK_STATUS_PAUSED, 222 AVRCP_PLAYBACK_STATUS_FWD_SEEK, 223 AVRCP_PLAYBACK_STATUS_REV_SEEK, 224 AVRCP_PLAYBACK_STATUS_ERROR = 0xFF 225 } avrcp_playback_status_t; 226 227 typedef enum{ 228 AVRCP_BATTERY_STATUS_NORMAL = 0x00, // Battery operation is in normal state 229 AVRCP_BATTERY_STATUS_WARNING, // unable to operate soon. Is provided when the battery level is going down. 230 AVRCP_BATTERY_STATUS_CRITICAL, // can not operate any more. Is provided when the battery level is going down. 231 AVRCP_BATTERY_STATUS_EXTERNAL, // Plugged to external power supply 232 AVRCP_BATTERY_STATUS_FULL_CHARGE // when the device is completely charged from the external power supply 233 } avrcp_battery_status_t; 234 235 236 typedef enum { 237 AVCTP_CONNECTION_IDLE, 238 AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE, 239 AVCTP_CONNECTION_W4_L2CAP_CONNECTED, 240 AVCTP_CONNECTION_OPENED, 241 AVCTP_W2_SEND_PRESS_COMMAND, 242 AVCTP_W2_SEND_RELEASE_COMMAND, 243 AVCTP_W4_STOP, 244 AVCTP_W2_SEND_COMMAND, 245 AVCTP_W2_SEND_RESPONSE, 246 AVCTP_W2_RECEIVE_PRESS_RESPONSE, 247 AVCTP_W2_RECEIVE_RESPONSE 248 } avctp_connection_state_t; 249 250 typedef struct { 251 uint16_t len; 252 uint8_t * value; 253 } avrcp_now_playing_info_item_t; 254 255 typedef struct { 256 uint8_t track_id[8]; 257 uint16_t track_nr; 258 char * title; 259 char * artist; 260 char * album; 261 char * genre; 262 uint32_t song_length_ms; 263 uint32_t song_position_ms; 264 } avrcp_track_t; 265 266 typedef struct { 267 btstack_linked_item_t item; 268 bd_addr_t remote_addr; 269 uint16_t l2cap_signaling_cid; 270 uint16_t avrcp_cid; 271 272 avctp_connection_state_t state; 273 uint8_t wait_to_send; 274 275 // command 276 uint8_t transaction_label; 277 avrcp_command_opcode_t command_opcode; 278 avrcp_command_type_t command_type; 279 avrcp_subunit_type_t subunit_type; 280 avrcp_subunit_id_t subunit_id; 281 avrcp_packet_type_t packet_type; 282 283 uint8_t cmd_operands[20]; 284 uint8_t cmd_operands_length; 285 btstack_timer_source_t press_and_hold_cmd_timer; 286 uint8_t continuous_fast_forward_cmd; 287 uint16_t notifications_enabled; 288 uint16_t notifications_to_register; 289 uint16_t notifications_to_deregister; 290 291 avrcp_subunit_type_t unit_type; 292 uint32_t company_id; 293 avrcp_subunit_type_t subunit_info_type; 294 const uint8_t * subunit_info_data; 295 uint16_t subunit_info_data_size; 296 297 avrcp_now_playing_info_item_t now_playing_info[AVRCP_MEDIA_ATTR_COUNT]; 298 uint8_t track_id[8]; 299 uint32_t song_length_ms; 300 uint32_t song_position_ms; 301 int total_tracks; 302 int track_nr; 303 uint8_t track_selected; 304 uint8_t track_changed; 305 306 avrcp_playback_status_t playback_status; 307 uint8_t playback_status_changed; 308 309 uint8_t playing_content_changed; 310 311 avrcp_battery_status_t battery_status; 312 uint8_t battery_status_changed; 313 uint8_t volume_percentage; 314 uint8_t volume_percentage_changed; 315 uint8_t now_playing_info_response; 316 uint8_t now_playing_info_attr_bitmap; 317 318 // used for fragmentation 319 avrcp_media_attribute_id_t next_attr_id; 320 int fragmented_value_offset; 321 } avrcp_connection_t; 322 323 typedef enum { 324 AVRCP_SHUFFLE_MODE_INVALID, 325 AVRCP_SHUFFLE_MODE_OFF, 326 AVRCP_SHUFFLE_MODE_ALL_TRACKS, 327 AVRCP_SHUFFLE_MODE_GROUP 328 } avrcp_shuffle_mode_t; 329 330 typedef enum { 331 AVRCP_REPEAT_MODE_INVALID, 332 AVRCP_REPEAT_MODE_OFF, 333 AVRCP_REPEAT_MODE_SINGLE_TRACK, 334 AVRCP_REPEAT_MODE_ALL_TRACKS, 335 AVRCP_REPEAT_MODE_GROUP 336 } avrcp_repeat_mode_t; 337 338 typedef enum{ 339 AVRCP_CONTROLLER = 0, 340 AVRCP_TARGET 341 } avrcp_role_t; 342 343 typedef enum { 344 UTF8 = 106 345 } rfc2978_charset_mib_enumid_t; 346 347 typedef struct { 348 avrcp_role_t role; 349 btstack_linked_list_t connections; 350 btstack_packet_handler_t avrcp_callback; 351 btstack_packet_handler_t packet_handler; 352 353 // SDP query 354 uint16_t avrcp_cid; 355 uint16_t avrcp_l2cap_psm; 356 uint16_t avrcp_version; 357 uint16_t avrcp_browsing_l2cap_psm; 358 uint16_t avrcp_browsing_version; 359 uint8_t role_supported; 360 } avrcp_context_t; 361 362 const char * avrcp_subunit2str(uint16_t index); 363 const char * avrcp_event2str(uint16_t index); 364 const char * avrcp_operation2str(uint8_t index); 365 const char * avrcp_attribute2str(uint8_t index); 366 const char * avrcp_play_status2str(uint8_t index); 367 const char * avrcp_ctype2str(uint8_t index); 368 const char * avrcp_repeat2str(uint8_t index); 369 const char * avrcp_shuffle2str(uint8_t index); 370 371 void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context); 372 373 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); 374 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid); 375 void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status); 376 void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid); 377 378 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size); 379 avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context); 380 avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context); 381 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid); 382 383 #if defined __cplusplus 384 } 385 #endif 386 387 #endif // __AVRCP_H