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 break; 1177 1178 default: 1179 // custom command response comes here 1180 switch (pdu_id){ 1181 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1182 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 1183 break; 1184 default: 1185 if (pdu_id != connection->pdu_id) { 1186 break; 1187 } 1188 uint8_t *in_place_buffer = packet + pos - 9; 1189 avrcp_controller_prepare_custom_command_response(connection, param_length, 1190 in_place_buffer); 1191 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, in_place_buffer, 1192 param_length + 9); 1193 1194 break; 1195 } 1196 break; 1197 } 1198 break; 1199 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 1200 if ((size - pos) < 1) return; 1201 uint8_t operation_id = packet[pos++]; 1202 switch (connection->state){ 1203 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1204 // trigger release for simple command: 1205 if (!connection->controller_press_and_hold_cmd_active){ 1206 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1207 break; 1208 } 1209 // for press and hold, send release if it just has been requested, otherwise, wait for next repeat 1210 if (connection->controller_press_and_hold_cmd_release){ 1211 connection->controller_press_and_hold_cmd_release = false; 1212 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1213 } else { 1214 connection->state = AVCTP_W4_STOP; 1215 } 1216 break; 1217 case AVCTP_W2_RECEIVE_RESPONSE: 1218 connection->state = AVCTP_CONNECTION_OPENED; 1219 break; 1220 default: 1221 break; 1222 } 1223 if (connection->state == AVCTP_W4_STOP){ 1224 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 1225 } 1226 if (connection->state == AVCTP_CONNECTION_OPENED) { 1227 // RELEASE response 1228 operation_id = operation_id & 0x7F; 1229 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 1230 } 1231 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 1232 // PRESS response 1233 avrcp_controller_request_pass_through_release_control_cmd(connection); 1234 } 1235 break; 1236 } 1237 default: 1238 break; 1239 } 1240 1241 // trigger pending notification reqistrations 1242 if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->controller_notifications_to_register){ 1243 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1244 } 1245 } 1246 1247 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 1248 switch (connection->state){ 1249 case AVCTP_W2_SEND_PRESS_COMMAND: 1250 avrcp_send_cmd_with_avctp_fragmentation(connection); 1251 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 1252 break; 1253 1254 case AVCTP_W2_SEND_RELEASE_COMMAND: 1255 avrcp_send_cmd_with_avctp_fragmentation(connection); 1256 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1257 break; 1258 1259 case AVCTP_W2_SEND_COMMAND: 1260 avrcp_send_cmd_with_avctp_fragmentation(connection); 1261 if (connection->data_offset < connection->data_len){ 1262 // continue AVCTP fragmentation 1263 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1264 return; 1265 } 1266 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1267 return; 1268 default: 1269 break; 1270 } 1271 1272 // send register notification if queued 1273 if (connection->controller_notifications_to_register != 0){ 1274 uint8_t event_id; 1275 for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 1276 if (connection->controller_notifications_to_register & (1 << event_id)){ 1277 connection->controller_notifications_to_register &= ~ (1 << event_id); 1278 avrcp_send_register_notification(connection, event_id); 1279 return; 1280 } 1281 } 1282 } 1283 } 1284 1285 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1286 avrcp_connection_t * connection; 1287 1288 switch (packet_type) { 1289 case L2CAP_DATA_PACKET: 1290 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1291 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1292 break; 1293 1294 case HCI_EVENT_PACKET: 1295 switch (hci_event_packet_get_type(packet)){ 1296 case L2CAP_EVENT_CAN_SEND_NOW: 1297 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1298 avrcp_controller_handle_can_send_now(connection); 1299 break; 1300 default: 1301 break; 1302 } 1303 default: 1304 break; 1305 } 1306 } 1307 1308 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){ 1309 avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 1310 } 1311 1312 void avrcp_controller_init(void){ 1313 avrcp_controller_context.role = AVRCP_CONTROLLER; 1314 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1315 avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler); 1316 } 1317 1318 void avrcp_controller_deinit(void){ 1319 memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t)); 1320 } 1321 1322 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 1323 btstack_assert(callback != NULL); 1324 avrcp_controller_context.avrcp_callback = callback; 1325 } 1326 1327 1328 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1329 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1330 } 1331 1332 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1333 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1334 } 1335 1336 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1337 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1338 } 1339 1340 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1341 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1342 } 1343 1344 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1345 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1346 } 1347 1348 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1349 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1350 } 1351 1352 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1353 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1354 } 1355 1356 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1357 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1358 } 1359 1360 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1361 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1362 } 1363 1364 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1365 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1366 } 1367 1368 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1369 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1370 } 1371 1372 /* start continuous cmds */ 1373 1374 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){ 1375 return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0); 1376 } 1377 1378 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1379 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1380 } 1381 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1382 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1383 } 1384 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1385 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1386 } 1387 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1388 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1389 } 1390 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1391 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1392 } 1393 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1394 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1395 } 1396 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1397 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1398 } 1399 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1400 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1401 } 1402 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1403 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1404 } 1405 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1406 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1407 } 1408 1409 /* stop continuous cmds */ 1410 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1411 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1412 if (!connection){ 1413 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1414 } 1415 1416 switch (connection->state){ 1417 // respond when we receive response for (repeated) press command 1418 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1419 connection->controller_press_and_hold_cmd_release = true; 1420 break; 1421 1422 // release already sent or on the way, nothing to do 1423 case AVCTP_W2_RECEIVE_RESPONSE: 1424 case AVCTP_W2_SEND_RELEASE_COMMAND: 1425 break; 1426 1427 // about to send next repeated press command or wait for it -> release right away 1428 case AVCTP_W2_SEND_PRESS_COMMAND: 1429 case AVCTP_W4_STOP: 1430 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1431 1432 // otherwise reject request 1433 default: 1434 return ERROR_CODE_COMMAND_DISALLOWED; 1435 } 1436 return ERROR_CODE_SUCCESS; 1437 } 1438 1439 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1440 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1441 if (!connection){ 1442 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1443 } 1444 return avrcp_controller_register_notification(connection, event_id); 1445 } 1446 1447 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1448 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1449 if (!connection){ 1450 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1451 } 1452 if (connection->remote_capabilities_state != AVRCP_REMOTE_CAPABILITIES_KNOWN){ 1453 return ERROR_CODE_COMMAND_DISALLOWED; 1454 } 1455 1456 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 1457 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1458 } 1459 1460 if ((connection->notifications_enabled & (1 << event_id)) == 0){ 1461 return ERROR_CODE_SUCCESS; 1462 } 1463 1464 connection->controller_notifications_to_deregister |= (1 << event_id); 1465 return ERROR_CODE_SUCCESS; 1466 } 1467 1468 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1469 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1470 if (!connection){ 1471 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1472 } 1473 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1474 1475 connection->state = AVCTP_W2_SEND_COMMAND; 1476 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_STATUS, 1477 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1478 0); 1479 1480 connection->data = connection->message_body; 1481 memset(connection->data, 0xFF, 5); 1482 connection->data_len = 5; 1483 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1484 return ERROR_CODE_SUCCESS; 1485 } 1486 1487 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1488 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1489 if (!connection){ 1490 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1491 } 1492 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1493 1494 connection->state = AVCTP_W2_SEND_COMMAND; 1495 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, AVRCP_CTYPE_STATUS, 1496 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1497 0); 1498 1499 connection->data = connection->message_body; 1500 memset(connection->data, 0xFF, 5); 1501 connection->data[0] = 7; // page: 0, extension_code: 7 1502 connection->data_len = 5; 1503 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1504 return ERROR_CODE_SUCCESS; 1505 } 1506 1507 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1508 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1509 if (!connection){ 1510 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1511 } 1512 if (connection->state != AVCTP_CONNECTION_OPENED){ 1513 return ERROR_CODE_COMMAND_DISALLOWED; 1514 } 1515 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY); 1516 return ERROR_CODE_SUCCESS; 1517 } 1518 1519 uint8_t avrcp_controller_get_supported_events(uint16_t avrcp_cid){ 1520 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1521 if (!connection){ 1522 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1523 } 1524 if (connection->state != AVCTP_CONNECTION_OPENED){ 1525 return ERROR_CODE_COMMAND_DISALLOWED; 1526 } 1527 1528 switch (connection->remote_capabilities_state){ 1529 case AVRCP_REMOTE_CAPABILITIES_NONE: 1530 connection->remote_capabilities_state = AVRCP_REMOTE_CAPABILITIES_W4_QUERY_RESULT; 1531 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 1532 break; 1533 case AVRCP_REMOTE_CAPABILITIES_KNOWN: 1534 avrcp_controller_emit_supported_events(connection); 1535 break; 1536 default: 1537 break; 1538 } 1539 return ERROR_CODE_SUCCESS; 1540 } 1541 1542 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1543 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1544 if (!connection){ 1545 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1546 } 1547 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1548 1549 connection->state = AVCTP_W2_SEND_COMMAND; 1550 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAY_STATUS, true); 1551 1552 connection->data_len = 0; 1553 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1554 return ERROR_CODE_SUCCESS; 1555 } 1556 1557 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1558 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1559 if (!connection){ 1560 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1561 } 1562 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1563 1564 connection->state = AVCTP_W2_SEND_COMMAND; 1565 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ADDRESSED_PLAYER, true); 1566 1567 connection->data_len = 2; 1568 big_endian_store_16(connection->data, 0, addressed_player_id); 1569 1570 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1571 return ERROR_CODE_SUCCESS; 1572 } 1573 1574 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 1575 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1576 if (!connection){ 1577 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1578 } 1579 1580 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1581 1582 if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) { 1583 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1584 } 1585 1586 connection->state = AVCTP_W2_SEND_COMMAND; 1587 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES, true); 1588 1589 uint8_t pos = 0; 1590 // write 8 bytes value 1591 memset(connection->data, pos, 8); // identifier: PLAYING 1592 pos += 8; 1593 1594 uint8_t num_attributes_index = pos; 1595 pos++; 1596 1597 // If num_attributes is set to zero, all attribute information shall be returned, 1598 // and the AttributeID field is omitted 1599 connection->data[num_attributes_index] = 0; 1600 uint8_t i; 1601 for (i = 0; i < num_attributes; i++){ 1602 // ignore invalid attribute ID and "get all attributes" 1603 if (AVRCP_MEDIA_ATTR_ALL < attributes[i] && attributes[i] < AVRCP_MEDIA_ATTR_RESERVED){ 1604 // every attribute is 4 bytes long 1605 big_endian_store_32(connection->data, pos, attributes[i]); 1606 pos += 4; 1607 connection->data[num_attributes_index]++; 1608 } 1609 } 1610 1611 // Parameter Length 1612 connection->data_len = pos; 1613 1614 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1615 return ERROR_CODE_SUCCESS; 1616 } 1617 1618 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1619 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1620 } 1621 1622 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){ 1623 if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){ 1624 return avrcp_controller_get_now_playing_info(avrcp_cid); 1625 } 1626 avrcp_media_attribute_id_t media_attrs[1]; 1627 media_attrs[0] = media_attribute_id; 1628 return avrcp_controller_get_element_attributes(avrcp_cid, 1, media_attrs); 1629 } 1630 1631 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1632 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1633 if (!connection){ 1634 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1635 } 1636 1637 // 1638 // allow sending of multiple set abs volume commands without waiting for response 1639 // 1640 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1641 switch (connection->state){ 1642 case AVCTP_CONNECTION_OPENED: 1643 status = ERROR_CODE_SUCCESS; 1644 break; 1645 case AVCTP_W2_RECEIVE_RESPONSE: 1646 // - is pending response also set abs volume 1647 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1648 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1649 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1650 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1651 if (connection->pdu_id != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1652 // - is next transaction id valid in window 1653 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1654 status = ERROR_CODE_SUCCESS; 1655 break; 1656 default: 1657 break; 1658 } 1659 if (status != ERROR_CODE_SUCCESS) return status; 1660 1661 connection->state = AVCTP_W2_SEND_COMMAND; 1662 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME, true); 1663 1664 // Parameter Length 1665 connection->data_len = 1; 1666 connection->data[0] = volume; 1667 1668 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1669 return ERROR_CODE_SUCCESS; 1670 } 1671 1672 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1673 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1674 if (!connection){ 1675 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1676 } 1677 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1678 1679 connection->state = AVCTP_W2_SEND_COMMAND; 1680 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, true); 1681 1682 connection->data_len = 5; 1683 connection->data[0] = 4; // NumPlayerApplicationSettingAttributeID 1684 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1685 connection->data[1] = 0x01; // equalizer (1-OFF, 2-ON) 1686 connection->data[2] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1687 connection->data[3] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1688 connection->data[4] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1689 1690 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1691 return ERROR_CODE_SUCCESS; 1692 } 1693 1694 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1695 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1696 if (!connection){ 1697 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1698 } 1699 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1700 1701 connection->state = AVCTP_W2_SEND_COMMAND; 1702 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE, true); 1703 1704 // Parameter Length 1705 connection->data_len = 3; 1706 connection->data[0] = 2; 1707 connection->data[1] = attr_id; 1708 connection->data[2] = attr_value; 1709 1710 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1711 return ERROR_CODE_SUCCESS; 1712 } 1713 1714 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1715 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1716 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1717 } 1718 1719 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1720 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1721 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1722 } 1723 1724 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1725 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1726 if (!connection){ 1727 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1728 } 1729 if (connection->state != AVCTP_CONNECTION_OPENED){ 1730 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1731 return ERROR_CODE_COMMAND_DISALLOWED; 1732 } 1733 connection->state = AVCTP_W2_SEND_COMMAND; 1734 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_PLAY_ITEM, true); 1735 1736 // Parameter Length 1737 connection->data_len = 11; 1738 connection->data[0] = scope; 1739 memset(&connection->data[1], 0, 8); 1740 if (uid){ 1741 (void)memcpy(&connection->data[1], uid, 8); 1742 } 1743 big_endian_store_16(connection->data, 9, uid_counter); 1744 1745 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1746 return ERROR_CODE_SUCCESS; 1747 } 1748 1749 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){ 1750 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1751 if (!connection){ 1752 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1753 } 1754 if (connection->state != AVCTP_CONNECTION_OPENED){ 1755 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1756 return ERROR_CODE_COMMAND_DISALLOWED; 1757 } 1758 1759 connection->state = AVCTP_W2_SEND_COMMAND; 1760 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_ADD_TO_NOW_PLAYING, true); 1761 1762 // Parameter Length 1763 connection->data_len = 11; 1764 connection->data[0] = scope; 1765 memset(&connection->data[1], 0, 8); 1766 if (uid){ 1767 (void)memcpy(&connection->data[1], uid, 8); 1768 } 1769 big_endian_store_16(connection->data, 9, uid_counter); 1770 1771 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1772 return ERROR_CODE_SUCCESS; 1773 } 1774 1775 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1776 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1777 if (!connection){ 1778 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1779 } 1780 connection->controller_max_num_fragments = max_num_fragments; 1781 return ERROR_CODE_SUCCESS; 1782 } 1783 1784 1785 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, 1786 avrcp_command_type_t command_type, 1787 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 1788 avrcp_pdu_id_t pdu_id, uint32_t company_id, 1789 const uint8_t * data, uint16_t data_len){ 1790 1791 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1792 if (!connection){ 1793 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1794 } 1795 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1796 1797 connection->state = AVCTP_W2_SEND_COMMAND; 1798 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT, command_type, subunit_type, 1799 subunit_id, pdu_id, company_id); 1800 1801 connection->data = (uint8_t *)data; 1802 connection->data_len = data_len; 1803 1804 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1805 return ERROR_CODE_SUCCESS; 1806 } 1807