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