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_controller.c" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "btstack.h" 46 #include "classic/avrcp.h" 47 #include "classic/avrcp_browsing_controller.h" 48 49 void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context); 50 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 51 52 static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){ 53 btstack_linked_list_iterator_t it; 54 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 55 while (btstack_linked_list_iterator_has_next(&it)){ 56 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 57 if (connection->avrcp_browsing_cid != browsing_cid) continue; 58 return connection; 59 } 60 return NULL; 61 } 62 63 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){ 64 btstack_linked_list_iterator_t it; 65 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 66 while (btstack_linked_list_iterator_has_next(&it)){ 67 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 68 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue; 69 return connection; 70 } 71 return NULL; 72 } 73 74 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 75 btstack_linked_list_iterator_t it; 76 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 77 while (btstack_linked_list_iterator_has_next(&it)){ 78 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 79 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue; 80 return connection->browsing_connection; 81 } 82 return NULL; 83 } 84 85 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){ 86 if (!callback) return; 87 uint8_t event[12]; 88 int pos = 0; 89 event[pos++] = HCI_EVENT_AVRCP_META; 90 event[pos++] = sizeof(event) - 2; 91 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED; 92 event[pos++] = status; 93 reverse_bd_addr(addr,&event[pos]); 94 pos += 6; 95 little_endian_store_16(event, pos, browsing_cid); 96 pos += 2; 97 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 98 } 99 100 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){ 101 if (!callback) return; 102 uint8_t event[5]; 103 int pos = 0; 104 event[pos++] = HCI_EVENT_AVRCP_META; 105 event[pos++] = sizeof(event) - 2; 106 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED; 107 little_endian_store_16(event, pos, browsing_cid); 108 pos += 2; 109 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 110 } 111 112 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){ 113 avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get(); 114 memset(connection, 0, sizeof(avrcp_browsing_connection_t)); 115 connection->state = AVCTP_CONNECTION_IDLE; 116 connection->transaction_label = 0xFF; 117 avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid(); 118 avrcp_connection->browsing_connection = connection; 119 return connection; 120 } 121 122 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){ 123 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context); 124 125 if (!avrcp_connection){ 126 log_error("avrcp: there is no previously established AVRCP controller connection."); 127 return ERROR_CODE_COMMAND_DISALLOWED; 128 } 129 130 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 131 if (connection){ 132 log_error(" avrcp_browsing_connect connection exists."); 133 return ERROR_CODE_SUCCESS; 134 } 135 136 connection = avrcp_browsing_create_connection(avrcp_connection); 137 if (!connection){ 138 log_error("avrcp: could not allocate connection struct."); 139 return BTSTACK_MEMORY_ALLOC_FAILED; 140 } 141 142 if (!browsing_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 143 144 *browsing_cid = avrcp_connection->avrcp_browsing_cid; 145 connection->ertm_buffer = ertm_buffer; 146 connection->ertm_buffer_size = size; 147 avrcp_connection->browsing_connection = connection; 148 149 memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); 150 151 return l2cap_create_ertm_channel(avrcp_browsing_controller_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm, 152 &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL); 153 154 } 155 156 void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ 157 UNUSED(channel); 158 UNUSED(size); 159 bd_addr_t event_addr; 160 uint16_t local_cid; 161 uint8_t status; 162 avrcp_browsing_connection_t * connection = NULL; 163 avrcp_connection_t * avrcp_connection = NULL; 164 165 if (packet_type != HCI_EVENT_PACKET) return; 166 167 switch (hci_event_packet_get_type(packet)) { 168 case HCI_EVENT_DISCONNECTION_COMPLETE: 169 avrcp_emit_browsing_connection_closed(context->avrcp_callback, 0); 170 break; 171 case L2CAP_EVENT_INCOMING_CONNECTION: 172 l2cap_event_incoming_connection_get_address(packet, event_addr); 173 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 174 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 175 if (!avrcp_connection) { 176 log_error("No previously created AVRCP controller connections"); 177 l2cap_decline_connection(local_cid); 178 break; 179 } 180 connection = avrcp_browsing_create_connection(avrcp_connection); 181 avrcp_connection->browsing_connection = connection; 182 connection->l2cap_browsing_cid = local_cid; 183 connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 184 log_info("L2CAP_EVENT_INCOMING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x", avrcp_connection->avrcp_browsing_cid, connection->l2cap_browsing_cid); 185 // l2cap_accept_connection(local_cid); 186 log_error("L2CAP Accepting incoming connection request in ERTM."); 187 l2cap_accept_ertm_connection(local_cid, &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size); 188 break; 189 190 case L2CAP_EVENT_CHANNEL_OPENED: 191 l2cap_event_channel_opened_get_address(packet, event_addr); 192 status = l2cap_event_channel_opened_get_status(packet); 193 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 194 195 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 196 if (!avrcp_connection){ 197 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr)); 198 avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST); 199 l2cap_disconnect(local_cid, 0); // reason isn't used 200 break; 201 } 202 203 connection = avrcp_connection->browsing_connection; 204 if (!connection){ 205 log_error("Failed to alloc AVRCP connection structure"); 206 avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, BTSTACK_MEMORY_ALLOC_FAILED); 207 l2cap_disconnect(local_cid, 0); // reason isn't used 208 break; 209 } 210 211 if (status != ERROR_CODE_SUCCESS){ 212 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 213 avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status); 214 btstack_memory_avrcp_browsing_connection_free(connection); 215 avrcp_connection->browsing_connection = NULL; 216 break; 217 } 218 connection->l2cap_browsing_cid = local_cid; 219 220 log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, connection->l2cap_browsing_cid); 221 connection->state = AVCTP_CONNECTION_OPENED; 222 avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS); 223 break; 224 225 case L2CAP_EVENT_CHANNEL_CLOSED: 226 // data: event (8), len(8), channel (16) 227 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 228 avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context); 229 230 if (avrcp_connection && avrcp_connection->browsing_connection){ 231 avrcp_emit_browsing_connection_closed(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid); 232 // free connection 233 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); 234 avrcp_connection->browsing_connection = NULL; 235 break; 236 } 237 break; 238 default: 239 break; 240 } 241 } 242 243 static int avrcp_browsing_controller_send_get_folder_items_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){ 244 uint8_t command[100]; 245 int pos = 0; 246 // transport header 247 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 248 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 249 // Profile IDentifier (PID) 250 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 251 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 252 command[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS; 253 254 uint32_t attribute_count = 0; 255 uint32_t attributes_to_copy = 0; 256 257 switch (connection->attr_bitmap){ 258 case AVRCP_MEDIA_ATTR_NONE: 259 attribute_count = AVRCP_MEDIA_ATTR_NONE; // 0xFFFFFFFF 260 break; 261 case AVRCP_MEDIA_ATTR_ALL: 262 attribute_count = AVRCP_MEDIA_ATTR_ALL; // 0 263 break; 264 default: 265 attribute_count = count_set_bits_uint32(connection->attr_bitmap & 0xff); 266 attributes_to_copy = attribute_count; 267 break; 268 } 269 270 big_endian_store_16(command, pos, 10 + attribute_count); 271 pos += 2; 272 command[pos++] = connection->scope; 273 big_endian_store_32(command, pos, connection->start_item); 274 pos += 4; 275 big_endian_store_32(command, pos, connection->end_item); 276 pos += 4; 277 command[pos++] = attribute_count; 278 279 int bit_position = 1; 280 while (attributes_to_copy){ 281 if (connection->attr_bitmap & (1 << bit_position)){ 282 big_endian_store_32(command, pos, bit_position); 283 pos += 4; 284 attributes_to_copy--; 285 } 286 bit_position++; 287 } 288 289 return l2cap_send(cid, command, pos); 290 } 291 292 static int avrcp_browsing_controller_send_change_path_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){ 293 uint8_t command[100]; 294 int pos = 0; 295 // transport header 296 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 297 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 298 // Profile IDentifier (PID) 299 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 300 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 301 command[pos++] = AVRCP_PDU_ID_CHANGE_PATH; 302 303 big_endian_store_16(command, pos, 11); 304 pos += 2; 305 big_endian_store_16(command, pos, connection->browsed_player_uid_counter); 306 pos += 2; 307 command[pos++] = connection->direction; 308 memcpy(command+pos, connection->folder_uid, 8); 309 pos += 8; 310 return l2cap_send(cid, command, pos); 311 } 312 313 static int avrcp_browsing_controller_send_set_browsed_player_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){ 314 uint8_t command[100]; 315 int pos = 0; 316 // transport header 317 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 318 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 319 // Profile IDentifier (PID) 320 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 321 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 322 command[pos++] = AVRCP_PDU_ID_SET_BROWSED_PLAYER; 323 324 big_endian_store_16(command, pos, 2); 325 pos += 2; 326 big_endian_store_16(command, pos, connection->browsed_player_id); 327 pos += 2; 328 return l2cap_send(cid, command, pos); 329 } 330 331 static int avrcp_browsing_controller_send_set_addressed_player_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){ 332 uint8_t command[100]; 333 int pos = 0; 334 // transport header 335 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 336 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0; 337 // Profile IDentifier (PID) 338 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 339 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 340 command[pos++] = AVRCP_PDU_ID_SET_ADDRESSED_PLAYER; 341 342 big_endian_store_16(command, pos, 2); 343 pos += 2; 344 big_endian_store_16(command, pos, connection->addressed_player_id); 345 pos += 2; 346 return l2cap_send(cid, command, pos); 347 } 348 349 static void avrcp_browsing_controller_handle_can_send_now(avrcp_browsing_connection_t * connection){ 350 switch (connection->state){ 351 case AVCTP_CONNECTION_OPENED: 352 if (connection->get_folder_item){ 353 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 354 connection->get_folder_item = 0; 355 avrcp_browsing_controller_send_get_folder_items_cmd(connection->l2cap_browsing_cid, connection); 356 break; 357 } 358 if (connection->change_path){ 359 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 360 connection->change_path = 0; 361 avrcp_browsing_controller_send_change_path_cmd(connection->l2cap_browsing_cid, connection); 362 break; 363 } 364 365 if (connection->set_browsed_player_id){ 366 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 367 connection->set_browsed_player_id = 0; 368 avrcp_browsing_controller_send_set_browsed_player_cmd(connection->l2cap_browsing_cid, connection); 369 break; 370 } 371 372 if (connection->set_addressed_player_id){ 373 connection->state = AVCTP_W2_RECEIVE_RESPONSE; 374 connection->set_addressed_player_id = 0; 375 avrcp_browsing_controller_send_set_addressed_player_cmd(connection->l2cap_browsing_cid, connection); 376 break; 377 } 378 default: 379 return; 380 } 381 } 382 383 static void avrcp_browsing_controller_emit_done(btstack_packet_handler_t callback, uint16_t browsing_cid, uint8_t browsing_status, uint8_t bluetooth_status){ 384 if (!callback) return; 385 uint8_t event[6]; 386 int pos = 0; 387 event[pos++] = HCI_EVENT_AVRCP_META; 388 event[pos++] = sizeof(event) - 2; 389 event[pos++] = AVRCP_SUBEVENT_BROWSING_MEDIA_ITEM_DONE; 390 little_endian_store_16(event, pos, browsing_cid); 391 pos += 2; 392 event[pos++] = browsing_status; 393 event[pos++] = bluetooth_status; 394 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 395 } 396 397 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 398 avrcp_browsing_connection_t * browsing_connection; 399 400 switch (packet_type) { 401 case L2CAP_DATA_PACKET:{ 402 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context); 403 if (!browsing_connection) break; 404 browsing_connection->state = AVCTP_CONNECTION_OPENED; 405 406 int pos = 3; 407 if (size < pos + 4){ 408 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND, ERROR_CODE_SUCCESS); 409 break; 410 } 411 412 avrcp_pdu_id_t pdu_id = packet[pos++]; 413 uint16_t length = big_endian_read_16(packet, pos); 414 pos += 2; 415 if (size + pos < length){ 416 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND, ERROR_CODE_SUCCESS); 417 break; 418 } 419 420 uint8_t browsing_status = packet[pos++]; 421 if (browsing_status != AVRCP_BROWSING_ERROR_CODE_SUCCESS){ 422 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, browsing_status, ERROR_CODE_SUCCESS); 423 break; 424 } 425 426 uint32_t i; 427 switch(pdu_id){ 428 case AVRCP_PDU_ID_CHANGE_PATH: 429 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER: 430 break; 431 case AVRCP_PDU_ID_SET_BROWSED_PLAYER:{ 432 browsing_connection->browsed_player_uid_counter = big_endian_read_16(packet, pos); 433 pos += 2; 434 uint32_t num_items = big_endian_read_32(packet, pos); 435 pos += 4; 436 437 for (i = 0; i < num_items; i++){ 438 uint16_t browsable_item_length = 5 + big_endian_read_16(packet, pos+3); 439 // reuse byte to put the new type AVRCP_BROWSING_MEDIA_ROOT_FOLDER 440 packet[pos-1] = AVRCP_BROWSING_MEDIA_ROOT_FOLDER; 441 (*avrcp_controller_context.avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, channel, packet+pos, browsable_item_length+1); 442 pos += browsable_item_length; 443 } 444 break; 445 } 446 case AVRCP_PDU_ID_GET_FOLDER_ITEMS:{ 447 // uint16_t uid_counter = big_endian_read_16(packet, pos); 448 pos += 2; 449 uint16_t num_items = big_endian_read_16(packet, pos); 450 pos += 2; 451 452 for (i = 0; i < num_items; i++){ 453 uint16_t browsable_item_length = 3 + big_endian_read_16(packet, pos+1); 454 (*avrcp_controller_context.avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, channel, packet+pos, browsable_item_length); 455 pos += browsable_item_length; 456 } 457 break; 458 } 459 default: 460 return; 461 } 462 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, browsing_status, ERROR_CODE_SUCCESS); 463 break; 464 } 465 case HCI_EVENT_PACKET: 466 switch (hci_event_packet_get_type(packet)){ 467 case L2CAP_EVENT_CAN_SEND_NOW: 468 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context); 469 if (!browsing_connection) break; 470 avrcp_browsing_controller_handle_can_send_now(browsing_connection); 471 break; 472 default: 473 avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context); 474 break; 475 } 476 default: 477 break; 478 } 479 } 480 481 void avrcp_browsing_controller_init(void){ 482 avrcp_controller_context.browsing_packet_handler = avrcp_browsing_controller_packet_handler; 483 l2cap_register_service(&avrcp_browsing_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 484 } 485 486 uint8_t avrcp_browsing_controller_connect(bd_addr_t bd_addr, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){ 487 return avrcp_browsing_connect(bd_addr, &avrcp_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid); 488 } 489 490 uint8_t avrcp_browsing_controller_disconnect(uint16_t avrcp_browsing_cid){ 491 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); 492 if (!avrcp_connection){ 493 log_error("avrcp_browsing_controller_disconnect: could not find a connection."); 494 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 495 } 496 if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 497 498 l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0); 499 return ERROR_CODE_SUCCESS; 500 } 501 502 /** 503 * @brief Retrieve a listing of the contents of a folder. 504 * @param scope 0-player list, 1-virtual file system, 2-search, 3-now playing 505 * @param start_item 506 * @param end_item 507 * @param attribute_count 508 * @param attribute_list 509 **/ 510 static uint8_t avrcp_browsing_controller_get_folder_items(uint16_t avrcp_browsing_cid, uint8_t scope, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){ 511 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); 512 if (!avrcp_connection){ 513 log_error("avrcp_browsing_controller_disconnect: could not find a connection."); 514 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 515 } 516 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 517 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 518 519 connection->get_folder_item = 1; 520 connection->scope = scope; 521 connection->start_item = start_item; 522 connection->end_item = end_item; 523 connection->attr_bitmap = attr_bitmap; 524 525 avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid); 526 return ERROR_CODE_SUCCESS; 527 } 528 529 uint8_t avrcp_browsing_controller_get_media_players(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){ 530 return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 0, start_item, end_item, attr_bitmap); 531 } 532 533 uint8_t avrcp_browsing_controller_browse_file_system(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){ 534 // return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 1, 0, 0xFFFFFFFF, attr_bitmap); 535 return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 1, start_item, end_item, attr_bitmap); 536 } 537 538 uint8_t avrcp_browsing_controller_browse_media(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){ 539 // return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 2, 0, 0xFFFFFFFF, 0, NULL); 540 return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 2, start_item, end_item, attr_bitmap); 541 } 542 543 uint8_t avrcp_browsing_controller_browse_now_playing_list(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){ 544 return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 3, start_item, end_item, attr_bitmap); 545 } 546 547 548 uint8_t avrcp_browsing_controller_set_browsed_player(uint16_t avrcp_browsing_cid, uint16_t browsed_player_id){ 549 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); 550 if (!avrcp_connection){ 551 log_error("avrcp_browsing_controller_change_path: could not find a connection."); 552 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 553 } 554 555 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 556 if (connection->state != AVCTP_CONNECTION_OPENED){ 557 log_error("avrcp_browsing_controller_change_path: connection in wrong state."); 558 return ERROR_CODE_COMMAND_DISALLOWED; 559 } 560 561 connection->set_browsed_player_id = 1; 562 connection->browsed_player_id = browsed_player_id; 563 avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid); 564 return ERROR_CODE_SUCCESS; 565 } 566 567 uint8_t avrcp_browsing_controller_set_addressed_player(uint16_t avrcp_browsing_cid, uint16_t addressed_player_id){ 568 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); 569 if (!avrcp_connection){ 570 log_error("avrcp_browsing_controller_change_path: could not find a connection."); 571 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 572 } 573 574 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 575 if (connection->state != AVCTP_CONNECTION_OPENED){ 576 log_error("avrcp_browsing_controller_change_path: connection in wrong state."); 577 return ERROR_CODE_COMMAND_DISALLOWED; 578 } 579 580 connection->set_addressed_player_id = 1; 581 connection->addressed_player_id = addressed_player_id; 582 avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid); 583 return ERROR_CODE_SUCCESS; 584 } 585 586 /** 587 * @brief Retrieve a listing of the contents of a folder. 588 * @param direction 0-folder up, 1-folder down 589 * @param folder_uid 8 bytes long 590 **/ 591 uint8_t avrcp_browsing_controller_change_path(uint16_t avrcp_browsing_cid, uint8_t direction, uint8_t * folder_uid){ 592 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context); 593 if (!avrcp_connection){ 594 log_error("avrcp_browsing_controller_change_path: could not find a connection."); 595 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 596 } 597 598 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 599 if (connection->state != AVCTP_CONNECTION_OPENED){ 600 log_error("avrcp_browsing_controller_change_path: connection in wrong state."); 601 return ERROR_CODE_COMMAND_DISALLOWED; 602 } 603 604 if (!connection->browsed_player_id){ 605 log_error("avrcp_browsing_controller_change_path: no browsed player set."); 606 return ERROR_CODE_COMMAND_DISALLOWED; 607 } 608 609 connection->change_path = 1; 610 connection->direction = direction; 611 memcpy(connection->folder_uid, folder_uid, 8); 612 avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid); 613 return ERROR_CODE_SUCCESS; 614 } 615 616 uint8_t avrcp_browsing_controller_go_up_one_level(uint16_t avrcp_browsing_cid){ 617 return avrcp_browsing_controller_change_path(avrcp_browsing_cid, 0, 0); 618 } 619 620 uint8_t avrcp_browsing_controller_go_down_one_level(uint16_t avrcp_browsing_cid, uint8_t * folder_uid){ 621 return avrcp_browsing_controller_change_path(avrcp_browsing_cid, 1, folder_uid); 622 } 623