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