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