xref: /btstack/src/classic/avrcp.c (revision 9ea49f801ca9d49a65636d9ce376c8a09ffa18c0)
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.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 // snprintf
43 #include <stdio.h>
44 
45 #include "bluetooth_psm.h"
46 #include "bluetooth_sdp.h"
47 #include "btstack_debug.h"
48 #include "btstack_event.h"
49 #include "btstack_memory.h"
50 #include "classic/sdp_client.h"
51 #include "classic/sdp_util.h"
52 #include "classic/avrcp.h"
53 
54 
55 typedef struct {
56     uint8_t  parse_sdp_record;
57     uint32_t record_id;
58     uint16_t avrcp_cid;
59     uint16_t avrcp_l2cap_psm;
60     uint16_t avrcp_version;
61 
62     uint16_t browsing_l2cap_psm;
63     uint16_t browsing_version;
64 } avrcp_sdp_query_context_t;
65 
66 
67 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
68 
69 static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service";
70 static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider";
71 static const char * default_avrcp_target_service_name = "BTstack AVRCP Target Service";
72 static const char * default_avrcp_target_service_provider_name = "BTstack AVRCP Target Service Provider";
73 
74 static uint16_t  avrcp_cid_counter;
75 
76 static btstack_context_callback_registration_t avrcp_handle_sdp_client_query_request;
77 
78 static avrcp_sdp_query_context_t sdp_query_context;
79 
80 static btstack_packet_handler_t avrcp_callback;
81 
82 static uint8_t   attribute_value[45];
83 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
84 
85 static btstack_linked_list_t connections;
86 static btstack_packet_handler_t avrcp_controller_packet_handler;
87 static btstack_packet_handler_t avrcp_target_packet_handler;
88 static bool l2cap_service_registered = false;
89 
90 static const char * avrcp_subunit_type_name[] = {
91     "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER",
92     "CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE",
93     "VENDOR_UNIQUE", "RESERVED_FOR_ALL_SUBUNIT_TYPES",
94     "EXTENDED_TO_NEXT_BYTE", "UNIT", "ERROR"
95 };
96 
97 const char * avrcp_subunit2str(uint16_t index){
98     if (index <= 11) return avrcp_subunit_type_name[index];
99     if ((index >= 0x1C) && (index <= 0x1F)) return avrcp_subunit_type_name[index - 0x10];
100     return avrcp_subunit_type_name[16];
101 }
102 
103 static const char * avrcp_event_name[] = {
104     "ERROR", "PLAYBACK_STATUS_CHANGED",
105     "TRACK_CHANGED", "TRACK_REACHED_END", "TRACK_REACHED_START",
106     "PLAYBACK_POS_CHANGED", "BATT_STATUS_CHANGED", "SYSTEM_STATUS_CHANGED",
107     "PLAYER_APPLICATION_SETTING_CHANGED", "NOW_PLAYING_CONTENT_CHANGED",
108     "AVAILABLE_PLAYERS_CHANGED", "ADDRESSED_PLAYER_CHANGED", "UIDS_CHANGED", "VOLUME_CHANGED"
109 };
110 const char * avrcp_event2str(uint16_t index){
111     if (index <= 0x0d) return avrcp_event_name[index];
112     return avrcp_event_name[0];
113 }
114 
115 static const char * avrcp_operation_name[] = {
116     "SKIP", NULL, NULL, NULL, NULL,
117     "VOLUME_UP", "VOLUME_DOWN", "MUTE", "PLAY", "STOP", "PAUSE", NULL,
118     "REWIND", "FAST_FORWARD", NULL, "FORWARD", "BACKWARD" // 0x4C
119 };
120 
121 const char * avrcp_operation2str(uint8_t operation_id){
122     char * name = NULL;
123     if ((operation_id >= AVRCP_OPERATION_ID_SKIP) && (operation_id <= AVRCP_OPERATION_ID_BACKWARD)){
124         name = (char *)avrcp_operation_name[operation_id - AVRCP_OPERATION_ID_SKIP];
125     }
126     if (name == NULL){
127         static char buffer[13];
128         snprintf(buffer, sizeof(buffer), "Unknown 0x%02x", operation_id);
129         buffer[sizeof(buffer)-1] = 0;
130         return buffer;
131     } else {
132         return name;
133     }
134 }
135 
136 static const char * avrcp_media_attribute_id_name[] = {
137     "NONE", "TITLE", "ARTIST", "ALBUM", "TRACK", "TOTAL TRACKS", "GENRE", "SONG LENGTH"
138 };
139 const char * avrcp_attribute2str(uint8_t index){
140     if ((index >= 1) && (index <= 7)) return avrcp_media_attribute_id_name[index];
141     return avrcp_media_attribute_id_name[0];
142 }
143 
144 static const char * avrcp_play_status_name[] = {
145     "STOPPED", "PLAYING", "PAUSED", "FORWARD SEEK", "REVERSE SEEK",
146     "ERROR" // 0xFF
147 };
148 const char * avrcp_play_status2str(uint8_t index){
149     if ((index >= 1) && (index <= 4)) return avrcp_play_status_name[index];
150     return avrcp_play_status_name[5];
151 }
152 
153 static const char * avrcp_ctype_name[] = {
154     "CONTROL",
155     "STATUS",
156     "SPECIFIC_INQUIRY",
157     "NOTIFY",
158     "GENERAL_INQUIRY",
159     "RESERVED5",
160     "RESERVED6",
161     "RESERVED7",
162     "NOT IMPLEMENTED IN REMOTE",
163     "ACCEPTED BY REMOTE",
164     "REJECTED BY REMOTE",
165     "IN_TRANSITION",
166     "IMPLEMENTED_STABLE",
167     "CHANGED_STABLE",
168     "RESERVED",
169     "INTERIM"
170 };
171 const char * avrcp_ctype2str(uint8_t index){
172     if (index < sizeof(avrcp_ctype_name)){
173         return avrcp_ctype_name[index];
174     }
175     return "NONE";
176 }
177 
178 static const char * avrcp_shuffle_mode_name[] = {
179     "SHUFFLE OFF",
180     "SHUFFLE ALL TRACKS",
181     "SHUFFLE GROUP"
182 };
183 
184 const char * avrcp_shuffle2str(uint8_t index){
185     if ((index >= 1) && (index <= 3)) return avrcp_shuffle_mode_name[index-1];
186     return "NONE";
187 }
188 
189 static const char * avrcp_repeat_mode_name[] = {
190     "REPEAT OFF",
191     "REPEAT SINGLE TRACK",
192     "REPEAT ALL TRACKS",
193     "REPEAT GROUP"
194 };
195 
196 const char * avrcp_repeat2str(uint8_t index){
197     if ((index >= 1) && (index <= 4)) return avrcp_repeat_mode_name[index-1];
198     return "NONE";
199 }
200 
201 btstack_linked_list_t avrcp_get_connections(void){
202     return connections;
203 }
204 
205 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){
206     uint8_t cmd_opcode_index = 5;
207     if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED;
208     return packet[cmd_opcode_index];
209 }
210 
211 void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features,
212     const char * service_name, const char * service_provider_name){
213     uint8_t* attribute;
214     de_create_sequence(service);
215 
216     // 0x0000 "Service Record Handle"
217     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
218     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
219 
220     // 0x0001 "Service Class ID List"
221     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
222     attribute = de_push_sequence(service);
223     {
224         if (controller){
225             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
226             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER);
227         } else {
228             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET);
229         }
230     }
231     de_pop_sequence(service, attribute);
232 
233     // 0x0004 "Protocol Descriptor List"
234     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
235     attribute = de_push_sequence(service);
236     {
237         uint8_t* l2cpProtocol = de_push_sequence(attribute);
238         {
239             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
240             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVCTP);
241         }
242         de_pop_sequence(attribute, l2cpProtocol);
243 
244         uint8_t* avctpProtocol = de_push_sequence(attribute);
245         {
246             de_add_number(avctpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP);  // avctpProtocol_service
247             de_add_number(avctpProtocol,  DE_UINT, DE_SIZE_16,  0x0104);    // version
248         }
249         de_pop_sequence(attribute, avctpProtocol);
250     }
251     de_pop_sequence(service, attribute);
252 
253     // 0x0005 "Public Browse Group"
254     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
255     attribute = de_push_sequence(service);
256     {
257         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
258     }
259     de_pop_sequence(service, attribute);
260 
261     // 0x0009 "Bluetooth Profile Descriptor List"
262     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
263     attribute = de_push_sequence(service);
264     {
265         uint8_t *avrcProfile = de_push_sequence(attribute);
266         {
267             de_add_number(avrcProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
268             de_add_number(avrcProfile,  DE_UINT, DE_SIZE_16, 0x0106);
269         }
270         de_pop_sequence(attribute, avrcProfile);
271     }
272     de_pop_sequence(service, attribute);
273 
274     // 0x000d "Additional Bluetooth Profile Descriptor List"
275     if (browsing){
276         de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS);
277         attribute = de_push_sequence(service);
278         {
279             uint8_t * des = de_push_sequence(attribute);
280             {
281                 uint8_t* browsing_l2cpProtocol = de_push_sequence(des);
282                 {
283                     de_add_number(browsing_l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
284                     de_add_number(browsing_l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PSM_AVCTP_BROWSING);
285                 }
286                 de_pop_sequence(des, browsing_l2cpProtocol);
287 
288                 uint8_t* browsing_avctpProtocol = de_push_sequence(des);
289                 {
290                     de_add_number(browsing_avctpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP);  // browsing_avctpProtocol_service
291                     de_add_number(browsing_avctpProtocol,  DE_UINT, DE_SIZE_16, 0x0104);                   // version
292                 }
293                 de_pop_sequence(des, browsing_avctpProtocol);
294             }
295             de_pop_sequence(attribute, des);
296         }
297         de_pop_sequence(service, attribute);
298     }
299 
300 
301     // 0x0100 "Service Name"
302     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
303     if (service_name){
304         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
305     } else {
306         if (controller){
307             de_add_data(service,  DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name);
308         } else {
309             de_add_data(service,  DE_STRING, strlen(default_avrcp_target_service_name), (uint8_t *) default_avrcp_target_service_name);
310         }
311     }
312 
313     // 0x0100 "Provider Name"
314     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
315     if (service_provider_name){
316         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
317     } else {
318         if (controller){
319             de_add_data(service,  DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name);
320         } else {
321             de_add_data(service,  DE_STRING, strlen(default_avrcp_target_service_provider_name), (uint8_t *) default_avrcp_target_service_provider_name);
322         }
323     }
324 
325     // 0x0311 "Supported Features"
326     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
327     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
328 }
329 
330 avrcp_connection_t * avrcp_get_connection_for_bd_addr_for_role(avrcp_role_t role, bd_addr_t addr){
331     btstack_linked_list_iterator_t it;
332     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
333     while (btstack_linked_list_iterator_has_next(&it)){
334         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
335         if (connection->role != role) continue;
336         if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
337         return connection;
338     }
339     return NULL;
340 }
341 
342 avrcp_connection_t * avrcp_get_connection_for_l2cap_signaling_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){
343     btstack_linked_list_iterator_t it;
344     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
345     while (btstack_linked_list_iterator_has_next(&it)){
346         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
347         if (connection->role != role) continue;
348         if (connection->l2cap_signaling_cid != l2cap_cid) continue;
349         return connection;
350     }
351     return NULL;
352 }
353 
354 avrcp_connection_t * avrcp_get_connection_for_avrcp_cid_for_role(avrcp_role_t role, uint16_t avrcp_cid){
355     btstack_linked_list_iterator_t it;
356     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
357     while (btstack_linked_list_iterator_has_next(&it)){
358         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
359         if (connection->role != role) continue;
360         if (connection->avrcp_cid != avrcp_cid) continue;
361         return connection;
362     }
363     return NULL;
364 }
365 
366 avrcp_connection_t * avrcp_get_connection_for_browsing_cid_for_role(avrcp_role_t role, uint16_t browsing_cid){
367     btstack_linked_list_iterator_t it;
368     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
369     while (btstack_linked_list_iterator_has_next(&it)){
370         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
371         if (connection->role != role) continue;
372         if (connection->avrcp_browsing_cid != browsing_cid) continue;
373         return connection;
374     }
375     return NULL;
376 }
377 
378 avrcp_connection_t * avrcp_get_connection_for_browsing_l2cap_cid_for_role(avrcp_role_t role, uint16_t browsing_l2cap_cid){
379     btstack_linked_list_iterator_t it;
380     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
381     while (btstack_linked_list_iterator_has_next(&it)){
382         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
383         if (connection->role != role) continue;
384         if (connection->browsing_connection &&  (connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid)) continue;
385         return connection;
386     }
387     return NULL;
388 }
389 
390 avrcp_browsing_connection_t * avrcp_get_browsing_connection_for_l2cap_cid_for_role(avrcp_role_t role, uint16_t l2cap_cid){
391     btstack_linked_list_iterator_t it;
392     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &connections);
393     while (btstack_linked_list_iterator_has_next(&it)){
394         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
395         if (connection->role != role) continue;
396         if (connection->browsing_connection && (connection->browsing_connection->l2cap_browsing_cid != l2cap_cid)) continue;
397         return connection->browsing_connection;
398     }
399     return NULL;
400 }
401 
402 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){
403     connection->wait_to_send = true;
404     l2cap_request_can_send_now_event(l2cap_cid);
405 }
406 
407 uint16_t avrcp_get_next_cid(avrcp_role_t role){
408     do {
409         if (avrcp_cid_counter == 0xffff) {
410             avrcp_cid_counter = 1;
411         } else {
412             avrcp_cid_counter++;
413         }
414     } while (avrcp_get_connection_for_avrcp_cid_for_role(role, avrcp_cid_counter) !=  NULL) ;
415     return avrcp_cid_counter;
416 }
417 
418 
419 static avrcp_connection_t * avrcp_create_connection(avrcp_role_t role, bd_addr_t remote_addr){
420     avrcp_connection_t * connection = btstack_memory_avrcp_connection_get();
421     if (!connection){
422         log_error("Not enough memory to create connection for role %d", role);
423         return NULL;
424     }
425 
426     connection->state = AVCTP_CONNECTION_IDLE;
427     connection->role = role;
428 
429     connection->transaction_id = 0xFF;
430     connection->transaction_id_counter = 0;
431 
432     connection->max_num_fragments = 0xFF;
433     log_info("avrcp_create_connection, role %d", role);
434     (void)memcpy(connection->remote_addr, remote_addr, 6);
435     btstack_linked_list_add(&connections, (btstack_linked_item_t *) connection);
436     return connection;
437 }
438 
439 static void avrcp_finalize_connection(avrcp_connection_t * connection){
440     btstack_run_loop_remove_timer(&connection->retry_timer);
441     btstack_linked_list_remove(&connections, (btstack_linked_item_t*) connection);
442     btstack_memory_avrcp_connection_free(connection);
443 }
444 
445 static void avrcp_emit_connection_established(uint16_t avrcp_cid, bd_addr_t addr, hci_con_handle_t con_handle, uint8_t status){
446     btstack_assert(avrcp_callback != NULL);
447 
448     uint8_t event[14];
449     int pos = 0;
450     event[pos++] = HCI_EVENT_AVRCP_META;
451     event[pos++] = sizeof(event) - 2;
452     event[pos++] = AVRCP_SUBEVENT_CONNECTION_ESTABLISHED;
453     event[pos++] = status;
454     little_endian_store_16(event, pos, avrcp_cid);
455     pos += 2;
456     reverse_bd_addr(addr,&event[pos]);
457     pos += 6;
458     little_endian_store_16(event, pos, con_handle);
459     pos += 2;
460     (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
461 }
462 
463 static void avrcp_emit_connection_closed(uint16_t avrcp_cid){
464     btstack_assert(avrcp_callback != NULL);
465 
466     uint8_t event[5];
467     int pos = 0;
468     event[pos++] = HCI_EVENT_AVRCP_META;
469     event[pos++] = sizeof(event) - 2;
470     event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED;
471     little_endian_store_16(event, pos, avrcp_cid);
472     pos += 2;
473     (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
474 }
475 
476 uint16_t avrcp_sdp_query_browsing_l2cap_psm(void){
477     return sdp_query_context.browsing_l2cap_psm;
478 }
479 
480 void avrcp_handle_sdp_client_query_attribute_value(uint8_t *packet){
481     des_iterator_t des_list_it;
482     des_iterator_t prot_it;
483 
484     // Handle new SDP record
485     if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context.record_id) {
486         sdp_query_context.record_id = sdp_event_query_attribute_byte_get_record_id(packet);
487         sdp_query_context.parse_sdp_record = 0;
488         // log_info("SDP Record: Nr: %d", record_id);
489     }
490 
491     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
492         attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
493 
494         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
495             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
496                 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
497                     if (de_get_element_type(attribute_value) != DE_DES) break;
498                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
499                         uint8_t * element = des_iterator_get_element(&des_list_it);
500                         if (de_get_element_type(element) != DE_UUID) continue;
501                         uint32_t uuid = de_get_uuid32(element);
502                         switch (uuid){
503                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET:
504                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL:
505                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER:
506                                 sdp_query_context.parse_sdp_record = 1;
507                                 break;
508                             default:
509                                 break;
510                         }
511                     }
512                     break;
513 
514                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
515                     if (!sdp_query_context.parse_sdp_record) break;
516                     // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
517                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
518                         uint8_t       *des_element;
519                         uint8_t       *element;
520                         uint32_t       uuid;
521 
522                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
523 
524                         des_element = des_iterator_get_element(&des_list_it);
525                         des_iterator_init(&prot_it, des_element);
526                         element = des_iterator_get_element(&prot_it);
527 
528                         if (de_get_element_type(element) != DE_UUID) continue;
529 
530                         uuid = de_get_uuid32(element);
531                         des_iterator_next(&prot_it);
532                         switch (uuid){
533                             case BLUETOOTH_PROTOCOL_L2CAP:
534                                 if (!des_iterator_has_more(&prot_it)) continue;
535                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_l2cap_psm);
536                                 break;
537                             case BLUETOOTH_PROTOCOL_AVCTP:
538                                 if (!des_iterator_has_more(&prot_it)) continue;
539                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_version);
540                                 break;
541                             default:
542                                 break;
543                         }
544                     }
545                 }
546                     break;
547                 case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: {
548                     // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
549                     if (!sdp_query_context.parse_sdp_record) break;
550                     if (de_get_element_type(attribute_value) != DE_DES) break;
551 
552                     des_iterator_t des_list_0_it;
553                     uint8_t       *element_0;
554 
555                     des_iterator_init(&des_list_0_it, attribute_value);
556                     element_0 = des_iterator_get_element(&des_list_0_it);
557 
558                     for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
559                         uint8_t       *des_element;
560                         uint8_t       *element;
561                         uint32_t       uuid;
562 
563                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
564 
565                         des_element = des_iterator_get_element(&des_list_it);
566                         des_iterator_init(&prot_it, des_element);
567                         element = des_iterator_get_element(&prot_it);
568 
569                         if (de_get_element_type(element) != DE_UUID) continue;
570 
571                         uuid = de_get_uuid32(element);
572                         des_iterator_next(&prot_it);
573                         switch (uuid){
574                             case BLUETOOTH_PROTOCOL_L2CAP:
575                                 if (!des_iterator_has_more(&prot_it)) continue;
576                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_l2cap_psm);
577                                 break;
578                             case BLUETOOTH_PROTOCOL_AVCTP:
579                                 if (!des_iterator_has_more(&prot_it)) continue;
580                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_version);
581                                 break;
582                             default:
583                                 break;
584                         }
585                     }
586                 }
587                     break;
588                 default:
589                     break;
590             }
591         }
592     } else {
593         log_error("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet));
594     }
595 }
596 
597 static void avrcp_handle_sdp_query_failed(avrcp_connection_t * connection, uint8_t status){
598     if (connection == NULL) return;
599     log_info("AVRCP: SDP query failed with status 0x%02x.", status);
600     avrcp_emit_connection_established(connection->avrcp_cid, connection->remote_addr, connection->con_handle, status);
601     avrcp_finalize_connection(connection);
602 }
603 
604 static void avrcp_handle_sdp_query_succeeded(avrcp_connection_t * connection){
605     if (connection == NULL) return;
606     connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
607     connection->avrcp_l2cap_psm = sdp_query_context.avrcp_l2cap_psm;
608     connection->browsing_version = sdp_query_context.browsing_version;
609     connection->browsing_l2cap_psm = sdp_query_context.browsing_l2cap_psm;
610 }
611 
612 static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
613     UNUSED(packet_type);
614     UNUSED(channel);
615     UNUSED(size);
616 
617     bool state_ok = true;
618     avrcp_connection_t * avrcp_target_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, sdp_query_context.avrcp_cid);
619     if (!avrcp_target_connection || avrcp_target_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) {
620         state_ok = false;
621     }
622     avrcp_connection_t * avrcp_controller_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, sdp_query_context.avrcp_cid);
623     if (!avrcp_controller_connection || avrcp_controller_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) {
624         state_ok = false;
625     }
626     if (!state_ok){
627         // something wrong, nevertheless, start next sdp query if this one is complete
628         if (hci_event_packet_get_type(packet) == SDP_EVENT_QUERY_COMPLETE){
629             (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
630         }
631         return;
632     }
633 
634     uint8_t status;
635 
636     switch (hci_event_packet_get_type(packet)){
637         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
638             avrcp_handle_sdp_client_query_attribute_value(packet);
639             return;
640 
641         case SDP_EVENT_QUERY_COMPLETE:
642             status = sdp_event_query_complete_get_status(packet);
643 
644             if (status != ERROR_CODE_SUCCESS){
645                 avrcp_handle_sdp_query_failed(avrcp_controller_connection, status);
646                 avrcp_handle_sdp_query_failed(avrcp_target_connection, status);
647                 break;
648             }
649 
650             if (!sdp_query_context.avrcp_l2cap_psm){
651                 avrcp_handle_sdp_query_failed(avrcp_controller_connection, SDP_SERVICE_NOT_FOUND);
652                 avrcp_handle_sdp_query_failed(avrcp_target_connection, SDP_SERVICE_NOT_FOUND);
653                 break;
654             }
655 
656             avrcp_handle_sdp_query_succeeded(avrcp_controller_connection);
657             avrcp_handle_sdp_query_succeeded(avrcp_target_connection);
658 
659             l2cap_create_channel(&avrcp_packet_handler, avrcp_target_connection->remote_addr, sdp_query_context.avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
660             break;
661 
662         default:
663             return;
664     }
665 
666     // register the SDP Query request to check if there is another connection waiting for the query
667     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
668     (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
669 }
670 
671 
672 static avrcp_connection_t * avrcp_handle_incoming_connection_for_role(avrcp_role_t role, avrcp_connection_t * connection, bd_addr_t event_addr, hci_con_handle_t con_handle, uint16_t local_cid, uint16_t avrcp_cid){
673     if (connection == NULL){
674         connection = avrcp_create_connection(role, event_addr);
675     }
676     if (connection) {
677         connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
678         connection->l2cap_signaling_cid = local_cid;
679         connection->avrcp_cid = avrcp_cid;
680         connection->con_handle = con_handle;
681         btstack_run_loop_remove_timer(&connection->retry_timer);
682     }
683     return connection;
684 }
685 
686 static void avrcp_handle_open_connection(avrcp_connection_t * connection, hci_con_handle_t con_handle, uint16_t local_cid, uint16_t l2cap_mtu){
687     connection->l2cap_signaling_cid = local_cid;
688     connection->l2cap_mtu = l2cap_mtu;
689     connection->con_handle = con_handle;
690     connection->incoming_declined = false;
691     connection->song_length_ms = 0xFFFFFFFF;
692     connection->song_position_ms = 0xFFFFFFFF;
693     connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
694     connection->state = AVCTP_CONNECTION_OPENED;
695 
696     log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x, role %d, state %d", connection->avrcp_cid, connection->l2cap_signaling_cid, connection->role, connection->state);
697 }
698 
699 static void avrcp_retry_timer_timeout_handler(btstack_timer_source_t * timer){
700     uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
701     avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
702     if (connection_controller == NULL) return;
703     avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
704     if (connection_target == NULL) return;
705 
706     if (connection_controller->state == AVCTP_CONNECTION_W2_L2CAP_RETRY){
707         connection_controller->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
708         connection_target->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
709         l2cap_create_channel(&avrcp_packet_handler, connection_controller->remote_addr, connection_controller->avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
710     }
711 }
712 
713 static void avrcp_retry_timer_start(avrcp_connection_t * connection){
714     btstack_run_loop_set_timer_handler(&connection->retry_timer, avrcp_retry_timer_timeout_handler);
715     btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avrcp_cid);
716 
717     // add some jitter/randomness to reconnect delay
718     uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F);
719     btstack_run_loop_set_timer(&connection->retry_timer, timeout);
720 
721     btstack_run_loop_add_timer(&connection->retry_timer);
722 }
723 
724 static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){
725     return (avrcp_frame_type_t)((header & 0x02) >> 1);
726 }
727 
728 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
729     UNUSED(channel);
730     UNUSED(size);
731     bd_addr_t event_addr;
732     uint16_t local_cid;
733     uint16_t l2cap_mtu;
734     uint8_t  status;
735     bool decline_connection;
736     bool outoing_active;
737     hci_con_handle_t con_handle;
738 
739     avrcp_connection_t * connection_controller;
740     avrcp_connection_t * connection_target;
741     bool can_send;
742 
743     switch (packet_type) {
744         case HCI_EVENT_PACKET:
745             switch (hci_event_packet_get_type(packet)) {
746 
747                 case L2CAP_EVENT_INCOMING_CONNECTION:
748                     btstack_assert(avrcp_controller_packet_handler != NULL);
749                     btstack_assert(avrcp_target_packet_handler != NULL);
750 
751                     l2cap_event_incoming_connection_get_address(packet, event_addr);
752                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
753                     con_handle = l2cap_event_incoming_connection_get_handle(packet);
754 
755                     outoing_active = false;
756                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
757                     if (connection_target != NULL){
758                         if (connection_target->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){
759                             outoing_active = true;
760                             connection_target->incoming_declined = true;
761                         }
762                     }
763 
764                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
765                     if (connection_controller != NULL){
766                         if (connection_controller->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) {
767                             outoing_active = true;
768                             connection_controller->incoming_declined = true;
769                         }
770                     }
771 
772                     decline_connection = outoing_active;
773                     if (decline_connection == false){
774                         uint16_t avrcp_cid;
775                         if ((connection_controller == NULL) || (connection_target == NULL)){
776                             avrcp_cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
777                         } else {
778                             avrcp_cid = connection_controller->avrcp_cid;
779                         }
780                         // create two connection objects (both)
781                         connection_target     = avrcp_handle_incoming_connection_for_role(AVRCP_TARGET, connection_target, event_addr, con_handle, local_cid, avrcp_cid);
782                         connection_controller = avrcp_handle_incoming_connection_for_role(AVRCP_CONTROLLER, connection_controller, event_addr, con_handle, local_cid, avrcp_cid);
783                         if ((connection_target == NULL) || (connection_controller == NULL)){
784                             decline_connection = true;
785                             if (connection_target) {
786                                 avrcp_finalize_connection(connection_target);
787                             }
788                             if (connection_controller) {
789                                 avrcp_finalize_connection(connection_controller);
790                             }
791                         }
792                     }
793                     if (decline_connection){
794                         l2cap_decline_connection(local_cid);
795                     } else {
796                         log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION local cid 0x%02x, state %d", local_cid, connection_controller->state);
797                         l2cap_accept_connection(local_cid);
798                     }
799                     break;
800 
801                 case L2CAP_EVENT_CHANNEL_OPENED:
802                     l2cap_event_channel_opened_get_address(packet, event_addr);
803                     status = l2cap_event_channel_opened_get_status(packet);
804                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
805                     l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
806                     con_handle = l2cap_event_channel_opened_get_handle(packet);
807 
808                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
809                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
810 
811                     // incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION
812                     // outgoing: structs are cteated in avrcp_connect()
813                     if ((connection_controller == NULL) || (connection_target == NULL)) {
814                         break;
815                     }
816 
817                     switch (status){
818                         case ERROR_CODE_SUCCESS:
819                             avrcp_handle_open_connection(connection_target, con_handle, local_cid, l2cap_mtu);
820                             avrcp_handle_open_connection(connection_controller, con_handle, local_cid, l2cap_mtu);
821                             avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status);
822                             return;
823                         case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
824                             if (connection_controller->incoming_declined == true){
825                                 log_info("Incoming connection was declined, and the outgoing failed");
826                                 connection_controller->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
827                                 connection_controller->incoming_declined = false;
828                                 connection_target->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
829                                 connection_target->incoming_declined = false;
830                                 avrcp_retry_timer_start(connection_controller);
831                                 return;
832                             }
833                             break;
834                         default:
835                             break;
836                     }
837                     log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
838                     avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status);
839                     avrcp_finalize_connection(connection_controller);
840                     avrcp_finalize_connection(connection_target);
841 
842                     break;
843 
844                 case L2CAP_EVENT_CHANNEL_CLOSED:
845                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
846 
847                     connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid);
848                     connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid);
849                     if ((connection_controller == NULL) || (connection_target == NULL)) {
850                         break;
851                     }
852                     avrcp_emit_connection_closed(connection_controller->avrcp_cid);
853                     avrcp_finalize_connection(connection_controller);
854                     avrcp_finalize_connection(connection_target);
855                     break;
856 
857                 case L2CAP_EVENT_CAN_SEND_NOW:
858                     local_cid = l2cap_event_can_send_now_get_local_cid(packet);
859                     can_send = true;
860 
861                     connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid);
862                     if ((connection_target != NULL) && connection_target->wait_to_send){
863                         connection_target->wait_to_send = false;
864                         (*avrcp_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
865                         can_send = false;
866                     }
867 
868                     connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid);
869                     if ((connection_controller != NULL) && connection_controller->wait_to_send){
870                         if (can_send){
871                             connection_controller->wait_to_send = false;
872                             (*avrcp_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
873                         } else {
874                             l2cap_request_can_send_now_event(local_cid);
875                         }
876                     }
877                     break;
878 
879                 default:
880                     break;
881             }
882             break;
883 
884         case L2CAP_DATA_PACKET:
885             switch (avrcp_get_frame_type(packet[0])){
886                 case AVRCP_RESPONSE_FRAME:
887                     (*avrcp_controller_packet_handler)(packet_type, channel, packet, size);
888                     break;
889                 case AVRCP_COMMAND_FRAME:
890                 default:    // make compiler happy
891                     (*avrcp_target_packet_handler)(packet_type, channel, packet, size);
892                     break;
893             }
894             break;
895 
896         default:
897             break;
898     }
899 }
900 
901 uint8_t avrcp_disconnect(uint16_t avrcp_cid){
902     avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
903     if (!connection_controller){
904         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
905     }
906     avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
907     if (!connection_target){
908         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
909     }
910     if (connection_controller->browsing_connection){
911         l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0);
912     }
913     l2cap_disconnect(connection_controller->l2cap_signaling_cid, 0);
914     return ERROR_CODE_SUCCESS;
915 }
916 
917 static void avrcp_handle_start_sdp_client_query(void * context){
918     UNUSED(context);
919 
920     btstack_linked_list_iterator_t it;
921     btstack_linked_list_iterator_init(&it, &connections);
922     while (btstack_linked_list_iterator_has_next(&it)){
923         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
924 
925         if (connection->state != AVCTP_CONNECTION_W2_SEND_SDP_QUERY) continue;
926         connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
927 
928         // prevent triggering SDP query twice (for each role once)
929         avrcp_connection_t * connection_with_opposite_role;
930         switch (connection->role){
931             case AVRCP_CONTROLLER:
932                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, connection->avrcp_cid);
933                 break;
934             case AVRCP_TARGET:
935                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, connection->avrcp_cid);
936                 break;
937             default:
938                 btstack_assert(false);
939                 return;
940         }
941         connection_with_opposite_role->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
942 
943         sdp_query_context.avrcp_l2cap_psm = 0;
944         sdp_query_context.avrcp_version  = 0;
945         sdp_query_context.avrcp_cid = connection->avrcp_cid;
946         sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP);
947         return;
948     }
949 }
950 
951 uint8_t avrcp_connect(bd_addr_t remote_addr, uint16_t * avrcp_cid){
952     btstack_assert(avrcp_controller_packet_handler != NULL);
953     btstack_assert(avrcp_target_packet_handler != NULL);
954 
955     avrcp_connection_t * connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr);
956     if (connection_controller){
957         return ERROR_CODE_COMMAND_DISALLOWED;
958     }
959     avrcp_connection_t * connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr);
960     if (connection_target){
961         return ERROR_CODE_COMMAND_DISALLOWED;
962     }
963 
964     uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
965 
966     connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr);
967     if (!connection_controller) return BTSTACK_MEMORY_ALLOC_FAILED;
968 
969     connection_target = avrcp_create_connection(AVRCP_TARGET, remote_addr);
970     if (!connection_target){
971         avrcp_finalize_connection(connection_controller);
972         return BTSTACK_MEMORY_ALLOC_FAILED;
973     }
974 
975     if (avrcp_cid != NULL){
976         *avrcp_cid = cid;
977     }
978 
979     connection_controller->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
980     connection_controller->avrcp_cid = cid;
981 
982     connection_target->state     = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
983     connection_target->avrcp_cid = cid;
984 
985     avrcp_handle_sdp_client_query_request.callback = &avrcp_handle_start_sdp_client_query;
986     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
987     (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
988     return ERROR_CODE_SUCCESS;
989 }
990 
991 void avrcp_init(void){
992     connections = NULL;
993     if (l2cap_service_registered) return;
994 
995     int status = l2cap_register_service(&avrcp_packet_handler, BLUETOOTH_PSM_AVCTP, 0xffff, gap_get_security_level());
996     if (status != ERROR_CODE_SUCCESS) return;
997     l2cap_service_registered = true;
998 }
999 
1000 void avrcp_deinit(void){
1001     avrcp_cid_counter = 0;
1002     l2cap_service_registered = false;
1003 
1004     (void) memset(&sdp_query_context, 0, sizeof(avrcp_sdp_query_context_t));
1005     (void) memset(attribute_value,0, sizeof(attribute_value));
1006 
1007     avrcp_callback = NULL;
1008     connections = NULL;
1009     avrcp_controller_packet_handler = NULL;
1010     avrcp_target_packet_handler = NULL;
1011 }
1012 
1013 void avrcp_register_controller_packet_handler(btstack_packet_handler_t callback){
1014     avrcp_controller_packet_handler = callback;
1015 }
1016 
1017 void avrcp_register_target_packet_handler(btstack_packet_handler_t callback){
1018     avrcp_target_packet_handler = callback;
1019 }
1020 
1021 void avrcp_register_packet_handler(btstack_packet_handler_t callback){
1022     btstack_assert(callback != NULL);
1023     avrcp_callback = callback;
1024 }
1025 
1026 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1027 #define FUZZ_CID 0x44
1028 #define FUZZ_CON_HANDLE 0x0001
1029 static bd_addr_t remote_addr = { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 };
1030 void avrcp_init_fuzz(void){
1031     // setup avrcp connections for cid
1032     avrcp_connection_t * connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr);
1033     avrcp_connection_t * connection_target     = avrcp_create_connection(AVRCP_TARGET, remote_addr);
1034     avrcp_handle_open_connection(connection_controller, FUZZ_CON_HANDLE, FUZZ_CID, 999);
1035     avrcp_handle_open_connection(connection_target, FUZZ_CON_HANDLE, FUZZ_CID, 999);
1036 }
1037 void avrcp_packet_handler_fuzz(uint8_t *packet, uint16_t size){
1038     avrcp_packet_handler(L2CAP_DATA_PACKET, FUZZ_CID, packet, size);
1039 }
1040 #endif