xref: /btstack/src/classic/avrcp.c (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "avrcp.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,  0x0104);    // 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, 0x0106);
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, 0x0104);                   // 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", role);
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, hci_con_handle_t con_handle, uint8_t status){
431     btstack_assert(avrcp_callback != NULL);
432 
433     uint8_t event[14];
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     little_endian_store_16(event, pos, avrcp_cid);
440     pos += 2;
441     reverse_bd_addr(addr,&event[pos]);
442     pos += 6;
443     little_endian_store_16(event, pos, con_handle);
444     pos += 2;
445     (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
446 }
447 
448 static void avrcp_emit_connection_closed(uint16_t avrcp_cid){
449     btstack_assert(avrcp_callback != NULL);
450 
451     uint8_t event[5];
452     int pos = 0;
453     event[pos++] = HCI_EVENT_AVRCP_META;
454     event[pos++] = sizeof(event) - 2;
455     event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED;
456     little_endian_store_16(event, pos, avrcp_cid);
457     pos += 2;
458     (*avrcp_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
459 }
460 
461 uint16_t avrcp_sdp_sdp_query_browsing_l2cap_psm(void){
462     return sdp_query_context.browsing_l2cap_psm;
463 }
464 
465 void avrcp_handle_sdp_client_query_attribute_value(uint8_t *packet){
466     des_iterator_t des_list_it;
467     des_iterator_t prot_it;
468 
469     // Handle new SDP record
470     if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context.record_id) {
471         sdp_query_context.record_id = sdp_event_query_attribute_byte_get_record_id(packet);
472         sdp_query_context.parse_sdp_record = 0;
473         // log_info("SDP Record: Nr: %d", record_id);
474     }
475 
476     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
477         attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
478 
479         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
480             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
481                 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
482                     if (de_get_element_type(attribute_value) != DE_DES) break;
483                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
484                         uint8_t * element = des_iterator_get_element(&des_list_it);
485                         if (de_get_element_type(element) != DE_UUID) continue;
486                         uint32_t uuid = de_get_uuid32(element);
487                         switch (uuid){
488                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET:
489                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL:
490                             case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER:
491                                 sdp_query_context.parse_sdp_record = 1;
492                                 break;
493                             default:
494                                 break;
495                         }
496                     }
497                     break;
498 
499                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
500                     if (!sdp_query_context.parse_sdp_record) break;
501                     // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
502                     for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
503                         uint8_t       *des_element;
504                         uint8_t       *element;
505                         uint32_t       uuid;
506 
507                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
508 
509                         des_element = des_iterator_get_element(&des_list_it);
510                         des_iterator_init(&prot_it, des_element);
511                         element = des_iterator_get_element(&prot_it);
512 
513                         if (de_get_element_type(element) != DE_UUID) continue;
514 
515                         uuid = de_get_uuid32(element);
516                         des_iterator_next(&prot_it);
517                         switch (uuid){
518                             case BLUETOOTH_PROTOCOL_L2CAP:
519                                 if (!des_iterator_has_more(&prot_it)) continue;
520                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_l2cap_psm);
521                                 break;
522                             case BLUETOOTH_PROTOCOL_AVCTP:
523                                 if (!des_iterator_has_more(&prot_it)) continue;
524                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.avrcp_version);
525                                 break;
526                             default:
527                                 break;
528                         }
529                     }
530                 }
531                     break;
532                 case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: {
533                     // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
534                     if (!sdp_query_context.parse_sdp_record) break;
535                     if (de_get_element_type(attribute_value) != DE_DES) break;
536 
537                     des_iterator_t des_list_0_it;
538                     uint8_t       *element_0;
539 
540                     des_iterator_init(&des_list_0_it, attribute_value);
541                     element_0 = des_iterator_get_element(&des_list_0_it);
542 
543                     for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
544                         uint8_t       *des_element;
545                         uint8_t       *element;
546                         uint32_t       uuid;
547 
548                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
549 
550                         des_element = des_iterator_get_element(&des_list_it);
551                         des_iterator_init(&prot_it, des_element);
552                         element = des_iterator_get_element(&prot_it);
553 
554                         if (de_get_element_type(element) != DE_UUID) continue;
555 
556                         uuid = de_get_uuid32(element);
557                         des_iterator_next(&prot_it);
558                         switch (uuid){
559                             case BLUETOOTH_PROTOCOL_L2CAP:
560                                 if (!des_iterator_has_more(&prot_it)) continue;
561                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_l2cap_psm);
562                                 break;
563                             case BLUETOOTH_PROTOCOL_AVCTP:
564                                 if (!des_iterator_has_more(&prot_it)) continue;
565                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context.browsing_version);
566                                 break;
567                             default:
568                                 break;
569                         }
570                     }
571                 }
572                     break;
573                 default:
574                     break;
575             }
576         }
577     } else {
578         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));
579     }
580 }
581 
582 static void avrcp_handle_sdp_query_failed(avrcp_connection_t * connection, uint8_t status){
583     if (connection == NULL) return;
584     log_info("AVRCP: SDP query failed with status 0x%02x.", status);
585     avrcp_emit_connection_established(connection->avrcp_cid, connection->remote_addr, connection->con_handle, status);
586     avrcp_finalize_connection(connection);
587 }
588 
589 static void avrcp_handle_sdp_query_succeeded(avrcp_connection_t * connection){
590     if (connection == NULL) return;
591     connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
592     connection->avrcp_l2cap_psm = sdp_query_context.avrcp_l2cap_psm;
593     connection->browsing_version = sdp_query_context.browsing_version;
594     connection->browsing_l2cap_psm = sdp_query_context.browsing_l2cap_psm;
595 }
596 
597 static void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
598     UNUSED(packet_type);
599     UNUSED(channel);
600     UNUSED(size);
601 
602     bool state_ok = true;
603     avrcp_connection_t * avrcp_target_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, sdp_query_context.avrcp_cid);
604     if (!avrcp_target_connection || avrcp_target_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) {
605         state_ok = false;
606     }
607     avrcp_connection_t * avrcp_controller_connection = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, sdp_query_context.avrcp_cid);
608     if (!avrcp_controller_connection || avrcp_controller_connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) {
609         state_ok = false;
610     }
611     if (!state_ok){
612         // something wrong, nevertheless, start next sdp query if this one is complete
613         if (hci_event_packet_get_type(packet) == SDP_EVENT_QUERY_COMPLETE){
614             (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
615         }
616         return;
617     }
618 
619     uint8_t status;
620 
621     switch (hci_event_packet_get_type(packet)){
622         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
623             avrcp_handle_sdp_client_query_attribute_value(packet);
624             return;
625 
626         case SDP_EVENT_QUERY_COMPLETE:
627             status = sdp_event_query_complete_get_status(packet);
628 
629             if (status != ERROR_CODE_SUCCESS){
630                 avrcp_handle_sdp_query_failed(avrcp_controller_connection, status);
631                 avrcp_handle_sdp_query_failed(avrcp_target_connection, status);
632                 break;
633             }
634 
635             if (!sdp_query_context.avrcp_l2cap_psm){
636                 avrcp_handle_sdp_query_failed(avrcp_controller_connection, SDP_SERVICE_NOT_FOUND);
637                 avrcp_handle_sdp_query_failed(avrcp_target_connection, SDP_SERVICE_NOT_FOUND);
638                 break;
639             }
640 
641             avrcp_handle_sdp_query_succeeded(avrcp_controller_connection);
642             avrcp_handle_sdp_query_succeeded(avrcp_target_connection);
643 
644             l2cap_create_channel(&avrcp_packet_handler, avrcp_target_connection->remote_addr, sdp_query_context.avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
645             break;
646 
647         default:
648             return;
649     }
650 
651     // register the SDP Query request to check if there is another connection waiting for the query
652     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
653     (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
654 }
655 
656 
657 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){
658     if (connection == NULL){
659         connection = avrcp_create_connection(role, event_addr);
660     }
661     if (connection) {
662         connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
663         connection->l2cap_signaling_cid = local_cid;
664         connection->avrcp_cid = avrcp_cid;
665         connection->con_handle = con_handle;
666         btstack_run_loop_remove_timer(&connection->retry_timer);
667     }
668     return connection;
669 }
670 
671 static void avrcp_handle_open_connection(avrcp_connection_t * connection, hci_con_handle_t con_handle, uint16_t local_cid, uint16_t l2cap_mtu){
672     connection->l2cap_signaling_cid = local_cid;
673     connection->l2cap_mtu = l2cap_mtu;
674     connection->con_handle = con_handle;
675     connection->incoming_declined = false;
676     connection->song_length_ms = 0xFFFFFFFF;
677     connection->song_position_ms = 0xFFFFFFFF;
678     connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
679     connection->state = AVCTP_CONNECTION_OPENED;
680 
681     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);
682 }
683 
684 static void avrcp_retry_timer_timeout_handler(btstack_timer_source_t * timer){
685     uint16_t avrcp_cid = (uint16_t)(uintptr_t) btstack_run_loop_get_timer_context(timer);
686     avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
687     if (connection_controller == NULL) return;
688     avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
689     if (connection_target == NULL) return;
690 
691     if (connection_controller->state == AVCTP_CONNECTION_W2_L2CAP_RETRY){
692         connection_controller->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
693         connection_target->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
694         l2cap_create_channel(&avrcp_packet_handler, connection_controller->remote_addr, connection_controller->avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
695     }
696 }
697 
698 static void avrcp_retry_timer_start(avrcp_connection_t * connection){
699     btstack_run_loop_set_timer_handler(&connection->retry_timer, avrcp_retry_timer_timeout_handler);
700     btstack_run_loop_set_timer_context(&connection->retry_timer, (void *)(uintptr_t)connection->avrcp_cid);
701 
702     // add some jitter/randomness to reconnect delay
703     uint32_t timeout = 100 + (btstack_run_loop_get_time_ms() & 0x7F);
704     btstack_run_loop_set_timer(&connection->retry_timer, timeout);
705 
706     btstack_run_loop_add_timer(&connection->retry_timer);
707 }
708 
709 static avrcp_frame_type_t avrcp_get_frame_type(uint8_t header){
710     return (avrcp_frame_type_t)((header & 0x02) >> 1);
711 }
712 
713 static void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
714     UNUSED(channel);
715     UNUSED(size);
716     bd_addr_t event_addr;
717     uint16_t local_cid;
718     uint16_t l2cap_mtu;
719     uint8_t  status;
720     bool decline_connection;
721     bool outoing_active;
722     hci_con_handle_t con_handle;
723 
724     avrcp_connection_t * connection_controller;
725     avrcp_connection_t * connection_target;
726 
727     switch (packet_type) {
728         case HCI_EVENT_PACKET:
729             switch (hci_event_packet_get_type(packet)) {
730 
731                 case L2CAP_EVENT_INCOMING_CONNECTION:
732                     btstack_assert(avrcp_controller_packet_handler != NULL);
733                     btstack_assert(avrcp_target_packet_handler != NULL);
734 
735                     l2cap_event_incoming_connection_get_address(packet, event_addr);
736                     local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
737                     con_handle = l2cap_event_incoming_connection_get_handle(packet);
738 
739                     outoing_active = false;
740                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
741                     if (connection_target != NULL){
742                         if (connection_target->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED){
743                             outoing_active = true;
744                             connection_target->incoming_declined = true;
745                         }
746                     }
747 
748                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
749                     if (connection_controller != NULL){
750                         if (connection_controller->state == AVCTP_CONNECTION_W4_L2CAP_CONNECTED) {
751                             outoing_active = true;
752                             connection_controller->incoming_declined = true;
753                         }
754                     }
755 
756                     decline_connection = outoing_active;
757                     if (decline_connection == false){
758                         uint16_t avrcp_cid;
759                         if ((connection_controller == NULL) || (connection_target == NULL)){
760                             avrcp_cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
761                         } else {
762                             avrcp_cid = connection_controller->avrcp_cid;
763                         }
764                         // create two connection objects (both)
765                         connection_target     = avrcp_handle_incoming_connection_for_role(AVRCP_TARGET, connection_target, event_addr, con_handle, local_cid, avrcp_cid);
766                         connection_controller = avrcp_handle_incoming_connection_for_role(AVRCP_CONTROLLER, connection_controller, event_addr, con_handle, local_cid, avrcp_cid);
767                         if ((connection_target == NULL) || (connection_controller == NULL)){
768                             decline_connection = true;
769                             if (connection_target) {
770                                 avrcp_finalize_connection(connection_target);
771                             }
772                             if (connection_controller) {
773                                 avrcp_finalize_connection(connection_controller);
774                             }
775                         }
776                     }
777                     if (decline_connection){
778                         l2cap_decline_connection(local_cid);
779                     } else {
780                         log_info("AVRCP: L2CAP_EVENT_INCOMING_CONNECTION local cid 0x%02x, state %d", local_cid, connection_controller->state);
781                         l2cap_accept_connection(local_cid);
782                     }
783                     break;
784 
785                 case L2CAP_EVENT_CHANNEL_OPENED:
786                     l2cap_event_channel_opened_get_address(packet, event_addr);
787                     status = l2cap_event_channel_opened_get_status(packet);
788                     local_cid = l2cap_event_channel_opened_get_local_cid(packet);
789                     l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
790                     con_handle = l2cap_event_channel_opened_get_handle(packet);
791 
792                     connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, event_addr);
793                     connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, event_addr);
794 
795                     // incoming: structs are already created in L2CAP_EVENT_INCOMING_CONNECTION
796                     // outgoing: structs are cteated in avrcp_connect()
797                     if ((connection_controller == NULL) || (connection_target == NULL)) {
798                         break;
799                     }
800 
801                     switch (status){
802                         case ERROR_CODE_SUCCESS:
803                             avrcp_handle_open_connection(connection_target, con_handle, local_cid, l2cap_mtu);
804                             avrcp_handle_open_connection(connection_controller, con_handle, local_cid, l2cap_mtu);
805                             avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status);
806                             return;
807                         case L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES:
808                             if (connection_controller->incoming_declined == true){
809                                 log_info("Incoming connection was declined, and the outgoing failed");
810                                 connection_controller->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
811                                 connection_controller->incoming_declined = false;
812                                 connection_target->state = AVCTP_CONNECTION_W2_L2CAP_RETRY;
813                                 connection_target->incoming_declined = false;
814                                 avrcp_retry_timer_start(connection_controller);
815                                 return;
816                             }
817                             break;
818                         default:
819                             break;
820                     }
821                     log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
822                     avrcp_emit_connection_established(connection_controller->avrcp_cid, event_addr, con_handle, status);
823                     avrcp_finalize_connection(connection_controller);
824                     avrcp_finalize_connection(connection_target);
825 
826                     break;
827 
828                 case L2CAP_EVENT_CHANNEL_CLOSED:
829                     local_cid = l2cap_event_channel_closed_get_local_cid(packet);
830 
831                     connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid);
832                     connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid);
833                     if ((connection_controller == NULL) || (connection_target == NULL)) {
834                         break;
835                     }
836                     avrcp_emit_connection_closed(connection_controller->avrcp_cid);
837                     avrcp_finalize_connection(connection_controller);
838                     avrcp_finalize_connection(connection_target);
839                     break;
840 
841                 case L2CAP_EVENT_CAN_SEND_NOW:
842                     local_cid = l2cap_event_can_send_now_get_local_cid(packet);
843 
844                     connection_target = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_TARGET, local_cid);
845                     if ((connection_target != NULL) && connection_target->wait_to_send){
846                         connection_target->wait_to_send = false;
847                         (*avrcp_target_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
848                         break;
849                     }
850 
851                     connection_controller = avrcp_get_connection_for_l2cap_signaling_cid_for_role(AVRCP_CONTROLLER, local_cid);
852                     if ((connection_controller != NULL) && connection_controller->wait_to_send){
853                         connection_controller->wait_to_send = false;
854                         (*avrcp_controller_packet_handler)(HCI_EVENT_PACKET, channel, packet, size);
855                         break;
856                     }
857                     break;
858 
859                 default:
860                     break;
861             }
862             break;
863 
864         case L2CAP_DATA_PACKET:
865             switch (avrcp_get_frame_type(packet[0])){
866                 case AVRCP_RESPONSE_FRAME:
867                     (*avrcp_controller_packet_handler)(packet_type, channel, packet, size);
868                     break;
869                 case AVRCP_COMMAND_FRAME:
870                 default:    // make compiler happy
871                     (*avrcp_target_packet_handler)(packet_type, channel, packet, size);
872                     break;
873             }
874             break;
875 
876         default:
877             break;
878     }
879 }
880 
881 uint8_t avrcp_disconnect(uint16_t avrcp_cid){
882     avrcp_connection_t * connection_controller = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, avrcp_cid);
883     if (!connection_controller){
884         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
885     }
886     avrcp_connection_t * connection_target = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, avrcp_cid);
887     if (!connection_target){
888         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
889     }
890     if (connection_controller->browsing_connection){
891         l2cap_disconnect(connection_controller->browsing_connection->l2cap_browsing_cid, 0);
892     }
893     l2cap_disconnect(connection_controller->l2cap_signaling_cid, 0);
894     return ERROR_CODE_SUCCESS;
895 }
896 
897 static void avrcp_handle_start_sdp_client_query(void * context){
898     UNUSED(context);
899 
900     btstack_linked_list_iterator_t it;
901     btstack_linked_list_iterator_init(&it, &connections);
902     while (btstack_linked_list_iterator_has_next(&it)){
903         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
904 
905         if (connection->state != AVCTP_CONNECTION_W2_SEND_SDP_QUERY) continue;
906         connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
907 
908         // prevent triggering SDP query twice (for each role once)
909         avrcp_connection_t * connection_with_opposite_role;
910         switch (connection->role){
911             case AVRCP_CONTROLLER:
912                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_TARGET, connection->avrcp_cid);
913                 break;
914             case AVRCP_TARGET:
915                 connection_with_opposite_role = avrcp_get_connection_for_avrcp_cid_for_role(AVRCP_CONTROLLER, connection->avrcp_cid);
916                 break;
917             default:
918                 btstack_assert(false);
919                 return;
920         }
921         connection_with_opposite_role->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
922 
923         sdp_query_context.avrcp_l2cap_psm = 0;
924         sdp_query_context.avrcp_version  = 0;
925         sdp_query_context.avrcp_cid = connection->avrcp_cid;
926         sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, (uint8_t *) connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP);
927         return;
928     }
929 }
930 
931 uint8_t avrcp_connect(bd_addr_t remote_addr, uint16_t * avrcp_cid){
932     btstack_assert(avrcp_controller_packet_handler != NULL);
933     btstack_assert(avrcp_target_packet_handler != NULL);
934 
935     avrcp_connection_t * connection_controller = avrcp_get_connection_for_bd_addr_for_role(AVRCP_CONTROLLER, remote_addr);
936     if (connection_controller){
937         return ERROR_CODE_COMMAND_DISALLOWED;
938     }
939     avrcp_connection_t * connection_target = avrcp_get_connection_for_bd_addr_for_role(AVRCP_TARGET, remote_addr);
940     if (connection_target){
941         return ERROR_CODE_COMMAND_DISALLOWED;
942     }
943 
944     uint16_t cid = avrcp_get_next_cid(AVRCP_CONTROLLER);
945 
946     connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr);
947     if (!connection_controller) return BTSTACK_MEMORY_ALLOC_FAILED;
948 
949     connection_target = avrcp_create_connection(AVRCP_TARGET, remote_addr);
950     if (!connection_target){
951         avrcp_finalize_connection(connection_controller);
952         return BTSTACK_MEMORY_ALLOC_FAILED;
953     }
954 
955     if (avrcp_cid != NULL){
956         *avrcp_cid = cid;
957     }
958 
959     connection_controller->state = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
960     connection_controller->avrcp_cid = cid;
961 
962     connection_target->state     = AVCTP_CONNECTION_W2_SEND_SDP_QUERY;
963     connection_target->avrcp_cid = cid;
964 
965     avrcp_handle_sdp_client_query_request.callback = &avrcp_handle_start_sdp_client_query;
966     // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback
967     (void) sdp_client_register_query_callback(&avrcp_handle_sdp_client_query_request);
968     return ERROR_CODE_SUCCESS;
969 }
970 
971 void avrcp_init(void){
972     connections = NULL;
973     if (l2cap_service_registered) return;
974 
975     int status = l2cap_register_service(&avrcp_packet_handler, BLUETOOTH_PSM_AVCTP, 0xffff, gap_get_security_level());
976     if (status != ERROR_CODE_SUCCESS) return;
977     l2cap_service_registered = true;
978 }
979 
980 void avrcp_register_controller_packet_handler(btstack_packet_handler_t callback){
981     avrcp_controller_packet_handler = callback;
982 }
983 
984 void avrcp_register_target_packet_handler(btstack_packet_handler_t callback){
985     avrcp_target_packet_handler = callback;
986 }
987 
988 void avrcp_register_packet_handler(btstack_packet_handler_t callback){
989     btstack_assert(callback != NULL);
990     avrcp_callback = callback;
991 }
992 
993 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
994 #define FUZZ_CID 0x44
995 static bd_addr_t remote_addr = { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 };
996 void avrcp_init_fuzz(void){
997     // setup avrcp connections for cid
998     avrcp_connection_t * connection_controller = avrcp_create_connection(AVRCP_CONTROLLER, remote_addr);
999     avrcp_connection_t * connection_target     = avrcp_create_connection(AVRCP_TARGET, remote_addr);
1000     avrcp_handle_open_connection(connection_controller, FUZZ_CID, 999);
1001     avrcp_handle_open_connection(connection_target, FUZZ_CID, 999);
1002 }
1003 void avrcp_packet_handler_fuzz(uint8_t *packet, uint16_t size){
1004     avrcp_packet_handler(L2CAP_DATA_PACKET, FUZZ_CID, packet, size);
1005 }
1006 #endif