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