xref: /btstack/src/classic/avrcp.c (revision e9c5f44ee8add45f6cd4be8b6faa9e09a2804fcc)
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 <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "classic/avrcp.h"
47 #include "classic/avrcp_controller.h"
48 
49 #define PSM_AVCTP                       BLUETOOTH_PROTOCOL_AVCTP
50 #define PSM_AVCTP_BROWSING              0x001b
51 
52 static const char * default_avrcp_controller_service_name = "BTstack AVRCP Controller Service";
53 static const char * default_avrcp_controller_service_provider_name = "BTstack AVRCP Controller Service Provider";
54 static const char * default_avrcp_target_service_name = "BTstack AVRCP Target Service";
55 static const char * default_avrcp_target_service_provider_name = "BTstack AVRCP Target Service Provider";
56 
57 static uint16_t  avrcp_cid_counter = 0;
58 
59 static avrcp_context_t * sdp_query_context;
60 static uint8_t   attribute_value[1000];
61 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
62 
63 
64 static const char * avrcp_subunit_type_name[] = {
65     "MONITOR", "AUDIO", "PRINTER", "DISC", "TAPE_RECORDER_PLAYER", "TUNER",
66     "CA", "CAMERA", "RESERVED", "PANEL", "BULLETIN_BOARD", "CAMERA_STORAGE",
67     "VENDOR_UNIQUE", "RESERVED_FOR_ALL_SUBUNIT_TYPES",
68     "EXTENDED_TO_NEXT_BYTE", "UNIT", "ERROR"
69 };
70 
71 const char * avrcp_subunit2str(uint16_t index){
72     if (index <= 11) return avrcp_subunit_type_name[index];
73     if (index >= 0x1C && index <= 0x1F) return avrcp_subunit_type_name[index - 0x10];
74     return avrcp_subunit_type_name[16];
75 }
76 
77 static const char * avrcp_event_name[] = {
78     "ERROR", "PLAYBACK_STATUS_CHANGED",
79     "TRACK_CHANGED", "TRACK_REACHED_END", "TRACK_REACHED_START",
80     "PLAYBACK_POS_CHANGED", "BATT_STATUS_CHANGED", "SYSTEM_STATUS_CHANGED",
81     "PLAYER_APPLICATION_SETTING_CHANGED", "NOW_PLAYING_CONTENT_CHANGED",
82     "AVAILABLE_PLAYERS_CHANGED", "ADDRESSED_PLAYER_CHANGED", "UIDS_CHANGED", "VOLUME_CHANGED"
83 };
84 const char * avrcp_event2str(uint16_t index){
85     if (index <= 0x0d) return avrcp_event_name[index];
86     return avrcp_event_name[0];
87 }
88 
89 static const char * avrcp_operation_name[] = {
90     "NOT SUPPORTED", // 0x3B
91     "SKIP", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED", "NOT SUPPORTED",
92     "VOLUME_UP", "VOLUME_DOWN", "MUTE", "PLAY", "STOP", "PAUSE", "NOT SUPPORTED",
93     "REWIND", "FAST_FORWARD", "NOT SUPPORTED", "FORWARD", "BACKWARD" // 0x4C
94 };
95 const char * avrcp_operation2str(uint8_t index){
96     if (index >= 0x3B && index <= 0x4C) return avrcp_operation_name[index - 0x3B];
97     return avrcp_operation_name[0];
98 }
99 
100 static const char * avrcp_media_attribute_id_name[] = {
101     "NONE", "TITLE", "ARTIST", "ALBUM", "TRACK", "TOTAL TRACKS", "GENRE", "SONG LENGTH"
102 };
103 const char * avrcp_attribute2str(uint8_t index){
104     if (index >= 1 && index <= 7) return avrcp_media_attribute_id_name[index];
105     return avrcp_media_attribute_id_name[0];
106 }
107 
108 static const char * avrcp_play_status_name[] = {
109     "STOPPED", "PLAYING", "PAUSED", "FORWARD SEEK", "REVERSE SEEK",
110     "ERROR" // 0xFF
111 };
112 const char * avrcp_play_status2str(uint8_t index){
113     if (index >= 1 && index <= 4) return avrcp_play_status_name[index];
114     return avrcp_play_status_name[5];
115 }
116 
117 static const char * avrcp_ctype_name[] = {
118     "CONTROL",
119     "STATUS",
120     "SPECIFIC_INQUIRY",
121     "NOTIFY",
122     "GENERAL_INQUIRY",
123     "RESERVED5",
124     "RESERVED6",
125     "RESERVED7",
126     "NOT IMPLEMENTED IN REMOTE",
127     "ACCEPTED BY REMOTE",
128     "REJECTED BY REMOTE",
129     "IN_TRANSITION",
130     "IMPLEMENTED_STABLE",
131     "CHANGED_STABLE",
132     "RESERVED",
133     "INTERIM"
134 };
135 const char * avrcp_ctype2str(uint8_t index){
136     if (index < sizeof(avrcp_ctype_name)){
137         return avrcp_ctype_name[index];
138     }
139     return "NONE";
140 }
141 
142 static const char * avrcp_shuffle_mode_name[] = {
143     "SHUFFLE OFF",
144     "SHUFFLE ALL TRACKS",
145     "SHUFFLE GROUP"
146 };
147 
148 const char * avrcp_shuffle2str(uint8_t index){
149     if (index >= 1 && index <= 3) return avrcp_shuffle_mode_name[index-1];
150     return "NONE";
151 }
152 
153 static const char * avrcp_repeat_mode_name[] = {
154     "REPEAT OFF",
155     "REPEAT SINGLE TRACK",
156     "REPEAT ALL TRACKS",
157     "REPEAT GROUP"
158 };
159 
160 const char * avrcp_repeat2str(uint8_t index){
161     if (index >= 1 && index <= 4) return avrcp_repeat_mode_name[index-1];
162     return "NONE";
163 }
164 
165 uint8_t avrcp_cmd_opcode(uint8_t *packet, uint16_t size){
166     uint8_t cmd_opcode_index = 5;
167     if (cmd_opcode_index > size) return AVRCP_CMD_OPCODE_UNDEFINED;
168     return packet[cmd_opcode_index];
169 }
170 
171 void avrcp_create_sdp_record(uint8_t controller, uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features,
172     const char * service_name, const char * service_provider_name){
173     uint8_t* attribute;
174     de_create_sequence(service);
175 
176     // 0x0000 "Service Record Handle"
177     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE);
178     de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle);
179 
180     // 0x0001 "Service Class ID List"
181     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST);
182     attribute = de_push_sequence(service);
183     {
184         if (controller){
185             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
186             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER);
187         } else {
188             de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET);
189         }
190     }
191     de_pop_sequence(service, attribute);
192 
193     // 0x0004 "Protocol Descriptor List"
194     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST);
195     attribute = de_push_sequence(service);
196     {
197         uint8_t* l2cpProtocol = de_push_sequence(attribute);
198         {
199             de_add_number(l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
200             de_add_number(l2cpProtocol,  DE_UINT, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP);
201         }
202         de_pop_sequence(attribute, l2cpProtocol);
203 
204         uint8_t* avctpProtocol = de_push_sequence(attribute);
205         {
206             de_add_number(avctpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP);  // avctpProtocol_service
207             de_add_number(avctpProtocol,  DE_UINT, DE_SIZE_16,  0x0103);    // version
208         }
209         de_pop_sequence(attribute, avctpProtocol);
210     }
211     de_pop_sequence(service, attribute);
212 
213     // 0x0005 "Public Browse Group"
214     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group
215     attribute = de_push_sequence(service);
216     {
217         de_add_number(attribute,  DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT);
218     }
219     de_pop_sequence(service, attribute);
220 
221     // 0x0009 "Bluetooth Profile Descriptor List"
222     de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST);
223     attribute = de_push_sequence(service);
224     {
225         uint8_t *avrcProfile = de_push_sequence(attribute);
226         {
227             de_add_number(avrcProfile,  DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL);
228             de_add_number(avrcProfile,  DE_UINT, DE_SIZE_16, 0x0105);
229         }
230         de_pop_sequence(attribute, avrcProfile);
231     }
232     de_pop_sequence(service, attribute);
233 
234     // 0x000d "Additional Bluetooth Profile Descriptor List"
235     if (browsing){
236         de_add_number(service,  DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS);
237         attribute = de_push_sequence(service);
238         {
239             uint8_t * des = de_push_sequence(attribute);
240             {
241                 uint8_t* browsing_l2cpProtocol = de_push_sequence(des);
242                 {
243                     de_add_number(browsing_l2cpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP);
244                     de_add_number(browsing_l2cpProtocol,  DE_UINT, DE_SIZE_16, PSM_AVCTP_BROWSING);
245                 }
246                 de_pop_sequence(des, browsing_l2cpProtocol);
247 
248                 uint8_t* browsing_avctpProtocol = de_push_sequence(des);
249                 {
250                     de_add_number(browsing_avctpProtocol,  DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_AVCTP);  // browsing_avctpProtocol_service
251                     de_add_number(browsing_avctpProtocol,  DE_UINT, DE_SIZE_16,  0x0103);    // version
252                 }
253                 de_pop_sequence(des, browsing_avctpProtocol);
254             }
255             de_pop_sequence(attribute, des);
256         }
257         de_pop_sequence(service, attribute);
258     }
259 
260 
261     // 0x0100 "Service Name"
262     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0100);
263     if (service_name){
264         de_add_data(service,  DE_STRING, strlen(service_name), (uint8_t *) service_name);
265     } else {
266         if (controller){
267             de_add_data(service,  DE_STRING, strlen(default_avrcp_controller_service_name), (uint8_t *) default_avrcp_controller_service_name);
268         } else {
269             de_add_data(service,  DE_STRING, strlen(default_avrcp_target_service_name), (uint8_t *) default_avrcp_target_service_name);
270         }
271     }
272 
273     // 0x0100 "Provider Name"
274     de_add_number(service,  DE_UINT, DE_SIZE_16, 0x0102);
275     if (service_provider_name){
276         de_add_data(service,  DE_STRING, strlen(service_provider_name), (uint8_t *) service_provider_name);
277     } else {
278         if (controller){
279             de_add_data(service,  DE_STRING, strlen(default_avrcp_controller_service_provider_name), (uint8_t *) default_avrcp_controller_service_provider_name);
280         } else {
281             de_add_data(service,  DE_STRING, strlen(default_avrcp_target_service_provider_name), (uint8_t *) default_avrcp_target_service_provider_name);
282         }
283     }
284 
285     // 0x0311 "Supported Features"
286     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);
287     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
288 }
289 
290 avrcp_connection_t * get_avrcp_connection_for_bd_addr(bd_addr_t addr, avrcp_context_t * context){
291     btstack_linked_list_iterator_t it;
292     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *) &context->connections);
293     while (btstack_linked_list_iterator_has_next(&it)){
294         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
295         if (memcmp(addr, connection->remote_addr, 6) != 0) continue;
296         return connection;
297     }
298     return NULL;
299 }
300 
301 avrcp_connection_t * get_avrcp_connection_for_l2cap_signaling_cid(uint16_t l2cap_cid, avrcp_context_t * context){
302     btstack_linked_list_iterator_t it;
303     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
304     while (btstack_linked_list_iterator_has_next(&it)){
305         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
306         if (connection->l2cap_signaling_cid != l2cap_cid) continue;
307         return connection;
308     }
309     return NULL;
310 }
311 
312 avrcp_connection_t * get_avrcp_connection_for_avrcp_cid(uint16_t avrcp_cid, avrcp_context_t * context){
313     btstack_linked_list_iterator_t it;
314     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
315     while (btstack_linked_list_iterator_has_next(&it)){
316         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
317         if (connection->avrcp_cid != avrcp_cid) continue;
318         return connection;
319     }
320     return NULL;
321 }
322 
323 void avrcp_request_can_send_now(avrcp_connection_t * connection, uint16_t l2cap_cid){
324     connection->wait_to_send = 1;
325     l2cap_request_can_send_now_event(l2cap_cid);
326 }
327 
328 
329 uint16_t avrcp_get_next_cid(void){
330     avrcp_cid_counter++;
331     if (avrcp_cid_counter == 0){
332         avrcp_cid_counter = 1;
333     }
334     return avrcp_cid_counter;
335 }
336 
337 static avrcp_connection_t * avrcp_create_connection(bd_addr_t remote_addr, avrcp_context_t * context){
338     avrcp_connection_t * connection = btstack_memory_avrcp_connection_get();
339     if (!connection){
340         log_error("avrcp: not enough memory to create connection");
341         return NULL;
342     }
343     connection->state = AVCTP_CONNECTION_IDLE;
344     connection->transaction_label = 0xFF;
345     connection->max_num_fragments = 0xFF;
346     connection->avrcp_cid = avrcp_get_next_cid();
347     memcpy(connection->remote_addr, remote_addr, 6);
348     btstack_linked_list_add(&context->connections, (btstack_linked_item_t *) connection);
349     return connection;
350 }
351 
352 static void avrcp_finalize_connection(avrcp_connection_t * connection, avrcp_context_t * context){
353     btstack_linked_list_remove(&context->connections, (btstack_linked_item_t*) connection);
354     btstack_memory_avrcp_connection_free(connection);
355 }
356 
357 void avrcp_emit_connection_established(btstack_packet_handler_t callback, uint16_t avrcp_cid, bd_addr_t addr, uint8_t status){
358     if (!callback) return;
359     uint8_t event[12];
360     int pos = 0;
361     event[pos++] = HCI_EVENT_AVRCP_META;
362     event[pos++] = sizeof(event) - 2;
363     event[pos++] = AVRCP_SUBEVENT_CONNECTION_ESTABLISHED;
364     event[pos++] = status;
365     reverse_bd_addr(addr,&event[pos]);
366     pos += 6;
367     little_endian_store_16(event, pos, avrcp_cid);
368     pos += 2;
369     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
370 }
371 
372 void avrcp_emit_connection_closed(btstack_packet_handler_t callback, uint16_t avrcp_cid){
373     if (!callback) return;
374     uint8_t event[5];
375     int pos = 0;
376     event[pos++] = HCI_EVENT_AVRCP_META;
377     event[pos++] = sizeof(event) - 2;
378     event[pos++] = AVRCP_SUBEVENT_CONNECTION_RELEASED;
379     little_endian_store_16(event, pos, avrcp_cid);
380     pos += 2;
381     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
382 }
383 
384 void avrcp_handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
385     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(sdp_query_context->avrcp_cid, sdp_query_context);
386     if (!connection) return;
387     if (connection->state != AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE) return;
388 
389     UNUSED(packet_type);
390     UNUSED(channel);
391     UNUSED(size);
392     uint8_t status;
393     des_iterator_t des_list_it;
394     des_iterator_t prot_it;
395     // uint32_t avdtp_remote_uuid    = 0;
396 
397     switch (hci_event_packet_get_type(packet)){
398         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
399             // Handle new SDP record
400             if (sdp_event_query_attribute_byte_get_record_id(packet) != sdp_query_context->record_id) {
401                 sdp_query_context->record_id = sdp_event_query_attribute_byte_get_record_id(packet);
402                 sdp_query_context->parse_sdp_record = 0;
403                 // log_info("SDP Record: Nr: %d", record_id);
404             }
405 
406             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
407                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
408 
409                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
410                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
411                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
412                             if (de_get_element_type(attribute_value) != DE_DES) break;
413                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
414                                 uint8_t * element = des_iterator_get_element(&des_list_it);
415                                 if (de_get_element_type(element) != DE_UUID) continue;
416                                 uint32_t uuid = de_get_uuid32(element);
417                                 switch (uuid){
418                                     case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET:
419                                         if (sdp_query_context->role == AVRCP_CONTROLLER) {
420                                             sdp_query_context->parse_sdp_record = 1;
421                                             break;
422                                         }
423                                         break;
424                                     case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL:
425                                     case BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL_CONTROLLER:
426                                         if (sdp_query_context->role == AVRCP_TARGET) {
427                                             sdp_query_context->parse_sdp_record = 1;
428                                             break;
429                                         }
430                                         break;
431                                     default:
432                                         break;
433                                 }
434                             }
435                             break;
436 
437                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
438                                 if (!sdp_query_context->parse_sdp_record) break;
439                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
440                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
441                                     uint8_t       *des_element;
442                                     uint8_t       *element;
443                                     uint32_t       uuid;
444 
445                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
446 
447                                     des_element = des_iterator_get_element(&des_list_it);
448                                     des_iterator_init(&prot_it, des_element);
449                                     element = des_iterator_get_element(&prot_it);
450 
451                                     if (de_get_element_type(element) != DE_UUID) continue;
452 
453                                     uuid = de_get_uuid32(element);
454                                     des_iterator_next(&prot_it);
455                                     switch (uuid){
456                                         case BLUETOOTH_PROTOCOL_L2CAP:
457                                             if (!des_iterator_has_more(&prot_it)) continue;
458                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_l2cap_psm);
459                                             break;
460                                         case BLUETOOTH_PROTOCOL_AVCTP:
461                                             if (!des_iterator_has_more(&prot_it)) continue;
462                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_query_context->avrcp_version);
463                                             break;
464                                         default:
465                                             break;
466                                     }
467                                 }
468                             }
469                             break;
470                         case BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS: {
471                                 // log_info("SDP Attribute: 0x%04x", sdp_event_query_attribute_byte_get_attribute_id(packet));
472                                 if (!sdp_query_context->parse_sdp_record) break;
473                                 if (de_get_element_type(attribute_value) != DE_DES) break;
474 
475                                 des_iterator_t des_list_0_it;
476                                 uint8_t       *element_0;
477 
478                                 des_iterator_init(&des_list_0_it, attribute_value);
479                                 element_0 = des_iterator_get_element(&des_list_0_it);
480 
481                                 for (des_iterator_init(&des_list_it, element_0); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
482                                     uint8_t       *des_element;
483                                     uint8_t       *element;
484                                     uint32_t       uuid;
485 
486                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
487 
488                                     des_element = des_iterator_get_element(&des_list_it);
489                                     des_iterator_init(&prot_it, des_element);
490                                     element = des_iterator_get_element(&prot_it);
491 
492                                     if (de_get_element_type(element) != DE_UUID) continue;
493 
494                                     uuid = de_get_uuid32(element);
495                                     des_iterator_next(&prot_it);
496                                     switch (uuid){
497                                         case BLUETOOTH_PROTOCOL_L2CAP:
498                                             if (!des_iterator_has_more(&prot_it)) continue;
499                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_l2cap_psm);
500                                             break;
501                                         case BLUETOOTH_PROTOCOL_AVCTP:
502                                             if (!des_iterator_has_more(&prot_it)) continue;
503                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->browsing_version);
504                                             break;
505                                         default:
506                                             break;
507                                     }
508                                 }
509                             }
510                             break;
511                         default:
512                             break;
513                     }
514                 }
515             } else {
516                 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));
517             }
518             break;
519 
520         case SDP_EVENT_QUERY_COMPLETE:{
521             status = sdp_event_query_complete_get_status(packet);
522 
523             if (status != ERROR_CODE_SUCCESS){
524                 log_info("AVRCP: SDP query failed with status 0x%02x.", status);
525                 avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, status);
526                 avrcp_finalize_connection(connection, sdp_query_context);
527                 break;
528             }
529 
530             if (!sdp_query_context->avrcp_l2cap_psm){
531                 connection->state = AVCTP_CONNECTION_IDLE;
532                 log_info("AVRCP: no suitable service found");
533                 avrcp_emit_connection_established(sdp_query_context->avrcp_callback, connection->avrcp_cid, connection->remote_addr, SDP_SERVICE_NOT_FOUND);
534                 avrcp_finalize_connection(connection, sdp_query_context);
535                 break;
536             }
537             connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
538             l2cap_create_channel(sdp_query_context->packet_handler, connection->remote_addr, sdp_query_context->avrcp_l2cap_psm, l2cap_max_mtu(), NULL);
539             break;
540         }
541     }
542 }
543 
544 void avrcp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){
545     UNUSED(channel);
546     UNUSED(size);
547     bd_addr_t event_addr;
548     uint16_t local_cid;
549     uint8_t  status;
550     avrcp_connection_t * connection = NULL;
551     uint16_t psm;
552 
553     if (packet_type != HCI_EVENT_PACKET) return;
554 
555     switch (hci_event_packet_get_type(packet)) {
556         case HCI_EVENT_DISCONNECTION_COMPLETE:
557             avrcp_emit_connection_closed(context->avrcp_callback, 0);
558             break;
559         case L2CAP_EVENT_INCOMING_CONNECTION:
560             l2cap_event_incoming_connection_get_address(packet, event_addr);
561             local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
562             connection = avrcp_create_connection(event_addr, context);
563             if (!connection) {
564                 log_error("Failed to alloc connection structure");
565                 l2cap_decline_connection(local_cid);
566                 break;
567             }
568 
569             psm = l2cap_event_incoming_connection_get_psm(packet);
570             if (psm == PSM_AVCTP){
571                 if (!connection->l2cap_signaling_cid){
572                     connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
573                     connection->l2cap_signaling_cid = local_cid;
574                     log_info("L2CAP_EVENT_INCOMING_CONNECTION avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->avrcp_cid, connection->l2cap_signaling_cid);
575                     l2cap_accept_connection(local_cid);
576                     break;
577                 }
578             }
579             log_info("L2CAP_EVENT_INCOMING_CONNECTION local_cid 0x%02x, psm 0x%2x, decline connection", local_cid, psm);
580             l2cap_decline_connection(local_cid);
581             break;
582 
583         case L2CAP_EVENT_CHANNEL_OPENED:
584             l2cap_event_channel_opened_get_address(packet, event_addr);
585             status = l2cap_event_channel_opened_get_status(packet);
586             local_cid = l2cap_event_channel_opened_get_local_cid(packet);
587 
588             connection = get_avrcp_connection_for_bd_addr(event_addr, context);
589             if (!connection){
590                 // TODO: validate if this cannot happen. If not,  drop disconnect call
591                 log_error("AVRCP connection lookup failed");
592                 l2cap_disconnect(local_cid, 0); // reason isn't used
593                 break;
594             }
595             if (status != ERROR_CODE_SUCCESS){
596                 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
597                 avrcp_emit_connection_established(context->avrcp_callback, connection->avrcp_cid, event_addr, status);
598                 avrcp_finalize_connection(connection, context);
599                 break;
600             }
601 
602             psm = l2cap_event_channel_opened_get_psm(packet);
603             if (psm == PSM_AVCTP){
604                 connection->l2cap_signaling_cid = local_cid;
605                 connection->l2cap_mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
606                 connection->song_length_ms = 0xFFFFFFFF;
607                 connection->song_position_ms = 0xFFFFFFFF;
608                 connection->playback_status = AVRCP_PLAYBACK_STATUS_ERROR;
609 
610                 log_info("L2CAP_EVENT_CHANNEL_OPENED avrcp_cid 0x%02x, l2cap_signaling_cid 0x%02x", connection->avrcp_cid, connection->l2cap_signaling_cid);
611                 connection->state = AVCTP_CONNECTION_OPENED;
612                 avrcp_emit_connection_established(context->avrcp_callback, connection->avrcp_cid, event_addr, ERROR_CODE_SUCCESS);
613             }
614             break;
615 
616         case L2CAP_EVENT_CHANNEL_CLOSED:
617             // data: event (8), len(8), channel (16)
618             local_cid = l2cap_event_channel_closed_get_local_cid(packet);
619             connection = get_avrcp_connection_for_l2cap_signaling_cid(local_cid, context);
620             if (connection){
621                 avrcp_emit_connection_closed(context->avrcp_callback, connection->avrcp_cid);
622                 avrcp_finalize_connection(connection, context);
623                 break;
624             }
625             break;
626         default:
627             break;
628     }
629 }
630 
631 uint8_t avrcp_connect(bd_addr_t bd_addr, avrcp_context_t * context, uint16_t * avrcp_cid){
632     avrcp_connection_t * connection = get_avrcp_connection_for_bd_addr(bd_addr, context);
633     if (connection) return ERROR_CODE_COMMAND_DISALLOWED;
634 
635     if (!sdp_client_ready()) return ERROR_CODE_COMMAND_DISALLOWED;
636 
637     connection = avrcp_create_connection(bd_addr, context);
638     if (!connection){
639         log_error("avrcp: could not allocate connection struct.");
640         return BTSTACK_MEMORY_ALLOC_FAILED;
641     }
642 
643     if (avrcp_cid){
644         *avrcp_cid = connection->avrcp_cid;
645     }
646 
647     context->avrcp_l2cap_psm = 0;
648     context->avrcp_version = 0;
649     context->avrcp_cid = connection->avrcp_cid;
650     connection->browsing_l2cap_psm = 0;
651     connection->state = AVCTP_CONNECTION_W4_SDP_QUERY_COMPLETE;
652 
653     sdp_query_context = context;
654     sdp_client_query_uuid16(&avrcp_handle_sdp_client_query_result, connection->remote_addr, BLUETOOTH_PROTOCOL_AVCTP);
655     return ERROR_CODE_SUCCESS;
656 }
657