1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "avrcp_target.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <inttypes.h> 44 45 #include "classic/avrcp.h" 46 #include "classic/avrcp_target.h" 47 48 #include "bluetooth_sdp.h" 49 #include "btstack_debug.h" 50 #include "btstack_event.h" 51 #include "btstack_util.h" 52 #include "l2cap.h" 53 54 #include <stdio.h> 55 #define AVRCP_ATTR_HEADER_LEN 8 56 57 static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0}; 58 static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; 59 60 avrcp_context_t avrcp_target_context; 61 62 static uint32_t default_companies[] = { 63 0x581900 //BT SIG registered CompanyID 64 }; 65 66 static int avrcp_target_supports_browsing(uint16_t target_supported_features){ 67 return target_supported_features & AVRCP_FEATURE_MASK_BROWSING; 68 } 69 70 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 71 avrcp_create_sdp_record(0, service, service_record_handle, avrcp_target_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 72 } 73 74 static void 75 avrcp_target_emit_operation(btstack_packet_handler_t callback, uint16_t avrcp_cid, avrcp_operation_id_t operation_id, 76 bool button_pressed, uint8_t operands_length, uint8_t operand) { 77 btstack_assert(callback != NULL); 78 79 uint8_t event[9]; 80 int pos = 0; 81 event[pos++] = HCI_EVENT_AVRCP_META; 82 event[pos++] = sizeof(event) - 2; 83 event[pos++] = AVRCP_SUBEVENT_OPERATION; 84 little_endian_store_16(event, pos, avrcp_cid); 85 pos += 2; 86 event[pos++] = operation_id; 87 event[pos++] = button_pressed ? 1 : 0; 88 event[pos++] = operands_length; 89 event[pos++] = operand; 90 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 91 } 92 93 static void avrcp_target_emit_volume_changed(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t absolute_volume){ 94 btstack_assert(callback != NULL); 95 96 uint8_t event[7]; 97 int offset = 0; 98 event[offset++] = HCI_EVENT_AVRCP_META; 99 event[offset++] = sizeof(event) - 2; 100 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 101 little_endian_store_16(event, offset, avrcp_cid); 102 offset += 2; 103 event[offset++] = AVRCP_CTYPE_NOTIFY; 104 event[offset++] = absolute_volume; 105 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 106 } 107 108 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){ 109 btstack_assert(callback != NULL); 110 111 uint8_t event[5]; 112 int pos = 0; 113 event[pos++] = HCI_EVENT_AVRCP_META; 114 event[pos++] = sizeof(event) - 2; 115 event[pos++] = subevent_id; 116 little_endian_store_16(event, pos, avrcp_cid); 117 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 118 } 119 120 // returns number of bytes stored 121 static uint16_t avrcp_target_pack_single_element_header(uint8_t * buffer, avrcp_media_attribute_id_t attr_id, uint16_t attr_value_size){ 122 btstack_assert(attr_id > AVRCP_MEDIA_ATTR_ALL); 123 btstack_assert(attr_id < AVRCP_MEDIA_ATTR_RESERVED); 124 uint16_t pos = 0; 125 big_endian_store_32(buffer, pos, attr_id); 126 big_endian_store_16(buffer, pos + 4, RFC2978_CHARSET_MIB_UTF8); 127 big_endian_store_16(buffer, pos + 6, attr_value_size); 128 return 8; 129 } 130 131 static uint16_t avrcp_now_playing_info_attr_id_value_len(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id){ 132 char buffer[AVRCP_MAX_ATTRIBUTE_SIZE]; 133 uint16_t str_len; 134 switch (attr_id) { 135 case AVRCP_MEDIA_ATTR_ALL: 136 case AVRCP_MEDIA_ATTR_NONE: 137 return 0; 138 case AVRCP_MEDIA_ATTR_TRACK: 139 str_len = sprintf(buffer, "%0" PRIu32, connection->target_track_nr); 140 break; 141 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 142 str_len = sprintf(buffer, "%0" PRIu32, connection->target_total_tracks); 143 break; 144 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 145 str_len = sprintf(buffer, "%0" PRIu32, connection->target_song_length_ms); 146 break; 147 default: 148 str_len = connection->target_now_playing_info[(uint16_t)attr_id - 1].len; 149 break; 150 } 151 return str_len; 152 } 153 154 static uint16_t avrcp_now_playing_info_value_len_with_headers(avrcp_connection_t * connection){ 155 uint16_t playing_info_len = 0; 156 157 uint8_t i; 158 for ( i = (uint8_t)AVRCP_MEDIA_ATTR_ALL + 1; i < (uint8_t) AVRCP_MEDIA_ATTR_RESERVED; i++){ 159 avrcp_media_attribute_id_t attr_id = (avrcp_media_attribute_id_t) i; 160 161 if ((connection->target_now_playing_info_attr_bitmap & (1 << attr_id)) == 0) { 162 continue; 163 } 164 165 switch (attr_id) { 166 case AVRCP_MEDIA_ATTR_ALL: 167 case AVRCP_MEDIA_ATTR_NONE: 168 case AVRCP_MEDIA_ATTR_DEFAULT_COVER_ART: 169 break; 170 default: 171 playing_info_len += AVRCP_ATTR_HEADER_LEN + avrcp_now_playing_info_attr_id_value_len(connection, attr_id); 172 break; 173 } 174 } 175 // for total num bytes that of the attributes + headers 176 playing_info_len += 1; 177 return playing_info_len; 178 } 179 180 static uint8_t * avrcp_get_attribute_value_from_u32(avrcp_connection_t * connection, uint32_t value, uint16_t * num_bytes_to_copy){ 181 *num_bytes_to_copy = 0; 182 183 if (connection->attribute_value_len == 0){ 184 connection->attribute_value_len = sprintf((char *)connection->attribute_value, "%0" PRIu32, value); 185 connection->attribute_value_offset = 0; 186 } 187 *num_bytes_to_copy = connection->attribute_value_len - connection->attribute_value_offset; 188 return connection->attribute_value + connection->attribute_value_offset; 189 } 190 191 static uint8_t * avrcp_get_next_value_fragment_for_attribute_id(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, uint16_t * num_bytes_to_copy){ 192 switch (attr_id){ 193 case AVRCP_MEDIA_ATTR_TRACK: 194 return avrcp_get_attribute_value_from_u32(connection, connection->target_track_nr, num_bytes_to_copy); 195 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 196 return avrcp_get_attribute_value_from_u32(connection, connection->target_total_tracks, num_bytes_to_copy); 197 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 198 return avrcp_get_attribute_value_from_u32(connection, connection->target_song_length_ms, num_bytes_to_copy); 199 default: 200 break; 201 } 202 int attr_index = attr_id - 1; 203 if (connection->attribute_value_len == 0){ 204 connection->attribute_value_len = avrcp_now_playing_info_attr_id_value_len(connection, attr_id); 205 connection->attribute_value_offset = 0; 206 } 207 *num_bytes_to_copy = connection->target_now_playing_info[attr_index].len - connection->attribute_value_offset; 208 return (uint8_t *) (connection->target_now_playing_info[attr_index].value + connection->attribute_value_offset); 209 } 210 211 // TODO Review 212 static uint16_t avrcp_store_avctp_now_playing_info_fragment(avrcp_connection_t * connection, uint16_t packet_size, uint8_t * packet){ 213 uint16_t num_free_bytes = packet_size; 214 215 uint16_t bytes_stored = 0; 216 217 while ((num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){ 218 if ((connection->target_now_playing_info_attr_bitmap & (1 << (uint8_t)connection->next_attr_id)) == 0) { 219 connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1); 220 continue; 221 } 222 223 // prepare attribute value 224 uint16_t num_bytes_to_copy; 225 uint8_t * attr_value_with_offset = avrcp_get_next_value_fragment_for_attribute_id(connection, 226 connection->next_attr_id, 227 &num_bytes_to_copy); 228 229 // store header 230 if (connection->attribute_value_offset == 0){ 231 // pack the whole attribute value header 232 if (connection->parser_attribute_header_pos == 0) { 233 avrcp_target_pack_single_element_header(connection->parser_attribute_header, connection->next_attr_id, 234 connection->attribute_value_len); 235 } 236 } 237 238 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN){ 239 uint16_t num_header_bytes_to_store = btstack_min(num_free_bytes, AVRCP_ATTRIBUTE_HEADER_LEN - connection->parser_attribute_header_pos); 240 memcpy(packet + bytes_stored, connection->parser_attribute_header + connection->parser_attribute_header_pos, num_header_bytes_to_store); 241 connection->parser_attribute_header_pos += num_header_bytes_to_store; 242 bytes_stored += num_header_bytes_to_store; 243 num_free_bytes -= num_header_bytes_to_store; 244 connection->data_offset += num_header_bytes_to_store; 245 246 if (num_free_bytes == 0){ 247 continue; 248 } 249 } 250 251 // store value 252 uint16_t num_attr_value_bytes_to_store = btstack_min(num_free_bytes, connection->attribute_value_len - connection->attribute_value_offset); 253 memcpy(packet + bytes_stored, attr_value_with_offset, num_attr_value_bytes_to_store); 254 bytes_stored += num_attr_value_bytes_to_store; 255 num_free_bytes -= num_attr_value_bytes_to_store; 256 connection->attribute_value_offset += num_attr_value_bytes_to_store; 257 connection->data_offset += num_attr_value_bytes_to_store; 258 259 if (connection->attribute_value_offset == connection->attribute_value_len){ 260 // C++ compatible version of connection->next_attr_id++ 261 connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1); 262 connection->attribute_value_offset = 0; 263 connection->attribute_value_len = 0; 264 connection->parser_attribute_header_pos = 0; 265 } 266 } 267 return bytes_stored; 268 } 269 270 static void avrcp_send_response_with_avctp_fragmentation(avrcp_connection_t * connection){ 271 l2cap_reserve_packet_buffer(); 272 uint8_t * packet = l2cap_get_outgoing_buffer(); 273 274 // transport header 275 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 276 277 uint16_t max_payload_size; 278 connection->avctp_packet_type = avctp_get_packet_type(connection, &max_payload_size); 279 connection->avrcp_packet_type = avrcp_get_packet_type(connection); 280 281 // AVCTP header 282 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 283 uint16_t pos = 0; 284 packet[pos++] = (connection->transaction_id << 4) | (connection->avctp_packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 285 286 uint16_t param_len = connection->data_len; 287 288 if (connection->avctp_packet_type == AVCTP_START_PACKET){ 289 uint16_t max_frame_size = btstack_min(connection->l2cap_mtu, AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE); 290 // first packet: max_payload_size 291 // rest packets 292 uint16_t num_payload_bytes = param_len - max_payload_size; 293 uint16_t frame_size_for_continue_packet = max_frame_size - avctp_get_num_bytes_for_header(AVCTP_CONTINUE_PACKET); 294 uint16_t num_avctp_packets = (num_payload_bytes + frame_size_for_continue_packet - 1)/frame_size_for_continue_packet + 1; 295 packet[pos++] = num_avctp_packets; 296 } 297 298 uint16_t bytes_stored = 0; 299 uint8_t i; 300 301 switch (connection->avctp_packet_type) { 302 case AVCTP_SINGLE_PACKET: 303 case AVCTP_START_PACKET: 304 // Profile IDentifier (PID) 305 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 306 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 307 308 // AVRCP message 309 // command_type 310 packet[pos++] = connection->command_type; 311 // subunit_type | subunit ID 312 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 313 // opcode 314 packet[pos++] = (uint8_t) connection->command_opcode; 315 316 switch (connection->command_opcode) { 317 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 318 big_endian_store_24(packet, pos, connection->company_id); 319 pos += 3; 320 packet[pos++] = connection->pdu_id; 321 // AVRCP packet type 322 323 packet[pos++] = (uint8_t)connection->avrcp_packet_type; 324 // parameter length 325 big_endian_store_16(packet, pos, param_len); 326 pos += 2; 327 328 switch (connection->pdu_id) { 329 // message is small enough to fit the single packet, no need for extra check 330 case AVRCP_PDU_ID_GET_CAPABILITIES: 331 // capability ID 332 packet[pos++] = connection->data[0]; 333 // num_capabilities 334 packet[pos++] = connection->data[1]; 335 336 switch ((avrcp_capability_id_t) connection->data[0]) { 337 case AVRCP_CAPABILITY_ID_EVENT: 338 for (i = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; 339 i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++) { 340 if ((connection->notifications_supported_by_target & (1 << i)) == 0) { 341 continue; 342 } 343 packet[pos++] = i; 344 } 345 break; 346 case AVRCP_CAPABILITY_ID_COMPANY: 347 // use Bluetooth SIG as default company 348 for (i = 0; i < connection->data[1]; i++) { 349 little_endian_store_24(packet, pos, 350 connection->target_supported_companies[i]); 351 pos += 3; 352 } 353 break; 354 default: 355 // error response 356 break; 357 } 358 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 359 return; 360 361 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 362 packet[pos++] = count_set_bits_uint32(connection->target_now_playing_info_attr_bitmap); 363 max_payload_size--; 364 365 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 366 367 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 368 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 369 return; 370 371 default: 372 // error response and other OPCODEs 373 break; 374 } 375 break; 376 377 case AVRCP_CMD_OPCODE_PASS_THROUGH: 378 packet[pos++] = connection->operation_id; 379 // parameter length 380 packet[pos++] = (uint8_t) connection->data_len; 381 pos += 2; 382 break; 383 case AVRCP_CMD_OPCODE_UNIT_INFO: 384 break; 385 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 386 break; 387 default: 388 btstack_assert(false); 389 return; 390 } 391 break; 392 case AVCTP_CONTINUE_PACKET: 393 case AVCTP_END_PACKET: 394 switch (connection->pdu_id) { 395 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 396 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 397 398 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 399 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 400 return; 401 402 default: 403 break; 404 } 405 break; 406 default: 407 btstack_assert(false); 408 return; 409 } 410 411 // compare number of bytes to store with the remaining buffer size 412 uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_payload_size - pos); 413 414 (void)memcpy(packet + pos, &connection->data[connection->data_offset], bytes_to_copy); 415 pos += bytes_to_copy; 416 connection->data_offset += bytes_to_copy; 417 connection->avrcp_frame_bytes_sent += pos; 418 419 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 420 } 421 422 static void avctp_send_reject_cmd_wrong_pid(avrcp_connection_t * connection){ 423 l2cap_reserve_packet_buffer(); 424 uint8_t * packet = l2cap_get_outgoing_buffer(); 425 426 // AVCTP header 427 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 428 packet[0] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 1; 429 big_endian_store_16(packet, 1, connection->message_body[0]); 430 l2cap_send_prepared(connection->l2cap_signaling_cid, 3); 431 } 432 433 static void avrcp_target_custome_command_data_init(avrcp_connection_t * connection, 434 avrcp_command_opcode_t opcode, avrcp_command_type_t command_type, 435 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 436 avrcp_pdu_id_t pdu_id, uint32_t company_id){ 437 438 connection->command_opcode = opcode; 439 connection->command_type = command_type; 440 connection->subunit_type = subunit_type; 441 connection->subunit_id = subunit_id; 442 connection->company_id = company_id << 16; 443 connection->pdu_id = pdu_id; 444 connection->data = NULL; 445 connection->data_offset = 0; 446 connection->data_len = 0; 447 connection->avrcp_frame_bytes_sent = 0; 448 } 449 450 static void avrcp_target_vendor_dependent_response_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_pdu_id_t pdu_id){ 451 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 452 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 453 connection->subunit_id = AVRCP_SUBUNIT_ID; 454 connection->company_id = BT_SIG_COMPANY_ID; 455 456 connection->command_type = command_type; 457 connection->pdu_id = pdu_id; 458 connection->data = connection->message_body; 459 connection->data_offset = 0; 460 connection->data_len = 0; 461 connection->avrcp_frame_bytes_sent = 0; 462 } 463 464 static void avrcp_target_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_operation_id_t opid){ 465 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 466 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 467 connection->subunit_id = AVRCP_SUBUNIT_ID; 468 469 connection->command_type = command_type; 470 connection->company_id = 0; 471 connection->pdu_id = AVRCP_PDU_ID_UNDEFINED; 472 connection->operation_id = opid; 473 474 connection->data = connection->message_body; 475 connection->data_offset = 0; 476 connection->data_len = 0; 477 connection->avrcp_frame_bytes_sent = 0; 478 } 479 480 481 static uint8_t avrcp_target_vendor_dependent_response_accept(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t status){ 482 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, pdu_id); 483 connection->data_len = 1; 484 connection->data[0] = status; 485 486 connection->target_accept_response = true; 487 connection->state = AVCTP_W2_SEND_RESPONSE; 488 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 489 return ERROR_CODE_SUCCESS; 490 } 491 492 static uint8_t avrcp_target_response_vendor_dependent_reject(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){ 493 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_REJECTED, pdu_id); 494 connection->data_len = 1; 495 connection->data[0] = status; 496 497 connection->state = AVCTP_W2_SEND_RESPONSE; 498 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 499 return ERROR_CODE_SUCCESS; 500 } 501 502 static uint8_t avrcp_target_response_vendor_dependent_not_implemented(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 503 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, pdu_id); 504 connection->data_len = 1; 505 connection->data[0] = event_id; 506 507 connection->state = AVCTP_W2_SEND_RESPONSE; 508 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 509 return ERROR_CODE_SUCCESS; 510 } 511 512 static uint8_t avrcp_target_response_vendor_dependent_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id, const uint8_t * value, uint16_t value_len){ 513 btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH); 514 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 515 connection->data_len = 1 + value_len; 516 connection->data[0] = event_id; 517 518 if (value && (value_len > 0)){ 519 (void)memcpy(connection->data + 1, value, value_len); 520 } 521 522 connection->state = AVCTP_W2_SEND_RESPONSE; 523 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 524 return ERROR_CODE_SUCCESS; 525 } 526 527 static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 528 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 529 530 connection->data_len = 5; 531 connection->data[0] = event_id; 532 big_endian_store_16(connection->data, 1, connection->target_addressed_player_id); 533 big_endian_store_16(connection->data, 3, connection->target_uid_counter); 534 535 connection->state = AVCTP_W2_SEND_RESPONSE; 536 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 537 return ERROR_CODE_SUCCESS; 538 } 539 540 static uint8_t avrcp_target_pass_through_response(uint16_t avrcp_cid, avrcp_command_type_t ctype, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 541 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 542 if (!connection){ 543 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 544 } 545 avrcp_target_pass_through_command_data_init(connection, ctype, opid); 546 547 if (operands_length == 1){ 548 connection->data_len = 1; 549 connection->message_body[0] = operand; 550 } 551 552 connection->state = AVCTP_W2_SEND_RESPONSE; 553 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 554 return ERROR_CODE_SUCCESS; 555 } 556 557 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 558 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand); 559 } 560 561 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 562 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 563 } 564 565 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 566 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 567 } 568 569 uint8_t avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){ 570 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 571 if (!connection){ 572 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 573 } 574 connection->target_unit_type = unit_type; 575 connection->company_id = company_id; 576 return ERROR_CODE_SUCCESS; 577 } 578 579 uint8_t avrcp_target_set_subunit_info(uint16_t avrcp_cid, avrcp_subunit_type_t subunit_type, const uint8_t * subunit_info_data, uint16_t subunit_info_data_size){ 580 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 581 if (!connection){ 582 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 583 } 584 connection->target_subunit_info_type = subunit_type; 585 connection->target_subunit_info_data = subunit_info_data; 586 connection->target_subunit_info_data_size = subunit_info_data_size; 587 return ERROR_CODE_SUCCESS; 588 } 589 590 // TODO Review 591 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 592 if (connection->state != AVCTP_CONNECTION_OPENED){ 593 return ERROR_CODE_COMMAND_DISALLOWED; 594 } 595 connection->state = AVCTP_W2_SEND_RESPONSE; 596 597 avrcp_target_custom_command_data_init(connection, 598 AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 599 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 600 connection->company_id); 601 602 uint8_t unit = 0; 603 connection->data = connection->message_body; 604 connection->data_len = 5; 605 connection->data[0] = 0x07; 606 connection->data[1] = (connection->target_unit_type << 4) | unit; 607 // company id is 3 bytes long 608 big_endian_store_24(connection->data, 2, connection->company_id); 609 610 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 611 return ERROR_CODE_SUCCESS; 612 } 613 614 // TODO Review 615 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 616 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 617 if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER; 618 619 connection->state = AVCTP_W2_SEND_RESPONSE; 620 621 avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, 622 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 623 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 624 connection->company_id); 625 626 uint8_t page = offset / 4; 627 uint8_t extension_code = 7; 628 connection->data = connection->message_body; 629 connection->data_len = 5; 630 connection->data[0] = (page << 4) | extension_code; 631 632 // mark non-existent entries with 0xff 633 memset(&connection->message_body[1], 0xFF, 4); 634 if ((connection->data != NULL) && (offset < connection->target_subunit_info_data_size)){ 635 uint8_t bytes_to_copy = btstack_min(connection->target_subunit_info_data_size - offset, 4); 636 memcpy(&connection->data[1], &connection->target_subunit_info_data[offset], bytes_to_copy); 637 } 638 639 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 640 return ERROR_CODE_SUCCESS; 641 } 642 643 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){ 644 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 645 646 uint8_t event_id; 647 uint8_t num_events = 0; 648 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 649 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 650 continue; 651 } 652 num_events++; 653 } 654 655 connection->data[0] = AVRCP_CAPABILITY_ID_EVENT; 656 connection->data[1] = num_events; 657 connection->data_len = 2 + num_events; 658 659 // fill the data later directly to the L2CAP outgoing buffer 660 connection->state = AVCTP_W2_SEND_RESPONSE; 661 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 662 return ERROR_CODE_SUCCESS; 663 } 664 665 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){ 666 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 667 668 connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY; 669 if (connection->target_supported_companies_num == 0){ 670 connection->target_supported_companies_num = 1; 671 connection->target_supported_companies = default_companies; 672 } 673 674 connection->data[1] = connection->target_supported_companies_num; 675 connection->data_len = 2 + connection->data[1] * 3; 676 677 // fill the data later directly to the L2CAP outgoing buffer and 678 // use Bluetooth SIG as default company 679 connection->state = AVCTP_W2_SEND_RESPONSE; 680 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 681 return ERROR_CODE_SUCCESS; 682 } 683 684 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 685 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 686 if (!connection){ 687 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 688 } 689 690 if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 691 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 692 } 693 694 connection->notifications_supported_by_target |= (1 << (uint8_t)event_id); 695 return ERROR_CODE_SUCCESS; 696 } 697 698 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){ 699 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 700 if (!connection){ 701 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 702 } 703 704 connection->target_supported_companies_num = num_companies; 705 connection->target_supported_companies = companies; 706 return ERROR_CODE_SUCCESS; 707 } 708 709 // TODO Review (use flags) 710 uint8_t avrcp_target_play_status(uint16_t avrcp_cid, uint32_t song_length_ms, uint32_t song_position_ms, avrcp_playback_status_t play_status){ 711 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 712 if (!connection){ 713 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 714 } 715 if (connection->state != AVCTP_CONNECTION_OPENED){ 716 return ERROR_CODE_COMMAND_DISALLOWED; 717 } 718 719 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS); 720 connection->data_len = 9; 721 big_endian_store_32(connection->data, 0, song_length_ms); 722 big_endian_store_32(connection->data, 4, song_position_ms); 723 connection->data[8] = play_status; 724 725 connection->state = AVCTP_W2_SEND_RESPONSE; 726 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 727 return ERROR_CODE_SUCCESS; 728 } 729 730 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 731 int index = attr_id - 1; 732 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 733 connection->target_now_playing_info[index].value = (uint8_t*)value; 734 connection->target_now_playing_info[index].len = strlen(value); 735 return ERROR_CODE_SUCCESS; 736 } 737 738 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 739 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 740 if (!connection){ 741 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 742 } 743 if (connection->target_playback_status == playback_status){ 744 return ERROR_CODE_SUCCESS; 745 } 746 747 connection->target_playback_status = playback_status; 748 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) { 749 connection->target_playback_status_changed = true; 750 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 751 } 752 return ERROR_CODE_SUCCESS; 753 } 754 755 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 756 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 757 if (!connection){ 758 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 759 } 760 if (!current_track){ 761 return ERROR_CODE_COMMAND_DISALLOWED; 762 } 763 764 (void)memcpy(connection->target_track_id, current_track->track_id, 8); 765 connection->target_song_length_ms = current_track->song_length_ms; 766 connection->target_track_nr = current_track->track_nr; 767 connection->target_total_tracks = total_tracks; 768 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 769 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 770 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 771 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 772 773 connection->target_track_selected = true; 774 775 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 776 connection->target_track_changed = true; 777 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 778 } 779 return ERROR_CODE_SUCCESS; 780 } 781 782 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 783 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 784 if (!connection){ 785 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 786 } 787 788 if (track_id == NULL){ 789 memset(connection->target_track_id, 0xFF, 8); 790 connection->target_track_selected = false; 791 } else { 792 (void)memcpy(connection->target_track_id, track_id, 8); 793 connection->target_track_selected = true; 794 } 795 796 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 797 connection->target_track_changed = true; 798 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 799 } 800 return ERROR_CODE_SUCCESS; 801 } 802 803 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 804 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 805 if (!connection){ 806 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 807 } 808 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 809 connection->target_playing_content_changed = true; 810 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 811 } 812 return ERROR_CODE_SUCCESS; 813 } 814 815 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){ 816 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 817 if (!connection){ 818 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 819 } 820 821 if (connection->target_addressed_player_id == player_id){ 822 return ERROR_CODE_SUCCESS; 823 } 824 825 connection->target_uid_counter = uid_counter; 826 connection->target_addressed_player_id = player_id; 827 828 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 829 connection->target_addressed_player_changed = true; 830 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 831 } 832 return ERROR_CODE_SUCCESS; 833 } 834 835 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 836 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 837 if (!connection){ 838 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 839 } 840 if (connection->target_battery_status == battery_status){ 841 return ERROR_CODE_SUCCESS; 842 } 843 844 connection->target_battery_status = battery_status; 845 846 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 847 connection->target_battery_status_changed = true; 848 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 849 } 850 return ERROR_CODE_SUCCESS; 851 } 852 853 uint8_t avrcp_target_adjust_absolute_volume(uint16_t avrcp_cid, uint8_t absolute_volume){ 854 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 855 if (!connection){ 856 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 857 } 858 859 connection->target_absolute_volume = absolute_volume; 860 return ERROR_CODE_SUCCESS; 861 } 862 863 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t absolute_volume){ 864 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 865 if (!connection){ 866 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 867 } 868 if (connection->target_absolute_volume == absolute_volume){ 869 return ERROR_CODE_SUCCESS; 870 } 871 872 connection->target_absolute_volume = absolute_volume; 873 874 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 875 connection->target_notify_absolute_volume_changed = true; 876 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 877 } 878 return ERROR_CODE_SUCCESS; 879 } 880 881 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){ 882 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return; 883 connection->target_notifications_transaction_label[notification] = transaction_label; 884 } 885 886 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){ 887 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0; 888 return connection->target_notifications_transaction_label[notification]; 889 } 890 891 static bool avcrp_operation_id_is_valid(avrcp_operation_id_t operation_id){ 892 if (operation_id < AVRCP_OPERATION_ID_RESERVED_1) return true; 893 894 if (operation_id < AVRCP_OPERATION_ID_0) return false; 895 if (operation_id < AVRCP_OPERATION_ID_RESERVED_2) return true; 896 897 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 898 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 899 900 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 901 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 902 903 if (operation_id < AVRCP_OPERATION_ID_SKIP) return false; 904 if (operation_id == AVRCP_OPERATION_ID_SKIP) return true; 905 906 if (operation_id < AVRCP_OPERATION_ID_POWER) return false; 907 if (operation_id < AVRCP_OPERATION_ID_RESERVED_4) return true; 908 909 if (operation_id < AVRCP_OPERATION_ID_ANGLE) return false; 910 if (operation_id < AVRCP_OPERATION_ID_RESERVED_5) return true; 911 912 if (operation_id < AVRCP_OPERATION_ID_F1) return false; 913 if (operation_id < AVRCP_OPERATION_ID_RESERVED_6) return true; 914 915 return false; 916 } 917 918 919 #ifdef ENABLE_AVCTP_FRAGMENTATION 920 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 921 // after header (transaction label and packet type) 922 uint16_t pos; 923 uint16_t bytes_to_store; 924 925 switch (packet_type){ 926 case AVCTP_START_PACKET: 927 if (size < 2) return; 928 929 // store header 930 pos = 0; 931 connection->avctp_reassembly_buffer[pos] = packet[pos]; 932 pos++; 933 connection->avctp_reassembly_size = pos; 934 935 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 936 pos++; 937 938 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 939 940 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 941 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 942 connection->avctp_reassembly_size += bytes_to_store; 943 break; 944 945 case AVCTP_CONTINUE_PACKET: 946 case AVCTP_END_PACKET: 947 if (size < 1) return; 948 949 // store remaining data, ignore header 950 pos = 1; 951 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 952 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 953 connection->avctp_reassembly_size += bytes_to_store; 954 break; 955 956 default: 957 return; 958 } 959 } 960 #endif 961 962 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t * packet, uint16_t size){ 963 uint8_t avctp_header = packet[0]; 964 connection->transaction_id = avctp_header >> 4; 965 966 avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t) ((avctp_header & 0x0F) >> 2); 967 switch (avctp_packet_type){ 968 case AVCTP_SINGLE_PACKET: 969 break; 970 971 #ifdef ENABLE_AVCTP_FRAGMENTATION 972 case AVCTP_START_PACKET: 973 case AVCTP_CONTINUE_PACKET: 974 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 975 return; 976 977 case AVCTP_END_PACKET: 978 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 979 980 packet = connection->avctp_reassembly_buffer; 981 size = connection->avctp_reassembly_size; 982 break; 983 #endif 984 default: 985 return; 986 } 987 988 if (size < 6u) return; 989 990 uint16_t pid = big_endian_read_16(packet, 1); 991 992 if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){ 993 log_info("Invalid pid 0x%02x, expected 0x%02x", pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 994 connection->target_reject_transport_header = true; 995 connection->target_invalid_pid = pid; 996 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 997 return; 998 } 999 1000 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 1001 // avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 1002 1003 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 1004 1005 int pos = 6; 1006 uint16_t length; 1007 avrcp_pdu_id_t pdu_id; 1008 // connection->data_len = 0; 1009 uint8_t offset; 1010 uint8_t operand; 1011 uint16_t event_mask; 1012 avrcp_operation_id_t operation_id; 1013 1014 switch (opcode){ 1015 case AVRCP_CMD_OPCODE_UNIT_INFO: 1016 avrcp_target_unit_info(connection); 1017 break; 1018 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 1019 if ((size - pos) < 3) return; 1020 // page: packet[pos] >> 4, 1021 offset = 4 * (packet[pos]>>4); 1022 // extension code (fixed 7) = packet[pos] & 0x0F 1023 // 4 bytes paga data, all 0xFF 1024 avrcp_target_subunit_info(connection, offset); 1025 break; 1026 1027 case AVRCP_CMD_OPCODE_PASS_THROUGH: 1028 if (size < 8) return; 1029 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 1030 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f); 1031 operand = 0; 1032 if ((packet[7] >= 1) && (size >= 9)){ 1033 operand = packet[8]; 1034 } 1035 1036 if (avcrp_operation_id_is_valid(operation_id)){ 1037 bool button_pressed = (packet[6] & 0x80) == 0; 1038 1039 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1040 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 1041 operation_id, button_pressed, packet[7], operand); 1042 } else { 1043 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1044 } 1045 break; 1046 1047 1048 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 1049 1050 if (size < 13) return; 1051 1052 // pos = 6 - company id 1053 (void)memcpy(connection->message_body, &packet[pos], 3); 1054 // connection->data_len = 3; 1055 pos += 3; 1056 // pos = 9 1057 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 1058 // 1 - reserved 1059 pos++; 1060 // 2-3 param length, 1061 length = big_endian_read_16(packet, pos); 1062 pos += 2; 1063 // pos = 13 1064 switch (pdu_id){ 1065 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 1066 if ((pos + 2) > size) return; 1067 bool ok = length == 4; 1068 if (avrcp_target_context.set_addressed_player_callback != NULL){ 1069 uint16_t player_id = big_endian_read_16(packet, pos); 1070 ok = avrcp_target_context.set_addressed_player_callback(player_id); 1071 } 1072 if (ok){ 1073 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS); 1074 } else { 1075 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 1076 } 1077 break; 1078 } 1079 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1080 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 1081 switch (capability_id){ 1082 case AVRCP_CAPABILITY_ID_EVENT: 1083 avrcp_target_response_vendor_dependent_supported_events(connection); 1084 break; 1085 case AVRCP_CAPABILITY_ID_COMPANY: 1086 avrcp_target_response_vendor_dependent_supported_companies(connection); 1087 break; 1088 default: 1089 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1090 break; 1091 } 1092 break; 1093 } 1094 case AVRCP_PDU_ID_GET_PLAY_STATUS: 1095 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 1096 break; 1097 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1098 if ((pos + 1) > size) return; 1099 connection->message_body[0] = packet[pos]; 1100 connection->target_abort_continue_response = true; 1101 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1102 break; 1103 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 1104 if ((pos + 1) > size) return; 1105 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 1106 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1107 return; 1108 } 1109 connection->target_continue_response = true; 1110 connection->target_now_playing_info_response = true; 1111 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1112 break; 1113 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 1114 if ((pos + 9) > size) return; 1115 uint8_t play_identifier[8]; 1116 memset(play_identifier, 0, 8); 1117 if (memcmp(&packet[pos], play_identifier, 8) != 0) { 1118 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1119 return; 1120 } 1121 pos += 8; 1122 uint8_t attribute_count = packet[pos++]; 1123 connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE; 1124 if (!attribute_count){ 1125 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1126 connection->target_now_playing_info_attr_bitmap = 0xFE; 1127 } else { 1128 int i; 1129 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1130 connection->target_now_playing_info_attr_bitmap = 0; 1131 if ((pos + attribute_count * 4) > size) return; 1132 for (i=0; i < attribute_count; i++){ 1133 uint32_t attr_id = big_endian_read_32(packet, pos); 1134 connection->target_now_playing_info_attr_bitmap |= (1 << attr_id); 1135 pos += 4; 1136 } 1137 } 1138 log_info("target_now_playing_info_attr_bitmap 0x%02x", connection->target_now_playing_info_attr_bitmap); 1139 connection->target_now_playing_info_response = true; 1140 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1141 break; 1142 } 1143 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 1144 if ((pos + 1) > size) return; 1145 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos]; 1146 1147 avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id); 1148 1149 if (event_id < AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED || 1150 event_id > AVRCP_NOTIFICATION_EVENT_MAX_VALUE){ 1151 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1152 return; 1153 } 1154 1155 switch (event_id){ 1156 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED: 1157 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 1158 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 1159 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END: 1160 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START: 1161 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED: 1162 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED: 1163 case AVRCP_NOTIFICATION_EVENT_MAX_VALUE: 1164 avrcp_target_response_vendor_dependent_not_implemented(connection, pdu_id, event_id); 1165 return; 1166 default: 1167 break; 1168 } 1169 1170 event_mask = (1 << event_id); 1171 connection->notifications_enabled |= event_mask; 1172 1173 switch (event_id){ 1174 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED: 1175 if (connection->target_track_selected){ 1176 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8); 1177 } else { 1178 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8); 1179 } 1180 break; 1181 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED: 1182 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_playback_status, 1); 1183 break; 1184 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED: 1185 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0); 1186 break; 1187 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED: 1188 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_absolute_volume, 1); 1189 break; 1190 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED: 1191 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_battery_status, 1); 1192 break; 1193 case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 1194 avrcp_target_response_addressed_player_changed_interim(connection, pdu_id, event_id); 1195 return; 1196 default: 1197 btstack_assert(false); 1198 return; 1199 } 1200 break; 1201 } 1202 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: { 1203 if ( (length != 1) || ((pos + 1) > size)){ 1204 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1205 break; 1206 } 1207 1208 uint8_t absolute_volume = packet[pos]; 1209 if (absolute_volume < 0x80){ 1210 connection->target_absolute_volume = absolute_volume; 1211 } 1212 avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->target_absolute_volume); 1213 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, connection->target_absolute_volume); 1214 break; 1215 } 1216 default: 1217 log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id); 1218 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1219 break; 1220 } 1221 break; 1222 default: 1223 log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size)); 1224 break; 1225 } 1226 } 1227 1228 static void avrcp_target_notification_init(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){ 1229 btstack_assert((value_len == 0) || (value != NULL)); 1230 1231 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1232 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, notification_id); 1233 1234 connection->data_len = 1 + value_len; 1235 connection->data[0] = notification_id; 1236 if (value_len > 0){ 1237 (void)memcpy(connection->data + 1, value, value_len); 1238 } 1239 } 1240 1241 static void avrcp_target_notification_addressed_player_changed_init(avrcp_connection_t * connection){ 1242 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1243 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED); 1244 1245 connection->data_len = 5; 1246 connection->data[0] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1247 big_endian_store_16(connection->data, 1, connection->target_addressed_player_id); 1248 big_endian_store_16(connection->data, 3, connection->target_uid_counter); 1249 } 1250 1251 1252 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){ 1253 if (notification_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX || notification_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX){ 1254 return; 1255 } 1256 connection->notifications_enabled &= ~(1 << notification_id); 1257 connection->target_notifications_transaction_label[notification_id] = 0; 1258 } 1259 1260 static void avrcp_request_next_avctp_segment(avrcp_connection_t * connection){ 1261 // AVCTP 1262 switch (connection->avctp_packet_type){ 1263 case AVCTP_END_PACKET: 1264 case AVCTP_SINGLE_PACKET: 1265 connection->state = AVCTP_CONNECTION_OPENED; 1266 break; 1267 default: 1268 connection->state = AVCTP_W2_SEND_RESPONSE; 1269 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1270 break; 1271 } 1272 } 1273 1274 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1275 avrcp_connection_t * connection; 1276 avrcp_notification_event_id_t notification_id = AVRCP_NOTIFICATION_EVENT_NONE; 1277 1278 switch (packet_type){ 1279 case L2CAP_DATA_PACKET: 1280 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1281 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1282 return; 1283 1284 case HCI_EVENT_PACKET: 1285 if (hci_event_packet_get_type(packet) != L2CAP_EVENT_CAN_SEND_NOW){ 1286 return; 1287 } 1288 1289 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1290 if (connection == NULL){ 1291 return; 1292 } 1293 1294 // START AVCTP 1295 if (connection->target_reject_transport_header){ 1296 connection->target_reject_transport_header = false; 1297 avctp_send_reject_cmd_wrong_pid(connection); 1298 connection->state = AVCTP_CONNECTION_OPENED; 1299 return; 1300 } 1301 // END AVCTP 1302 1303 if (connection->state == AVCTP_W2_SEND_RESPONSE){ 1304 // data already prepared 1305 break; 1306 } 1307 1308 if (connection->target_accept_response){ 1309 connection->target_accept_response = false; 1310 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE); 1311 break; 1312 } 1313 1314 if (connection->target_abort_continue_response){ 1315 connection->target_abort_continue_response = false; 1316 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 1317 break; 1318 } 1319 1320 if (connection->target_now_playing_info_response){ 1321 connection->target_now_playing_info_response = false; 1322 if (connection->target_continue_response){ 1323 if (connection->data_len == 0){ 1324 avrcp_target_response_vendor_dependent_reject(connection, connection->pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1325 return; 1326 } 1327 connection->target_continue_response = false; 1328 } else { 1329 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES); 1330 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection); 1331 } 1332 break; 1333 } 1334 1335 if (connection->target_track_changed){ 1336 connection->target_track_changed = false; 1337 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED; 1338 avrcp_target_notification_init(connection, notification_id, connection->target_track_id, 8); 1339 break; 1340 } 1341 1342 if (connection->target_playback_status_changed){ 1343 connection->target_playback_status_changed = false; 1344 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED; 1345 uint8_t playback_status = (uint8_t) connection->target_playback_status; 1346 avrcp_target_notification_init(connection, notification_id, &playback_status, 1); 1347 break; 1348 } 1349 1350 if (connection->target_playing_content_changed){ 1351 connection->target_playing_content_changed = false; 1352 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED; 1353 avrcp_target_notification_init(connection, notification_id, NULL, avrcp_now_playing_info_value_len_with_headers(connection)); 1354 break; 1355 } 1356 1357 if (connection->target_battery_status_changed){ 1358 connection->target_battery_status_changed = false; 1359 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 1360 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->target_battery_status, 1); 1361 break; 1362 } 1363 1364 if (connection->target_notify_absolute_volume_changed){ 1365 connection->target_notify_absolute_volume_changed = false; 1366 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; 1367 avrcp_target_notification_init(connection, notification_id, &connection->target_absolute_volume, 1); 1368 break; 1369 } 1370 1371 if (connection->target_addressed_player_changed){ 1372 connection->target_addressed_player_changed = false; 1373 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1374 avrcp_target_notification_addressed_player_changed_init(connection); 1375 break; 1376 } 1377 1378 // nothing to send, exit 1379 return; 1380 1381 default: 1382 return; 1383 } 1384 1385 avrcp_send_response_with_avctp_fragmentation(connection); 1386 avrcp_target_reset_notification(connection, notification_id); 1387 avrcp_request_next_avctp_segment(connection); 1388 } 1389 1390 void avrcp_target_init(void){ 1391 avrcp_target_context.role = AVRCP_TARGET; 1392 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1393 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1394 } 1395 1396 void avrcp_target_deinit(void){ 1397 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1398 } 1399 1400 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1401 btstack_assert(callback != NULL); 1402 avrcp_target_context.avrcp_callback = callback; 1403 } 1404 1405 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1406 btstack_assert(callback != NULL); 1407 avrcp_target_context.set_addressed_player_callback = callback; 1408 } 1409 1410 1411