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(false, 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 = snprintf(buffer, sizeof(buffer), "%" PRIu32, connection->target_track_nr); 140 break; 141 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 142 str_len = snprintf(buffer, sizeof(buffer), "%" PRIu32, connection->target_total_tracks); 143 break; 144 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 145 str_len = snprintf(buffer, sizeof(buffer), "%" 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 // "4294967296" = 10 chars + \0 185 connection->attribute_value_len = snprintf((char *)connection->attribute_value, 11, "%" PRIu32, value); 186 connection->attribute_value_offset = 0; 187 } 188 *num_bytes_to_copy = connection->attribute_value_len - connection->attribute_value_offset; 189 return connection->attribute_value + connection->attribute_value_offset; 190 } 191 192 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){ 193 switch (attr_id){ 194 case AVRCP_MEDIA_ATTR_TRACK: 195 return avrcp_get_attribute_value_from_u32(connection, connection->target_track_nr, num_bytes_to_copy); 196 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 197 return avrcp_get_attribute_value_from_u32(connection, connection->target_total_tracks, num_bytes_to_copy); 198 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 199 return avrcp_get_attribute_value_from_u32(connection, connection->target_song_length_ms, num_bytes_to_copy); 200 default: 201 break; 202 } 203 int attr_index = attr_id - 1; 204 if (connection->attribute_value_len == 0){ 205 connection->attribute_value_len = avrcp_now_playing_info_attr_id_value_len(connection, attr_id); 206 connection->attribute_value_offset = 0; 207 } 208 *num_bytes_to_copy = connection->target_now_playing_info[attr_index].len - connection->attribute_value_offset; 209 return (uint8_t *) (connection->target_now_playing_info[attr_index].value + connection->attribute_value_offset); 210 } 211 212 // TODO Review 213 static uint16_t avrcp_store_avctp_now_playing_info_fragment(avrcp_connection_t * connection, uint16_t packet_size, uint8_t * packet){ 214 uint16_t num_free_bytes = packet_size; 215 216 uint16_t bytes_stored = 0; 217 218 while ((num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){ 219 if ((connection->target_now_playing_info_attr_bitmap & (1 << (uint8_t)connection->next_attr_id)) == 0) { 220 connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1); 221 continue; 222 } 223 224 // prepare attribute value 225 uint16_t num_bytes_to_copy; 226 uint8_t * attr_value_with_offset = avrcp_get_next_value_fragment_for_attribute_id(connection, 227 connection->next_attr_id, 228 &num_bytes_to_copy); 229 230 // store header 231 if (connection->attribute_value_offset == 0){ 232 // pack the whole attribute value header 233 if (connection->parser_attribute_header_pos == 0) { 234 avrcp_target_pack_single_element_header(connection->parser_attribute_header, connection->next_attr_id, 235 connection->attribute_value_len); 236 } 237 } 238 239 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN){ 240 uint16_t num_header_bytes_to_store = btstack_min(num_free_bytes, AVRCP_ATTRIBUTE_HEADER_LEN - connection->parser_attribute_header_pos); 241 memcpy(packet + bytes_stored, connection->parser_attribute_header + connection->parser_attribute_header_pos, num_header_bytes_to_store); 242 connection->parser_attribute_header_pos += num_header_bytes_to_store; 243 bytes_stored += num_header_bytes_to_store; 244 num_free_bytes -= num_header_bytes_to_store; 245 connection->data_offset += num_header_bytes_to_store; 246 247 if (num_free_bytes == 0){ 248 continue; 249 } 250 } 251 252 // store value 253 uint16_t num_attr_value_bytes_to_store = btstack_min(num_free_bytes, connection->attribute_value_len - connection->attribute_value_offset); 254 memcpy(packet + bytes_stored, attr_value_with_offset, num_attr_value_bytes_to_store); 255 bytes_stored += num_attr_value_bytes_to_store; 256 num_free_bytes -= num_attr_value_bytes_to_store; 257 connection->attribute_value_offset += num_attr_value_bytes_to_store; 258 connection->data_offset += num_attr_value_bytes_to_store; 259 260 if (connection->attribute_value_offset == connection->attribute_value_len){ 261 // C++ compatible version of connection->next_attr_id++ 262 connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1); 263 connection->attribute_value_offset = 0; 264 connection->attribute_value_len = 0; 265 connection->parser_attribute_header_pos = 0; 266 } 267 } 268 return bytes_stored; 269 } 270 271 static void avrcp_send_response_with_avctp_fragmentation(avrcp_connection_t * connection){ 272 l2cap_reserve_packet_buffer(); 273 uint8_t * packet = l2cap_get_outgoing_buffer(); 274 275 // transport header 276 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 277 278 uint16_t max_payload_size; 279 connection->avctp_packet_type = avctp_get_packet_type(connection, &max_payload_size); 280 connection->avrcp_packet_type = avrcp_get_packet_type(connection); 281 282 // AVCTP header 283 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 284 uint16_t pos = 0; 285 packet[pos++] = (connection->transaction_id << 4) | (connection->avctp_packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 286 287 uint16_t param_len = connection->data_len; 288 289 if (connection->avctp_packet_type == AVCTP_START_PACKET){ 290 uint16_t max_frame_size = btstack_min(connection->l2cap_mtu, AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE); 291 // first packet: max_payload_size 292 // rest packets 293 uint16_t num_payload_bytes = param_len - max_payload_size; 294 uint16_t frame_size_for_continue_packet = max_frame_size - avctp_get_num_bytes_for_header(AVCTP_CONTINUE_PACKET); 295 uint16_t num_avctp_packets = (num_payload_bytes + frame_size_for_continue_packet - 1)/frame_size_for_continue_packet + 1; 296 btstack_assert(num_avctp_packets <= 255); 297 packet[pos++] = (uint8_t) num_avctp_packets; 298 } 299 300 uint16_t bytes_stored = 0; 301 uint8_t i; 302 303 switch (connection->avctp_packet_type) { 304 case AVCTP_SINGLE_PACKET: 305 case AVCTP_START_PACKET: 306 // Profile IDentifier (PID) 307 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 308 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 309 310 // AVRCP message 311 // command_type 312 packet[pos++] = connection->command_type; 313 // subunit_type | subunit ID 314 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 315 // opcode 316 packet[pos++] = (uint8_t) connection->command_opcode; 317 318 switch (connection->command_opcode) { 319 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 320 big_endian_store_24(packet, pos, connection->company_id); 321 pos += 3; 322 packet[pos++] = connection->pdu_id; 323 // AVRCP packet type 324 325 packet[pos++] = (uint8_t)connection->avrcp_packet_type; 326 // parameter length 327 big_endian_store_16(packet, pos, param_len); 328 pos += 2; 329 330 switch (connection->pdu_id) { 331 // message is small enough to fit the single packet, no need for extra check 332 case AVRCP_PDU_ID_GET_CAPABILITIES: 333 // capability ID 334 packet[pos++] = connection->data[0]; 335 // num_capabilities 336 packet[pos++] = connection->data[1]; 337 338 switch ((avrcp_capability_id_t) connection->data[0]) { 339 case AVRCP_CAPABILITY_ID_EVENT: 340 for (i = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; 341 i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++) { 342 if ((connection->notifications_supported_by_target & (1 << i)) == 0) { 343 continue; 344 } 345 packet[pos++] = i; 346 } 347 break; 348 case AVRCP_CAPABILITY_ID_COMPANY: 349 // use Bluetooth SIG as default company 350 for (i = 0; i < connection->data[1]; i++) { 351 little_endian_store_24(packet, pos, 352 connection->target_supported_companies[i]); 353 pos += 3; 354 } 355 break; 356 default: 357 // error response 358 break; 359 } 360 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 361 return; 362 363 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 364 packet[pos++] = count_set_bits_uint32(connection->target_now_playing_info_attr_bitmap); 365 max_payload_size--; 366 367 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 368 369 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 370 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 371 return; 372 373 default: 374 // error response and other OPCODEs 375 break; 376 } 377 break; 378 379 case AVRCP_CMD_OPCODE_PASS_THROUGH: 380 packet[pos++] = connection->operation_id; 381 // parameter length 382 packet[pos++] = (uint8_t) connection->data_len; 383 pos += 2; 384 break; 385 case AVRCP_CMD_OPCODE_UNIT_INFO: 386 break; 387 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 388 break; 389 default: 390 btstack_assert(false); 391 return; 392 } 393 break; 394 case AVCTP_CONTINUE_PACKET: 395 case AVCTP_END_PACKET: 396 switch (connection->pdu_id) { 397 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 398 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 399 400 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 401 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 402 return; 403 404 default: 405 break; 406 } 407 break; 408 default: 409 btstack_assert(false); 410 return; 411 } 412 413 // compare number of bytes to store with the remaining buffer size 414 uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_payload_size - pos); 415 416 (void)memcpy(packet + pos, &connection->data[connection->data_offset], bytes_to_copy); 417 pos += bytes_to_copy; 418 connection->data_offset += bytes_to_copy; 419 connection->avrcp_frame_bytes_sent += pos; 420 421 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 422 } 423 424 static void avctp_send_reject_cmd_wrong_pid(avrcp_connection_t * connection){ 425 l2cap_reserve_packet_buffer(); 426 uint8_t * packet = l2cap_get_outgoing_buffer(); 427 428 // AVCTP header 429 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 430 packet[0] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 1; 431 big_endian_store_16(packet, 1, connection->message_body[0]); 432 l2cap_send_prepared(connection->l2cap_signaling_cid, 3); 433 } 434 435 static void avrcp_target_custom_command_data_init(avrcp_connection_t * connection, 436 avrcp_command_opcode_t opcode, avrcp_command_type_t command_type, 437 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 438 avrcp_pdu_id_t pdu_id, uint32_t company_id){ 439 440 connection->command_opcode = opcode; 441 connection->command_type = command_type; 442 connection->subunit_type = subunit_type; 443 connection->subunit_id = subunit_id; 444 connection->company_id = company_id << 16; 445 connection->pdu_id = pdu_id; 446 connection->data = NULL; 447 connection->data_offset = 0; 448 connection->data_len = 0; 449 connection->avrcp_frame_bytes_sent = 0; 450 } 451 452 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){ 453 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 454 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 455 connection->subunit_id = AVRCP_SUBUNIT_ID; 456 connection->company_id = BT_SIG_COMPANY_ID; 457 458 connection->command_type = command_type; 459 connection->pdu_id = pdu_id; 460 connection->data = connection->message_body; 461 connection->data_offset = 0; 462 connection->data_len = 0; 463 connection->avrcp_frame_bytes_sent = 0; 464 } 465 466 static void avrcp_target_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_operation_id_t opid){ 467 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 468 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 469 connection->subunit_id = AVRCP_SUBUNIT_ID; 470 471 connection->command_type = command_type; 472 connection->company_id = 0; 473 connection->pdu_id = AVRCP_PDU_ID_UNDEFINED; 474 connection->operation_id = opid; 475 476 connection->data = connection->message_body; 477 connection->data_offset = 0; 478 connection->data_len = 0; 479 connection->avrcp_frame_bytes_sent = 0; 480 } 481 482 483 static uint8_t avrcp_target_vendor_dependent_response_accept(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t status){ 484 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, pdu_id); 485 connection->data_len = 1; 486 connection->data[0] = status; 487 488 connection->target_accept_response = true; 489 connection->state = AVCTP_W2_SEND_RESPONSE; 490 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 491 return ERROR_CODE_SUCCESS; 492 } 493 494 static uint8_t avrcp_target_response_vendor_dependent_reject(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){ 495 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_REJECTED, pdu_id); 496 connection->data_len = 1; 497 connection->data[0] = status; 498 499 connection->state = AVCTP_W2_SEND_RESPONSE; 500 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 501 return ERROR_CODE_SUCCESS; 502 } 503 504 static uint8_t avrcp_target_response_vendor_dependent_not_implemented(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 505 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, pdu_id); 506 connection->data_len = 1; 507 connection->data[0] = event_id; 508 509 connection->state = AVCTP_W2_SEND_RESPONSE; 510 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 511 return ERROR_CODE_SUCCESS; 512 } 513 514 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){ 515 btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH); 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 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 593 if (connection->state != AVCTP_CONNECTION_OPENED){ 594 return ERROR_CODE_COMMAND_DISALLOWED; 595 } 596 597 avrcp_target_custom_command_data_init(connection, 598 AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 599 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 600 connection->company_id); 601 602 uint8_t unit = 0; 603 connection->data = connection->message_body; 604 connection->data_len = 5; 605 connection->data[0] = 0x07; 606 connection->data[1] = (connection->target_unit_type << 4) | unit; 607 // company id is 3 bytes long 608 big_endian_store_24(connection->data, 2, connection->company_id); 609 610 connection->state = AVCTP_W2_SEND_RESPONSE; 611 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 612 return ERROR_CODE_SUCCESS; 613 } 614 615 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 616 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 617 if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER; 618 619 avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, 620 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 621 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 622 connection->company_id); 623 624 uint8_t page = offset / 4; 625 uint8_t extension_code = 7; 626 connection->data = connection->message_body; 627 connection->data_len = 5; 628 connection->data[0] = (page << 4) | extension_code; 629 630 // mark non-existent entries with 0xff 631 memset(&connection->message_body[1], 0xFF, 4); 632 if ((connection->data != NULL) && (offset < connection->target_subunit_info_data_size)){ 633 uint8_t bytes_to_copy = btstack_min(connection->target_subunit_info_data_size - offset, 4); 634 memcpy(&connection->data[1], &connection->target_subunit_info_data[offset], bytes_to_copy); 635 } 636 637 connection->state = AVCTP_W2_SEND_RESPONSE; 638 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 639 return ERROR_CODE_SUCCESS; 640 } 641 642 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){ 643 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 644 645 uint8_t event_id; 646 uint8_t num_events = 0; 647 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 648 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 649 continue; 650 } 651 num_events++; 652 } 653 654 connection->data[0] = AVRCP_CAPABILITY_ID_EVENT; 655 connection->data[1] = num_events; 656 connection->data_len = 2 + num_events; 657 658 // fill the data later directly to the L2CAP outgoing buffer 659 connection->state = AVCTP_W2_SEND_RESPONSE; 660 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 661 return ERROR_CODE_SUCCESS; 662 } 663 664 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){ 665 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 666 667 connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY; 668 if (connection->target_supported_companies_num == 0){ 669 connection->target_supported_companies_num = 1; 670 connection->target_supported_companies = default_companies; 671 } 672 673 connection->data[1] = connection->target_supported_companies_num; 674 connection->data_len = 2 + connection->data[1] * 3; 675 676 // fill the data later directly to the L2CAP outgoing buffer and 677 // use Bluetooth SIG as default company 678 connection->state = AVCTP_W2_SEND_RESPONSE; 679 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 680 return ERROR_CODE_SUCCESS; 681 } 682 683 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 684 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 685 if (!connection){ 686 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 687 } 688 689 if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 690 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 691 } 692 693 connection->notifications_supported_by_target |= (1 << (uint8_t)event_id); 694 return ERROR_CODE_SUCCESS; 695 } 696 697 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){ 698 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 699 if (!connection){ 700 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 701 } 702 703 connection->target_supported_companies_num = num_companies; 704 connection->target_supported_companies = companies; 705 return ERROR_CODE_SUCCESS; 706 } 707 708 // TODO Review (use flags) 709 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){ 710 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 711 if (!connection){ 712 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 713 } 714 if (connection->state != AVCTP_CONNECTION_OPENED){ 715 return ERROR_CODE_COMMAND_DISALLOWED; 716 } 717 718 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS); 719 connection->data_len = 9; 720 big_endian_store_32(connection->data, 0, song_length_ms); 721 big_endian_store_32(connection->data, 4, song_position_ms); 722 connection->data[8] = play_status; 723 724 connection->state = AVCTP_W2_SEND_RESPONSE; 725 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 726 return ERROR_CODE_SUCCESS; 727 } 728 729 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 730 int index = attr_id - 1; 731 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 732 uint16_t value_len = (uint16_t)strlen(value); 733 btstack_assert(value_len <= 255); 734 connection->target_now_playing_info[index].value = (uint8_t*)value; 735 connection->target_now_playing_info[index].len = value_len; 736 return ERROR_CODE_SUCCESS; 737 } 738 739 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 740 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 741 if (!connection){ 742 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 743 } 744 if (connection->target_playback_status == playback_status){ 745 return ERROR_CODE_SUCCESS; 746 } 747 748 connection->target_playback_status = playback_status; 749 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) { 750 connection->target_playback_status_changed = true; 751 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 752 } 753 return ERROR_CODE_SUCCESS; 754 } 755 756 static void avrcp_target_register_track_changed(avrcp_connection_t * connection, const uint8_t * track_id){ 757 if (track_id == NULL){ 758 memset(connection->target_track_id, 0xFF, 8); 759 connection->target_track_selected = false; 760 } else { 761 (void)memcpy(connection->target_track_id, track_id, 8); 762 connection->target_track_selected = true; 763 } 764 765 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 766 connection->target_track_changed = true; 767 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 768 } 769 } 770 771 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 772 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 773 if (!connection){ 774 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 775 } 776 avrcp_target_register_track_changed(connection, track_id); 777 return ERROR_CODE_SUCCESS; 778 } 779 780 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 781 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 782 if (!connection){ 783 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 784 } 785 if (!current_track){ 786 return ERROR_CODE_COMMAND_DISALLOWED; 787 } 788 789 (void)memcpy(connection->target_track_id, current_track->track_id, 8); 790 connection->target_song_length_ms = current_track->song_length_ms; 791 connection->target_track_nr = current_track->track_nr; 792 connection->target_total_tracks = total_tracks; 793 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 794 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 795 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 796 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 797 798 avrcp_target_register_track_changed(connection, current_track->track_id); 799 return ERROR_CODE_SUCCESS; 800 } 801 802 803 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 804 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 805 if (!connection){ 806 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 807 } 808 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 809 connection->target_playing_content_changed = true; 810 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 811 } 812 return ERROR_CODE_SUCCESS; 813 } 814 815 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){ 816 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 817 if (!connection){ 818 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 819 } 820 821 if (connection->target_addressed_player_id == player_id){ 822 return ERROR_CODE_SUCCESS; 823 } 824 825 connection->target_uid_counter = uid_counter; 826 connection->target_addressed_player_id = player_id; 827 828 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 829 connection->target_addressed_player_changed = true; 830 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 831 } 832 return ERROR_CODE_SUCCESS; 833 } 834 835 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 836 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 837 if (!connection){ 838 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 839 } 840 if (connection->target_battery_status == battery_status){ 841 return ERROR_CODE_SUCCESS; 842 } 843 844 connection->target_battery_status = battery_status; 845 846 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 847 connection->target_battery_status_changed = true; 848 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 849 } 850 return ERROR_CODE_SUCCESS; 851 } 852 853 uint8_t avrcp_target_adjust_absolute_volume(uint16_t avrcp_cid, uint8_t absolute_volume){ 854 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 855 if (!connection){ 856 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 857 } 858 859 connection->target_absolute_volume = absolute_volume; 860 return ERROR_CODE_SUCCESS; 861 } 862 863 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t absolute_volume){ 864 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 865 if (!connection){ 866 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 867 } 868 if (connection->target_absolute_volume == absolute_volume){ 869 return ERROR_CODE_SUCCESS; 870 } 871 872 connection->target_absolute_volume = absolute_volume; 873 874 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 875 connection->target_notify_absolute_volume_changed = true; 876 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 877 } 878 return ERROR_CODE_SUCCESS; 879 } 880 881 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){ 882 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return; 883 connection->target_notifications_transaction_label[notification] = transaction_label; 884 } 885 886 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){ 887 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0; 888 return connection->target_notifications_transaction_label[notification]; 889 } 890 891 static bool avcrp_operation_id_is_valid(avrcp_operation_id_t operation_id){ 892 if (operation_id < AVRCP_OPERATION_ID_RESERVED_1) return true; 893 894 if (operation_id < AVRCP_OPERATION_ID_0) return false; 895 if (operation_id < AVRCP_OPERATION_ID_RESERVED_2) return true; 896 897 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 898 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 899 900 if (operation_id < AVRCP_OPERATION_ID_CHANNEL_UP) return false; 901 if (operation_id < AVRCP_OPERATION_ID_RESERVED_3) return true; 902 903 if (operation_id < AVRCP_OPERATION_ID_SKIP) return false; 904 if (operation_id == AVRCP_OPERATION_ID_SKIP) return true; 905 906 if (operation_id < AVRCP_OPERATION_ID_POWER) return false; 907 if (operation_id < AVRCP_OPERATION_ID_RESERVED_4) return true; 908 909 if (operation_id < AVRCP_OPERATION_ID_ANGLE) return false; 910 if (operation_id < AVRCP_OPERATION_ID_RESERVED_5) return true; 911 912 if (operation_id < AVRCP_OPERATION_ID_F1) return false; 913 if (operation_id < AVRCP_OPERATION_ID_RESERVED_6) return true; 914 915 return false; 916 } 917 918 919 #ifdef ENABLE_AVCTP_FRAGMENTATION 920 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 921 // after header (transaction label and packet type) 922 uint16_t pos; 923 uint16_t bytes_to_store; 924 925 switch (packet_type){ 926 case AVCTP_START_PACKET: 927 if (size < 2) return; 928 929 // store header 930 pos = 0; 931 connection->avctp_reassembly_buffer[pos] = packet[pos]; 932 pos++; 933 connection->avctp_reassembly_size = pos; 934 935 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 936 pos++; 937 938 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 939 940 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 941 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 942 connection->avctp_reassembly_size += bytes_to_store; 943 break; 944 945 case AVCTP_CONTINUE_PACKET: 946 case AVCTP_END_PACKET: 947 if (size < 1) return; 948 949 // store remaining data, ignore header 950 pos = 1; 951 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 952 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 953 connection->avctp_reassembly_size += bytes_to_store; 954 break; 955 956 default: 957 return; 958 } 959 } 960 #endif 961 962 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t * packet, uint16_t size){ 963 uint8_t avctp_header = packet[0]; 964 connection->transaction_id = avctp_header >> 4; 965 966 avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t) ((avctp_header & 0x0F) >> 2); 967 switch (avctp_packet_type){ 968 case AVCTP_SINGLE_PACKET: 969 break; 970 971 #ifdef ENABLE_AVCTP_FRAGMENTATION 972 case AVCTP_START_PACKET: 973 case AVCTP_CONTINUE_PACKET: 974 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 975 return; 976 977 case AVCTP_END_PACKET: 978 avctp_reassemble_message(connection, avctp_packet_type, packet, size); 979 980 packet = connection->avctp_reassembly_buffer; 981 size = connection->avctp_reassembly_size; 982 break; 983 #endif 984 default: 985 return; 986 } 987 988 if (size < 6u) return; 989 990 uint16_t pid = big_endian_read_16(packet, 1); 991 992 if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){ 993 log_info("Invalid pid 0x%02x, expected 0x%02x", pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 994 connection->target_reject_transport_header = true; 995 connection->target_invalid_pid = pid; 996 connection->state = AVCTP_W2_SEND_RESPONSE; 997 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 998 return; 999 } 1000 1001 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 1002 // avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 1003 1004 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 1005 1006 int pos = 6; 1007 uint16_t length; 1008 avrcp_pdu_id_t pdu_id; 1009 // connection->data_len = 0; 1010 uint8_t offset; 1011 uint8_t operand; 1012 uint16_t event_mask; 1013 avrcp_operation_id_t operation_id; 1014 1015 switch (opcode){ 1016 case AVRCP_CMD_OPCODE_UNIT_INFO: 1017 avrcp_target_unit_info(connection); 1018 break; 1019 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 1020 if ((size - pos) < 3) return; 1021 // page: packet[pos] >> 4, 1022 offset = 4 * (packet[pos]>>4); 1023 // extension code (fixed 7) = packet[pos] & 0x0F 1024 // 4 bytes paga data, all 0xFF 1025 avrcp_target_subunit_info(connection, offset); 1026 break; 1027 1028 case AVRCP_CMD_OPCODE_PASS_THROUGH: 1029 if (size < 8) return; 1030 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 1031 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f); 1032 operand = 0; 1033 if ((packet[7] >= 1) && (size >= 9)){ 1034 operand = packet[8]; 1035 } 1036 1037 if (avcrp_operation_id_is_valid(operation_id)){ 1038 bool button_pressed = (packet[6] & 0x80) == 0; 1039 1040 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1041 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 1042 operation_id, button_pressed, packet[7], operand); 1043 } else { 1044 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1045 } 1046 break; 1047 1048 1049 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 1050 1051 if (size < 13) return; 1052 1053 // pos = 6 - company id 1054 (void)memcpy(connection->message_body, &packet[pos], 3); 1055 // connection->data_len = 3; 1056 pos += 3; 1057 // pos = 9 1058 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 1059 // 1 - reserved 1060 pos++; 1061 // 2-3 param length, 1062 length = big_endian_read_16(packet, pos); 1063 pos += 2; 1064 // pos = 13 1065 switch (pdu_id){ 1066 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 1067 if ((pos + 2) > size) return; 1068 bool ok = length == 4; 1069 if (avrcp_target_context.set_addressed_player_callback != NULL){ 1070 uint16_t player_id = big_endian_read_16(packet, pos); 1071 ok = avrcp_target_context.set_addressed_player_callback(player_id); 1072 } 1073 if (ok){ 1074 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS); 1075 } else { 1076 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 1077 } 1078 break; 1079 } 1080 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1081 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 1082 switch (capability_id){ 1083 case AVRCP_CAPABILITY_ID_EVENT: 1084 avrcp_target_response_vendor_dependent_supported_events(connection); 1085 break; 1086 case AVRCP_CAPABILITY_ID_COMPANY: 1087 avrcp_target_response_vendor_dependent_supported_companies(connection); 1088 break; 1089 default: 1090 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1091 break; 1092 } 1093 break; 1094 } 1095 case AVRCP_PDU_ID_GET_PLAY_STATUS: 1096 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 1097 break; 1098 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1099 if ((pos + 1) > size) return; 1100 connection->target_abort_continue_response = true; 1101 connection->state = AVCTP_W2_SEND_RESPONSE; 1102 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1103 break; 1104 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 1105 if ((pos + 1) > size) return; 1106 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 1107 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1108 return; 1109 } 1110 connection->target_continue_response = true; 1111 connection->target_now_playing_info_response = true; 1112 connection->state = AVCTP_W2_SEND_RESPONSE; 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 connection->state = AVCTP_W2_SEND_RESPONSE; 1143 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1144 break; 1145 } 1146 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 1147 if ((pos + 1) > size) return; 1148 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos]; 1149 1150 avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id); 1151 1152 if (event_id < AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED || 1153 event_id > AVRCP_NOTIFICATION_EVENT_MAX_VALUE){ 1154 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1155 return; 1156 } 1157 1158 switch (event_id){ 1159 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED: 1160 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 1161 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 1162 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END: 1163 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START: 1164 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED: 1165 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED: 1166 case AVRCP_NOTIFICATION_EVENT_MAX_VALUE: 1167 avrcp_target_response_vendor_dependent_not_implemented(connection, pdu_id, event_id); 1168 return; 1169 default: 1170 break; 1171 } 1172 1173 event_mask = (1 << event_id); 1174 connection->notifications_enabled |= event_mask; 1175 1176 switch (event_id){ 1177 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED: 1178 if (connection->target_track_selected){ 1179 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8); 1180 } else { 1181 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8); 1182 } 1183 break; 1184 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED: 1185 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_playback_status, 1); 1186 break; 1187 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED: 1188 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0); 1189 break; 1190 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED: 1191 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_absolute_volume, 1); 1192 break; 1193 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED: 1194 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->target_battery_status, 1); 1195 break; 1196 case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 1197 avrcp_target_response_addressed_player_changed_interim(connection, pdu_id, event_id); 1198 return; 1199 default: 1200 btstack_assert(false); 1201 return; 1202 } 1203 break; 1204 } 1205 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: { 1206 if ( (length != 1) || ((pos + 1) > size)){ 1207 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1208 break; 1209 } 1210 1211 uint8_t absolute_volume = packet[pos]; 1212 if (absolute_volume < 0x80){ 1213 connection->target_absolute_volume = absolute_volume; 1214 } 1215 avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->target_absolute_volume); 1216 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, connection->target_absolute_volume); 1217 break; 1218 } 1219 default: 1220 log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id); 1221 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1222 break; 1223 } 1224 break; 1225 default: 1226 log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size)); 1227 break; 1228 } 1229 } 1230 1231 static void avrcp_target_notification_init(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){ 1232 btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH); 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 != NULL){ 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 if (connection->state == AVCTP_W2_SEND_RESPONSE){ 1297 // start AVCTP 1298 if (connection->target_reject_transport_header){ 1299 connection->target_reject_transport_header = false; 1300 avctp_send_reject_cmd_wrong_pid(connection); 1301 connection->state = AVCTP_CONNECTION_OPENED; 1302 return; 1303 } 1304 // end AVCTP 1305 1306 // start AVRCP 1307 if (connection->target_abort_continue_response){ 1308 connection->target_abort_continue_response = false; 1309 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 1310 break; 1311 } 1312 1313 if (connection->target_now_playing_info_response){ 1314 connection->target_now_playing_info_response = false; 1315 if (connection->target_continue_response){ 1316 connection->target_continue_response = false; 1317 if (connection->data_len == 0){ 1318 avrcp_target_response_vendor_dependent_reject(connection, connection->pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1319 return; 1320 } 1321 } else { 1322 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES); 1323 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection); 1324 } 1325 break; 1326 } 1327 1328 // data already prepared 1329 break; 1330 } 1331 1332 // Notifications 1333 1334 if (connection->target_track_changed){ 1335 connection->target_track_changed = false; 1336 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED; 1337 avrcp_target_notification_init(connection, notification_id, connection->target_track_id, 8); 1338 break; 1339 } 1340 1341 if (connection->target_playback_status_changed){ 1342 connection->target_playback_status_changed = false; 1343 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED; 1344 uint8_t playback_status = (uint8_t) connection->target_playback_status; 1345 avrcp_target_notification_init(connection, notification_id, &playback_status, 1); 1346 break; 1347 } 1348 1349 if (connection->target_playing_content_changed){ 1350 connection->target_playing_content_changed = false; 1351 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED; 1352 avrcp_target_notification_init(connection, notification_id, NULL, 0); 1353 break; 1354 } 1355 1356 if (connection->target_battery_status_changed){ 1357 connection->target_battery_status_changed = false; 1358 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 1359 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->target_battery_status, 1); 1360 break; 1361 } 1362 1363 if (connection->target_notify_absolute_volume_changed){ 1364 connection->target_notify_absolute_volume_changed = false; 1365 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; 1366 avrcp_target_notification_init(connection, notification_id, &connection->target_absolute_volume, 1); 1367 break; 1368 } 1369 1370 if (connection->target_addressed_player_changed){ 1371 connection->target_addressed_player_changed = false; 1372 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1373 avrcp_target_notification_addressed_player_changed_init(connection); 1374 break; 1375 } 1376 1377 // nothing to send, exit 1378 return; 1379 1380 default: 1381 return; 1382 } 1383 1384 avrcp_send_response_with_avctp_fragmentation(connection); 1385 avrcp_target_reset_notification(connection, notification_id); 1386 avrcp_request_next_avctp_segment(connection); 1387 } 1388 1389 void avrcp_target_init(void){ 1390 avrcp_target_context.role = AVRCP_TARGET; 1391 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1392 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1393 } 1394 1395 void avrcp_target_deinit(void){ 1396 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1397 } 1398 1399 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1400 btstack_assert(callback != NULL); 1401 avrcp_target_context.avrcp_callback = callback; 1402 } 1403 1404 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1405 btstack_assert(callback != NULL); 1406 avrcp_target_context.set_addressed_player_callback = callback; 1407 } 1408 1409 1410