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