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){ 96 uint8_t event[7]; 97 uint8_t pos = 0; 98 event[pos++] = HCI_EVENT_AVRCP_META; 99 event[pos++] = sizeof(event) - 2; 100 event[pos++] = AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE; 101 little_endian_store_16(event, pos, connection->avrcp_cid); 102 pos += 2; 103 event[pos++] = status; 104 event[pos++] = event_id; 105 UNUSED(pos); 106 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 107 } 108 109 static void avrcp_controller_emit_supported_events(avrcp_connection_t * connection){ 110 uint8_t ctype = (uint8_t) AVRCP_CTYPE_RESPONSE_CHANGED_STABLE; 111 uint8_t event_id; 112 113 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 114 if ( (connection->remote_supported_notifications & (1<<event_id)) == 0){ 115 continue; 116 } 117 uint8_t event[8]; 118 uint8_t pos = 0; 119 event[pos++] = HCI_EVENT_AVRCP_META; 120 event[pos++] = sizeof(event) - 2; 121 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID; 122 little_endian_store_16(event, pos, connection->avrcp_cid); 123 pos += 2; 124 event[pos++] = ctype; 125 event[pos++] = 0; 126 event[pos++] = event_id; 127 UNUSED(pos); 128 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 129 } 130 131 uint8_t event[7]; 132 uint8_t pos = 0; 133 event[pos++] = HCI_EVENT_AVRCP_META; 134 event[pos++] = sizeof(event) - 2; 135 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE; 136 little_endian_store_16(event, pos, connection->avrcp_cid); 137 pos += 2; 138 event[pos++] = ctype; 139 event[pos++] = 0; 140 UNUSED(pos); 141 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 142 } 143 144 static void avrcp_controller_emit_notification_for_event_id(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id, 145 avrcp_command_type_t ctype, const uint8_t *payload, 146 uint16_t size) { 147 switch (event_id){ 148 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{ 149 if (size < 4) break; 150 uint32_t song_position = big_endian_read_32(payload, 0); 151 uint16_t offset = 0; 152 uint8_t event[10]; 153 event[offset++] = HCI_EVENT_AVRCP_META; 154 event[offset++] = sizeof(event) - 2; 155 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED; 156 little_endian_store_16(event, offset, avrcp_cid); 157 offset += 2; 158 event[offset++] = ctype; 159 little_endian_store_32(event, offset, song_position); 160 offset += 4; 161 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 162 break; 163 } 164 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 165 if (size < 1) break; 166 uint16_t offset = 0; 167 uint8_t event[7]; 168 event[offset++] = HCI_EVENT_AVRCP_META; 169 event[offset++] = sizeof(event) - 2; 170 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 171 little_endian_store_16(event, offset, avrcp_cid); 172 offset += 2; 173 event[offset++] = ctype; 174 event[offset++] = payload[0]; 175 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 176 break; 177 } 178 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 179 uint16_t offset = 0; 180 uint8_t event[6]; 181 event[offset++] = HCI_EVENT_AVRCP_META; 182 event[offset++] = sizeof(event) - 2; 183 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 184 little_endian_store_16(event, offset, avrcp_cid); 185 offset += 2; 186 event[offset++] = ctype; 187 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 188 break; 189 } 190 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 191 uint16_t offset = 0; 192 uint8_t event[6]; 193 event[offset++] = HCI_EVENT_AVRCP_META; 194 event[offset++] = sizeof(event) - 2; 195 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 196 little_endian_store_16(event, offset, avrcp_cid); 197 offset += 2; 198 event[offset++] = ctype; 199 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 200 break; 201 } 202 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 203 uint16_t offset = 0; 204 uint8_t event[6]; 205 event[offset++] = HCI_EVENT_AVRCP_META; 206 event[offset++] = sizeof(event) - 2; 207 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 208 little_endian_store_16(event, offset, avrcp_cid); 209 offset += 2; 210 event[offset++] = ctype; 211 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 212 break; 213 } 214 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 215 if (size < 1) break; 216 uint16_t offset = 0; 217 uint8_t event[7]; 218 event[offset++] = HCI_EVENT_AVRCP_META; 219 event[offset++] = sizeof(event) - 2; 220 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 221 little_endian_store_16(event, offset, avrcp_cid); 222 offset += 2; 223 event[offset++] = ctype; 224 event[offset++] = payload[0] & 0x7F; 225 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 226 break; 227 } 228 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 229 if (size < 2) break; 230 uint8_t event[8]; 231 uint16_t offset = 0; 232 uint16_t uuid = big_endian_read_16(payload, 0); 233 event[offset++] = HCI_EVENT_AVRCP_META; 234 event[offset++] = sizeof(event) - 2; 235 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED; 236 little_endian_store_16(event, offset, avrcp_cid); 237 offset += 2; 238 event[offset++] = ctype; 239 little_endian_store_16(event, offset, uuid); 240 offset += 2; 241 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 242 break; 243 } 244 245 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{ 246 uint16_t offset = 0; 247 uint8_t event[6]; 248 event[offset++] = HCI_EVENT_AVRCP_META; 249 event[offset++] = sizeof(event) - 2; 250 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END; 251 little_endian_store_16(event, offset, avrcp_cid); 252 offset += 2; 253 event[offset++] = ctype; 254 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 255 break; 256 } 257 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{ 258 uint16_t offset = 0; 259 uint8_t event[6]; 260 event[offset++] = HCI_EVENT_AVRCP_META; 261 event[offset++] = sizeof(event) - 2; 262 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START; 263 little_endian_store_16(event, offset, avrcp_cid); 264 offset += 2; 265 event[offset++] = ctype; 266 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 267 break; 268 } 269 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{ 270 if (size < 1) break; 271 uint16_t offset = 0; 272 uint8_t event[7]; 273 event[offset++] = HCI_EVENT_AVRCP_META; 274 event[offset++] = sizeof(event) - 2; 275 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 276 little_endian_store_16(event, offset, avrcp_cid); 277 offset += 2; 278 event[offset++] = ctype; 279 event[offset++] = payload[0]; 280 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 281 break; 282 } 283 284 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{ 285 if (size < 1) break; 286 uint16_t offset = 0; 287 uint8_t event[7]; 288 event[offset++] = HCI_EVENT_AVRCP_META; 289 event[offset++] = sizeof(event) - 2; 290 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED; 291 little_endian_store_16(event, offset, avrcp_cid); 292 offset += 2; 293 event[offset++] = ctype; 294 event[offset++] = payload[0]; 295 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 296 break; 297 } 298 299 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 300 default: 301 log_info("avrcp: not implemented"); 302 break; 303 } 304 } 305 306 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){ 307 btstack_assert(callback != NULL); 308 309 uint8_t event[8]; 310 int pos = 0; 311 event[pos++] = HCI_EVENT_AVRCP_META; 312 event[pos++] = sizeof(event) - 2; 313 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 314 little_endian_store_16(event, pos, avrcp_cid); 315 pos += 2; 316 event[pos++] = ctype; 317 event[pos++] = repeat_mode; 318 event[pos++] = shuffle_mode; 319 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 320 } 321 322 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){ 323 uint8_t event[7]; 324 int pos = 0; 325 event[pos++] = HCI_EVENT_AVRCP_META; 326 event[pos++] = sizeof(event) - 2; 327 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 328 little_endian_store_16(event, pos, avrcp_cid); 329 pos += 2; 330 event[pos++] = ctype; 331 event[pos++] = status; 332 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 333 } 334 335 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){ 336 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 337 int pos = 0; 338 event[pos++] = HCI_EVENT_AVRCP_META; 339 // reserve one byte for subevent type and data len 340 int data_len_pos = pos; 341 pos++; 342 int subevent_type_pos = pos; 343 pos++; 344 little_endian_store_16(event, pos, avrcp_cid); 345 pos += 2; 346 event[pos++] = ctype; 347 348 switch (attr_id){ 349 case AVRCP_MEDIA_ATTR_TITLE: 350 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 351 event[pos++] = value_len; 352 (void)memcpy(event + pos, value, value_len); 353 break; 354 case AVRCP_MEDIA_ATTR_ARTIST: 355 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ARTIST_INFO; 356 event[pos++] = value_len; 357 (void)memcpy(event + pos, value, value_len); 358 break; 359 case AVRCP_MEDIA_ATTR_ALBUM: 360 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 361 event[pos++] = value_len; 362 (void)memcpy(event + pos, value, value_len); 363 break; 364 case AVRCP_MEDIA_ATTR_GENRE: 365 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 366 event[pos++] = value_len; 367 (void)memcpy(event + pos, value, value_len); 368 break; 369 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 370 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 371 if (value){ 372 little_endian_store_32(event, pos, btstack_atoi((char *)value)); 373 } else { 374 little_endian_store_32(event, pos, 0); 375 } 376 pos += 4; 377 break; 378 case AVRCP_MEDIA_ATTR_TRACK: 379 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 380 if (value){ 381 event[pos++] = btstack_atoi((char *)value); 382 } else { 383 event[pos++] = 0; 384 } 385 break; 386 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 387 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 388 if (value){ 389 event[pos++] = btstack_atoi((char *)value); 390 } else { 391 event[pos++] = 0; 392 } 393 break; 394 default: 395 break; 396 } 397 event[data_len_pos] = pos - 2; 398 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 399 } 400 401 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){ 402 btstack_assert(callback != NULL); 403 404 uint8_t event[7]; 405 int pos = 0; 406 event[pos++] = HCI_EVENT_AVRCP_META; 407 event[pos++] = sizeof(event) - 2; 408 event[pos++] = subevent; 409 little_endian_store_16(event, pos, avrcp_cid); 410 pos += 2; 411 event[pos++] = ctype; 412 event[pos++] = operation_id; 413 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 414 } 415 416 static void avrcp_parser_reset(avrcp_connection_t * connection){ 417 connection->list_offset = 0; 418 connection->num_attributes = 0; 419 connection->num_parsed_attributes = 0; 420 connection->parser_attribute_header_pos = 0; 421 connection->num_received_fragments = 0; 422 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 423 } 424 425 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 426 uint16_t attribute_total_value_len; 427 uint32_t attribute_id; 428 switch(connection->parser_state){ 429 case AVRCP_PARSER_GET_ATTRIBUTE_HEADER: 430 connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 431 connection->list_offset++; 432 433 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return; 434 435 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 436 connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE); 437 if (connection->attribute_value_len > 0){ 438 // get ready for attribute value 439 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 440 return; 441 } 442 443 // emit empty attribute 444 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 445 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); 446 447 // done, see below 448 break; 449 450 case AVRCP_PARSER_GET_ATTRIBUTE_VALUE: 451 connection->attribute_value[connection->attribute_value_offset++] = byte; 452 connection->list_offset++; 453 454 if (connection->attribute_value_offset < connection->attribute_value_len) return; 455 456 // emit (potentially partial) attribute 457 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 458 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); 459 460 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 461 if (connection->attribute_value_offset < attribute_total_value_len){ 462 // ignore rest of attribute 463 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 464 return; 465 } 466 467 // done, see below 468 break; 469 470 case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 471 connection->attribute_value_offset++; 472 connection->list_offset++; 473 474 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 475 if (connection->attribute_value_offset < attribute_total_value_len) return; 476 477 // done, see below 478 break; 479 480 default: 481 return; 482 } 483 484 // attribute fully read, check if more to come 485 if (connection->list_offset < connection->list_size){ 486 // more to come, reset parser 487 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 488 connection->parser_attribute_header_pos = 0; 489 connection->attribute_value_offset = 0; 490 } else { 491 // fully done 492 avrcp_parser_reset(connection); 493 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 494 } 495 } 496 497 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){ 498 int i; 499 for (i=0;i<num_bytes_to_read;i++){ 500 avrcp_parser_process_byte(packet[i], connection, ctype); 501 } 502 } 503 504 505 static int avrcp_send_cmd(avrcp_connection_t * connection, avrcp_packet_type_t packet_type){ 506 uint8_t command[AVRCP_CMD_BUFFER_SIZE]; 507 uint16_t pos = 0; 508 509 // non-fragmented: transport header (1) + PID (2) 510 // fragmented: transport header (1) + num packets (1) + PID (2) 511 512 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 513 command[pos++] = (connection->transaction_id << 4) | (packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 514 515 if (packet_type == AVRCP_START_PACKET){ 516 // num packets: (3 bytes overhead (PID, num packets) + command) / (MTU - transport header). 517 // to get number of packets using integer division, we subtract 1 from the data e.g. len = 5, packet size 5 => need 1 packet 518 command[pos++] = ((connection->cmd_operands_fragmented_len + 3 - 1) / (AVRCP_CMD_BUFFER_SIZE - 1)) + 1; 519 } 520 521 if ((packet_type == AVRCP_SINGLE_PACKET) || (packet_type == AVRCP_START_PACKET)){ 522 // Profile IDentifier (PID) 523 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 524 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 525 526 // command_type 527 command[pos++] = connection->command_type; 528 // subunit_type | subunit ID 529 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 530 // opcode 531 command[pos++] = (uint8_t)connection->command_opcode; 532 } 533 534 if (packet_type == AVRCP_SINGLE_PACKET){ 535 // operands 536 (void)memcpy(command + pos, connection->cmd_operands, 537 connection->cmd_operands_length); 538 pos += connection->cmd_operands_length; 539 } else { 540 uint16_t bytes_free = AVRCP_CMD_BUFFER_SIZE - pos; 541 uint16_t bytes_to_store = connection->cmd_operands_fragmented_len-connection->cmd_operands_fragmented_pos; 542 uint16_t bytes_to_copy = btstack_min(bytes_to_store, bytes_free); 543 (void)memcpy(command + pos, 544 &connection->cmd_operands_fragmented_buffer[connection->cmd_operands_fragmented_pos], 545 bytes_to_copy); 546 pos += bytes_to_copy; 547 connection->cmd_operands_fragmented_pos += bytes_to_copy; 548 } 549 550 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 551 } 552 553 static int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){ 554 uint8_t command[18]; 555 uint16_t pos = 0; 556 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 557 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 558 command[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 559 560 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 561 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 562 command[pos++] = AVRCP_CTYPE_NOTIFY; 563 command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID; 564 command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 565 566 big_endian_store_24(command, pos, BT_SIG_COMPANY_ID); 567 pos += 3; 568 command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 569 command[pos++] = 0; // reserved(upper 6) | packet_type -> 0 570 big_endian_store_16(command, pos, 5); // parameter length 571 pos += 2; 572 command[pos++] = event_id; 573 big_endian_store_32(command, pos, 1); // send notification on playback position every second, for other notifications it is ignored 574 pos += 4; 575 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 576 } 577 578 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 579 UNUSED(timer); 580 avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer); 581 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 582 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 583 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 584 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 585 } 586 587 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 588 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 589 btstack_run_loop_set_timer_handler(&connection->press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 590 btstack_run_loop_set_timer_context(&connection->press_and_hold_cmd_timer, connection); 591 btstack_run_loop_set_timer(&connection->press_and_hold_cmd_timer, 2000); // 2 seconds timeout 592 btstack_run_loop_add_timer(&connection->press_and_hold_cmd_timer); 593 } 594 595 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 596 connection->press_and_hold_cmd_active = false; 597 btstack_run_loop_remove_timer(&connection->press_and_hold_cmd_timer); 598 } 599 600 601 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 602 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 603 if (connection->press_and_hold_cmd_active){ 604 avrcp_press_and_hold_timer_stop(connection); 605 } 606 connection->cmd_operands[0] = 0x80 | connection->cmd_operands[0]; 607 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 608 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 609 return ERROR_CODE_SUCCESS; 610 } 611 612 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){ 613 log_info("Send command %d", opid); 614 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 615 if (!connection){ 616 log_error("Could not find a connection. avrcp cid 0x%02x", avrcp_cid); 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->remote_supported_notifications_queried && (connection->remote_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->remote_supported_notifications_queried){ 686 connection->remote_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); 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)){ 767 avrcp_controller_register_notification(connection, event_id); 768 } else { 769 connection->notifications_to_deregister &= reset_event_mask; 770 } 771 break; 772 default: 773 connection->notifications_to_register &= reset_event_mask; 774 connection->notifications_enabled &= reset_event_mask; 775 connection->notifications_to_deregister &= reset_event_mask; 776 break; 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->remote_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->remote_supported_notifications_suppress_emit_result){ 1032 connection->remote_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->remote_supported_notifications & (1<<i)) == 0){ 1039 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, i); 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 log_error("avrcp_stop_play: could not find a connection."); 1339 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1340 } 1341 1342 switch (connection->state){ 1343 // respond when we receive response for (repeated) press command 1344 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1345 connection->press_and_hold_cmd_release = true; 1346 break; 1347 1348 // release already sent or on the way, nothing to do 1349 case AVCTP_W2_RECEIVE_RESPONSE: 1350 case AVCTP_W2_SEND_RELEASE_COMMAND: 1351 break; 1352 1353 // about to send next repeated press command or wait for it -> release right away 1354 case AVCTP_W2_SEND_PRESS_COMMAND: 1355 case AVCTP_W4_STOP: 1356 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1357 1358 // otherwise reject request 1359 default: 1360 return ERROR_CODE_COMMAND_DISALLOWED; 1361 } 1362 return ERROR_CODE_SUCCESS; 1363 } 1364 1365 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1366 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1367 if (!connection){ 1368 log_error("avrcp_get_play_status: could not find a connection."); 1369 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1370 } 1371 return avrcp_controller_register_notification(connection, event_id); 1372 } 1373 1374 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1375 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1376 if (!connection){ 1377 log_error("avrcp_get_play_status: could not find a connection."); 1378 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1379 } 1380 connection->notifications_to_deregister |= (1 << event_id); 1381 return ERROR_CODE_SUCCESS; 1382 } 1383 1384 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1385 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1386 if (!connection){ 1387 log_error("avrcp_unit_info: could not find a connection."); 1388 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1389 } 1390 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1391 connection->state = AVCTP_W2_SEND_COMMAND; 1392 1393 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1394 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 1395 connection->command_type = AVRCP_CTYPE_STATUS; 1396 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1397 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1398 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1399 connection->cmd_operands_length = 5; 1400 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1401 return ERROR_CODE_SUCCESS; 1402 } 1403 1404 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1405 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1406 if (!connection){ 1407 log_error("avrcp_unit_info: could not find a connection."); 1408 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1409 } 1410 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1411 connection->state = AVCTP_W2_SEND_COMMAND; 1412 1413 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1414 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 1415 connection->command_type = AVRCP_CTYPE_STATUS; 1416 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 1417 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 1418 memset(connection->cmd_operands, 0xFF, connection->cmd_operands_length); 1419 connection->cmd_operands[0] = 7; // page: 0, extention_code: 7 1420 connection->cmd_operands_length = 5; 1421 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1422 return ERROR_CODE_SUCCESS; 1423 } 1424 1425 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1426 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1427 if (!connection){ 1428 log_error("avrcp_get_capabilities: could not find a connection."); 1429 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1430 } 1431 if (connection->state != AVCTP_CONNECTION_OPENED){ 1432 return ERROR_CODE_COMMAND_DISALLOWED; 1433 } 1434 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY); 1435 return ERROR_CODE_SUCCESS; 1436 } 1437 1438 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1439 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1440 if (!connection){ 1441 log_error("avrcp_get_capabilities: could not find a connection."); 1442 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1443 } 1444 if (connection->state != AVCTP_CONNECTION_OPENED){ 1445 return ERROR_CODE_COMMAND_DISALLOWED; 1446 } 1447 1448 if (!connection->remote_supported_notifications_queried){ 1449 connection->remote_supported_notifications_queried = true; 1450 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 1451 return ERROR_CODE_SUCCESS; 1452 } 1453 1454 avrcp_controller_emit_supported_events(connection); 1455 return ERROR_CODE_SUCCESS; 1456 } 1457 1458 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1459 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1460 if (!connection){ 1461 log_error("avrcp_get_play_status: could not find a connection."); 1462 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1463 } 1464 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1465 connection->state = AVCTP_W2_SEND_COMMAND; 1466 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1467 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1468 connection->command_type = AVRCP_CTYPE_STATUS; 1469 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1470 connection->subunit_id = AVRCP_SUBUNIT_ID; 1471 big_endian_store_24(connection->cmd_operands, 0, BT_SIG_COMPANY_ID); 1472 connection->cmd_operands[3] = AVRCP_PDU_ID_GET_PLAY_STATUS; 1473 connection->cmd_operands[4] = 0; // reserved(upper 6) | packet_type -> 0 1474 big_endian_store_16(connection->cmd_operands, 5, 0); // parameter length 1475 connection->cmd_operands_length = 7; 1476 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1477 return ERROR_CODE_SUCCESS; 1478 } 1479 1480 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1481 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1482 if (!connection){ 1483 log_error("avrcp_get_capabilities: could not find a connection."); 1484 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1485 } 1486 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1487 connection->state = AVCTP_W2_SEND_COMMAND; 1488 1489 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1490 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1491 connection->command_type = AVRCP_CTYPE_CONTROL; 1492 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1493 connection->subunit_id = AVRCP_SUBUNIT_ID; 1494 int pos = 0; 1495 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1496 pos += 3; 1497 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; // PDU ID 1498 connection->cmd_operands[pos++] = 0; 1499 1500 // Parameter Length 1501 big_endian_store_16(connection->cmd_operands, pos, 2); 1502 pos += 2; 1503 1504 big_endian_store_16(connection->cmd_operands, pos, addressed_player_id); 1505 pos += 2; 1506 1507 connection->cmd_operands_length = pos; 1508 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1509 return ERROR_CODE_SUCCESS; 1510 } 1511 1512 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 1513 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1514 if (!connection){ 1515 log_error("avrcp_get_capabilities: could not find a connection."); 1516 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1517 } 1518 1519 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1520 1521 if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) { 1522 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1523 } 1524 connection->state = AVCTP_W2_SEND_COMMAND; 1525 1526 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1527 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1528 connection->command_type = AVRCP_CTYPE_STATUS; 1529 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1530 connection->subunit_id = AVRCP_SUBUNIT_ID; 1531 int pos = 0; 1532 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1533 pos += 3; 1534 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; // PDU ID 1535 connection->cmd_operands[pos++] = 0; 1536 1537 // Parameter Length 1538 big_endian_store_16(connection->cmd_operands, pos, 9); 1539 pos += 2; 1540 1541 // write 8 bytes value 1542 memset(connection->cmd_operands + pos, 0, 8); // identifier: PLAYING 1543 pos += 8; 1544 1545 connection->cmd_operands[pos++] = num_attributes; // attribute count, if 0 get all attributes 1546 1547 int i; 1548 for (i = 0; i < num_attributes; i++){ 1549 // every attribute is 4 bytes long 1550 big_endian_store_32(connection->cmd_operands, pos, attributes[i]); 1551 pos += 4; 1552 } 1553 1554 connection->cmd_operands_length = pos; 1555 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1556 return ERROR_CODE_SUCCESS; 1557 } 1558 1559 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1560 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1561 } 1562 1563 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1564 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1565 if (!connection){ 1566 log_error("avrcp_get_capabilities: could not find a connection."); 1567 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1568 } 1569 1570 // 1571 // allow sending of multiple set abs volume commands without waiting for response 1572 // 1573 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1574 switch (connection->state){ 1575 case AVCTP_CONNECTION_OPENED: 1576 status = ERROR_CODE_SUCCESS; 1577 break; 1578 case AVCTP_W2_RECEIVE_RESPONSE: 1579 // - is pending response also set abs volume 1580 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1581 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1582 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1583 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1584 if (connection->cmd_operands[3] != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1585 // - is next transaction id valid in window 1586 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1587 status = ERROR_CODE_SUCCESS; 1588 break; 1589 default: 1590 break; 1591 } 1592 if (status != ERROR_CODE_SUCCESS) return status; 1593 1594 connection->state = AVCTP_W2_SEND_COMMAND; 1595 1596 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1597 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1598 connection->command_type = AVRCP_CTYPE_CONTROL; 1599 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1600 connection->subunit_id = AVRCP_SUBUNIT_ID; 1601 int pos = 0; 1602 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1603 pos += 3; 1604 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME; // PDU ID 1605 connection->cmd_operands[pos++] = 0; 1606 1607 // Parameter Length 1608 big_endian_store_16(connection->cmd_operands, pos, 1); 1609 pos += 2; 1610 connection->cmd_operands[pos++] = volume; 1611 1612 connection->cmd_operands_length = pos; 1613 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1614 return ERROR_CODE_SUCCESS; 1615 } 1616 1617 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1618 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1619 if (!connection){ 1620 log_error("avrcp_get_capabilities: could not find a 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 log_error("avrcp_get_capabilities: could not find a connection."); 1650 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1651 } 1652 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1653 connection->state = AVCTP_W2_SEND_COMMAND; 1654 1655 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1656 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1657 connection->command_type = AVRCP_CTYPE_CONTROL; 1658 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1659 connection->subunit_id = AVRCP_SUBUNIT_ID; 1660 int pos = 0; 1661 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1662 pos += 3; 1663 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE; // PDU ID 1664 connection->cmd_operands[pos++] = 0; 1665 // Parameter Length 1666 big_endian_store_16(connection->cmd_operands, pos, 3); 1667 pos += 2; 1668 connection->cmd_operands[pos++] = 2; 1669 connection->cmd_operands_length = pos; 1670 connection->cmd_operands[pos++] = attr_id; 1671 connection->cmd_operands[pos++] = attr_value; 1672 connection->cmd_operands_length = pos; 1673 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1674 return ERROR_CODE_SUCCESS; 1675 } 1676 1677 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1678 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1679 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1680 } 1681 1682 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1683 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1684 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1685 } 1686 1687 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1688 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1689 if (!connection){ 1690 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1691 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1692 } 1693 if (connection->state != AVCTP_CONNECTION_OPENED){ 1694 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1695 return ERROR_CODE_COMMAND_DISALLOWED; 1696 } 1697 connection->state = AVCTP_W2_SEND_COMMAND; 1698 1699 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1700 connection->command_type = AVRCP_CTYPE_CONTROL; 1701 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1702 connection->subunit_id = AVRCP_SUBUNIT_ID; 1703 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1704 int pos = 0; 1705 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1706 pos += 3; 1707 connection->cmd_operands[pos++] = AVRCP_PDU_ID_PLAY_ITEM; // PDU ID 1708 // reserved 1709 connection->cmd_operands[pos++] = 0; 1710 // Parameter Length 1711 big_endian_store_16(connection->cmd_operands, pos, 11); 1712 pos += 2; 1713 connection->cmd_operands[pos++] = scope; 1714 memset(&connection->cmd_operands[pos], 0, 8); 1715 if (uid){ 1716 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1717 } 1718 pos += 8; 1719 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1720 pos += 2; 1721 connection->cmd_operands_length = pos; 1722 1723 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1724 return ERROR_CODE_SUCCESS; 1725 } 1726 1727 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){ 1728 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1729 if (!connection){ 1730 log_error("Could not find a connection with cid 0%02x.", avrcp_cid); 1731 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1732 } 1733 if (connection->state != AVCTP_CONNECTION_OPENED){ 1734 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1735 return ERROR_CODE_COMMAND_DISALLOWED; 1736 } 1737 connection->state = AVCTP_W2_SEND_COMMAND; 1738 1739 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1740 connection->command_type = AVRCP_CTYPE_CONTROL; 1741 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 1742 connection->subunit_id = AVRCP_SUBUNIT_ID; 1743 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 1744 int pos = 0; 1745 big_endian_store_24(connection->cmd_operands, pos, BT_SIG_COMPANY_ID); 1746 pos += 3; 1747 connection->cmd_operands[pos++] = AVRCP_PDU_ID_ADD_TO_NOW_PLAYING; // PDU ID 1748 // reserved 1749 connection->cmd_operands[pos++] = 0; 1750 // Parameter Length 1751 big_endian_store_16(connection->cmd_operands, pos, 11); 1752 pos += 2; 1753 connection->cmd_operands[pos++] = scope; 1754 memset(&connection->cmd_operands[pos], 0, 8); 1755 if (uid){ 1756 (void)memcpy(&connection->cmd_operands[pos], uid, 8); 1757 } 1758 pos += 8; 1759 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 1760 pos += 2; 1761 connection->cmd_operands_length = pos; 1762 1763 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1764 return ERROR_CODE_SUCCESS; 1765 } 1766 1767 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1768 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1769 if (!connection){ 1770 log_error("avrcp_controller_play_item: could not find a connection."); 1771 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1772 } 1773 connection->max_num_fragments = max_num_fragments; 1774 return ERROR_CODE_SUCCESS; 1775 } 1776 1777 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){ 1778 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1779 if (!connection){ 1780 log_error("avrcp_controller_play_item: could not find a connection."); 1781 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1782 } 1783 1784 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1785 connection->state = AVCTP_W2_SEND_FRAGMENTED_COMMAND; 1786 1787 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 1788 connection->command_opcode = command_opcode; 1789 connection->command_type = command_type; 1790 connection->subunit_type = subunit_type; 1791 connection->subunit_id = subunit_id; 1792 connection->cmd_operands_fragmented_buffer = command_buffer; 1793 connection->cmd_operands_fragmented_pos = 0; 1794 connection->cmd_operands_fragmented_len = command_len; 1795 1796 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1797 return ERROR_CODE_SUCCESS; 1798 } 1799