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