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 avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){ 55 btstack_linked_list_iterator_t it; 56 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 57 while (btstack_linked_list_iterator_has_next(&it)){ 58 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 59 if (connection->avrcp_browsing_cid != browsing_cid) continue; 60 return connection; 61 } 62 return NULL; 63 } 64 65 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){ 66 btstack_linked_list_iterator_t it; 67 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 68 while (btstack_linked_list_iterator_has_next(&it)){ 69 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 70 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue; 71 return connection; 72 } 73 return NULL; 74 } 75 76 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){ 77 btstack_linked_list_iterator_t it; 78 btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections); 79 while (btstack_linked_list_iterator_has_next(&it)){ 80 avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it); 81 if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue; 82 return connection->browsing_connection; 83 } 84 return NULL; 85 } 86 87 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){ 88 if (!callback) return; 89 uint8_t event[12]; 90 int pos = 0; 91 event[pos++] = HCI_EVENT_AVRCP_META; 92 event[pos++] = sizeof(event) - 2; 93 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED; 94 event[pos++] = status; 95 reverse_bd_addr(addr,&event[pos]); 96 pos += 6; 97 little_endian_store_16(event, pos, browsing_cid); 98 pos += 2; 99 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 100 } 101 102 static void avrcp_emit_incoming_browsing_connection(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr){ 103 if (!callback) return; 104 uint8_t event[11]; 105 int pos = 0; 106 event[pos++] = HCI_EVENT_AVRCP_META; 107 event[pos++] = sizeof(event) - 2; 108 event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION; 109 reverse_bd_addr(addr,&event[pos]); 110 pos += 6; 111 little_endian_store_16(event, pos, browsing_cid); 112 pos += 2; 113 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 114 } 115 116 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){ 117 if (!callback) return; 118 uint8_t event[5]; 119 int pos = 0; 120 event[pos++] = HCI_EVENT_AVRCP_META; 121 event[pos++] = sizeof(event) - 2; 122 event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED; 123 little_endian_store_16(event, pos, browsing_cid); 124 pos += 2; 125 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 126 } 127 128 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){ 129 avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get(); 130 memset(connection, 0, sizeof(avrcp_browsing_connection_t)); 131 connection->state = AVCTP_CONNECTION_IDLE; 132 connection->transaction_label = 0xFF; 133 avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid(); 134 avrcp_connection->browsing_connection = connection; 135 return connection; 136 } 137 138 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){ 139 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context); 140 141 if (!avrcp_connection){ 142 log_error("avrcp: there is no previously established AVRCP controller connection."); 143 return ERROR_CODE_COMMAND_DISALLOWED; 144 } 145 146 avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection; 147 if (connection){ 148 log_error(" avrcp_browsing_connect connection exists."); 149 return ERROR_CODE_SUCCESS; 150 } 151 152 connection = avrcp_browsing_create_connection(avrcp_connection); 153 if (!connection){ 154 log_error("avrcp: could not allocate connection struct."); 155 return BTSTACK_MEMORY_ALLOC_FAILED; 156 } 157 158 if (!browsing_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST; 159 160 *browsing_cid = avrcp_connection->avrcp_browsing_cid; 161 connection->ertm_buffer = ertm_buffer; 162 connection->ertm_buffer_size = size; 163 avrcp_connection->browsing_connection = connection; 164 165 memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); 166 167 return l2cap_create_ertm_channel(avrcp_browsing_target_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm, 168 &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL); 169 170 } 171 172 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){ 173 UNUSED(channel); 174 UNUSED(size); 175 bd_addr_t event_addr; 176 uint16_t local_cid; 177 uint8_t status; 178 avrcp_browsing_connection_t * browsing_connection = NULL; 179 avrcp_connection_t * avrcp_connection = NULL; 180 181 if (packet_type != HCI_EVENT_PACKET) return; 182 183 switch (hci_event_packet_get_type(packet)) { 184 case HCI_EVENT_DISCONNECTION_COMPLETE: 185 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, 0); 186 break; 187 case L2CAP_EVENT_INCOMING_CONNECTION: 188 l2cap_event_incoming_connection_get_address(packet, event_addr); 189 local_cid = l2cap_event_incoming_connection_get_local_cid(packet); 190 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 191 if (!avrcp_connection) { 192 log_error("No previously created AVRCP controller connections"); 193 l2cap_decline_connection(local_cid); 194 break; 195 } 196 browsing_connection = avrcp_browsing_create_connection(avrcp_connection); 197 browsing_connection->l2cap_browsing_cid = local_cid; 198 browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION; 199 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); 200 avrcp_emit_incoming_browsing_connection(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr); 201 break; 202 203 case L2CAP_EVENT_CHANNEL_OPENED: 204 l2cap_event_channel_opened_get_address(packet, event_addr); 205 status = l2cap_event_channel_opened_get_status(packet); 206 local_cid = l2cap_event_channel_opened_get_local_cid(packet); 207 208 avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context); 209 if (!avrcp_connection){ 210 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr)); 211 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST); 212 l2cap_disconnect(local_cid, 0); // reason isn't used 213 break; 214 } 215 216 browsing_connection = avrcp_connection->browsing_connection; 217 if (status != ERROR_CODE_SUCCESS){ 218 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status); 219 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status); 220 btstack_memory_avrcp_browsing_connection_free(browsing_connection); 221 avrcp_connection->browsing_connection = NULL; 222 break; 223 } 224 if (browsing_connection->state != AVCTP_CONNECTION_W4_L2CAP_CONNECTED) break; 225 226 browsing_connection->l2cap_browsing_cid = local_cid; 227 228 log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid); 229 browsing_connection->state = AVCTP_CONNECTION_OPENED; 230 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS); 231 break; 232 233 case L2CAP_EVENT_CHANNEL_CLOSED: 234 // data: event (8), len(8), channel (16) 235 local_cid = l2cap_event_channel_closed_get_local_cid(packet); 236 avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context); 237 238 if (avrcp_connection && avrcp_connection->browsing_connection){ 239 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid); 240 // free connection 241 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); 242 avrcp_connection->browsing_connection = NULL; 243 break; 244 } 245 break; 246 default: 247 break; 248 } 249 } 250 251 252 253 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 254 avrcp_browsing_connection_t * browsing_connection; 255 256 switch (packet_type) { 257 case L2CAP_DATA_PACKET:{ 258 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context); 259 if (!browsing_connection) break; 260 break; 261 } 262 case HCI_EVENT_PACKET: 263 switch (hci_event_packet_get_type(packet)){ 264 case L2CAP_EVENT_CAN_SEND_NOW: 265 browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context); 266 if (!browsing_connection) break; 267 // avrcp_browsing_target_handle_can_send_now(browsing_connection); 268 break; 269 default: 270 avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_target_context); 271 break; 272 } 273 break; 274 275 default: 276 break; 277 } 278 } 279 280 void avrcp_browsing_target_init(void){ 281 avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler; 282 l2cap_register_service(&avrcp_browsing_target_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_0); 283 } 284 285 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){ 286 if (callback == NULL){ 287 log_error("avrcp_browsing_target_register_packet_handler called with NULL callback"); 288 return; 289 } 290 avrcp_target_context.browsing_avrcp_callback = callback; 291 } 292 293 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){ 294 return avrcp_browsing_connect(bd_addr, &avrcp_target_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid); 295 } 296 297 uint8_t avrcp_browsing_target_disconnect(uint16_t avrcp_browsing_cid){ 298 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 299 if (!avrcp_connection){ 300 log_error("avrcp_browsing_target_disconnect: could not find a connection."); 301 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 302 } 303 if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 304 305 l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0); 306 return ERROR_CODE_SUCCESS; 307 } 308 309 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){ 310 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 311 if (!avrcp_connection){ 312 log_error("avrcp_browsing_decline_incoming_connection: could not find a connection."); 313 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 314 } 315 if (!avrcp_connection->browsing_connection){ 316 log_error("avrcp_browsing_decline_incoming_connection: no browsing connection."); 317 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 318 } 319 320 if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){ 321 log_error("avrcp_browsing_decline_incoming_connection: browsing connection in a wrong state."); 322 return ERROR_CODE_COMMAND_DISALLOWED; 323 } 324 325 avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED; 326 avrcp_connection->browsing_connection->ertm_buffer = ertm_buffer; 327 avrcp_connection->browsing_connection->ertm_buffer_size = size; 328 memcpy(&avrcp_connection->browsing_connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t)); 329 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); 330 return ERROR_CODE_SUCCESS; 331 } 332 333 uint8_t avrcp_browsing_target_decline_incoming_connection(uint16_t avrcp_browsing_cid){ 334 avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context); 335 if (!avrcp_connection){ 336 log_error("avrcp_browsing_decline_incoming_connection: could not find a connection."); 337 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 338 } 339 if (!avrcp_connection->browsing_connection) return ERROR_CODE_SUCCESS; 340 if (avrcp_connection->browsing_connection->state > AVCTP_CONNECTION_W4_ERTM_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED; 341 342 l2cap_decline_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid); 343 // free connection 344 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection); 345 avrcp_connection->browsing_connection = NULL; 346 return ERROR_CODE_SUCCESS; 347 } 348