xref: /btstack/src/classic/avrcp_browsing.c (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
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.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 
43 #include "bluetooth_psm.h"
44 #include "bluetooth_sdp.h"
45 #include "btstack_debug.h"
46 #include "btstack_event.h"
47 #include "btstack_memory.h"
48 #include "classic/sdp_client.h"
49 #include "classic/sdp_util.h"
50 #include "classic/avrcp_browsing.h"
51 
52 typedef struct {
53     uint16_t browsing_cid;
54     uint16_t browsing_l2cap_psm;
55     uint16_t browsing_version;
56 } avrcp_browsing_sdp_query_context_t;
57 
58 static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
59 
60 static btstack_packet_handler_t avrcp_browsing_callback;
61 static avrcp_browsing_sdp_query_context_t sdp_query_context;
62 
63 static bool l2cap_browsing_service_registered = false;
64 static btstack_packet_handler_t avrcp_browsing_controller_packet_handler;
65 static btstack_packet_handler_t avrcp_browsing_target_packet_handler;
66 static btstack_context_callback_registration_t avrcp_browsing_handle_sdp_client_query_request;
67 
68 static bd_addr_t avrcp_browsing_sdp_addr;
69 
70 void avrcp_browsing_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){
71     connection->wait_to_send = true;
72     l2cap_request_can_send_now_event(l2cap_cid);
73 }
74 
75 static void avrcp_retry_timer_timeout_handler(btstack_timer_source_t * timer){
76     uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
77     avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
78     if (connection_controller == NULL) return;
79     avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
80     if (connection_target == NULL) return;
81 
82     if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) return;
83 
84     if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W2_L2CAP_RETRY){
85         connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
86         connection_target->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
87 
88         l2cap_create_ertm_channel(avrcp_browsing_packet_handler, connection_controller->remote_addr, connection_controller->browsing_l2cap_psm,
89                 &connection_controller->browsing_connection->ertm_config,
90                 connection_controller->browsing_connection->ertm_buffer,
91                 connection_controller->browsing_connection->ertm_buffer_size, NULL);
92     }
93 }
94 
95 static void avrcp_retry_timer_start(avrcp_connection_t * connection){
96     btstack_run_loop_set_timer_handler(&connection->retry_timer, avrcp_retry_timer_timeout_handler);
97     btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avrcp_cid);
98 
99     // add some jitter/randomness to reconnect delay
100     uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F);
101     btstack_run_loop_set_timer(&connection->retry_timer, timeout);
102 
103     btstack_run_loop_add_timer(&connection->retry_timer);
104 }
105 
106 // AVRCP Browsing Service functions
107 static void avrcp_browsing_finalize_connection(avrcp_connection_t * connection){
108     btstack_run_loop_remove_timer(&connection->retry_timer);
109     btstack_memory_avrcp_browsing_connection_free(connection->browsing_connection);
110     connection->browsing_connection = NULL;
111 }
112 
113 static void avrcp_browsing_emit_connection_established(uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
114     btstack_assert(avrcp_browsing_callback != NULL);
115 
116     uint8_t event[12];
117     int pos = 0;
118     event[pos++] = HCI_EVENT_AVRCP_META;
119     event[pos++] = sizeof(event) - 2;
120     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
121     event[pos++] = status;
122     reverse_bd_addr(addr,&event[pos]);
123     pos += 6;
124     little_endian_store_16(event, pos, browsing_cid);
125     pos += 2;
126     (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
127 }
128 
129 static void avrcp_browsing_emit_incoming_connection(uint16_t browsing_cid, bd_addr_t addr){
130     btstack_assert(avrcp_browsing_callback != NULL);
131 
132     uint8_t event[11];
133     int pos = 0;
134     event[pos++] = HCI_EVENT_AVRCP_META;
135     event[pos++] = sizeof(event) - 2;
136     event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
137     reverse_bd_addr(addr,&event[pos]);
138     pos += 6;
139     little_endian_store_16(event, pos, browsing_cid);
140     pos += 2;
141     (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
142 }
143 
144 static void avrcp_browsing_emit_connection_closed(uint16_t browsing_cid){
145     btstack_assert(avrcp_browsing_callback != NULL);
146 
147     uint8_t event[5];
148     int pos = 0;
149     event[pos++] = HCI_EVENT_AVRCP_META;
150     event[pos++] = sizeof(event) - 2;
151     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED;
152     little_endian_store_16(event, pos, browsing_cid);
153     pos += 2;
154     (*avrcp_browsing_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
155 }
156 
157 
158 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection, uint16_t avrcp_browsing_cid){
159     avrcp_browsing_connection_t * browsing_connection = btstack_memory_avrcp_browsing_connection_get();
160     if (!browsing_connection){
161         log_error("Not enough memory to create browsing connection");
162         return NULL;
163     }
164     browsing_connection->state = AVCTP_CONNECTION_IDLE;
165     browsing_connection->transaction_label = 0xFF;
166 
167     avrcp_connection->avrcp_browsing_cid = avrcp_browsing_cid;
168     avrcp_connection->browsing_connection = browsing_connection;
169 
170     log_info("avrcp_browsing_create_connection, avrcp cid 0x%02x", avrcp_connection->avrcp_browsing_cid);
171     return browsing_connection;
172 }
173 
174 static void avrcp_browsing_configure_ertm(avrcp_browsing_connection_t * browsing_connection, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
175     browsing_connection->ertm_buffer = ertm_buffer;
176     browsing_connection->ertm_buffer_size = ertm_buffer_size;
177 
178     if (ertm_buffer_size > 0) {
179         (void)memcpy(&browsing_connection->ertm_config, ertm_config,
180                  sizeof(l2cap_ertm_config_t));
181         log_info("avrcp_browsing_configure_ertm");
182     }
183 }
184 
185 static avrcp_browsing_connection_t * avrcp_browsing_handle_incoming_connection(avrcp_connection_t * connection, uint16_t local_cid, uint16_t avrcp_browsing_cid){
186     if (connection->browsing_connection == NULL){
187         avrcp_browsing_create_connection(connection, avrcp_browsing_cid);
188     }
189     if (connection->browsing_connection) {
190         connection->browsing_connection->l2cap_browsing_cid = local_cid;
191         connection->browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
192         btstack_run_loop_remove_timer(&connection->retry_timer);
193     }
194     return connection->browsing_connection;
195 }
196 
197 static void avrcp_browsing_handle_open_connection_for_role(avrcp_connection_t * connection, uint16_t local_cid){
198     connection->browsing_connection->l2cap_browsing_cid = local_cid;
199     connection->browsing_connection->incoming_declined = false;
200     connection->browsing_connection->state = AVCTP_CONNECTION_OPENED;
201     log_info("L2CAP_EVENT_CHANNEL_OPENED browsing_avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role);
202 }
203 
204 static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){
205     return (avrcp_frame_type_t)((header & 0x02) >> 1);
206 }
207 
208 static void avrcp_browsing_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
209     UNUSED(channel);
210     UNUSED(size);
211     bd_addr_t event_addr;
212     uint16_t local_cid;
213     uint8_t  status;
214     bool decline_connection;
215     bool outoing_active;
216 
217     avrcp_connection_t * connection_controller;
218     avrcp_connection_t * connection_target;
219 
220     switch (packet_type){
221         case L2CAP_DATA_PACKET:
222             switch (avrcp_get_frame_type(packet[0])){
223                 case AVRCP_RESPONSE_FRAME:
224                     (*avrcp_browsing_controller_packet_handler)(packet_type, channel, packet, size);
225                     break;
226                 case AVRCP_COMMAND_FRAME:
227                 default:    // make compiler happy
228                     (*avrcp_browsing_target_packet_handler)(packet_type, channel, packet, size);
229                     break;
230             }
231             break;
232         case HCI_EVENT_PACKET:
233             btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
234             btstack_assert(avrcp_browsing_target_packet_handler != NULL);
235 
236             switch (hci_event_packet_get_type(packet)) {
237 
238                 case L2CAP_EVENT_INCOMING_CONNECTION:
239                     btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
240                     btstack_assert(avrcp_browsing_target_packet_handler != NULL);
241 
242                     l2cap_event_incoming_connection_get_address(packet, event_addr);
243                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
244                     outoing_active = false;
245 
246                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
247                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
248 
249                     if (connection_target == NULL || connection_controller == NULL) {
250                         l2cap_decline_connection(local_cid);
251                         return;
252                     }
253 
254                     if (connection_target->browsing_connection != NULL){
255                         if (connection_target->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){
256                             outoing_active = true;
257                             connection_target->browsing_connection->incoming_declined = true;
258                         }
259                     }
260 
261                     if (connection_controller->browsing_connection != NULL){
262                         if (connection_controller->browsing_connection->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) {
263                             outoing_active = true;
264                             connection_controller->browsing_connection->incoming_declined = true;
265                         }
266                     }
267 
268                     decline_connection = outoing_active;
269                     if (decline_connection == false){
270                         uint16_t avrcp_browsing_cid;
271                         if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)){
272                             avrcp_browsing_cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
273                         } else {
274                             avrcp_browsing_cid = connection_controller->avrcp_browsing_cid;
275                         }
276 
277                         // create two connection objects (both)
278                         connection_target->browsing_connection     = avrcp_browsing_handle_incoming_connection(connection_target, local_cid, avrcp_browsing_cid);
279                         connection_controller->browsing_connection = avrcp_browsing_handle_incoming_connection(connection_controller, local_cid, avrcp_browsing_cid);
280 
281                         if ((connection_target->browsing_connection  == NULL) || (connection_controller->browsing_connection == NULL)){
282                             decline_connection = true;
283                             if (connection_target->browsing_connection) {
284                                 avrcp_browsing_finalize_connection(connection_target);
285                             }
286                             if (connection_controller->browsing_connection) {
287                                 avrcp_browsing_finalize_connection(connection_controller);
288                             }
289                         }
290                     }
291                     if (decline_connection){
292                         l2cap_decline_connection(local_cid);
293                     } else {
294                         log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION browsing_avrcp_cid 0x%02x", connection_controller->avrcp_browsing_cid);
295                         avrcp_browsing_emit_incoming_connection(connection_controller->avrcp_browsing_cid, event_addr);
296                     }
297                     break;
298 
299                 case L2CAP_EVENT_CHANNEL_OPENED:
300                     l2cap_event_channel_opened_get_address(packet, event_addr);
301                     status = l2cap_event_channel_opened_get_status(packet);
302                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
303 
304                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
305                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
306 
307                     // incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION
308                     // outgoing: structs are cteated in avrcp_connect() and avrcp_browsing_connect()
309                     if ((connection_controller == NULL) || (connection_target == NULL)) {
310                         break;
311                     }
312                     if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
313                         break;
314                     }
315 
316                     switch (status){
317                         case ERROR_CODE_SUCCESS:
318                             avrcp_browsing_handle_open_connection_for_role(connection_target, local_cid);
319                             avrcp_browsing_handle_open_connection_for_role(connection_controller, local_cid);
320                             avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
321                             return;
322                         case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
323                             if (connection_controller->browsing_connection->incoming_declined == true){
324                                 log_info("Incoming browsing connection was declined, and the outgoing failed");
325                                 connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
326                                 connection_controller->browsing_connection->incoming_declined = false;
327                                 connection_target->browsing_connection->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
328                                 connection_target->browsing_connection->incoming_declined = false;
329                                 avrcp_retry_timer_start(connection_controller);
330                                 return;
331                             }
332                             break;
333                         default:
334                             break;
335                     }
336                     log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
337                     avrcp_browsing_emit_connection_established(connection_controller->avrcp_browsing_cid, event_addr, status);
338                     avrcp_browsing_finalize_connection(connection_controller);
339                     avrcp_browsing_finalize_connection(connection_target);
340                     break;
341 
342                 case L2CAP_EVENT_CHANNEL_CLOSED:
343                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
344 
345                     connection_controller = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
346                     connection_target = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
347                     if ((connection_controller == NULL) || (connection_target == NULL)) {
348                         break;
349                     }
350                     if ((connection_controller->browsing_connection == NULL) || (connection_target->browsing_connection == NULL)) {
351                         break;
352                     }
353                     avrcp_browsing_emit_connection_closed(connection_controller->avrcp_browsing_cid);
354                     avrcp_browsing_finalize_connection(connection_controller);
355                     avrcp_browsing_finalize_connection(connection_target);
356                     break;
357 
358                 case L2CAP_EVENT_CAN_SEND_NOW:
359                     local_cid = l2cap_event_can_send_now_get_local_cid(packet);
360                     connection_target = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_TARGET, local_cid);
361                     if ((connection_target != NULL) && (connection_target->browsing_connection != NULL) && connection_target->browsing_connection->wait_to_send) {
362                         connection_target->browsing_connection->wait_to_send = false;
363                         (*avrcp_browsing_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
364                         break;
365                     }
366                     connection_controller = avrcp_get_connection_for_browsing_l2cap_cid_for_role(AVRCP_CONTROLLER, local_cid);
367                     if ((connection_controller != NULL) && (connection_controller->browsing_connection != NULL) && connection_controller->browsing_connection->wait_to_send) {
368                         connection_controller->browsing_connection->wait_to_send = false;
369                         (*avrcp_browsing_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
370                         break;
371                     }
372                     break;
373 
374                 default:
375                     break;
376             }
377             break;
378         default:
379             break;
380     }
381 
382 }
383 
384 void avrcp_browsing_init(void){
385     if (l2cap_browsing_service_registered) return;
386     int status = l2cap_register_service(&avrcp_browsing_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2);
387 
388     if (status != ERROR_CODE_SUCCESS) return;
389     l2cap_browsing_service_registered = true;
390 }
391 
392 static void avrcp_browsing_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
393     UNUSED(packet_type);
394     UNUSED(channel);
395     UNUSED(size);
396 
397     avrcp_connection_t * avrcp_target_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, sdp_query_context.browsing_cid);
398     avrcp_connection_t * avrcp_controller_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, sdp_query_context.browsing_cid);
399 
400     if ((avrcp_target_connection == NULL) || (avrcp_target_connection->browsing_connection == NULL)) return;
401     if (avrcp_target_connection->browsing_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return;
402 
403     if ((avrcp_controller_connection == NULL) || (avrcp_controller_connection->browsing_connection == NULL)) return;
404     if (avrcp_controller_connection->browsing_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return;
405 
406 
407     uint8_t status;
408     uint16_t browsing_l2cap_psm;
409 
410     switch (hci_event_packet_get_type(packet)){
411         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
412             avrcp_handle_sdp_client_query_attribute_value(packet);
413             break;
414 
415         case SDP_EVENT_QUERY_COMPLETE:
416             status = sdp_event_query_complete_get_status(packet);
417 
418             if (status != ERROR_CODE_SUCCESS){
419                 avrcp_browsing_emit_connection_established(avrcp_target_connection->avrcp_browsing_cid, avrcp_browsing_sdp_addr, status);
420                 avrcp_browsing_finalize_connection(avrcp_target_connection);
421                 avrcp_browsing_finalize_connection(avrcp_controller_connection);
422                 break;
423             }
424 
425             browsing_l2cap_psm = avrcp_sdp_sdp_query_browsing_l2cap_psm();
426             if (!browsing_l2cap_psm){
427                 avrcp_browsing_emit_connection_established(avrcp_target_connection->avrcp_browsing_cid, avrcp_browsing_sdp_addr, SDP_SERVICE_NOT_FOUND);
428                 avrcp_browsing_finalize_connection(avrcp_target_connection);
429                 avrcp_browsing_finalize_connection(avrcp_controller_connection);
430                 break;
431             }
432 
433             l2cap_create_ertm_channel(avrcp_browsing_packet_handler, avrcp_browsing_sdp_addr, browsing_l2cap_psm,
434                                             &avrcp_controller_connection->browsing_connection->ertm_config,
435                                             avrcp_controller_connection->browsing_connection->ertm_buffer,
436                                             avrcp_controller_connection->browsing_connection->ertm_buffer_size, NULL);
437             break;
438 
439         default:
440             break;
441     }
442     // register the SDP Query request to check if there is another connection waiting for the query
443     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
444     (void) sdp_client_register_query_callback(&avrcp_browsing_handle_sdp_client_query_request);
445 }
446 
447 static void avrcp_browsing_handle_start_sdp_client_query(void * context){
448     UNUSED(context);
449     // TODO
450 
451     btstack_linked_list_t connections = avrcp_get_connections();
452     btstack_linked_list_iterator_t it;
453     btstack_linked_list_iterator_init(&it, &connections);
454     while (btstack_linked_list_iterator_has_next(&it)){
455         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
456         if (connection->browsing_connection == NULL) continue;
457         if (connection->browsing_connection->state != AVCTP_CONNECTION_W2_SEND_SDP_QUERY) continue;
458         connection->browsing_connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
459 
460         // prevent triggering SDP query twice (for each role once)
461         avrcp_connection_t * connection_with_opposite_role;
462         switch (connection->role){
463             case AVRCP_CONTROLLER:
464                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, connection->avrcp_cid);
465                 break;
466             case AVRCP_TARGET:
467                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, connection->avrcp_cid);
468                 break;
469             default:
470                 btstack_assert(false);
471                 return;
472         }
473         if (connection->browsing_connection != NULL){
474             connection_with_opposite_role->browsing_connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
475         }
476 
477         sdp_query_context.browsing_l2cap_psm = 0;
478         sdp_query_context.browsing_version = 0;
479         sdp_query_context.browsing_cid = connection->avrcp_browsing_cid;
480 
481         sdp_client_query_uuid16(&avrcp_browsing_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP);
482         return;
483     }
484 }
485 
486 
487 uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){
488     btstack_assert(avrcp_browsing_controller_packet_handler != NULL);
489     btstack_assert(avrcp_browsing_target_packet_handler != NULL);
490 
491     avrcp_connection_t * connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr);
492     if (!connection_controller){
493         return ERROR_CODE_COMMAND_DISALLOWED;
494     }
495     avrcp_connection_t * connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr);
496     if (!connection_target){
497         return ERROR_CODE_COMMAND_DISALLOWED;
498     }
499 
500     if (connection_controller->browsing_connection){
501         return ERROR_CODE_COMMAND_DISALLOWED;
502     }
503     if (connection_target->browsing_connection){
504         return ERROR_CODE_COMMAND_DISALLOWED;
505     }
506 
507     uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
508 
509     connection_controller->browsing_connection = avrcp_browsing_create_connection(connection_controller, cid);
510     if (!connection_controller->browsing_connection) return BTSTACK_MEMORY_ALLOC_FAILED;
511 
512     connection_target->browsing_connection = avrcp_browsing_create_connection(connection_target, cid);
513     if (!connection_target->browsing_connection){
514         avrcp_browsing_finalize_connection(connection_controller);
515         return BTSTACK_MEMORY_ALLOC_FAILED;
516     }
517     avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
518     avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
519 
520     if (avrcp_browsing_cid != NULL){
521         *avrcp_browsing_cid = cid;
522     }
523 
524     if (connection_controller->browsing_l2cap_psm == 0){
525         memcpy(avrcp_browsing_sdp_addr, remote_addr, 6);
526         connection_controller->browsing_connection->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
527         connection_target->browsing_connection->state     = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
528         avrcp_browsing_handle_sdp_client_query_request.callback = &avrcp_browsing_handle_start_sdp_client_query;
529 
530         // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
531         (void) sdp_client_register_query_callback(&avrcp_browsing_handle_sdp_client_query_request);
532         return ERROR_CODE_SUCCESS;
533     } else {
534         connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
535         connection_target->browsing_connection->state     = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
536 
537         return l2cap_create_ertm_channel(avrcp_browsing_packet_handler, remote_addr, connection_controller->browsing_l2cap_psm,
538                                          &connection_controller->browsing_connection->ertm_config,
539                                          connection_controller->browsing_connection->ertm_buffer,
540                                          connection_controller->browsing_connection->ertm_buffer_size, NULL);
541     }
542 }
543 
544 uint8_t avrcp_browsing_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t ertm_buffer_size, l2cap_ertm_config_t * ertm_config){
545     avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
546     if (!connection_controller){
547         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
548     }
549     avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
550     if (!connection_target){
551         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
552     }
553 
554     if (!connection_controller->browsing_connection){
555         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
556     }
557     if (!connection_target->browsing_connection){
558         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
559     }
560 
561     if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
562         return ERROR_CODE_COMMAND_DISALLOWED;
563     }
564 
565     avrcp_browsing_configure_ertm(connection_controller->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
566     avrcp_browsing_configure_ertm(connection_target->browsing_connection, ertm_buffer, ertm_buffer_size, ertm_config);
567 
568     connection_controller->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
569     connection_target->browsing_connection->state     = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
570 
571     l2cap_accept_ertm_connection(connection_controller->browsing_connection->l2cap_browsing_cid,
572         &connection_controller->browsing_connection->ertm_config,
573         connection_controller->browsing_connection->ertm_buffer,
574         connection_controller->browsing_connection->ertm_buffer_size);
575     return ERROR_CODE_SUCCESS;
576 }
577 
578 
579 uint8_t avrcp_browsing_decline_incoming_connection(uint16_t avrcp_browsing_cid){
580     avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
581     if (!connection_controller){
582         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
583     }
584     avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
585     if (!connection_target){
586         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
587     }
588 
589     if (!connection_controller->browsing_connection){
590         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
591     }
592     if (!connection_target->browsing_connection){
593         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
594     }
595 
596     if (connection_controller->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
597         return ERROR_CODE_COMMAND_DISALLOWED;
598     }
599 
600     l2cap_decline_connection(connection_controller->browsing_connection->l2cap_browsing_cid);
601 
602     avrcp_browsing_finalize_connection(connection_controller);
603     avrcp_browsing_finalize_connection(connection_target);
604     return ERROR_CODE_SUCCESS;
605 }
606 
607 uint8_t avrcp_browsing_disconnect(uint16_t avrcp_browsing_cid){
608     avrcp_connection_t * connection_controller = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_CONTROLLER, avrcp_browsing_cid);
609     if (!connection_controller){
610         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
611     }
612     avrcp_connection_t * connection_target = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
613     if (!connection_target){
614         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
615     }
616 
617     if (!connection_controller->browsing_connection){
618         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
619     }
620     if (!connection_target->browsing_connection){
621         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
622     }
623 
624     l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0);
625     return ERROR_CODE_SUCCESS;
626 }
627 
628 void avrcp_browsing_register_controller_packet_handler(btstack_packet_handler_t callback){
629     avrcp_browsing_controller_packet_handler = callback;
630 }
631 
632 void avrcp_browsing_register_target_packet_handler(btstack_packet_handler_t callback){
633     avrcp_browsing_target_packet_handler = callback;
634 }
635 
636 void avrcp_browsing_register_packet_handler(btstack_packet_handler_t callback){
637     btstack_assert(callback != NULL);
638     avrcp_browsing_callback = callback;
639 }
640