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->press_and_hold_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->press_and_hold_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, bool 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->press_and_hold_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->press_and_hold_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, false); 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, true); 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->press_and_hold_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_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){ 1139 return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0); 1140 } 1141 1142 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1143 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1144 } 1145 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1146 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1147 } 1148 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1149 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1150 } 1151 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1152 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1153 } 1154 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1155 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1156 } 1157 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1158 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1159 } 1160 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1161 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1162 } 1163 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1164 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1165 } 1166 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1167 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1168 } 1169 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1170 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1171 } 1172 1173 /* stop continuous cmds */ 1174 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1175 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1176 if (!connection){ 1177 log_error("avrcp_stop_play: could not find a connection."); 1178 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1179 } 1180 if (connection->state != AVCTP_W4_STOP) return ERROR_CODE_COMMAND_DISALLOWED; 1181 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1182 } 1183 1184 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1185 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1186 if (!connection){ 1187 log_error("avrcp_get_play_status: could not find a connection."); 1188 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1189 } 1190 avrcp_controller_register_notification(connection, event_id); 1191 return ERROR_CODE_SUCCESS; 1192 } 1193 1194 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1195 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1196 if (!connection){ 1197 log_error("avrcp_get_play_status: could not find a connection."); 1198 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1199 } 1200 connection->notifications_to_deregister |= (1 << event_id); 1201 return ERROR_CODE_SUCCESS; 1202 } 1203 1204 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1205 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1206 if (!connection){ 1207 log_error("avrcp_unit_info: could not find a connection."); 1208 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1209 } 1210 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1211 connection->state = AVCTP_W2_SEND_COMMAND; 1212 1213 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1214 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 1215 connection->command_type = AVRCP_CTYPE_STATUS; 1216 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1217 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1218 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1219 connection->cmd_operands_length = 5; 1220 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1221 return ERROR_CODE_SUCCESS; 1222 } 1223 1224 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1225 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1226 if (!connection){ 1227 log_error("avrcp_unit_info: could not find a connection."); 1228 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1229 } 1230 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1231 connection->state = AVCTP_W2_SEND_COMMAND; 1232 1233 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1234 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 1235 connection->command_type = AVRCP_CTYPE_STATUS; 1236 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1237 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1238 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1239 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 1240 connection->cmd_operands_length = 5; 1241 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1242 return ERROR_CODE_SUCCESS; 1243 } 1244 1245 static uint8_t avrcp_controller_get_capabilities(uint16_t avrcp_cid, uint8_t capability_id){ 1246 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1247 if (!connection){ 1248 log_error("avrcp_get_capabilities: could not find a connection."); 1249 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1250 } 1251 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1252 connection->state = AVCTP_W2_SEND_COMMAND; 1253 1254 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1255 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1256 connection->command_type = AVRCP_CTYPE_STATUS; 1257 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1258 connection->subunit_id = AVRCP_SUBUNIT_ID; 1259 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1260 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CAPABILITIES; // PDU ID 1261 connection->cmd_operands[4] = 0; 1262 big_endian_store_16(connection->cmd_operands, 5, 1); // parameter length 1263 connection->cmd_operands[7] = capability_id; // capability ID 1264 connection->cmd_operands_length = 8; 1265 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1266 return ERROR_CODE_SUCCESS; 1267 } 1268 1269 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1270 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY); 1271 } 1272 1273 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1274 return avrcp_controller_get_capabilities(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT); 1275 } 1276 1277 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1278 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1279 if (!connection){ 1280 log_error("avrcp_get_play_status: could not find a connection."); 1281 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1282 } 1283 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1284 connection->state = AVCTP_W2_SEND_COMMAND; 1285 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1286 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1287 connection->command_type = AVRCP_CTYPE_STATUS; 1288 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1289 connection->subunit_id = AVRCP_SUBUNIT_ID; 1290 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1291 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1292 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1293 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1294 connection->cmd_operands_length = 7; 1295 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1296 return ERROR_CODE_SUCCESS; 1297 } 1298 1299 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1300 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1301 if (!connection){ 1302 log_error("avrcp_get_capabilities: could not find a connection."); 1303 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1304 } 1305 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1306 connection->state = AVCTP_W2_SEND_COMMAND; 1307 1308 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1309 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1310 connection->command_type = AVRCP_CTYPE_CONTROL; 1311 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1312 connection->subunit_id = AVRCP_SUBUNIT_ID; 1313 int pos = 0; 1314 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1315 pos += 3; 1316 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1317 connection->cmd_operands[pos++] = 0; 1318 1319 // Parameter Length 1320 big_endian_store_16(connection->cmd_operands, pos, 2); 1321 pos += 2; 1322 1323 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1324 pos += 2; 1325 1326 connection->cmd_operands_length = pos; 1327 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1328 return ERROR_CODE_SUCCESS; 1329 } 1330 1331 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 1332 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1333 if (!connection){ 1334 log_error("avrcp_get_capabilities: could not find a connection."); 1335 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1336 } 1337 1338 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1339 1340 if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) { 1341 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1342 } 1343 connection->state = AVCTP_W2_SEND_COMMAND; 1344 1345 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1346 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1347 connection->command_type = AVRCP_CTYPE_STATUS; 1348 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1349 connection->subunit_id = AVRCP_SUBUNIT_ID; 1350 int pos = 0; 1351 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1352 pos += 3; 1353 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1354 connection->cmd_operands[pos++] = 0; 1355 1356 // Parameter Length 1357 big_endian_store_16(connection->cmd_operands, pos, 9); 1358 pos += 2; 1359 1360 // write 8 bytes value 1361 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1362 pos += 8; 1363 1364 connection->cmd_operands[pos++] = num_attributes; // attribute count, if 0 get all attributes 1365 1366 int i; 1367 for (i = 0; i < num_attributes; i++){ 1368 // every attribute is 4 bytes long 1369 big_endian_store_32(connection->cmd_operands, pos, attributes[i]); 1370 pos += 4; 1371 } 1372 1373 connection->cmd_operands_length = pos; 1374 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1375 return ERROR_CODE_SUCCESS; 1376 } 1377 1378 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1379 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1380 } 1381 1382 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1383 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1384 if (!connection){ 1385 log_error("avrcp_get_capabilities: could not find a connection."); 1386 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1387 } 1388 1389 // 1390 // allow sending of multiple set abs volume commands without waiting for response 1391 // 1392 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1393 switch (connection->state){ 1394 case AVCTP_CONNECTION_OPENED: 1395 status = ERROR_CODE_SUCCESS; 1396 break; 1397 case AVCTP_W2_RECEIVE_RESPONSE: 1398 // - is pending response also set abs volume 1399 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1400 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1401 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1402 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1403 if (connection->cmd_operands[3] != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1404 // - is next transaction id valid in window 1405 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1406 status = ERROR_CODE_SUCCESS; 1407 break; 1408 default: 1409 break; 1410 } 1411 if (status != ERROR_CODE_SUCCESS) return status; 1412 1413 connection->state = AVCTP_W2_SEND_COMMAND; 1414 1415 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1416 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1417 connection->command_type = AVRCP_CTYPE_CONTROL; 1418 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1419 connection->subunit_id = AVRCP_SUBUNIT_ID; 1420 int pos = 0; 1421 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1422 pos += 3; 1423 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1424 connection->cmd_operands[pos++] = 0; 1425 1426 // Parameter Length 1427 big_endian_store_16(connection->cmd_operands, pos, 1); 1428 pos += 2; 1429 connection->cmd_operands[pos++] = volume; 1430 1431 connection->cmd_operands_length = pos; 1432 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1433 return ERROR_CODE_SUCCESS; 1434 } 1435 1436 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1437 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1438 if (!connection){ 1439 log_error("avrcp_get_capabilities: could not find a connection."); 1440 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1441 } 1442 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1443 connection->state = AVCTP_W2_SEND_COMMAND; 1444 1445 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1446 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1447 connection->command_type = AVRCP_CTYPE_STATUS; 1448 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1449 connection->subunit_id = AVRCP_SUBUNIT_ID; 1450 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1451 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1452 connection->cmd_operands[4] = 0; 1453 big_endian_store_16(connection->cmd_operands, 5, 5); // parameter length 1454 connection->cmd_operands[7] = 4; // NumPlayerApplicationSettingAttributeID 1455 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1456 connection->cmd_operands[8] = 0x01; // equalizer (1-OFF, 2-ON) 1457 connection->cmd_operands[9] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1458 connection->cmd_operands[10] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1459 connection->cmd_operands[11] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1460 connection->cmd_operands_length = 12; 1461 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1462 return ERROR_CODE_SUCCESS; 1463 } 1464 1465 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1466 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1467 if (!connection){ 1468 log_error("avrcp_get_capabilities: could not find a connection."); 1469 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1470 } 1471 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1472 connection->state = AVCTP_W2_SEND_COMMAND; 1473 1474 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1475 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1476 connection->command_type = AVRCP_CTYPE_CONTROL; 1477 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1478 connection->subunit_id = AVRCP_SUBUNIT_ID; 1479 int pos = 0; 1480 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1481 pos += 3; 1482 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1483 connection->cmd_operands[pos++] = 0; 1484 // Parameter Length 1485 big_endian_store_16(connection->cmd_operands, pos, 3); 1486 pos += 2; 1487 connection->cmd_operands[pos++] = 2; 1488 connection->cmd_operands_length = pos; 1489 connection->cmd_operands[pos++] = attr_id; 1490 connection->cmd_operands[pos++] = attr_value; 1491 connection->cmd_operands_length = pos; 1492 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1493 return ERROR_CODE_SUCCESS; 1494 } 1495 1496 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1497 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1498 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1499 } 1500 1501 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1502 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1503 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1504 } 1505 1506 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1507 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1508 if (!connection){ 1509 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1510 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1511 } 1512 if (connection->state != AVCTP_CONNECTION_OPENED){ 1513 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1514 return ERROR_CODE_COMMAND_DISALLOWED; 1515 } 1516 connection->state = AVCTP_W2_SEND_COMMAND; 1517 1518 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1519 connection->command_type = AVRCP_CTYPE_CONTROL; 1520 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1521 connection->subunit_id = AVRCP_SUBUNIT_ID; 1522 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1523 int pos = 0; 1524 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1525 pos += 3; 1526 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1527 // reserved 1528 connection->cmd_operands[pos++] = 0; 1529 // Parameter Length 1530 big_endian_store_16(connection->cmd_operands, pos, 11); 1531 pos += 2; 1532 connection->cmd_operands[pos++] = scope; 1533 memset(&connection->cmd_operands[pos], 0, 8); 1534 if (uid){ 1535 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1536 } 1537 pos += 8; 1538 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1539 pos += 2; 1540 connection->cmd_operands_length = pos; 1541 1542 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1543 return ERROR_CODE_SUCCESS; 1544 } 1545 1546 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){ 1547 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1548 if (!connection){ 1549 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1550 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1551 } 1552 if (connection->state != AVCTP_CONNECTION_OPENED){ 1553 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1554 return ERROR_CODE_COMMAND_DISALLOWED; 1555 } 1556 connection->state = AVCTP_W2_SEND_COMMAND; 1557 1558 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1559 connection->command_type = AVRCP_CTYPE_CONTROL; 1560 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1561 connection->subunit_id = AVRCP_SUBUNIT_ID; 1562 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1563 int pos = 0; 1564 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1565 pos += 3; 1566 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1567 // reserved 1568 connection->cmd_operands[pos++] = 0; 1569 // Parameter Length 1570 big_endian_store_16(connection->cmd_operands, pos, 11); 1571 pos += 2; 1572 connection->cmd_operands[pos++] = scope; 1573 memset(&connection->cmd_operands[pos], 0, 8); 1574 if (uid){ 1575 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1576 } 1577 pos += 8; 1578 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1579 pos += 2; 1580 connection->cmd_operands_length = pos; 1581 1582 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1583 return ERROR_CODE_SUCCESS; 1584 } 1585 1586 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1587 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1588 if (!connection){ 1589 log_error("avrcp_controller_play_item: could not find a connection."); 1590 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1591 } 1592 connection->max_num_fragments = max_num_fragments; 1593 return ERROR_CODE_SUCCESS; 1594 } 1595 1596 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){ 1597 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1598 if (!connection){ 1599 log_error("avrcp_controller_play_item: could not find a connection."); 1600 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1601 } 1602 1603 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1604 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1605 1606 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1607 connection->command_opcode = command_opcode; 1608 connection->command_type = command_type; 1609 connection->subunit_type = subunit_type; 1610 connection->subunit_id = subunit_id; 1611 connection->cmd_operands_fragmented_buffer = command_buffer; 1612 connection->cmd_operands_fragmented_pos = 0; 1613 connection->cmd_operands_fragmented_len = command_len; 1614 1615 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1616 return ERROR_CODE_SUCCESS; 1617 } 1618