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 MATTHIAS 24 * RINGWALD 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 <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <inttypes.h> 45 #include "btstack.h" 46 #include "classic/avrcp.h" 47 #include "classic/avrcp_browsing_target.h" 48 49 #define PSM_AVCTP_BROWSING 0x001b 50 51 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context); 52 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 53 54 static void avrcp_browsing_target_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){ 55 connection->wait_to_send = 1; 56 l2cap_request_can_send_now_event(l2cap_cid); 57 } 58 59 static int avrcp_browsing_target_handle_can_send_now(avrcp_browsing_connection_t * connection){ 60 int pos = 0; 61 // printf("avrcp_browsing_target_handle_can_send_now, cmd_operands_length %d\n", connection->cmd_operands_length); 62 // printf_hexdump(connection->cmd_operands, connection->cmd_operands_length); 63 64 // l2cap_reserve_packet_buffer(); 65 // uint8_t * packet = l2cap_get_outgoing_buffer(); 66 uint8_t packet[300]; 67 connection->packet_type = AVRCP_SINGLE_PACKET; 68 69 packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 70 // Profile IDentifier (PID) 71 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 72 packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 73 memcpy(packet+pos, connection->cmd_operands, connection->cmd_operands_length); 74 75 pos += connection->cmd_operands_length; 76 connection->wait_to_send = 0; 77 // return l2cap_send_prepared(connection->l2cap_browsing_cid, pos); 78 return l2cap_send(connection->l2cap_browsing_cid, packet, pos); 79 } 80 81 82 static uint8_t avrcp_browsing_target_response_general_reject(avrcp_browsing_connection_t * connection, avrcp_status_code_t status){ 83 // AVRCP_CTYPE_RESPONSE_REJECTED 84 int pos = 0; 85 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT; 86 // connection->cmd_operands[pos++] = 0; 87 // param length 88 big_endian_store_16(connection->cmd_operands, pos, 1); 89 pos += 2; 90 connection->cmd_operands[pos++] = status; 91 connection->cmd_operands_length = 4; 92 connection->state = AVCTP_W2_SEND_RESPONSE; 93 avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid); 94 return ERROR_CODE_SUCCESS; 95 } 96 97 static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){ 98 btstack_linked_list_iterator_t it; 99 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 100 while (btstack_linked_list_iterator_has_next(&it)){ 101 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 102 if (connection->avrcp_browsing_cid != browsing_cid) continue; 103 return connection; 104 } 105 return NULL; 106 } 107 108 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){ 109 btstack_linked_list_iterator_t it; 110 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 111 while (btstack_linked_list_iterator_has_next(&it)){ 112 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 113 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue; 114 return connection; 115 } 116 return NULL; 117 } 118 119 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 120 btstack_linked_list_iterator_t it; 121 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 122 while (btstack_linked_list_iterator_has_next(&it)){ 123 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 124 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue; 125 return connection->browsing_connection; 126 } 127 return NULL; 128 } 129 130 static void avrcp_browsing_target_emit_get_folder_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){ 131 if (!callback) return; 132 uint8_t event[10]; 133 int pos = 0; 134 event[pos++] = HCI_EVENT_AVRCP_META; 135 event[pos++] = sizeof(event) - 2; 136 event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_FOLDER_ITEMS; 137 little_endian_store_16(event, pos, browsing_cid); 138 pos += 2; 139 event[pos++] = connection->scope; 140 big_endian_store_32(event, pos, connection->attr_bitmap); 141 pos += 4; 142 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 143 } 144 145 static void avrcp_browsing_target_emit_get_total_num_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){ 146 if (!callback) return; 147 uint8_t event[10]; 148 int pos = 0; 149 event[pos++] = HCI_EVENT_AVRCP_META; 150 event[pos++] = sizeof(event) - 2; 151 event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_TOTAL_NUM_ITEMS; 152 little_endian_store_16(event, pos, browsing_cid); 153 pos += 2; 154 event[pos++] = connection->scope; 155 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 156 } 157 158 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){ 159 if (!callback) return; 160 uint8_t event[12]; 161 int pos = 0; 162 event[pos++] = HCI_EVENT_AVRCP_META; 163 event[pos++] = sizeof(event) - 2; 164 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED; 165 event[pos++] = status; 166 reverse_bd_addr(addr,&event[pos]); 167 pos += 6; 168 little_endian_store_16(event, pos, browsing_cid); 169 pos += 2; 170 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 171 } 172 173 static void avrcp_emit_incoming_browsing_connection(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr){ 174 if (!callback) return; 175 uint8_t event[11]; 176 int pos = 0; 177 event[pos++] = HCI_EVENT_AVRCP_META; 178 event[pos++] = sizeof(event) - 2; 179 event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION; 180 reverse_bd_addr(addr,&event[pos]); 181 pos += 6; 182 little_endian_store_16(event, pos, browsing_cid); 183 pos += 2; 184 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 185 } 186 187 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){ 188 if (!callback) return; 189 uint8_t event[5]; 190 int pos = 0; 191 event[pos++] = HCI_EVENT_AVRCP_META; 192 event[pos++] = sizeof(event) - 2; 193 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED; 194 little_endian_store_16(event, pos, browsing_cid); 195 pos += 2; 196 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 197 } 198 199 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){ 200 avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get(); 201 memset(connection, 0, sizeof(avrcp_browsing_connection_t)); 202 connection->state = AVCTP_CONNECTION_IDLE; 203 connection->transaction_label = 0xFF; 204 avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid(); 205 avrcp_connection->browsing_connection = connection; 206 return connection; 207 } 208 209 static uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, avrcp_context_t * context, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * browsing_cid){ 210 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context); 211 212 if (!avrcp_connection){ 213 log_error("avrcp: there is no previously established AVRCP controller connection."); 214 return ERROR_CODE_COMMAND_DISALLOWED; 215 } 216 217 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 218 if (connection){ 219 log_error(" avrcp_browsing_connect connection exists."); 220 return ERROR_CODE_SUCCESS; 221 } 222 223 connection = avrcp_browsing_create_connection(avrcp_connection); 224 if (!connection){ 225 log_error("avrcp: could not allocate connection struct."); 226 return BTSTACK_MEMORY_ALLOC_FAILED; 227 } 228 229 if (browsing_cid){ 230 *browsing_cid = avrcp_connection->avrcp_browsing_cid; 231 } 232 233 connection->ertm_buffer = ertm_buffer; 234 connection->ertm_buffer_size = size; 235 avrcp_connection->browsing_connection = connection; 236 237 memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); 238 239 return l2cap_create_ertm_channel(avrcp_browsing_target_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm, 240 &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL); 241 242 } 243 244 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ 245 UNUSED(channel); 246 UNUSED(size); 247 bd_addr_t event_addr; 248 uint16_t local_cid; 249 uint8_t status; 250 avrcp_browsing_connection_t * browsing_connection = NULL; 251 avrcp_connection_t * avrcp_connection = NULL; 252 253 if (packet_type != HCI_EVENT_PACKET) return; 254 255 switch (hci_event_packet_get_type(packet)) { 256 case HCI_EVENT_DISCONNECTION_COMPLETE: 257 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, 0); 258 break; 259 case L2CAP_EVENT_INCOMING_CONNECTION: 260 l2cap_event_incoming_connection_get_address(packet, event_addr); 261 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 262 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 263 if (!avrcp_connection) { 264 log_error("No previously created AVRCP controller connections"); 265 l2cap_decline_connection(local_cid); 266 break; 267 } 268 browsing_connection = avrcp_browsing_create_connection(avrcp_connection); 269 browsing_connection->l2cap_browsing_cid = local_cid; 270 browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION; 271 log_info("Emit AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x\n", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid); 272 avrcp_emit_incoming_browsing_connection(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr); 273 break; 274 275 case L2CAP_EVENT_CHANNEL_OPENED: 276 l2cap_event_channel_opened_get_address(packet, event_addr); 277 status = l2cap_event_channel_opened_get_status(packet); 278 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 279 280 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 281 if (!avrcp_connection){ 282 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr)); 283 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST); 284 l2cap_disconnect(local_cid, 0); // reason isn't used 285 break; 286 } 287 288 browsing_connection = avrcp_connection->browsing_connection; 289 if (status != ERROR_CODE_SUCCESS){ 290 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 291 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status); 292 btstack_memory_avrcp_browsing_connection_free(browsing_connection); 293 avrcp_connection->browsing_connection = NULL; 294 break; 295 } 296 if (browsing_connection->state != AVCTP_CONNECTION_W4_L2CAP_CONNECTED) break; 297 298 browsing_connection->l2cap_browsing_cid = local_cid; 299 300 log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid); 301 browsing_connection->state = AVCTP_CONNECTION_OPENED; 302 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS); 303 break; 304 305 case L2CAP_EVENT_CHANNEL_CLOSED: 306 // data: event (8), len(8), channel (16) 307 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 308 avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context); 309 310 if (avrcp_connection && avrcp_connection->browsing_connection){ 311 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid); 312 // free connection 313 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); 314 avrcp_connection->browsing_connection = NULL; 315 break; 316 } 317 break; 318 default: 319 break; 320 } 321 } 322 323 324 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 325 avrcp_browsing_connection_t * browsing_connection; 326 327 switch (packet_type) { 328 case L2CAP_DATA_PACKET:{ 329 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context); 330 if (!browsing_connection) break; 331 // printf_hexdump(packet,size); 332 int pos = 0; 333 uint8_t transport_header = packet[pos++]; 334 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 335 browsing_connection->transaction_label = transport_header >> 4; 336 avrcp_packet_type_t avctp_packet_type = (transport_header & 0x0F) >> 2; 337 // printf("L2CAP_DATA_PACKET, transaction_label %d\n", browsing_connection->transaction_label); 338 switch (avctp_packet_type){ 339 case AVRCP_SINGLE_PACKET: 340 case AVRCP_START_PACKET: 341 // uint8_t frame_type = (transport_header & 0x03) >> 1; 342 // uint8_t ipid = transport_header & 0x01; 343 browsing_connection->subunit_type = packet[pos++] >> 2; 344 browsing_connection->subunit_id = 0; 345 browsing_connection->command_opcode = packet[pos++]; 346 // printf("subunit_id") 347 // pos += 2; 348 browsing_connection->num_packets = 1; 349 if (avctp_packet_type == AVRCP_START_PACKET){ 350 browsing_connection->num_packets = packet[pos++]; 351 } 352 browsing_connection->pdu_id = packet[pos++]; 353 // uint16_t length = big_endian_read_16(packet, pos); 354 // pos += 2; 355 break; 356 default: 357 break; 358 } 359 // printf("pdu id 0x%2x\n", browsing_connection->pdu_id); 360 // uint32_t i; 361 switch (avctp_packet_type){ 362 case AVRCP_SINGLE_PACKET: 363 case AVRCP_END_PACKET: 364 switch(browsing_connection->pdu_id){ 365 case AVRCP_PDU_ID_GET_FOLDER_ITEMS: 366 printf("\n"); 367 browsing_connection->scope = packet[pos++]; 368 browsing_connection->start_item = big_endian_read_32(packet, pos); 369 pos += 4; 370 browsing_connection->end_item = big_endian_read_32(packet, pos); 371 pos += 4; 372 uint8_t attr_count = packet[pos++]; 373 374 while (attr_count){ 375 uint32_t attr_id = big_endian_read_32(packet, pos); 376 pos += 4; 377 browsing_connection->attr_bitmap |= (1 << attr_id); 378 attr_count--; 379 } 380 avrcp_browsing_target_emit_get_folder_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection); 381 break; 382 case AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:{ 383 // send total num items 384 browsing_connection->scope = packet[pos++]; 385 avrcp_browsing_target_emit_get_total_num_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection); 386 // uint32_t num_items = big_endian_read_32(packet, pos); 387 // pos += 4; 388 break; 389 } 390 default: 391 // printf("send avrcp_browsing_target_response_general_reject\n"); 392 avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND); 393 log_info(" not parsed pdu ID 0x%02x", browsing_connection->pdu_id); 394 break; 395 } 396 browsing_connection->state = AVCTP_CONNECTION_OPENED; 397 // avrcp_browsing_target_emit_done_with_uid_counter(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection->uid_counter, browsing_connection->browsing_status, ERROR_CODE_SUCCESS); 398 break; 399 default: 400 break; 401 } 402 // printf(" paket done\n"); 403 break; 404 } 405 406 case HCI_EVENT_PACKET: 407 switch (hci_event_packet_get_type(packet)){ 408 case L2CAP_EVENT_CAN_SEND_NOW: 409 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context); 410 if (!browsing_connection) break; 411 if (browsing_connection->state != AVCTP_W2_SEND_RESPONSE) return; 412 browsing_connection->state = AVCTP_CONNECTION_OPENED; 413 avrcp_browsing_target_handle_can_send_now(browsing_connection); 414 break; 415 default: 416 avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_target_context); 417 break; 418 } 419 break; 420 421 default: 422 break; 423 } 424 } 425 426 void avrcp_browsing_target_init(void){ 427 avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler; 428 l2cap_register_service(&avrcp_browsing_target_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2); 429 } 430 431 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){ 432 if (callback == NULL){ 433 log_error("avrcp_browsing_target_register_packet_handler called with NULL callback"); 434 return; 435 } 436 avrcp_target_context.browsing_avrcp_callback = callback; 437 } 438 439 uint8_t avrcp_browsing_target_connect(bd_addr_t bd_addr, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){ 440 return avrcp_browsing_connect(bd_addr, &avrcp_target_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid); 441 } 442 443 uint8_t avrcp_browsing_target_disconnect(uint16_t avrcp_browsing_cid){ 444 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 445 if (!avrcp_connection){ 446 log_error("avrcp_browsing_target_disconnect: could not find a connection."); 447 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 448 } 449 if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 450 451 l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0); 452 return ERROR_CODE_SUCCESS; 453 } 454 455 uint8_t avrcp_browsing_target_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config){ 456 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 457 if (!avrcp_connection){ 458 log_error("avrcp_browsing_decline_incoming_connection: could not find a connection."); 459 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 460 } 461 if (!avrcp_connection->browsing_connection){ 462 log_error("avrcp_browsing_decline_incoming_connection: no browsing connection."); 463 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 464 } 465 466 if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){ 467 log_error("avrcp_browsing_decline_incoming_connection: browsing connection in a wrong state."); 468 return ERROR_CODE_COMMAND_DISALLOWED; 469 } 470 471 avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 472 avrcp_connection->browsing_connection->ertm_buffer = ertm_buffer; 473 avrcp_connection->browsing_connection->ertm_buffer_size = size; 474 memcpy(&avrcp_connection->browsing_connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); 475 l2cap_accept_ertm_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid, &avrcp_connection->browsing_connection->ertm_config, avrcp_connection->browsing_connection->ertm_buffer, avrcp_connection->browsing_connection->ertm_buffer_size); 476 return ERROR_CODE_SUCCESS; 477 } 478 479 uint8_t avrcp_browsing_target_decline_incoming_connection(uint16_t avrcp_browsing_cid){ 480 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 481 if (!avrcp_connection){ 482 log_error("avrcp_browsing_decline_incoming_connection: could not find a connection."); 483 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 484 } 485 if (!avrcp_connection->browsing_connection) return ERROR_CODE_SUCCESS; 486 if (avrcp_connection->browsing_connection->state > AVCTP_CONNECTION_W4_ERTM_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED; 487 488 l2cap_decline_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid); 489 // free connection 490 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); 491 avrcp_connection->browsing_connection = NULL; 492 return ERROR_CODE_SUCCESS; 493 } 494 495 uint8_t avrcp_subevent_browsing_get_folder_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint8_t * attr_list, uint16_t attr_list_size){ 496 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 497 if (!avrcp_connection){ 498 log_error("avrcp_browsing_controller_disconnect: could not find a connection."); 499 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 500 } 501 502 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 503 if (!connection){ 504 log_info("avrcp_subevent_browsing_get_folder_items_response: could not find a connection."); 505 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 506 } 507 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 508 509 if (connection->state != AVCTP_CONNECTION_OPENED) { 510 log_info("avrcp_subevent_browsing_get_folder_items_response: wrong state."); 511 return ERROR_CODE_COMMAND_DISALLOWED; 512 } 513 int pos = 0; 514 515 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS; 516 big_endian_store_16(connection->cmd_operands, pos, attr_list_size); 517 pos += 2; 518 519 connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS; 520 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 521 pos += 2; 522 523 // TODO: fragmentation 524 if (attr_list_size > sizeof(connection->cmd_operands)){ 525 connection->attr_list = attr_list; 526 connection->attr_list_size = attr_list_size; 527 log_info(" todo: list too big, invoke fragmentation"); 528 return 1; 529 } 530 memcpy(&connection->cmd_operands[pos], attr_list, attr_list_size); 531 pos += attr_list_size; 532 connection->cmd_operands_length = pos; 533 // printf_hexdump(connection->cmd_operands, connection->cmd_operands_length); 534 535 connection->state = AVCTP_W2_SEND_RESPONSE; 536 avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid); 537 return ERROR_CODE_SUCCESS; 538 } 539 540 541 uint8_t avrcp_subevent_browsing_get_total_num_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint32_t total_num_items){ 542 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 543 if (!avrcp_connection){ 544 log_error("avrcp_browsing_controller_disconnect: could not find a connection."); 545 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 546 } 547 548 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 549 if (!connection){ 550 log_info("avrcp_subevent_browsing_get_folder_items_response: could not find a connection."); 551 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 552 } 553 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 554 555 if (connection->state != AVCTP_CONNECTION_OPENED) { 556 log_info("avrcp_subevent_browsing_get_folder_items_response: wrong state."); 557 return ERROR_CODE_COMMAND_DISALLOWED; 558 } 559 560 int pos = 0; 561 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS; 562 big_endian_store_16(connection->cmd_operands, pos, 7); 563 pos += 2; 564 connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS; 565 big_endian_store_16(connection->cmd_operands, pos, uid_counter); 566 pos += 2; 567 big_endian_store_32(connection->cmd_operands, pos, total_num_items); 568 pos += 4; 569 connection->cmd_operands_length = pos; 570 // printf_hexdump(connection->cmd_operands, connection->cmd_operands_length); 571 572 connection->state = AVCTP_W2_SEND_RESPONSE; 573 avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid); 574 return ERROR_CODE_SUCCESS; 575 } 576 577 578 579 /* 580 int pos = 0; 581 connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT; 582 // connection->cmd_operands[pos++] = 0; 583 // param length 584 big_endian_store_16(connection->cmd_operands, pos, 1); 585 pos += 2; 586 connection->cmd_operands[pos++] = status; 587 connection->cmd_operands_length = 4; 588 connection->state = AVCTP_W2_SEND_RESPONSE; 589 */ 590 591