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