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