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 MATTHIAS 24 * RINGWALD 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 <string.h> 42 #include <inttypes.h> 43 44 #include "btstack.h" 45 #include "classic/avrcp.h" 46 47 #define AVRCP_ATTR_HEADER_LEN 8 48 49 static const uint8_t AVRCP_NOTIFICATION_TRACK_SELECTED[] = {0,0,0,0,0,0,0,0}; 50 static const uint8_t AVRCP_NOTIFICATION_TRACK_NOT_SELECTED[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; 51 52 avrcp_context_t avrcp_target_context; 53 54 static int avrcp_target_supports_browsing(uint16_t target_supported_features){ 55 return target_supported_features & AVRCP_FEATURE_MASK_BROWSING; 56 } 57 58 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){ 59 avrcp_create_sdp_record(0, service, service_record_handle, avrcp_target_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 60 } 61 62 static void 63 avrcp_target_emit_operation(btstack_packet_handler_t callback, uint16_t avrcp_cid, avrcp_operation_id_t operation_id, 64 bool button_pressed, uint8_t operands_length, uint8_t operand) { 65 btstack_assert(callback != NULL); 66 67 uint8_t event[9]; 68 int pos = 0; 69 event[pos++] = HCI_EVENT_AVRCP_META; 70 event[pos++] = sizeof(event) - 2; 71 event[pos++] = AVRCP_SUBEVENT_OPERATION; 72 little_endian_store_16(event, pos, avrcp_cid); 73 pos += 2; 74 event[pos++] = operation_id; 75 event[pos++] = button_pressed ? 1 : 0; 76 event[pos++] = operands_length; 77 event[pos++] = operand; 78 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 79 } 80 81 static void avrcp_target_emit_volume_changed(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t absolute_volume){ 82 btstack_assert(callback != NULL); 83 84 uint8_t event[7]; 85 int offset = 0; 86 event[offset++] = HCI_EVENT_AVRCP_META; 87 event[offset++] = sizeof(event) - 2; 88 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 89 little_endian_store_16(event, offset, avrcp_cid); 90 offset += 2; 91 event[offset++] = AVRCP_CTYPE_NOTIFY; 92 event[offset++] = absolute_volume; 93 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 94 } 95 96 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){ 97 btstack_assert(callback != NULL); 98 99 uint8_t event[5]; 100 int pos = 0; 101 event[pos++] = HCI_EVENT_AVRCP_META; 102 event[pos++] = sizeof(event) - 2; 103 event[pos++] = subevent_id; 104 little_endian_store_16(event, pos, avrcp_cid); 105 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 106 } 107 108 // returns number of bytes stored 109 static uint16_t avrcp_target_pack_single_element_header(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint16_t attr_value_size){ 110 btstack_assert(attr_id >= 1); 111 btstack_assert(attr_id <= AVRCP_MEDIA_ATTR_COUNT); 112 113 big_endian_store_32(packet, pos, attr_id); 114 big_endian_store_16(packet, pos+4, RFC2978_CHARSET_MIB_UTF8); 115 big_endian_store_16(packet, pos+6, attr_value_size); 116 return 8; 117 } 118 119 // returns number of bytes stored 120 static uint16_t avrcp_target_pack_single_element_attribute_number(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint32_t value){ 121 uint16_t attr_value_length = sprintf((char *)(packet+pos+8), "%0" PRIu32, value); 122 (void) avrcp_target_pack_single_element_header(packet, pos, attr_id, attr_value_length); 123 return 8 + attr_value_length; 124 } 125 126 // returns number of bytes stored 127 static uint16_t avrcp_target_pack_single_element_attribute_string_fragment(uint8_t * packet, uint16_t pos, avrcp_media_attribute_id_t attr_id, uint8_t * attr_value, uint16_t attr_value_to_copy, uint16_t attr_value_size, bool header){ 128 if (attr_value_size == 0) return 0; 129 uint16_t bytes_stored = 0; 130 if (header){ 131 bytes_stored += avrcp_target_pack_single_element_header(packet, pos, attr_id, attr_value_size); 132 } 133 (void)memcpy(packet + pos + bytes_stored, attr_value, attr_value_to_copy); 134 bytes_stored += attr_value_to_copy; 135 return bytes_stored; 136 } 137 138 static int avrcp_target_abort_continue_response(uint16_t cid, avrcp_connection_t * connection){ 139 uint16_t pos = 0; 140 l2cap_reserve_packet_buffer(); 141 uint8_t * packet = l2cap_get_outgoing_buffer(); 142 143 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 144 connection->command_type = AVRCP_CTYPE_RESPONSE_ACCEPTED; 145 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 146 connection->subunit_id = AVRCP_SUBUNIT_ID; 147 148 packet[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 149 // Profile IDentifier (PID) 150 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 151 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 152 153 // command_type 154 packet[pos++] = connection->command_type; 155 // subunit_type | subunit ID 156 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 157 // opcode 158 packet[pos++] = (uint8_t)connection->command_opcode; 159 160 // company id is 3 bytes long 161 big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID); 162 pos += 3; 163 164 packet[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; 165 166 // reserve byte for packet type 167 packet[pos++] = AVRCP_SINGLE_PACKET; 168 big_endian_store_16(packet, pos, 0); 169 pos += 2; 170 return l2cap_send_prepared(cid, pos); 171 } 172 173 static int avrcp_target_send_now_playing_info(uint16_t cid, avrcp_connection_t * connection){ 174 uint16_t pos = 0; 175 l2cap_reserve_packet_buffer(); 176 uint8_t * packet = l2cap_get_outgoing_buffer(); 177 uint16_t size = l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid); 178 179 packet[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 180 // Profile IDentifier (PID) 181 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 182 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 183 184 // command_type 185 packet[pos++] = connection->command_type; 186 // subunit_type | subunit ID 187 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 188 // opcode 189 packet[pos++] = (uint8_t)connection->command_opcode; 190 191 // company id is 3 bytes long 192 big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID); 193 pos += 3; 194 195 packet[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 196 197 // reserve byte for packet type 198 uint8_t pos_packet_type = pos; 199 pos++; 200 201 uint16_t playing_info_buffer_len_position = pos; 202 pos += 2; 203 if (connection->next_attr_id == AVRCP_MEDIA_ATTR_NONE){ 204 packet[pos_packet_type] = AVRCP_SINGLE_PACKET; 205 connection->packet_type = AVRCP_SINGLE_PACKET; 206 packet[pos++] = count_set_bits_uint32(connection->now_playing_info_attr_bitmap); 207 connection->next_attr_id = AVRCP_MEDIA_ATTR_ALL; 208 } 209 210 uint8_t fragmented = 0; 211 int num_free_bytes = size - pos - 2; 212 uint8_t MAX_NUMBER_ATTR_LEN = 10; 213 214 while (!fragmented && (num_free_bytes > 0) && (connection->next_attr_id <= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS)){ 215 avrcp_media_attribute_id_t attr_id = connection->next_attr_id; 216 int attr_index = attr_id - 1; 217 218 if (connection->now_playing_info_attr_bitmap & (1 << attr_id)){ 219 int num_written_bytes = 0; 220 int num_bytes_to_write = 0; 221 switch (attr_id){ 222 case AVRCP_MEDIA_ATTR_ALL: 223 case AVRCP_MEDIA_ATTR_NONE: 224 break; 225 case AVRCP_MEDIA_ATTR_TRACK: 226 num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN; 227 if (num_free_bytes >= num_bytes_to_write){ 228 num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->track_nr); 229 break; 230 } 231 fragmented = 1; 232 connection->attribute_value_offset = 0; 233 break; 234 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 235 num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN; 236 if (num_free_bytes >= num_bytes_to_write){ 237 num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->total_tracks); 238 break; 239 } 240 fragmented = 1; 241 connection->attribute_value_offset = 0; 242 break; 243 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 244 num_bytes_to_write = AVRCP_ATTR_HEADER_LEN + MAX_NUMBER_ATTR_LEN; 245 if (num_free_bytes >= num_bytes_to_write){ 246 num_written_bytes = avrcp_target_pack_single_element_attribute_number(packet, pos, attr_id, connection->song_length_ms); 247 break; 248 } 249 fragmented = 1; 250 connection->attribute_value_offset = 0; 251 break; 252 default:{ 253 bool header = connection->attribute_value_offset == 0; 254 uint8_t * attr_value = (uint8_t *) (connection->now_playing_info[attr_index].value + connection->attribute_value_offset); 255 uint16_t attr_value_len = connection->now_playing_info[attr_index].len - connection->attribute_value_offset; 256 257 num_bytes_to_write = attr_value_len + (header * AVRCP_ATTR_HEADER_LEN); 258 if (num_bytes_to_write <= num_free_bytes){ 259 connection->attribute_value_offset = 0; 260 num_written_bytes = num_bytes_to_write; 261 avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header); 262 break; 263 } 264 fragmented = 1; 265 num_written_bytes = num_free_bytes; 266 attr_value_len = num_free_bytes - (header * AVRCP_ATTR_HEADER_LEN); 267 avrcp_target_pack_single_element_attribute_string_fragment(packet, pos, attr_id, attr_value, attr_value_len, connection->now_playing_info[attr_index].len, header); 268 connection->attribute_value_offset += attr_value_len; 269 break; 270 } 271 } 272 pos += num_written_bytes; 273 num_free_bytes -= num_written_bytes; 274 } 275 if (!fragmented){ 276 // C++ compatible version of connection->next_attr_id++ 277 connection->next_attr_id = (avrcp_media_attribute_id_t) (((int) connection->next_attr_id) + 1); 278 } 279 } 280 281 if (fragmented){ 282 switch (connection->packet_type){ 283 case AVRCP_SINGLE_PACKET: 284 connection->packet_type = AVRCP_START_PACKET; 285 break; 286 default: 287 connection->packet_type = AVRCP_CONTINUE_PACKET; 288 break; 289 } 290 } else { 291 if (connection->next_attr_id >= AVRCP_MEDIA_ATTR_SONG_LENGTH_MS){ // DONE 292 if (connection->packet_type != AVRCP_SINGLE_PACKET){ 293 connection->packet_type = AVRCP_END_PACKET; 294 } 295 } 296 } 297 packet[pos_packet_type] = connection->packet_type; 298 // store attr value length 299 big_endian_store_16(packet, playing_info_buffer_len_position, pos - playing_info_buffer_len_position - 2); 300 return l2cap_send_prepared(cid, size); 301 } 302 303 304 305 static int avrcp_target_send_response(uint16_t cid, avrcp_connection_t * connection){ 306 int pos = 0; 307 l2cap_reserve_packet_buffer(); 308 uint8_t * packet = l2cap_get_outgoing_buffer(); 309 310 // transport header 311 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 312 313 // TODO: check for fragmentation 314 connection->packet_type = AVRCP_SINGLE_PACKET; 315 316 packet[pos++] = (connection->transaction_id << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 317 // Profile IDentifier (PID) 318 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 319 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 320 // command_type 321 packet[pos++] = connection->command_type; 322 // subunit_type | subunit ID 323 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 324 // opcode 325 packet[pos++] = (uint8_t)connection->command_opcode; 326 327 (void)memcpy(packet + pos, connection->cmd_operands, 328 connection->cmd_operands_length); 329 pos += connection->cmd_operands_length; 330 331 return l2cap_send_prepared(cid, pos); 332 } 333 334 static void avrcp_target_response_setup(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 335 avrcp_command_opcode_t opcode){ 336 connection->command_type = command_type; 337 connection->subunit_type = subunit_type; 338 connection->subunit_id = subunit_id; 339 connection->command_opcode = opcode; 340 } 341 342 static uint8_t avrcp_target_response_accept(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t status){ 343 // AVRCP_CTYPE_RESPONSE_REJECTED 344 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_ACCEPTED, subunit_type, subunit_id, opcode); 345 // company id is 3 bytes long 346 int pos = connection->cmd_operands_length; 347 connection->cmd_operands[pos++] = pdu_id; 348 connection->cmd_operands[pos++] = 0; 349 // param length 350 big_endian_store_16(connection->cmd_operands, pos, 1); 351 pos += 2; 352 connection->cmd_operands[pos++] = status; 353 connection->cmd_operands_length = pos; 354 connection->accept_response = 1; 355 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 356 return ERROR_CODE_SUCCESS; 357 } 358 359 static uint8_t avrcp_target_response_reject(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){ 360 // AVRCP_CTYPE_RESPONSE_REJECTED 361 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_REJECTED, subunit_type, subunit_id, opcode); 362 // company id is 3 bytes long 363 int pos = connection->cmd_operands_length; 364 connection->cmd_operands[pos++] = pdu_id; 365 connection->cmd_operands[pos++] = 0; 366 // param length 367 big_endian_store_16(connection->cmd_operands, pos, 1); 368 pos += 2; 369 connection->cmd_operands[pos++] = status; 370 connection->cmd_operands_length = pos; 371 connection->state = AVCTP_W2_SEND_RESPONSE; 372 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 373 return ERROR_CODE_SUCCESS; 374 } 375 376 static uint8_t avrcp_target_response_not_implemented(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, uint8_t event_id){ 377 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_NOT_IMPLEMENTED, subunit_type, subunit_id, opcode); 378 // company id is 3 bytes long 379 int pos = connection->cmd_operands_length; 380 connection->cmd_operands[pos++] = pdu_id; 381 connection->cmd_operands[pos++] = 0; 382 // param length 383 big_endian_store_16(connection->cmd_operands, pos, 1); 384 pos += 2; 385 connection->cmd_operands[pos++] = event_id; 386 connection->cmd_operands_length = pos; 387 388 connection->state = AVCTP_W2_SEND_RESPONSE; 389 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 390 return ERROR_CODE_SUCCESS; 391 } 392 393 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){ 394 395 // company id is 3 bytes long 396 int pos = connection->cmd_operands_length; 397 connection->cmd_operands[pos++] = pdu_id; 398 connection->cmd_operands[pos++] = 0; 399 // param length 400 big_endian_store_16(connection->cmd_operands, pos, 1 + value_len); 401 pos += 2; 402 connection->cmd_operands[pos++] = event_id; 403 if (value && (value_len > 0)){ 404 (void)memcpy(connection->cmd_operands + pos, value, value_len); 405 pos += value_len; 406 } 407 connection->cmd_operands_length = pos; 408 connection->state = AVCTP_W2_SEND_RESPONSE; 409 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 410 return ERROR_CODE_SUCCESS; 411 } 412 413 static uint8_t avrcp_target_response_addressed_player_changed_interim(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id){ 414 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 415 416 // company id is 3 bytes long 417 int pos = connection->cmd_operands_length; 418 connection->cmd_operands[pos++] = pdu_id; 419 connection->cmd_operands[pos++] = 0; 420 // param length 421 big_endian_store_16(connection->cmd_operands, pos, 5); 422 pos += 2; 423 connection->cmd_operands[pos++] = AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED; 424 big_endian_read_16( &connection->cmd_operands[pos], connection->addressed_player_id); 425 pos += 2; 426 big_endian_read_16( &connection->cmd_operands[pos], connection->uid_counter); 427 pos += 2; 428 429 connection->cmd_operands_length = pos; 430 connection->state = AVCTP_W2_SEND_RESPONSE; 431 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 432 return ERROR_CODE_SUCCESS; 433 } 434 435 static uint8_t avrcp_target_pass_through_response(uint16_t avrcp_cid, avrcp_command_type_t cmd_type, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 436 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 437 if (!connection){ 438 log_error("Could not find a connection."); 439 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 440 } 441 avrcp_target_response_setup(connection, cmd_type, AVRCP_SUBUNIT_TYPE_PANEL, AVRCP_SUBUNIT_ID, AVRCP_CMD_OPCODE_PASS_THROUGH); 442 443 int pos = 0; 444 connection->cmd_operands[pos++] = opid; 445 connection->cmd_operands[pos++] = operands_length; 446 if (operands_length == 1){ 447 connection->cmd_operands[pos++] = operand; 448 } 449 connection->cmd_operands_length = pos; 450 451 connection->state = AVCTP_W2_SEND_RESPONSE; 452 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 453 return ERROR_CODE_SUCCESS; 454 } 455 456 uint8_t avrcp_target_operation_rejected(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 457 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_REJECTED, opid, operands_length, operand); 458 } 459 460 uint8_t avrcp_target_operation_accepted(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 461 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 462 } 463 464 uint8_t avrcp_target_operation_not_implemented(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint8_t operands_length, uint8_t operand){ 465 return avrcp_target_pass_through_response(avrcp_cid, AVRCP_CTYPE_RESPONSE_ACCEPTED, opid, operands_length, operand); 466 } 467 468 void avrcp_target_set_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){ 469 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 470 if (!connection){ 471 log_error("avrcp_target_set_unit_info: could not find a connection."); 472 return; 473 } 474 connection->unit_type = unit_type; 475 connection->company_id = company_id; 476 } 477 478 void 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){ 479 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 480 if (!connection){ 481 log_error("avrcp_target_set_subunit_info: could not find a connection."); 482 return; 483 } 484 connection->subunit_info_type = subunit_type; 485 connection->subunit_info_data = subunit_info_data; 486 connection->subunit_info_data_size = subunit_info_data_size; 487 } 488 489 static uint8_t avrcp_target_unit_info(avrcp_connection_t * connection){ 490 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 491 492 uint8_t unit = 0; 493 connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 494 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 495 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 496 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 497 498 connection->cmd_operands_length = 5; 499 connection->cmd_operands[0] = 0x07; 500 connection->cmd_operands[1] = (connection->unit_type << 4) | unit; 501 // company id is 3 bytes long 502 big_endian_store_32(connection->cmd_operands, 2, connection->company_id); 503 504 connection->state = AVCTP_W2_SEND_RESPONSE; 505 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 506 return ERROR_CODE_SUCCESS; 507 } 508 509 510 static uint8_t avrcp_target_subunit_info(avrcp_connection_t * connection, uint8_t offset){ 511 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 512 if ((offset - 4) > connection->subunit_info_data_size) return AVRCP_STATUS_INVALID_PARAMETER; 513 514 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 515 connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 516 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 517 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 518 519 uint8_t page = offset / 4; 520 uint8_t extension_code = 7; 521 connection->cmd_operands_length = 5; 522 connection->cmd_operands[0] = (page << 4) | extension_code; 523 524 (void)memcpy(connection->cmd_operands + 1, 525 connection->subunit_info_data + offset, 4); 526 527 connection->state = AVCTP_W2_SEND_RESPONSE; 528 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 529 return ERROR_CODE_SUCCESS; 530 } 531 532 static inline uint8_t avrcp_prepare_vendor_dependent_response(uint16_t avrcp_cid, avrcp_connection_t ** out_connection, avrcp_pdu_id_t pdu_id, uint16_t param_length){ 533 *out_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 534 if (!*out_connection){ 535 log_error("avrcp tartget: could not find a connection."); 536 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 537 } 538 539 if ((*out_connection)->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 540 (*out_connection)->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 541 (*out_connection)->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 542 (*out_connection)->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 543 (*out_connection)->subunit_id = AVRCP_SUBUNIT_ID; 544 545 (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = pdu_id; 546 // reserved 547 (*out_connection)->cmd_operands[(*out_connection)->cmd_operands_length++] = 0; 548 // param length 549 big_endian_store_16((*out_connection)->cmd_operands, (*out_connection)->cmd_operands_length, param_length); 550 (*out_connection)->cmd_operands_length += 2; 551 return ERROR_CODE_SUCCESS; 552 } 553 554 static uint8_t avrcp_target_capability(uint16_t avrcp_cid, avrcp_capability_id_t capability_id, uint8_t num_capabilities, const uint8_t *capabilities, uint8_t capabilities_size){ 555 avrcp_connection_t * connection = NULL; 556 uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_CAPABILITIES, 2 + capabilities_size); 557 if (status != ERROR_CODE_SUCCESS) return status; 558 559 connection->cmd_operands[connection->cmd_operands_length++] = capability_id; 560 connection->cmd_operands[connection->cmd_operands_length++] = num_capabilities; 561 (void)memcpy(connection->cmd_operands + connection->cmd_operands_length, 562 capabilities, capabilities_size); 563 connection->cmd_operands_length += capabilities_size; 564 565 connection->state = AVCTP_W2_SEND_RESPONSE; 566 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 567 return ERROR_CODE_SUCCESS; 568 } 569 570 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t num_event_ids, const uint8_t *event_ids, uint8_t event_ids_size){ 571 return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, num_event_ids, event_ids, event_ids_size); 572 } 573 574 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t num_company_ids, const uint8_t *company_ids, uint8_t company_ids_size){ 575 return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, num_company_ids, company_ids, company_ids_size); 576 } 577 578 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){ 579 avrcp_connection_t * connection = NULL; 580 uint8_t status = avrcp_prepare_vendor_dependent_response(avrcp_cid, &connection, AVRCP_PDU_ID_GET_PLAY_STATUS, 11); 581 if (status != ERROR_CODE_SUCCESS) return status; 582 583 big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_length_ms); 584 connection->cmd_operands_length += 4; 585 big_endian_store_32(connection->cmd_operands, connection->cmd_operands_length, song_position_ms); 586 connection->cmd_operands_length += 4; 587 connection->cmd_operands[connection->cmd_operands_length++] = play_status; 588 589 connection->state = AVCTP_W2_SEND_RESPONSE; 590 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 591 return ERROR_CODE_SUCCESS; 592 } 593 594 static uint8_t avrcp_target_now_playing_info(avrcp_connection_t * connection){ 595 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 596 connection->now_playing_info_response = 1; 597 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 598 connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 599 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 600 connection->subunit_id = AVRCP_SUBUNIT_ID; 601 602 connection->state = AVCTP_W2_SEND_RESPONSE; 603 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 604 return ERROR_CODE_SUCCESS; 605 } 606 607 static uint8_t avrcp_target_store_media_attr(avrcp_connection_t * connection, avrcp_media_attribute_id_t attr_id, const char * value){ 608 int index = attr_id - 1; 609 if (!value) return AVRCP_STATUS_INVALID_PARAMETER; 610 connection->now_playing_info[index].value = (uint8_t*)value; 611 connection->now_playing_info[index].len = strlen(value); 612 return ERROR_CODE_SUCCESS; 613 } 614 615 uint8_t avrcp_target_set_playback_status(uint16_t avrcp_cid, avrcp_playback_status_t playback_status){ 616 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 617 if (!connection){ 618 log_error("avrcp_unit_info: could not find a connection."); 619 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 620 } 621 if (connection->playback_status == playback_status) return ERROR_CODE_SUCCESS; 622 623 connection->playback_status = playback_status; 624 connection->playback_status_changed = 1; 625 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 626 return ERROR_CODE_SUCCESS; 627 } 628 629 void avrcp_target_set_now_playing_info(uint16_t avrcp_cid, const avrcp_track_t * current_track, uint16_t total_tracks){ 630 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 631 if (!connection){ 632 log_error("avrcp_unit_info: could not find a connection. cid 0x%02x\n", avrcp_cid); 633 return; 634 } 635 if (!current_track){ 636 connection->track_selected = 0; 637 connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR; 638 return; 639 } 640 (void)memcpy(connection->track_id, current_track->track_id, 8); 641 connection->song_length_ms = current_track->song_length_ms; 642 connection->track_nr = current_track->track_nr; 643 connection->total_tracks = total_tracks; 644 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_TITLE, current_track->title); 645 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ARTIST, current_track->artist); 646 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_ALBUM, current_track->album); 647 avrcp_target_store_media_attr(connection, AVRCP_MEDIA_ATTR_GENRE, current_track->genre); 648 connection->track_selected = 1; 649 650 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 651 connection->track_changed = 1; 652 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 653 } 654 return; 655 } 656 657 uint8_t avrcp_target_track_changed(uint16_t avrcp_cid, uint8_t * track_id){ 658 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 659 if (!connection){ 660 log_error("avrcp_target_track_changed: could not find connection."); 661 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 662 } 663 if (!track_id) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 664 665 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED)) { 666 connection->track_changed = 1; 667 (void)memcpy(connection->track_id, track_id, 8); 668 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 669 } 670 return ERROR_CODE_SUCCESS; 671 } 672 673 uint8_t avrcp_target_playing_content_changed(uint16_t avrcp_cid){ 674 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 675 if (!connection){ 676 log_error("avrcp_target_playing_content_changed: could not find a connection."); 677 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 678 } 679 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED)) { 680 connection->playing_content_changed = 1; 681 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 682 } 683 return ERROR_CODE_SUCCESS; 684 } 685 686 uint8_t avrcp_target_addressed_player_changed(uint16_t avrcp_cid, uint16_t player_id, uint16_t uid_counter){ 687 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 688 if (!connection){ 689 log_error("avrcp_unit_info: could not find a connection."); 690 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 691 } 692 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED)) { 693 connection->uid_counter = uid_counter; 694 connection->addressed_player_id = player_id; 695 } 696 return ERROR_CODE_SUCCESS; 697 } 698 699 uint8_t avrcp_target_battery_status_changed(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 700 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 701 if (!connection){ 702 log_error("avrcp_unit_info: could not find a connection."); 703 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 704 } 705 if (connection->battery_status == battery_status) return ERROR_CODE_SUCCESS; 706 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED)) { 707 connection->battery_status = battery_status; 708 connection->battery_status_changed = 1; 709 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 710 } 711 return ERROR_CODE_SUCCESS; 712 } 713 714 uint8_t avrcp_target_volume_changed(uint16_t avrcp_cid, uint8_t volume_percentage){ 715 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid); 716 if (!connection){ 717 log_error("avrcp_unit_info: could not find a connection."); 718 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 719 } 720 connection->volume_percentage = volume_percentage; 721 if (connection->notifications_enabled & (1 << AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED )) { 722 connection->notify_volume_percentage_changed = 1; 723 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 724 } 725 return ERROR_CODE_SUCCESS; 726 } 727 728 static void avrcp_target_set_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification, uint8_t transaction_label){ 729 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return; 730 connection->notifications_transaction_label[notification] = transaction_label; 731 } 732 733 static uint8_t avrcp_target_get_transaction_label_for_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification){ 734 if (notification > AVRCP_NOTIFICATION_EVENT_MAX_VALUE) return 0; 735 return connection->notifications_transaction_label[notification]; 736 } 737 738 static uint8_t avrcp_is_receive_pass_through_cmd(uint8_t operation_id){ 739 return operation_id & 0x80; 740 } 741 742 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 743 744 if (size < 6u) return; 745 746 uint16_t pid = 0; 747 uint8_t transport_header = packet[0]; 748 connection->transaction_id = transport_header >> 4; 749 750 avrcp_packet_type_t packet_type = (avrcp_packet_type_t) ((transport_header & 0x0F) >> 2); 751 switch (packet_type){ 752 case AVRCP_SINGLE_PACKET: 753 pid = big_endian_read_16(packet, 1); 754 break; 755 case AVRCP_START_PACKET: 756 pid = big_endian_read_16(packet, 2); 757 break; 758 default: 759 break; 760 } 761 762 switch (packet_type){ 763 case AVRCP_SINGLE_PACKET: 764 case AVRCP_START_PACKET: 765 if (pid != BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL){ 766 log_info("Invalid pid 0x%02x, expected 0x%02x", connection->invalid_pid, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL); 767 connection->reject_transport_header = 1; 768 connection->invalid_pid = pid; 769 connection->transport_header = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2 ) | (AVRCP_RESPONSE_FRAME << 1) | 1; 770 connection->state = AVCTP_W2_SEND_RESPONSE; 771 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 772 return; 773 } 774 break; 775 default: 776 break; 777 } 778 779 if (packet_type != AVRCP_SINGLE_PACKET) return; 780 781 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3); 782 avrcp_subunit_id_t subunit_id = (avrcp_subunit_id_t) (packet[4] & 0x07); 783 784 avrcp_command_opcode_t opcode = (avrcp_command_opcode_t) avrcp_cmd_opcode(packet,size); 785 786 int pos = 6; 787 uint16_t length; 788 avrcp_pdu_id_t pdu_id; 789 connection->cmd_operands_length = 0; 790 791 switch (opcode){ 792 case AVRCP_CMD_OPCODE_UNIT_INFO: 793 avrcp_target_unit_info(connection); 794 break; 795 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 796 if ((size - pos) < 3) return; 797 uint8_t offset = 4 * (packet[pos+2]>>4); 798 avrcp_target_subunit_info(connection, offset); 799 break; 800 } 801 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 802 if (size < 8) return; 803 log_info("AVRCP_OPERATION_ID 0x%02x, operands length %d", packet[6], packet[7]); 804 avrcp_operation_id_t operation_id = (avrcp_operation_id_t) packet[6]; 805 uint8_t operand = 0; 806 if ((packet[7] >= 1) && (size >= 9)){ 807 operand = packet[8]; 808 } 809 if (avrcp_is_receive_pass_through_cmd(operation_id)){ 810 operation_id = (avrcp_operation_id_t) (packet[6] & 0x7F); 811 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 812 break; 813 } 814 815 switch (operation_id){ 816 case AVRCP_OPERATION_ID_PLAY: 817 case AVRCP_OPERATION_ID_PAUSE: 818 case AVRCP_OPERATION_ID_STOP: 819 case AVRCP_OPERATION_ID_VOLUME_UP: 820 case AVRCP_OPERATION_ID_VOLUME_DOWN: 821 case AVRCP_OPERATION_ID_REWIND: 822 case AVRCP_OPERATION_ID_FAST_FORWARD: 823 case AVRCP_OPERATION_ID_FORWARD: 824 case AVRCP_OPERATION_ID_BACKWARD: 825 case AVRCP_OPERATION_ID_SKIP: 826 case AVRCP_OPERATION_ID_MUTE: 827 case AVRCP_OPERATION_ID_CHANNEL_UP: 828 case AVRCP_OPERATION_ID_CHANNEL_DOWN: 829 case AVRCP_OPERATION_ID_SELECT: 830 case AVRCP_OPERATION_ID_UP: 831 case AVRCP_OPERATION_ID_DOWN: 832 case AVRCP_OPERATION_ID_LEFT: 833 case AVRCP_OPERATION_ID_RIGHT: 834 case AVRCP_OPERATION_ID_ROOT_MENU: 835 avrcp_target_operation_accepted(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 836 avrcp_target_emit_operation(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 837 operation_id, true, packet[7], operand); 838 break; 839 case AVRCP_OPERATION_ID_UNDEFINED: 840 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 841 return; 842 default: 843 avrcp_target_operation_not_implemented(connection->avrcp_cid, (avrcp_operation_id_t) packet[6], packet[7], operand); 844 return; 845 } 846 break; 847 } 848 849 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 850 851 if (size < 13) return; 852 853 // pos = 6 - company id 854 (void)memcpy(connection->cmd_operands, &packet[pos], 3); 855 connection->cmd_operands_length = 3; 856 pos += 3; 857 // pos = 9 858 pdu_id = (avrcp_pdu_id_t) packet[pos++]; 859 // 1 - reserved 860 pos++; 861 // 2-3 param length, 862 length = big_endian_read_16(packet, pos); 863 pos += 2; 864 // pos = 13 865 switch (pdu_id){ 866 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:{ 867 if ((pos + 2) > size) return; 868 bool ok = length == 4; 869 if (avrcp_target_context.set_addressed_player_callback != NULL){ 870 uint16_t player_id = big_endian_read_16(packet, pos); 871 ok = avrcp_target_context.set_addressed_player_callback(player_id); 872 } 873 if (ok){ 874 avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_SUCCESS); 875 } else { 876 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PLAYER_ID); 877 } 878 break; 879 } 880 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 881 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos]; 882 switch (capability_id){ 883 case AVRCP_CAPABILITY_ID_EVENT: 884 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY); 885 break; 886 case AVRCP_CAPABILITY_ID_COMPANY: 887 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY); 888 break; 889 default: 890 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 891 break; 892 } 893 break; 894 } 895 case AVRCP_PDU_ID_GET_PLAY_STATUS: 896 avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY); 897 break; 898 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 899 if ((pos + 1) > size) return; 900 connection->cmd_operands[0] = packet[pos]; 901 connection->abort_continue_response = 1; 902 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 903 break; 904 case AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE: 905 if ((pos + 1) > size) return; 906 if (packet[pos] != AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES){ 907 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 908 return; 909 } 910 connection->now_playing_info_response = 1; 911 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 912 break; 913 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 914 if ((pos + 9) > size) return; 915 uint8_t play_identifier[8]; 916 memset(play_identifier, 0, 8); 917 if (memcmp(&packet[pos], play_identifier, 8) != 0) { 918 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 919 return; 920 } 921 pos += 8; 922 uint8_t attribute_count = packet[pos++]; 923 connection->next_attr_id = AVRCP_MEDIA_ATTR_NONE; 924 if (!attribute_count){ 925 connection->now_playing_info_attr_bitmap = 0xFE; 926 } else { 927 int i; 928 connection->now_playing_info_attr_bitmap = 0; 929 if ((pos + attribute_count * 2) > size) return; 930 for (i=0; i < attribute_count; i++){ 931 uint16_t attr_id = big_endian_read_16(packet, pos); 932 pos += 2; 933 connection->now_playing_info_attr_bitmap |= (1 << attr_id); 934 } 935 } 936 log_info("now_playing_info_attr_bitmap 0x%02x", connection->now_playing_info_attr_bitmap); 937 avrcp_target_now_playing_info(connection); 938 break; 939 } 940 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 941 if ((pos + 1) > size) return; 942 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos]; 943 uint16_t event_mask = (1 << event_id); 944 avrcp_target_set_transaction_label_for_notification(connection, event_id, connection->transaction_id); 945 946 switch (event_id){ 947 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED: 948 connection->notifications_enabled |= event_mask; 949 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 950 if (connection->track_selected){ 951 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_SELECTED, 8); 952 } else { 953 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, AVRCP_NOTIFICATION_TRACK_NOT_SELECTED, 8); 954 } 955 break; 956 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED: 957 connection->notifications_enabled |= event_mask; 958 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 959 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->playback_status, 1); 960 break; 961 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED: 962 connection->notifications_enabled |= event_mask; 963 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 964 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, NULL, 0); 965 break; 966 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED: 967 connection->notify_volume_percentage_changed = 0; 968 connection->notifications_enabled |= event_mask; 969 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 970 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->volume_percentage, 1); 971 break; 972 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED: 973 connection->notifications_enabled |= event_mask; 974 avrcp_target_response_setup(connection, AVRCP_CTYPE_RESPONSE_INTERIM, subunit_type, subunit_id, opcode); 975 avrcp_target_response_vendor_dependent_interim(connection, pdu_id, event_id, (const uint8_t *)&connection->battery_status, 1); 976 break; 977 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED: 978 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 979 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 980 avrcp_target_response_not_implemented(connection, subunit_type, subunit_id, opcode, pdu_id, event_id); 981 return; 982 case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 983 connection->notifications_enabled |= event_mask; 984 avrcp_target_response_addressed_player_changed_interim(connection, subunit_type, subunit_id, opcode, pdu_id); 985 return; 986 default: 987 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER); 988 return; 989 } 990 break; 991 } 992 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME: { 993 if (length != 1){ 994 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 995 break; 996 } 997 998 if ((pos + 1) > size) return; 999 uint8_t absolute_volume = packet[pos]; 1000 if (absolute_volume < 0x80){ 1001 connection->volume_percentage = absolute_volume; 1002 } 1003 avrcp_target_response_accept(connection, subunit_type, subunit_id, opcode, pdu_id, connection->volume_percentage); 1004 avrcp_target_emit_volume_changed(avrcp_target_context.avrcp_callback, connection->avrcp_cid, connection->volume_percentage); 1005 break; 1006 } 1007 default: 1008 log_info("AVRCP target: unhandled pdu id 0x%02x", pdu_id); 1009 avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND); 1010 break; 1011 } 1012 break; 1013 default: 1014 log_info("AVRCP target: opcode 0x%02x not implemented", avrcp_cmd_opcode(packet,size)); 1015 break; 1016 } 1017 } 1018 1019 static int avrcp_target_send_notification(uint16_t cid, avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id, uint8_t * value, uint16_t value_len){ 1020 if (!connection){ 1021 log_error("avrcp tartget: could not find a connection."); 1022 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1023 } 1024 1025 btstack_assert((value_len == 0) || (value != NULL)); 1026 1027 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1028 connection->command_type = AVRCP_CTYPE_RESPONSE_CHANGED_STABLE; 1029 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1030 connection->subunit_id = AVRCP_SUBUNIT_ID; 1031 connection->transaction_id = avrcp_target_get_transaction_label_for_notification(connection, notification_id); 1032 1033 uint16_t pos = 0; 1034 l2cap_reserve_packet_buffer(); 1035 uint8_t * packet = l2cap_get_outgoing_buffer(); 1036 1037 // value <= 8 ==> pdu <= 22 < L2CAP Default MTU 1038 btstack_assert((14 + value_len) <= l2cap_get_remote_mtu_for_local_cid(connection->l2cap_signaling_cid)); 1039 1040 connection->packet_type = AVRCP_SINGLE_PACKET; 1041 packet[pos++] = (connection->transaction_id << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 1042 // Profile IDentifier (PID) 1043 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 1044 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 1045 1046 // command_type 1047 packet[pos++] = connection->command_type; 1048 // subunit_type | subunit ID 1049 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 1050 // opcode 1051 packet[pos++] = (uint8_t)connection->command_opcode; 1052 1053 // company id is 3 bytes long 1054 big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID); 1055 pos += 3; 1056 1057 packet[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 1058 packet[pos++] = 0; 1059 1060 big_endian_store_16(packet, pos, 1 + value_len); 1061 pos += 2; 1062 packet[pos++] = notification_id; 1063 if (value_len > 0){ 1064 (void)memcpy(packet + pos, value, value_len); 1065 pos += value_len; 1066 } 1067 1068 return l2cap_send_prepared(cid, pos); 1069 } 1070 1071 static void avrcp_target_reset_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t notification_id){ 1072 if (!connection){ 1073 log_error("avrcp tartget: could not find a connection."); 1074 return; 1075 } 1076 connection->notifications_enabled &= ~(1 << notification_id); 1077 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1078 avrcp_target_set_transaction_label_for_notification(connection, notification_id, 0); 1079 } 1080 1081 static void avrcp_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1082 avrcp_connection_t * connection; 1083 switch (packet_type) { 1084 case L2CAP_DATA_PACKET: 1085 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1086 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1087 break; 1088 case HCI_EVENT_PACKET: 1089 switch (hci_event_packet_get_type(packet)){ 1090 case L2CAP_EVENT_CAN_SEND_NOW:{ 1091 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, channel); 1092 1093 if (connection->accept_response){ 1094 connection->accept_response = 0; 1095 avrcp_target_send_response(connection->l2cap_signaling_cid, connection); 1096 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1097 return; 1098 } 1099 1100 if (connection->abort_continue_response){ 1101 connection->abort_continue_response = 0; 1102 connection->now_playing_info_response = 0; 1103 avrcp_target_abort_continue_response(connection->l2cap_signaling_cid, connection); 1104 return; 1105 } 1106 1107 if (connection->now_playing_info_response){ 1108 connection->now_playing_info_response = 0; 1109 avrcp_target_send_now_playing_info(connection->l2cap_signaling_cid, connection); 1110 return; 1111 } 1112 1113 if (connection->track_changed){ 1114 connection->track_changed = 0; 1115 avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED, connection->track_id, 8); 1116 avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED); 1117 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1118 return; 1119 } 1120 1121 if (connection->playback_status_changed){ 1122 connection->playback_status_changed = 0; 1123 uint8_t playback_status = (uint8_t) connection->playback_status; 1124 avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED, &playback_status, 1); 1125 avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED); 1126 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1127 return; 1128 } 1129 1130 if (connection->playing_content_changed){ 1131 connection->playing_content_changed = 0; 1132 avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED, NULL, 0); 1133 avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED); 1134 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1135 return; 1136 } 1137 1138 if (connection->battery_status_changed){ 1139 connection->battery_status_changed = 0; 1140 avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED, (uint8_t *)&connection->battery_status, 1); 1141 avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED); 1142 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1143 return; 1144 } 1145 1146 if (connection->notify_volume_percentage_changed){ 1147 connection->notify_volume_percentage_changed = 0; 1148 avrcp_target_send_notification(connection->l2cap_signaling_cid, connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED, &connection->volume_percentage, 1); 1149 avrcp_target_reset_notification(connection, AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED); 1150 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1151 return; 1152 } 1153 1154 if (connection->reject_transport_header){ 1155 connection->state = AVCTP_CONNECTION_OPENED; 1156 connection->reject_transport_header = 0; 1157 l2cap_reserve_packet_buffer(); 1158 uint8_t * out_buffer = l2cap_get_outgoing_buffer(); 1159 out_buffer[0] = connection->transport_header; 1160 big_endian_store_16(out_buffer, 1, connection->invalid_pid); 1161 l2cap_send_prepared(connection->l2cap_signaling_cid, 3); 1162 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1163 return; 1164 } 1165 1166 switch (connection->state){ 1167 case AVCTP_W2_SEND_RESPONSE: 1168 connection->state = AVCTP_CONNECTION_OPENED; 1169 avrcp_target_send_response(connection->l2cap_signaling_cid, connection); 1170 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1171 return; 1172 default: 1173 break; 1174 } 1175 1176 break; 1177 } 1178 default: 1179 break; 1180 } 1181 default: 1182 break; 1183 } 1184 } 1185 1186 void avrcp_target_init(void){ 1187 avrcp_target_context.role = AVRCP_TARGET; 1188 avrcp_target_context.packet_handler = avrcp_target_packet_handler; 1189 avrcp_register_target_packet_handler(&avrcp_target_packet_handler); 1190 } 1191 1192 void avrcp_target_deinit(void){ 1193 memset(&avrcp_target_context, 0, sizeof(avrcp_context_t)); 1194 } 1195 1196 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 1197 btstack_assert(callback != NULL); 1198 avrcp_target_context.avrcp_callback = callback; 1199 } 1200 1201 void avrcp_target_register_set_addressed_player_handler(bool (*callback)(uint16_t player_id)){ 1202 btstack_assert(callback != NULL); 1203 avrcp_target_context.set_addressed_player_callback = callback; 1204 } 1205 1206 1207