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