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 connection->state = AVCTP_W2_SEND_RESPONSE; 463 } 464 465 static void avrcp_target_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_operation_id_t opid){ 466 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 467 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 468 connection->subunit_id = AVRCP_SUBUNIT_ID; 469 470 connection->command_type = command_type; 471 connection->company_id = 0; 472 connection->pdu_id = AVRCP_PDU_ID_UNDEFINED; 473 connection->operation_id = opid; 474 475 connection->data = connection->message_body; 476 connection->data_offset = 0; 477 connection->data_len = 0; 478 connection->avrcp_frame_bytes_sent = 0; 479 } 480 481 482 static uint8_t avrcp_target_vendor_dependent_response_accept(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t status){ 483 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, pdu_id); 484 connection->data_len = 1; 485 connection->data[0] = status; 486 487 connection->target_accept_response = true; 488 connection->state = AVCTP_W2_SEND_RESPONSE; 489 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 490 return ERROR_CODE_SUCCESS; 491 } 492 493 static uint8_t avrcp_target_response_vendor_dependent_reject(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){ 494 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_REJECTED, pdu_id); 495 connection->data_len = 1; 496 connection->data[0] = status; 497 498 connection->state = AVCTP_W2_SEND_RESPONSE; 499 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 500 return ERROR_CODE_SUCCESS; 501 } 502 503 static uint8_t avrcp_target_response_vendor_dependent_not_implemented(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 504 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, pdu_id); 505 connection->data_len = 1; 506 connection->data[0] = event_id; 507 508 connection->state = AVCTP_W2_SEND_RESPONSE; 509 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 510 return ERROR_CODE_SUCCESS; 511 } 512 513 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){ 514 btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH); 515 516 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 517 connection->data_len = 1 + value_len; 518 connection->data[0] = event_id; 519 520 if (value && (value_len > 0)){ 521 (void)memcpy(connection->data + 1, value, value_len); 522 } 523 524 connection->state = AVCTP_W2_SEND_RESPONSE; 525 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 526 return ERROR_CODE_SUCCESS; 527 } 528 529 static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 530 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 531 532 connection->data_len = 5; 533 connection->data[0] = event_id; 534 big_endian_store_16(connection->data, 1, connection->target_addressed_player_id); 535 big_endian_store_16(connection->data, 3, connection->target_uid_counter); 536 537 connection->state = AVCTP_W2_SEND_RESPONSE; 538 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 539 return ERROR_CODE_SUCCESS; 540 } 541 542 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){ 543 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 544 if (!connection){ 545 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 546 } 547 avrcp_target_pass_through_command_data_init(connection, ctype, opid); 548 549 if (operands_length == 1){ 550 connection->data_len = 1; 551 connection->message_body[0] = operand; 552 } 553 554 connection->state = AVCTP_W2_SEND_RESPONSE; 555 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 556 return ERROR_CODE_SUCCESS; 557 } 558 559 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 560 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand); 561 } 562 563 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 564 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 565 } 566 567 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 568 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 569 } 570 571 uint8_t avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){ 572 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 573 if (!connection){ 574 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 575 } 576 connection->target_unit_type = unit_type; 577 connection->company_id = company_id; 578 return ERROR_CODE_SUCCESS; 579 } 580 581 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){ 582 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 583 if (!connection){ 584 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 585 } 586 connection->target_subunit_info_type = subunit_type; 587 connection->target_subunit_info_data = subunit_info_data; 588 connection->target_subunit_info_data_size = subunit_info_data_size; 589 return ERROR_CODE_SUCCESS; 590 } 591 592 // TODO Review 593 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 594 if (connection->state != AVCTP_CONNECTION_OPENED){ 595 return ERROR_CODE_COMMAND_DISALLOWED; 596 } 597 connection->state = AVCTP_W2_SEND_RESPONSE; 598 599 avrcp_target_custom_command_data_init(connection, 600 AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 601 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 602 connection->company_id); 603 604 uint8_t unit = 0; 605 connection->data = connection->message_body; 606 connection->data_len = 5; 607 connection->data[0] = 0x07; 608 connection->data[1] = (connection->target_unit_type << 4) | unit; 609 // company id is 3 bytes long 610 big_endian_store_24(connection->data, 2, connection->company_id); 611 612 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 613 return ERROR_CODE_SUCCESS; 614 } 615 616 // TODO Review 617 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 618 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 619 if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER; 620 621 connection->state = AVCTP_W2_SEND_RESPONSE; 622 623 avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, 624 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 625 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 626 connection->company_id); 627 628 uint8_t page = offset / 4; 629 uint8_t extension_code = 7; 630 connection->data = connection->message_body; 631 connection->data_len = 5; 632 connection->data[0] = (page << 4) | extension_code; 633 634 // mark non-existent entries with 0xff 635 memset(&connection->message_body[1], 0xFF, 4); 636 if ((connection->data != NULL) && (offset < connection->target_subunit_info_data_size)){ 637 uint8_t bytes_to_copy = btstack_min(connection->target_subunit_info_data_size - offset, 4); 638 memcpy(&connection->data[1], &connection->target_subunit_info_data[offset], bytes_to_copy); 639 } 640 641 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 642 return ERROR_CODE_SUCCESS; 643 } 644 645 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){ 646 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 647 connection->state = AVCTP_W2_SEND_RESPONSE; 648 649 uint8_t event_id; 650 uint8_t num_events = 0; 651 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 652 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 653 continue; 654 } 655 num_events++; 656 } 657 658 connection->data[0] = AVRCP_CAPABILITY_ID_EVENT; 659 connection->data[1] = num_events; 660 connection->data_len = 2 + num_events; 661 662 // fill the data later directly to the L2CAP outgoing buffer 663 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 664 return ERROR_CODE_SUCCESS; 665 } 666 667 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){ 668 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 669 connection->state = AVCTP_W2_SEND_RESPONSE; 670 671 connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY; 672 if (connection->target_supported_companies_num == 0){ 673 connection->target_supported_companies_num = 1; 674 connection->target_supported_companies = default_companies; 675 } 676 677 connection->data[1] = connection->target_supported_companies_num; 678 connection->data_len = 2 + connection->data[1] * 3; 679 680 // fill the data later directly to the L2CAP outgoing buffer and 681 // use Bluetooth SIG as default company 682 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 683 return ERROR_CODE_SUCCESS; 684 } 685 686 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 687 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 688 if (!connection){ 689 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 690 } 691 692 if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 693 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 694 } 695 696 connection->notifications_supported_by_target |= (1 << (uint8_t)event_id); 697 return ERROR_CODE_SUCCESS; 698 } 699 700 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){ 701 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 702 if (!connection){ 703 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 704 } 705 706 connection->target_supported_companies_num = num_companies; 707 connection->target_supported_companies = companies; 708 return ERROR_CODE_SUCCESS; 709 } 710 711 // TODO Review (use flags) 712 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){ 713 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 714 if (!connection){ 715 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 716 } 717 if (connection->state != AVCTP_CONNECTION_OPENED){ 718 return ERROR_CODE_COMMAND_DISALLOWED; 719 } 720 721 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS); 722 connection->data_len = 9; 723 big_endian_store_32(connection->data, 0, song_length_ms); 724 big_endian_store_32(connection->data, 4, song_position_ms); 725 connection->data[8] = play_status; 726 727 connection->state = AVCTP_W2_SEND_RESPONSE; 728 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 729 return ERROR_CODE_SUCCESS; 730 } 731 732 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 733 int index = attr_id - 1; 734 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 735 connection->target_now_playing_info[index].value = (uint8_t*)value; 736 connection->target_now_playing_info[index].len = strlen(value); 737 return ERROR_CODE_SUCCESS; 738 } 739 740 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 741 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 742 if (!connection){ 743 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 744 } 745 if (connection->target_playback_status == playback_status){ 746 return ERROR_CODE_SUCCESS; 747 } 748 749 connection->target_playback_status = playback_status; 750 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) { 751 connection->target_playback_status_changed = true; 752 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 753 } 754 return ERROR_CODE_SUCCESS; 755 } 756 757 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 758 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 759 if (!connection){ 760 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 761 } 762 if (!current_track){ 763 return ERROR_CODE_COMMAND_DISALLOWED; 764 } 765 766 (void)memcpy(connection->target_track_id, current_track->track_id, 8); 767 connection->target_song_length_ms = current_track->song_length_ms; 768 connection->target_track_nr = current_track->track_nr; 769 connection->target_total_tracks = total_tracks; 770 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 771 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 772 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 773 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 774 775 connection->target_track_selected = true; 776 777 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 778 connection->target_track_changed = true; 779 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 780 } 781 return ERROR_CODE_SUCCESS; 782 } 783 784 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 785 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 786 if (!connection){ 787 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 788 } 789 790 if (track_id == NULL){ 791 memset(connection->target_track_id, 0xFF, 8); 792 connection->target_track_selected = false; 793 } else { 794 (void)memcpy(connection->target_track_id, track_id, 8); 795 connection->target_track_selected = true; 796 } 797 798 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 799 connection->target_track_changed = true; 800 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 801 } 802 return ERROR_CODE_SUCCESS; 803 } 804 805 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 806 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 807 if (!connection){ 808 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 809 } 810 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 811 connection->target_playing_content_changed = true; 812 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 813 } 814 return ERROR_CODE_SUCCESS; 815 } 816 817 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){ 818 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 819 if (!connection){ 820 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 821 } 822 823 if (connection->target_addressed_player_id == player_id){ 824 return ERROR_CODE_SUCCESS; 825 } 826 827 connection->target_uid_counter = uid_counter; 828 connection->target_addressed_player_id = player_id; 829 830 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 831 connection->target_addressed_player_changed = true; 832 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 833 } 834 return ERROR_CODE_SUCCESS; 835 } 836 837 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 838 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 839 if (!connection){ 840 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 841 } 842 if (connection->target_battery_status == battery_status){ 843 return ERROR_CODE_SUCCESS; 844 } 845 846 connection->target_battery_status = battery_status; 847 848 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 849 connection->target_battery_status_changed = true; 850 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 851 } 852 return ERROR_CODE_SUCCESS; 853 } 854 855 uint8_t avrcp_target_adjust_absolute_volume(uint16_t avrcp_cid, uint8_t absolute_volume){ 856 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 857 if (!connection){ 858 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 859 } 860 861 connection->target_absolute_volume = absolute_volume; 862 return ERROR_CODE_SUCCESS; 863 } 864 865 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t absolute_volume){ 866 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 867 if (!connection){ 868 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 869 } 870 if (connection->target_absolute_volume == absolute_volume){ 871 return ERROR_CODE_SUCCESS; 872 } 873 874 connection->target_absolute_volume = absolute_volume; 875 876 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 877 connection->target_notify_absolute_volume_changed = true; 878 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 879 } 880 return ERROR_CODE_SUCCESS; 881 } 882 883 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){ 884 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return; 885 connection->target_notifications_transaction_label[notification] = transaction_label; 886 } 887 888 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){ 889 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0; 890 return connection->target_notifications_transaction_label[notification]; 891 } 892 893 static bool avcrp_operation_id_is_valid(avrcp_operation_id_t operation_id){ 894 if (operation_id < AVRCP_OPERATION_ID_RESERVED_1) return true; 895 896 if (operation_id < AVRCP_OPERATION_ID_0) return false; 897 if (operation_id < AVRCP_OPERATION_ID_RESERVED_2) return true; 898 899 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 900 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 901 902 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 903 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 904 905 if (operation_id < AVRCP_OPERATION_ID_SKIP) return false; 906 if (operation_id == AVRCP_OPERATION_ID_SKIP) return true; 907 908 if (operation_id < AVRCP_OPERATION_ID_POWER) return false; 909 if (operation_id < AVRCP_OPERATION_ID_RESERVED_4) return true; 910 911 if (operation_id < AVRCP_OPERATION_ID_ANGLE) return false; 912 if (operation_id < AVRCP_OPERATION_ID_RESERVED_5) return true; 913 914 if (operation_id < AVRCP_OPERATION_ID_F1) return false; 915 if (operation_id < AVRCP_OPERATION_ID_RESERVED_6) return true; 916 917 return false; 918 } 919 920 921 #ifdef ENABLE_AVCTP_FRAGMENTATION 922 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 923 // after header (transaction label and packet type) 924 uint16_t pos; 925 uint16_t bytes_to_store; 926 927 switch (packet_type){ 928 case AVCTP_START_PACKET: 929 if (size < 2) return; 930 931 // store header 932 pos = 0; 933 connection->avctp_reassembly_buffer[pos] = packet[pos]; 934 pos++; 935 connection->avctp_reassembly_size = pos; 936 937 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 938 pos++; 939 940 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 941 942 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 943 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 944 connection->avctp_reassembly_size += bytes_to_store; 945 break; 946 947 case AVCTP_CONTINUE_PACKET: 948 case AVCTP_END_PACKET: 949 if (size < 1) return; 950 951 // store remaining data, ignore header 952 pos = 1; 953 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 954 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 955 connection->avctp_reassembly_size += bytes_to_store; 956 break; 957 958 default: 959 return; 960 } 961 } 962 #endif 963 964 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t * packet, uint16_t size){ 965 uint8_t avctp_header = packet[0]; 966 connection->transaction_id = avctp_header >> 4; 967 968 avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t) ((avctp_header & 0x0F) >> 2); 969 switch (avctp_packet_type){ 970 case AVCTP_SINGLE_PACKET: 971 break; 972 973 #ifdef ENABLE_AVCTP_FRAGMENTATION 974 case AVCTP_START_PACKET: 975 case AVCTP_CONTINUE_PACKET: 976 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 977 return; 978 979 case AVCTP_END_PACKET: 980 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 981 982 packet = connection->avctp_reassembly_buffer; 983 size = connection->avctp_reassembly_size; 984 break; 985 #endif 986 default: 987 return; 988 } 989 990 if (size < 6u) return; 991 992 uint16_t pid = big_endian_read_16(packet, 1); 993 994 if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){ 995 log_info("Invalid pid 0x%02x, expected 0x%02x", pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 996 connection->target_reject_transport_header = true; 997 connection->target_invalid_pid = pid; 998 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 999 return; 1000 } 1001 1002 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 1003 // avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 1004 1005 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 1006 1007 int pos = 6; 1008 uint16_t length; 1009 avrcp_pdu_id_t pdu_id; 1010 // connection->data_len = 0; 1011 uint8_t offset; 1012 uint8_t operand; 1013 uint16_t event_mask; 1014 avrcp_operation_id_t operation_id; 1015 1016 switch (opcode){ 1017 case AVRCP_CMD_OPCODE_UNIT_INFO: 1018 avrcp_target_unit_info(connection); 1019 break; 1020 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 1021 if ((size - pos) < 3) return; 1022 // page: packet[pos] >> 4, 1023 offset = 4 * (packet[pos]>>4); 1024 // extension code (fixed 7) = packet[pos] & 0x0F 1025 // 4 bytes paga data, all 0xFF 1026 avrcp_target_subunit_info(connection, offset); 1027 break; 1028 1029 case AVRCP_CMD_OPCODE_PASS_THROUGH: 1030 if (size < 8) return; 1031 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 1032 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f); 1033 operand = 0; 1034 if ((packet[7] >= 1) && (size >= 9)){ 1035 operand = packet[8]; 1036 } 1037 1038 if (avcrp_operation_id_is_valid(operation_id)){ 1039 bool button_pressed = (packet[6] & 0x80) == 0; 1040 1041 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1042 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 1043 operation_id, button_pressed, packet[7], operand); 1044 } else { 1045 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1046 } 1047 break; 1048 1049 1050 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 1051 1052 if (size < 13) return; 1053 1054 // pos = 6 - company id 1055 (void)memcpy(connection->message_body, &packet[pos], 3); 1056 // connection->data_len = 3; 1057 pos += 3; 1058 // pos = 9 1059 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 1060 // 1 - reserved 1061 pos++; 1062 // 2-3 param length, 1063 length = big_endian_read_16(packet, pos); 1064 pos += 2; 1065 // pos = 13 1066 switch (pdu_id){ 1067 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 1068 if ((pos + 2) > size) return; 1069 bool ok = length == 4; 1070 if (avrcp_target_context.set_addressed_player_callback != NULL){ 1071 uint16_t player_id = big_endian_read_16(packet, pos); 1072 ok = avrcp_target_context.set_addressed_player_callback(player_id); 1073 } 1074 if (ok){ 1075 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS); 1076 } else { 1077 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 1078 } 1079 break; 1080 } 1081 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1082 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 1083 switch (capability_id){ 1084 case AVRCP_CAPABILITY_ID_EVENT: 1085 avrcp_target_response_vendor_dependent_supported_events(connection); 1086 break; 1087 case AVRCP_CAPABILITY_ID_COMPANY: 1088 avrcp_target_response_vendor_dependent_supported_companies(connection); 1089 break; 1090 default: 1091 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1092 break; 1093 } 1094 break; 1095 } 1096 case AVRCP_PDU_ID_GET_PLAY_STATUS: 1097 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 1098 break; 1099 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1100 if ((pos + 1) > size) return; 1101 connection->message_body[0] = packet[pos]; 1102 connection->target_abort_continue_response = true; 1103 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1104 break; 1105 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 1106 if ((pos + 1) > size) return; 1107 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 1108 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1109 return; 1110 } 1111 connection->target_continue_response = true; 1112 connection->target_now_playing_info_response = true; 1113 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1114 break; 1115 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 1116 if ((pos + 9) > size) return; 1117 uint8_t play_identifier[8]; 1118 memset(play_identifier, 0, 8); 1119 if (memcmp(&packet[pos], play_identifier, 8) != 0) { 1120 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1121 return; 1122 } 1123 pos += 8; 1124 uint8_t attribute_count = packet[pos++]; 1125 connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE; 1126 if (!attribute_count){ 1127 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1128 connection->target_now_playing_info_attr_bitmap = 0xFE; 1129 } else { 1130 int i; 1131 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1132 connection->target_now_playing_info_attr_bitmap = 0; 1133 if ((pos + attribute_count * 4) > size) return; 1134 for (i=0; i < attribute_count; i++){ 1135 uint32_t attr_id = big_endian_read_32(packet, pos); 1136 connection->target_now_playing_info_attr_bitmap |= (1 << attr_id); 1137 pos += 4; 1138 } 1139 } 1140 log_info("target_now_playing_info_attr_bitmap 0x%02x", connection->target_now_playing_info_attr_bitmap); 1141 connection->target_now_playing_info_response = true; 1142 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1143 break; 1144 } 1145 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 1146 if ((pos + 1) > size) return; 1147 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos]; 1148 1149 avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id); 1150 1151 if (event_id < AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED || 1152 event_id > AVRCP_NOTIFICATION_EVENT_MAX_VALUE){ 1153 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1154 return; 1155 } 1156 1157 switch (event_id){ 1158 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED: 1159 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 1160 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 1161 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END: 1162 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START: 1163 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED: 1164 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED: 1165 case AVRCP_NOTIFICATION_EVENT_MAX_VALUE: 1166 avrcp_target_response_vendor_dependent_not_implemented(connection, pdu_id, event_id); 1167 return; 1168 default: 1169 break; 1170 } 1171 1172 event_mask = (1 << event_id); 1173 connection->notifications_enabled |= event_mask; 1174 1175 switch (event_id){ 1176 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED: 1177 if (connection->target_track_selected){ 1178 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8); 1179 } else { 1180 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8); 1181 } 1182 break; 1183 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED: 1184 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_playback_status, 1); 1185 break; 1186 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED: 1187 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0); 1188 break; 1189 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED: 1190 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_absolute_volume, 1); 1191 break; 1192 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED: 1193 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_battery_status, 1); 1194 break; 1195 case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 1196 avrcp_target_response_addressed_player_changed_interim(connection, pdu_id, event_id); 1197 return; 1198 default: 1199 btstack_assert(false); 1200 return; 1201 } 1202 break; 1203 } 1204 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: { 1205 if ( (length != 1) || ((pos + 1) > size)){ 1206 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1207 break; 1208 } 1209 1210 uint8_t absolute_volume = packet[pos]; 1211 if (absolute_volume < 0x80){ 1212 connection->target_absolute_volume = absolute_volume; 1213 } 1214 avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->target_absolute_volume); 1215 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, connection->target_absolute_volume); 1216 break; 1217 } 1218 default: 1219 log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id); 1220 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1221 break; 1222 } 1223 break; 1224 default: 1225 log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size)); 1226 break; 1227 } 1228 } 1229 1230 static void avrcp_target_notification_init(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){ 1231 btstack_assert((value_len == 0) || (value != NULL)); 1232 1233 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1234 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, notification_id); 1235 1236 connection->data_len = 1 + value_len; 1237 connection->data[0] = notification_id; 1238 if (value_len > 0){ 1239 (void)memcpy(connection->data + 1, value, value_len); 1240 } 1241 } 1242 1243 static void avrcp_target_notification_addressed_player_changed_init(avrcp_connection_t * connection){ 1244 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1245 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED); 1246 1247 connection->data_len = 5; 1248 connection->data[0] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1249 big_endian_store_16(connection->data, 1, connection->target_addressed_player_id); 1250 big_endian_store_16(connection->data, 3, connection->target_uid_counter); 1251 } 1252 1253 1254 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){ 1255 if (notification_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX || notification_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX){ 1256 return; 1257 } 1258 connection->notifications_enabled &= ~(1 << notification_id); 1259 connection->target_notifications_transaction_label[notification_id] = 0; 1260 } 1261 1262 static void avrcp_request_next_avctp_segment(avrcp_connection_t * connection){ 1263 // AVCTP 1264 switch (connection->avctp_packet_type){ 1265 case AVCTP_END_PACKET: 1266 case AVCTP_SINGLE_PACKET: 1267 connection->state = AVCTP_CONNECTION_OPENED; 1268 break; 1269 default: 1270 connection->state = AVCTP_W2_SEND_RESPONSE; 1271 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1272 break; 1273 } 1274 } 1275 1276 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1277 avrcp_connection_t * connection; 1278 avrcp_notification_event_id_t notification_id = AVRCP_NOTIFICATION_EVENT_NONE; 1279 1280 switch (packet_type){ 1281 case L2CAP_DATA_PACKET: 1282 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1283 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1284 return; 1285 1286 case HCI_EVENT_PACKET: 1287 if (hci_event_packet_get_type(packet) != L2CAP_EVENT_CAN_SEND_NOW){ 1288 return; 1289 } 1290 1291 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1292 if (connection == NULL){ 1293 return; 1294 } 1295 1296 // START AVCTP 1297 if (connection->target_reject_transport_header){ 1298 connection->target_reject_transport_header = false; 1299 avctp_send_reject_cmd_wrong_pid(connection); 1300 connection->state = AVCTP_CONNECTION_OPENED; 1301 return; 1302 } 1303 // END AVCTP 1304 1305 if (connection->state == AVCTP_W2_SEND_RESPONSE){ 1306 // data already prepared 1307 break; 1308 } 1309 1310 if (connection->target_accept_response){ 1311 connection->target_accept_response = false; 1312 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE); 1313 break; 1314 } 1315 1316 if (connection->target_abort_continue_response){ 1317 connection->target_abort_continue_response = false; 1318 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 1319 break; 1320 } 1321 1322 if (connection->target_now_playing_info_response){ 1323 connection->target_now_playing_info_response = false; 1324 if (connection->target_continue_response){ 1325 if (connection->data_len == 0){ 1326 avrcp_target_response_vendor_dependent_reject(connection, connection->pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1327 return; 1328 } 1329 connection->target_continue_response = false; 1330 } else { 1331 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES); 1332 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection); 1333 } 1334 break; 1335 } 1336 1337 if (connection->target_track_changed){ 1338 connection->target_track_changed = false; 1339 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED; 1340 avrcp_target_notification_init(connection, notification_id, connection->target_track_id, 8); 1341 break; 1342 } 1343 1344 if (connection->target_playback_status_changed){ 1345 connection->target_playback_status_changed = false; 1346 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED; 1347 uint8_t playback_status = (uint8_t) connection->target_playback_status; 1348 avrcp_target_notification_init(connection, notification_id, &playback_status, 1); 1349 break; 1350 } 1351 1352 if (connection->target_playing_content_changed){ 1353 connection->target_playing_content_changed = false; 1354 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED; 1355 avrcp_target_notification_init(connection, notification_id, NULL, avrcp_now_playing_info_value_len_with_headers(connection)); 1356 break; 1357 } 1358 1359 if (connection->target_battery_status_changed){ 1360 connection->target_battery_status_changed = false; 1361 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 1362 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->target_battery_status, 1); 1363 break; 1364 } 1365 1366 if (connection->target_notify_absolute_volume_changed){ 1367 connection->target_notify_absolute_volume_changed = false; 1368 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; 1369 avrcp_target_notification_init(connection, notification_id, &connection->target_absolute_volume, 1); 1370 break; 1371 } 1372 1373 if (connection->target_addressed_player_changed){ 1374 connection->target_addressed_player_changed = false; 1375 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1376 avrcp_target_notification_addressed_player_changed_init(connection); 1377 break; 1378 } 1379 1380 // nothing to send, exit 1381 return; 1382 1383 default: 1384 return; 1385 } 1386 1387 avrcp_send_response_with_avctp_fragmentation(connection); 1388 avrcp_target_reset_notification(connection, notification_id); 1389 avrcp_request_next_avctp_segment(connection); 1390 } 1391 1392 void avrcp_target_init(void){ 1393 avrcp_target_context.role = AVRCP_TARGET; 1394 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1395 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1396 } 1397 1398 void avrcp_target_deinit(void){ 1399 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1400 } 1401 1402 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1403 btstack_assert(callback != NULL); 1404 avrcp_target_context.avrcp_callback = callback; 1405 } 1406 1407 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1408 btstack_assert(callback != NULL); 1409 avrcp_target_context.set_addressed_player_callback = callback; 1410 } 1411 1412 1413