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