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