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 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 591 if (connection->state != AVCTP_CONNECTION_OPENED){ 592 return ERROR_CODE_COMMAND_DISALLOWED; 593 } 594 595 avrcp_target_custom_command_data_init(connection, 596 AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 597 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 598 connection->company_id); 599 600 uint8_t unit = 0; 601 connection->data = connection->message_body; 602 connection->data_len = 5; 603 connection->data[0] = 0x07; 604 connection->data[1] = (connection->target_unit_type << 4) | unit; 605 // company id is 3 bytes long 606 big_endian_store_24(connection->data, 2, connection->company_id); 607 608 connection->state = AVCTP_W2_SEND_RESPONSE; 609 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 610 return ERROR_CODE_SUCCESS; 611 } 612 613 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 614 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 615 if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER; 616 617 avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, 618 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 619 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 620 connection->company_id); 621 622 uint8_t page = offset / 4; 623 uint8_t extension_code = 7; 624 connection->data = connection->message_body; 625 connection->data_len = 5; 626 connection->data[0] = (page << 4) | extension_code; 627 628 // mark non-existent entries with 0xff 629 memset(&connection->message_body[1], 0xFF, 4); 630 if ((connection->data != NULL) && (offset < connection->target_subunit_info_data_size)){ 631 uint8_t bytes_to_copy = btstack_min(connection->target_subunit_info_data_size - offset, 4); 632 memcpy(&connection->data[1], &connection->target_subunit_info_data[offset], bytes_to_copy); 633 } 634 635 connection->state = AVCTP_W2_SEND_RESPONSE; 636 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 637 return ERROR_CODE_SUCCESS; 638 } 639 640 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){ 641 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 642 643 uint8_t event_id; 644 uint8_t num_events = 0; 645 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 646 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 647 continue; 648 } 649 num_events++; 650 } 651 652 connection->data[0] = AVRCP_CAPABILITY_ID_EVENT; 653 connection->data[1] = num_events; 654 connection->data_len = 2 + num_events; 655 656 // fill the data later directly to the L2CAP outgoing buffer 657 connection->state = AVCTP_W2_SEND_RESPONSE; 658 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 659 return ERROR_CODE_SUCCESS; 660 } 661 662 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){ 663 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 664 665 connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY; 666 if (connection->target_supported_companies_num == 0){ 667 connection->target_supported_companies_num = 1; 668 connection->target_supported_companies = default_companies; 669 } 670 671 connection->data[1] = connection->target_supported_companies_num; 672 connection->data_len = 2 + connection->data[1] * 3; 673 674 // fill the data later directly to the L2CAP outgoing buffer and 675 // use Bluetooth SIG as default company 676 connection->state = AVCTP_W2_SEND_RESPONSE; 677 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 678 return ERROR_CODE_SUCCESS; 679 } 680 681 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 682 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 683 if (!connection){ 684 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 685 } 686 687 if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 688 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 689 } 690 691 connection->notifications_supported_by_target |= (1 << (uint8_t)event_id); 692 return ERROR_CODE_SUCCESS; 693 } 694 695 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){ 696 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 697 if (!connection){ 698 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 699 } 700 701 connection->target_supported_companies_num = num_companies; 702 connection->target_supported_companies = companies; 703 return ERROR_CODE_SUCCESS; 704 } 705 706 // TODO Review (use flags) 707 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){ 708 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 709 if (!connection){ 710 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 711 } 712 if (connection->state != AVCTP_CONNECTION_OPENED){ 713 return ERROR_CODE_COMMAND_DISALLOWED; 714 } 715 716 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS); 717 connection->data_len = 9; 718 big_endian_store_32(connection->data, 0, song_length_ms); 719 big_endian_store_32(connection->data, 4, song_position_ms); 720 connection->data[8] = play_status; 721 722 connection->state = AVCTP_W2_SEND_RESPONSE; 723 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 724 return ERROR_CODE_SUCCESS; 725 } 726 727 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 728 int index = attr_id - 1; 729 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 730 connection->target_now_playing_info[index].value = (uint8_t*)value; 731 connection->target_now_playing_info[index].len = strlen(value); 732 return ERROR_CODE_SUCCESS; 733 } 734 735 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 736 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 737 if (!connection){ 738 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 739 } 740 if (connection->target_playback_status == playback_status){ 741 return ERROR_CODE_SUCCESS; 742 } 743 744 connection->target_playback_status = playback_status; 745 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) { 746 connection->target_playback_status_changed = true; 747 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 748 } 749 return ERROR_CODE_SUCCESS; 750 } 751 752 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 753 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 754 if (!connection){ 755 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 756 } 757 if (!current_track){ 758 return ERROR_CODE_COMMAND_DISALLOWED; 759 } 760 761 (void)memcpy(connection->target_track_id, current_track->track_id, 8); 762 connection->target_song_length_ms = current_track->song_length_ms; 763 connection->target_track_nr = current_track->track_nr; 764 connection->target_total_tracks = total_tracks; 765 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 766 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 767 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 768 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 769 770 connection->target_track_selected = true; 771 772 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 773 connection->target_track_changed = true; 774 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 775 } 776 return ERROR_CODE_SUCCESS; 777 } 778 779 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 780 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 781 if (!connection){ 782 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 783 } 784 785 if (track_id == NULL){ 786 memset(connection->target_track_id, 0xFF, 8); 787 connection->target_track_selected = false; 788 } else { 789 (void)memcpy(connection->target_track_id, track_id, 8); 790 connection->target_track_selected = true; 791 } 792 793 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 794 connection->target_track_changed = true; 795 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 796 } 797 return ERROR_CODE_SUCCESS; 798 } 799 800 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 801 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 802 if (!connection){ 803 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 804 } 805 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 806 connection->target_playing_content_changed = true; 807 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 808 } 809 return ERROR_CODE_SUCCESS; 810 } 811 812 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){ 813 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 814 if (!connection){ 815 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 816 } 817 818 if (connection->target_addressed_player_id == player_id){ 819 return ERROR_CODE_SUCCESS; 820 } 821 822 connection->target_uid_counter = uid_counter; 823 connection->target_addressed_player_id = player_id; 824 825 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 826 connection->target_addressed_player_changed = true; 827 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 828 } 829 return ERROR_CODE_SUCCESS; 830 } 831 832 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 833 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 834 if (!connection){ 835 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 836 } 837 if (connection->target_battery_status == battery_status){ 838 return ERROR_CODE_SUCCESS; 839 } 840 841 connection->target_battery_status = battery_status; 842 843 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 844 connection->target_battery_status_changed = true; 845 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 846 } 847 return ERROR_CODE_SUCCESS; 848 } 849 850 uint8_t avrcp_target_adjust_absolute_volume(uint16_t avrcp_cid, uint8_t absolute_volume){ 851 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 852 if (!connection){ 853 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 854 } 855 856 connection->target_absolute_volume = absolute_volume; 857 return ERROR_CODE_SUCCESS; 858 } 859 860 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t absolute_volume){ 861 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 862 if (!connection){ 863 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 864 } 865 if (connection->target_absolute_volume == absolute_volume){ 866 return ERROR_CODE_SUCCESS; 867 } 868 869 connection->target_absolute_volume = absolute_volume; 870 871 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 872 connection->target_notify_absolute_volume_changed = true; 873 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 874 } 875 return ERROR_CODE_SUCCESS; 876 } 877 878 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){ 879 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return; 880 connection->target_notifications_transaction_label[notification] = transaction_label; 881 } 882 883 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){ 884 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0; 885 return connection->target_notifications_transaction_label[notification]; 886 } 887 888 static bool avcrp_operation_id_is_valid(avrcp_operation_id_t operation_id){ 889 if (operation_id < AVRCP_OPERATION_ID_RESERVED_1) return true; 890 891 if (operation_id < AVRCP_OPERATION_ID_0) return false; 892 if (operation_id < AVRCP_OPERATION_ID_RESERVED_2) return true; 893 894 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 895 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) 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_SKIP) return false; 901 if (operation_id == AVRCP_OPERATION_ID_SKIP) return true; 902 903 if (operation_id < AVRCP_OPERATION_ID_POWER) return false; 904 if (operation_id < AVRCP_OPERATION_ID_RESERVED_4) return true; 905 906 if (operation_id < AVRCP_OPERATION_ID_ANGLE) return false; 907 if (operation_id < AVRCP_OPERATION_ID_RESERVED_5) return true; 908 909 if (operation_id < AVRCP_OPERATION_ID_F1) return false; 910 if (operation_id < AVRCP_OPERATION_ID_RESERVED_6) return true; 911 912 return false; 913 } 914 915 916 #ifdef ENABLE_AVCTP_FRAGMENTATION 917 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 918 // after header (transaction label and packet type) 919 uint16_t pos; 920 uint16_t bytes_to_store; 921 922 switch (packet_type){ 923 case AVCTP_START_PACKET: 924 if (size < 2) return; 925 926 // store header 927 pos = 0; 928 connection->avctp_reassembly_buffer[pos] = packet[pos]; 929 pos++; 930 connection->avctp_reassembly_size = pos; 931 932 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 933 pos++; 934 935 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 936 937 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 938 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 939 connection->avctp_reassembly_size += bytes_to_store; 940 break; 941 942 case AVCTP_CONTINUE_PACKET: 943 case AVCTP_END_PACKET: 944 if (size < 1) return; 945 946 // store remaining data, ignore header 947 pos = 1; 948 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 949 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 950 connection->avctp_reassembly_size += bytes_to_store; 951 break; 952 953 default: 954 return; 955 } 956 } 957 #endif 958 959 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t * packet, uint16_t size){ 960 uint8_t avctp_header = packet[0]; 961 connection->transaction_id = avctp_header >> 4; 962 963 avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t) ((avctp_header & 0x0F) >> 2); 964 switch (avctp_packet_type){ 965 case AVCTP_SINGLE_PACKET: 966 break; 967 968 #ifdef ENABLE_AVCTP_FRAGMENTATION 969 case AVCTP_START_PACKET: 970 case AVCTP_CONTINUE_PACKET: 971 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 972 return; 973 974 case AVCTP_END_PACKET: 975 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 976 977 packet = connection->avctp_reassembly_buffer; 978 size = connection->avctp_reassembly_size; 979 break; 980 #endif 981 default: 982 return; 983 } 984 985 if (size < 6u) return; 986 987 uint16_t pid = big_endian_read_16(packet, 1); 988 989 if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){ 990 log_info("Invalid pid 0x%02x, expected 0x%02x", pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 991 connection->target_reject_transport_header = true; 992 connection->target_invalid_pid = pid; 993 connection->state = AVCTP_W2_SEND_RESPONSE; 994 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 995 return; 996 } 997 998 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 999 // avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 1000 1001 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 1002 1003 int pos = 6; 1004 uint16_t length; 1005 avrcp_pdu_id_t pdu_id; 1006 // connection->data_len = 0; 1007 uint8_t offset; 1008 uint8_t operand; 1009 uint16_t event_mask; 1010 avrcp_operation_id_t operation_id; 1011 1012 switch (opcode){ 1013 case AVRCP_CMD_OPCODE_UNIT_INFO: 1014 avrcp_target_unit_info(connection); 1015 break; 1016 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 1017 if ((size - pos) < 3) return; 1018 // page: packet[pos] >> 4, 1019 offset = 4 * (packet[pos]>>4); 1020 // extension code (fixed 7) = packet[pos] & 0x0F 1021 // 4 bytes paga data, all 0xFF 1022 avrcp_target_subunit_info(connection, offset); 1023 break; 1024 1025 case AVRCP_CMD_OPCODE_PASS_THROUGH: 1026 if (size < 8) return; 1027 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 1028 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f); 1029 operand = 0; 1030 if ((packet[7] >= 1) && (size >= 9)){ 1031 operand = packet[8]; 1032 } 1033 1034 if (avcrp_operation_id_is_valid(operation_id)){ 1035 bool button_pressed = (packet[6] & 0x80) == 0; 1036 1037 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1038 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 1039 operation_id, button_pressed, packet[7], operand); 1040 } else { 1041 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1042 } 1043 break; 1044 1045 1046 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 1047 1048 if (size < 13) return; 1049 1050 // pos = 6 - company id 1051 (void)memcpy(connection->message_body, &packet[pos], 3); 1052 // connection->data_len = 3; 1053 pos += 3; 1054 // pos = 9 1055 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 1056 // 1 - reserved 1057 pos++; 1058 // 2-3 param length, 1059 length = big_endian_read_16(packet, pos); 1060 pos += 2; 1061 // pos = 13 1062 switch (pdu_id){ 1063 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 1064 if ((pos + 2) > size) return; 1065 bool ok = length == 4; 1066 if (avrcp_target_context.set_addressed_player_callback != NULL){ 1067 uint16_t player_id = big_endian_read_16(packet, pos); 1068 ok = avrcp_target_context.set_addressed_player_callback(player_id); 1069 } 1070 if (ok){ 1071 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS); 1072 } else { 1073 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 1074 } 1075 break; 1076 } 1077 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1078 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 1079 switch (capability_id){ 1080 case AVRCP_CAPABILITY_ID_EVENT: 1081 avrcp_target_response_vendor_dependent_supported_events(connection); 1082 break; 1083 case AVRCP_CAPABILITY_ID_COMPANY: 1084 avrcp_target_response_vendor_dependent_supported_companies(connection); 1085 break; 1086 default: 1087 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1088 break; 1089 } 1090 break; 1091 } 1092 case AVRCP_PDU_ID_GET_PLAY_STATUS: 1093 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 1094 break; 1095 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1096 if ((pos + 1) > size) return; 1097 connection->target_abort_continue_response = true; 1098 connection->state = AVCTP_W2_SEND_RESPONSE; 1099 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1100 break; 1101 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 1102 if ((pos + 1) > size) return; 1103 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 1104 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1105 return; 1106 } 1107 connection->target_continue_response = true; 1108 connection->target_now_playing_info_response = true; 1109 connection->state = AVCTP_W2_SEND_RESPONSE; 1110 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1111 break; 1112 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 1113 if ((pos + 9) > size) return; 1114 uint8_t play_identifier[8]; 1115 memset(play_identifier, 0, 8); 1116 if (memcmp(&packet[pos], play_identifier, 8) != 0) { 1117 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1118 return; 1119 } 1120 pos += 8; 1121 uint8_t attribute_count = packet[pos++]; 1122 connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE; 1123 if (!attribute_count){ 1124 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1125 connection->target_now_playing_info_attr_bitmap = 0xFE; 1126 } else { 1127 int i; 1128 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1129 connection->target_now_playing_info_attr_bitmap = 0; 1130 if ((pos + attribute_count * 4) > size) return; 1131 for (i=0; i < attribute_count; i++){ 1132 uint32_t attr_id = big_endian_read_32(packet, pos); 1133 connection->target_now_playing_info_attr_bitmap |= (1 << attr_id); 1134 pos += 4; 1135 } 1136 } 1137 log_info("target_now_playing_info_attr_bitmap 0x%02x", connection->target_now_playing_info_attr_bitmap); 1138 connection->target_now_playing_info_response = true; 1139 connection->state = AVCTP_W2_SEND_RESPONSE; 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 if (connection->state == AVCTP_W2_SEND_RESPONSE){ 1295 // start AVCTP 1296 if (connection->target_reject_transport_header){ 1297 connection->target_reject_transport_header = false; 1298 avctp_send_reject_cmd_wrong_pid(connection); 1299 connection->state = AVCTP_CONNECTION_OPENED; 1300 return; 1301 } 1302 // end AVCTP 1303 1304 // start AVRCP 1305 if (connection->target_abort_continue_response){ 1306 connection->target_abort_continue_response = false; 1307 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 1308 break; 1309 } 1310 1311 if (connection->target_now_playing_info_response){ 1312 connection->target_now_playing_info_response = false; 1313 if (connection->target_continue_response){ 1314 connection->target_continue_response = false; 1315 if (connection->data_len == 0){ 1316 avrcp_target_response_vendor_dependent_reject(connection, connection->pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1317 return; 1318 } 1319 } else { 1320 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES); 1321 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection); 1322 } 1323 break; 1324 } 1325 1326 // data already prepared 1327 break; 1328 } 1329 1330 // Notifications 1331 1332 if (connection->target_track_changed){ 1333 connection->target_track_changed = false; 1334 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED; 1335 avrcp_target_notification_init(connection, notification_id, connection->target_track_id, 8); 1336 break; 1337 } 1338 1339 if (connection->target_playback_status_changed){ 1340 connection->target_playback_status_changed = false; 1341 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED; 1342 uint8_t playback_status = (uint8_t) connection->target_playback_status; 1343 avrcp_target_notification_init(connection, notification_id, &playback_status, 1); 1344 break; 1345 } 1346 1347 if (connection->target_playing_content_changed){ 1348 connection->target_playing_content_changed = false; 1349 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED; 1350 avrcp_target_notification_init(connection, notification_id, NULL, avrcp_now_playing_info_value_len_with_headers(connection)); 1351 break; 1352 } 1353 1354 if (connection->target_battery_status_changed){ 1355 connection->target_battery_status_changed = false; 1356 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 1357 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->target_battery_status, 1); 1358 break; 1359 } 1360 1361 if (connection->target_notify_absolute_volume_changed){ 1362 connection->target_notify_absolute_volume_changed = false; 1363 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; 1364 avrcp_target_notification_init(connection, notification_id, &connection->target_absolute_volume, 1); 1365 break; 1366 } 1367 1368 if (connection->target_addressed_player_changed){ 1369 connection->target_addressed_player_changed = false; 1370 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1371 avrcp_target_notification_addressed_player_changed_init(connection); 1372 break; 1373 } 1374 1375 // nothing to send, exit 1376 return; 1377 1378 default: 1379 return; 1380 } 1381 1382 avrcp_send_response_with_avctp_fragmentation(connection); 1383 avrcp_target_reset_notification(connection, notification_id); 1384 avrcp_request_next_avctp_segment(connection); 1385 } 1386 1387 void avrcp_target_init(void){ 1388 avrcp_target_context.role = AVRCP_TARGET; 1389 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1390 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1391 } 1392 1393 void avrcp_target_deinit(void){ 1394 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1395 } 1396 1397 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1398 btstack_assert(callback != NULL); 1399 avrcp_target_context.avrcp_callback = callback; 1400 } 1401 1402 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1403 btstack_assert(callback != NULL); 1404 avrcp_target_context.set_addressed_player_callback = callback; 1405 } 1406 1407 1408