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