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_browsing_target.c" 39 40 #include <stdint.h> 41 #include <string.h> 42 #include <inttypes.h> 43 #include "classic/avrcp.h" 44 #include "classic/avrcp_browsing.h" 45 #include "classic/avrcp_browsing_target.h" 46 #include "classic/avrcp_target.h" 47 #include "classic/avrcp_controller.h" 48 49 #include "bluetooth_sdp.h" 50 #include "btstack_debug.h" 51 #include "btstack_event.h" 52 53 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 54 55 static int avrcp_browsing_target_handle_can_send_now(avrcp_browsing_connection_t * connection){ 56 int pos = 0; 57 58 // l2cap_reserve_packet_buffer(); 59 // uint8_t * packet = l2cap_get_outgoing_buffer(); 60 uint8_t packet[400]; 61 connection->packet_type = AVRCP_SINGLE_PACKET; 62 63 packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 64 // Profile IDentifier (PID) 65 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 66 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 67 (void)memcpy(packet + pos, connection->cmd_operands, 68 connection->cmd_operands_length); 69 70 pos += connection->cmd_operands_length; 71 connection->wait_to_send = false; 72 // return l2cap_send_prepared(connection->l2cap_browsing_cid, pos); 73 return l2cap_send(connection->l2cap_browsing_cid, packet, pos); 74 } 75 76 77 static uint8_t avrcp_browsing_target_response_general_reject(avrcp_browsing_connection_t * connection, avrcp_status_code_t status){ 78 // AVRCP_CTYPE_RESPONSE_REJECTED 79 int pos = 0; 80 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT; 81 // connection->message_body[pos++] = 0; 82 // param length 83 big_endian_store_16(connection->cmd_operands, pos, 1); 84 pos += 2; 85 connection->cmd_operands[pos++] = status; 86 connection->cmd_operands_length = 4; 87 connection->state = AVCTP_W2_SEND_RESPONSE; 88 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 89 return ERROR_CODE_SUCCESS; 90 } 91 92 static void avrcp_browsing_target_emit_get_folder_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){ 93 btstack_assert(callback != NULL); 94 95 uint8_t event[18]; 96 int pos = 0; 97 event[pos++] = HCI_EVENT_AVRCP_META; 98 event[pos++] = sizeof(event) - 2; 99 event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_FOLDER_ITEMS; 100 little_endian_store_16(event, pos, browsing_cid); 101 pos += 2; 102 event[pos++] = connection->scope; 103 big_endian_store_32(event, pos, connection->start_item); 104 pos += 4; 105 big_endian_store_32(event, pos, connection->end_item); 106 pos += 4; 107 big_endian_store_32(event, pos, connection->attr_bitmap); 108 pos += 4; 109 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 110 } 111 112 static void avrcp_browsing_target_emit_get_total_num_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){ 113 btstack_assert(callback != NULL); 114 115 uint8_t event[10]; 116 int pos = 0; 117 event[pos++] = HCI_EVENT_AVRCP_META; 118 event[pos++] = sizeof(event) - 2; 119 event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_TOTAL_NUM_ITEMS; 120 little_endian_store_16(event, pos, browsing_cid); 121 pos += 2; 122 event[pos++] = connection->scope; 123 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 124 } 125 126 static void avrcp_browsing_target_emit_set_browsed_player(btstack_packet_handler_t callback, uint16_t browsing_cid, uint16_t browsed_player_id){ 127 btstack_assert(callback != NULL); 128 129 uint8_t event[10]; 130 int pos = 0; 131 event[pos++] = HCI_EVENT_AVRCP_META; 132 event[pos++] = sizeof(event) - 2; 133 event[pos++] = AVRCP_SUBEVENT_BROWSING_SET_BROWSED_PLAYER; 134 little_endian_store_16(event, pos, browsing_cid); 135 pos += 2; 136 little_endian_store_16(event, pos, browsed_player_id); 137 pos += 2; 138 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 139 } 140 141 static void avrcp_browsing_target_emit_change_path(btstack_packet_handler_t callback, uint16_t browsing_cid, uint16_t uid_counter, avrcp_browsing_direction_t direction, uint8_t * item_id){ 142 btstack_assert(callback != NULL); 143 144 uint8_t event[19]; 145 int pos = 0; 146 event[pos++] = HCI_EVENT_AVRCP_META; 147 event[pos++] = sizeof(event) - 2; 148 event[pos++] = AVRCP_SUBEVENT_BROWSING_CHANGE_PATH; 149 little_endian_store_16(event, pos, browsing_cid); 150 pos += 2; 151 little_endian_store_16(event, pos, uid_counter); 152 pos += 2; 153 event[pos++] = direction; 154 memcpy(&event[pos], item_id, 8); 155 pos += 8; 156 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 157 } 158 159 static void avrcp_browsing_target_emit_get_item_attributes(btstack_packet_handler_t callback, uint16_t browsing_cid, uint16_t uid_counter, uint8_t scope, uint8_t * item_id, uint8_t attr_num, uint8_t * attr_list){ 160 btstack_assert(callback != NULL); 161 btstack_assert(attr_num <= AVRCP_MEDIA_ATTR_NUM); 162 163 uint8_t event[19 + 4 * AVRCP_MEDIA_ATTR_NUM]; 164 int pos = 0; 165 event[pos++] = HCI_EVENT_AVRCP_META; 166 event[pos++] = sizeof(event) - 2; 167 event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_ITEM_ATTRIBUTES; 168 little_endian_store_16(event, pos, browsing_cid); 169 pos += 2; 170 little_endian_store_16(event, pos, uid_counter); 171 pos += 2; 172 event[pos++] = scope; 173 memcpy(&event[pos], item_id, 8); 174 pos += 8; 175 uint16_t attr_len = attr_num * 4; 176 little_endian_store_16(event, pos, attr_len); 177 pos += 2; 178 179 memcpy(&event[pos], attr_list, attr_len); 180 pos += attr_len; 181 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 182 } 183 184 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 185 UNUSED(size); 186 avrcp_browsing_connection_t * browsing_connection; 187 avrcp_browsing_direction_t direction; 188 189 switch (packet_type) { 190 case L2CAP_DATA_PACKET:{ 191 browsing_connection = avrcp_get_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel); 192 if (!browsing_connection) break; 193 int pos = 0; 194 uint8_t transport_header = packet[pos++]; 195 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 196 browsing_connection->transaction_label = transport_header >> 4; 197 avctp_packet_type_t avctp_packet_type = (avctp_packet_type_t)((transport_header & 0x0F) >> 2); 198 switch (avctp_packet_type){ 199 case AVCTP_SINGLE_PACKET: 200 case AVCTP_START_PACKET: 201 browsing_connection->subunit_type = packet[pos++] >> 2; 202 browsing_connection->subunit_id = 0; 203 browsing_connection->command_opcode = packet[pos++]; 204 browsing_connection->num_packets = 1; 205 if (avctp_packet_type == AVCTP_START_PACKET){ 206 browsing_connection->num_packets = packet[pos++]; 207 } 208 browsing_connection->pdu_id = packet[pos++]; 209 uint16_t parameter_length = big_endian_read_16(packet, pos); 210 pos += 2; 211 212 switch(browsing_connection->pdu_id){ 213 case AVRCP_PDU_ID_GET_FOLDER_ITEMS: 214 if (parameter_length < 10){ 215 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 216 break; 217 } 218 219 browsing_connection->scope = packet[pos++]; 220 browsing_connection->start_item = big_endian_read_32(packet, pos); 221 pos += 4; 222 browsing_connection->end_item = big_endian_read_32(packet, pos); 223 pos += 4; 224 uint8_t attr_count = packet[pos++]; 225 browsing_connection->attr_bitmap = 0; 226 227 while (attr_count){ 228 uint32_t attr_id = big_endian_read_32(packet, pos); 229 pos += 4; 230 browsing_connection->attr_bitmap |= (1 << attr_id); 231 attr_count--; 232 } 233 avrcp_browsing_target_emit_get_folder_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection); 234 break; 235 236 case AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:{ 237 // send total num items 238 if (parameter_length != 1){ 239 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_SCOPE); 240 break; 241 } 242 243 browsing_connection->scope = packet[pos++]; 244 avrcp_browsing_target_emit_get_total_num_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection); 245 break; 246 } 247 case AVRCP_PDU_ID_SET_BROWSED_PLAYER: 248 // player_id (2) 249 if (parameter_length != 2){ 250 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 251 break; 252 } 253 if ( (pos + 2) > size ){ 254 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_PLAYER_ID); 255 break; 256 } 257 avrcp_browsing_target_emit_set_browsed_player(avrcp_target_context.browsing_avrcp_callback, channel, big_endian_read_16(packet, pos)); 258 break; 259 260 case AVRCP_PDU_ID_CHANGE_PATH: 261 // one level up or down in the virtual filesystem 262 if (parameter_length != 11){ 263 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 264 break; 265 } 266 browsing_connection->uid_counter = big_endian_read_16(packet, pos); 267 pos += 2; 268 browsing_connection->direction = (avrcp_browsing_direction_t)packet[pos++]; 269 270 if (browsing_connection->direction > AVRCP_BROWSING_DIRECTION_FOLDER_RFU){ 271 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_DIRECTION); 272 break; 273 } 274 memcpy(browsing_connection->item_uid, &packet[pos], 8); 275 avrcp_browsing_target_emit_change_path(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection->uid_counter, browsing_connection->direction, browsing_connection->item_uid); 276 break; 277 278 case AVRCP_PDU_ID_GET_ITEM_ATTRIBUTES:{ 279 if (parameter_length < 12){ 280 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 281 break; 282 } 283 284 browsing_connection->scope = packet[pos++]; 285 memcpy(browsing_connection->item_uid, &packet[pos], 8); 286 pos += 8; 287 browsing_connection->uid_counter = big_endian_read_16(packet, pos); 288 pos += 2; 289 browsing_connection->attr_list_size = packet[pos++]; 290 browsing_connection->attr_list = &packet[pos]; 291 292 avrcp_browsing_target_emit_get_item_attributes(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection->uid_counter, 293 browsing_connection->scope, browsing_connection->item_uid, browsing_connection->attr_list_size, browsing_connection->attr_list); 294 break; 295 } 296 297 default: 298 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 299 log_info("not parsed pdu ID 0x%02x", browsing_connection->pdu_id); 300 break; 301 } 302 browsing_connection->state = AVCTP_CONNECTION_OPENED; 303 break; 304 305 default: 306 break; 307 } 308 break; 309 } 310 311 case HCI_EVENT_PACKET: 312 switch (hci_event_packet_get_type(packet)){ 313 case L2CAP_EVENT_CAN_SEND_NOW: 314 browsing_connection = avrcp_get_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel); 315 if (browsing_connection->state != AVCTP_W2_SEND_RESPONSE) return; 316 browsing_connection->state = AVCTP_CONNECTION_OPENED; 317 avrcp_browsing_target_handle_can_send_now(browsing_connection); 318 break; 319 default: 320 break; 321 } 322 break; 323 324 default: 325 break; 326 } 327 } 328 329 void avrcp_browsing_target_init(void){ 330 avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler; 331 avrcp_browsing_register_target_packet_handler(avrcp_browsing_target_packet_handler); 332 } 333 334 void avrcp_browsing_target_deinit(void){ 335 avrcp_controller_context.browsing_packet_handler = NULL; 336 } 337 338 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){ 339 btstack_assert(callback != NULL); 340 avrcp_target_context.browsing_avrcp_callback = callback; 341 } 342 343 uint8_t avrcp_browsing_target_send_get_folder_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint8_t * attr_list, uint16_t attr_list_size, uint16_t num_items){ 344 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 345 if (!avrcp_connection){ 346 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 347 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 348 } 349 350 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 351 if (!connection){ 352 log_info("Could not find a browsing connection."); 353 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 354 } 355 if (connection->state != AVCTP_CONNECTION_OPENED){ 356 return ERROR_CODE_COMMAND_DISALLOWED; 357 } 358 359 // TODO: handle response to SetAddressedPlayer 360 361 uint16_t pos = 0; 362 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS; 363 uint8_t param_length = 5; 364 uint8_t param_length_pos = pos; 365 pos += 2; 366 367 avrcp_status_code_t status = AVRCP_STATUS_SUCCESS; 368 uint8_t status_pos = pos; 369 pos++; 370 371 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 372 pos += 2; 373 374 // TODO: fragmentation 375 if (attr_list_size > sizeof(connection->cmd_operands)){ 376 connection->attr_list = attr_list; 377 connection->attr_list_size = attr_list_size; 378 log_info(" todo: list too big, invoke fragmentation"); 379 return 1; 380 } 381 382 uint16_t items_byte_len = 0; 383 if (connection->start_item < num_items) { 384 if (connection->end_item < num_items) { 385 items_byte_len = connection->end_item - connection->start_item + 1; 386 } else { 387 items_byte_len = num_items - connection->start_item; 388 } 389 390 } else { 391 big_endian_store_16(connection->cmd_operands, pos, 0); 392 pos += 2; 393 } 394 big_endian_store_16(connection->cmd_operands, pos, items_byte_len); 395 pos += 2; 396 param_length += items_byte_len; 397 398 if (items_byte_len > 0){ 399 (void)memcpy(&connection->cmd_operands[pos], attr_list, attr_list_size); 400 pos += attr_list_size; 401 } else { 402 status = AVRCP_STATUS_RANGE_OUT_OF_BOUNDS; 403 } 404 405 big_endian_store_16(connection->cmd_operands, param_length_pos, param_length); 406 connection->cmd_operands[status_pos] = status; 407 408 btstack_assert(pos <= 400); 409 connection->cmd_operands_length = pos; 410 411 connection->state = AVCTP_W2_SEND_RESPONSE; 412 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 413 return ERROR_CODE_SUCCESS; 414 } 415 416 uint8_t avrcp_browsing_target_send_change_path_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, avrcp_status_code_t status, uint32_t num_items){ 417 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 418 if (!avrcp_connection){ 419 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 420 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 421 } 422 423 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 424 if (!connection){ 425 log_info("Could not find a browsing connection."); 426 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 427 } 428 if (connection->state != AVCTP_CONNECTION_OPENED){ 429 return ERROR_CODE_COMMAND_DISALLOWED; 430 } 431 432 uint16_t pos = 0; 433 connection->cmd_operands[pos++] = AVRCP_PDU_ID_CHANGE_PATH; 434 // Param length 435 big_endian_store_16(connection->cmd_operands, pos, 5); 436 pos += 2; 437 connection->cmd_operands[pos++] = status; 438 big_endian_store_32(connection->cmd_operands, pos, num_items); 439 pos += 4; 440 441 connection->cmd_operands_length = pos; 442 connection->state = AVCTP_W2_SEND_RESPONSE; 443 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 444 return ERROR_CODE_SUCCESS; 445 } 446 447 uint8_t avrcp_browsing_target_send_get_item_attributes_response(uint16_t avrcp_browsing_cid, avrcp_status_code_t status, uint8_t * attr_list, uint16_t attr_list_size, uint8_t num_items){ 448 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 449 if (!avrcp_connection){ 450 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 451 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 452 } 453 454 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 455 if (!connection){ 456 log_info("Could not find a browsing connection."); 457 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 458 } 459 if (connection->state != AVCTP_CONNECTION_OPENED){ 460 return ERROR_CODE_COMMAND_DISALLOWED; 461 } 462 463 // TODO: fragmentation 464 if (attr_list_size > (sizeof(connection->cmd_operands) - 5)){ 465 connection->attr_list = attr_list; 466 connection->attr_list_size = attr_list_size; 467 log_info(" todo: list too big, invoke fragmentation"); 468 return 1; 469 } 470 471 uint16_t pos = 0; 472 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_ITEM_ATTRIBUTES; 473 474 uint8_t param_length_pos = pos; 475 big_endian_store_16(connection->cmd_operands, pos, 1); 476 pos += 2; 477 connection->cmd_operands[pos++] = status; 478 479 if (status != AVRCP_STATUS_SUCCESS){ 480 connection->cmd_operands_length = pos; 481 connection->state = AVCTP_W2_SEND_RESPONSE; 482 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 483 return ERROR_CODE_SUCCESS; 484 } 485 486 connection->cmd_operands[pos++] = num_items; 487 (void)memcpy(&connection->cmd_operands[pos], attr_list, attr_list_size); 488 pos += attr_list_size; 489 490 big_endian_store_16(connection->cmd_operands, param_length_pos, pos - 3); 491 connection->cmd_operands_length = pos; 492 connection->state = AVCTP_W2_SEND_RESPONSE; 493 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 494 return ERROR_CODE_SUCCESS; 495 } 496 497 uint8_t avrcp_browsing_target_send_accept_set_browsed_player(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint16_t browsed_player_id, uint8_t * response, uint16_t response_size){ 498 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 499 if (!avrcp_connection){ 500 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 501 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 502 } 503 504 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 505 if (!connection){ 506 log_info("Could not find a browsing connection."); 507 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 508 } 509 510 if (connection->state != AVCTP_CONNECTION_OPENED) { 511 log_info("Browsing connection wrong state."); 512 return ERROR_CODE_COMMAND_DISALLOWED; 513 } 514 515 connection->browsed_player_id = browsed_player_id; 516 517 uint16_t pos = 0; 518 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_BROWSED_PLAYER; 519 big_endian_store_16(connection->cmd_operands, pos, response_size + 2 + 1); // uuid counter + status 520 pos += 2; 521 522 connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS; 523 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 524 pos += 2; 525 526 // TODO: fragmentation 527 if (response_size > sizeof(connection->cmd_operands)){ 528 connection->attr_list = response; 529 connection->attr_list_size = response_size; 530 log_info(" todo: list too big, invoke fragmentation"); 531 return 1; 532 } 533 534 (void)memcpy(&connection->cmd_operands[pos], response, response_size); 535 pos += response_size; 536 btstack_assert(pos <= 255); 537 connection->cmd_operands_length = (uint8_t) pos; 538 539 connection->state = AVCTP_W2_SEND_RESPONSE; 540 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 541 return ERROR_CODE_SUCCESS; 542 } 543 544 uint8_t avrcp_browsing_target_send_reject_set_browsed_player(uint16_t avrcp_browsing_cid, avrcp_status_code_t status){ 545 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 546 if (!avrcp_connection){ 547 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 548 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 549 } 550 551 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 552 if (!connection){ 553 log_info("Could not find a browsing connection."); 554 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 555 } 556 557 if (connection->state != AVCTP_CONNECTION_OPENED) { 558 log_info("Browsing connection wrong state."); 559 return ERROR_CODE_COMMAND_DISALLOWED; 560 } 561 562 int pos = 0; 563 connection->cmd_operands[pos++] = AVRCP_PDU_ID_SET_BROWSED_PLAYER; 564 big_endian_store_16(connection->cmd_operands, pos, 1); 565 pos += 2; 566 connection->cmd_operands[pos++] = status; 567 connection->cmd_operands_length = pos; 568 569 connection->state = AVCTP_W2_SEND_RESPONSE; 570 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 571 return ERROR_CODE_SUCCESS; 572 } 573 574 uint8_t avrcp_browsing_target_send_get_total_num_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint32_t total_num_items){ 575 avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid); 576 if (!avrcp_connection){ 577 log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid); 578 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 579 } 580 581 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 582 if (!connection){ 583 log_info("Could not find a browsing connection."); 584 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 585 } 586 587 if (connection->state != AVCTP_CONNECTION_OPENED) { 588 log_info("Browsing connection wrong state."); 589 return ERROR_CODE_COMMAND_DISALLOWED; 590 } 591 592 int pos = 0; 593 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS; 594 big_endian_store_16(connection->cmd_operands, pos, 7); 595 pos += 2; 596 connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS; 597 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 598 pos += 2; 599 big_endian_store_32(connection->cmd_operands, pos, total_num_items); 600 pos += 4; 601 connection->cmd_operands_length = pos; 602 603 connection->state = AVCTP_W2_SEND_RESPONSE; 604 avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid); 605 return ERROR_CODE_SUCCESS; 606 } 607 608