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