xref: /btstack/src/classic/avrcp_browsing_target.c (revision 6568eb54ed831806170f9543f5f26e00d711b3c4)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avrcp_browsing_target.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <inttypes.h>
45 #include "btstack.h"
46 #include "classic/avrcp.h"
47 #include "classic/avrcp_browsing_target.h"
48 
49 #define PSM_AVCTP_BROWSING              0x001b
50 
51 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context);
52 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
53 
54 static void avrcp_browsing_target_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){
55     connection->wait_to_send = 1;
56     l2cap_request_can_send_now_event(l2cap_cid);
57 }
58 
59 static int avrcp_browsing_target_handle_can_send_now(avrcp_browsing_connection_t * connection){
60 
61     int pos = 0;
62     l2cap_reserve_packet_buffer();
63     uint8_t * packet = l2cap_get_outgoing_buffer();
64 
65     // transport header
66     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
67 
68     // TODO: check for fragmentation
69     connection->packet_type = AVRCP_SINGLE_PACKET;
70     printf("ttransaction %d \n", connection->transaction_label);
71     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
72     // Profile IDentifier (PID)
73     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
74     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
75     // command_type
76     // packet[pos++] = connection->command_type;
77     // // subunit_type | subunit ID
78     // packet[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
79     // // opcode
80     // packet[pos++] = (uint8_t)connection->command_opcode;
81     // operands
82         // company id is 3 bytes long
83     // big_endian_store_24(packet, pos, BT_SIG_COMPANY_ID);
84     // pos += 3;
85     memcpy(packet+pos, connection->cmd_operands, connection->cmd_operands_length);
86     // printf_hexdump(packet+pos, connection->cmd_operands_length);
87 
88     pos += connection->cmd_operands_length;
89     connection->wait_to_send = 0;
90     printf(" send reject \n");
91     printf_hexdump(packet, pos);
92     return l2cap_send_prepared(connection->l2cap_browsing_cid, pos);
93 }
94 
95 
96 static uint8_t avrcp_browsing_target_response_reject(avrcp_browsing_connection_t * connection, avrcp_status_code_t status){
97     // AVRCP_CTYPE_RESPONSE_REJECTED
98     int pos = 0;
99     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT;
100     // connection->cmd_operands[pos++] = 0;
101     // param length
102     big_endian_store_16(connection->cmd_operands, pos, 1);
103     pos += 2;
104     connection->cmd_operands[pos++] = status;
105     connection->cmd_operands_length = 4;
106     connection->state = AVCTP_W2_SEND_RESPONSE;
107     avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid);
108     return ERROR_CODE_SUCCESS;
109 }
110 
111 static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){
112     btstack_linked_list_iterator_t it;
113     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
114     while (btstack_linked_list_iterator_has_next(&it)){
115         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
116         if (connection->avrcp_browsing_cid != browsing_cid) continue;
117         return connection;
118     }
119     return NULL;
120 }
121 
122 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){
123     btstack_linked_list_iterator_t it;
124     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
125     while (btstack_linked_list_iterator_has_next(&it)){
126         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
127         if (connection->browsing_connection &&  connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue;
128         return connection;
129     }
130     return NULL;
131 }
132 
133 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){
134     btstack_linked_list_iterator_t it;
135     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
136     while (btstack_linked_list_iterator_has_next(&it)){
137         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
138         if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue;
139         return connection->browsing_connection;
140     }
141     return NULL;
142 }
143 
144 static void avrcp_browsing_target_emit_get_folder_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){
145     if (!callback) return;
146     uint8_t event[10];
147     int pos = 0;
148     event[pos++] = HCI_EVENT_AVRCP_META;
149     event[pos++] = sizeof(event) - 2;
150     event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_FOLDER_ITEMS;
151     little_endian_store_16(event, pos, browsing_cid);
152     pos += 2;
153     event[pos++] = connection->scope;
154     big_endian_store_32(event, pos, connection->attr_bitmap);
155     pos += 4;
156     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
157 }
158 
159 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
160     if (!callback) return;
161     uint8_t event[12];
162     int pos = 0;
163     event[pos++] = HCI_EVENT_AVRCP_META;
164     event[pos++] = sizeof(event) - 2;
165     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
166     event[pos++] = status;
167     reverse_bd_addr(addr,&event[pos]);
168     pos += 6;
169     little_endian_store_16(event, pos, browsing_cid);
170     pos += 2;
171     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
172 }
173 
174 static void avrcp_emit_incoming_browsing_connection(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr){
175     if (!callback) return;
176     uint8_t event[11];
177     int pos = 0;
178     event[pos++] = HCI_EVENT_AVRCP_META;
179     event[pos++] = sizeof(event) - 2;
180     event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
181     reverse_bd_addr(addr,&event[pos]);
182     pos += 6;
183     little_endian_store_16(event, pos, browsing_cid);
184     pos += 2;
185     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
186 }
187 
188 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){
189     if (!callback) return;
190     uint8_t event[5];
191     int pos = 0;
192     event[pos++] = HCI_EVENT_AVRCP_META;
193     event[pos++] = sizeof(event) - 2;
194     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED;
195     little_endian_store_16(event, pos, browsing_cid);
196     pos += 2;
197     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
198 }
199 
200 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){
201     avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get();
202     memset(connection, 0, sizeof(avrcp_browsing_connection_t));
203     connection->state = AVCTP_CONNECTION_IDLE;
204     connection->transaction_label = 0xFF;
205     avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid();
206     avrcp_connection->browsing_connection = connection;
207     return connection;
208 }
209 
210 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){
211     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context);
212 
213     if (!avrcp_connection){
214         log_error("avrcp: there is no previously established AVRCP controller connection.");
215         return ERROR_CODE_COMMAND_DISALLOWED;
216     }
217 
218     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
219     if (connection){
220         log_error(" avrcp_browsing_connect connection exists.");
221         return ERROR_CODE_SUCCESS;
222     }
223 
224     connection = avrcp_browsing_create_connection(avrcp_connection);
225     if (!connection){
226         log_error("avrcp: could not allocate connection struct.");
227         return BTSTACK_MEMORY_ALLOC_FAILED;
228     }
229 
230     if (!browsing_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
231 
232     *browsing_cid = avrcp_connection->avrcp_browsing_cid;
233     connection->ertm_buffer = ertm_buffer;
234     connection->ertm_buffer_size = size;
235     avrcp_connection->browsing_connection = connection;
236 
237     memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t));
238 
239     return l2cap_create_ertm_channel(avrcp_browsing_target_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm,
240                     &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL);
241 
242 }
243 
244 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){
245     UNUSED(channel);
246     UNUSED(size);
247     bd_addr_t event_addr;
248     uint16_t local_cid;
249     uint8_t  status;
250     avrcp_browsing_connection_t * browsing_connection = NULL;
251     avrcp_connection_t * avrcp_connection = NULL;
252 
253     if (packet_type != HCI_EVENT_PACKET) return;
254 
255     switch (hci_event_packet_get_type(packet)) {
256         case HCI_EVENT_DISCONNECTION_COMPLETE:
257             avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, 0);
258             break;
259         case L2CAP_EVENT_INCOMING_CONNECTION:
260             l2cap_event_incoming_connection_get_address(packet, event_addr);
261             local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
262             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
263             if (!avrcp_connection) {
264                 log_error("No previously created AVRCP controller connections");
265                 l2cap_decline_connection(local_cid);
266                 break;
267             }
268             browsing_connection = avrcp_browsing_create_connection(avrcp_connection);
269             browsing_connection->l2cap_browsing_cid = local_cid;
270             browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
271             log_info("Emit AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x\n", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid);
272             avrcp_emit_incoming_browsing_connection(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr);
273             break;
274 
275         case L2CAP_EVENT_CHANNEL_OPENED:
276             l2cap_event_channel_opened_get_address(packet, event_addr);
277             status = l2cap_event_channel_opened_get_status(packet);
278             local_cid = l2cap_event_channel_opened_get_local_cid(packet);
279 
280             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
281             if (!avrcp_connection){
282                 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr));
283                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST);
284                 l2cap_disconnect(local_cid, 0); // reason isn't used
285                 break;
286             }
287 
288             browsing_connection = avrcp_connection->browsing_connection;
289             if (status != ERROR_CODE_SUCCESS){
290                 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
291                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status);
292                 btstack_memory_avrcp_browsing_connection_free(browsing_connection);
293                 avrcp_connection->browsing_connection = NULL;
294                 break;
295             }
296             if (browsing_connection->state != AVCTP_CONNECTION_W4_L2CAP_CONNECTED) break;
297 
298             browsing_connection->l2cap_browsing_cid = local_cid;
299 
300             log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid);
301             browsing_connection->state = AVCTP_CONNECTION_OPENED;
302             avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS);
303             break;
304 
305         case L2CAP_EVENT_CHANNEL_CLOSED:
306             // data: event (8), len(8), channel (16)
307             local_cid = l2cap_event_channel_closed_get_local_cid(packet);
308             avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context);
309 
310             if (avrcp_connection && avrcp_connection->browsing_connection){
311                 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid);
312                 // free connection
313                 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
314                 avrcp_connection->browsing_connection = NULL;
315                 break;
316             }
317             break;
318         default:
319             break;
320     }
321 }
322 
323 
324 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
325     avrcp_browsing_connection_t * browsing_connection;
326 
327     switch (packet_type) {
328         case L2CAP_DATA_PACKET:{
329             browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context);
330             if (!browsing_connection) break;
331             printf_hexdump(packet,size);
332 
333             int pos = 0;
334             uint8_t transport_header = packet[pos++];
335             // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
336             browsing_connection->transaction_label = transport_header >> 4;
337             avrcp_packet_type_t avctp_packet_type = (transport_header & 0x0F) >> 2;
338             printf("L2CAP_DATA_PACKET, transaction_label %d\n", browsing_connection->transaction_label);
339             switch (avctp_packet_type){
340                 case AVRCP_SINGLE_PACKET:
341                 case AVRCP_START_PACKET:
342                     // uint8_t frame_type = (transport_header & 0x03) >> 1;
343                     // uint8_t ipid = transport_header & 0x01;
344                     browsing_connection->subunit_type = packet[pos++] >> 2;
345                     browsing_connection->subunit_id = 0;
346                     browsing_connection->command_opcode = packet[pos++];
347                     // printf("subunit_id")
348                     // pos += 2;
349                     browsing_connection->num_packets = 1;
350                     if (avctp_packet_type == AVRCP_START_PACKET){
351                         browsing_connection->num_packets = packet[pos++];
352                     }
353                     browsing_connection->pdu_id = packet[pos++];
354                     // uint16_t length = big_endian_read_16(packet, pos);
355                     // pos += 2;
356                     break;
357                 default:
358                     break;
359             }
360             printf("pdu id 0x%2x\n", browsing_connection->pdu_id);
361             // uint32_t i;
362             switch(browsing_connection->pdu_id){
363                 case AVRCP_PDU_ID_GET_FOLDER_ITEMS:
364                     printf("\n");
365                     browsing_connection->scope = packet[pos++];
366                     browsing_connection->start_item = big_endian_read_32(packet, pos);
367                     pos += 4;
368                     browsing_connection->end_item = big_endian_read_32(packet, pos);
369                     pos += 4;
370                     uint8_t attr_count = packet[pos++];
371 
372                     while (attr_count){
373                         uint32_t attr_id = big_endian_read_32(packet, pos);
374                         pos += 4;
375                         browsing_connection->attr_bitmap |= (1 << attr_id);
376                         attr_count--;
377                     }
378                     avrcp_browsing_target_emit_get_folder_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection);
379 
380                     break;
381                 default:
382                     printf(" not parsed pdu ID 0x%02x\n", browsing_connection->pdu_id);
383                     break;
384             }
385 
386             switch (avctp_packet_type){
387                 case AVRCP_SINGLE_PACKET:
388                 case AVRCP_END_PACKET:
389                     printf("send avrcp_browsing_target_response_reject\n");
390                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
391                     avrcp_browsing_target_response_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND);
392                     // avrcp_browsing_target_emit_done_with_uid_counter(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection->uid_counter, browsing_connection->browsing_status, ERROR_CODE_SUCCESS);
393                     break;
394                 default:
395                     break;
396             }
397             // printf(" paket done\n");
398             break;
399         }
400 
401         case HCI_EVENT_PACKET:
402             switch (hci_event_packet_get_type(packet)){
403                 case L2CAP_EVENT_CAN_SEND_NOW:
404                     browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_target_context);
405                     if (!browsing_connection) break;
406                     if (browsing_connection->state != AVCTP_W2_SEND_RESPONSE) return;
407                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
408                     avrcp_browsing_target_handle_can_send_now(browsing_connection);
409                     break;
410                 default:
411                     avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
412                     break;
413             }
414             break;
415 
416         default:
417             break;
418     }
419 }
420 
421 void avrcp_browsing_target_init(void){
422     avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler;
423     l2cap_register_service(&avrcp_browsing_target_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_0);
424 }
425 
426 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){
427     if (callback == NULL){
428         log_error("avrcp_browsing_target_register_packet_handler called with NULL callback");
429         return;
430     }
431     avrcp_target_context.browsing_avrcp_callback = callback;
432 }
433 
434 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){
435     return avrcp_browsing_connect(bd_addr, &avrcp_target_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid);
436 }
437 
438 uint8_t avrcp_browsing_target_disconnect(uint16_t avrcp_browsing_cid){
439     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context);
440     if (!avrcp_connection){
441         log_error("avrcp_browsing_target_disconnect: could not find a connection.");
442         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
443     }
444     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
445 
446     l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0);
447     return ERROR_CODE_SUCCESS;
448 }
449 
450 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){
451     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context);
452     if (!avrcp_connection){
453         log_error("avrcp_browsing_decline_incoming_connection: could not find a connection.");
454         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
455     }
456     if (!avrcp_connection->browsing_connection){
457         log_error("avrcp_browsing_decline_incoming_connection: no browsing connection.");
458         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
459     }
460 
461     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
462         log_error("avrcp_browsing_decline_incoming_connection: browsing connection in a wrong state.");
463         return ERROR_CODE_COMMAND_DISALLOWED;
464     }
465 
466     avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
467     avrcp_connection->browsing_connection->ertm_buffer = ertm_buffer;
468     avrcp_connection->browsing_connection->ertm_buffer_size = size;
469     memcpy(&avrcp_connection->browsing_connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t));
470     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);
471     return ERROR_CODE_SUCCESS;
472 }
473 
474 uint8_t avrcp_browsing_target_decline_incoming_connection(uint16_t avrcp_browsing_cid){
475     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_target_context);
476     if (!avrcp_connection){
477         log_error("avrcp_browsing_decline_incoming_connection: could not find a connection.");
478         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
479     }
480     if (!avrcp_connection->browsing_connection) return ERROR_CODE_SUCCESS;
481     if (avrcp_connection->browsing_connection->state > AVCTP_CONNECTION_W4_ERTM_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED;
482 
483     l2cap_decline_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid);
484     // free connection
485     btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
486     avrcp_connection->browsing_connection = NULL;
487     return ERROR_CODE_SUCCESS;
488 }
489