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