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