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 #define __BTSTACK_FILE__ "avrcp.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "btstack.h" 46 #include "classic/avrcp.h" 47 48 #define PSM_AVCTP BLUETOOTH_PROTOCOL_AVCTP 49 #define PSM_AVCTP_BROWSING 0x001b 50 51 /* 52 Category 1: Player/Recorder 53 Category 2: Monitor/Amplifier 54 Category 3: Tuner 55 Category 4: Menu 56 */ 57 58 /* controller supported features 59 Bit 0 = Category 1 60 Bit 1 = Category 2 61 Bit 2 = Category 3 62 Bit 3 = Category 4 63 Bit 4-5 = RFA 64 Bit 6 = Supports browsing 65 Bit 7-15 = RFA 66 The bits for supported categories are set to 1. Others are set to 0. 67 */ 68 69 /* target supported features 70 Bit 0 = Category 1 71 Bit 1 = Category 2 72 Bit 2 = Category 3 73 Bit 3 = Category 4 74 Bit 4 = Player Application Settings. Bit 0 should be set for this bit to be set. 75 Bit 5 = Group Navigation. Bit 0 should be set for this bit to be set. 76 Bit 6 = Supports browsing*4 77 Bit 7 = Supports multiple media player applications 78 Bit 8-15 = RFA 79 The bits for supported categories are set to 1. Others are set to 0. 80 */ 81 82 static uint16_t avrcp_cid_counter = 1; 83 84 // TODO: merge with avdtp_packet_type_t 85 typedef enum { 86 AVRCP_SINGLE_PACKET= 0, 87 AVRCP_START_PACKET , 88 AVRCP_CONTINUE_PACKET , 89 AVRCP_END_PACKET 90 } avrcp_packet_type_t; 91 92 typedef enum { 93 AVRCP_COMMAND_FRAME = 0, 94 AVRCP_RESPONSE_FRAME 95 } avrcp_frame_type_t; 96 97 static int record_id = -1; 98 static uint8_t attribute_value[1000]; 99 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 100 101 static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service"; 102 static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider"; 103 static const char * default_avrcp_target_service_name = "BTstack AVRCP Target Service"; 104 static const char * default_avrcp_target_service_provider_name = "BTstack AVRCP Target Service Provider"; 105 106 static btstack_packet_handler_t avrcp_callback; 107 108 static avrcp_context_t avrcp_controller_context; 109 110 static const char * avrcp_subunit_type_name[] = { 111 "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER", 112 "CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE", 113 "VENDOR_UNIQUE", "RESERVED_FOR_ALL_SUBUNIT_TYPES", 114 "EXTENDED_TO_NEXT_BYTE", "UNIT", "ERROR" 115 }; 116 const char * avrcp_subunit2str(uint16_t index){ 117 if (index <= 11) return avrcp_subunit_type_name[index]; 118 if (index >= 0x1C && index <= 0x1F) return avrcp_subunit_type_name[index - 0x10]; 119 return avrcp_subunit_type_name[16]; 120 } 121 122 static const char * avrcp_event_name[] = { 123 "ERROR", "PLAYBACK_STATUS_CHANGED", 124 "TRACK_CHANGED", "TRACK_REACHED_END", "TRACK_REACHED_START", 125 "PLAYBACK_POS_CHANGED", "BATT_STATUS_CHANGED", "SYSTEM_STATUS_CHANGED", 126 "PLAYER_APPLICATION_SETTING_CHANGED", "NOW_PLAYING_CONTENT_CHANGED", 127 "AVAILABLE_PLAYERS_CHANGED", "ADDRESSED_PLAYER_CHANGED", "UIDS_CHANGED", "VOLUME_CHANGED" 128 }; 129 const char * avrcp_event2str(uint16_t index){ 130 if (index <= 0x0d) return avrcp_event_name[index]; 131 return avrcp_event_name[0]; 132 } 133 134 static const char * avrcp_operation_name[] = { 135 "NOT SUPPORTED", // 0x3B 136 "SKIP", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED", 137 "VOLUME_UP", "VOLUME_DOWN", "MUTE", "PLAY", "STOP", "PAUSE", "NOT SUPPORTED", 138 "REWIND", "FAST_FORWARD", "NOT SUPPORTED", "FORWARD", "BACKWARD" // 0x4C 139 }; 140 const char * avrcp_operation2str(uint8_t index){ 141 if (index >= 0x3B && index <= 0x4C) return avrcp_operation_name[index - 0x3B]; 142 return avrcp_operation_name[0]; 143 } 144 145 static const char * avrcp_media_attribute_id_name[] = { 146 "NONE", "TITLE", "ARTIST", "ALBUM", "TRACK", "TOTAL TRACKS", "GENRE", "SONG LENGTH" 147 }; 148 const char * avrcp_attribute2str(uint8_t index){ 149 if (index >= 1 && index <= 7) return avrcp_media_attribute_id_name[index]; 150 return avrcp_media_attribute_id_name[0]; 151 } 152 153 static const char * avrcp_play_status_name[] = { 154 "STOPPED", "PLAYING", "PAUSED", "FORWARD SEEK", "REVERSE SEEK", 155 "ERROR" // 0xFF 156 }; 157 const char * avrcp_play_status2str(uint8_t index){ 158 if (index >= 1 && index <= 4) return avrcp_play_status_name[index]; 159 return avrcp_play_status_name[5]; 160 } 161 162 static const char * avrcp_ctype_name[] = { 163 "CONTROL", 164 "STATUS", 165 "SPECIFIC_INQUIRY", 166 "NOTIFY", 167 "GENERAL_INQUIRY", 168 "RESERVED5", 169 "RESERVED6", 170 "RESERVED7", 171 "NOT IMPLEMENTED IN REMOTE", 172 "ACCEPTED BY REMOTE", 173 "REJECTED BY REMOTE", 174 "IN_TRANSITION", 175 "IMPLEMENTED_STABLE", 176 "CHANGED_STABLE", 177 "RESERVED", 178 "INTERIM" 179 }; 180 const char * avrcp_ctype2str(uint8_t index){ 181 if (index < sizeof(avrcp_ctype_name)){ 182 return avrcp_ctype_name[index]; 183 } 184 return "NONE"; 185 } 186 187 static const char * avrcp_shuffle_mode_name[] = { 188 "SHUFFLE OFF", 189 "SHUFFLE ALL TRACKS", 190 "SHUFFLE GROUP" 191 }; 192 193 const char * avrcp_shuffle2str(uint8_t index){ 194 if (index >= 1 && index <= 3) return avrcp_shuffle_mode_name[index-1]; 195 return "NONE"; 196 } 197 198 static const char * avrcp_repeat_mode_name[] = { 199 "REPEAT OFF", 200 "REPEAT SINGLE TRACK", 201 "REPEAT ALL TRACKS", 202 "REPEAT GROUP" 203 }; 204 205 const char * avrcp_repeat2str(uint8_t index){ 206 if (index >= 1 && index <= 4) return avrcp_repeat_mode_name[index-1]; 207 return "NONE"; 208 } 209 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context); 210 211 static avrcp_sdp_query_context_t sdp_query_context; 212 static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 213 214 static 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){ 215 uint8_t* attribute; 216 de_create_sequence(service); 217 218 // 0x0000 "Service Record Handle" 219 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 220 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 221 222 // 0x0001 "Service Class ID List" 223 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 224 attribute = de_push_sequence(service); 225 { 226 if (controller){ 227 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 228 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER); 229 } else { 230 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET); 231 } 232 } 233 de_pop_sequence(service, attribute); 234 235 // 0x0004 "Protocol Descriptor List" 236 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 237 attribute = de_push_sequence(service); 238 { 239 uint8_t* l2cpProtocol = de_push_sequence(attribute); 240 { 241 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 242 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); 243 } 244 de_pop_sequence(attribute, l2cpProtocol); 245 246 uint8_t* avctpProtocol = de_push_sequence(attribute); 247 { 248 de_add_number(avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // avctpProtocol_service 249 de_add_number(avctpProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 250 } 251 de_pop_sequence(attribute, avctpProtocol); 252 } 253 de_pop_sequence(service, attribute); 254 255 // 0x0005 "Public Browse Group" 256 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 257 attribute = de_push_sequence(service); 258 { 259 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 260 } 261 de_pop_sequence(service, attribute); 262 263 // 0x0009 "Bluetooth Profile Descriptor List" 264 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 265 attribute = de_push_sequence(service); 266 { 267 uint8_t *avrcProfile = de_push_sequence(attribute); 268 { 269 de_add_number(avrcProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 270 de_add_number(avrcProfile, DE_UINT, DE_SIZE_16, 0x0105); 271 } 272 de_pop_sequence(attribute, avrcProfile); 273 } 274 de_pop_sequence(service, attribute); 275 276 // 0x000d "Additional Bluetooth Profile Descriptor List" 277 if (browsing){ 278 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); 279 attribute = de_push_sequence(service); 280 { 281 uint8_t * des = de_push_sequence(attribute); 282 { 283 uint8_t* browsing_l2cpProtocol = de_push_sequence(des); 284 { 285 de_add_number(browsing_l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 286 de_add_number(browsing_l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_AVCTP_BROWSING); 287 } 288 de_pop_sequence(des, browsing_l2cpProtocol); 289 290 uint8_t* browsing_avctpProtocol = de_push_sequence(des); 291 { 292 de_add_number(browsing_avctpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP); // browsing_avctpProtocol_service 293 de_add_number(browsing_avctpProtocol, DE_UINT, DE_SIZE_16, 0x0103); // version 294 } 295 de_pop_sequence(des, browsing_avctpProtocol); 296 } 297 de_pop_sequence(attribute, des); 298 } 299 de_pop_sequence(service, attribute); 300 } 301 302 303 // 0x0100 "Service Name" 304 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 305 if (service_name){ 306 de_add_data(service, DE_STRING, strlen(service_name), (uint8_t *) service_name); 307 } else { 308 if (controller){ 309 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name); 310 } else { 311 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_name), (uint8_t *) default_avrcp_target_service_name); 312 } 313 } 314 315 // 0x0100 "Provider Name" 316 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0102); 317 if (service_provider_name){ 318 de_add_data(service, DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name); 319 } else { 320 if (controller){ 321 de_add_data(service, DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name); 322 } else { 323 de_add_data(service, DE_STRING, strlen(default_avrcp_target_service_provider_name), (uint8_t *) default_avrcp_target_service_provider_name); 324 } 325 } 326 327 // 0x0311 "Supported Features" 328 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); 329 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 330 } 331 332 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){ 333 avrcp_create_sdp_record(1, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); 334 } 335 336 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){ 337 avrcp_create_sdp_record(0, service, service_record_handle, browsing, supported_features, service_name, service_provider_name); 338 } 339 340 static void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status){ 341 if (!callback) return; 342 uint8_t event[12]; 343 int pos = 0; 344 event[pos++] = HCI_EVENT_AVRCP_META; 345 event[pos++] = sizeof(event) - 2; 346 event[pos++] = AVRCP_SUBEVENT_CONNECTION_ESTABLISHED; 347 event[pos++] = status; 348 reverse_bd_addr(addr,&event[pos]); 349 pos += 6; 350 little_endian_store_16(event, pos, avrcp_cid); 351 pos += 2; 352 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 353 } 354 355 static void avrcp_emit_repeat_and_shuffle_mode(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_repeat_mode_t repeat_mode, avrcp_shuffle_mode_t shuffle_mode){ 356 if (!callback) return; 357 uint8_t event[8]; 358 int pos = 0; 359 event[pos++] = HCI_EVENT_AVRCP_META; 360 event[pos++] = sizeof(event) - 2; 361 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 362 little_endian_store_16(event, pos, avrcp_cid); 363 pos += 2; 364 event[pos++] = ctype; 365 event[pos++] = repeat_mode; 366 event[pos++] = shuffle_mode; 367 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 368 } 369 370 static void avrcp_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){ 371 if (!callback) return; 372 uint8_t event[7]; 373 int pos = 0; 374 event[pos++] = HCI_EVENT_AVRCP_META; 375 event[pos++] = sizeof(event) - 2; 376 event[pos++] = subevent; 377 little_endian_store_16(event, pos, avrcp_cid); 378 pos += 2; 379 event[pos++] = ctype; 380 event[pos++] = operation_id; 381 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 382 } 383 384 static void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid){ 385 if (!callback) return; 386 uint8_t event[5]; 387 int pos = 0; 388 event[pos++] = HCI_EVENT_AVRCP_META; 389 event[pos++] = sizeof(event) - 2; 390 event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED; 391 little_endian_store_16(event, pos, avrcp_cid); 392 pos += 2; 393 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 394 } 395 396 static avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){ 397 btstack_linked_list_iterator_t it; 398 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 399 while (btstack_linked_list_iterator_has_next(&it)){ 400 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 401 if (memcmp(addr, connection->remote_addr, 6) != 0) continue; 402 return connection; 403 } 404 return NULL; 405 } 406 407 static avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 408 btstack_linked_list_iterator_t it; 409 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 410 while (btstack_linked_list_iterator_has_next(&it)){ 411 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 412 if (connection->l2cap_signaling_cid != l2cap_cid) continue; 413 return connection; 414 } 415 return NULL; 416 } 417 418 static avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 419 btstack_linked_list_iterator_t it; 420 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 421 while (btstack_linked_list_iterator_has_next(&it)){ 422 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 423 if (connection->avrcp_cid != l2cap_cid) continue; 424 return connection; 425 } 426 return NULL; 427 } 428 429 static void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){ 430 connection->wait_to_send = 1; 431 l2cap_request_can_send_now_event(l2cap_cid); 432 } 433 434 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 435 UNUSED(timer); 436 avrcp_connection_t * connection = btstack_run_loop_get_timer_context(timer); 437 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 438 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 439 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 440 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 441 } 442 443 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 444 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 445 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 446 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 447 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 448 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 449 } 450 451 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 452 connection->continuous_fast_forward_cmd = 0; 453 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 454 } 455 456 static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 457 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 458 if (connection->continuous_fast_forward_cmd){ 459 avrcp_press_and_hold_timer_stop(connection); 460 } 461 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 462 connection->transaction_label++; 463 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 464 return ERROR_CODE_SUCCESS; 465 } 466 467 static inline uint8_t request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, uint8_t continuous_fast_forward_cmd, avrcp_context_t * context){ 468 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, context); 469 if (!connection){ 470 log_error("avrcp: could not find a connection."); 471 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 472 } 473 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 474 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 475 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 476 connection->command_type = AVRCP_CTYPE_CONTROL; 477 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 478 connection->subunit_id = AVRCP_SUBUNIT_ID; 479 connection->cmd_operands_length = 0; 480 481 connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 482 connection->cmd_operands_length = 2; 483 connection->cmd_operands[0] = opid; 484 if (playback_speed > 0){ 485 connection->cmd_operands[2] = playback_speed; 486 connection->cmd_operands_length++; 487 } 488 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 489 490 if (connection->continuous_fast_forward_cmd){ 491 avrcp_press_and_hold_timer_start(connection); 492 } 493 494 connection->transaction_label++; 495 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 496 return ERROR_CODE_SUCCESS; 497 } 498 499 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 500 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0, &avrcp_controller_context); 501 } 502 503 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 504 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1, &avrcp_controller_context); 505 } 506 507 static int avrcp_send_cmd(uint16_t cid, avrcp_connection_t * connection){ 508 uint8_t command[30]; 509 int pos = 0; 510 // transport header 511 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 512 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 513 // Profile IDentifier (PID) 514 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 515 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 516 517 // command_type 518 command[pos++] = connection->command_type; 519 // subunit_type | subunit ID 520 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 521 // opcode 522 command[pos++] = (uint8_t)connection->command_opcode; 523 // operands 524 memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 525 pos += connection->cmd_operands_length; 526 527 return l2cap_send(cid, command, pos); 528 } 529 530 static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 531 if (connection->notifications_to_deregister & (1 << event_id)) return 0; 532 if (connection->notifications_enabled & (1 << event_id)) return 0; 533 if (connection->notifications_to_register & (1 << event_id)) return 0; 534 connection->notifications_to_register |= (1 << event_id); 535 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 536 return 1; 537 } 538 539 static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 540 connection->transaction_label++; 541 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 542 connection->command_type = AVRCP_CTYPE_NOTIFY; 543 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 544 connection->subunit_id = AVRCP_SUBUNIT_ID; 545 int pos = 0; 546 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 547 pos += 3; 548 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 549 connection->cmd_operands[pos++] = 0; // reserved(upper 6) | packet_type -> 0 550 big_endian_store_16(connection->cmd_operands, pos, 5); // parameter length 551 pos += 2; 552 connection->cmd_operands[pos++] = event_id; 553 big_endian_store_32(connection->cmd_operands, pos, 0); 554 pos += 4; 555 connection->cmd_operands_length = pos; 556 // AVRCP_SPEC_V14.pdf 166 557 // answer page 61 558 } 559 560 static uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){ 561 uint8_t cmd_opcode_index = 5; 562 if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED; 563 return packet[cmd_opcode_index]; 564 } 565 566 static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 567 if (!sdp_query_context.connection) return; 568 if (sdp_query_context.connection->state != AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE) return; 569 UNUSED(packet_type); 570 UNUSED(channel); 571 UNUSED(size); 572 573 des_iterator_t des_list_it; 574 des_iterator_t prot_it; 575 // uint32_t avdtp_remote_uuid = 0; 576 577 switch (hci_event_packet_get_type(packet)){ 578 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 579 // Handle new SDP record 580 if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) { 581 record_id = sdp_event_query_attribute_byte_get_record_id(packet); 582 // log_info("SDP Record: Nr: %d", record_id); 583 } 584 585 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 586 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 587 588 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 589 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 590 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 591 if (de_get_element_type(attribute_value) != DE_DES) break; 592 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 593 uint8_t * element = des_iterator_get_element(&des_list_it); 594 if (de_get_element_type(element) != DE_UUID) continue; 595 uint32_t uuid = de_get_uuid32(element); 596 switch (uuid){ 597 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET: 598 if (sdp_query_context.avrcp_context->role == AVRCP_TARGET) { 599 sdp_query_context.role_supported = 1; 600 break; 601 } 602 break; 603 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL: 604 case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER: 605 if (sdp_query_context.avrcp_context->role == AVRCP_CONTROLLER) { 606 sdp_query_context.role_supported = 1; 607 break; 608 } 609 break; 610 default: 611 break; 612 } 613 } 614 break; 615 616 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { 617 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 618 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 619 uint8_t *des_element; 620 uint8_t *element; 621 uint32_t uuid; 622 623 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 624 625 des_element = des_iterator_get_element(&des_list_it); 626 des_iterator_init(&prot_it, des_element); 627 element = des_iterator_get_element(&prot_it); 628 629 if (de_get_element_type(element) != DE_UUID) continue; 630 631 uuid = de_get_uuid32(element); 632 switch (uuid){ 633 case BLUETOOTH_PROTOCOL_L2CAP: 634 if (!des_iterator_has_more(&prot_it)) continue; 635 des_iterator_next(&prot_it); 636 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_l2cap_psm); 637 break; 638 case BLUETOOTH_PROTOCOL_AVCTP: 639 if (!des_iterator_has_more(&prot_it)) continue; 640 des_iterator_next(&prot_it); 641 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_version); 642 break; 643 default: 644 break; 645 } 646 } 647 } 648 break; 649 case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: { 650 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet)); 651 if (de_get_element_type(attribute_value) != DE_DES) break; 652 653 des_iterator_t des_list_0_it; 654 uint8_t *element_0; 655 656 des_iterator_init(&des_list_0_it, attribute_value); 657 element_0 = des_iterator_get_element(&des_list_0_it); 658 659 for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 660 uint8_t *des_element; 661 uint8_t *element; 662 uint32_t uuid; 663 664 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 665 666 des_element = des_iterator_get_element(&des_list_it); 667 des_iterator_init(&prot_it, des_element); 668 element = des_iterator_get_element(&prot_it); 669 670 if (de_get_element_type(element) != DE_UUID) continue; 671 672 uuid = de_get_uuid32(element); 673 switch (uuid){ 674 case BLUETOOTH_PROTOCOL_L2CAP: 675 if (!des_iterator_has_more(&prot_it)) continue; 676 des_iterator_next(&prot_it); 677 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_browsing_l2cap_psm); 678 break; 679 case BLUETOOTH_PROTOCOL_AVCTP: 680 if (!des_iterator_has_more(&prot_it)) continue; 681 des_iterator_next(&prot_it); 682 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_browsing_version); 683 break; 684 default: 685 break; 686 } 687 } 688 } 689 break; 690 default: 691 break; 692 } 693 } 694 } else { 695 log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 696 } 697 break; 698 699 case SDP_EVENT_QUERY_COMPLETE: 700 log_info("General query done with status %d.", sdp_event_query_complete_get_status(packet)); 701 if (!sdp_query_context.role_supported || !sdp_query_context.avrcp_l2cap_psm){ 702 sdp_query_context.connection->state = AVCTP_CONNECTION_IDLE; 703 avrcp_emit_connection_established(sdp_query_context.avrcp_context->avrcp_callback, sdp_query_context.connection->avrcp_cid, sdp_query_context.connection->remote_addr, SDP_SERVICE_NOT_FOUND); 704 break; 705 } 706 log_info("AVRCP Control PSM 0x%02x, Browsing PSM 0x%02x", sdp_query_context.avrcp_l2cap_psm, sdp_query_context.avrcp_browsing_l2cap_psm); 707 708 sdp_query_context.connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 709 l2cap_create_channel(sdp_query_context.avrcp_context->packet_handler, sdp_query_context.connection->remote_addr, sdp_query_context.avrcp_l2cap_psm, l2cap_max_mtu(), NULL); 710 break; 711 } 712 } 713 714 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 715 uint8_t operands[20]; 716 uint8_t opcode; 717 int pos = 3; 718 // uint8_t transport_header = packet[0]; 719 // uint8_t transaction_label = transport_header >> 4; 720 // uint8_t packet_type = (transport_header & 0x0F) >> 2; 721 // uint8_t frame_type = (transport_header & 0x03) >> 1; 722 // uint8_t ipid = transport_header & 0x01; 723 // uint8_t byte_value = packet[2]; 724 // uint16_t pid = (byte_value << 8) | packet[2]; 725 726 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 727 uint8_t byte_value = packet[pos++]; 728 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 729 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 730 opcode = packet[pos++]; 731 732 // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 733 // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 734 // // printf_hexdump(packet+pos, size-pos); 735 736 uint8_t pdu_id; 737 uint16_t param_length; 738 switch (avrcp_cmd_opcode(packet,size)){ 739 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 740 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 741 connection->state = AVCTP_CONNECTION_OPENED; 742 743 // operands: 744 memcpy(operands, packet+pos, 5); 745 uint8_t unit_type = operands[1] >> 3; 746 uint8_t unit = operands[1] & 0x07; 747 uint32_t company_id = operands[2] << 16 | operands[3] << 8 | operands[4]; 748 log_info(" UNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06x", 749 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 750 break; 751 } 752 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 753 if (size - pos < 7) { 754 log_error("avrcp: wrong packet size"); 755 return; 756 }; 757 // operands: 758 memcpy(operands, packet+pos, 7); 759 pos += 7; 760 // uint32_t company_id = operands[0] << 16 | operands[1] << 8 | operands[2]; 761 pdu_id = operands[3]; 762 763 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE && pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 764 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 765 return; 766 } 767 connection->state = AVCTP_CONNECTION_OPENED; 768 769 770 // uint8_t unit_type = operands[4] >> 3; 771 // uint8_t unit = operands[4] & 0x07; 772 param_length = big_endian_read_16(operands, 5); 773 774 // printf(" VENDOR DEPENDENT response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06x\n", 775 // ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 776 777 // if (ctype == AVRCP_CTYPE_RESPONSE_INTERIM) return; 778 log_info(" VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 779 switch (pdu_id){ 780 case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{ 781 uint8_t num_attributes = packet[pos++]; 782 int i; 783 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 784 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 785 for (i = 0; i < num_attributes; i++){ 786 uint8_t attribute_id = packet[pos++]; 787 uint8_t value = packet[pos++]; 788 switch (attribute_id){ 789 case 0x02: 790 repeat_mode = (avrcp_repeat_mode_t) value; 791 break; 792 case 0x03: 793 shuffle_mode = (avrcp_shuffle_mode_t) value; 794 break; 795 default: 796 break; 797 } 798 } 799 avrcp_emit_repeat_and_shuffle_mode(avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 800 break; 801 } 802 case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{ 803 uint8_t event[6]; 804 int offset = 0; 805 event[offset++] = HCI_EVENT_AVRCP_META; 806 event[offset++] = sizeof(event) - 2; 807 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 808 little_endian_store_16(event, offset, connection->avrcp_cid); 809 offset += 2; 810 event[offset++] = ctype; 811 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 812 break; 813 } 814 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 815 uint8_t event[7]; 816 int offset = 0; 817 event[offset++] = HCI_EVENT_AVRCP_META; 818 event[offset++] = sizeof(event) - 2; 819 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 820 little_endian_store_16(event, offset, connection->avrcp_cid); 821 offset += 2; 822 event[offset++] = ctype; 823 event[offset++] = packet[pos++]; 824 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 825 break; 826 } 827 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 828 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 829 uint8_t capability_count = packet[pos++]; 830 int i; 831 switch (capability_id){ 832 case AVRCP_CAPABILITY_ID_COMPANY: 833 // log_info("Supported companies %d: ", capability_count); 834 for (i = 0; i < capability_count; i++){ 835 uint32_t company_id = big_endian_read_24(packet, pos); 836 pos += 3; 837 log_info(" 0x%06x, ", company_id); 838 } 839 break; 840 case AVRCP_CAPABILITY_ID_EVENT: 841 // log_info("Supported events %d: ", capability_count); 842 for (i = 0; i < capability_count; i++){ 843 uint8_t event_id = packet[pos++]; 844 log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 845 } 846 break; 847 } 848 break; 849 } 850 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 851 uint32_t song_length = big_endian_read_32(packet, pos); 852 pos += 4; 853 uint32_t song_position = big_endian_read_32(packet, pos); 854 pos += 4; 855 uint8_t play_status = packet[pos]; 856 // log_info(" GET_PLAY_STATUS length 0x%04X, position 0x%04X, status %s", song_length, song_position, avrcp_play_status2str(play_status)); 857 858 uint8_t event[15]; 859 int offset = 0; 860 event[offset++] = HCI_EVENT_AVRCP_META; 861 event[offset++] = sizeof(event) - 2; 862 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 863 little_endian_store_16(event, offset, connection->avrcp_cid); 864 offset += 2; 865 event[offset++] = ctype; 866 little_endian_store_32(event, offset, song_length); 867 offset += 4; 868 little_endian_store_32(event, offset, song_position); 869 offset += 4; 870 event[offset++] = play_status; 871 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 872 break; 873 } 874 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 875 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos++]; 876 uint16_t event_mask = (1 << event_id); 877 uint16_t reset_event_mask = ~event_mask; 878 switch (ctype){ 879 case AVRCP_CTYPE_RESPONSE_INTERIM: 880 // register as enabled 881 connection->notifications_enabled |= event_mask; 882 // printf("INTERIM notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 883 break; 884 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 885 // received change, event is considered deregistered 886 // we are re-enabling it automatically, if it is not 887 // explicitly disabled 888 connection->notifications_enabled &= reset_event_mask; 889 if (! (connection->notifications_to_deregister & event_mask)){ 890 avrcp_register_notification(connection, event_id); 891 // printf("CHANGED_STABLE notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 892 } else { 893 connection->notifications_to_deregister &= reset_event_mask; 894 } 895 break; 896 default: 897 connection->notifications_to_register &= reset_event_mask; 898 connection->notifications_enabled &= reset_event_mask; 899 connection->notifications_to_deregister &= reset_event_mask; 900 break; 901 } 902 903 switch (event_id){ 904 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 905 uint8_t event[7]; 906 int offset = 0; 907 event[offset++] = HCI_EVENT_AVRCP_META; 908 event[offset++] = sizeof(event) - 2; 909 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 910 little_endian_store_16(event, offset, connection->avrcp_cid); 911 offset += 2; 912 event[offset++] = ctype; 913 event[offset++] = packet[pos]; 914 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 915 break; 916 } 917 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 918 uint8_t event[6]; 919 int offset = 0; 920 event[offset++] = HCI_EVENT_AVRCP_META; 921 event[offset++] = sizeof(event) - 2; 922 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 923 little_endian_store_16(event, offset, connection->avrcp_cid); 924 offset += 2; 925 event[offset++] = ctype; 926 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 927 break; 928 } 929 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 930 uint8_t event[6]; 931 int offset = 0; 932 event[offset++] = HCI_EVENT_AVRCP_META; 933 event[offset++] = sizeof(event) - 2; 934 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 935 little_endian_store_16(event, offset, connection->avrcp_cid); 936 offset += 2; 937 event[offset++] = ctype; 938 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 939 break; 940 } 941 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 942 uint8_t event[6]; 943 int offset = 0; 944 event[offset++] = HCI_EVENT_AVRCP_META; 945 event[offset++] = sizeof(event) - 2; 946 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 947 little_endian_store_16(event, offset, connection->avrcp_cid); 948 offset += 2; 949 event[offset++] = ctype; 950 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 951 break; 952 } 953 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 954 uint8_t event[7]; 955 int offset = 0; 956 event[offset++] = HCI_EVENT_AVRCP_META; 957 event[offset++] = sizeof(event) - 2; 958 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 959 little_endian_store_16(event, offset, connection->avrcp_cid); 960 offset += 2; 961 event[offset++] = ctype; 962 event[offset++] = packet[pos++] & 0x7F; 963 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 964 break; 965 } 966 // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 967 // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 968 // int i; 969 // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 970 // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 971 // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 972 // } 973 // break; 974 // } 975 // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 976 // uint16_t player_id = big_endian_read_16(packet, pos); 977 // pos += 2; 978 // uint16_t uid_counter = big_endian_read_16(packet, pos); 979 // pos += 2; 980 // break; 981 // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 982 // uint16_t uid_counter = big_endian_read_16(packet, pos); 983 // pos += 2; 984 // break; 985 default: 986 log_info("avrcp: not implemented"); 987 break; 988 } 989 if (connection->notifications_to_register != 0){ 990 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 991 } 992 break; 993 } 994 995 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 996 uint8_t num_attributes = packet[pos++]; 997 int i; 998 struct item { 999 uint16_t len; 1000 uint8_t * value; 1001 } items[AVRCP_MEDIA_ATTR_COUNT]; 1002 memset(items, 0, sizeof(items)); 1003 1004 uint16_t string_attributes_len = 0; 1005 uint8_t num_string_attributes = 0; 1006 uint16_t total_event_payload_for_string_attributes = HCI_EVENT_PAYLOAD_SIZE-2; 1007 uint16_t max_string_attribute_value_len = 0; 1008 if (ctype == AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE || ctype == AVRCP_CTYPE_RESPONSE_CHANGED_STABLE){ 1009 for (i = 0; i < num_attributes; i++){ 1010 avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) big_endian_read_32(packet, pos); 1011 pos += 4; 1012 // uint16_t character_set = big_endian_read_16(packet, pos); 1013 pos += 2; 1014 uint16_t attr_value_length = big_endian_read_16(packet, pos); 1015 pos += 2; 1016 1017 // debug - to remove later 1018 uint8_t value[100]; 1019 uint16_t value_len = sizeof(value) <= attr_value_length? sizeof(value) - 1 : attr_value_length; 1020 memcpy(value, packet+pos, value_len); 1021 value[value_len] = 0; 1022 // printf("Now Playing Info %s: %s \n", attribute2str(attr_id), value); 1023 // end debug 1024 1025 if ((attr_id >= 1) || (attr_id <= AVRCP_MEDIA_ATTR_COUNT)) { 1026 items[attr_id-1].len = attr_value_length; 1027 items[attr_id-1].value = &packet[pos]; 1028 switch (attr_id){ 1029 case AVRCP_MEDIA_ATTR_TITLE: 1030 case AVRCP_MEDIA_ATTR_ARTIST: 1031 case AVRCP_MEDIA_ATTR_ALBUM: 1032 case AVRCP_MEDIA_ATTR_GENRE: 1033 num_string_attributes++; 1034 string_attributes_len += attr_value_length; 1035 if (max_string_attribute_value_len < attr_value_length){ 1036 max_string_attribute_value_len = attr_value_length; 1037 } 1038 break; 1039 default: 1040 break; 1041 } 1042 } 1043 pos += attr_value_length; 1044 } 1045 } 1046 1047 // subtract space for fixed fields 1048 total_event_payload_for_string_attributes -= 14 + 4; // 4 for '\0' 1049 1050 // @TODO optimize space by repeatedly decreasing max_string_attribute_value_len until it fits into buffer instead of crude divion 1051 uint16_t max_value_len = total_event_payload_for_string_attributes > string_attributes_len? max_string_attribute_value_len : total_event_payload_for_string_attributes/(string_attributes_len+1) - 1; 1052 // printf("num_string_attributes %d, string_attributes_len %d, total_event_payload_for_string_attributes %d, max_value_len %d \n", num_string_attributes, string_attributes_len, total_event_payload_for_string_attributes, max_value_len); 1053 1054 const uint8_t attribute_order[] = { 1055 AVRCP_MEDIA_ATTR_TRACK, 1056 AVRCP_MEDIA_ATTR_TOTAL_TRACKS, 1057 AVRCP_MEDIA_ATTR_SONG_LENGTH, 1058 AVRCP_MEDIA_ATTR_TITLE, 1059 AVRCP_MEDIA_ATTR_ARTIST, 1060 AVRCP_MEDIA_ATTR_ALBUM, 1061 AVRCP_MEDIA_ATTR_GENRE 1062 }; 1063 1064 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 1065 event[0] = HCI_EVENT_AVRCP_META; 1066 pos = 2; 1067 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO; 1068 little_endian_store_16(event, pos, connection->avrcp_cid); 1069 pos += 2; 1070 event[pos++] = ctype; 1071 for (i = 0; i < sizeof(attribute_order); i++){ 1072 avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) attribute_order[i]; 1073 uint16_t value_len = 0; 1074 switch (attr_id){ 1075 case AVRCP_MEDIA_ATTR_TITLE: 1076 case AVRCP_MEDIA_ATTR_ARTIST: 1077 case AVRCP_MEDIA_ATTR_ALBUM: 1078 case AVRCP_MEDIA_ATTR_GENRE: 1079 if (items[attr_id-1].value){ 1080 value_len = items[attr_id-1].len <= max_value_len ? items[attr_id-1].len : max_value_len; 1081 } 1082 event[pos++] = value_len + 1; 1083 if (value_len){ 1084 memcpy(event+pos, items[attr_id-1].value, value_len); 1085 pos += value_len; 1086 } 1087 event[pos++] = 0; 1088 break; 1089 case AVRCP_MEDIA_ATTR_SONG_LENGTH: 1090 if (items[attr_id-1].value){ 1091 little_endian_store_32(event, pos, btstack_atoi((char *)items[attr_id-1].value)); 1092 } else { 1093 little_endian_store_32(event, pos, 0); 1094 } 1095 pos += 4; 1096 break; 1097 case AVRCP_MEDIA_ATTR_TRACK: 1098 case AVRCP_MEDIA_ATTR_TOTAL_TRACKS: 1099 if (items[attr_id-1].value){ 1100 event[pos++] = btstack_atoi((char *)items[attr_id-1].value); 1101 } else { 1102 event[pos++] = 0; 1103 } 1104 break; 1105 } 1106 } 1107 event[1] = pos - 2; 1108 // printf_hexdump(event, pos); 1109 (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, pos); 1110 break; 1111 } 1112 default: 1113 break; 1114 } 1115 break; 1116 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 1117 // 0x80 | connection->cmd_operands[0] 1118 uint8_t operation_id = packet[pos++]; 1119 switch (connection->state){ 1120 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1121 if (connection->continuous_fast_forward_cmd){ 1122 connection->state = AVCTP_W4_STOP; 1123 } else { 1124 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1125 } 1126 break; 1127 case AVCTP_W2_RECEIVE_RESPONSE: 1128 connection->state = AVCTP_CONNECTION_OPENED; 1129 break; 1130 default: 1131 // check for notifications? move state transition down 1132 // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 1133 break; 1134 } 1135 if (connection->state == AVCTP_W4_STOP){ 1136 avrcp_emit_operation_status(avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 1137 } 1138 if (connection->state == AVCTP_CONNECTION_OPENED) { 1139 // RELEASE response 1140 operation_id = operation_id & 0x7F; 1141 avrcp_emit_operation_status(avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 1142 } 1143 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 1144 // PRESS response 1145 request_pass_through_release_control_cmd(connection); 1146 } 1147 break; 1148 } 1149 default: 1150 break; 1151 } 1152 } 1153 1154 static void avrcp_handle_can_send_now(avrcp_connection_t * connection){ 1155 int i; 1156 switch (connection->state){ 1157 case AVCTP_W2_SEND_PRESS_COMMAND: 1158 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 1159 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 1160 break; 1161 case AVCTP_W2_SEND_COMMAND: 1162 case AVCTP_W2_SEND_RELEASE_COMMAND: 1163 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1164 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 1165 break; 1166 case AVCTP_CONNECTION_OPENED: 1167 if (connection->notifications_to_register != 0){ 1168 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 1169 if (connection->notifications_to_register & (1<<i)){ 1170 connection->notifications_to_register &= ~ (1 << i); 1171 avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 1172 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1173 avrcp_send_cmd(connection->l2cap_signaling_cid, connection); 1174 return; 1175 } 1176 } 1177 } 1178 return; 1179 default: 1180 return; 1181 } 1182 } 1183 1184 static avrcp_connection_t * avrcp_create_connection(bd_addr_t remote_addr, avrcp_context_t * context){ 1185 avrcp_connection_t * connection = btstack_memory_avrcp_connection_get(); 1186 memset(connection, 0, sizeof(avrcp_connection_t)); 1187 connection->state = AVCTP_CONNECTION_IDLE; 1188 connection->transaction_label = 0xFF; 1189 memcpy(connection->remote_addr, remote_addr, 6); 1190 btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection); 1191 return connection; 1192 } 1193 1194 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ 1195 bd_addr_t event_addr; 1196 uint16_t local_cid; 1197 avrcp_connection_t * connection = NULL; 1198 1199 switch (packet_type) { 1200 case L2CAP_DATA_PACKET: 1201 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, context); 1202 if (!connection) break; 1203 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1204 break; 1205 1206 case HCI_EVENT_PACKET: 1207 switch (hci_event_packet_get_type(packet)) { 1208 case L2CAP_EVENT_INCOMING_CONNECTION: 1209 l2cap_event_incoming_connection_get_address(packet, event_addr); 1210 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 1211 connection = avrcp_create_connection(event_addr, context); 1212 if (!connection) { 1213 log_error("Failed to alloc connection structure"); 1214 l2cap_decline_connection(local_cid); 1215 break; 1216 } 1217 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 1218 connection->l2cap_signaling_cid = local_cid; 1219 l2cap_accept_connection(local_cid); 1220 break; 1221 1222 case L2CAP_EVENT_CHANNEL_OPENED: 1223 l2cap_event_channel_opened_get_address(packet, event_addr); 1224 1225 connection = get_avrcp_connection_for_bd_addr(event_addr, context); 1226 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 1227 1228 if (!connection){ 1229 // incoming connection 1230 connection = avrcp_create_connection(event_addr, context); 1231 if (!connection) { 1232 log_error("Failed to alloc connection structure"); 1233 l2cap_disconnect(local_cid, 0); // reason isn't used 1234 break; 1235 } 1236 connection->l2cap_signaling_cid = local_cid; 1237 connection->avrcp_cid = avrcp_cid_counter++; 1238 } 1239 1240 if (l2cap_event_channel_opened_get_status(packet)){ 1241 log_info("L2CAP connection to connection %s failed. status code 0x%02x", 1242 bd_addr_to_str(event_addr), l2cap_event_channel_opened_get_status(packet)); 1243 if (connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED && connection->l2cap_signaling_cid == local_cid){ 1244 avrcp_emit_connection_established(avrcp_callback, connection->avrcp_cid, event_addr, l2cap_event_channel_opened_get_status(packet)); 1245 } 1246 // free connection 1247 btstack_linked_list_remove(&context->connections, (btstack_linked_item_t*) connection); 1248 btstack_memory_avrcp_connection_free(connection); 1249 break; 1250 } 1251 connection->l2cap_signaling_cid = local_cid; 1252 log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x",connection->avrcp_cid, connection->l2cap_signaling_cid); 1253 connection->state = AVCTP_CONNECTION_OPENED; 1254 avrcp_emit_connection_established(avrcp_callback, connection->avrcp_cid, event_addr, ERROR_CODE_SUCCESS); 1255 1256 // hack 1257 // l2cap_create_channel(avrcp_controller_context.packet_handler, connection->remote_addr, 0x001b, l2cap_max_mtu(), NULL); 1258 break; 1259 1260 case L2CAP_EVENT_CAN_SEND_NOW: 1261 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, context); 1262 if (!connection) break; 1263 avrcp_handle_can_send_now(connection); 1264 break; 1265 1266 case L2CAP_EVENT_CHANNEL_CLOSED: 1267 // data: event (8), len(8), channel (16) 1268 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 1269 connection = get_avrcp_connection_for_l2cap_signaling_cid(local_cid, context); 1270 if (connection){ 1271 avrcp_emit_connection_closed(avrcp_callback, connection->avrcp_cid); 1272 // free connection 1273 btstack_linked_list_remove(&context->connections, (btstack_linked_item_t*) connection); 1274 btstack_memory_avrcp_connection_free(connection); 1275 break; 1276 } 1277 break; 1278 default: 1279 break; 1280 } 1281 break; 1282 default: 1283 break; 1284 } 1285 } 1286 1287 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1288 packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 1289 } 1290 1291 void avrcp_controller_init(void){ 1292 avrcp_controller_context.role = AVRCP_CONTROLLER; 1293 avrcp_controller_context.connections = NULL; 1294 avrcp_controller_context.avrcp_callback = avrcp_callback; 1295 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1296 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 1297 } 1298 1299 void avrcp_register_packet_handler(btstack_packet_handler_t callback){ 1300 if (callback == NULL){ 1301 log_error("avrcp_register_packet_handler called with NULL callback"); 1302 return; 1303 } 1304 avrcp_callback = callback; 1305 avrcp_controller_context.avrcp_callback = avrcp_callback; 1306 } 1307 1308 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid){ 1309 avrcp_connection_t * connection = get_avrcp_connection_for_bd_addr(bd_addr, context); 1310 if (connection){ 1311 return ERROR_CODE_COMMAND_DISALLOWED; 1312 } 1313 1314 connection = avrcp_create_connection(bd_addr, context); 1315 if (!connection){ 1316 log_error("avrcp: could not allocate connection struct."); 1317 return BTSTACK_MEMORY_ALLOC_FAILED; 1318 } 1319 1320 if (!avrcp_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 1321 1322 *avrcp_cid = avrcp_cid_counter++; 1323 connection->avrcp_cid = *avrcp_cid; 1324 1325 connection->state = AVCTP_SIGNALING_W4_SDP_QUERY_COMPLETE; 1326 sdp_query_context.avrcp_context = context; 1327 sdp_query_context.avrcp_l2cap_psm = 0; 1328 sdp_query_context.avrcp_version = 0; 1329 sdp_query_context.connection = connection; 1330 1331 sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, bd_addr, BLUETOOTH_PROTOCOL_AVCTP); 1332 return ERROR_CODE_SUCCESS; 1333 } 1334 1335 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 1336 return avrcp_connect(bd_addr, &avrcp_controller_context, avrcp_cid); 1337 } 1338 1339 uint8_t avrcp_unit_info(uint16_t avrcp_cid){ 1340 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1341 if (!connection){ 1342 log_error("avrcp_unit_info: could not find a connection."); 1343 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1344 } 1345 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1346 connection->state = AVCTP_W2_SEND_COMMAND; 1347 1348 connection->transaction_label++; 1349 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 1350 connection->command_type = AVRCP_CTYPE_STATUS; 1351 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1352 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1353 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1354 connection->cmd_operands_length = 5; 1355 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1356 return ERROR_CODE_SUCCESS; 1357 } 1358 1359 static uint8_t avrcp_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 1360 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1361 if (!connection){ 1362 log_error("avrcp_get_capabilities: could not find a connection."); 1363 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1364 } 1365 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1366 connection->state = AVCTP_W2_SEND_COMMAND; 1367 1368 connection->transaction_label++; 1369 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1370 connection->command_type = AVRCP_CTYPE_STATUS; 1371 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1372 connection->subunit_id = AVRCP_SUBUNIT_ID; 1373 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1374 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 1375 connection->cmd_operands[4] = 0; 1376 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 1377 connection->cmd_operands[7] = capability_id; // capability ID 1378 connection->cmd_operands_length = 8; 1379 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1380 return ERROR_CODE_SUCCESS; 1381 } 1382 1383 uint8_t avrcp_get_supported_company_ids(uint16_t avrcp_cid){ 1384 return avrcp_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 1385 } 1386 1387 uint8_t avrcp_get_supported_events(uint16_t avrcp_cid){ 1388 return avrcp_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 1389 } 1390 1391 1392 uint8_t avrcp_play(uint16_t avrcp_cid){ 1393 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1394 } 1395 1396 uint8_t avrcp_stop(uint16_t avrcp_cid){ 1397 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1398 } 1399 1400 uint8_t avrcp_pause(uint16_t avrcp_cid){ 1401 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1402 } 1403 1404 uint8_t avrcp_forward(uint16_t avrcp_cid){ 1405 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1406 } 1407 1408 uint8_t avrcp_backward(uint16_t avrcp_cid){ 1409 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1410 } 1411 1412 uint8_t avrcp_start_rewind(uint16_t avrcp_cid){ 1413 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1414 } 1415 1416 uint8_t avrcp_volume_up(uint16_t avrcp_cid){ 1417 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1418 } 1419 1420 uint8_t avrcp_volume_down(uint16_t avrcp_cid){ 1421 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1422 } 1423 1424 uint8_t avrcp_mute(uint16_t avrcp_cid){ 1425 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1426 } 1427 1428 uint8_t avrcp_skip(uint16_t avrcp_cid){ 1429 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1430 } 1431 1432 uint8_t avrcp_stop_rewind(uint16_t avrcp_cid){ 1433 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1434 if (!connection){ 1435 log_error("avrcp_stop_rewind: could not find a connection."); 1436 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1437 } 1438 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1439 return request_pass_through_release_control_cmd(connection); 1440 } 1441 1442 uint8_t avrcp_start_fast_forward(uint16_t avrcp_cid){ 1443 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1444 } 1445 1446 uint8_t avrcp_fast_forward(uint16_t avrcp_cid){ 1447 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1448 } 1449 1450 uint8_t avrcp_rewind(uint16_t avrcp_cid){ 1451 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1452 } 1453 1454 1455 uint8_t avrcp_stop_fast_forward(uint16_t avrcp_cid){ 1456 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1457 if (!connection){ 1458 log_error("avrcp_stop_fast_forward: could not find a connection."); 1459 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1460 } 1461 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1462 return request_pass_through_release_control_cmd(connection); 1463 } 1464 1465 uint8_t avrcp_get_play_status(uint16_t avrcp_cid){ 1466 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1467 if (!connection){ 1468 log_error("avrcp_get_play_status: could not find a connection."); 1469 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1470 } 1471 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1472 connection->state = AVCTP_W2_SEND_COMMAND; 1473 connection->transaction_label++; 1474 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1475 connection->command_type = AVRCP_CTYPE_STATUS; 1476 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1477 connection->subunit_id = AVRCP_SUBUNIT_ID; 1478 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1479 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1480 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1481 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1482 connection->cmd_operands_length = 7; 1483 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1484 return ERROR_CODE_SUCCESS; 1485 } 1486 1487 uint8_t avrcp_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1488 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1489 if (!connection){ 1490 log_error("avrcp_get_play_status: could not find a connection."); 1491 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1492 } 1493 avrcp_register_notification(connection, event_id); 1494 return ERROR_CODE_SUCCESS; 1495 } 1496 1497 uint8_t avrcp_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1498 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1499 if (!connection){ 1500 log_error("avrcp_get_play_status: could not find a connection."); 1501 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1502 } 1503 connection->notifications_to_deregister |= (1 << event_id); 1504 return ERROR_CODE_SUCCESS; 1505 } 1506 1507 uint8_t avrcp_get_now_playing_info(uint16_t avrcp_cid){ 1508 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1509 if (!connection){ 1510 log_error("avrcp_get_capabilities: could not find a connection."); 1511 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1512 } 1513 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1514 connection->state = AVCTP_W2_SEND_COMMAND; 1515 1516 connection->transaction_label++; 1517 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1518 connection->command_type = AVRCP_CTYPE_STATUS; 1519 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1520 connection->subunit_id = AVRCP_SUBUNIT_ID; 1521 int pos = 0; 1522 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1523 pos += 3; 1524 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1525 connection->cmd_operands[pos++] = 0; 1526 1527 // Parameter Length 1528 big_endian_store_16(connection->cmd_operands, pos, 9); 1529 pos += 2; 1530 1531 // write 8 bytes value 1532 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1533 pos += 8; 1534 1535 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 1536 // every attribute is 4 bytes long 1537 1538 connection->cmd_operands_length = pos; 1539 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1540 return ERROR_CODE_SUCCESS; 1541 } 1542 1543 uint8_t avrcp_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1544 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1545 if (!connection){ 1546 log_error("avrcp_get_capabilities: could not find a connection."); 1547 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1548 } 1549 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1550 connection->state = AVCTP_W2_SEND_COMMAND; 1551 1552 connection->transaction_label++; 1553 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1554 connection->command_type = AVRCP_CTYPE_CONTROL; 1555 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1556 connection->subunit_id = AVRCP_SUBUNIT_ID; 1557 int pos = 0; 1558 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1559 pos += 3; 1560 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1561 connection->cmd_operands[pos++] = 0; 1562 1563 // Parameter Length 1564 big_endian_store_16(connection->cmd_operands, pos, 1); 1565 pos += 2; 1566 connection->cmd_operands[pos++] = volume; 1567 1568 connection->cmd_operands_length = pos; 1569 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1570 return ERROR_CODE_SUCCESS; 1571 } 1572 1573 uint8_t avrcp_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1574 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1575 if (!connection){ 1576 log_error("avrcp_get_capabilities: could not find a connection."); 1577 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1578 } 1579 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1580 connection->state = AVCTP_W2_SEND_COMMAND; 1581 1582 connection->transaction_label++; 1583 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1584 connection->command_type = AVRCP_CTYPE_STATUS; 1585 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1586 connection->subunit_id = AVRCP_SUBUNIT_ID; 1587 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1588 connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 1589 connection->cmd_operands[4] = 0; 1590 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1591 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1592 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1593 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1594 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1595 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1596 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1597 connection->cmd_operands_length = 12; 1598 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1599 return ERROR_CODE_SUCCESS; 1600 } 1601 1602 static uint8_t avrcp_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1603 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1604 if (!connection){ 1605 log_error("avrcp_get_capabilities: could not find a connection."); 1606 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1607 } 1608 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1609 connection->state = AVCTP_W2_SEND_COMMAND; 1610 1611 connection->transaction_label++; 1612 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1613 connection->command_type = AVRCP_CTYPE_CONTROL; 1614 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1615 connection->subunit_id = AVRCP_SUBUNIT_ID; 1616 int pos = 0; 1617 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1618 pos += 3; 1619 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 1620 connection->cmd_operands[pos++] = 0; 1621 // Parameter Length 1622 big_endian_store_16(connection->cmd_operands, pos, 3); 1623 pos += 2; 1624 connection->cmd_operands[pos++] = 2; 1625 connection->cmd_operands_length = pos; 1626 connection->cmd_operands[pos++] = attr_id; 1627 connection->cmd_operands[pos++] = attr_value; 1628 connection->cmd_operands_length = pos; 1629 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1630 return ERROR_CODE_SUCCESS; 1631 } 1632 1633 uint8_t avrcp_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1634 if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1635 return avrcp_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1636 } 1637 1638 uint8_t avrcp_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1639 if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1640 return avrcp_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1641 } 1642 1643 uint8_t avrcp_disconnect(uint16_t avrcp_cid){ 1644 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1645 if (!connection){ 1646 log_error("avrcp_get_capabilities: could not find a connection."); 1647 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1648 } 1649 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1650 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1651 return ERROR_CODE_SUCCESS; 1652 }