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->controller_notifications_supported_by_target_queried && (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 if (!connection->controller_notifications_supported_by_target_queried){ 749 connection->controller_notifications_supported_by_target_suppress_emit_result = true; 750 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 751 return ERROR_CODE_SUCCESS; 752 } 753 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 754 return ERROR_CODE_SUCCESS; 755 } 756 757 static uint8_t avrcp_controller_request_continuation(avrcp_connection_t * connection, avrcp_pdu_id_t pdu_id){ 758 connection->state = AVCTP_W2_SEND_COMMAND; 759 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, pdu_id, false); 760 761 // Parameter Length 762 connection->data_len = 3; 763 big_endian_store_16(connection->data, 0, 1); 764 connection->data[2] = AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES; 765 766 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 767 return ERROR_CODE_SUCCESS; 768 } 769 770 static uint8_t avrcp_controller_request_abort_continuation(avrcp_connection_t * connection){ 771 return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE); 772 } 773 774 static uint8_t avrcp_controller_request_continue_response(avrcp_connection_t * connection){ 775 return avrcp_controller_request_continuation(connection, AVRCP_PDU_ID_REQUEST_CONTINUING_RESPONSE); 776 } 777 778 static void avrcp_controller_handle_notification(avrcp_connection_t *connection, avrcp_command_type_t ctype, uint8_t *payload, uint16_t size) { 779 if (size < 1) return; 780 uint16_t pos = 0; 781 avrcp_notification_event_id_t event_id = (avrcp_notification_event_id_t) payload[pos++]; 782 if ( (event_id < AVRCP_NOTIFICATION_EVENT_FIRST_INDEX) || (event_id > AVRCP_NOTIFICATION_EVENT_LAST_INDEX)){ 783 return; 784 } 785 786 uint16_t event_mask = (1 << event_id); 787 uint16_t reset_event_mask = ~event_mask; 788 789 switch (ctype){ 790 case AVRCP_CTYPE_RESPONSE_REJECTED: 791 connection->controller_notifications_to_deregister &= reset_event_mask; 792 connection->controller_notifications_to_register &= reset_event_mask; 793 connection->controller_initial_status_reported &= reset_event_mask; 794 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, event_id, false); 795 return; 796 797 case AVRCP_CTYPE_RESPONSE_INTERIM: 798 // register as enabled 799 connection->notifications_enabled |= event_mask; 800 801 // check if initial value is already sent 802 if ((connection->controller_initial_status_reported & event_mask) != 0 ){ 803 return; 804 } 805 // emit event only once, initially 806 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, true); 807 connection->controller_initial_status_reported |= event_mask; 808 // emit initial value after this switch 809 break; 810 811 case AVRCP_CTYPE_RESPONSE_CHANGED_STABLE: 812 // received change, event is considered de-registered 813 // we are re-enabling it automatically, if it is not 814 // explicitly disabled 815 connection->notifications_enabled &= reset_event_mask; 816 if ((connection->controller_notifications_to_deregister & event_mask) == 0){ 817 avrcp_controller_register_notification(connection, event_id); 818 } else { 819 connection->controller_notifications_to_deregister &= reset_event_mask; 820 connection->controller_notifications_to_register &= reset_event_mask; 821 connection->controller_initial_status_reported &= reset_event_mask; 822 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_SUCCESS, event_id, false); 823 } 824 break; 825 826 default: 827 return; 828 } 829 830 avrcp_controller_emit_notification_for_event_id(connection->avrcp_cid, event_id, ctype, payload + pos, size - pos); 831 } 832 833 #ifdef ENABLE_AVCTP_FRAGMENTATION 834 static void avctp_reassemble_message(avrcp_connection_t * connection, avctp_packet_type_t packet_type, uint8_t *packet, uint16_t size){ 835 // after header (transaction label and packet type) 836 uint16_t pos; 837 uint16_t bytes_to_store; 838 839 switch (packet_type){ 840 case AVCTP_START_PACKET: 841 if (size < 2) return; 842 843 // store header 844 pos = 0; 845 connection->avctp_reassembly_buffer[pos] = packet[pos]; 846 pos++; 847 connection->avctp_reassembly_size = pos; 848 849 // NOTE: num packets not needed for reassembly, ignoring it does not pose security risk -> no need to store it 850 pos++; 851 852 // PID in reassembled packet is at offset 1, it will be read later after the avctp_reassemble_message with AVCTP_END_PACKET is called 853 854 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 855 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 856 connection->avctp_reassembly_size += bytes_to_store; 857 break; 858 859 case AVCTP_CONTINUE_PACKET: 860 case AVCTP_END_PACKET: 861 if (size < 1) return; 862 863 // store remaining data, ignore header 864 pos = 1; 865 bytes_to_store = btstack_min(size - pos, sizeof(connection->avctp_reassembly_buffer) - connection->avctp_reassembly_size); 866 memcpy(&connection->avctp_reassembly_buffer[connection->avctp_reassembly_size], &packet[pos], bytes_to_store); 867 connection->avctp_reassembly_size += bytes_to_store; 868 break; 869 870 default: 871 return; 872 } 873 } 874 #endif 875 876 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 877 if (size < 6u) return; 878 uint8_t pdu_id; 879 avrcp_packet_type_t vendor_dependent_avrcp_packet_type; 880 881 uint16_t pos = 0; 882 connection->controller_last_confirmed_transaction_id = packet[pos] >> 4; 883 avrcp_frame_type_t frame_type = (avrcp_frame_type_t)((packet[pos] >> 1) & 0x01); 884 avctp_packet_type_t packet_type = (avctp_packet_type_t)((packet[pos] >> 2) & 0x03); 885 pos++; 886 887 if (frame_type != AVRCP_RESPONSE_FRAME) return; 888 889 switch (packet_type){ 890 case AVCTP_SINGLE_PACKET: 891 break; 892 893 #ifdef ENABLE_AVCTP_FRAGMENTATION 894 case AVCTP_START_PACKET: 895 case AVCTP_CONTINUE_PACKET: 896 avctp_reassemble_message(connection, packet_type, packet, size); 897 return; 898 899 case AVCTP_END_PACKET: 900 avctp_reassemble_message(connection, packet_type, packet, size); 901 902 packet = connection->avctp_reassembly_buffer; 903 size = connection->avctp_reassembly_size; 904 break; 905 #endif 906 907 default: 908 return; 909 } 910 911 pos += 2; // PID 912 913 avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 914 915 #ifdef ENABLE_LOG_INFO 916 uint8_t byte_value = packet[pos]; 917 avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 918 avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 919 #endif 920 pos++; 921 922 uint8_t opcode = packet[pos++]; 923 uint16_t param_length; 924 925 switch (opcode){ 926 case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{ 927 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 928 connection->state = AVCTP_CONNECTION_OPENED; 929 930 #ifdef ENABLE_LOG_INFO 931 // page, extension code (1) 932 pos++; 933 uint8_t unit_type = packet[pos] >> 3; 934 uint8_t max_subunit_ID = packet[pos] & 0x07; 935 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); 936 #endif 937 break; 938 } 939 case AVRCP_CMD_OPCODE_UNIT_INFO:{ 940 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 941 connection->state = AVCTP_CONNECTION_OPENED; 942 943 #ifdef ENABLE_LOG_INFO 944 // byte value 7 (1) 945 pos++; 946 uint8_t unit_type = packet[pos] >> 3; 947 uint8_t unit = packet[pos] & 0x07; 948 pos++; 949 uint32_t company_id = big_endian_read_24(packet, pos); 950 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, 951 ctype, subunit_type, subunit_id, opcode, unit_type, unit, company_id); 952 #endif 953 break; 954 } 955 case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT: 956 if ((size - pos) < 7){ 957 return; 958 } 959 // Company ID (3) 960 pos += 3; 961 pdu_id = packet[pos++]; 962 vendor_dependent_avrcp_packet_type = (avrcp_packet_type_t)(packet[pos++] & 0x03); 963 param_length = big_endian_read_16(packet, pos); 964 pos += 2; 965 966 if ((size - pos) < param_length) { 967 return; 968 } 969 970 // handle asynchronous notifications, without changing state 971 if (pdu_id == AVRCP_PDU_ID_REGISTER_NOTIFICATION){ 972 avrcp_controller_handle_notification(connection, ctype, packet + pos, size - pos); 973 break; 974 } 975 if (connection->state != AVCTP_W2_RECEIVE_RESPONSE) return; 976 connection->state = AVCTP_CONNECTION_OPENED; 977 978 log_info("VENDOR DEPENDENT response: pdu id 0x%02x, param_length %d, status %s", pdu_id, param_length, avrcp_ctype2str(ctype)); 979 switch (pdu_id){ 980 case AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE:{ 981 uint8_t num_attributes = packet[pos++]; 982 int i; 983 avrcp_repeat_mode_t repeat_mode = AVRCP_REPEAT_MODE_INVALID; 984 avrcp_shuffle_mode_t shuffle_mode = AVRCP_SHUFFLE_MODE_INVALID; 985 for (i = 0; i < num_attributes; i++){ 986 uint8_t attribute_id = packet[pos++]; 987 uint8_t value = packet[pos++]; 988 switch (attribute_id){ 989 case 0x02: 990 repeat_mode = (avrcp_repeat_mode_t) value; 991 break; 992 case 0x03: 993 shuffle_mode = (avrcp_shuffle_mode_t) value; 994 break; 995 default: 996 break; 997 } 998 } 999 avrcp_controller_emit_repeat_and_shuffle_mode(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, repeat_mode, shuffle_mode); 1000 break; 1001 } 1002 1003 case AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE:{ 1004 uint16_t offset = 0; 1005 uint8_t event[6]; 1006 event[offset++] = HCI_EVENT_AVRCP_META; 1007 event[offset++] = sizeof(event) - 2; 1008 event[offset++] = AVRCP_SUBEVENT_PLAYER_APPLICATION_VALUE_RESPONSE; 1009 little_endian_store_16(event, offset, connection->avrcp_cid); 1010 offset += 2; 1011 event[offset++] = ctype; 1012 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1013 break; 1014 } 1015 1016 case AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME:{ 1017 uint16_t offset = 0; 1018 uint8_t event[7]; 1019 event[offset++] = HCI_EVENT_AVRCP_META; 1020 event[offset++] = sizeof(event) - 2; 1021 event[offset++] = AVRCP_SUBEVENT_SET_ABSOLUTE_VOLUME_RESPONSE; 1022 little_endian_store_16(event, offset, connection->avrcp_cid); 1023 offset += 2; 1024 event[offset++] = ctype; 1025 event[offset++] = packet[pos++]; 1026 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1027 break; 1028 } 1029 1030 case AVRCP_PDU_ID_GET_CAPABILITIES:{ 1031 avrcp_capability_id_t capability_id = (avrcp_capability_id_t) packet[pos++]; 1032 uint8_t capability_count = 0; 1033 if (param_length > 1){ 1034 capability_count = packet[pos++]; 1035 } 1036 uint16_t i; 1037 uint16_t offset = 0; 1038 uint8_t event[10]; 1039 1040 switch (capability_id){ 1041 1042 case AVRCP_CAPABILITY_ID_COMPANY: 1043 for (i = 0; (i < capability_count) && ((size - pos) >= 3); i++){ 1044 uint32_t company_id = big_endian_read_24(packet, pos); 1045 pos += 3; 1046 log_info(" 0x%06" PRIx32 ", ", company_id); 1047 1048 offset = 0; 1049 event[offset++] = HCI_EVENT_AVRCP_META; 1050 event[offset++] = sizeof(event) - 2; 1051 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID; 1052 little_endian_store_16(event, offset, connection->avrcp_cid); 1053 offset += 2; 1054 event[offset++] = ctype; 1055 event[offset++] = 0; 1056 little_endian_store_24(event, offset, company_id); 1057 offset += 3; 1058 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1059 } 1060 1061 offset = 0; 1062 event[offset++] = HCI_EVENT_AVRCP_META; 1063 event[offset++] = sizeof(event) - 2; 1064 event[offset++] = AVRCP_SUBEVENT_GET_CAPABILITY_COMPANY_ID_DONE; 1065 little_endian_store_16(event, offset, connection->avrcp_cid); 1066 offset += 2; 1067 event[offset++] = ctype; 1068 event[offset++] = 0; 1069 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, offset); 1070 break; 1071 1072 case AVRCP_CAPABILITY_ID_EVENT: 1073 for (i = 0; (i < capability_count) && ((size - pos) >= 1); i++){ 1074 uint8_t event_id = packet[pos++]; 1075 connection->notifications_supported_by_target |= (1 << event_id); 1076 } 1077 1078 // if the get supported events query is triggered by avrcp_controller_enable_notification call, 1079 // avrcp_controller_emit_supported_events should be suppressed 1080 if (connection->controller_notifications_supported_by_target_suppress_emit_result){ 1081 connection->controller_notifications_supported_by_target_suppress_emit_result = false; 1082 // also, notification might not be supported 1083 // if so, emit AVRCP_SUBEVENT_ENABLE_NOTIFICATION_COMPLETE event to app, 1084 // and update controller_notifications_to_register bitmap 1085 for (i = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; i < (uint8_t) AVRCP_NOTIFICATION_EVENT_LAST_INDEX; i++){ 1086 if ((connection->controller_notifications_to_register & (1 << i)) != 0){ 1087 if ((connection->notifications_supported_by_target & (1 << i)) == 0){ 1088 avrcp_controller_emit_notification_complete(connection, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE, i, false); 1089 connection->controller_notifications_to_register &= ~(1 << i); 1090 } 1091 } 1092 } 1093 break; 1094 } 1095 // supported events are emitted only if the get supported events query 1096 // is triggered by avrcp_controller_get_supported_events call 1097 avrcp_controller_emit_supported_events(connection); 1098 break; 1099 1100 default: 1101 // ignore 1102 break; 1103 } 1104 break; 1105 } 1106 1107 case AVRCP_PDU_ID_GET_PLAY_STATUS:{ 1108 uint32_t song_length = big_endian_read_32(packet, pos); 1109 pos += 4; 1110 uint32_t song_position = big_endian_read_32(packet, pos); 1111 pos += 4; 1112 uint8_t play_status = packet[pos]; 1113 1114 uint8_t event[15]; 1115 int offset = 0; 1116 event[offset++] = HCI_EVENT_AVRCP_META; 1117 event[offset++] = sizeof(event) - 2; 1118 event[offset++] = AVRCP_SUBEVENT_PLAY_STATUS; 1119 little_endian_store_16(event, offset, connection->avrcp_cid); 1120 offset += 2; 1121 event[offset++] = ctype; 1122 little_endian_store_32(event, offset, song_length); 1123 offset += 4; 1124 little_endian_store_32(event, offset, song_position); 1125 offset += 4; 1126 event[offset++] = play_status; 1127 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1128 break; 1129 } 1130 1131 case AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES:{ 1132 switch (vendor_dependent_avrcp_packet_type){ 1133 case AVRCP_START_PACKET: 1134 case AVRCP_SINGLE_PACKET: 1135 avrcp_parser_reset(connection); 1136 connection->list_size = param_length; 1137 // num_attributes 1138 pos++; 1139 1140 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1141 if (vendor_dependent_avrcp_packet_type == AVRCP_START_PACKET){ 1142 avrcp_controller_request_continue_response(connection); 1143 return; 1144 } 1145 break; 1146 case AVRCP_CONTINUE_PACKET: 1147 case AVRCP_END_PACKET: 1148 connection->controller_num_received_fragments++; 1149 1150 if (connection->controller_num_received_fragments < connection->controller_max_num_fragments){ 1151 avrcp_controller_parse_and_emit_element_attrs(packet+pos, size-pos, connection, ctype); 1152 1153 if (vendor_dependent_avrcp_packet_type == AVRCP_CONTINUE_PACKET){ 1154 avrcp_controller_request_continue_response(connection); 1155 return; 1156 } 1157 } else { 1158 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 1); 1159 avrcp_parser_reset(connection); 1160 avrcp_controller_request_abort_continuation(connection); 1161 return; 1162 } 1163 break; 1164 default: 1165 btstack_assert(false); 1166 break; 1167 } 1168 } 1169 default: 1170 // custom command response comes here 1171 switch (pdu_id){ 1172 case AVRCP_PDU_ID_REQUEST_ABORT_CONTINUING_RESPONSE: 1173 avrcp_controller_emit_now_playing_info_event_done(avrcp_controller_context.avrcp_callback, connection->avrcp_cid, ctype, 0); 1174 break; 1175 default: 1176 if (pdu_id != connection->pdu_id) { 1177 break; 1178 } 1179 uint8_t *in_place_buffer = packet + pos - 9; 1180 avrcp_controller_prepare_custom_command_response(connection, param_length, 1181 in_place_buffer); 1182 (*avrcp_controller_context.avrcp_callback)(HCI_EVENT_PACKET, 0, in_place_buffer, 1183 param_length + 9); 1184 1185 break; 1186 } 1187 break; 1188 } 1189 break; 1190 case AVRCP_CMD_OPCODE_PASS_THROUGH:{ 1191 if ((size - pos) < 1) return; 1192 uint8_t operation_id = packet[pos++]; 1193 switch (connection->state){ 1194 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1195 // trigger release for simple command: 1196 if (!connection->controller_press_and_hold_cmd_active){ 1197 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1198 break; 1199 } 1200 // for press and hold, send release if it just has been requested, otherwise, wait for next repeat 1201 if (connection->controller_press_and_hold_cmd_release){ 1202 connection->controller_press_and_hold_cmd_release = false; 1203 connection->state = AVCTP_W2_SEND_RELEASE_COMMAND; 1204 } else { 1205 connection->state = AVCTP_W4_STOP; 1206 } 1207 break; 1208 case AVCTP_W2_RECEIVE_RESPONSE: 1209 connection->state = AVCTP_CONNECTION_OPENED; 1210 break; 1211 default: 1212 break; 1213 } 1214 if (connection->state == AVCTP_W4_STOP){ 1215 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_START, connection->avrcp_cid, ctype, operation_id); 1216 } 1217 if (connection->state == AVCTP_CONNECTION_OPENED) { 1218 // RELEASE response 1219 operation_id = operation_id & 0x7F; 1220 avrcp_controller_emit_operation_status(avrcp_controller_context.avrcp_callback, AVRCP_SUBEVENT_OPERATION_COMPLETE, connection->avrcp_cid, ctype, operation_id); 1221 } 1222 if (connection->state == AVCTP_W2_SEND_RELEASE_COMMAND){ 1223 // PRESS response 1224 avrcp_controller_request_pass_through_release_control_cmd(connection); 1225 } 1226 break; 1227 } 1228 default: 1229 break; 1230 } 1231 1232 // trigger pending notification reqistrations 1233 if ((connection->state == AVCTP_CONNECTION_OPENED) && connection->controller_notifications_to_register){ 1234 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1235 } 1236 } 1237 1238 static void avrcp_controller_handle_can_send_now(avrcp_connection_t * connection){ 1239 switch (connection->state){ 1240 case AVCTP_W2_SEND_PRESS_COMMAND: 1241 case AVCTP_W2_SEND_COMMAND: 1242 case AVCTP_W2_SEND_RELEASE_COMMAND: 1243 avrcp_send_cmd_with_avctp_fragmentation(connection); 1244 if (connection->data_offset < connection->data_len){ 1245 // continue AVCTP fragmentation 1246 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1247 return; 1248 } 1249 if (connection->state == AVCTP_W2_SEND_PRESS_COMMAND){ 1250 connection->state = AVCTP_W2_RECEIVE_PRESS_RESPONSE; 1251 } else { 1252 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 1253 } 1254 return; 1255 default: 1256 break; 1257 } 1258 1259 // send register notification if queued 1260 if (connection->controller_notifications_to_register != 0){ 1261 uint8_t event_id; 1262 for (event_id = (uint8_t)AVRCP_NOTIFICATION_EVENT_FIRST_INDEX; event_id < (uint8_t)AVRCP_NOTIFICATION_EVENT_LAST_INDEX; event_id++){ 1263 if (connection->controller_notifications_to_register & (1 << event_id)){ 1264 connection->controller_notifications_to_register &= ~ (1 << event_id); 1265 avrcp_send_register_notification(connection, event_id); 1266 return; 1267 } 1268 } 1269 } 1270 } 1271 1272 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1273 avrcp_connection_t * connection; 1274 1275 switch (packet_type) { 1276 case L2CAP_DATA_PACKET: 1277 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1278 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 1279 break; 1280 1281 case HCI_EVENT_PACKET: 1282 switch (hci_event_packet_get_type(packet)){ 1283 case L2CAP_EVENT_CAN_SEND_NOW: 1284 connection = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, channel); 1285 avrcp_controller_handle_can_send_now(connection); 1286 break; 1287 default: 1288 break; 1289 } 1290 default: 1291 break; 1292 } 1293 } 1294 1295 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){ 1296 avrcp_create_sdp_record(1, service, service_record_handle, avrcp_controller_supports_browsing(supported_features), supported_features, service_name, service_provider_name); 1297 } 1298 1299 void avrcp_controller_init(void){ 1300 avrcp_controller_context.role = AVRCP_CONTROLLER; 1301 avrcp_controller_context.packet_handler = avrcp_controller_packet_handler; 1302 avrcp_register_controller_packet_handler(&avrcp_controller_packet_handler); 1303 } 1304 1305 void avrcp_controller_deinit(void){ 1306 memset(&avrcp_controller_context, 0, sizeof(avrcp_context_t)); 1307 } 1308 1309 void avrcp_controller_register_packet_handler(btstack_packet_handler_t callback){ 1310 btstack_assert(callback != NULL); 1311 avrcp_controller_context.avrcp_callback = callback; 1312 } 1313 1314 1315 uint8_t avrcp_controller_play(uint16_t avrcp_cid){ 1316 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1317 } 1318 1319 uint8_t avrcp_controller_stop(uint16_t avrcp_cid){ 1320 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1321 } 1322 1323 uint8_t avrcp_controller_pause(uint16_t avrcp_cid){ 1324 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1325 } 1326 1327 uint8_t avrcp_controller_forward(uint16_t avrcp_cid){ 1328 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1329 } 1330 1331 uint8_t avrcp_controller_backward(uint16_t avrcp_cid){ 1332 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1333 } 1334 1335 uint8_t avrcp_controller_volume_up(uint16_t avrcp_cid){ 1336 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1337 } 1338 1339 uint8_t avrcp_controller_volume_down(uint16_t avrcp_cid){ 1340 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1341 } 1342 1343 uint8_t avrcp_controller_mute(uint16_t avrcp_cid){ 1344 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1345 } 1346 1347 uint8_t avrcp_controller_skip(uint16_t avrcp_cid){ 1348 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_SKIP, 0); 1349 } 1350 1351 uint8_t avrcp_controller_fast_forward(uint16_t avrcp_cid){ 1352 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1353 } 1354 1355 uint8_t avrcp_controller_rewind(uint16_t avrcp_cid){ 1356 return request_single_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1357 } 1358 1359 /* start continuous cmds */ 1360 1361 uint8_t avrcp_controller_start_press_and_hold_cmd(uint16_t avrcp_cid, avrcp_operation_id_t operation_id){ 1362 return request_continuous_pass_through_press_control_cmd(avrcp_cid, operation_id, 0); 1363 } 1364 1365 uint8_t avrcp_controller_press_and_hold_play(uint16_t avrcp_cid){ 1366 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PLAY, 0); 1367 } 1368 uint8_t avrcp_controller_press_and_hold_stop(uint16_t avrcp_cid){ 1369 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_STOP, 0); 1370 } 1371 uint8_t avrcp_controller_press_and_hold_pause(uint16_t avrcp_cid){ 1372 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_PAUSE, 0); 1373 } 1374 uint8_t avrcp_controller_press_and_hold_forward(uint16_t avrcp_cid){ 1375 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FORWARD, 0); 1376 } 1377 uint8_t avrcp_controller_press_and_hold_backward(uint16_t avrcp_cid){ 1378 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_BACKWARD, 0); 1379 } 1380 uint8_t avrcp_controller_press_and_hold_fast_forward(uint16_t avrcp_cid){ 1381 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_FAST_FORWARD, 0); 1382 } 1383 uint8_t avrcp_controller_press_and_hold_rewind(uint16_t avrcp_cid){ 1384 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_REWIND, 0); 1385 } 1386 uint8_t avrcp_controller_press_and_hold_volume_up(uint16_t avrcp_cid){ 1387 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_UP, 0); 1388 } 1389 uint8_t avrcp_controller_press_and_hold_volume_down(uint16_t avrcp_cid){ 1390 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_VOLUME_DOWN, 0); 1391 } 1392 uint8_t avrcp_controller_press_and_hold_mute(uint16_t avrcp_cid){ 1393 return request_continuous_pass_through_press_control_cmd(avrcp_cid, AVRCP_OPERATION_ID_MUTE, 0); 1394 } 1395 1396 /* stop continuous cmds */ 1397 uint8_t avrcp_controller_release_press_and_hold_cmd(uint16_t avrcp_cid){ 1398 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1399 if (!connection){ 1400 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1401 } 1402 1403 switch (connection->state){ 1404 // respond when we receive response for (repeated) press command 1405 case AVCTP_W2_RECEIVE_PRESS_RESPONSE: 1406 connection->controller_press_and_hold_cmd_release = true; 1407 break; 1408 1409 // release already sent or on the way, nothing to do 1410 case AVCTP_W2_RECEIVE_RESPONSE: 1411 case AVCTP_W2_SEND_RELEASE_COMMAND: 1412 break; 1413 1414 // about to send next repeated press command or wait for it -> release right away 1415 case AVCTP_W2_SEND_PRESS_COMMAND: 1416 case AVCTP_W4_STOP: 1417 return avrcp_controller_request_pass_through_release_control_cmd(connection); 1418 1419 // otherwise reject request 1420 default: 1421 return ERROR_CODE_COMMAND_DISALLOWED; 1422 } 1423 return ERROR_CODE_SUCCESS; 1424 } 1425 1426 uint8_t avrcp_controller_enable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1427 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1428 if (!connection){ 1429 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1430 } 1431 return avrcp_controller_register_notification(connection, event_id); 1432 } 1433 1434 uint8_t avrcp_controller_disable_notification(uint16_t avrcp_cid, avrcp_notification_event_id_t event_id){ 1435 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1436 if (!connection){ 1437 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1438 } 1439 if (!connection->controller_notifications_supported_by_target_queried){ 1440 return ERROR_CODE_COMMAND_DISALLOWED; 1441 } 1442 1443 if ((connection->notifications_supported_by_target & (1 << event_id)) == 0){ 1444 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1445 } 1446 1447 if ((connection->notifications_enabled & (1 << event_id)) == 0){ 1448 return ERROR_CODE_SUCCESS; 1449 } 1450 1451 connection->controller_notifications_to_deregister |= (1 << event_id); 1452 return ERROR_CODE_SUCCESS; 1453 } 1454 1455 uint8_t avrcp_controller_unit_info(uint16_t avrcp_cid){ 1456 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1457 if (!connection){ 1458 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1459 } 1460 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1461 1462 connection->state = AVCTP_W2_SEND_COMMAND; 1463 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_UNIT_INFO, AVRCP_CTYPE_STATUS, 1464 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1465 0); 1466 1467 connection->data = connection->message_body; 1468 memset(connection->data, 0xFF, 5); 1469 connection->data_len = 5; 1470 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1471 return ERROR_CODE_SUCCESS; 1472 } 1473 1474 uint8_t avrcp_controller_subunit_info(uint16_t avrcp_cid){ 1475 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1476 if (!connection){ 1477 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1478 } 1479 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1480 1481 connection->state = AVCTP_W2_SEND_COMMAND; 1482 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_SUBUNIT_INFO, AVRCP_CTYPE_STATUS, 1483 AVRCP_SUBUNIT_TYPE_UNIT, AVRCP_SUBUNIT_ID_IGNORE, AVRCP_PDU_ID_UNDEFINED, 1484 0); 1485 1486 connection->data = connection->message_body; 1487 memset(connection->data, 0xFF, 5); 1488 connection->data[0] = 7; // page: 0, extension_code: 7 1489 connection->data_len = 5; 1490 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1491 return ERROR_CODE_SUCCESS; 1492 } 1493 1494 uint8_t avrcp_controller_get_supported_company_ids(uint16_t avrcp_cid){ 1495 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1496 if (!connection){ 1497 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1498 } 1499 if (connection->state != AVCTP_CONNECTION_OPENED){ 1500 return ERROR_CODE_COMMAND_DISALLOWED; 1501 } 1502 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_COMPANY); 1503 return ERROR_CODE_SUCCESS; 1504 } 1505 1506 uint8_t avrcp_controller_get_supported_events(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 1515 if (!connection->controller_notifications_supported_by_target_queried){ 1516 connection->controller_notifications_supported_by_target_queried = true; 1517 avrcp_controller_get_capabilities_for_connection(connection, AVRCP_CAPABILITY_ID_EVENT); 1518 return ERROR_CODE_SUCCESS; 1519 } 1520 1521 avrcp_controller_emit_supported_events(connection); 1522 return ERROR_CODE_SUCCESS; 1523 } 1524 1525 uint8_t avrcp_controller_get_play_status(uint16_t avrcp_cid){ 1526 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1527 if (!connection){ 1528 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1529 } 1530 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1531 1532 connection->state = AVCTP_W2_SEND_COMMAND; 1533 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_PLAY_STATUS, true); 1534 1535 connection->data_len = 0; 1536 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1537 return ERROR_CODE_SUCCESS; 1538 } 1539 1540 uint8_t avrcp_controller_set_addressed_player(uint16_t avrcp_cid, uint16_t addressed_player_id){ 1541 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1542 if (!connection){ 1543 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1544 } 1545 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1546 1547 connection->state = AVCTP_W2_SEND_COMMAND; 1548 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ADDRESSED_PLAYER, true); 1549 1550 connection->data_len = 2; 1551 big_endian_store_16(connection->data, 0, addressed_player_id); 1552 1553 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1554 return ERROR_CODE_SUCCESS; 1555 } 1556 1557 uint8_t avrcp_controller_get_element_attributes(uint16_t avrcp_cid, uint8_t num_attributes, avrcp_media_attribute_id_t * attributes){ 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 1563 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1564 1565 if (num_attributes >= AVRCP_MEDIA_ATTR_RESERVED) { 1566 return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 1567 } 1568 1569 connection->state = AVCTP_W2_SEND_COMMAND; 1570 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_ELEMENT_ATTRIBUTES, true); 1571 1572 uint8_t pos = 0; 1573 // write 8 bytes value 1574 memset(connection->data, pos, 8); // identifier: PLAYING 1575 pos += 8; 1576 1577 uint8_t num_attributes_index = pos; 1578 pos++; 1579 1580 // If num_attributes is set to zero, all attribute information shall be returned, 1581 // and the AttributeID field is omitted 1582 connection->data[num_attributes_index] = 0; 1583 uint8_t i; 1584 for (i = 0; i < num_attributes; i++){ 1585 // ignore invalid attribute ID and "get all attributes" 1586 if (AVRCP_MEDIA_ATTR_ALL < attributes[i] && attributes[i] < AVRCP_MEDIA_ATTR_RESERVED){ 1587 // every attribute is 4 bytes long 1588 big_endian_store_32(connection->data, pos, attributes[i]); 1589 pos += 4; 1590 connection->data[num_attributes_index]++; 1591 } 1592 } 1593 1594 // Parameter Length 1595 connection->data_len = pos; 1596 1597 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1598 return ERROR_CODE_SUCCESS; 1599 } 1600 1601 uint8_t avrcp_controller_get_now_playing_info(uint16_t avrcp_cid){ 1602 return avrcp_controller_get_element_attributes(avrcp_cid, 0, NULL); 1603 } 1604 1605 uint8_t avrcp_controller_get_now_playing_info_for_media_attribute_id(uint16_t avrcp_cid, avrcp_media_attribute_id_t media_attribute_id){ 1606 if (media_attribute_id == AVRCP_MEDIA_ATTR_ALL){ 1607 return avrcp_controller_get_now_playing_info(avrcp_cid); 1608 } 1609 avrcp_media_attribute_id_t media_attrs[1]; 1610 media_attrs[0] = media_attribute_id; 1611 return avrcp_controller_get_element_attributes(avrcp_cid, 1, media_attrs); 1612 } 1613 1614 uint8_t avrcp_controller_set_absolute_volume(uint16_t avrcp_cid, uint8_t volume){ 1615 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1616 if (!connection){ 1617 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1618 } 1619 1620 // 1621 // allow sending of multiple set abs volume commands without waiting for response 1622 // 1623 uint8_t status = ERROR_CODE_COMMAND_DISALLOWED; 1624 switch (connection->state){ 1625 case AVCTP_CONNECTION_OPENED: 1626 status = ERROR_CODE_SUCCESS; 1627 break; 1628 case AVCTP_W2_RECEIVE_RESPONSE: 1629 // - is pending response also set abs volume 1630 if (connection->command_opcode != AVRCP_CMD_OPCODE_VENDOR_DEPENDENT) break; 1631 if (connection->command_type != AVRCP_CTYPE_CONTROL) break; 1632 if (connection->subunit_type != AVRCP_SUBUNIT_TYPE_PANEL) break; 1633 if (connection->subunit_id != AVRCP_SUBUNIT_ID) break; 1634 if (connection->pdu_id != AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME) break; 1635 // - is next transaction id valid in window 1636 if (avrcp_controller_is_transaction_id_valid(connection, avrcp_controller_calc_next_transaction_label(connection->transaction_id_counter)) == false) break; 1637 status = ERROR_CODE_SUCCESS; 1638 break; 1639 default: 1640 break; 1641 } 1642 if (status != ERROR_CODE_SUCCESS) return status; 1643 1644 connection->state = AVCTP_W2_SEND_COMMAND; 1645 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_ABSOLUTE_VOLUME, true); 1646 1647 // Parameter Length 1648 connection->data_len = 1; 1649 connection->data[0] = volume; 1650 1651 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1652 return ERROR_CODE_SUCCESS; 1653 } 1654 1655 uint8_t avrcp_controller_query_shuffle_and_repeat_modes(uint16_t avrcp_cid){ 1656 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1657 if (!connection){ 1658 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1659 } 1660 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1661 1662 connection->state = AVCTP_W2_SEND_COMMAND; 1663 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_STATUS, AVRCP_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE, true); 1664 1665 connection->data_len = 5; 1666 connection->data[0] = 4; // NumPlayerApplicationSettingAttributeID 1667 // PlayerApplicationSettingAttributeID1 AVRCP Spec, Appendix F, 133 1668 connection->data[1] = 0x01; // equalizer (1-OFF, 2-ON) 1669 connection->data[2] = 0x02; // repeat (1-off, 2-single track, 3-all tracks, 4-group repeat) 1670 connection->data[3] = 0x03; // shuffle (1-off, 2-all tracks, 3-group shuffle) 1671 connection->data[4] = 0x04; // scan (1-off, 2-all tracks, 3-group scan) 1672 1673 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1674 return ERROR_CODE_SUCCESS; 1675 } 1676 1677 static uint8_t avrcp_controller_set_current_player_application_setting_value(uint16_t avrcp_cid, uint8_t attr_id, uint8_t attr_value){ 1678 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1679 if (!connection){ 1680 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1681 } 1682 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1683 1684 connection->state = AVCTP_W2_SEND_COMMAND; 1685 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE, true); 1686 1687 // Parameter Length 1688 connection->data_len = 3; 1689 connection->data[0] = 2; 1690 connection->data[1] = attr_id; 1691 connection->data[2] = attr_value; 1692 1693 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1694 return ERROR_CODE_SUCCESS; 1695 } 1696 1697 uint8_t avrcp_controller_set_shuffle_mode(uint16_t avrcp_cid, avrcp_shuffle_mode_t mode){ 1698 if ((mode < AVRCP_SHUFFLE_MODE_OFF) || (mode > AVRCP_SHUFFLE_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1699 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x03, mode); 1700 } 1701 1702 uint8_t avrcp_controller_set_repeat_mode(uint16_t avrcp_cid, avrcp_repeat_mode_t mode){ 1703 if ((mode < AVRCP_REPEAT_MODE_OFF) || (mode > AVRCP_REPEAT_MODE_GROUP)) return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 1704 return avrcp_controller_set_current_player_application_setting_value(avrcp_cid, 0x02, mode); 1705 } 1706 1707 uint8_t avrcp_controller_play_item_for_scope(uint16_t avrcp_cid, uint8_t * uid, uint16_t uid_counter, avrcp_browsing_scope_t scope){ 1708 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1709 if (!connection){ 1710 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1711 } 1712 if (connection->state != AVCTP_CONNECTION_OPENED){ 1713 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1714 return ERROR_CODE_COMMAND_DISALLOWED; 1715 } 1716 connection->state = AVCTP_W2_SEND_COMMAND; 1717 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_PLAY_ITEM, true); 1718 1719 // Parameter Length 1720 connection->data_len = 11; 1721 connection->data[0] = scope; 1722 memset(&connection->data[1], 0, 8); 1723 if (uid){ 1724 (void)memcpy(&connection->data[1], uid, 8); 1725 } 1726 big_endian_store_16(connection->data, 9, uid_counter); 1727 1728 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1729 return ERROR_CODE_SUCCESS; 1730 } 1731 1732 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){ 1733 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1734 if (!connection){ 1735 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1736 } 1737 if (connection->state != AVCTP_CONNECTION_OPENED){ 1738 log_error("Connection in wrong state, expected %d, received %d", AVCTP_CONNECTION_OPENED, connection->state); 1739 return ERROR_CODE_COMMAND_DISALLOWED; 1740 } 1741 1742 connection->state = AVCTP_W2_SEND_COMMAND; 1743 avrcp_controller_vendor_dependent_command_data_init(connection, AVRCP_CTYPE_CONTROL, AVRCP_PDU_ID_ADD_TO_NOW_PLAYING, true); 1744 1745 // Parameter Length 1746 connection->data_len = 11; 1747 connection->data[0] = scope; 1748 memset(&connection->data[1], 0, 8); 1749 if (uid){ 1750 (void)memcpy(&connection->data[1], uid, 8); 1751 } 1752 big_endian_store_16(connection->data, 9, uid_counter); 1753 1754 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1755 return ERROR_CODE_SUCCESS; 1756 } 1757 1758 uint8_t avrcp_controller_set_max_num_fragments(uint16_t avrcp_cid, uint8_t max_num_fragments){ 1759 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1760 if (!connection){ 1761 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1762 } 1763 connection->controller_max_num_fragments = max_num_fragments; 1764 return ERROR_CODE_SUCCESS; 1765 } 1766 1767 1768 uint8_t avrcp_controller_send_custom_command(uint16_t avrcp_cid, 1769 avrcp_command_type_t command_type, 1770 avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, 1771 avrcp_pdu_id_t pdu_id, uint32_t company_id, 1772 const uint8_t * data, uint16_t data_len){ 1773 1774 avrcp_connection_t * connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid); 1775 if (!connection){ 1776 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1777 } 1778 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 1779 1780 connection->state = AVCTP_W2_SEND_COMMAND; 1781 avrcp_controller_custom_command_data_init(connection, AVRCP_CMD_OPCODE_VENDOR_DEPENDENT, command_type, subunit_type, 1782 subunit_id, pdu_id, company_id); 1783 1784 connection->data = (uint8_t *)data; 1785 connection->data_len = data_len; 1786 1787 avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid); 1788 return ERROR_CODE_SUCCESS; 1789 } 1790