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->track_nr); 140 break; 141 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 142 str_len = sprintf(buffer, "%0" PRIu32, connection->total_tracks); 143 break; 144 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 145 str_len = sprintf(buffer, "%0" PRIu32, connection->song_length_ms); 146 break; 147 default: 148 str_len = connection->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->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->track_nr, num_bytes_to_copy); 195 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 196 return avrcp_get_attribute_value_from_u32(connection, connection->total_tracks, num_bytes_to_copy); 197 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 198 return avrcp_get_attribute_value_from_u32(connection, connection->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->now_playing_info[attr_index].len - connection->attribute_value_offset; 208 return (uint8_t *) (connection->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->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->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 uint8_t param_len = connection->data_len; 287 288 if (connection->avctp_packet_type == AVCTP_START_PACKET){ 289 uint8_t max_frame_size = btstack_min(l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid), 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->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 packet[pos++] = connection->data[0]; 332 connection->data_offset++; 333 334 packet[pos++] = connection->data[1]; 335 connection->data_offset++; 336 337 switch ((avrcp_capability_id_t) connection->data[0]) { 338 case AVRCP_CAPABILITY_ID_EVENT: 339 for (i = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; 340 i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++) { 341 if ((connection->target_supported_notifications & (1 << i)) == 0) { 342 continue; 343 } 344 packet[pos++] = i; 345 connection->data_offset++; 346 } 347 break; 348 case AVRCP_CAPABILITY_ID_COMPANY: 349 // use Bluetooth SIG as default company 350 if (connection->target_supported_companies_num == 0) { 351 little_endian_store_24(packet, pos, default_companies[0]); 352 pos += 3; 353 connection->data_offset += 3; 354 } else { 355 for (i = 0; i < connection->data[1]; i++) { 356 little_endian_store_24(packet, pos, 357 connection->target_supported_companies[i]); 358 pos += 3; 359 connection->data_offset += 3; 360 } 361 } 362 break; 363 default: 364 btstack_assert(false); 365 return; 366 } 367 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 368 return; 369 370 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 371 372 packet[pos++] = connection->num_attributes; 373 max_payload_size--; 374 375 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 376 377 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 378 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 379 return; 380 381 default: 382 break; 383 } 384 break; 385 386 case AVRCP_CMD_OPCODE_PASS_THROUGH: 387 packet[pos++] = connection->operation_id; 388 // parameter length 389 packet[pos++] = (uint8_t) connection->data_len; 390 pos += 2; 391 break; 392 case AVRCP_CMD_OPCODE_UNIT_INFO: 393 break; 394 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 395 break; 396 default: 397 btstack_assert(false); 398 return; 399 } 400 break; 401 case AVCTP_CONTINUE_PACKET: 402 case AVCTP_END_PACKET: 403 switch (connection->pdu_id) { 404 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 405 bytes_stored = avrcp_store_avctp_now_playing_info_fragment(connection, max_payload_size, packet + pos); 406 407 connection->avrcp_frame_bytes_sent += bytes_stored + pos; 408 l2cap_send_prepared(connection->l2cap_signaling_cid, pos + bytes_stored); 409 return; 410 411 default: 412 break; 413 } 414 break; 415 default: 416 btstack_assert(false); 417 return; 418 } 419 420 // compare number of bytes to store with the remaining buffer size 421 uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_payload_size - pos); 422 423 (void)memcpy(packet + pos, &connection->data[connection->data_offset], bytes_to_copy); 424 pos += bytes_to_copy; 425 connection->data_offset += bytes_to_copy; 426 connection->avrcp_frame_bytes_sent += pos; 427 428 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 429 } 430 431 static void avctp_send_reject_cmd_wrong_pid(avrcp_connection_t * connection){ 432 l2cap_reserve_packet_buffer(); 433 uint8_t * packet = l2cap_get_outgoing_buffer(); 434 435 // AVCTP header 436 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 437 packet[0] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 1; 438 big_endian_store_16(packet, 1, connection->cmd_operands[0]); 439 l2cap_send_prepared(connection->l2cap_signaling_cid, 3); 440 } 441 442 static void avrcp_target_custome_command_data_init(avrcp_connection_t * connection, 443 avrcp_command_opcode_t opcode, avrcp_command_type_t command_type, 444 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 445 avrcp_pdu_id_t pdu_id, uint32_t company_id){ 446 447 connection->command_opcode = opcode; 448 connection->command_type = command_type; 449 connection->subunit_type = subunit_type; 450 connection->subunit_id = subunit_id; 451 connection->company_id = company_id << 16; 452 connection->pdu_id = pdu_id; 453 connection->data = NULL; 454 connection->data_offset = 0; 455 connection->data_len = 0; 456 } 457 458 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){ 459 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 460 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 461 connection->subunit_id = AVRCP_SUBUNIT_ID; 462 connection->company_id = BT_SIG_COMPANY_ID; 463 464 connection->command_type = command_type; 465 connection->pdu_id = pdu_id; 466 connection->data = connection->cmd_operands; 467 connection->data_offset = 0; 468 connection->avrcp_frame_bytes_sent = 0; 469 connection->state = AVCTP_W2_SEND_RESPONSE; 470 } 471 472 static void avrcp_target_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_operation_id_t opid){ 473 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 474 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 475 connection->subunit_id = AVRCP_SUBUNIT_ID; 476 477 connection->command_type = command_type; 478 connection->company_id = 0; 479 connection->pdu_id = AVRCP_PDU_ID_UNDEFINED; 480 connection->operation_id = opid; 481 482 connection->data = connection->cmd_operands; 483 connection->data_offset = 0; 484 connection->data_len = 0; 485 } 486 487 488 static uint8_t avrcp_target_vendor_dependent_response_accept(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t status){ 489 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, pdu_id); 490 connection->data_len = 1; 491 connection->data[0] = status; 492 493 connection->accept_response = 1; 494 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 495 return ERROR_CODE_SUCCESS; 496 } 497 498 static uint8_t avrcp_target_response_vendor_dependent_reject(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){ 499 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_REJECTED, pdu_id); 500 connection->data_len = 1; 501 connection->data[0] = status; 502 503 connection->state = AVCTP_W2_SEND_RESPONSE; 504 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 505 return ERROR_CODE_SUCCESS; 506 } 507 508 static uint8_t avrcp_target_response_vendor_dependent_not_implemented(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 509 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, pdu_id); 510 connection->data_len = 1; 511 connection->data[0] = event_id; 512 513 connection->state = AVCTP_W2_SEND_RESPONSE; 514 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 515 return ERROR_CODE_SUCCESS; 516 } 517 518 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){ 519 btstack_assert(value_len + 1 < AVRCP_MAX_COMMAND_PARAMETER_LENGTH); 520 521 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 522 connection->data_len = 1 + value_len; 523 connection->data[0] = event_id; 524 525 if (value && (value_len > 0)){ 526 (void)memcpy(connection->data + 1, value, value_len); 527 } 528 529 connection->state = AVCTP_W2_SEND_RESPONSE; 530 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 531 return ERROR_CODE_SUCCESS; 532 } 533 534 static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 535 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_INTERIM, pdu_id); 536 537 connection->data_len = 5; 538 connection->data[0] = event_id; 539 big_endian_store_16(connection->data, 1, connection->addressed_player_id); 540 big_endian_store_16(connection->data, 3, connection->uid_counter); 541 542 connection->state = AVCTP_W2_SEND_RESPONSE; 543 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 544 return ERROR_CODE_SUCCESS; 545 } 546 547 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){ 548 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 549 if (!connection){ 550 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 551 } 552 avrcp_target_pass_through_command_data_init(connection, ctype, opid); 553 554 if (operands_length == 1){ 555 connection->data_len = 1; 556 connection->cmd_operands[0] = operand; 557 } 558 559 connection->state = AVCTP_W2_SEND_RESPONSE; 560 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 561 return ERROR_CODE_SUCCESS; 562 } 563 564 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 565 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand); 566 } 567 568 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 569 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 570 } 571 572 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 573 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 574 } 575 576 uint8_t avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){ 577 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 578 if (!connection){ 579 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 580 } 581 connection->unit_type = unit_type; 582 connection->company_id = company_id; 583 return ERROR_CODE_SUCCESS; 584 } 585 586 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){ 587 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 588 if (!connection){ 589 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 590 } 591 connection->subunit_info_type = subunit_type; 592 connection->subunit_info_data = subunit_info_data; 593 connection->subunit_info_data_size = subunit_info_data_size; 594 return ERROR_CODE_SUCCESS; 595 } 596 597 // TODO Review 598 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 599 if (connection->state != AVCTP_CONNECTION_OPENED){ 600 return ERROR_CODE_COMMAND_DISALLOWED; 601 } 602 connection->state = AVCTP_W2_SEND_RESPONSE; 603 604 avrcp_target_custom_command_data_init(connection, 605 AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 606 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 607 connection->company_id); 608 609 uint8_t unit = 0; 610 connection->data = connection->cmd_operands; 611 connection->data_len = 5; 612 connection->data[0] = 0x07; 613 connection->data[1] = (connection->unit_type << 4) | unit; 614 // company id is 3 bytes long 615 big_endian_store_24(connection->data, 2, connection->company_id); 616 617 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 618 return ERROR_CODE_SUCCESS; 619 } 620 621 // TODO Review 622 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 623 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 624 if (offset >= 32) return AVRCP_STATUS_INVALID_PARAMETER; 625 626 connection->state = AVCTP_W2_SEND_RESPONSE; 627 628 avrcp_target_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, 629 AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, 630 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 631 connection->company_id); 632 633 uint8_t page = offset / 4; 634 uint8_t extension_code = 7; 635 connection->data = connection->cmd_operands; 636 connection->data_len = 5; 637 connection->data[0] = (page << 4) | extension_code; 638 639 // mark non-existent entries with 0xff 640 memset(&connection->cmd_operands[1], 0xFF, 4); 641 if ((connection->data != NULL) && (offset < connection->subunit_info_data_size)){ 642 uint8_t bytes_to_copy = btstack_min(connection->subunit_info_data_size - offset, 4); 643 memcpy(&connection->data[1], &connection->subunit_info_data[offset], bytes_to_copy); 644 } 645 646 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 647 return ERROR_CODE_SUCCESS; 648 } 649 650 static uint8_t avrcp_target_response_vendor_dependent_supported_events(avrcp_connection_t * connection){ 651 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 652 connection->state = AVCTP_W2_SEND_RESPONSE; 653 654 uint8_t event_id; 655 uint8_t num_events = 0; 656 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 657 if ((connection->target_supported_notifications & (1<<event_id)) == 0){ 658 continue; 659 } 660 num_events++; 661 } 662 663 connection->data[0] = AVRCP_CAPABILITY_ID_EVENT; 664 connection->data[1] = num_events; 665 connection->data_len = 2 + num_events; 666 667 // fill the data later directly to the L2CAP outgoing buffer 668 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 669 return ERROR_CODE_SUCCESS; 670 } 671 672 static uint8_t avrcp_target_response_vendor_dependent_supported_companies(avrcp_connection_t * connection){ 673 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_CAPABILITIES); 674 connection->state = AVCTP_W2_SEND_RESPONSE; 675 676 connection->data[0] = AVRCP_CAPABILITY_ID_COMPANY; 677 connection->data[1] = connection->target_supported_companies_num; 678 connection->data_len = 2 + connection->target_supported_companies_num * 3; 679 680 // fill the data later directly to the L2CAP outgoing buffer and 681 // use Bluetooth SIG as default company 682 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 683 return ERROR_CODE_SUCCESS; 684 } 685 686 uint8_t avrcp_target_support_event(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 687 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 688 if (!connection){ 689 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 690 } 691 692 if ((event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 693 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 694 } 695 696 connection->target_supported_notifications |= (1 << (uint8_t)event_id); 697 return ERROR_CODE_SUCCESS; 698 } 699 700 uint8_t avrcp_target_support_companies(uint16_t avrcp_cid, uint8_t num_companies, const uint32_t *companies){ 701 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 702 if (!connection){ 703 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 704 } 705 706 connection->target_supported_companies_num = num_companies; 707 connection->target_supported_companies = companies; 708 return ERROR_CODE_SUCCESS; 709 } 710 711 // TODO Review (use flags) 712 uint8_t avrcp_target_play_status(uint16_t avrcp_cid, uint32_t song_length_ms, uint32_t song_position_ms, avrcp_playback_status_t play_status){ 713 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 714 if (!connection){ 715 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 716 } 717 if (connection->state != AVCTP_CONNECTION_OPENED){ 718 return ERROR_CODE_COMMAND_DISALLOWED; 719 } 720 721 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE, AVRCP_PDU_ID_GET_PLAY_STATUS); 722 connection->data_len = 9; 723 big_endian_store_32(connection->data, 0, song_length_ms); 724 big_endian_store_32(connection->data, 4, song_position_ms); 725 connection->data[8] = play_status; 726 727 connection->state = AVCTP_W2_SEND_RESPONSE; 728 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 729 return ERROR_CODE_SUCCESS; 730 } 731 732 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 733 int index = attr_id - 1; 734 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 735 connection->now_playing_info[index].value = (uint8_t*)value; 736 connection->now_playing_info[index].len = strlen(value); 737 return ERROR_CODE_SUCCESS; 738 } 739 740 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 741 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 742 if (!connection){ 743 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 744 } 745 if (connection->playback_status == playback_status){ 746 return ERROR_CODE_SUCCESS; 747 } 748 749 connection->playback_status = playback_status; 750 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED)) { 751 connection->playback_status_changed = 1; 752 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 753 } 754 return ERROR_CODE_SUCCESS; 755 } 756 757 uint8_t avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 758 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 759 if (!connection){ 760 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 761 } 762 if (!current_track){ 763 connection->track_selected = 0; 764 connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR; 765 return ERROR_CODE_COMMAND_DISALLOWED; 766 } 767 768 (void)memcpy(connection->track_id, current_track->track_id, 8); 769 connection->song_length_ms = current_track->song_length_ms; 770 connection->track_nr = current_track->track_nr; 771 connection->total_tracks = total_tracks; 772 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 773 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 774 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 775 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 776 777 connection->track_selected = 1; 778 779 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 780 connection->track_changed = 1; 781 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 782 } 783 return ERROR_CODE_SUCCESS; 784 } 785 786 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 787 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 788 if (!connection){ 789 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 790 } 791 if (!track_id){ 792 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 793 } 794 795 (void)memcpy(connection->track_id, track_id, 8); 796 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 797 connection->track_changed = 1; 798 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 799 } 800 return ERROR_CODE_SUCCESS; 801 } 802 803 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 804 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 805 if (!connection){ 806 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 807 } 808 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 809 connection->playing_content_changed = 1; 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->addressed_player_id == player_id){ 822 return ERROR_CODE_SUCCESS; 823 } 824 825 connection->uid_counter = uid_counter; 826 connection->addressed_player_id = player_id; 827 828 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 829 connection->addressed_player_changed = 1; 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->battery_status == battery_status){ 841 return ERROR_CODE_SUCCESS; 842 } 843 844 connection->battery_status = battery_status; 845 846 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 847 connection->battery_status_changed = 1; 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->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->absolute_volume == absolute_volume){ 869 return ERROR_CODE_SUCCESS; 870 } 871 872 connection->absolute_volume = absolute_volume; 873 874 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 875 connection->notify_absolute_volume_changed = 1; 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->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->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->reject_transport_header = true; 995 connection->invalid_pid = pid; 996 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 997 return; 998 } 999 1000 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 1001 // avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 1002 1003 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 1004 1005 int pos = 6; 1006 uint16_t length; 1007 avrcp_pdu_id_t pdu_id; 1008 // connection->data_len = 0; 1009 uint8_t offset; 1010 uint8_t operand; 1011 uint16_t event_mask; 1012 avrcp_operation_id_t operation_id; 1013 1014 switch (opcode){ 1015 case AVRCP_CMD_OPCODE_UNIT_INFO: 1016 avrcp_target_unit_info(connection); 1017 break; 1018 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 1019 if ((size - pos) < 3) return; 1020 // page: packet[pos] >> 4, 1021 offset = 4 * (packet[pos]>>4); 1022 // extension code (fixed 7) = packet[pos] & 0x0F 1023 // 4 bytes paga data, all 0xFF 1024 avrcp_target_subunit_info(connection, offset); 1025 break; 1026 1027 case AVRCP_CMD_OPCODE_PASS_THROUGH: 1028 if (size < 8) return; 1029 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 1030 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7f); 1031 operand = 0; 1032 if ((packet[7] >= 1) && (size >= 9)){ 1033 operand = packet[8]; 1034 } 1035 1036 if (avcrp_operation_id_is_valid(operation_id)){ 1037 bool button_pressed = (packet[6] & 0x80) == 0; 1038 1039 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1040 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 1041 operation_id, button_pressed, packet[7], operand); 1042 } else { 1043 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 1044 } 1045 break; 1046 1047 1048 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 1049 1050 if (size < 13) return; 1051 1052 // pos = 6 - company id 1053 (void)memcpy(connection->cmd_operands, &packet[pos], 3); 1054 // connection->data_len = 3; 1055 pos += 3; 1056 // pos = 9 1057 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 1058 // 1 - reserved 1059 pos++; 1060 // 2-3 param length, 1061 length = big_endian_read_16(packet, pos); 1062 pos += 2; 1063 // pos = 13 1064 switch (pdu_id){ 1065 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 1066 if ((pos + 2) > size) return; 1067 bool ok = length == 4; 1068 if (avrcp_target_context.set_addressed_player_callback != NULL){ 1069 uint16_t player_id = big_endian_read_16(packet, pos); 1070 ok = avrcp_target_context.set_addressed_player_callback(player_id); 1071 } 1072 if (ok){ 1073 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, AVRCP_STATUS_SUCCESS); 1074 } else { 1075 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 1076 } 1077 break; 1078 } 1079 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1080 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 1081 switch (capability_id){ 1082 case AVRCP_CAPABILITY_ID_EVENT: 1083 avrcp_target_response_vendor_dependent_supported_events(connection); 1084 break; 1085 case AVRCP_CAPABILITY_ID_COMPANY: 1086 avrcp_target_response_vendor_dependent_supported_companies(connection); 1087 break; 1088 default: 1089 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1090 break; 1091 } 1092 break; 1093 } 1094 case AVRCP_PDU_ID_GET_PLAY_STATUS: 1095 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 1096 break; 1097 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1098 if ((pos + 1) > size) return; 1099 connection->cmd_operands[0] = packet[pos]; 1100 connection->abort_continue_response = 1; 1101 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1102 break; 1103 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 1104 if ((pos + 1) > size) return; 1105 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 1106 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1107 return; 1108 } 1109 connection->now_playing_info_response = true; 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->now_playing_info_attr_bitmap = 0xFE; 1126 } else { 1127 int i; 1128 connection->next_attr_id = AVRCP_MEDIA_ATTR_TITLE; 1129 connection->now_playing_info_attr_bitmap = 0; 1130 connection->num_attributes = 0; 1131 if ((pos + attribute_count * 4) > size) return; 1132 for (i=0; i < attribute_count; i++){ 1133 uint32_t attr_id = big_endian_read_32(packet, pos); 1134 connection->now_playing_info_attr_bitmap |= (1 << attr_id); 1135 pos += 4; 1136 } 1137 } 1138 log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap); 1139 connection->num_attributes = count_set_bits_uint32(connection->now_playing_info_attr_bitmap); 1140 connection->now_playing_info_response = true; 1141 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1142 break; 1143 } 1144 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 1145 if ((pos + 1) > size) return; 1146 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos]; 1147 1148 avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id); 1149 1150 if (event_id < AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED || 1151 event_id > AVRCP_NOTIFICATION_EVENT_MAX_VALUE){ 1152 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 1153 return; 1154 } 1155 1156 switch (event_id){ 1157 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED: 1158 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 1159 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 1160 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END: 1161 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START: 1162 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED: 1163 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED: 1164 case AVRCP_NOTIFICATION_EVENT_MAX_VALUE: 1165 avrcp_target_response_vendor_dependent_not_implemented(connection, pdu_id, event_id); 1166 return; 1167 default: 1168 break; 1169 } 1170 1171 event_mask = (1 << event_id); 1172 connection->notifications_enabled |= event_mask; 1173 1174 switch (event_id){ 1175 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED: 1176 if (connection->track_selected){ 1177 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8); 1178 } else { 1179 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8); 1180 } 1181 break; 1182 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED: 1183 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1); 1184 break; 1185 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED: 1186 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0); 1187 break; 1188 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED: 1189 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->absolute_volume, 1); 1190 break; 1191 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED: 1192 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1); 1193 break; 1194 case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 1195 avrcp_target_response_addressed_player_changed_interim(connection, pdu_id, event_id); 1196 return; 1197 default: 1198 btstack_assert(false); 1199 return; 1200 } 1201 break; 1202 } 1203 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: { 1204 if ( (length != 1) || ((pos + 1) > size)){ 1205 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1206 break; 1207 } 1208 1209 uint8_t absolute_volume = packet[pos]; 1210 if (absolute_volume < 0x80){ 1211 connection->absolute_volume = absolute_volume; 1212 } 1213 avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->absolute_volume); 1214 avrcp_target_vendor_dependent_response_accept(connection, pdu_id, connection->absolute_volume); 1215 break; 1216 } 1217 default: 1218 log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id); 1219 avrcp_target_response_vendor_dependent_reject(connection, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1220 break; 1221 } 1222 break; 1223 default: 1224 log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size)); 1225 break; 1226 } 1227 } 1228 1229 static void avrcp_target_notification_init(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){ 1230 btstack_assert((value_len == 0) || (value != NULL)); 1231 1232 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1233 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, notification_id); 1234 1235 connection->data_len = 1 + value_len; 1236 connection->data[0] = notification_id; 1237 if (value_len > 0){ 1238 (void)memcpy(connection->data + 1, value, value_len); 1239 } 1240 } 1241 1242 static void avrcp_target_notification_addressed_player_changed_init(avrcp_connection_t * connection){ 1243 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_CHANGED_STABLE, AVRCP_PDU_ID_REGISTER_NOTIFICATION); 1244 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED); 1245 1246 connection->data_len = 5; 1247 connection->data[0] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1248 big_endian_store_16(connection->data, 1, connection->addressed_player_id); 1249 big_endian_store_16(connection->data, 3, connection->uid_counter); 1250 } 1251 1252 1253 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){ 1254 if (notification_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX || notification_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX){ 1255 return; 1256 } 1257 connection->notifications_enabled &= ~(1 << notification_id); 1258 connection->notifications_transaction_label[notification_id] = 0; 1259 } 1260 1261 static void avrcp_request_next_avctp_segment(avrcp_connection_t * connection){ 1262 // AVCTP 1263 switch (connection->avctp_packet_type){ 1264 case AVCTP_END_PACKET: 1265 case AVCTP_SINGLE_PACKET: 1266 connection->avrcp_frame_bytes_sent = 0; 1267 connection->data_offset = 0; 1268 connection->data_len = 0; 1269 connection->state = AVCTP_CONNECTION_OPENED; 1270 break; 1271 default: 1272 connection->state = AVCTP_W2_SEND_RESPONSE; 1273 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1274 break; 1275 } 1276 } 1277 1278 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1279 avrcp_connection_t * connection; 1280 bool send_response = true; 1281 avrcp_notification_event_id_t notification_id = AVRCP_NOTIFICATION_EVENT_NONE; 1282 1283 switch (packet_type){ 1284 case L2CAP_DATA_PACKET: 1285 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1286 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1287 return; 1288 1289 case HCI_EVENT_PACKET: 1290 if (hci_event_packet_get_type(packet) != L2CAP_EVENT_CAN_SEND_NOW){ 1291 return; 1292 } 1293 1294 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1295 if (connection == NULL){ 1296 return; 1297 } 1298 1299 // START AVCTP 1300 if (connection->reject_transport_header){ 1301 connection->reject_transport_header = false; 1302 avctp_send_reject_cmd_wrong_pid(connection); 1303 connection->state = AVCTP_CONNECTION_OPENED; 1304 return; 1305 } 1306 // END AVCTP 1307 1308 if (connection->accept_response){ 1309 connection->accept_response = 0; 1310 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE); 1311 break; 1312 } 1313 1314 if (connection->abort_continue_response){ 1315 connection->abort_continue_response = 0; 1316 connection->now_playing_info_response = false; 1317 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 1318 break; 1319 } 1320 1321 if (connection->now_playing_info_response){ 1322 connection->now_playing_info_response = false; 1323 avrcp_target_vendor_dependent_response_data_init(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES); 1324 connection->data_len = avrcp_now_playing_info_value_len_with_headers(connection); 1325 break; 1326 } 1327 1328 if (connection->track_changed){ 1329 connection->track_changed = 0; 1330 notification_id = AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED; 1331 avrcp_target_notification_init(connection, notification_id, connection->track_id, 8); 1332 break; 1333 } 1334 1335 if (connection->playback_status_changed){ 1336 connection->playback_status_changed = 0; 1337 notification_id = AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED; 1338 uint8_t playback_status = (uint8_t) connection->playback_status; 1339 avrcp_target_notification_init(connection, notification_id, &playback_status, 1); 1340 break; 1341 } 1342 1343 if (connection->playing_content_changed){ 1344 connection->playing_content_changed = 0; 1345 notification_id = AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED; 1346 avrcp_target_notification_init(connection, notification_id, NULL, avrcp_now_playing_info_value_len_with_headers(connection)); 1347 break; 1348 } 1349 1350 if (connection->battery_status_changed){ 1351 connection->battery_status_changed = 0; 1352 notification_id = AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 1353 avrcp_target_notification_init(connection, notification_id, (uint8_t *)&connection->battery_status, 1); 1354 break; 1355 } 1356 1357 if (connection->notify_absolute_volume_changed){ 1358 connection->notify_absolute_volume_changed = 0; 1359 notification_id = AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; 1360 avrcp_target_notification_init(connection, notification_id, &connection->absolute_volume, 1); 1361 break; 1362 } 1363 1364 if (connection->addressed_player_changed){ 1365 connection->addressed_player_changed = 0; 1366 notification_id = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 1367 avrcp_target_notification_addressed_player_changed_init(connection); 1368 break; 1369 } 1370 1371 send_response = false; 1372 break; 1373 1374 default: 1375 return; 1376 } 1377 1378 if (connection != NULL && (send_response || connection->state == AVCTP_W2_SEND_RESPONSE)){ 1379 avrcp_send_response_with_avctp_fragmentation(connection); 1380 avrcp_target_reset_notification(connection, notification_id); 1381 avrcp_request_next_avctp_segment(connection); 1382 } 1383 } 1384 1385 void avrcp_target_init(void){ 1386 avrcp_target_context.role = AVRCP_TARGET; 1387 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1388 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1389 } 1390 1391 void avrcp_target_deinit(void){ 1392 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1393 } 1394 1395 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1396 btstack_assert(callback != NULL); 1397 avrcp_target_context.avrcp_callback = callback; 1398 } 1399 1400 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1401 btstack_assert(callback != NULL); 1402 avrcp_target_context.set_addressed_player_callback = callback; 1403 } 1404 1405 1406