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