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_controller.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 #include "classic/avrcp_controller.h" 49 50 // made public in avrcp_controller.h 51 avrcp_context_t avrcp_controller_context; 52 53 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){ 54 return controller_supported_features & (1 << AVRCP_CONTROLLER_SUPPORTED_FEATURE_BROWSING); 55 } 56 57 void avrcp_controller_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t supported_features, const char * service_name, const char * service_provider_name){ 58 avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 59 } 60 61 static void avrcp_emit_repeat_and_shuffle_mode(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_repeat_mode_t repeat_mode, avrcp_shuffle_mode_t shuffle_mode){ 62 if (!callback) return; 63 uint8_t event[8]; 64 int pos = 0; 65 event[pos++] = HCI_EVENT_AVRCP_META; 66 event[pos++] = sizeof(event) - 2; 67 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 68 little_endian_store_16(event, pos, avrcp_cid); 69 pos += 2; 70 event[pos++] = ctype; 71 event[pos++] = repeat_mode; 72 event[pos++] = shuffle_mode; 73 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 74 } 75 76 static void avrcp_emit_operation_status(btstack_packet_handler_t callback, uint8_t subevent, uint16_t avrcp_cid, uint8_t ctype, uint8_t operation_id){ 77 if (!callback) return; 78 uint8_t event[7]; 79 int pos = 0; 80 event[pos++] = HCI_EVENT_AVRCP_META; 81 event[pos++] = sizeof(event) - 2; 82 event[pos++] = subevent; 83 little_endian_store_16(event, pos, avrcp_cid); 84 pos += 2; 85 event[pos++] = ctype; 86 event[pos++] = operation_id; 87 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 88 } 89 90 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 91 UNUSED(timer); 92 avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer); 93 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 94 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 95 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 96 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 97 } 98 99 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 100 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 101 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 102 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 103 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 104 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 105 } 106 107 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 108 connection->continuous_fast_forward_cmd = 0; 109 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 110 } 111 112 static uint8_t request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 113 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 114 if (connection->continuous_fast_forward_cmd){ 115 avrcp_press_and_hold_timer_stop(connection); 116 } 117 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 118 connection->transaction_label++; 119 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 120 return ERROR_CODE_SUCCESS; 121 } 122 123 static inline uint8_t request_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed, uint8_t continuous_fast_forward_cmd){ 124 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 125 if (!connection){ 126 log_error("avrcp: could not find a connection."); 127 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 128 } 129 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 130 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 131 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 132 connection->command_type = AVRCP_CTYPE_CONTROL; 133 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 134 connection->subunit_id = AVRCP_SUBUNIT_ID; 135 connection->cmd_operands_length = 0; 136 137 connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 138 connection->cmd_operands_length = 2; 139 connection->cmd_operands[0] = opid; 140 if (playback_speed > 0){ 141 connection->cmd_operands[2] = playback_speed; 142 connection->cmd_operands_length++; 143 } 144 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 145 146 if (connection->continuous_fast_forward_cmd){ 147 avrcp_press_and_hold_timer_start(connection); 148 } 149 150 connection->transaction_label++; 151 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 152 return ERROR_CODE_SUCCESS; 153 } 154 155 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 156 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0); 157 } 158 159 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 160 return request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1); 161 } 162 163 #define AVRCP_CMD_BUFFER_SIZE 30 164 static uint16_t avrcp_get_max_payload_size_for_packet_type(avrcp_packet_type_t packet_type){ 165 switch (packet_type){ 166 case AVRCP_SINGLE_PACKET: 167 return AVRCP_CMD_BUFFER_SIZE - 3; 168 case AVRCP_START_PACKET: 169 return AVRCP_CMD_BUFFER_SIZE - 4; 170 case AVRCP_CONTINUE_PACKET: 171 case AVRCP_END_PACKET: 172 return AVRCP_CMD_BUFFER_SIZE - 1; 173 } 174 return 0; 175 } 176 177 static int avrcp_send_cmd(avrcp_connection_t * connection, avrcp_packet_type_t packet_type){ 178 uint8_t command[AVRCP_CMD_BUFFER_SIZE]; 179 int pos = 0; 180 uint16_t max_bytes = sizeof(command) - 1; 181 182 // transport header 183 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 184 command[pos++] = (connection->transaction_label << 4) | (packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 185 186 if (packet_type == AVRCP_START_PACKET){ 187 // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header) 188 command[pos++] = ((connection->cmd_operands_fragmented_len + 3 - 1) / (AVRCP_CMD_BUFFER_SIZE - 1)) + 1; 189 max_bytes -= 3; 190 } 191 192 if (packet_type == AVRCP_SINGLE_PACKET || packet_type == AVRCP_START_PACKET){ 193 // Profile IDentifier (PID) 194 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 195 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 196 197 // command_type 198 command[pos++] = connection->command_type; 199 // subunit_type | subunit ID 200 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 201 // opcode 202 command[pos++] = (uint8_t)connection->command_opcode; 203 } 204 205 if (packet_type == AVRCP_SINGLE_PACKET){ 206 // operands 207 memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 208 pos += connection->cmd_operands_length; 209 } else { 210 uint16_t bytes_to_copy = btstack_min(connection->cmd_operands_fragmented_len-connection->cmd_operands_fragmented_pos, max_bytes); 211 memcpy(command+pos, &connection->cmd_operands_fragmented_buffer[connection->cmd_operands_fragmented_pos], bytes_to_copy); 212 pos += bytes_to_copy; 213 connection->cmd_operands_fragmented_pos += bytes_to_copy; 214 } 215 216 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 217 } 218 219 static int avrcp_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 220 if (connection->notifications_to_deregister & (1 << event_id)) return 0; 221 if (connection->notifications_enabled & (1 << event_id)) return 0; 222 if (connection->notifications_to_register & (1 << event_id)) return 0; 223 connection->notifications_to_register |= (1 << event_id); 224 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 225 return 1; 226 } 227 228 static void avrcp_prepare_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 229 connection->transaction_label++; 230 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 231 connection->command_type = AVRCP_CTYPE_NOTIFY; 232 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 233 connection->subunit_id = AVRCP_SUBUNIT_ID; 234 int pos = 0; 235 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 236 pos += 3; 237 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 238 connection->cmd_operands[pos++] = 0; // reserved(upper 6) | packet_type -> 0 239 big_endian_store_16(connection->cmd_operands, pos, 5); // parameter length 240 pos += 2; 241 connection->cmd_operands[pos++] = event_id; 242 big_endian_store_32(connection->cmd_operands, pos, 1); // send notification on playback position every second, for other notifications it is ignored 243 pos += 4; 244 connection->cmd_operands_length = pos; 245 // AVRCP_SPEC_V14.pdf 166 246 // answer page 61 247 } 248 249 250 static void avrcp_parser_reset(avrcp_connection_t * connection){ 251 connection->list_offset = 0; 252 connection->num_attributes = 0; 253 connection->num_parsed_attributes = 0; 254 connection->parser_attribute_header_pos = 0; 255 connection->num_received_fragments = 0; 256 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 257 } 258 259 static void avrcp_controller_emit_now_playing_info_event_done(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, uint8_t status){ 260 uint8_t event[7]; 261 int pos = 0; 262 event[pos++] = HCI_EVENT_AVRCP_META; 263 event[pos++] = sizeof(event) - 2; 264 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 265 little_endian_store_16(event, pos, avrcp_cid); 266 pos += 2; 267 event[pos++] = ctype; 268 event[pos++] = status; 269 // printf_hexdump(event, pos); 270 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 271 } 272 273 static void avrcp_controller_emit_now_playing_info_event(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t ctype, avrcp_media_attribute_id_t attr_id, uint8_t * value, uint16_t value_len){ 274 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 275 int pos = 0; 276 event[pos++] = HCI_EVENT_AVRCP_META; 277 // reserve one byte for subevent type and data len 278 int data_len_pos = pos; 279 pos++; 280 int subevent_type_pos = pos; 281 pos++; 282 little_endian_store_16(event, pos, avrcp_cid); 283 pos += 2; 284 event[pos++] = ctype; 285 286 switch (attr_id){ 287 case AVRCP_MEDIA_ATTR_TITLE: 288 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 289 event[pos++] = value_len; 290 memcpy(event+pos, value, value_len); 291 break; 292 case AVRCP_MEDIA_ATTR_ARTIST: 293 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO; 294 event[pos++] = value_len; 295 memcpy(event+pos, value, value_len); 296 break; 297 case AVRCP_MEDIA_ATTR_ALBUM: 298 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 299 event[pos++] = value_len; 300 memcpy(event+pos, value, value_len); 301 break; 302 case AVRCP_MEDIA_ATTR_GENRE: 303 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 304 event[pos++] = value_len; 305 memcpy(event+pos, value, value_len); 306 break; 307 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 308 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 309 if (value){ 310 little_endian_store_32(event, pos, btstack_atoi((char *)value)); 311 } else { 312 little_endian_store_32(event, pos, 0); 313 } 314 pos += 4; 315 break; 316 case AVRCP_MEDIA_ATTR_TRACK: 317 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 318 if (value){ 319 event[pos++] = btstack_atoi((char *)value); 320 } else { 321 event[pos++] = 0; 322 } 323 break; 324 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 325 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 326 if (value){ 327 event[pos++] = btstack_atoi((char *)value); 328 } else { 329 event[pos++] = 0; 330 } 331 break; 332 default: 333 break; 334 } 335 event[data_len_pos] = pos - 2; 336 // printf("Send attribute 0x%02x, len %u\n", attr_id, value_len); 337 // printf_hexdump(value, value_len); 338 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 339 } 340 341 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 342 uint16_t attribute_total_value_len; 343 uint32_t attribute_id; 344 // printf("avrcp_parser_process_byte: %02x, state %02x\n", byte, connection->parser_state); 345 switch(connection->parser_state){ 346 case AVRCP_PARSER_GET_ATTRIBUTE_HEADER: 347 connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 348 connection->list_offset++; 349 350 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return; 351 352 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 353 connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE); 354 if (connection->attribute_value_len > 0){ 355 // get ready for attribute value 356 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 357 return; 358 } 359 360 // emit empty attribute 361 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 362 avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len); 363 364 // done, see below 365 break; 366 367 case AVRCP_PARSER_GET_ATTRIBUTE_VALUE: 368 connection->attribute_value[connection->attribute_value_offset++] = byte; 369 connection->list_offset++; 370 371 if (connection->attribute_value_offset < connection->attribute_value_len) return; 372 373 // emit (potentially partial) attribute 374 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 375 avrcp_controller_emit_now_playing_info_event(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, (avrcp_media_attribute_id_t) attribute_id, connection->attribute_value, connection->attribute_value_len); 376 377 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 378 if (connection->attribute_value_offset < attribute_total_value_len){ 379 // ignore rest of attribute 380 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 381 return; 382 } 383 384 // done, see below 385 break; 386 387 case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 388 connection->attribute_value_offset++; 389 connection->list_offset++; 390 391 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 392 if (connection->attribute_value_offset < attribute_total_value_len) return; 393 394 // done, see below 395 break; 396 397 default: 398 return; 399 } 400 401 // attribute fully read, check if more to come 402 if (connection->list_offset < connection->list_size){ 403 // more to come, reset parser 404 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 405 connection->parser_attribute_header_pos = 0; 406 connection->attribute_value_offset = 0; 407 } else { 408 // fully done 409 avrcp_parser_reset(connection); 410 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 411 } 412 } 413 414 static void avrcp_source_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 415 int i; 416 for (i=0;i<num_bytes_to_read;i++){ 417 avrcp_parser_process_byte(packet[i], connection, ctype); 418 } 419 } 420 421 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 422 connection->state = AVCTP_W2_SEND_COMMAND; 423 connection->transaction_label++; 424 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 425 connection->command_type = AVRCP_CTYPE_CONTROL; 426 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 427 connection->subunit_id = AVRCP_SUBUNIT_ID; 428 int pos = 0; 429 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 430 pos += 3; 431 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID 432 connection->cmd_operands[pos++] = 0; 433 // Parameter Length 434 connection->cmd_operands_length = 8; 435 big_endian_store_16(connection->cmd_operands, pos, 1); 436 pos += 2; 437 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 438 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 439 return ERROR_CODE_SUCCESS; 440 } 441 442 443 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 444 connection->state = AVCTP_W2_SEND_COMMAND; 445 connection->transaction_label++; 446 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 447 connection->command_type = AVRCP_CTYPE_CONTROL; 448 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 449 connection->subunit_id = AVRCP_SUBUNIT_ID; 450 int pos = 0; 451 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 452 pos += 3; 453 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID 454 connection->cmd_operands[pos++] = 0; 455 // Parameter Length 456 connection->cmd_operands_length = 8; 457 big_endian_store_16(connection->cmd_operands, pos, 1); 458 pos += 2; 459 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 460 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 461 return ERROR_CODE_SUCCESS; 462 } 463 464 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 465 uint8_t operands[20]; 466 uint8_t opcode; 467 int pos = 3; 468 // uint8_t transport_header = packet[0]; 469 // uint8_t transaction_label = transport_header >> 4; 470 // uint8_t packet_type = (transport_header & 0x0F) >> 2; 471 // uint8_t frame_type = (transport_header & 0x03) >> 1; 472 // uint8_t ipid = transport_header & 0x01; 473 // uint8_t byte_value = packet[2]; 474 // uint16_t pid = (byte_value << 8) | packet[2]; 475 476 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 477 uint8_t byte_value = packet[pos++]; 478 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 479 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 480 opcode = packet[pos++]; 481 482 // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 483 // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 484 // // printf_hexdump(packet+pos, size-pos); 485 486 uint8_t pdu_id; 487 uint16_t param_length; 488 switch (avrcp_cmd_opcode(packet,size)){ 489 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 490 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 491 connection->state = AVCTP_CONNECTION_OPENED; 492 // operands: 493 memcpy(operands, packet+pos, 5); 494 uint8_t unit_type = operands[1] >> 3; 495 uint8_t max_subunit_ID = operands[1] & 0x07; 496 log_info(" SUBUNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, max_subunit_ID %d", ctype, subunit_type, subunit_id, opcode, unit_type, max_subunit_ID); 497 break; 498 } 499 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 500 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 501 connection->state = AVCTP_CONNECTION_OPENED; 502 // operands: 503 memcpy(operands, packet+pos, 5); 504 uint8_t unit_type = operands[1] >> 3; 505 uint8_t unit = operands[1] & 0x07; 506 uint32_t company_id = operands[2] << 16 | operands[3] << 8 | operands[4]; 507 log_info(" UNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06" PRIx32, 508 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 509 break; 510 } 511 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 512 if (size - pos < 7) { 513 log_error("avrcp: wrong packet size"); 514 return; 515 }; 516 // operands: 517 memcpy(operands, packet+pos, 7); 518 pos += 7; 519 // uint32_t company_id = operands[0] << 16 | operands[1] << 8 | operands[2]; 520 pdu_id = operands[3]; 521 522 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE && pdu_id != AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 523 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 524 return; 525 } 526 connection->state = AVCTP_CONNECTION_OPENED; 527 528 529 // uint8_t unit_type = operands[4] >> 3; 530 // uint8_t unit = operands[4] & 0x07; 531 param_length = big_endian_read_16(operands, 5); 532 533 // printf(" VENDOR DEPENDENT response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), unit_type 0x%02x, unit %d, company_id 0x%06x\n", 534 // ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id ); 535 536 // if (ctype == AVRCP_CTYPE_RESPONSE_INTERIM) return; 537 log_info(" VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 538 switch (pdu_id){ 539 case AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue:{ 540 uint8_t num_attributes = packet[pos++]; 541 int i; 542 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 543 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 544 for (i = 0; i < num_attributes; i++){ 545 uint8_t attribute_id = packet[pos++]; 546 uint8_t value = packet[pos++]; 547 switch (attribute_id){ 548 case 0x02: 549 repeat_mode = (avrcp_repeat_mode_t) value; 550 break; 551 case 0x03: 552 shuffle_mode = (avrcp_shuffle_mode_t) value; 553 break; 554 default: 555 break; 556 } 557 } 558 avrcp_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 559 break; 560 } 561 case AVRCP_PDU_ID_SetPlayerApplicationSettingValue:{ 562 uint8_t event[6]; 563 int offset = 0; 564 event[offset++] = HCI_EVENT_AVRCP_META; 565 event[offset++] = sizeof(event) - 2; 566 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 567 little_endian_store_16(event, offset, connection->avrcp_cid); 568 offset += 2; 569 event[offset++] = ctype; 570 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 571 break; 572 } 573 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 574 uint8_t event[7]; 575 int offset = 0; 576 event[offset++] = HCI_EVENT_AVRCP_META; 577 event[offset++] = sizeof(event) - 2; 578 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 579 little_endian_store_16(event, offset, connection->avrcp_cid); 580 offset += 2; 581 event[offset++] = ctype; 582 event[offset++] = packet[pos++]; 583 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 584 break; 585 } 586 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 587 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 588 uint8_t capability_count = packet[pos++]; 589 int i; 590 switch (capability_id){ 591 case AVRCP_CAPABILITY_ID_COMPANY: 592 // log_info("Supported companies %d: ", capability_count); 593 for (i = 0; i < capability_count; i++){ 594 uint32_t company_id = big_endian_read_24(packet, pos); 595 pos += 3; 596 log_info(" 0x%06" PRIx32 ", ", company_id); 597 } 598 break; 599 case AVRCP_CAPABILITY_ID_EVENT: 600 // log_info("Supported events %d: ", capability_count); 601 for (i = 0; i < capability_count; i++){ 602 uint8_t event_id = packet[pos++]; 603 log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 604 } 605 break; 606 } 607 break; 608 } 609 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 610 uint32_t song_length = big_endian_read_32(packet, pos); 611 pos += 4; 612 uint32_t song_position = big_endian_read_32(packet, pos); 613 pos += 4; 614 uint8_t play_status = packet[pos]; 615 // log_info(" GET_PLAY_STATUS length 0x%04X, position 0x%04X, status %s", song_length, song_position, avrcp_play_status2str(play_status)); 616 617 uint8_t event[15]; 618 int offset = 0; 619 event[offset++] = HCI_EVENT_AVRCP_META; 620 event[offset++] = sizeof(event) - 2; 621 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 622 little_endian_store_16(event, offset, connection->avrcp_cid); 623 offset += 2; 624 event[offset++] = ctype; 625 little_endian_store_32(event, offset, song_length); 626 offset += 4; 627 little_endian_store_32(event, offset, song_position); 628 offset += 4; 629 event[offset++] = play_status; 630 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 631 break; 632 } 633 case AVRCP_PDU_ID_REGISTER_NOTIFICATION:{ 634 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) packet[pos++]; 635 uint16_t event_mask = (1 << event_id); 636 uint16_t reset_event_mask = ~event_mask; 637 switch (ctype){ 638 case AVRCP_CTYPE_RESPONSE_INTERIM: 639 // register as enabled 640 connection->notifications_enabled |= event_mask; 641 // printf("INTERIM notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 642 break; 643 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 644 // received change, event is considered deregistered 645 // we are re-enabling it automatically, if it is not 646 // explicitly disabled 647 connection->notifications_enabled &= reset_event_mask; 648 if (! (connection->notifications_to_deregister & event_mask)){ 649 avrcp_register_notification(connection, event_id); 650 // printf("CHANGED_STABLE notifications_enabled 0x%2x, notifications_to_register 0x%2x\n", connection->notifications_enabled, connection->notifications_to_register); 651 } else { 652 connection->notifications_to_deregister &= reset_event_mask; 653 } 654 break; 655 default: 656 connection->notifications_to_register &= reset_event_mask; 657 connection->notifications_enabled &= reset_event_mask; 658 connection->notifications_to_deregister &= reset_event_mask; 659 break; 660 } 661 662 switch (event_id){ 663 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{ 664 uint32_t song_position = big_endian_read_32(packet, pos); 665 uint8_t event[10]; 666 int offset = 0; 667 event[offset++] = HCI_EVENT_AVRCP_META; 668 event[offset++] = sizeof(event) - 2; 669 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED; 670 little_endian_store_16(event, offset, connection->avrcp_cid); 671 offset += 2; 672 event[offset++] = ctype; 673 little_endian_store_32(event, offset, song_position); 674 offset += 4; 675 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 676 break; 677 } 678 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 679 uint8_t event[7]; 680 int offset = 0; 681 event[offset++] = HCI_EVENT_AVRCP_META; 682 event[offset++] = sizeof(event) - 2; 683 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 684 little_endian_store_16(event, offset, connection->avrcp_cid); 685 offset += 2; 686 event[offset++] = ctype; 687 event[offset++] = packet[pos]; 688 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 689 break; 690 } 691 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 692 uint8_t event[6]; 693 int offset = 0; 694 event[offset++] = HCI_EVENT_AVRCP_META; 695 event[offset++] = sizeof(event) - 2; 696 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 697 little_endian_store_16(event, offset, connection->avrcp_cid); 698 offset += 2; 699 event[offset++] = ctype; 700 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 701 break; 702 } 703 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 704 uint8_t event[6]; 705 int offset = 0; 706 event[offset++] = HCI_EVENT_AVRCP_META; 707 event[offset++] = sizeof(event) - 2; 708 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 709 little_endian_store_16(event, offset, connection->avrcp_cid); 710 offset += 2; 711 event[offset++] = ctype; 712 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 713 break; 714 } 715 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 716 uint8_t event[6]; 717 int offset = 0; 718 event[offset++] = HCI_EVENT_AVRCP_META; 719 event[offset++] = sizeof(event) - 2; 720 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 721 little_endian_store_16(event, offset, connection->avrcp_cid); 722 offset += 2; 723 event[offset++] = ctype; 724 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 725 break; 726 } 727 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 728 uint8_t event[7]; 729 int offset = 0; 730 event[offset++] = HCI_EVENT_AVRCP_META; 731 event[offset++] = sizeof(event) - 2; 732 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 733 little_endian_store_16(event, offset, connection->avrcp_cid); 734 offset += 2; 735 event[offset++] = ctype; 736 event[offset++] = packet[pos++] & 0x7F; 737 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 738 break; 739 } 740 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 741 uint8_t event[7]; 742 int offset = 0; 743 event[offset++] = HCI_EVENT_AVRCP_META; 744 event[offset++] = sizeof(event) - 2; 745 event[offset++] = AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED; 746 little_endian_store_16(event, offset, connection->avrcp_cid); 747 offset += 2; 748 event[offset++] = ctype; 749 event[offset++] = packet[pos++] & 0x7F; 750 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 751 break; 752 } 753 754 // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 755 // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 756 // int i; 757 // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 758 // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 759 // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 760 // } 761 // break; 762 // } 763 // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 764 // uint16_t player_id = big_endian_read_16(packet, pos); 765 // pos += 2; 766 // uint16_t uid_counter = big_endian_read_16(packet, pos); 767 // pos += 2; 768 // break; 769 // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 770 // uint16_t uid_counter = big_endian_read_16(packet, pos); 771 // pos += 2; 772 // break; 773 default: 774 log_info("avrcp: not implemented"); 775 break; 776 } 777 if (connection->notifications_to_register != 0){ 778 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 779 } 780 break; 781 } 782 783 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 784 avrcp_packet_type_t packet_type = (avrcp_packet_type_t) (operands[4] & 0x03); 785 switch (packet_type){ 786 case AVRCP_START_PACKET: 787 case AVRCP_SINGLE_PACKET: 788 avrcp_parser_reset(connection); 789 connection->list_size = param_length; 790 connection->num_attributes = packet[pos++]; 791 792 // printf("AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES num_attributes %d, total size %d, packet type 0x%02x \n", connection->num_attributes, connection->list_size, operands[4] & 0x03); 793 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 794 795 if (packet_type == AVRCP_START_PACKET){ 796 avrcp_controller_request_continue_response(connection); 797 } 798 break; 799 case AVRCP_CONTINUE_PACKET: 800 case AVRCP_END_PACKET: 801 connection->num_received_fragments++; 802 if (connection->num_received_fragments < connection->max_num_fragments){ 803 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 804 if (packet_type == AVRCP_CONTINUE_PACKET){ 805 avrcp_controller_request_continue_response(connection); 806 } 807 } else { 808 avrcp_controller_request_abort_continuation(connection); 809 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 810 avrcp_parser_reset(connection); 811 } 812 break; 813 } 814 } 815 default: 816 break; 817 } 818 break; 819 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 820 // 0x80 | connection->cmd_operands[0] 821 uint8_t operation_id = packet[pos++]; 822 switch (connection->state){ 823 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 824 if (connection->continuous_fast_forward_cmd){ 825 connection->state = AVCTP_W4_STOP; 826 } else { 827 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 828 } 829 break; 830 case AVCTP_W2_RECEIVE_RESPONSE: 831 connection->state = AVCTP_CONNECTION_OPENED; 832 break; 833 default: 834 // check for notifications? move state transition down 835 // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 836 break; 837 } 838 if (connection->state == AVCTP_W4_STOP){ 839 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 840 } 841 if (connection->state == AVCTP_CONNECTION_OPENED) { 842 // RELEASE response 843 operation_id = operation_id & 0x7F; 844 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 845 } 846 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 847 // PRESS response 848 request_pass_through_release_control_cmd(connection); 849 } 850 break; 851 } 852 default: 853 break; 854 } 855 856 // trigger pending notification reqistrations 857 if (connection->state == AVCTP_CONNECTION_OPENED && connection->notifications_to_register){ 858 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 859 } 860 } 861 862 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 863 int i; 864 switch (connection->state){ 865 case AVCTP_W2_SEND_PRESS_COMMAND: 866 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 867 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 868 break; 869 case AVCTP_W2_SEND_COMMAND: 870 case AVCTP_W2_SEND_RELEASE_COMMAND: 871 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 872 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 873 break; 874 case AVCTP_CONNECTION_OPENED: 875 if (connection->notifications_to_register != 0){ 876 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 877 if (connection->notifications_to_register & (1<<i)){ 878 connection->notifications_to_register &= ~ (1 << i); 879 avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 880 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 881 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 882 return; 883 } 884 } 885 } 886 return; 887 case AVCTP_W2_SEND_FRAGMENTED_COMMAND: 888 if (connection->cmd_operands_fragmented_pos == 0){ 889 avrcp_send_cmd(connection, AVRCP_START_PACKET); 890 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 891 } else { 892 if (connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){ 893 avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET); 894 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 895 } else { 896 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 897 avrcp_send_cmd(connection, AVRCP_END_PACKET); 898 } 899 } 900 default: 901 return; 902 } 903 } 904 905 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 906 avrcp_connection_t * connection; 907 908 switch (packet_type) { 909 case L2CAP_DATA_PACKET: 910 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel); 911 if (!connection) break; 912 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 913 break; 914 case HCI_EVENT_PACKET: 915 switch (hci_event_packet_get_type(packet)){ 916 case L2CAP_EVENT_CAN_SEND_NOW: 917 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel); 918 if (!connection) break; 919 avrcp_controller_handle_can_send_now(connection); 920 break; 921 default: 922 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 923 break; 924 } 925 default: 926 break; 927 } 928 } 929 930 void avrcp_controller_init(void){ 931 avrcp_init(); 932 avrcp_controller_context.role = AVRCP_CONTROLLER; 933 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 934 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_2); 935 } 936 937 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 938 if (callback == NULL){ 939 log_error("avrcp_register_packet_handler called with NULL callback"); 940 return; 941 } 942 avrcp_controller_context.avrcp_callback = callback; 943 } 944 945 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 946 return avrcp_connect(AVRCP_CONTROLLER, bd_addr, &avrcp_controller_context, avrcp_cid); 947 } 948 949 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 950 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 951 if (!connection){ 952 log_error("avrcp_unit_info: could not find a connection."); 953 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 954 } 955 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 956 connection->state = AVCTP_W2_SEND_COMMAND; 957 958 connection->transaction_label++; 959 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 960 connection->command_type = AVRCP_CTYPE_STATUS; 961 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 962 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 963 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 964 connection->cmd_operands_length = 5; 965 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 966 return ERROR_CODE_SUCCESS; 967 } 968 969 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 970 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 971 if (!connection){ 972 log_error("avrcp_unit_info: could not find a connection."); 973 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 974 } 975 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 976 connection->state = AVCTP_W2_SEND_COMMAND; 977 978 connection->transaction_label++; 979 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 980 connection->command_type = AVRCP_CTYPE_STATUS; 981 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 982 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 983 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 984 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 985 connection->cmd_operands_length = 5; 986 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 987 return ERROR_CODE_SUCCESS; 988 } 989 990 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 991 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 992 if (!connection){ 993 log_error("avrcp_get_capabilities: could not find a connection."); 994 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 995 } 996 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 997 connection->state = AVCTP_W2_SEND_COMMAND; 998 999 connection->transaction_label++; 1000 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1001 connection->command_type = AVRCP_CTYPE_STATUS; 1002 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1003 connection->subunit_id = AVRCP_SUBUNIT_ID; 1004 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1005 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 1006 connection->cmd_operands[4] = 0; 1007 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 1008 connection->cmd_operands[7] = capability_id; // capability ID 1009 connection->cmd_operands_length = 8; 1010 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1011 return ERROR_CODE_SUCCESS; 1012 } 1013 1014 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1015 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 1016 } 1017 1018 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1019 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 1020 } 1021 1022 1023 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1024 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1025 } 1026 1027 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1028 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1029 } 1030 1031 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1032 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1033 } 1034 1035 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1036 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1037 } 1038 1039 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1040 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1041 } 1042 1043 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1044 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1045 } 1046 1047 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1048 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1049 } 1050 1051 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1052 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1053 } 1054 1055 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1056 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1057 } 1058 1059 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1060 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1061 } 1062 1063 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1064 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1065 } 1066 1067 1068 /* start cmds */ 1069 1070 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1071 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1072 if (!connection){ 1073 log_error("avrcp_stop_play: could not find a connection."); 1074 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1075 } 1076 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1077 return request_pass_through_release_control_cmd(connection); 1078 } 1079 1080 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1081 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1082 } 1083 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1084 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1085 } 1086 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1087 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1088 } 1089 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1090 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1091 } 1092 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1093 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1094 } 1095 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1096 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1097 } 1098 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1099 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1100 } 1101 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1102 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1103 } 1104 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1105 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1106 } 1107 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1108 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1109 } 1110 1111 1112 /* stop continuous cmds */ 1113 1114 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1115 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1116 if (!connection){ 1117 log_error("avrcp_get_play_status: could not find a connection."); 1118 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1119 } 1120 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1121 connection->state = AVCTP_W2_SEND_COMMAND; 1122 connection->transaction_label++; 1123 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1124 connection->command_type = AVRCP_CTYPE_STATUS; 1125 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1126 connection->subunit_id = AVRCP_SUBUNIT_ID; 1127 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1128 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1129 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1130 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1131 connection->cmd_operands_length = 7; 1132 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1133 return ERROR_CODE_SUCCESS; 1134 } 1135 1136 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1137 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1138 if (!connection){ 1139 log_error("avrcp_get_play_status: could not find a connection."); 1140 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1141 } 1142 avrcp_register_notification(connection, event_id); 1143 return ERROR_CODE_SUCCESS; 1144 } 1145 1146 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1147 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1148 if (!connection){ 1149 log_error("avrcp_get_play_status: could not find a connection."); 1150 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1151 } 1152 connection->notifications_to_deregister |= (1 << event_id); 1153 return ERROR_CODE_SUCCESS; 1154 } 1155 1156 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1157 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1158 if (!connection){ 1159 log_error("avrcp_get_capabilities: could not find a connection."); 1160 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1161 } 1162 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1163 connection->state = AVCTP_W2_SEND_COMMAND; 1164 1165 connection->transaction_label++; 1166 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1167 connection->command_type = AVRCP_CTYPE_CONTROL; 1168 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1169 connection->subunit_id = AVRCP_SUBUNIT_ID; 1170 int pos = 0; 1171 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1172 pos += 3; 1173 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1174 connection->cmd_operands[pos++] = 0; 1175 1176 // Parameter Length 1177 big_endian_store_16(connection->cmd_operands, pos, 2); 1178 pos += 2; 1179 1180 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1181 pos += 2; 1182 1183 connection->cmd_operands_length = pos; 1184 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1185 return ERROR_CODE_SUCCESS; 1186 } 1187 1188 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1189 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1190 if (!connection){ 1191 log_error("avrcp_get_capabilities: could not find a connection."); 1192 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1193 } 1194 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1195 connection->state = AVCTP_W2_SEND_COMMAND; 1196 1197 connection->transaction_label++; 1198 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1199 connection->command_type = AVRCP_CTYPE_STATUS; 1200 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1201 connection->subunit_id = AVRCP_SUBUNIT_ID; 1202 int pos = 0; 1203 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1204 pos += 3; 1205 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1206 connection->cmd_operands[pos++] = 0; 1207 1208 // Parameter Length 1209 big_endian_store_16(connection->cmd_operands, pos, 9); 1210 pos += 2; 1211 1212 // write 8 bytes value 1213 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1214 pos += 8; 1215 1216 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 1217 // every attribute is 4 bytes long 1218 1219 connection->cmd_operands_length = pos; 1220 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1221 return ERROR_CODE_SUCCESS; 1222 } 1223 1224 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1225 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1226 if (!connection){ 1227 log_error("avrcp_get_capabilities: could not find a connection."); 1228 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1229 } 1230 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1231 connection->state = AVCTP_W2_SEND_COMMAND; 1232 1233 connection->transaction_label++; 1234 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1235 connection->command_type = AVRCP_CTYPE_CONTROL; 1236 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1237 connection->subunit_id = AVRCP_SUBUNIT_ID; 1238 int pos = 0; 1239 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1240 pos += 3; 1241 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1242 connection->cmd_operands[pos++] = 0; 1243 1244 // Parameter Length 1245 big_endian_store_16(connection->cmd_operands, pos, 1); 1246 pos += 2; 1247 connection->cmd_operands[pos++] = volume; 1248 1249 connection->cmd_operands_length = pos; 1250 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1251 return ERROR_CODE_SUCCESS; 1252 } 1253 1254 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1255 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1256 if (!connection){ 1257 log_error("avrcp_get_capabilities: could not find a connection."); 1258 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1259 } 1260 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1261 connection->state = AVCTP_W2_SEND_COMMAND; 1262 1263 connection->transaction_label++; 1264 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1265 connection->command_type = AVRCP_CTYPE_STATUS; 1266 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1267 connection->subunit_id = AVRCP_SUBUNIT_ID; 1268 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1269 connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 1270 connection->cmd_operands[4] = 0; 1271 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1272 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1273 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1274 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1275 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1276 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1277 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1278 connection->cmd_operands_length = 12; 1279 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1280 return ERROR_CODE_SUCCESS; 1281 } 1282 1283 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1284 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1285 if (!connection){ 1286 log_error("avrcp_get_capabilities: could not find a connection."); 1287 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1288 } 1289 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1290 connection->state = AVCTP_W2_SEND_COMMAND; 1291 1292 connection->transaction_label++; 1293 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1294 connection->command_type = AVRCP_CTYPE_CONTROL; 1295 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1296 connection->subunit_id = AVRCP_SUBUNIT_ID; 1297 int pos = 0; 1298 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1299 pos += 3; 1300 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 1301 connection->cmd_operands[pos++] = 0; 1302 // Parameter Length 1303 big_endian_store_16(connection->cmd_operands, pos, 3); 1304 pos += 2; 1305 connection->cmd_operands[pos++] = 2; 1306 connection->cmd_operands_length = pos; 1307 connection->cmd_operands[pos++] = attr_id; 1308 connection->cmd_operands[pos++] = attr_value; 1309 connection->cmd_operands_length = pos; 1310 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1311 return ERROR_CODE_SUCCESS; 1312 } 1313 1314 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1315 if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1316 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1317 } 1318 1319 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1320 if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1321 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1322 } 1323 1324 uint8_t avrcp_controller_disconnect(uint16_t avrcp_cid){ 1325 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1326 if (!connection){ 1327 log_error("avrcp_get_capabilities: could not find a connection."); 1328 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1329 } 1330 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1331 if (connection->browsing_connection){ 1332 if (connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1333 l2cap_disconnect(connection->browsing_connection->l2cap_browsing_cid, 0); 1334 } 1335 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1336 return ERROR_CODE_SUCCESS; 1337 } 1338 1339 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1340 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1341 if (!connection){ 1342 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1343 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1344 } 1345 if (connection->state != AVCTP_CONNECTION_OPENED){ 1346 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1347 return ERROR_CODE_COMMAND_DISALLOWED; 1348 } 1349 connection->state = AVCTP_W2_SEND_COMMAND; 1350 1351 connection->transaction_label++; 1352 connection->command_type = AVRCP_CTYPE_CONTROL; 1353 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1354 connection->subunit_id = AVRCP_SUBUNIT_ID; 1355 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1356 int pos = 0; 1357 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1358 pos += 3; 1359 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1360 // reserved 1361 connection->cmd_operands[pos++] = 0; 1362 // Parameter Length 1363 big_endian_store_16(connection->cmd_operands, pos, 11); 1364 pos += 2; 1365 connection->cmd_operands[pos++] = scope; 1366 memset(&connection->cmd_operands[pos], 0, 8); 1367 if (uid){ 1368 memcpy(&connection->cmd_operands[pos], uid, 8); 1369 } 1370 pos += 8; 1371 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1372 pos += 2; 1373 connection->cmd_operands_length = pos; 1374 1375 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1376 return ERROR_CODE_SUCCESS; 1377 } 1378 1379 uint8_t avrcp_controller_add_item_from_scope_to_now_playing_list(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1380 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1381 if (!connection){ 1382 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1383 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1384 } 1385 if (connection->state != AVCTP_CONNECTION_OPENED){ 1386 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1387 return ERROR_CODE_COMMAND_DISALLOWED; 1388 } 1389 connection->state = AVCTP_W2_SEND_COMMAND; 1390 1391 connection->transaction_label++; 1392 connection->command_type = AVRCP_CTYPE_CONTROL; 1393 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1394 connection->subunit_id = AVRCP_SUBUNIT_ID; 1395 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1396 int pos = 0; 1397 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1398 pos += 3; 1399 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1400 // reserved 1401 connection->cmd_operands[pos++] = 0; 1402 // Parameter Length 1403 big_endian_store_16(connection->cmd_operands, pos, 11); 1404 pos += 2; 1405 connection->cmd_operands[pos++] = scope; 1406 memset(&connection->cmd_operands[pos], 0, 8); 1407 if (uid){ 1408 memcpy(&connection->cmd_operands[pos], uid, 8); 1409 } 1410 pos += 8; 1411 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1412 pos += 2; 1413 connection->cmd_operands_length = pos; 1414 1415 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1416 return ERROR_CODE_SUCCESS; 1417 } 1418 1419 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1420 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1421 if (!connection){ 1422 log_error("avrcp_controller_play_item: could not find a connection."); 1423 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1424 } 1425 connection->max_num_fragments = max_num_fragments; 1426 return ERROR_CODE_SUCCESS; 1427 } 1428 1429 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, avrcp_command_type_t command_type, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t command_opcode, const uint8_t * command_buffer, uint16_t command_len){ 1430 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid); 1431 if (!connection){ 1432 log_error("avrcp_controller_play_item: could not find a connection."); 1433 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1434 } 1435 1436 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1437 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1438 1439 connection->transaction_label++; 1440 connection->command_opcode = command_opcode; 1441 connection->command_type = command_type; 1442 connection->subunit_type = subunit_type; 1443 connection->subunit_id = subunit_id; 1444 connection->cmd_operands_fragmented_buffer = command_buffer; 1445 connection->cmd_operands_fragmented_pos = 0; 1446 connection->cmd_operands_fragmented_len = command_len; 1447 1448 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1449 return ERROR_CODE_SUCCESS; 1450 } 1451