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