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 51 static const char * avrcp_default_controller_service_name = "AVRCP Controller"; 52 static const char * avrcp_default_controller_service_provider_name = "BlueKitchen"; 53 54 // made public in avrcp_controller.h 55 avrcp_context_t avrcp_controller_context; 56 57 static uint8_t avrcp_controller_calc_next_transaction_label(uint8_t current_transaction_label){ 58 current_transaction_label++; 59 if (current_transaction_label == 16){ 60 current_transaction_label = 1; 61 } 62 return current_transaction_label; 63 } 64 65 static uint8_t avrcp_controller_get_next_transaction_label(avrcp_connection_t * connection){ 66 connection->transaction_id_counter = avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter); 67 return connection->transaction_id_counter; 68 } 69 70 static bool avrcp_controller_is_transaction_id_valid(avrcp_connection_t * connection, uint8_t transaction_id){ 71 uint8_t delta = ((int8_t) transaction_id - connection->controller_last_confirmed_transaction_id) & 0x0f; 72 return delta < 15; 73 } 74 75 static void avrcp_controller_custom_command_data_init(avrcp_connection_t * connection, 76 avrcp_command_opcode_t opcode, avrcp_command_type_t command_type, 77 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 78 avrcp_pdu_id_t pdu_id, uint32_t company_id){ 79 80 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 81 connection->command_opcode = opcode; 82 connection->command_type = command_type; 83 connection->subunit_type = subunit_type; 84 connection->subunit_id = subunit_id; 85 connection->company_id = company_id; 86 connection->pdu_id = pdu_id; 87 connection->data = NULL; 88 connection->data_offset = 0; 89 connection->data_len = 0; 90 } 91 92 static void avrcp_controller_vendor_dependent_command_data_init(avrcp_connection_t * connection, avrcp_command_type_t command_type, avrcp_pdu_id_t pdu_id, bool get_next_transaction_label){ 93 if (get_next_transaction_label){ 94 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 95 } 96 connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 97 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 98 connection->subunit_id = AVRCP_SUBUNIT_ID; 99 connection->company_id = BT_SIG_COMPANY_ID; 100 101 connection->command_type = command_type; 102 connection->pdu_id = pdu_id; 103 connection->data = connection->message_body; 104 connection->data_offset = 0; 105 connection->data_len = 0; 106 } 107 108 static void avrcp_controller_pass_through_command_data_init(avrcp_connection_t * connection, avrcp_operation_id_t opid){ 109 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 110 connection->command_opcode = AVRCP_CMD_OPCODE_PASS_THROUGH; 111 connection->command_type = AVRCP_CTYPE_CONTROL; 112 connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; 113 connection->subunit_id = AVRCP_SUBUNIT_ID; 114 115 connection->company_id = 0; 116 connection->pdu_id = AVRCP_PDU_ID_UNDEFINED; 117 connection->operation_id = opid; 118 119 connection->data = connection->message_body; 120 connection->data_offset = 0; 121 connection->data_len = 0; 122 } 123 124 static int avrcp_controller_supports_browsing(uint16_t controller_supported_features){ 125 return controller_supported_features & AVRCP_FEATURE_MASK_BROWSING; 126 } 127 128 static void avrcp_controller_prepare_custom_command_response(avrcp_connection_t * connection, uint16_t response_len, uint8_t * in_place_buffer){ 129 uint8_t pos = 0; 130 in_place_buffer[pos++] = HCI_EVENT_AVRCP_META; 131 // skip len 132 pos++; 133 in_place_buffer[pos++] = AVRCP_SUBEVENT_CUSTOM_COMMAND_RESPONSE; 134 little_endian_store_16(in_place_buffer, pos, connection->avrcp_cid); 135 pos += 2; 136 in_place_buffer[pos++] = (uint8_t)connection->command_type; 137 in_place_buffer[pos++] = (uint8_t)connection->pdu_id; 138 little_endian_store_16(in_place_buffer, pos, response_len); 139 pos += 2; 140 in_place_buffer[1] = pos + response_len - 2; 141 } 142 143 static void avrcp_controller_emit_notification_complete(avrcp_connection_t * connection, uint8_t status, uint8_t event_id, bool enabled){ 144 uint8_t event[8]; 145 uint8_t pos = 0; 146 event[pos++] = HCI_EVENT_AVRCP_META; 147 event[pos++] = sizeof(event) - 2; 148 event[pos++] = AVRCP_SUBEVENT_NOTIFICATION_STATE; 149 little_endian_store_16(event, pos, connection->avrcp_cid); 150 pos += 2; 151 event[pos++] = status; 152 event[pos++] = enabled ? 1 : 0; 153 event[pos++] = event_id; 154 UNUSED(pos); 155 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 156 } 157 158 static void avrcp_controller_emit_supported_events(avrcp_connection_t * connection){ 159 uint8_t ctype = (uint8_t) AVRCP_CTYPE_RESPONSE_CHANGED_STABLE; 160 uint8_t event_id; 161 162 for (event_id = (uint8_t) AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 163 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 164 continue; 165 } 166 uint8_t event[8]; 167 uint8_t pos = 0; 168 event[pos++] = HCI_EVENT_AVRCP_META; 169 event[pos++] = sizeof(event) - 2; 170 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID; 171 little_endian_store_16(event, pos, connection->avrcp_cid); 172 pos += 2; 173 event[pos++] = ctype; 174 event[pos++] = 0; 175 event[pos++] = event_id; 176 UNUSED(pos); 177 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 178 } 179 180 uint8_t event[7]; 181 uint8_t pos = 0; 182 event[pos++] = HCI_EVENT_AVRCP_META; 183 event[pos++] = sizeof(event) - 2; 184 event[pos++] = AVRCP_SUBEVENT_GET_CAPABILITY_EVENT_ID_DONE; 185 little_endian_store_16(event, pos, connection->avrcp_cid); 186 pos += 2; 187 event[pos++] = ctype; 188 event[pos++] = 0; 189 UNUSED(pos); 190 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 191 } 192 193 static void avrcp_controller_emit_notification_for_event_id(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id, 194 avrcp_command_type_t ctype, const uint8_t *payload, 195 uint16_t size) { 196 switch (event_id){ 197 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_POS_CHANGED:{ 198 if (size < 4) break; 199 uint32_t song_position = big_endian_read_32(payload, 0); 200 uint16_t offset = 0; 201 uint8_t event[10]; 202 event[offset++] = HCI_EVENT_AVRCP_META; 203 event[offset++] = sizeof(event) - 2; 204 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_POS_CHANGED; 205 little_endian_store_16(event, offset, avrcp_cid); 206 offset += 2; 207 event[offset++] = ctype; 208 little_endian_store_32(event, offset, song_position); 209 offset += 4; 210 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 211 break; 212 } 213 case AVRCP_NOTIFICATION_EVENT_PLAYBACK_STATUS_CHANGED:{ 214 if (size < 1) break; 215 uint16_t offset = 0; 216 uint8_t event[7]; 217 event[offset++] = HCI_EVENT_AVRCP_META; 218 event[offset++] = sizeof(event) - 2; 219 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_PLAYBACK_STATUS_CHANGED; 220 little_endian_store_16(event, offset, avrcp_cid); 221 offset += 2; 222 event[offset++] = ctype; 223 event[offset++] = payload[0]; 224 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 225 break; 226 } 227 case AVRCP_NOTIFICATION_EVENT_TRACK_CHANGED:{ 228 uint16_t offset = 0; 229 uint8_t event[6]; 230 event[offset++] = HCI_EVENT_AVRCP_META; 231 event[offset++] = sizeof(event) - 2; 232 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_TRACK_CHANGED; 233 little_endian_store_16(event, offset, avrcp_cid); 234 offset += 2; 235 event[offset++] = ctype; 236 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 237 break; 238 } 239 case AVRCP_NOTIFICATION_EVENT_NOW_PLAYING_CONTENT_CHANGED:{ 240 uint16_t offset = 0; 241 uint8_t event[6]; 242 event[offset++] = HCI_EVENT_AVRCP_META; 243 event[offset++] = sizeof(event) - 2; 244 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_NOW_PLAYING_CONTENT_CHANGED; 245 little_endian_store_16(event, offset, avrcp_cid); 246 offset += 2; 247 event[offset++] = ctype; 248 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 249 break; 250 } 251 case AVRCP_NOTIFICATION_EVENT_AVAILABLE_PLAYERS_CHANGED:{ 252 uint16_t offset = 0; 253 uint8_t event[6]; 254 event[offset++] = HCI_EVENT_AVRCP_META; 255 event[offset++] = sizeof(event) - 2; 256 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_AVAILABLE_PLAYERS_CHANGED; 257 little_endian_store_16(event, offset, avrcp_cid); 258 offset += 2; 259 event[offset++] = ctype; 260 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 261 break; 262 } 263 case AVRCP_NOTIFICATION_EVENT_VOLUME_CHANGED:{ 264 if (size < 1) break; 265 uint16_t offset = 0; 266 uint8_t event[7]; 267 event[offset++] = HCI_EVENT_AVRCP_META; 268 event[offset++] = sizeof(event) - 2; 269 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_VOLUME_CHANGED; 270 little_endian_store_16(event, offset, avrcp_cid); 271 offset += 2; 272 event[offset++] = ctype; 273 event[offset++] = payload[0] & 0x7F; 274 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 275 break; 276 } 277 case AVRCP_NOTIFICATION_EVENT_UIDS_CHANGED:{ 278 if (size < 2) break; 279 uint8_t event[8]; 280 uint16_t offset = 0; 281 uint16_t uuid = big_endian_read_16(payload, 0); 282 event[offset++] = HCI_EVENT_AVRCP_META; 283 event[offset++] = sizeof(event) - 2; 284 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_UIDS_CHANGED; 285 little_endian_store_16(event, offset, avrcp_cid); 286 offset += 2; 287 event[offset++] = ctype; 288 little_endian_store_16(event, offset, uuid); 289 offset += 2; 290 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 291 break; 292 } 293 294 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_END:{ 295 uint16_t offset = 0; 296 uint8_t event[6]; 297 event[offset++] = HCI_EVENT_AVRCP_META; 298 event[offset++] = sizeof(event) - 2; 299 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_END; 300 little_endian_store_16(event, offset, avrcp_cid); 301 offset += 2; 302 event[offset++] = ctype; 303 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 304 break; 305 } 306 case AVRCP_NOTIFICATION_EVENT_TRACK_REACHED_START:{ 307 uint16_t offset = 0; 308 uint8_t event[6]; 309 event[offset++] = HCI_EVENT_AVRCP_META; 310 event[offset++] = sizeof(event) - 2; 311 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_TRACK_REACHED_START; 312 little_endian_store_16(event, offset, avrcp_cid); 313 offset += 2; 314 event[offset++] = ctype; 315 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 316 break; 317 } 318 case AVRCP_NOTIFICATION_EVENT_BATT_STATUS_CHANGED:{ 319 if (size < 1) break; 320 uint16_t offset = 0; 321 uint8_t event[7]; 322 event[offset++] = HCI_EVENT_AVRCP_META; 323 event[offset++] = sizeof(event) - 2; 324 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_BATT_STATUS_CHANGED; 325 little_endian_store_16(event, offset, avrcp_cid); 326 offset += 2; 327 event[offset++] = ctype; 328 event[offset++] = payload[0]; 329 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 330 break; 331 } 332 333 case AVRCP_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED:{ 334 if (size < 1) break; 335 uint16_t offset = 0; 336 uint8_t event[7]; 337 event[offset++] = HCI_EVENT_AVRCP_META; 338 event[offset++] = sizeof(event) - 2; 339 event[offset++] = AVRCP_SUBEVENT_NOTIFICATION_EVENT_SYSTEM_STATUS_CHANGED; 340 little_endian_store_16(event, offset, avrcp_cid); 341 offset += 2; 342 event[offset++] = ctype; 343 event[offset++] = payload[0]; 344 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 345 break; 346 } 347 348 case AVRCP_NOTIFICATION_EVENT_PLAYER_APPLICATION_SETTING_CHANGED: 349 default: 350 log_info("avrcp: not implemented"); 351 break; 352 } 353 } 354 355 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){ 356 btstack_assert(callback != NULL); 357 358 uint8_t event[8]; 359 int pos = 0; 360 event[pos++] = HCI_EVENT_AVRCP_META; 361 event[pos++] = sizeof(event) - 2; 362 event[pos++] = AVRCP_SUBEVENT_SHUFFLE_AND_REPEAT_MODE; 363 little_endian_store_16(event, pos, avrcp_cid); 364 pos += 2; 365 event[pos++] = ctype; 366 event[pos++] = repeat_mode; 367 event[pos++] = shuffle_mode; 368 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 369 } 370 371 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){ 372 uint8_t event[7]; 373 int pos = 0; 374 event[pos++] = HCI_EVENT_AVRCP_META; 375 event[pos++] = sizeof(event) - 2; 376 event[pos++] = AVRCP_SUBEVENT_NOW_PLAYING_INFO_DONE; 377 little_endian_store_16(event, pos, avrcp_cid); 378 pos += 2; 379 event[pos++] = ctype; 380 event[pos++] = status; 381 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 382 } 383 384 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){ 385 uint8_t event[HCI_EVENT_BUFFER_SIZE]; 386 uint16_t pos = 0; 387 event[pos++] = HCI_EVENT_AVRCP_META; 388 // reserve one byte for subevent type and data len 389 uint16_t data_len_pos = pos; 390 pos++; 391 uint16_t subevent_type_pos = pos; 392 pos++; 393 little_endian_store_16(event, pos, avrcp_cid); 394 pos += 2; 395 event[pos++] = ctype; 396 397 switch (attr_id){ 398 case AVRCP_MEDIA_ATTR_TITLE: 399 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TITLE_INFO; 400 btstack_assert(value_len <= 255); 401 event[pos++] = (uint8_t) 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 btstack_assert(value_len <= 255); 407 event[pos++] = (uint8_t) value_len; 408 (void)memcpy(event + pos, value, value_len); 409 break; 410 case AVRCP_MEDIA_ATTR_ALBUM: 411 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_ALBUM_INFO; 412 btstack_assert(value_len <= 255); 413 event[pos++] = (uint8_t) value_len; 414 (void)memcpy(event + pos, value, value_len); 415 break; 416 case AVRCP_MEDIA_ATTR_GENRE: 417 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_GENRE_INFO; 418 btstack_assert(value_len <= 255); 419 event[pos++] = (uint8_t) value_len; 420 (void)memcpy(event + pos, value, value_len); 421 break; 422 #ifdef ENABLE_AVRCP_COVER_ART 423 case AVRCP_MEDIA_ATTR_DEFAULT_COVER_ART: 424 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_COVER_ART_INFO; 425 btstack_assert(value_len <= 255); 426 event[pos++] = (uint8_t) value_len; 427 (void)memcpy(event + pos, value, value_len); 428 break; 429 #endif 430 case AVRCP_MEDIA_ATTR_SONG_LENGTH_MS: 431 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_SONG_LENGTH_MS_INFO; 432 if (value){ 433 little_endian_store_32(event, pos, btstack_atoi((char *)value)); 434 } else { 435 little_endian_store_32(event, pos, 0); 436 } 437 pos += 4; 438 break; 439 case AVRCP_MEDIA_ATTR_TRACK: 440 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TRACK_INFO; 441 if (value){ 442 event[pos++] = btstack_atoi((char *)value); 443 } else { 444 event[pos++] = 0; 445 } 446 break; 447 case AVRCP_MEDIA_ATTR_TOTAL_NUM_ITEMS: 448 event[subevent_type_pos] = AVRCP_SUBEVENT_NOW_PLAYING_TOTAL_TRACKS_INFO; 449 if (value){ 450 event[pos++] = btstack_atoi((char *)value); 451 } else { 452 event[pos++] = 0; 453 } 454 break; 455 default: 456 break; 457 } 458 event[data_len_pos] = pos - 2; 459 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 460 } 461 462 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){ 463 btstack_assert(callback != NULL); 464 465 uint8_t event[7]; 466 int pos = 0; 467 event[pos++] = HCI_EVENT_AVRCP_META; 468 event[pos++] = sizeof(event) - 2; 469 event[pos++] = subevent; 470 little_endian_store_16(event, pos, avrcp_cid); 471 pos += 2; 472 event[pos++] = ctype; 473 event[pos++] = operation_id; 474 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 475 } 476 477 static void avrcp_parser_reset(avrcp_connection_t * connection){ 478 connection->list_offset = 0; 479 connection->parser_attribute_header_pos = 0; 480 connection->controller_num_received_fragments = 0; 481 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 482 } 483 484 static void avrcp_parser_process_byte(uint8_t byte, avrcp_connection_t * connection, avrcp_command_type_t ctype){ 485 uint16_t attribute_total_value_len; 486 uint32_t attribute_id; 487 switch(connection->parser_state){ 488 case AVRCP_PARSER_GET_ATTRIBUTE_HEADER: 489 connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte; 490 connection->list_offset++; 491 492 if (connection->parser_attribute_header_pos < AVRCP_ATTRIBUTE_HEADER_LEN) return; 493 494 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 495 connection->attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTE_SIZE); 496 if (connection->attribute_value_len > 0){ 497 // get ready for attribute value 498 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE; 499 return; 500 } 501 502 // emit empty attribute 503 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 504 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); 505 506 // done, see below 507 break; 508 509 case AVRCP_PARSER_GET_ATTRIBUTE_VALUE: 510 connection->attribute_value[connection->attribute_value_offset++] = byte; 511 connection->list_offset++; 512 513 if (connection->attribute_value_offset < connection->attribute_value_len) return; 514 515 // emit (potentially partial) attribute 516 attribute_id = big_endian_read_32(connection->parser_attribute_header, 0); 517 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); 518 519 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 520 if (connection->attribute_value_offset < attribute_total_value_len){ 521 // ignore rest of attribute 522 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE; 523 return; 524 } 525 526 // done, see below 527 break; 528 529 case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE: 530 connection->attribute_value_offset++; 531 connection->list_offset++; 532 533 attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 6); 534 if (connection->attribute_value_offset < attribute_total_value_len) return; 535 536 // done, see below 537 break; 538 539 default: 540 return; 541 } 542 543 // attribute fully read, check if more to come 544 if (connection->list_offset < connection->list_size){ 545 // more to come, reset parser 546 connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER; 547 connection->parser_attribute_header_pos = 0; 548 connection->attribute_value_offset = 0; 549 } else { 550 // fully done 551 avrcp_parser_reset(connection); 552 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 553 } 554 } 555 556 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){ 557 int i; 558 for (i=0;i<num_bytes_to_read;i++){ 559 avrcp_parser_process_byte(packet[i], connection, ctype); 560 } 561 } 562 563 static void avrcp_send_cmd_with_avctp_fragmentation(avrcp_connection_t * connection){ 564 l2cap_reserve_packet_buffer(); 565 uint8_t * packet = l2cap_get_outgoing_buffer(); 566 567 uint16_t max_payload_size; 568 connection->avctp_packet_type = avctp_get_packet_type(connection, &max_payload_size); 569 connection->avrcp_packet_type = avrcp_get_packet_type(connection); 570 571 // non-fragmented: transport header (1) + PID (2) 572 // fragmented: transport header (1) + num packets (1) + PID (2) 573 574 uint16_t param_len = connection->data_len; 575 // AVCTP header 576 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 577 uint16_t pos = 0; 578 packet[pos++] = (connection->transaction_id << 4) | (connection->avctp_packet_type << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 579 580 if (connection->avctp_packet_type == AVCTP_START_PACKET){ 581 uint16_t max_frame_size = btstack_min(connection->l2cap_mtu, AVRCP_MAX_AV_C_MESSAGE_FRAME_SIZE); 582 // first packet: max_payload_size 583 // rest packets 584 uint16_t num_payload_bytes = param_len - max_payload_size; 585 uint16_t frame_size_for_continue_packet = max_frame_size - avctp_get_num_bytes_for_header(AVCTP_CONTINUE_PACKET); 586 uint16_t num_avctp_packets = (num_payload_bytes + frame_size_for_continue_packet - 1)/frame_size_for_continue_packet + 1; 587 btstack_assert(num_avctp_packets <= 255); 588 packet[pos++] = (uint8_t) num_avctp_packets; 589 } 590 591 switch (connection->avctp_packet_type){ 592 case AVCTP_SINGLE_PACKET: 593 case AVCTP_START_PACKET: 594 connection->data_offset = 0; 595 // Profile IDentifier (PID) 596 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 597 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 598 599 // command_type 600 packet[pos++] = connection->command_type; 601 // subunit_type | subunit ID 602 packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 603 // opcode 604 packet[pos++] = (uint8_t)connection->command_opcode; 605 606 switch (connection->command_opcode){ 607 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 608 big_endian_store_24(packet, pos, connection->company_id); 609 pos += 3; 610 packet[pos++] = connection->pdu_id; 611 packet[pos++] = connection->avrcp_packet_type; // reserved(upper 6) | AVRCP packet_type 612 big_endian_store_16(packet, pos, connection->data_len); // parameter length 613 pos += 2; 614 break; 615 case AVRCP_CMD_OPCODE_PASS_THROUGH: 616 packet[pos++] = connection->operation_id; 617 packet[pos++] = (uint8_t)connection->data_len; // parameter length 618 break; 619 case AVRCP_CMD_OPCODE_UNIT_INFO: 620 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 621 break; 622 default: 623 btstack_assert(false); 624 return; 625 } 626 break; 627 case AVCTP_CONTINUE_PACKET: 628 case AVCTP_END_PACKET: 629 break; 630 default: 631 btstack_assert(false); 632 return; 633 } 634 // compare number of bytes to store with the remaining buffer size 635 uint16_t bytes_to_copy = btstack_min(connection->data_len - connection->data_offset, max_payload_size - pos); 636 637 (void)memcpy(packet + pos, &connection->data[connection->data_offset], bytes_to_copy); 638 pos += bytes_to_copy; 639 connection->data_offset += bytes_to_copy; 640 641 l2cap_send_prepared(connection->l2cap_signaling_cid, pos); 642 } 643 644 static int avrcp_send_register_notification(avrcp_connection_t * connection, uint8_t event_id){ 645 uint8_t command[18]; 646 uint16_t pos = 0; 647 // transport header : transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 648 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 649 command[pos++] = (connection->transaction_id << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 650 651 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 652 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 653 command[pos++] = AVRCP_CTYPE_NOTIFY; 654 command[pos++] = (AVRCP_SUBUNIT_TYPE_PANEL << 3) | AVRCP_SUBUNIT_ID; 655 command[pos++] = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT; 656 657 big_endian_store_24(command, pos, BT_SIG_COMPANY_ID); 658 pos += 3; 659 command[pos++] = AVRCP_PDU_ID_REGISTER_NOTIFICATION; 660 command[pos++] = 0; // reserved(upper 6) | packet_type -> 0 661 big_endian_store_16(command, pos, 5); // parameter length 662 pos += 2; 663 command[pos++] = event_id; 664 big_endian_store_32(command, pos, 1); // send notification on playback position every second, for other notifications it is ignored 665 pos += 4; 666 return l2cap_send(connection->l2cap_signaling_cid, command, pos); 667 } 668 669 static void avrcp_press_and_hold_timeout_handler(btstack_timer_source_t * timer){ 670 UNUSED(timer); 671 avrcp_connection_t * connection = (avrcp_connection_t*) btstack_run_loop_get_timer_context(timer); 672 btstack_run_loop_set_timer(&connection->controller_press_and_hold_cmd_timer, 2000); // 2 seconds timeout 673 btstack_run_loop_add_timer(&connection->controller_press_and_hold_cmd_timer); 674 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 675 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 676 } 677 678 static void avrcp_press_and_hold_timer_start(avrcp_connection_t * connection){ 679 btstack_run_loop_remove_timer(&connection->controller_press_and_hold_cmd_timer); 680 btstack_run_loop_set_timer_handler(&connection->controller_press_and_hold_cmd_timer, avrcp_press_and_hold_timeout_handler); 681 btstack_run_loop_set_timer_context(&connection->controller_press_and_hold_cmd_timer, connection); 682 btstack_run_loop_set_timer(&connection->controller_press_and_hold_cmd_timer, 2000); // 2 seconds timeout 683 btstack_run_loop_add_timer(&connection->controller_press_and_hold_cmd_timer); 684 } 685 686 static void avrcp_press_and_hold_timer_stop(avrcp_connection_t * connection){ 687 connection->controller_press_and_hold_cmd_active = false; 688 btstack_run_loop_remove_timer(&connection->controller_press_and_hold_cmd_timer); 689 } 690 691 692 static uint8_t avrcp_controller_request_pass_through_release_control_cmd(avrcp_connection_t * connection){ 693 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 694 if (connection->controller_press_and_hold_cmd_active){ 695 avrcp_press_and_hold_timer_stop(connection); 696 } 697 connection->operation_id = (avrcp_operation_id_t)(0x80 | connection->operation_id); 698 connection->transaction_id = avrcp_controller_get_next_transaction_label(connection); 699 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 700 return ERROR_CODE_SUCCESS; 701 } 702 703 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){ 704 UNUSED(playback_speed); 705 706 log_info("Send command %d", opid); 707 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 708 if (!connection){ 709 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 710 } 711 712 if (connection->state != AVCTP_CONNECTION_OPENED){ 713 log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid); 714 return ERROR_CODE_COMMAND_DISALLOWED; 715 } 716 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 717 avrcp_controller_pass_through_command_data_init(connection, opid); 718 719 connection->controller_press_and_hold_cmd_active = continuous_cmd; 720 if (connection->controller_press_and_hold_cmd_active){ 721 avrcp_press_and_hold_timer_start(connection); 722 } 723 724 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 725 return ERROR_CODE_SUCCESS; 726 } 727 728 static uint8_t request_single_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 729 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, false); 730 } 731 732 static uint8_t request_continuous_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid, uint16_t playback_speed){ 733 return avrcp_controller_request_pass_through_press_control_cmd(avrcp_cid, opid, playback_speed, true); 734 } 735 736 static void avrcp_controller_get_capabilities_for_connection(avrcp_connection_t * connection, uint8_t capability_id){ 737 connection->state = AVCTP_W2_SEND_COMMAND; 738 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CAPABILITIES, true); 739 740 // Parameter Length 741 connection->data_len = 1; 742 connection->data[0] = capability_id; // capability ID 743 744 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 745 } 746 747 static uint8_t avrcp_controller_register_notification(avrcp_connection_t * connection, avrcp_notification_event_id_t event_id){ 748 if ((connection->remote_capabilities_state == AVRCP_REMOTE_CAPABILITIES_KNOWN) && (connection->notifications_supported_by_target & (1 << event_id)) == 0){ 749 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 750 } 751 if ((connection->controller_notifications_to_deregister & (1 << event_id)) != 0){ 752 return ERROR_CODE_COMMAND_DISALLOWED; 753 } 754 if ( (connection->notifications_enabled & (1 << event_id)) != 0){ 755 return ERROR_CODE_SUCCESS; 756 } 757 connection->controller_notifications_to_register |= (1 << event_id); 758 759 switch (connection->remote_capabilities_state){ 760 case AVRCP_REMOTE_CAPABILITIES_NONE: 761 connection->remote_capabilities_state = AVRCP_REMOTE_CAPABILITIES_W4_QUERY_RESULT; 762 connection->controller_notifications_supported_by_target_suppress_emit_result = true; 763 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 764 break; 765 case AVRCP_REMOTE_CAPABILITIES_KNOWN: 766 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 767 break; 768 default: 769 break; 770 } 771 return ERROR_CODE_SUCCESS; 772 } 773 774 static uint8_t avrcp_controller_request_continuation(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id){ 775 connection->state = AVCTP_W2_SEND_COMMAND; 776 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, pdu_id, false); 777 778 // Parameter Length 779 connection->data_len = 3; 780 big_endian_store_16(connection->data, 0, 1); 781 connection->data[2] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 782 783 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 784 return ERROR_CODE_SUCCESS; 785 } 786 787 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 788 return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 789 } 790 791 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 792 return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE); 793 } 794 795 static void avrcp_controller_handle_notification(avrcp_connection_t *connection, avrcp_command_type_t ctype, uint8_t *payload, uint16_t size) { 796 if (size < 1) return; 797 uint16_t pos = 0; 798 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++]; 799 if ( (event_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 800 return; 801 } 802 803 uint16_t event_mask = (1 << event_id); 804 uint16_t reset_event_mask = ~event_mask; 805 806 switch (ctype){ 807 case AVRCP_CTYPE_RESPONSE_REJECTED: 808 connection->controller_notifications_to_deregister &= reset_event_mask; 809 connection->controller_notifications_to_register &= reset_event_mask; 810 connection->controller_initial_status_reported &= reset_event_mask; 811 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, event_id, false); 812 return; 813 814 case AVRCP_CTYPE_RESPONSE_INTERIM: 815 // register as enabled 816 connection->notifications_enabled |= event_mask; 817 818 // check if initial value is already sent 819 if ((connection->controller_initial_status_reported & event_mask) != 0 ){ 820 return; 821 } 822 // emit event only once, initially 823 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, true); 824 connection->controller_initial_status_reported |= event_mask; 825 // emit initial value after this switch 826 break; 827 828 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 829 // received change, event is considered de-registered 830 // we are re-enabling it automatically, if it is not 831 // explicitly disabled 832 connection->notifications_enabled &= reset_event_mask; 833 if ((connection->controller_notifications_to_deregister & event_mask) == 0){ 834 avrcp_controller_register_notification(connection, event_id); 835 } else { 836 connection->controller_notifications_to_deregister &= reset_event_mask; 837 connection->controller_notifications_to_register &= reset_event_mask; 838 connection->controller_initial_status_reported &= reset_event_mask; 839 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, false); 840 } 841 break; 842 843 default: 844 return; 845 } 846 847 avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload + pos, size - pos); 848 } 849 850 #ifdef ENABLE_AVCTP_FRAGMENTATION 851 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 852 // after header (transaction label and packet type) 853 uint16_t pos; 854 uint16_t bytes_to_store; 855 856 switch (packet_type){ 857 case AVCTP_START_PACKET: 858 if (size < 2) return; 859 860 // store header 861 pos = 0; 862 connection->avctp_reassembly_buffer[pos] = packet[pos]; 863 pos++; 864 connection->avctp_reassembly_size = pos; 865 866 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 867 pos++; 868 869 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 870 871 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 872 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 873 connection->avctp_reassembly_size += bytes_to_store; 874 break; 875 876 case AVCTP_CONTINUE_PACKET: 877 case AVCTP_END_PACKET: 878 if (size < 1) return; 879 880 // store remaining data, ignore header 881 pos = 1; 882 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 883 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 884 connection->avctp_reassembly_size += bytes_to_store; 885 break; 886 887 default: 888 return; 889 } 890 } 891 #endif 892 893 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 894 if (size < 6u) return; 895 uint8_t pdu_id; 896 avrcp_packet_type_t vendor_dependent_avrcp_packet_type; 897 898 uint16_t pos = 0; 899 connection->controller_last_confirmed_transaction_id = packet[pos] >> 4; 900 avrcp_frame_type_t frame_type = (avrcp_frame_type_t)((packet[pos] >> 1) & 0x01); 901 avctp_packet_type_t packet_type = (avctp_packet_type_t)((packet[pos] >> 2) & 0x03); 902 pos++; 903 904 if (frame_type != AVRCP_RESPONSE_FRAME) return; 905 906 switch (packet_type){ 907 case AVCTP_SINGLE_PACKET: 908 break; 909 910 #ifdef ENABLE_AVCTP_FRAGMENTATION 911 case AVCTP_START_PACKET: 912 case AVCTP_CONTINUE_PACKET: 913 avctp_reassemble_message(connection, packet_type, packet, size); 914 return; 915 916 case AVCTP_END_PACKET: 917 avctp_reassemble_message(connection, packet_type, packet, size); 918 919 packet = connection->avctp_reassembly_buffer; 920 size = connection->avctp_reassembly_size; 921 break; 922 #endif 923 924 default: 925 return; 926 } 927 928 pos += 2; // PID 929 930 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 931 932 #ifdef ENABLE_LOG_INFO 933 uint8_t byte_value = packet[pos]; 934 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 935 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 936 #endif 937 pos++; 938 939 uint8_t opcode = packet[pos++]; 940 uint16_t param_length; 941 942 switch (opcode){ 943 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 944 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 945 connection->state = AVCTP_CONNECTION_OPENED; 946 947 #ifdef ENABLE_LOG_INFO 948 // page, extension code (1) 949 pos++; 950 uint8_t unit_type = packet[pos] >> 3; 951 uint8_t max_subunit_ID = packet[pos] & 0x07; 952 log_info("SUBUNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), target_unit_type 0x%02x, max_subunit_ID %d", ctype, subunit_type, subunit_id, opcode, unit_type, max_subunit_ID); 953 #endif 954 break; 955 } 956 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 957 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 958 connection->state = AVCTP_CONNECTION_OPENED; 959 960 #ifdef ENABLE_LOG_INFO 961 // byte value 7 (1) 962 pos++; 963 uint8_t unit_type = packet[pos] >> 3; 964 uint8_t unit = packet[pos] & 0x07; 965 pos++; 966 uint32_t company_id = big_endian_read_24(packet, pos); 967 log_info("UNIT INFO response: ctype 0x%02x (0C), subunit_type 0x%02x (1F), subunit_id 0x%02x (07), opcode 0x%02x (30), target_unit_type 0x%02x, unit %d, company_id 0x%06" PRIx32, 968 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 969 #endif 970 break; 971 } 972 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 973 if ((size - pos) < 7){ 974 return; 975 } 976 // Company ID (3) 977 pos += 3; 978 // PDU ID (1) 979 pdu_id = packet[pos++]; 980 // Packet Type (1) 981 vendor_dependent_avrcp_packet_type = (avrcp_packet_type_t)(packet[pos++] & 0x03); 982 // Param Length (2) 983 param_length = big_endian_read_16(packet, pos); 984 pos += 2; 985 986 if ((size - pos) < param_length) { 987 return; 988 } 989 990 // handle asynchronous notifications, without changing state 991 if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 992 avrcp_controller_handle_notification(connection, ctype, packet + pos, size - pos); 993 break; 994 } 995 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 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 avrcp_player_application_setting_attribute_id_t attribute_id = (avrcp_player_application_setting_attribute_id_t)packet[pos++]; 1007 uint8_t value = packet[pos++]; 1008 switch (attribute_id){ 1009 case AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_REPEAT_MODE_STATUS: 1010 repeat_mode = (avrcp_repeat_mode_t) value; 1011 break; 1012 case AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_SHUFFLE_STATUS: 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_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES:{ 1024 uint8_t num_attributes = packet[pos++]; 1025 int i; 1026 for (i = 0; i < num_attributes; i++){ 1027 avrcp_player_application_setting_attribute_id_t attribute_id = (avrcp_player_application_setting_attribute_id_t)packet[pos++]; 1028 log_info("TODO send event: attribute_id %d", attribute_id); 1029 } 1030 break; 1031 } 1032 1033 case AVRCP_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES:{ 1034 uint8_t num_setting_values = packet[pos++]; 1035 int i; 1036 for (i = 0; i < num_setting_values; i++){ 1037 uint8_t value = packet[pos++]; 1038 log_info("TODO send event: value %d", value); 1039 } 1040 break; 1041 } 1042 1043 case AVRCP_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT:{ 1044 uint8_t num_attributes = packet[pos++]; 1045 1046 int i; 1047 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 1048 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 1049 for (i = 0; i < num_attributes; i++){ 1050 avrcp_player_application_setting_attribute_id_t attribute_id = (avrcp_player_application_setting_attribute_id_t)packet[pos++]; 1051 uint8_t character_set_id = little_endian_read_16(packet, pos); 1052 pos += 2; 1053 uint8_t value_len = packet[pos++]; 1054 char * value = (char *)&packet[pos]; 1055 pos += value_len; 1056 1057 log_info("TODO send event: attribute_id %d, value_len %d, value %s", attribute_id, value_len, value); 1058 UNUSED(character_set_id); 1059 UNUSED(repeat_mode); 1060 UNUSED(shuffle_mode); 1061 switch (attribute_id){ 1062 case AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_REPEAT_MODE_STATUS: 1063 break; 1064 case AVRCP_PLAYER_APPLICATION_SETTING_ATTRIBUTE_ID_SHUFFLE_STATUS: 1065 break; 1066 default: 1067 break; 1068 } 1069 } 1070 break; 1071 } 1072 1073 case AVRCP_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT:{ 1074 uint8_t num_setting_values = packet[pos++]; 1075 int i; 1076 for (i = 0; i < num_setting_values; i++){ 1077 uint8_t value = packet[pos++]; 1078 log_info("TODO send event: value %d", value); 1079 } 1080 break; 1081 } 1082 1083 case AVRCP_PDU_ID_INFORM_DISPLAYABLE_CHARACTERSET: 1084 case AVRCP_PDU_ID_INFORM_BATTERY_STATUS_OF_CT: 1085 break; 1086 1087 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{ 1088 uint16_t offset = 0; 1089 uint8_t event[6]; 1090 event[offset++] = HCI_EVENT_AVRCP_META; 1091 event[offset++] = sizeof(event) - 2; 1092 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 1093 little_endian_store_16(event, offset, connection->avrcp_cid); 1094 offset += 2; 1095 event[offset++] = ctype; 1096 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1097 break; 1098 } 1099 1100 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 1101 uint16_t offset = 0; 1102 uint8_t event[7]; 1103 event[offset++] = HCI_EVENT_AVRCP_META; 1104 event[offset++] = sizeof(event) - 2; 1105 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 1106 little_endian_store_16(event, offset, connection->avrcp_cid); 1107 offset += 2; 1108 event[offset++] = ctype; 1109 event[offset++] = packet[pos++]; 1110 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1111 break; 1112 } 1113 1114 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1115 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 1116 uint8_t capability_count = 0; 1117 if (param_length > 1){ 1118 capability_count = packet[pos++]; 1119 } 1120 uint16_t i; 1121 uint16_t offset; 1122 uint8_t event[10]; 1123 1124 switch (capability_id){ 1125 1126 case AVRCP_CAPABILITY_ID_COMPANY: 1127 for (i = 0; (i < capability_count) && ((size - pos) >= 3); i++){ 1128 uint32_t company_id = big_endian_read_24(packet, pos); 1129 pos += 3; 1130 log_info(" 0x%06" PRIx32 ", ", company_id); 1131 1132 offset = 0; 1133 event[offset++] = HCI_EVENT_AVRCP_META; 1134 event[offset++] = sizeof(event) - 2; 1135 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID; 1136 little_endian_store_16(event, offset, connection->avrcp_cid); 1137 offset += 2; 1138 event[offset++] = ctype; 1139 event[offset++] = 0; 1140 little_endian_store_24(event, offset, company_id); 1141 offset += 3; 1142 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1143 } 1144 1145 offset = 0; 1146 event[offset++] = HCI_EVENT_AVRCP_META; 1147 event[offset++] = sizeof(event) - 2; 1148 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE; 1149 little_endian_store_16(event, offset, connection->avrcp_cid); 1150 offset += 2; 1151 event[offset++] = ctype; 1152 event[offset++] = 0; 1153 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1154 break; 1155 1156 case AVRCP_CAPABILITY_ID_EVENT: 1157 for (i = 0; (i < capability_count) && ((size - pos) >= 1); i++){ 1158 uint8_t event_id = packet[pos++]; 1159 connection->notifications_supported_by_target |= (1 << event_id); 1160 } 1161 1162 connection->remote_capabilities_state = AVRCP_REMOTE_CAPABILITIES_KNOWN; 1163 1164 // if the get supported events query is triggered by avrcp_controller_enable_notification call, 1165 // avrcp_controller_emit_supported_events should be suppressed 1166 if (connection->controller_notifications_supported_by_target_suppress_emit_result){ 1167 connection->controller_notifications_supported_by_target_suppress_emit_result = false; 1168 // also, notification might not be supported 1169 // if so, emit AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE event to app, 1170 // and update controller_notifications_to_register bitmap 1171 for (i = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++){ 1172 if ((connection->controller_notifications_to_register & (1 << i)) != 0){ 1173 if ((connection->notifications_supported_by_target & (1 << i)) == 0){ 1174 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, (uint8_t) i, false); 1175 connection->controller_notifications_to_register &= ~(1 << i); 1176 } 1177 } 1178 } 1179 break; 1180 } 1181 // supported events are emitted only if the get supported events query 1182 // is triggered by avrcp_controller_get_supported_events call 1183 avrcp_controller_emit_supported_events(connection); 1184 break; 1185 1186 default: 1187 // ignore 1188 break; 1189 } 1190 break; 1191 } 1192 1193 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 1194 uint32_t song_length = big_endian_read_32(packet, pos); 1195 pos += 4; 1196 uint32_t song_position = big_endian_read_32(packet, pos); 1197 pos += 4; 1198 uint8_t play_status = packet[pos]; 1199 1200 uint8_t event[15]; 1201 int offset = 0; 1202 event[offset++] = HCI_EVENT_AVRCP_META; 1203 event[offset++] = sizeof(event) - 2; 1204 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 1205 little_endian_store_16(event, offset, connection->avrcp_cid); 1206 offset += 2; 1207 event[offset++] = ctype; 1208 little_endian_store_32(event, offset, song_length); 1209 offset += 4; 1210 little_endian_store_32(event, offset, song_position); 1211 offset += 4; 1212 event[offset++] = play_status; 1213 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1214 break; 1215 } 1216 1217 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES: 1218 switch (vendor_dependent_avrcp_packet_type){ 1219 case AVRCP_START_PACKET: 1220 case AVRCP_SINGLE_PACKET: 1221 avrcp_parser_reset(connection); 1222 connection->list_size = param_length; 1223 // num_attributes 1224 pos++; 1225 1226 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1227 if (vendor_dependent_avrcp_packet_type == AVRCP_START_PACKET){ 1228 avrcp_controller_request_continue_response(connection); 1229 return; 1230 } 1231 break; 1232 case AVRCP_CONTINUE_PACKET: 1233 case AVRCP_END_PACKET: 1234 connection->controller_num_received_fragments++; 1235 1236 if (connection->controller_num_received_fragments < connection->controller_max_num_fragments){ 1237 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1238 1239 if (vendor_dependent_avrcp_packet_type == AVRCP_CONTINUE_PACKET){ 1240 avrcp_controller_request_continue_response(connection); 1241 return; 1242 } 1243 } else { 1244 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 1245 avrcp_parser_reset(connection); 1246 avrcp_controller_request_abort_continuation(connection); 1247 return; 1248 } 1249 break; 1250 default: 1251 btstack_assert(false); 1252 break; 1253 } 1254 break; 1255 1256 default: 1257 // custom command response comes here 1258 switch (pdu_id){ 1259 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1260 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 1261 break; 1262 default: 1263 if (pdu_id != connection->pdu_id) { 1264 break; 1265 } 1266 uint8_t *in_place_buffer = packet + pos - 9; 1267 avrcp_controller_prepare_custom_command_response(connection, param_length, 1268 in_place_buffer); 1269 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, in_place_buffer, 1270 param_length + 9); 1271 1272 break; 1273 } 1274 break; 1275 } 1276 break; 1277 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 1278 if ((size - pos) < 1) return; 1279 uint8_t operation_id = packet[pos++]; 1280 UNUSED(pos); 1281 switch (connection->state){ 1282 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1283 // trigger release for simple command: 1284 if (!connection->controller_press_and_hold_cmd_active){ 1285 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1286 break; 1287 } 1288 // for press and hold, send release if it just has been requested, otherwise, wait for next repeat 1289 if (connection->controller_press_and_hold_cmd_release){ 1290 connection->controller_press_and_hold_cmd_release = false; 1291 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1292 } else { 1293 connection->state = AVCTP_W4_STOP; 1294 } 1295 break; 1296 case AVCTP_W2_RECEIVE_RESPONSE: 1297 connection->state = AVCTP_CONNECTION_OPENED; 1298 break; 1299 default: 1300 break; 1301 } 1302 if (connection->state == AVCTP_W4_STOP){ 1303 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 1304 } 1305 if (connection->state == AVCTP_CONNECTION_OPENED) { 1306 // RELEASE response 1307 operation_id = operation_id & 0x7F; 1308 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 1309 } 1310 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 1311 // PRESS response 1312 avrcp_controller_request_pass_through_release_control_cmd(connection); 1313 } 1314 break; 1315 } 1316 default: 1317 break; 1318 } 1319 1320 // trigger pending notification reqistrations 1321 if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->controller_notifications_to_register){ 1322 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1323 } 1324 } 1325 1326 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 1327 // identifier (9) + num attributes (1) + attributes (4 * num attributes) 1328 uint8_t get_element_attributes_command[9+8*((AVRCP_MEDIA_ATTR_NUM-AVRCP_MEDIA_ATTR_TITLE) + 1)]; 1329 uint16_t pos; 1330 uint16_t num_attributes_index; 1331 uint8_t i; 1332 1333 switch (connection->state){ 1334 case AVCTP_W2_SEND_PRESS_COMMAND: 1335 avrcp_send_cmd_with_avctp_fragmentation(connection); 1336 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 1337 return; 1338 1339 case AVCTP_W2_SEND_RELEASE_COMMAND: 1340 avrcp_send_cmd_with_avctp_fragmentation(connection); 1341 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1342 return; 1343 1344 case AVCTP_W2_SEND_COMMAND: 1345 avrcp_send_cmd_with_avctp_fragmentation(connection); 1346 if (connection->data_offset < connection->data_len){ 1347 // continue AVCTP fragmentation 1348 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1349 return; 1350 } 1351 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1352 return; 1353 case AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST: 1354 // build command in local buffer 1355 pos = 0; 1356 connection->data = get_element_attributes_command; 1357 // write identifier 1358 memset(connection->data, 0, 8); 1359 pos += 8; 1360 num_attributes_index = pos; 1361 // If num_attributes is set to zero, all attribute information shall be returned, 1362 // and the AttributeID field is omitted 1363 connection->data[num_attributes_index] = 0; 1364 pos++; 1365 for (i = 0; i < (uint8_t)AVRCP_MEDIA_ATTR_NUM - (uint8_t)AVRCP_MEDIA_ATTR_TITLE; i++){ 1366 if ((connection->controller_element_attributes & (1<<i)) != 0){ 1367 // every attribute is 4 bytes long 1368 big_endian_store_32(connection->data, pos, AVRCP_MEDIA_ATTR_TITLE + i); 1369 pos += 4; 1370 connection->data[num_attributes_index]++; 1371 } 1372 } 1373 1374 // Parameter Length 1375 connection->data_len = pos; 1376 1377 log_info("AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST, len %u", connection->data_len); 1378 1379 // send data - assume it will fit into one packet 1380 avrcp_send_cmd_with_avctp_fragmentation(connection); 1381 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1382 return; 1383 default: 1384 break; 1385 } 1386 1387 // send register notification if queued, 1388 // avrcp_handle_l2cap_data_packet_for_signaling_connection will trigger next one 1389 if (connection->controller_notifications_to_register != 0){ 1390 uint8_t event_id; 1391 for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 1392 if (connection->controller_notifications_to_register & (1 << event_id)){ 1393 connection->controller_notifications_to_register &= ~ (1 << event_id); 1394 avrcp_send_register_notification(connection, event_id); 1395 return; 1396 } 1397 } 1398 } 1399 } 1400 1401 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1402 avrcp_connection_t * connection; 1403 1404 switch (packet_type) { 1405 case L2CAP_DATA_PACKET: 1406 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1407 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1408 break; 1409 1410 case HCI_EVENT_PACKET: 1411 switch (hci_event_packet_get_type(packet)){ 1412 case L2CAP_EVENT_CAN_SEND_NOW: 1413 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1414 avrcp_controller_handle_can_send_now(connection); 1415 break; 1416 default: 1417 break; 1418 } 1419 default: 1420 break; 1421 } 1422 } 1423 1424 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){ 1425 if (service_name == NULL){ 1426 service_name = avrcp_default_controller_service_name; 1427 } 1428 if (service_provider_name == NULL){ 1429 service_provider_name = avrcp_default_controller_service_provider_name; 1430 } 1431 avrcp_create_sdp_record(true, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 1432 } 1433 1434 void avrcp_controller_init(void){ 1435 avrcp_controller_context.role = AVRCP_CONTROLLER; 1436 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1437 avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler); 1438 } 1439 1440 void avrcp_controller_deinit(void){ 1441 memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t)); 1442 } 1443 1444 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 1445 btstack_assert(callback != NULL); 1446 avrcp_controller_context.avrcp_callback = callback; 1447 } 1448 1449 1450 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1451 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1452 } 1453 1454 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1455 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1456 } 1457 1458 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1459 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1460 } 1461 1462 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1463 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1464 } 1465 1466 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1467 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1468 } 1469 1470 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1471 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1472 } 1473 1474 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1475 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1476 } 1477 1478 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1479 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1480 } 1481 1482 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1483 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1484 } 1485 1486 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1487 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1488 } 1489 1490 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1491 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1492 } 1493 1494 /* start continuous cmds */ 1495 1496 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){ 1497 return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0); 1498 } 1499 1500 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1501 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1502 } 1503 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1504 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1505 } 1506 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1507 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1508 } 1509 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1510 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1511 } 1512 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1513 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1514 } 1515 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1516 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1517 } 1518 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1519 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1520 } 1521 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1522 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1523 } 1524 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1525 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1526 } 1527 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1528 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1529 } 1530 1531 uint8_t avrcp_controller_0(uint16_t avrcp_cid){ 1532 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_0, 0); 1533 } 1534 uint8_t avrcp_controller_1(uint16_t avrcp_cid){ 1535 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_1, 0); 1536 } 1537 uint8_t avrcp_controller_2(uint16_t avrcp_cid){ 1538 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_2, 0); 1539 } 1540 uint8_t avrcp_controller_3(uint16_t avrcp_cid){ 1541 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_3, 0); 1542 } 1543 uint8_t avrcp_controller_4(uint16_t avrcp_cid){ 1544 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_4, 0); 1545 } 1546 uint8_t avrcp_controller_5(uint16_t avrcp_cid){ 1547 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_5, 0); 1548 } 1549 uint8_t avrcp_controller_6(uint16_t avrcp_cid){ 1550 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_6, 0); 1551 } 1552 uint8_t avrcp_controller_7(uint16_t avrcp_cid){ 1553 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_7, 0); 1554 } 1555 uint8_t avrcp_controller_8(uint16_t avrcp_cid){ 1556 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_8, 0); 1557 } 1558 uint8_t avrcp_controller_9(uint16_t avrcp_cid){ 1559 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_9, 0); 1560 } 1561 uint8_t avrcp_controller_dot(uint16_t avrcp_cid){ 1562 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_DOT, 0); 1563 } 1564 uint8_t avrcp_controller_enter(uint16_t avrcp_cid){ 1565 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_ENTER, 0); 1566 } 1567 uint8_t avrcp_controller_clear(uint16_t avrcp_cid){ 1568 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_CLEAR, 0); 1569 } 1570 uint8_t avrcp_controller_channel_up(uint16_t avrcp_cid){ 1571 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_CHANNEL_UP, 0); 1572 } 1573 uint8_t avrcp_controller_channel_down(uint16_t avrcp_cid){ 1574 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_CHANNEL_DOWN, 0); 1575 } 1576 uint8_t avrcp_controller_previous_channel(uint16_t avrcp_cid){ 1577 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PREVIOUS_CHANNEL, 0); 1578 } 1579 uint8_t avrcp_controller_sound_select(uint16_t avrcp_cid){ 1580 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SOUND_SELECT, 0); 1581 } 1582 uint8_t avrcp_controller_input_select(uint16_t avrcp_cid){ 1583 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_INPUT_SELECT, 0); 1584 } 1585 uint8_t avrcp_controller_display_information(uint16_t avrcp_cid){ 1586 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_DISPLAY_INFORMATION, 0); 1587 } 1588 uint8_t avrcp_controller_help(uint16_t avrcp_cid){ 1589 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_HELP, 0); 1590 } 1591 uint8_t avrcp_controller_power(uint16_t avrcp_cid){ 1592 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_POWER, 0); 1593 } 1594 uint8_t avrcp_controller_angle(uint16_t avrcp_cid){ 1595 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_ANGLE, 0); 1596 } 1597 uint8_t avrcp_controller_subpicture(uint16_t avrcp_cid){ 1598 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SUBPICTURE, 0); 1599 } 1600 uint8_t avrcp_controller_F1(uint16_t avrcp_cid){ 1601 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_F1, 0); 1602 } 1603 uint8_t avrcp_controller_F2(uint16_t avrcp_cid){ 1604 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_F2, 0); 1605 } 1606 uint8_t avrcp_controller_F3(uint16_t avrcp_cid){ 1607 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_F3, 0); 1608 } 1609 uint8_t avrcp_controller_F4(uint16_t avrcp_cid){ 1610 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_F4, 0); 1611 } 1612 uint8_t avrcp_controller_F5(uint16_t avrcp_cid){ 1613 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_F5, 0); 1614 } 1615 1616 uint8_t avrcp_controller_select(uint16_t avrcp_cid){ 1617 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SELECT, 0); 1618 } 1619 uint8_t avrcp_controller_up(uint16_t avrcp_cid){ 1620 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_UP, 0); 1621 } 1622 uint8_t avrcp_controller_down(uint16_t avrcp_cid){ 1623 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_DOWN, 0); 1624 } 1625 uint8_t avrcp_controller_left(uint16_t avrcp_cid){ 1626 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_LEFT, 0); 1627 } 1628 uint8_t avrcp_controller_right(uint16_t avrcp_cid){ 1629 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_RIGHT, 0); 1630 } 1631 uint8_t avrcp_controller_right_up(uint16_t avrcp_cid){ 1632 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_RIGHT_UP, 0); 1633 } 1634 uint8_t avrcp_controller_right_down(uint16_t avrcp_cid){ 1635 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_RIGHT_DOWN, 0); 1636 } 1637 uint8_t avrcp_controller_left_up(uint16_t avrcp_cid){ 1638 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_LEFT_UP, 0); 1639 } 1640 uint8_t avrcp_controller_left_down(uint16_t avrcp_cid){ 1641 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_LEFT_DOWN, 0); 1642 } 1643 uint8_t avrcp_controller_root_menu(uint16_t avrcp_cid){ 1644 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_ROOT_MENU, 0); 1645 } 1646 uint8_t avrcp_controller_setup_menu(uint16_t avrcp_cid){ 1647 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SETUP_MENU, 0); 1648 } 1649 uint8_t avrcp_controller_contents_menu(uint16_t avrcp_cid){ 1650 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_CONTENTS_MENU, 0); 1651 } 1652 uint8_t avrcp_controller_favorite_menu(uint16_t avrcp_cid){ 1653 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAVORITE_MENU, 0); 1654 } 1655 uint8_t avrcp_controller_exit(uint16_t avrcp_cid){ 1656 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_EXIT, 0); 1657 } 1658 uint8_t avrcp_controller_page_up(uint16_t avrcp_cid){ 1659 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAGE_UP, 0); 1660 } 1661 uint8_t avrcp_controller_page_down(uint16_t avrcp_cid){ 1662 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAGE_DOWN, 0); 1663 } 1664 1665 static uint8_t avrcp_controller_request_group_pass_through_press_control_cmd(uint16_t avrcp_cid, avrcp_group_operation_id_t opid){ 1666 log_info("Send group command %d", opid); 1667 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1668 if (!connection){ 1669 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1670 } 1671 1672 if (connection->state != AVCTP_CONNECTION_OPENED){ 1673 log_error("Connection in wrong state %d, expected %d. avrcp cid 0x%02x", connection->state, AVCTP_CONNECTION_OPENED, avrcp_cid); 1674 return ERROR_CODE_COMMAND_DISALLOWED; 1675 } 1676 connection->state = AVCTP_W2_SEND_PRESS_COMMAND; 1677 avrcp_controller_pass_through_command_data_init(connection, AVRCP_OPERATION_ID_VENDOR_UNIQUE); 1678 connection->company_id = BT_SIG_COMPANY_ID; 1679 connection->data_len = 5; 1680 big_endian_store_24(connection->data, 0, BT_SIG_COMPANY_ID); 1681 big_endian_store_16(connection->data, 3, opid); 1682 1683 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1684 return ERROR_CODE_SUCCESS; 1685 } 1686 1687 uint8_t avrcp_controller_next_group(uint16_t avrcp_cid){ 1688 return avrcp_controller_request_group_pass_through_press_control_cmd(avrcp_cid, AVRCP_GROUP_OPERATION_ID_GOTO_NEXT); 1689 } 1690 1691 uint8_t avrcp_controller_previous_group(uint16_t avrcp_cid){ 1692 return avrcp_controller_request_group_pass_through_press_control_cmd(avrcp_cid, AVRCP_GROUP_OPERATION_ID_GOTO_PREVIOUS); 1693 } 1694 1695 1696 /* stop continuous cmds */ 1697 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1698 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1699 if (!connection){ 1700 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1701 } 1702 1703 switch (connection->state){ 1704 // respond when we receive response for (repeated) press command 1705 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1706 connection->controller_press_and_hold_cmd_release = true; 1707 break; 1708 1709 // release already sent or on the way, nothing to do 1710 case AVCTP_W2_RECEIVE_RESPONSE: 1711 case AVCTP_W2_SEND_RELEASE_COMMAND: 1712 break; 1713 1714 // about to send next repeated press command or wait for it -> release right away 1715 case AVCTP_W2_SEND_PRESS_COMMAND: 1716 case AVCTP_W4_STOP: 1717 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1718 1719 // otherwise reject request 1720 default: 1721 return ERROR_CODE_COMMAND_DISALLOWED; 1722 } 1723 return ERROR_CODE_SUCCESS; 1724 } 1725 1726 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1727 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1728 if (!connection){ 1729 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1730 } 1731 return avrcp_controller_register_notification(connection, event_id); 1732 } 1733 1734 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1735 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1736 if (!connection){ 1737 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1738 } 1739 if (connection->remote_capabilities_state != AVRCP_REMOTE_CAPABILITIES_KNOWN){ 1740 return ERROR_CODE_COMMAND_DISALLOWED; 1741 } 1742 1743 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 1744 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1745 } 1746 1747 if ((connection->notifications_enabled & (1 << event_id)) == 0){ 1748 return ERROR_CODE_SUCCESS; 1749 } 1750 1751 connection->controller_notifications_to_deregister |= (1 << event_id); 1752 return ERROR_CODE_SUCCESS; 1753 } 1754 1755 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1756 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1757 if (!connection){ 1758 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1759 } 1760 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1761 1762 connection->state = AVCTP_W2_SEND_COMMAND; 1763 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_STATUS, 1764 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1765 0); 1766 1767 connection->data = connection->message_body; 1768 memset(connection->data, 0xFF, 5); 1769 connection->data_len = 5; 1770 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1771 return ERROR_CODE_SUCCESS; 1772 } 1773 1774 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1775 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1776 if (!connection){ 1777 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1778 } 1779 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1780 1781 connection->state = AVCTP_W2_SEND_COMMAND; 1782 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, AVRCP_CTYPE_STATUS, 1783 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1784 0); 1785 1786 connection->data = connection->message_body; 1787 memset(connection->data, 0xFF, 5); 1788 connection->data[0] = 7; // page: 0, extension_code: 7 1789 connection->data_len = 5; 1790 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1791 return ERROR_CODE_SUCCESS; 1792 } 1793 1794 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1795 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1796 if (!connection){ 1797 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1798 } 1799 if (connection->state != AVCTP_CONNECTION_OPENED){ 1800 return ERROR_CODE_COMMAND_DISALLOWED; 1801 } 1802 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY); 1803 return ERROR_CODE_SUCCESS; 1804 } 1805 1806 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1807 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1808 if (!connection){ 1809 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1810 } 1811 if (connection->state != AVCTP_CONNECTION_OPENED){ 1812 return ERROR_CODE_COMMAND_DISALLOWED; 1813 } 1814 1815 switch (connection->remote_capabilities_state){ 1816 case AVRCP_REMOTE_CAPABILITIES_NONE: 1817 connection->remote_capabilities_state = AVRCP_REMOTE_CAPABILITIES_W4_QUERY_RESULT; 1818 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 1819 break; 1820 case AVRCP_REMOTE_CAPABILITIES_KNOWN: 1821 avrcp_controller_emit_supported_events(connection); 1822 break; 1823 default: 1824 break; 1825 } 1826 return ERROR_CODE_SUCCESS; 1827 } 1828 1829 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1830 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1831 if (!connection){ 1832 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1833 } 1834 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1835 1836 connection->state = AVCTP_W2_SEND_COMMAND; 1837 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAY_STATUS, true); 1838 1839 connection->data_len = 0; 1840 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1841 return ERROR_CODE_SUCCESS; 1842 } 1843 1844 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1845 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1846 if (!connection){ 1847 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1848 } 1849 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1850 1851 connection->state = AVCTP_W2_SEND_COMMAND; 1852 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ADDRESSED_PLAYER, true); 1853 1854 connection->data_len = 2; 1855 big_endian_store_16(connection->data, 0, addressed_player_id); 1856 1857 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1858 return ERROR_CODE_SUCCESS; 1859 } 1860 1861 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 1862 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1863 if (!connection){ 1864 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1865 } 1866 1867 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1868 1869 if (num_attributes >= AVRCP_MEDIA_ATTR_NUM) { 1870 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1871 } 1872 1873 connection->state = AVCTP_W2_SEND_GET_ELEMENT_ATTRIBUTES_REQUEST; 1874 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES, true); 1875 1876 // collect element attributes 1877 uint8_t i; 1878 uint16_t attributes_set = 0; 1879 for (i = 0; i < num_attributes; i++){ 1880 // ignore invalid attribute ID and "get all attributes" 1881 if (AVRCP_MEDIA_ATTR_ALL < attributes[i] && attributes[i] < AVRCP_MEDIA_ATTR_NUM){ 1882 attributes_set |= 1 << (attributes[i] - AVRCP_MEDIA_ATTR_TITLE); 1883 } 1884 } 1885 connection->controller_element_attributes = attributes_set; 1886 1887 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1888 return ERROR_CODE_SUCCESS; 1889 } 1890 1891 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1892 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1893 } 1894 1895 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){ 1896 if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){ 1897 return avrcp_controller_get_now_playing_info(avrcp_cid); 1898 } 1899 avrcp_media_attribute_id_t media_attrs[1]; 1900 media_attrs[0] = media_attribute_id; 1901 return avrcp_controller_get_element_attributes(avrcp_cid, 1, media_attrs); 1902 } 1903 1904 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1905 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1906 if (!connection){ 1907 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1908 } 1909 1910 // 1911 // allow sending of multiple set abs volume commands without waiting for response 1912 // 1913 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1914 switch (connection->state){ 1915 case AVCTP_CONNECTION_OPENED: 1916 status = ERROR_CODE_SUCCESS; 1917 break; 1918 case AVCTP_W2_RECEIVE_RESPONSE: 1919 // - is pending response also set abs volume 1920 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1921 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1922 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1923 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1924 if (connection->pdu_id != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1925 // - is next transaction id valid in window 1926 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1927 status = ERROR_CODE_SUCCESS; 1928 break; 1929 default: 1930 break; 1931 } 1932 if (status != ERROR_CODE_SUCCESS) return status; 1933 1934 connection->state = AVCTP_W2_SEND_COMMAND; 1935 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME, true); 1936 1937 // Parameter Length 1938 connection->data_len = 1; 1939 connection->data[0] = volume; 1940 1941 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1942 return ERROR_CODE_SUCCESS; 1943 } 1944 1945 uint8_t avrcp_controller_query_player_application_setting_attributes(uint16_t avrcp_cid){ 1946 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1947 if (!connection){ 1948 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1949 } 1950 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1951 1952 connection->state = AVCTP_W2_SEND_COMMAND; 1953 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES, true); 1954 1955 connection->data_len = 0; 1956 1957 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1958 return ERROR_CODE_SUCCESS; 1959 } 1960 1961 uint8_t avrcp_controller_query_player_application_setting_values(uint16_t avrcp_cid, avrcp_player_application_setting_attribute_id_t attribute_id){ 1962 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1963 if (!connection){ 1964 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1965 } 1966 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1967 1968 connection->state = AVCTP_W2_SEND_COMMAND; 1969 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES, true); 1970 1971 connection->data_len = 1; 1972 connection->data[0] = attribute_id; 1973 1974 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1975 return ERROR_CODE_SUCCESS; 1976 } 1977 1978 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1979 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1980 if (!connection){ 1981 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1982 } 1983 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1984 1985 connection->state = AVCTP_W2_SEND_COMMAND; 1986 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, true); 1987 1988 connection->data_len = 5; 1989 connection->data[0] = 4; // NumPlayerApplicationSettingAttributeID 1990 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1991 connection->data[1] = 0x01; // equalizer (1-OFF, 2-ON) 1992 connection->data[2] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1993 connection->data[3] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1994 connection->data[4] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1995 1996 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1997 return ERROR_CODE_SUCCESS; 1998 } 1999 2000 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 2001 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2002 if (!connection){ 2003 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2004 } 2005 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2006 2007 connection->state = AVCTP_W2_SEND_COMMAND; 2008 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE, true); 2009 2010 // Parameter Length 2011 connection->data_len = 3; 2012 connection->data[0] = 2; 2013 connection->data[1] = attr_id; 2014 connection->data[2] = attr_value; 2015 2016 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2017 return ERROR_CODE_SUCCESS; 2018 } 2019 2020 uint8_t avrcp_controller_query_player_application_setting_attribute_text(uint16_t avrcp_cid){ 2021 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2022 if (!connection){ 2023 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2024 } 2025 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2026 2027 connection->state = AVCTP_W2_SEND_COMMAND; 2028 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT, true); 2029 2030 connection->data_len = 5; 2031 connection->data[0] = 4; // NumPlayerApplicationSettingAttributeID 2032 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 2033 connection->data[1] = 0x01; // equalizer (1-OFF, 2-ON) 2034 connection->data[2] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 2035 connection->data[3] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 2036 connection->data[4] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 2037 2038 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2039 return ERROR_CODE_SUCCESS; 2040 } 2041 2042 uint8_t avrcp_controller_query_player_application_setting_value_text(uint16_t avrcp_cid){ 2043 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2044 if (!connection){ 2045 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2046 } 2047 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2048 2049 connection->state = AVCTP_W2_SEND_COMMAND; 2050 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT, true); 2051 2052 connection->data_len = 5; 2053 connection->data[0] = 4; // NumPlayerApplicationSettingAttributeID 2054 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 2055 connection->data[1] = 0x01; // equalizer (1-OFF, 2-ON) 2056 connection->data[2] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 2057 connection->data[3] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 2058 connection->data[4] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 2059 2060 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2061 return ERROR_CODE_SUCCESS; 2062 } 2063 2064 uint8_t avrcp_controller_inform_displayable_characterset(uint16_t avrcp_cid, uint8_t character_set_num, uint16_t * character_set){ 2065 btstack_assert(character_set_num > 4); 2066 2067 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2068 if (!connection){ 2069 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2070 } 2071 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2072 2073 connection->state = AVCTP_W2_SEND_COMMAND; 2074 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_INFORM_DISPLAYABLE_CHARACTERSET, true); 2075 2076 // Parameter Length 2077 connection->data_len = character_set_num * 2; 2078 uint8_t pos = 0; 2079 2080 connection->data[pos++] = character_set_num; 2081 uint8_t i; 2082 for (i = 0; i < character_set_num; i++){ 2083 little_endian_store_16(connection->data, pos, character_set[i]); 2084 pos += i * 2; 2085 } 2086 2087 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2088 return ERROR_CODE_SUCCESS; 2089 } 2090 2091 uint8_t avrcp_controller_inform_battery_status(uint16_t avrcp_cid, avrcp_battery_status_t battery_status){ 2092 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2093 if (!connection){ 2094 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2095 } 2096 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2097 2098 connection->state = AVCTP_W2_SEND_COMMAND; 2099 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_INFORM_BATTERY_STATUS_OF_CT, true); 2100 2101 // Parameter Length 2102 connection->data_len = 1; 2103 connection->data[0] = battery_status; 2104 2105 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2106 return ERROR_CODE_SUCCESS; 2107 } 2108 2109 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 2110 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2111 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 2112 } 2113 2114 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 2115 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2116 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 2117 } 2118 2119 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 2120 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2121 if (!connection){ 2122 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2123 } 2124 if (connection->state != AVCTP_CONNECTION_OPENED){ 2125 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 2126 return ERROR_CODE_COMMAND_DISALLOWED; 2127 } 2128 connection->state = AVCTP_W2_SEND_COMMAND; 2129 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_PLAY_ITEM, true); 2130 2131 // Parameter Length 2132 connection->data_len = 11; 2133 connection->data[0] = scope; 2134 memset(&connection->data[1], 0, 8); 2135 if (uid){ 2136 (void)memcpy(&connection->data[1], uid, 8); 2137 } 2138 big_endian_store_16(connection->data, 9, uid_counter); 2139 2140 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2141 return ERROR_CODE_SUCCESS; 2142 } 2143 2144 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){ 2145 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2146 if (!connection){ 2147 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2148 } 2149 if (connection->state != AVCTP_CONNECTION_OPENED){ 2150 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 2151 return ERROR_CODE_COMMAND_DISALLOWED; 2152 } 2153 2154 connection->state = AVCTP_W2_SEND_COMMAND; 2155 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_ADD_TO_NOW_PLAYING, true); 2156 2157 // Parameter Length 2158 connection->data_len = 11; 2159 connection->data[0] = scope; 2160 memset(&connection->data[1], 0, 8); 2161 if (uid){ 2162 (void)memcpy(&connection->data[1], uid, 8); 2163 } 2164 big_endian_store_16(connection->data, 9, uid_counter); 2165 2166 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2167 return ERROR_CODE_SUCCESS; 2168 } 2169 2170 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 2171 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2172 if (!connection){ 2173 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2174 } 2175 connection->controller_max_num_fragments = max_num_fragments; 2176 return ERROR_CODE_SUCCESS; 2177 } 2178 2179 2180 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, 2181 avrcp_command_type_t command_type, 2182 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 2183 avrcp_pdu_id_t pdu_id, uint32_t company_id, 2184 const uint8_t * data, uint16_t data_len){ 2185 2186 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2187 if (!connection){ 2188 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2189 } 2190 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 2191 2192 connection->state = AVCTP_W2_SEND_COMMAND; 2193 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT, command_type, subunit_type, 2194 subunit_id, pdu_id, company_id); 2195 2196 connection->data = (uint8_t *)data; 2197 connection->data_len = data_len; 2198 2199 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2200 return ERROR_CODE_SUCCESS; 2201 } 2202 2203 uint8_t avrcp_controller_force_send_press_cmd(uint16_t avrcp_cid, avrcp_operation_id_t opid){ 2204 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 2205 if (!connection){ 2206 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2207 } 2208 if (connection->state < AVCTP_CONNECTION_OPENED){ 2209 return ERROR_CODE_COMMAND_DISALLOWED; 2210 } 2211 2212 connection->state = AVCTP_W2_SEND_COMMAND; 2213 avrcp_controller_pass_through_command_data_init(connection, opid); 2214 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 2215 return ERROR_CODE_SUCCESS; 2216 }