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