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