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