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