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