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 uint16_t 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 int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){ 474 uint8_t command[18]; 475 uint16_t pos = 0; 476 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 477 connection->transaction_label++; 478 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 479 480 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 481 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 482 command[pos++] = AVRCP_CTYPE_NOTIFY; 483 command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID; 484 command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 485 486 big_endian_store_24(command, pos, BT_SIG_COMPANY_ID); 487 pos += 3; 488 command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 489 command[pos++] = 0; // reserved(upper 6) | packet_type -> 0 490 big_endian_store_16(command, pos, 5); // parameter length 491 pos += 2; 492 command[pos++] = event_id; 493 big_endian_store_32(command, pos, 1); // send notification on playback position every second, for other notifications it is ignored 494 pos += 4; 495 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 496 } 497 498 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 499 UNUSED(timer); 500 avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer); 501 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 502 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 503 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 504 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 505 } 506 507 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 508 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 509 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 510 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 511 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 512 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 513 } 514 515 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 516 connection->continuous_fast_forward_cmd = 0; 517 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 518 } 519 520 521 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 522 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 523 if (connection->continuous_fast_forward_cmd){ 524 avrcp_press_and_hold_timer_stop(connection); 525 } 526 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 527 connection->transaction_label++; 528 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 529 return ERROR_CODE_SUCCESS; 530 } 531 532 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){ 533 log_info("Send command %d", opid); 534 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 535 if (!connection){ 536 log_error("Could not find a connection. avrcp cid 0x%02x", avrcp_cid); 537 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 538 } 539 540 if (connection->state != AVCTP_CONNECTION_OPENED){ 541 log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid); 542 return ERROR_CODE_COMMAND_DISALLOWED; 543 } 544 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 545 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 546 connection->command_type = AVRCP_CTYPE_CONTROL; 547 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 548 connection->subunit_id = AVRCP_SUBUNIT_ID; 549 connection->cmd_operands_length = 0; 550 551 connection->continuous_fast_forward_cmd = continuous_fast_forward_cmd; 552 connection->cmd_operands_length = 2; 553 connection->cmd_operands[0] = opid; 554 if (playback_speed > 0){ 555 connection->cmd_operands[2] = playback_speed; 556 connection->cmd_operands_length++; 557 } 558 connection->cmd_operands[1] = connection->cmd_operands_length - 2; 559 560 if (connection->continuous_fast_forward_cmd){ 561 avrcp_press_and_hold_timer_start(connection); 562 } 563 564 connection->transaction_label++; 565 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 566 return ERROR_CODE_SUCCESS; 567 } 568 569 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 570 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 0); 571 } 572 573 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 574 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, 1); 575 } 576 577 static int avrcp_controller_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 578 if (connection->notifications_to_deregister & (1 << event_id)) return 0; 579 if (connection->notifications_enabled & (1 << event_id)) return 0; 580 if (connection->notifications_to_register & (1 << event_id)) return 0; 581 connection->notifications_to_register |= (1 << event_id); 582 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 583 return 1; 584 } 585 586 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 587 connection->state = AVCTP_W2_SEND_COMMAND; 588 connection->transaction_label++; 589 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 590 connection->command_type = AVRCP_CTYPE_CONTROL; 591 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 592 connection->subunit_id = AVRCP_SUBUNIT_ID; 593 int pos = 0; 594 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 595 pos += 3; 596 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE; // PDU ID 597 connection->cmd_operands[pos++] = 0; 598 // Parameter Length 599 connection->cmd_operands_length = 8; 600 big_endian_store_16(connection->cmd_operands, pos, 1); 601 pos += 2; 602 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 603 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 604 return ERROR_CODE_SUCCESS; 605 } 606 607 608 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 609 connection->state = AVCTP_W2_SEND_COMMAND; 610 connection->transaction_label++; 611 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 612 connection->command_type = AVRCP_CTYPE_CONTROL; 613 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 614 connection->subunit_id = AVRCP_SUBUNIT_ID; 615 int pos = 0; 616 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 617 pos += 3; 618 connection->cmd_operands[pos++] = AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE; // PDU ID 619 connection->cmd_operands[pos++] = 0; 620 // Parameter Length 621 connection->cmd_operands_length = 8; 622 big_endian_store_16(connection->cmd_operands, pos, 1); 623 pos += 2; 624 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 625 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 626 return ERROR_CODE_SUCCESS; 627 } 628 629 static void avrcp_controller_handle_notification(avrcp_connection_t * connection, avrcp_command_type_t ctype, uint8_t * payload){ 630 uint16_t pos = 0; 631 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++]; 632 uint16_t event_mask = (1 << event_id); 633 uint16_t reset_event_mask = ~event_mask; 634 switch (ctype){ 635 case AVRCP_CTYPE_RESPONSE_INTERIM: 636 // register as enabled 637 connection->notifications_enabled |= event_mask; 638 break; 639 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 640 // received change, event is considered deregistered 641 // we are re-enabling it automatically, if it is not 642 // explicitly disabled 643 connection->notifications_enabled &= reset_event_mask; 644 if (! (connection->notifications_to_deregister & event_mask)){ 645 avrcp_controller_register_notification(connection, event_id); 646 } else { 647 connection->notifications_to_deregister &= reset_event_mask; 648 } 649 break; 650 default: 651 connection->notifications_to_register &= reset_event_mask; 652 connection->notifications_enabled &= reset_event_mask; 653 connection->notifications_to_deregister &= reset_event_mask; 654 break; 655 } 656 657 avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload+pos); 658 } 659 660 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 661 if (size < 6u) return; 662 663 uint16_t pos = 3; 664 uint8_t pdu_id; 665 666 avrcp_frame_type_t frame_type = (avrcp_frame_type_t)((packet[0] >> 1) & 0x01); 667 if (frame_type != AVRCP_RESPONSE_FRAME) return; 668 669 avrcp_packet_type_t packet_type = (avrcp_packet_type_t)((packet[0] >> 2) & 0x03); 670 switch (packet_type){ 671 case AVRCP_SINGLE_PACKET: 672 break; 673 default: 674 log_info("Fragmentation is not supported"); 675 return; 676 } 677 678 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 679 680 uint8_t byte_value = packet[pos++]; 681 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 682 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 683 uint8_t opcode = packet[pos++]; 684 685 uint16_t param_length; 686 687 switch (opcode){ 688 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 689 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 690 connection->state = AVCTP_CONNECTION_OPENED; 691 // page, extention code (1) 692 pos++; 693 uint8_t unit_type = packet[pos] >> 3; 694 uint8_t max_subunit_ID = packet[pos] & 0x07; 695 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); 696 break; 697 } 698 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 699 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 700 connection->state = AVCTP_CONNECTION_OPENED; 701 // byte value 7 (1) 702 pos++; 703 uint8_t unit_type = packet[pos] >> 3; 704 uint8_t unit = packet[pos] & 0x07; 705 pos++; 706 uint32_t company_id = big_endian_read_24(packet, pos); 707 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, 708 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 709 break; 710 } 711 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 712 // Company ID (3) 713 pos += 3; 714 pdu_id = packet[pos++]; 715 // packet type (1) 716 pos++; 717 param_length = big_endian_read_16(packet, pos); 718 pos += 2; 719 log_info("operands length %d, remaining size %d", param_length, size - pos); 720 721 if ((size - pos) < param_length) { 722 log_error("Wrong packet size %d < %d", size - pos, param_length); 723 return; 724 }; 725 726 // handle asynchronous notifications, without changing state 727 if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 728 avrcp_controller_handle_notification(connection, ctype, packet+pos); 729 break; 730 } 731 732 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE){ 733 log_info("AVRCP_CMD_OPCODE_VENDOR_DEPENDENT state %d", connection->state); 734 return; 735 } 736 connection->state = AVCTP_CONNECTION_OPENED; 737 738 log_info("VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 739 switch (pdu_id){ 740 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{ 741 uint8_t num_attributes = packet[pos++]; 742 int i; 743 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 744 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 745 for (i = 0; i < num_attributes; i++){ 746 uint8_t attribute_id = packet[pos++]; 747 uint8_t value = packet[pos++]; 748 switch (attribute_id){ 749 case 0x02: 750 repeat_mode = (avrcp_repeat_mode_t) value; 751 break; 752 case 0x03: 753 shuffle_mode = (avrcp_shuffle_mode_t) value; 754 break; 755 default: 756 break; 757 } 758 } 759 avrcp_controller_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 760 break; 761 } 762 763 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{ 764 uint16_t offset = 0; 765 uint8_t event[6]; 766 event[offset++] = HCI_EVENT_AVRCP_META; 767 event[offset++] = sizeof(event) - 2; 768 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 769 little_endian_store_16(event, offset, connection->avrcp_cid); 770 offset += 2; 771 event[offset++] = ctype; 772 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 773 break; 774 } 775 776 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 777 uint16_t offset = 0; 778 uint8_t event[7]; 779 event[offset++] = HCI_EVENT_AVRCP_META; 780 event[offset++] = sizeof(event) - 2; 781 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 782 little_endian_store_16(event, offset, connection->avrcp_cid); 783 offset += 2; 784 event[offset++] = ctype; 785 event[offset++] = packet[pos++]; 786 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 787 break; 788 } 789 790 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 791 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 792 uint8_t capability_count = packet[pos++]; 793 uint16_t i; 794 uint16_t offset = 0; 795 uint8_t event[10]; 796 797 switch (capability_id){ 798 799 case AVRCP_CAPABILITY_ID_COMPANY: 800 for (i = 0; i < capability_count; i++){ 801 uint32_t company_id = big_endian_read_24(packet, pos); 802 pos += 3; 803 log_info(" 0x%06" PRIx32 ", ", company_id); 804 805 offset = 0; 806 event[offset++] = HCI_EVENT_AVRCP_META; 807 event[offset++] = sizeof(event) - 2; 808 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID; 809 little_endian_store_16(event, offset, connection->avrcp_cid); 810 offset += 2; 811 event[offset++] = ctype; 812 event[offset++] = 0; 813 little_endian_store_24(event, offset, company_id); 814 offset += 3; 815 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 816 break; 817 } 818 819 offset = 0; 820 event[offset++] = HCI_EVENT_AVRCP_META; 821 event[offset++] = sizeof(event) - 2; 822 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE; 823 little_endian_store_16(event, offset, connection->avrcp_cid); 824 offset += 2; 825 event[offset++] = ctype; 826 event[offset++] = 0; 827 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 828 break; 829 830 case AVRCP_CAPABILITY_ID_EVENT: 831 for (i = 0; i < capability_count; i++){ 832 uint8_t event_id = packet[pos++]; 833 log_info(" 0x%02x %s", event_id, avrcp_event2str(event_id)); 834 835 offset = 0; 836 event[offset++] = HCI_EVENT_AVRCP_META; 837 event[offset++] = sizeof(event) - 2; 838 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID; 839 little_endian_store_16(event, offset, connection->avrcp_cid); 840 offset += 2; 841 event[offset++] = ctype; 842 event[offset++] = 0; 843 event[offset++] = event_id; 844 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 845 } 846 847 offset = 0; 848 event[offset++] = HCI_EVENT_AVRCP_META; 849 event[offset++] = sizeof(event) - 2; 850 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE; 851 little_endian_store_16(event, offset, connection->avrcp_cid); 852 offset += 2; 853 event[offset++] = ctype; 854 event[offset++] = 0; 855 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 856 break; 857 } 858 break; 859 } 860 861 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 862 uint32_t song_length = big_endian_read_32(packet, pos); 863 pos += 4; 864 uint32_t song_position = big_endian_read_32(packet, pos); 865 pos += 4; 866 uint8_t play_status = packet[pos]; 867 868 uint8_t event[15]; 869 int offset = 0; 870 event[offset++] = HCI_EVENT_AVRCP_META; 871 event[offset++] = sizeof(event) - 2; 872 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 873 little_endian_store_16(event, offset, connection->avrcp_cid); 874 offset += 2; 875 event[offset++] = ctype; 876 little_endian_store_32(event, offset, song_length); 877 offset += 4; 878 little_endian_store_32(event, offset, song_position); 879 offset += 4; 880 event[offset++] = play_status; 881 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 882 break; 883 } 884 885 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 886 switch (packet_type){ 887 case AVRCP_START_PACKET: 888 case AVRCP_SINGLE_PACKET: 889 avrcp_parser_reset(connection); 890 connection->list_size = param_length; 891 connection->num_attributes = packet[pos++]; 892 893 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 894 895 if (packet_type == AVRCP_START_PACKET){ 896 avrcp_controller_request_continue_response(connection); 897 } 898 break; 899 case AVRCP_CONTINUE_PACKET: 900 case AVRCP_END_PACKET: 901 connection->num_received_fragments++; 902 if (connection->num_received_fragments < connection->max_num_fragments){ 903 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 904 if (packet_type == AVRCP_CONTINUE_PACKET){ 905 avrcp_controller_request_continue_response(connection); 906 } 907 } else { 908 avrcp_controller_request_abort_continuation(connection); 909 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 910 avrcp_parser_reset(connection); 911 } 912 break; 913 } 914 } 915 default: 916 break; 917 } 918 break; 919 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 920 uint8_t operation_id = packet[pos++]; 921 switch (connection->state){ 922 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 923 if (connection->continuous_fast_forward_cmd){ 924 connection->state = AVCTP_W4_STOP; 925 } else { 926 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 927 } 928 break; 929 case AVCTP_W2_RECEIVE_RESPONSE: 930 connection->state = AVCTP_CONNECTION_OPENED; 931 break; 932 default: 933 break; 934 } 935 if (connection->state == AVCTP_W4_STOP){ 936 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 937 } 938 if (connection->state == AVCTP_CONNECTION_OPENED) { 939 // RELEASE response 940 operation_id = operation_id & 0x7F; 941 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 942 } 943 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 944 // PRESS response 945 avrcp_controller_request_pass_through_release_control_cmd(connection); 946 } 947 break; 948 } 949 default: 950 break; 951 } 952 953 // trigger pending notification reqistrations 954 if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->notifications_to_register){ 955 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 956 } 957 } 958 959 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 960 switch (connection->state){ 961 case AVCTP_W2_SEND_PRESS_COMMAND: 962 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 963 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 964 return; 965 case AVCTP_W2_SEND_COMMAND: 966 case AVCTP_W2_SEND_RELEASE_COMMAND: 967 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 968 avrcp_send_cmd(connection, AVRCP_SINGLE_PACKET); 969 return; 970 case AVCTP_W2_SEND_FRAGMENTED_COMMAND: 971 if (connection->cmd_operands_fragmented_pos == 0){ 972 avrcp_send_cmd(connection, AVRCP_START_PACKET); 973 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 974 } else { 975 if ((connection->cmd_operands_fragmented_len - connection->cmd_operands_fragmented_pos) > avrcp_get_max_payload_size_for_packet_type(AVRCP_CONTINUE_PACKET)){ 976 avrcp_send_cmd(connection, AVRCP_CONTINUE_PACKET); 977 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 978 } else { 979 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 980 avrcp_send_cmd(connection, AVRCP_END_PACKET); 981 } 982 } 983 return; 984 default: 985 break; 986 } 987 // send register notification if queued 988 if (connection->notifications_to_register != 0){ 989 uint8_t event_id; 990 for (event_id = 1; event_id <= AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED; event_id++){ 991 if (connection->notifications_to_register & (1<<event_id)){ 992 connection->notifications_to_register &= ~ (1 << event_id); 993 avrcp_send_register_notification(connection, event_id); 994 return; 995 } 996 } 997 } 998 } 999 1000 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1001 avrcp_connection_t * connection; 1002 1003 switch (packet_type) { 1004 case L2CAP_DATA_PACKET: 1005 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1006 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1007 break; 1008 1009 case HCI_EVENT_PACKET: 1010 switch (hci_event_packet_get_type(packet)){ 1011 case L2CAP_EVENT_CAN_SEND_NOW: 1012 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1013 avrcp_controller_handle_can_send_now(connection); 1014 break; 1015 default: 1016 break; 1017 } 1018 default: 1019 break; 1020 } 1021 } 1022 1023 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){ 1024 avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 1025 } 1026 1027 void avrcp_controller_init(void){ 1028 avrcp_controller_context.role = AVRCP_CONTROLLER; 1029 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1030 avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler); 1031 } 1032 1033 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 1034 btstack_assert(callback != NULL); 1035 avrcp_controller_context.avrcp_callback = callback; 1036 } 1037 1038 1039 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1040 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1041 } 1042 1043 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1044 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1045 } 1046 1047 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1048 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1049 } 1050 1051 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1052 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1053 } 1054 1055 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1056 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1057 } 1058 1059 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1060 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1061 } 1062 1063 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1064 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1065 } 1066 1067 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1068 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1069 } 1070 1071 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1072 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1073 } 1074 1075 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1076 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1077 } 1078 1079 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1080 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1081 } 1082 1083 /* start continuous cmds */ 1084 1085 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1086 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1087 } 1088 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1089 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1090 } 1091 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1092 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1093 } 1094 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1095 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1096 } 1097 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1098 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1099 } 1100 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1101 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1102 } 1103 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1104 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1105 } 1106 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1107 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1108 } 1109 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1110 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1111 } 1112 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1113 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1114 } 1115 1116 /* stop continuous cmds */ 1117 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1118 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1119 if (!connection){ 1120 log_error("avrcp_stop_play: could not find a connection."); 1121 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1122 } 1123 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1124 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1125 } 1126 1127 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1128 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1129 if (!connection){ 1130 log_error("avrcp_get_play_status: could not find a connection."); 1131 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1132 } 1133 avrcp_controller_register_notification(connection, event_id); 1134 return ERROR_CODE_SUCCESS; 1135 } 1136 1137 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1138 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1139 if (!connection){ 1140 log_error("avrcp_get_play_status: could not find a connection."); 1141 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1142 } 1143 connection->notifications_to_deregister |= (1 << event_id); 1144 return ERROR_CODE_SUCCESS; 1145 } 1146 1147 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1148 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1149 if (!connection){ 1150 log_error("avrcp_unit_info: could not find a connection."); 1151 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1152 } 1153 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1154 connection->state = AVCTP_W2_SEND_COMMAND; 1155 1156 connection->transaction_label++; 1157 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 1158 connection->command_type = AVRCP_CTYPE_STATUS; 1159 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1160 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1161 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1162 connection->cmd_operands_length = 5; 1163 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1164 return ERROR_CODE_SUCCESS; 1165 } 1166 1167 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1168 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1169 if (!connection){ 1170 log_error("avrcp_unit_info: could not find a connection."); 1171 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1172 } 1173 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1174 connection->state = AVCTP_W2_SEND_COMMAND; 1175 1176 connection->transaction_label++; 1177 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 1178 connection->command_type = AVRCP_CTYPE_STATUS; 1179 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1180 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1181 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1182 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 1183 connection->cmd_operands_length = 5; 1184 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1185 return ERROR_CODE_SUCCESS; 1186 } 1187 1188 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 1189 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1190 if (!connection){ 1191 log_error("avrcp_get_capabilities: could not find a connection."); 1192 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1193 } 1194 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1195 connection->state = AVCTP_W2_SEND_COMMAND; 1196 1197 connection->transaction_label++; 1198 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1199 connection->command_type = AVRCP_CTYPE_STATUS; 1200 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1201 connection->subunit_id = AVRCP_SUBUNIT_ID; 1202 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1203 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 1204 connection->cmd_operands[4] = 0; 1205 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 1206 connection->cmd_operands[7] = capability_id; // capability ID 1207 connection->cmd_operands_length = 8; 1208 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1209 return ERROR_CODE_SUCCESS; 1210 } 1211 1212 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1213 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 1214 } 1215 1216 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1217 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 1218 } 1219 1220 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1221 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1222 if (!connection){ 1223 log_error("avrcp_get_play_status: could not find a connection."); 1224 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1225 } 1226 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1227 connection->state = AVCTP_W2_SEND_COMMAND; 1228 connection->transaction_label++; 1229 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1230 connection->command_type = AVRCP_CTYPE_STATUS; 1231 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1232 connection->subunit_id = AVRCP_SUBUNIT_ID; 1233 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1234 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1235 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1236 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1237 connection->cmd_operands_length = 7; 1238 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1239 return ERROR_CODE_SUCCESS; 1240 } 1241 1242 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1243 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1244 if (!connection){ 1245 log_error("avrcp_get_capabilities: could not find a connection."); 1246 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1247 } 1248 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1249 connection->state = AVCTP_W2_SEND_COMMAND; 1250 1251 connection->transaction_label++; 1252 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1253 connection->command_type = AVRCP_CTYPE_CONTROL; 1254 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1255 connection->subunit_id = AVRCP_SUBUNIT_ID; 1256 int pos = 0; 1257 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1258 pos += 3; 1259 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1260 connection->cmd_operands[pos++] = 0; 1261 1262 // Parameter Length 1263 big_endian_store_16(connection->cmd_operands, pos, 2); 1264 pos += 2; 1265 1266 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1267 pos += 2; 1268 1269 connection->cmd_operands_length = pos; 1270 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1271 return ERROR_CODE_SUCCESS; 1272 } 1273 1274 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1275 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1276 if (!connection){ 1277 log_error("avrcp_get_capabilities: could not find a connection."); 1278 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1279 } 1280 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1281 connection->state = AVCTP_W2_SEND_COMMAND; 1282 1283 connection->transaction_label++; 1284 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1285 connection->command_type = AVRCP_CTYPE_STATUS; 1286 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1287 connection->subunit_id = AVRCP_SUBUNIT_ID; 1288 int pos = 0; 1289 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1290 pos += 3; 1291 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1292 connection->cmd_operands[pos++] = 0; 1293 1294 // Parameter Length 1295 big_endian_store_16(connection->cmd_operands, pos, 9); 1296 pos += 2; 1297 1298 // write 8 bytes value 1299 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1300 pos += 8; 1301 1302 connection->cmd_operands[pos++] = 0; // attribute count, if 0 get all attributes 1303 // every attribute is 4 bytes long 1304 1305 connection->cmd_operands_length = pos; 1306 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1307 return ERROR_CODE_SUCCESS; 1308 } 1309 1310 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1311 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1312 if (!connection){ 1313 log_error("avrcp_get_capabilities: could not find a connection."); 1314 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1315 } 1316 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1317 connection->state = AVCTP_W2_SEND_COMMAND; 1318 1319 connection->transaction_label++; 1320 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1321 connection->command_type = AVRCP_CTYPE_CONTROL; 1322 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1323 connection->subunit_id = AVRCP_SUBUNIT_ID; 1324 int pos = 0; 1325 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1326 pos += 3; 1327 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1328 connection->cmd_operands[pos++] = 0; 1329 1330 // Parameter Length 1331 big_endian_store_16(connection->cmd_operands, pos, 1); 1332 pos += 2; 1333 connection->cmd_operands[pos++] = volume; 1334 1335 connection->cmd_operands_length = pos; 1336 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1337 return ERROR_CODE_SUCCESS; 1338 } 1339 1340 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1341 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1342 if (!connection){ 1343 log_error("avrcp_get_capabilities: could not find a connection."); 1344 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1345 } 1346 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1347 connection->state = AVCTP_W2_SEND_COMMAND; 1348 1349 connection->transaction_label++; 1350 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1351 connection->command_type = AVRCP_CTYPE_STATUS; 1352 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1353 connection->subunit_id = AVRCP_SUBUNIT_ID; 1354 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1355 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1356 connection->cmd_operands[4] = 0; 1357 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1358 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1359 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1360 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1361 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1362 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1363 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1364 connection->cmd_operands_length = 12; 1365 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1366 return ERROR_CODE_SUCCESS; 1367 } 1368 1369 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1370 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1371 if (!connection){ 1372 log_error("avrcp_get_capabilities: could not find a connection."); 1373 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1374 } 1375 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1376 connection->state = AVCTP_W2_SEND_COMMAND; 1377 1378 connection->transaction_label++; 1379 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1380 connection->command_type = AVRCP_CTYPE_CONTROL; 1381 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1382 connection->subunit_id = AVRCP_SUBUNIT_ID; 1383 int pos = 0; 1384 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1385 pos += 3; 1386 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1387 connection->cmd_operands[pos++] = 0; 1388 // Parameter Length 1389 big_endian_store_16(connection->cmd_operands, pos, 3); 1390 pos += 2; 1391 connection->cmd_operands[pos++] = 2; 1392 connection->cmd_operands_length = pos; 1393 connection->cmd_operands[pos++] = attr_id; 1394 connection->cmd_operands[pos++] = attr_value; 1395 connection->cmd_operands_length = pos; 1396 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1397 return ERROR_CODE_SUCCESS; 1398 } 1399 1400 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1401 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1402 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1403 } 1404 1405 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1406 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1407 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1408 } 1409 1410 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1411 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1412 if (!connection){ 1413 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1414 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1415 } 1416 if (connection->state != AVCTP_CONNECTION_OPENED){ 1417 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1418 return ERROR_CODE_COMMAND_DISALLOWED; 1419 } 1420 connection->state = AVCTP_W2_SEND_COMMAND; 1421 1422 connection->transaction_label++; 1423 connection->command_type = AVRCP_CTYPE_CONTROL; 1424 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1425 connection->subunit_id = AVRCP_SUBUNIT_ID; 1426 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1427 int pos = 0; 1428 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1429 pos += 3; 1430 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1431 // reserved 1432 connection->cmd_operands[pos++] = 0; 1433 // Parameter Length 1434 big_endian_store_16(connection->cmd_operands, pos, 11); 1435 pos += 2; 1436 connection->cmd_operands[pos++] = scope; 1437 memset(&connection->cmd_operands[pos], 0, 8); 1438 if (uid){ 1439 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1440 } 1441 pos += 8; 1442 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1443 pos += 2; 1444 connection->cmd_operands_length = pos; 1445 1446 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1447 return ERROR_CODE_SUCCESS; 1448 } 1449 1450 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){ 1451 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1452 if (!connection){ 1453 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1454 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1455 } 1456 if (connection->state != AVCTP_CONNECTION_OPENED){ 1457 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1458 return ERROR_CODE_COMMAND_DISALLOWED; 1459 } 1460 connection->state = AVCTP_W2_SEND_COMMAND; 1461 1462 connection->transaction_label++; 1463 connection->command_type = AVRCP_CTYPE_CONTROL; 1464 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1465 connection->subunit_id = AVRCP_SUBUNIT_ID; 1466 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1467 int pos = 0; 1468 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1469 pos += 3; 1470 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1471 // reserved 1472 connection->cmd_operands[pos++] = 0; 1473 // Parameter Length 1474 big_endian_store_16(connection->cmd_operands, pos, 11); 1475 pos += 2; 1476 connection->cmd_operands[pos++] = scope; 1477 memset(&connection->cmd_operands[pos], 0, 8); 1478 if (uid){ 1479 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1480 } 1481 pos += 8; 1482 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1483 pos += 2; 1484 connection->cmd_operands_length = pos; 1485 1486 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1487 return ERROR_CODE_SUCCESS; 1488 } 1489 1490 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1491 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1492 if (!connection){ 1493 log_error("avrcp_controller_play_item: could not find a connection."); 1494 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1495 } 1496 connection->max_num_fragments = max_num_fragments; 1497 return ERROR_CODE_SUCCESS; 1498 } 1499 1500 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){ 1501 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1502 if (!connection){ 1503 log_error("avrcp_controller_play_item: could not find a connection."); 1504 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1505 } 1506 1507 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1508 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1509 1510 connection->transaction_label++; 1511 connection->command_opcode = command_opcode; 1512 connection->command_type = command_type; 1513 connection->subunit_type = subunit_type; 1514 connection->subunit_id = subunit_id; 1515 connection->cmd_operands_fragmented_buffer = command_buffer; 1516 connection->cmd_operands_fragmented_pos = 0; 1517 connection->cmd_operands_fragmented_len = command_len; 1518 1519 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1520 return ERROR_CODE_SUCCESS; 1521 } 1522