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 = (avrcp_connection_t*) 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, 1); // send notification on playback position every second, for other notifications it is ignored 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, (avrcp_media_attribute_id_t) 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, (avrcp_media_attribute_id_t) 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_POS_CHANGED:{ 660 uint32_t song_position = big_endian_read_32(packet, pos); 661 uint8_t event[10]; 662 int offset = 0; 663 event[offset++] = HCI_EVENT_AVRCP_META; 664 event[offset++] = sizeof(event) - 2; 665 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED; 666 little_endian_store_16(event, offset, connection->avrcp_cid); 667 offset += 2; 668 event[offset++] = ctype; 669 little_endian_store_32(event, offset, song_position); 670 offset += 4; 671 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 672 break; 673 } 674 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 675 uint8_t event[7]; 676 int offset = 0; 677 event[offset++] = HCI_EVENT_AVRCP_META; 678 event[offset++] = sizeof(event) - 2; 679 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 680 little_endian_store_16(event, offset, connection->avrcp_cid); 681 offset += 2; 682 event[offset++] = ctype; 683 event[offset++] = packet[pos]; 684 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 685 break; 686 } 687 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 688 uint8_t event[6]; 689 int offset = 0; 690 event[offset++] = HCI_EVENT_AVRCP_META; 691 event[offset++] = sizeof(event) - 2; 692 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 693 little_endian_store_16(event, offset, connection->avrcp_cid); 694 offset += 2; 695 event[offset++] = ctype; 696 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 697 break; 698 } 699 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 700 uint8_t event[6]; 701 int offset = 0; 702 event[offset++] = HCI_EVENT_AVRCP_META; 703 event[offset++] = sizeof(event) - 2; 704 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 705 little_endian_store_16(event, offset, connection->avrcp_cid); 706 offset += 2; 707 event[offset++] = ctype; 708 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 709 break; 710 } 711 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 712 uint8_t event[6]; 713 int offset = 0; 714 event[offset++] = HCI_EVENT_AVRCP_META; 715 event[offset++] = sizeof(event) - 2; 716 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 717 little_endian_store_16(event, offset, connection->avrcp_cid); 718 offset += 2; 719 event[offset++] = ctype; 720 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 721 break; 722 } 723 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 724 uint8_t event[7]; 725 int offset = 0; 726 event[offset++] = HCI_EVENT_AVRCP_META; 727 event[offset++] = sizeof(event) - 2; 728 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 729 little_endian_store_16(event, offset, connection->avrcp_cid); 730 offset += 2; 731 event[offset++] = ctype; 732 event[offset++] = packet[pos++] & 0x7F; 733 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 734 break; 735 } 736 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 737 uint8_t event[7]; 738 int offset = 0; 739 event[offset++] = HCI_EVENT_AVRCP_META; 740 event[offset++] = sizeof(event) - 2; 741 event[offset++] = AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED; 742 little_endian_store_16(event, offset, connection->avrcp_cid); 743 offset += 2; 744 event[offset++] = ctype; 745 event[offset++] = packet[pos++] & 0x7F; 746 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 747 break; 748 } 749 750 // case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED:{ 751 // uint8_t num_PlayerApplicationSettingAttributes = packet[pos++]; 752 // int i; 753 // for (i = 0; i < num_PlayerApplicationSettingAttributes; i++){ 754 // uint8_t PlayerApplicationSetting_AttributeID = packet[pos++]; 755 // uint8_t PlayerApplicationSettingValueID = packet[pos++]; 756 // } 757 // break; 758 // } 759 // case AVRCP_NOTIFICATION_EVENT_ADDRESSED_PLAYER_CHANGED: 760 // uint16_t player_id = big_endian_read_16(packet, pos); 761 // pos += 2; 762 // uint16_t uid_counter = big_endian_read_16(packet, pos); 763 // pos += 2; 764 // break; 765 // case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED: 766 // uint16_t uid_counter = big_endian_read_16(packet, pos); 767 // pos += 2; 768 // break; 769 default: 770 log_info("avrcp: not implemented"); 771 break; 772 } 773 if (connection->notifications_to_register != 0){ 774 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 775 } 776 break; 777 } 778 779 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 780 avrcp_packet_type_t packet_type = (avrcp_packet_type_t) (operands[4] & 0x03); 781 switch (packet_type){ 782 case AVRCP_START_PACKET: 783 case AVRCP_SINGLE_PACKET: 784 avrcp_parser_reset(connection); 785 connection->list_size = param_length; 786 connection->num_attributes = packet[pos++]; 787 788 // 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); 789 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 790 791 if (packet_type == AVRCP_START_PACKET){ 792 avrcp_controller_request_continue_response(connection); 793 } 794 break; 795 case AVRCP_CONTINUE_PACKET: 796 case AVRCP_END_PACKET: 797 connection->num_received_fragments++; 798 if (connection->num_received_fragments < connection->max_num_fragments){ 799 avrcp_source_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 800 if (packet_type == AVRCP_CONTINUE_PACKET){ 801 avrcp_controller_request_continue_response(connection); 802 } 803 } else { 804 avrcp_controller_request_abort_continuation(connection); 805 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 806 avrcp_parser_reset(connection); 807 } 808 break; 809 } 810 } 811 default: 812 break; 813 } 814 break; 815 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 816 // 0x80 | connection->cmd_operands[0] 817 uint8_t operation_id = packet[pos++]; 818 switch (connection->state){ 819 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 820 if (connection->continuous_fast_forward_cmd){ 821 connection->state = AVCTP_W4_STOP; 822 } else { 823 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 824 } 825 break; 826 case AVCTP_W2_RECEIVE_RESPONSE: 827 connection->state = AVCTP_CONNECTION_OPENED; 828 break; 829 default: 830 // check for notifications? move state transition down 831 // log_info("AVRCP_CMD_OPCODE_PASS_THROUGH state %d\n", connection->state); 832 break; 833 } 834 if (connection->state == AVCTP_W4_STOP){ 835 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 836 } 837 if (connection->state == AVCTP_CONNECTION_OPENED) { 838 // RELEASE response 839 operation_id = operation_id & 0x7F; 840 avrcp_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 841 } 842 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 843 // PRESS response 844 request_pass_through_release_control_cmd(connection); 845 } 846 break; 847 } 848 default: 849 break; 850 } 851 } 852 853 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 854 int i; 855 switch (connection->state){ 856 case AVCTP_W2_SEND_PRESS_COMMAND: 857 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 858 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 859 break; 860 case AVCTP_W2_SEND_COMMAND: 861 case AVCTP_W2_SEND_RELEASE_COMMAND: 862 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 863 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 864 break; 865 case AVCTP_CONNECTION_OPENED: 866 if (connection->notifications_to_register != 0){ 867 for (i = 1; i <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; i++){ 868 if (connection->notifications_to_register & (1<<i)){ 869 connection->notifications_to_register &= ~ (1 << i); 870 avrcp_prepare_notification(connection, (avrcp_notification_event_id_t) i); 871 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 872 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 873 return; 874 } 875 } 876 } 877 return; 878 case AVCTP_W2_SEND_FRAGMENTED_COMMAND: 879 if (connection->cmd_operands_fragmented_pos == 0){ 880 avrcp_send_cmd(connection, AVRCP_START_PACKET); 881 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 882 } else { 883 if (connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){ 884 avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET); 885 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 886 } else { 887 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 888 avrcp_send_cmd(connection, AVRCP_END_PACKET); 889 } 890 } 891 default: 892 return; 893 } 894 } 895 896 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 897 avrcp_connection_t * connection; 898 899 switch (packet_type) { 900 case L2CAP_DATA_PACKET: 901 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 902 if (!connection) break; 903 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 904 break; 905 case HCI_EVENT_PACKET: 906 switch (hci_event_packet_get_type(packet)){ 907 case L2CAP_EVENT_CAN_SEND_NOW: 908 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_controller_context); 909 if (!connection) break; 910 avrcp_controller_handle_can_send_now(connection); 911 break; 912 default: 913 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 914 break; 915 } 916 default: 917 break; 918 } 919 } 920 921 void avrcp_controller_init(void){ 922 avrcp_controller_context.role = AVRCP_CONTROLLER; 923 avrcp_controller_context.connections = NULL; 924 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 925 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_2); 926 } 927 928 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 929 if (callback == NULL){ 930 log_error("avrcp_register_packet_handler called with NULL callback"); 931 return; 932 } 933 avrcp_controller_context.avrcp_callback = callback; 934 } 935 936 uint8_t avrcp_controller_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 937 return avrcp_connect(bd_addr, &avrcp_controller_context, avrcp_cid); 938 } 939 940 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 941 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 942 if (!connection){ 943 log_error("avrcp_unit_info: could not find a connection."); 944 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 945 } 946 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 947 connection->state = AVCTP_W2_SEND_COMMAND; 948 949 connection->transaction_label++; 950 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 951 connection->command_type = AVRCP_CTYPE_STATUS; 952 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 953 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 954 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 955 connection->cmd_operands_length = 5; 956 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 957 return ERROR_CODE_SUCCESS; 958 } 959 960 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 961 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 962 if (!connection){ 963 log_error("avrcp_unit_info: could not find a connection."); 964 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 965 } 966 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 967 connection->state = AVCTP_W2_SEND_COMMAND; 968 969 connection->transaction_label++; 970 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 971 connection->command_type = AVRCP_CTYPE_STATUS; 972 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 973 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 974 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 975 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 976 connection->cmd_operands_length = 5; 977 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 978 return ERROR_CODE_SUCCESS; 979 } 980 981 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 982 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 983 if (!connection){ 984 log_error("avrcp_get_capabilities: could not find a connection."); 985 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 986 } 987 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 988 connection->state = AVCTP_W2_SEND_COMMAND; 989 990 connection->transaction_label++; 991 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 992 connection->command_type = AVRCP_CTYPE_STATUS; 993 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 994 connection->subunit_id = AVRCP_SUBUNIT_ID; 995 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 996 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 997 connection->cmd_operands[4] = 0; 998 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 999 connection->cmd_operands[7] = capability_id; // capability ID 1000 connection->cmd_operands_length = 8; 1001 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1002 return ERROR_CODE_SUCCESS; 1003 } 1004 1005 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1006 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 1007 } 1008 1009 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1010 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 1011 } 1012 1013 1014 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1015 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1016 } 1017 1018 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1019 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1020 } 1021 1022 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1023 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1024 } 1025 1026 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1027 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1028 } 1029 1030 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1031 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1032 } 1033 1034 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1035 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1036 } 1037 1038 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1039 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1040 } 1041 1042 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1043 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1044 } 1045 1046 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1047 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1048 } 1049 1050 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1051 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1052 } 1053 1054 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1055 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1056 } 1057 1058 1059 /* start cmds */ 1060 1061 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1062 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1063 if (!connection){ 1064 log_error("avrcp_stop_play: could not find a connection."); 1065 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1066 } 1067 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1068 return request_pass_through_release_control_cmd(connection); 1069 } 1070 1071 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1072 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1073 } 1074 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1075 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1076 } 1077 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1078 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1079 } 1080 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1081 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1082 } 1083 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1084 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1085 } 1086 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1087 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1088 } 1089 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1090 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1091 } 1092 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1093 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1094 } 1095 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1096 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1097 } 1098 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1099 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1100 } 1101 1102 1103 /* stop continuous cmds */ 1104 1105 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1106 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1107 if (!connection){ 1108 log_error("avrcp_get_play_status: could not find a connection."); 1109 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1110 } 1111 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1112 connection->state = AVCTP_W2_SEND_COMMAND; 1113 connection->transaction_label++; 1114 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1115 connection->command_type = AVRCP_CTYPE_STATUS; 1116 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1117 connection->subunit_id = AVRCP_SUBUNIT_ID; 1118 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1119 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1120 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1121 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1122 connection->cmd_operands_length = 7; 1123 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1124 return ERROR_CODE_SUCCESS; 1125 } 1126 1127 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1128 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1129 if (!connection){ 1130 log_error("avrcp_get_play_status: could not find a connection."); 1131 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1132 } 1133 avrcp_register_notification(connection, event_id); 1134 return ERROR_CODE_SUCCESS; 1135 } 1136 1137 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1138 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1139 if (!connection){ 1140 log_error("avrcp_get_play_status: could not find a connection."); 1141 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1142 } 1143 connection->notifications_to_deregister |= (1 << event_id); 1144 return ERROR_CODE_SUCCESS; 1145 } 1146 1147 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1148 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1149 if (!connection){ 1150 log_error("avrcp_get_capabilities: could not find a connection."); 1151 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1152 } 1153 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1154 connection->state = AVCTP_W2_SEND_COMMAND; 1155 1156 connection->transaction_label++; 1157 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1158 connection->command_type = AVRCP_CTYPE_CONTROL; 1159 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1160 connection->subunit_id = AVRCP_SUBUNIT_ID; 1161 int pos = 0; 1162 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1163 pos += 3; 1164 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1165 connection->cmd_operands[pos++] = 0; 1166 1167 // Parameter Length 1168 big_endian_store_16(connection->cmd_operands, pos, 2); 1169 pos += 2; 1170 1171 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1172 pos += 2; 1173 1174 connection->cmd_operands_length = pos; 1175 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1176 return ERROR_CODE_SUCCESS; 1177 } 1178 1179 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1180 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1181 if (!connection){ 1182 log_error("avrcp_get_capabilities: could not find a connection."); 1183 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1184 } 1185 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1186 connection->state = AVCTP_W2_SEND_COMMAND; 1187 1188 connection->transaction_label++; 1189 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1190 connection->command_type = AVRCP_CTYPE_STATUS; 1191 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1192 connection->subunit_id = AVRCP_SUBUNIT_ID; 1193 int pos = 0; 1194 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1195 pos += 3; 1196 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1197 connection->cmd_operands[pos++] = 0; 1198 1199 // Parameter Length 1200 big_endian_store_16(connection->cmd_operands, pos, 9); 1201 pos += 2; 1202 1203 // write 8 bytes value 1204 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1205 pos += 8; 1206 1207 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 1208 // every attribute is 4 bytes long 1209 1210 connection->cmd_operands_length = pos; 1211 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1212 return ERROR_CODE_SUCCESS; 1213 } 1214 1215 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1216 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1217 if (!connection){ 1218 log_error("avrcp_get_capabilities: could not find a connection."); 1219 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1220 } 1221 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1222 connection->state = AVCTP_W2_SEND_COMMAND; 1223 1224 connection->transaction_label++; 1225 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1226 connection->command_type = AVRCP_CTYPE_CONTROL; 1227 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1228 connection->subunit_id = AVRCP_SUBUNIT_ID; 1229 int pos = 0; 1230 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1231 pos += 3; 1232 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1233 connection->cmd_operands[pos++] = 0; 1234 1235 // Parameter Length 1236 big_endian_store_16(connection->cmd_operands, pos, 1); 1237 pos += 2; 1238 connection->cmd_operands[pos++] = volume; 1239 1240 connection->cmd_operands_length = pos; 1241 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1242 return ERROR_CODE_SUCCESS; 1243 } 1244 1245 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1246 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1247 if (!connection){ 1248 log_error("avrcp_get_capabilities: could not find a connection."); 1249 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1250 } 1251 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1252 connection->state = AVCTP_W2_SEND_COMMAND; 1253 1254 connection->transaction_label++; 1255 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1256 connection->command_type = AVRCP_CTYPE_STATUS; 1257 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1258 connection->subunit_id = AVRCP_SUBUNIT_ID; 1259 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1260 connection->cmd_operands[3] = AVRCP_PDU_ID_GetCurrentPlayerApplicationSettingValue; // PDU ID 1261 connection->cmd_operands[4] = 0; 1262 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1263 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1264 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1265 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1266 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1267 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1268 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1269 connection->cmd_operands_length = 12; 1270 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1271 return ERROR_CODE_SUCCESS; 1272 } 1273 1274 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1275 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1276 if (!connection){ 1277 log_error("avrcp_get_capabilities: could not find a connection."); 1278 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1279 } 1280 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1281 connection->state = AVCTP_W2_SEND_COMMAND; 1282 1283 connection->transaction_label++; 1284 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1285 connection->command_type = AVRCP_CTYPE_CONTROL; 1286 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1287 connection->subunit_id = AVRCP_SUBUNIT_ID; 1288 int pos = 0; 1289 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1290 pos += 3; 1291 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SetPlayerApplicationSettingValue; // PDU ID 1292 connection->cmd_operands[pos++] = 0; 1293 // Parameter Length 1294 big_endian_store_16(connection->cmd_operands, pos, 3); 1295 pos += 2; 1296 connection->cmd_operands[pos++] = 2; 1297 connection->cmd_operands_length = pos; 1298 connection->cmd_operands[pos++] = attr_id; 1299 connection->cmd_operands[pos++] = attr_value; 1300 connection->cmd_operands_length = pos; 1301 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1302 return ERROR_CODE_SUCCESS; 1303 } 1304 1305 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1306 if (mode < AVRCP_SHUFFLE_MODE_OFF || mode > AVRCP_SHUFFLE_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1307 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1308 } 1309 1310 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1311 if (mode < AVRCP_REPEAT_MODE_OFF || mode > AVRCP_REPEAT_MODE_GROUP) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1312 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1313 } 1314 1315 uint8_t avrcp_controller_disconnect(uint16_t avrcp_cid){ 1316 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1317 if (!connection){ 1318 log_error("avrcp_get_capabilities: could not find a connection."); 1319 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1320 } 1321 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1322 if (connection->browsing_connection){ 1323 if (connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1324 l2cap_disconnect(connection->browsing_connection->l2cap_browsing_cid, 0); 1325 } 1326 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 1327 return ERROR_CODE_SUCCESS; 1328 } 1329 1330 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1331 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1332 if (!connection){ 1333 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1334 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1335 } 1336 if (connection->state != AVCTP_CONNECTION_OPENED){ 1337 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1338 return ERROR_CODE_COMMAND_DISALLOWED; 1339 } 1340 connection->state = AVCTP_W2_SEND_COMMAND; 1341 1342 connection->transaction_label++; 1343 connection->command_type = AVRCP_CTYPE_CONTROL; 1344 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1345 connection->subunit_id = AVRCP_SUBUNIT_ID; 1346 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1347 int pos = 0; 1348 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1349 pos += 3; 1350 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1351 // reserved 1352 connection->cmd_operands[pos++] = 0; 1353 // Parameter Length 1354 big_endian_store_16(connection->cmd_operands, pos, 11); 1355 pos += 2; 1356 connection->cmd_operands[pos++] = scope; 1357 memset(&connection->cmd_operands[pos], 0, 8); 1358 if (uid){ 1359 memcpy(&connection->cmd_operands[pos], uid, 8); 1360 } 1361 pos += 8; 1362 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1363 pos += 2; 1364 connection->cmd_operands_length = pos; 1365 1366 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1367 return ERROR_CODE_SUCCESS; 1368 } 1369 1370 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){ 1371 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1372 if (!connection){ 1373 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1374 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1375 } 1376 if (connection->state != AVCTP_CONNECTION_OPENED){ 1377 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1378 return ERROR_CODE_COMMAND_DISALLOWED; 1379 } 1380 connection->state = AVCTP_W2_SEND_COMMAND; 1381 1382 connection->transaction_label++; 1383 connection->command_type = AVRCP_CTYPE_CONTROL; 1384 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1385 connection->subunit_id = AVRCP_SUBUNIT_ID; 1386 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1387 int pos = 0; 1388 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1389 pos += 3; 1390 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1391 // reserved 1392 connection->cmd_operands[pos++] = 0; 1393 // Parameter Length 1394 big_endian_store_16(connection->cmd_operands, pos, 11); 1395 pos += 2; 1396 connection->cmd_operands[pos++] = scope; 1397 memset(&connection->cmd_operands[pos], 0, 8); 1398 if (uid){ 1399 memcpy(&connection->cmd_operands[pos], uid, 8); 1400 } 1401 pos += 8; 1402 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1403 pos += 2; 1404 connection->cmd_operands_length = pos; 1405 1406 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1407 return ERROR_CODE_SUCCESS; 1408 } 1409 1410 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1411 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1412 if (!connection){ 1413 log_error("avrcp_controller_play_item: could not find a connection."); 1414 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1415 } 1416 connection->max_num_fragments = max_num_fragments; 1417 return ERROR_CODE_SUCCESS; 1418 } 1419 1420 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){ 1421 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_controller_context); 1422 if (!connection){ 1423 log_error("avrcp_controller_play_item: could not find a connection."); 1424 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1425 } 1426 1427 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1428 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1429 1430 connection->transaction_label++; 1431 connection->command_opcode = command_opcode; 1432 connection->command_type = command_type; 1433 connection->subunit_type = subunit_type; 1434 connection->subunit_id = subunit_id; 1435 connection->cmd_operands_fragmented_buffer = command_buffer; 1436 connection->cmd_operands_fragmented_pos = 0; 1437 connection->cmd_operands_fragmented_len = command_len; 1438 1439 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1440 return ERROR_CODE_SUCCESS; 1441 } 1442