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