xref: /btstack/src/classic/avrcp_browsing_controller.c (revision cb93c223bc3c718fbf6e414578af3369cbf3345f)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "avrcp_browsing_controller.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <inttypes.h>
45 #include "btstack.h"
46 #include "classic/avrcp.h"
47 #include "classic/avrcp_browsing_controller.h"
48 
49 #define PSM_AVCTP_BROWSING              0x001b
50 
51 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context);
52 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
53 
54 static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){
55     btstack_linked_list_iterator_t it;
56     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
57     while (btstack_linked_list_iterator_has_next(&it)){
58         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
59         if (connection->avrcp_browsing_cid != browsing_cid) continue;
60         return connection;
61     }
62     return NULL;
63 }
64 
65 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){
66     btstack_linked_list_iterator_t it;
67     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
68     while (btstack_linked_list_iterator_has_next(&it)){
69         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
70         if (connection->browsing_connection &&  connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue;
71         return connection;
72     }
73     return NULL;
74 }
75 
76 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){
77     btstack_linked_list_iterator_t it;
78     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
79     while (btstack_linked_list_iterator_has_next(&it)){
80         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
81         if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue;
82         return connection->browsing_connection;
83     }
84     return NULL;
85 }
86 
87 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
88     if (!callback) return;
89     uint8_t event[12];
90     int pos = 0;
91     event[pos++] = HCI_EVENT_AVRCP_META;
92     event[pos++] = sizeof(event) - 2;
93     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
94     event[pos++] = status;
95     reverse_bd_addr(addr,&event[pos]);
96     pos += 6;
97     little_endian_store_16(event, pos, browsing_cid);
98     pos += 2;
99     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
100 }
101 
102 static void avrcp_emit_incoming_browsing_connection(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr){
103     if (!callback) return;
104     uint8_t event[11];
105     int pos = 0;
106     event[pos++] = HCI_EVENT_AVRCP_META;
107     event[pos++] = sizeof(event) - 2;
108     event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
109     reverse_bd_addr(addr,&event[pos]);
110     pos += 6;
111     little_endian_store_16(event, pos, browsing_cid);
112     pos += 2;
113     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
114 }
115 
116 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){
117     if (!callback) return;
118     uint8_t event[5];
119     int pos = 0;
120     event[pos++] = HCI_EVENT_AVRCP_META;
121     event[pos++] = sizeof(event) - 2;
122     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED;
123     little_endian_store_16(event, pos, browsing_cid);
124     pos += 2;
125     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
126 }
127 
128 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){
129     avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get();
130     memset(connection, 0, sizeof(avrcp_browsing_connection_t));
131     connection->state = AVCTP_CONNECTION_IDLE;
132     connection->transaction_label = 0xFF;
133     avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid();
134     avrcp_connection->browsing_connection = connection;
135     return connection;
136 }
137 
138 static uint8_t avrcp_browsing_connect(bd_addr_t remote_addr, avrcp_context_t * context, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * browsing_cid){
139     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context);
140 
141     if (!avrcp_connection){
142         log_error("avrcp: there is no previously established AVRCP controller connection.");
143         return ERROR_CODE_COMMAND_DISALLOWED;
144     }
145 
146     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
147     if (connection){
148         log_error(" avrcp_browsing_connect connection exists.");
149         return ERROR_CODE_SUCCESS;
150     }
151 
152     connection = avrcp_browsing_create_connection(avrcp_connection);
153     if (!connection){
154         log_error("avrcp: could not allocate connection struct.");
155         return BTSTACK_MEMORY_ALLOC_FAILED;
156     }
157 
158     if (browsing_cid){
159         *browsing_cid = avrcp_connection->avrcp_browsing_cid;
160     }
161 
162     connection->ertm_buffer = ertm_buffer;
163     connection->ertm_buffer_size = size;
164     avrcp_connection->browsing_connection = connection;
165     avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
166     memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t));
167 
168     return l2cap_create_ertm_channel(avrcp_browsing_controller_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm,
169                     &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL);
170 
171 }
172 
173 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){
174     UNUSED(channel);
175     UNUSED(size);
176     bd_addr_t event_addr;
177     uint16_t local_cid;
178     uint8_t  status;
179     avrcp_browsing_connection_t * browsing_connection = NULL;
180     avrcp_connection_t * avrcp_connection = NULL;
181 
182     if (packet_type != HCI_EVENT_PACKET) return;
183 
184     switch (hci_event_packet_get_type(packet)) {
185         case HCI_EVENT_DISCONNECTION_COMPLETE:
186             avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, 0);
187             break;
188         case L2CAP_EVENT_INCOMING_CONNECTION:
189             l2cap_event_incoming_connection_get_address(packet, event_addr);
190             local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
191             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
192             if (!avrcp_connection) {
193                 log_error("No previously created AVRCP controller connections");
194                 l2cap_decline_connection(local_cid);
195                 break;
196             }
197             browsing_connection = avrcp_browsing_create_connection(avrcp_connection);
198             browsing_connection->l2cap_browsing_cid = local_cid;
199             browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
200             log_info("Emit AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x\n", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid);
201             avrcp_emit_incoming_browsing_connection(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr);
202             break;
203 
204         case L2CAP_EVENT_CHANNEL_OPENED:
205             l2cap_event_channel_opened_get_address(packet, event_addr);
206             status = l2cap_event_channel_opened_get_status(packet);
207             local_cid = l2cap_event_channel_opened_get_local_cid(packet);
208 
209             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
210             if (!avrcp_connection){
211                 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr));
212                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST);
213                 l2cap_disconnect(local_cid, 0); // reason isn't used
214                 break;
215             }
216 
217             browsing_connection = avrcp_connection->browsing_connection;
218             if (status != ERROR_CODE_SUCCESS){
219                 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
220                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status);
221                 btstack_memory_avrcp_browsing_connection_free(browsing_connection);
222                 avrcp_connection->browsing_connection = NULL;
223                 break;
224             }
225             browsing_connection->l2cap_browsing_cid = local_cid;
226 
227             log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid);
228             browsing_connection->state = AVCTP_CONNECTION_OPENED;
229             avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS);
230             break;
231 
232         case L2CAP_EVENT_CHANNEL_CLOSED:
233             // data: event (8), len(8), channel (16)
234             local_cid = l2cap_event_channel_closed_get_local_cid(packet);
235             avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context);
236 
237             if (avrcp_connection && avrcp_connection->browsing_connection){
238                 avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid);
239                 // free connection
240                 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
241                 avrcp_connection->browsing_connection = NULL;
242                 break;
243             }
244             break;
245         default:
246             break;
247     }
248 }
249 
250 static int avrcp_browsing_controller_send_get_folder_items_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
251     uint8_t command[100];
252     int pos = 0;
253 
254     // // if (connection->cmd_operands[3] == AVRCP_PDU_ID_GET_CAPABILITIES){
255     //     printf("cheet\n");
256     //     uint8_t buffer[] = {0xB0, 0x11, 0x0E, 0xFF, 0x00, 0x02, 0xFF, 0xFF};
257     //     memcpy(command, buffer, sizeof(buffer));
258     //     pos =  sizeof(buffer);
259     //     return l2cap_send(cid, command, pos);
260     // // }
261 
262     // transport header
263     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
264     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
265     // Profile IDentifier (PID)
266     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
267     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
268     command[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS;
269 
270     uint32_t attribute_count = 0;
271     uint32_t attributes_to_copy = 0;
272 
273     switch (connection->attr_bitmap){
274         case AVRCP_MEDIA_ATTR_NONE:
275             attribute_count = AVRCP_MEDIA_ATTR_NONE; // 0xFFFFFFFF
276             break;
277         case AVRCP_MEDIA_ATTR_ALL:
278             attribute_count = AVRCP_MEDIA_ATTR_ALL;  // 0
279             break;
280         default:
281             attribute_count    = count_set_bits_uint32(connection->attr_bitmap & 0xff);
282             attributes_to_copy = attribute_count;
283             break;
284     }
285     big_endian_store_16(command, pos, 9 + 1 + attribute_count*4);
286     pos += 2;
287 
288     command[pos++] = connection->scope;
289     big_endian_store_32(command, pos, connection->start_item);
290     pos += 4;
291     big_endian_store_32(command, pos, connection->end_item);
292     pos += 4;
293     command[pos++] = attribute_count;
294 
295     int bit_position = 1;
296     while (attributes_to_copy){
297         if (connection->attr_bitmap & (1 << bit_position)){
298             big_endian_store_32(command, pos, bit_position);
299             pos += 4;
300             attributes_to_copy--;
301         }
302         bit_position++;
303     }
304     return l2cap_send(cid, command, pos);
305 }
306 
307 
308 static int avrcp_browsing_controller_send_get_item_attributes_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
309     uint8_t command[100];
310     int pos = 0;
311     // transport header
312     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
313     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
314     // Profile IDentifier (PID)
315     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
316     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
317     command[pos++] = AVRCP_PDU_ID_GET_ITEM_ATTRIBUTES;
318 
319     uint32_t attribute_count;
320     uint32_t attributes_to_copy = 0;
321 
322     switch (connection->attr_bitmap){
323         case AVRCP_MEDIA_ATTR_NONE:
324         case AVRCP_MEDIA_ATTR_ALL:
325             attribute_count = 0;
326             break;
327         default:
328             attribute_count = count_set_bits_uint32(connection->attr_bitmap & 0xff);
329             attributes_to_copy = attribute_count;
330             break;
331     }
332 
333     big_endian_store_16(command, pos, 12 + attribute_count*4);
334     pos += 2;
335 
336     command[pos++] = connection->scope;
337     memcpy(command+pos, connection->folder_uid, 8);
338     pos += 8;
339     big_endian_store_16(command, pos, connection->uid_counter);
340     pos += 2;
341     command[pos++] = attribute_count;
342 
343     int bit_position = 1;
344     while (attributes_to_copy){
345         if (connection->attr_bitmap & (1 << bit_position)){
346             big_endian_store_32(command, pos, bit_position);
347             pos += 4;
348             attributes_to_copy--;
349         }
350         bit_position++;
351     }
352 
353     return l2cap_send(cid, command, pos);
354 }
355 
356 
357 static int avrcp_browsing_controller_send_change_path_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
358     uint8_t command[100];
359     int pos = 0;
360     // transport header
361     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
362     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
363     // Profile IDentifier (PID)
364     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
365     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
366     command[pos++] = AVRCP_PDU_ID_CHANGE_PATH;
367 
368     big_endian_store_16(command, pos, 11);
369     pos += 2;
370     pos += 2;
371     command[pos++] = connection->direction;
372     memcpy(command+pos, connection->folder_uid, 8);
373     pos += 8;
374     return l2cap_send(cid, command, pos);
375 }
376 
377 static int avrcp_browsing_controller_send_search_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
378     uint8_t command[100];
379     int pos = 0;
380     // transport header
381     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
382     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
383     // Profile IDentifier (PID)
384     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
385     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
386     command[pos++] = AVRCP_PDU_ID_SEARCH;
387 
388     big_endian_store_16(command, pos, 4 + connection->search_str_len);
389     pos += 2;
390 
391     big_endian_store_16(command, pos, 0x006A);
392     pos += 2;
393     big_endian_store_16(command, pos, connection->search_str_len);
394     pos += 2;
395 
396     memcpy(command+pos,connection->search_str, connection->search_str_len);
397     pos += connection->search_str_len;
398     return l2cap_send(cid, command, pos);
399 }
400 
401 static int avrcp_browsing_controller_send_set_browsed_player_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
402     uint8_t command[100];
403     int pos = 0;
404     // transport header
405     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
406     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
407     // Profile IDentifier (PID)
408     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
409     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
410     command[pos++] = AVRCP_PDU_ID_SET_BROWSED_PLAYER;
411 
412     big_endian_store_16(command, pos, 2);
413     pos += 2;
414     big_endian_store_16(command, pos, connection->browsed_player_id);
415     pos += 2;
416     return l2cap_send(cid, command, pos);
417 }
418 
419 static int avrcp_browsing_controller_send_get_total_nr_items_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
420     uint8_t command[7];
421     int pos = 0;
422     // transport header
423     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
424     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
425     // Profile IDentifier (PID)
426     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
427     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
428     command[pos++] = AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS;
429 
430     big_endian_store_16(command, pos, 1);
431     pos += 2;
432     command[pos++] = connection->get_total_nr_items_scope;
433     return l2cap_send(cid, command, pos);
434 }
435 
436 static void avrcp_browsing_controller_handle_can_send_now(avrcp_browsing_connection_t * connection){
437     switch (connection->state){
438         case AVCTP_CONNECTION_OPENED:
439             if (connection->set_browsed_player_id){
440                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
441                 connection->set_browsed_player_id = 0;
442                 avrcp_browsing_controller_send_set_browsed_player_cmd(connection->l2cap_browsing_cid, connection);
443                 break;
444             }
445 
446             if (connection->get_total_nr_items){
447                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
448                 connection->get_total_nr_items = 0;
449                 avrcp_browsing_controller_send_get_total_nr_items_cmd(connection->l2cap_browsing_cid, connection);
450                 break;
451             }
452 
453             if (connection->get_folder_items){
454                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
455                 connection->get_folder_items = 0;
456                 avrcp_browsing_controller_send_get_folder_items_cmd(connection->l2cap_browsing_cid, connection);
457                 break;
458             }
459 
460             if (connection->get_item_attributes){
461                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
462                 connection->get_item_attributes = 0;
463                 avrcp_browsing_controller_send_get_item_attributes_cmd(connection->l2cap_browsing_cid, connection);
464                 break;
465             }
466 
467             if (connection->change_path){
468                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
469                 connection->change_path = 0;
470                 avrcp_browsing_controller_send_change_path_cmd(connection->l2cap_browsing_cid, connection);
471                 break;
472             }
473 
474             if (connection->search){
475                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
476                 connection->search = 0;
477                 avrcp_browsing_controller_send_search_cmd(connection->l2cap_browsing_cid, connection);
478                 break;
479             }
480             break;
481         default:
482             return;
483     }
484 }
485 
486 
487 static void avrcp_browsing_controller_emit_done_with_uid_counter(btstack_packet_handler_t callback, uint16_t browsing_cid, uint16_t uid_counter, uint8_t browsing_status, uint8_t bluetooth_status){
488     if (!callback) return;
489     uint8_t event[9];
490     int pos = 0;
491     event[pos++] = HCI_EVENT_AVRCP_META;
492     event[pos++] = sizeof(event) - 2;
493     event[pos++] = AVRCP_SUBEVENT_BROWSING_DONE;
494     little_endian_store_16(event, pos, browsing_cid);
495     pos += 2;
496     little_endian_store_16(event, pos, uid_counter);
497     pos += 2;
498     event[pos++] = browsing_status;
499     event[pos++] = bluetooth_status;
500     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
501 }
502 
503 static void avrcp_parser_reset(avrcp_browsing_connection_t * connection){
504     connection->parser_attribute_header_pos = 0;
505     connection->parsed_attribute_value_offset = 0;
506     connection->parsed_num_attributes = 0;
507     connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
508 }
509 
510 
511 static void avrcp_browsing_parser_process_byte(uint8_t byte, avrcp_browsing_connection_t * connection){
512     uint8_t prepended_header_size = 1;
513     switch(connection->parser_state){
514         case AVRCP_PARSER_GET_ATTRIBUTE_HEADER:{
515             connection->parser_attribute_header[connection->parser_attribute_header_pos++] = byte;
516             if (connection->parser_attribute_header_pos < AVRCP_BROWSING_ITEM_HEADER_LEN) break;
517 
518             uint16_t attribute_total_value_len = big_endian_read_16(connection->parser_attribute_header, 1);
519             connection->parsed_attribute_value[connection->parsed_attribute_value_offset++] = connection->parser_attribute_header[0];   // prepend with item type
520             connection->parsed_attribute_value_len = btstack_min(attribute_total_value_len, AVRCP_MAX_ATTRIBUTTE_SIZE - prepended_header_size);                 // reduce AVRCP_MAX_ATTRIBUTTE_SIZE for the size ot item type
521             connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_VALUE;
522             // printf("AVRCP_PARSER_GET_ATTRIBUTE_HEADER value len %d, to parse %d, offset %d\n", attribute_total_value_len, connection->parsed_attribute_value_len, connection->parsed_attribute_value_offset);
523             break;
524         }
525         case AVRCP_PARSER_GET_ATTRIBUTE_VALUE:{
526             connection->parsed_attribute_value[connection->parsed_attribute_value_offset++] = byte;
527             if (connection->parsed_attribute_value_offset < connection->parsed_attribute_value_len + prepended_header_size){
528                 break;
529             }
530             if (connection->parsed_attribute_value_offset < big_endian_read_16(connection->parser_attribute_header, 1)){
531                 connection->parser_state = AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE;
532                 break;
533             }
534             connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
535             (*avrcp_controller_context.browsing_avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, connection->l2cap_browsing_cid, &connection->parsed_attribute_value[0], connection->parsed_attribute_value_offset);
536             connection->parsed_num_attributes++;
537             connection->parsed_attribute_value_offset = 0;
538             connection->parser_attribute_header_pos = 0;
539 
540             if (connection->parsed_num_attributes == connection->num_items){
541                 avrcp_parser_reset(connection);
542                 break;
543             }
544             break;
545         }
546         case AVRCP_PARSER_IGNORE_REST_OF_ATTRIBUTE_VALUE:
547             connection->parsed_attribute_value_offset++;
548             if (connection->parsed_attribute_value_offset < big_endian_read_16(connection->parser_attribute_header, 1) + prepended_header_size){
549                 break;
550             }
551             connection->parser_state = AVRCP_PARSER_GET_ATTRIBUTE_HEADER;
552             (*avrcp_controller_context.browsing_avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, connection->l2cap_browsing_cid, &connection->parsed_attribute_value[0], connection->parsed_attribute_value_offset);
553             connection->parsed_num_attributes++;
554             connection->parsed_attribute_value_offset = 0;
555             connection->parser_attribute_header_pos = 0;
556 
557             if (connection->parsed_num_attributes == connection->num_items){
558                 avrcp_parser_reset(connection);
559                 break;
560             }
561             break;
562         default:
563             break;
564     }
565 }
566 
567 static void avrcp_browsing_parse_and_emit_element_attrs(uint8_t * packet, uint16_t num_bytes_to_read, avrcp_browsing_connection_t * connection){
568     int i;
569     for (i=0;i<num_bytes_to_read;i++){
570         avrcp_browsing_parser_process_byte(packet[i], connection);
571     }
572 }
573 
574 static void avrcp_browsing_controller_emit_failed(btstack_packet_handler_t callback, uint16_t browsing_cid, uint8_t browsing_status, uint8_t bluetooth_status){
575     avrcp_browsing_controller_emit_done_with_uid_counter(callback, browsing_cid, 0, browsing_status, bluetooth_status);
576 }
577 
578 
579 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
580     avrcp_browsing_connection_t * browsing_connection;
581 
582     switch (packet_type) {
583         case L2CAP_DATA_PACKET:{
584             browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
585             if (!browsing_connection) break;
586             // printf("received \n");
587             // printf_hexdump(packet,size);
588             int pos = 0;
589             uint8_t transport_header = packet[pos++];
590             // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
591             browsing_connection->transaction_label = transport_header >> 4;
592             avrcp_packet_type_t avctp_packet_type = (transport_header & 0x0F) >> 2;
593             // printf("L2CAP_DATA_PACKET, packet type %d\n", avctp_packet_type);
594             switch (avctp_packet_type){
595                 case AVRCP_SINGLE_PACKET:
596                 case AVRCP_START_PACKET:
597                     // uint8_t frame_type = (transport_header & 0x03) >> 1;
598                     // uint8_t ipid = transport_header & 0x01;
599                     pos += 2;
600                     browsing_connection->num_packets = 1;
601                     if (avctp_packet_type == AVRCP_START_PACKET){
602                         browsing_connection->num_packets = packet[pos++];
603                     }
604                     if (pos + 4 > size){
605                         browsing_connection->state = AVCTP_CONNECTION_OPENED;
606                         avrcp_browsing_controller_emit_failed(avrcp_controller_context.browsing_avrcp_callback, channel, AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND, ERROR_CODE_SUCCESS);
607                         return;
608                     }
609                     browsing_connection->pdu_id = packet[pos++];
610                     // uint16_t length = big_endian_read_16(packet, pos);
611                     pos += 2;
612                     browsing_connection->browsing_status = packet[pos++];
613                     if (browsing_connection->browsing_status != AVRCP_BROWSING_ERROR_CODE_SUCCESS){
614                         browsing_connection->state = AVCTP_CONNECTION_OPENED;
615                         avrcp_browsing_controller_emit_failed(avrcp_controller_context.browsing_avrcp_callback, channel, browsing_connection->browsing_status, ERROR_CODE_SUCCESS);
616                         return;
617                     }
618                     break;
619                 default:
620                     break;
621             }
622 
623             uint32_t i;
624             switch(browsing_connection->pdu_id){
625                 case AVRCP_PDU_ID_CHANGE_PATH:
626                     // printf("AVRCP_PDU_ID_CHANGE_PATH \n");
627                     break;
628                 case AVRCP_PDU_ID_SET_ADDRESSED_PLAYER:
629                     // printf("AVRCP_PDU_ID_SET_ADDRESSED_PLAYER \n");
630                     break;
631                 case AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:{
632                     // uint32_t num_items = big_endian_read_32(packet, pos);
633                     // pos += 4;
634                     // printf("TODO: send event, uid_counter %d, num_items %d\n", browsing_connection->uid_counter, num_items);
635                     break;
636                 }
637                 case AVRCP_PDU_ID_SET_BROWSED_PLAYER:{
638                     // printf("AVRCP_PDU_ID_SET_BROWSED_PLAYER \n");
639                     browsing_connection->uid_counter =  big_endian_read_16(packet, pos);
640                     pos += 2;
641                     // uint32_t num_items = big_endian_read_32(packet, pos);
642                     pos += 4;
643                     // uint16_t charset = big_endian_read_16(packet, pos);
644                     pos += 2;
645                     uint8_t folder_depth = packet[pos++];
646 
647                     for (i = 0; i < folder_depth; i++){
648                         uint16_t folder_name_length = big_endian_read_16(packet, pos);
649                         pos += 2;
650                         // reuse packet and add data type as a header
651                         packet[pos-1] = AVRCP_BROWSING_MEDIA_ROOT_FOLDER;
652                         (*avrcp_controller_context.browsing_avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, channel, packet+pos-1, folder_name_length+1);
653                         pos += folder_name_length;
654                     }
655                     break;
656                 }
657                 case AVRCP_PDU_ID_GET_FOLDER_ITEMS:{
658                     // printf("AVRCP_PDU_ID_GET_FOLDER_ITEMS \n");
659                     switch (avctp_packet_type){
660                         case AVRCP_SINGLE_PACKET:
661                         case AVRCP_START_PACKET:
662                             avrcp_parser_reset(browsing_connection);
663                             browsing_connection->uid_counter =  big_endian_read_16(packet, pos);
664                             pos += 2;
665                             browsing_connection->num_items = big_endian_read_16(packet, pos); //num_items
666                             pos += 2;
667                             avrcp_browsing_parse_and_emit_element_attrs(packet+pos, size-pos, browsing_connection);
668                             break;
669 
670                         case AVRCP_CONTINUE_PACKET:
671                             avrcp_browsing_parse_and_emit_element_attrs(packet+pos, size-pos, browsing_connection);
672                             break;
673 
674                         case AVRCP_END_PACKET:
675                             avrcp_browsing_parse_and_emit_element_attrs(packet+pos, size-pos, browsing_connection);
676                             avrcp_parser_reset(browsing_connection);
677                             break;
678                     }
679                     break;
680                 }
681                 case AVRCP_PDU_ID_SEARCH:{
682                     browsing_connection->uid_counter =  big_endian_read_16(packet, pos);
683                     pos += 2;
684                     // uint32_t num_items = big_endian_read_32(packet, pos);
685                     // printf("TODO: send as event, search found %d items\n", num_items);
686                     break;
687                 }
688                 case AVRCP_PDU_ID_GET_ITEM_ATTRIBUTES:
689                     packet[pos-1] = AVRCP_BROWSING_MEDIA_ELEMENT_ITEM_ATTRIBUTE;
690                     (*avrcp_controller_context.browsing_avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, channel, packet+pos-1, size - pos + 1);
691                     break;
692                 default:
693                     log_info(" not parsed pdu ID 0x%02x", browsing_connection->pdu_id);
694                     break;
695             }
696 
697             switch (avctp_packet_type){
698                 case AVRCP_SINGLE_PACKET:
699                 case AVRCP_END_PACKET:
700                     // printf("reset browsing connection state to OPENED\n");
701                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
702                     avrcp_browsing_controller_emit_done_with_uid_counter(avrcp_controller_context.browsing_avrcp_callback, channel, browsing_connection->uid_counter, browsing_connection->browsing_status, ERROR_CODE_SUCCESS);
703                     break;
704                 default:
705                     break;
706             }
707             // printf(" paket done\n");
708             break;
709         }
710         case HCI_EVENT_PACKET:
711             switch (hci_event_packet_get_type(packet)){
712                 case L2CAP_EVENT_CAN_SEND_NOW:
713                     browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
714                     if (!browsing_connection) break;
715                     avrcp_browsing_controller_handle_can_send_now(browsing_connection);
716                     break;
717                 default:
718                     avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context);
719                     break;
720             }
721             break;
722 
723         default:
724             break;
725     }
726 }
727 
728 void avrcp_browsing_controller_init(void){
729     avrcp_controller_context.browsing_packet_handler = avrcp_browsing_controller_packet_handler;
730     l2cap_register_service(&avrcp_browsing_controller_packet_handler, PSM_AVCTP_BROWSING, 0xffff, LEVEL_2);
731 }
732 
733 void avrcp_browsing_controller_register_packet_handler(btstack_packet_handler_t callback){
734     if (callback == NULL){
735         log_error("avrcp_browsing_controller_register_packet_handler called with NULL callback");
736         return;
737     }
738     avrcp_controller_context.browsing_avrcp_callback = callback;
739 }
740 
741 uint8_t avrcp_browsing_controller_connect(bd_addr_t bd_addr, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){
742     return avrcp_browsing_connect(bd_addr, &avrcp_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid);
743 }
744 
745 uint8_t avrcp_browsing_controller_disconnect(uint16_t avrcp_browsing_cid){
746     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
747     if (!avrcp_connection){
748         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
749         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
750     }
751     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
752 
753     l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0);
754     return ERROR_CODE_SUCCESS;
755 }
756 
757 uint8_t avrcp_browsing_controller_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config){
758     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
759     if (!avrcp_connection){
760         log_error("avrcp_browsing_controller_decline_incoming_connection: could not find a connection.");
761         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
762     }
763     if (!avrcp_connection->browsing_connection){
764         log_error("avrcp_browsing_controller_decline_incoming_connection: no browsing connection.");
765         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
766     }
767 
768     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
769         log_error("avrcp_browsing_controller_decline_incoming_connection: browsing connection in a wrong state.");
770         return ERROR_CODE_COMMAND_DISALLOWED;
771     }
772 
773     avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
774     avrcp_connection->browsing_connection->ertm_buffer = ertm_buffer;
775     avrcp_connection->browsing_connection->ertm_buffer_size = size;
776     memcpy(&avrcp_connection->browsing_connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t));
777     l2cap_accept_ertm_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid, &avrcp_connection->browsing_connection->ertm_config, avrcp_connection->browsing_connection->ertm_buffer, avrcp_connection->browsing_connection->ertm_buffer_size);
778     return ERROR_CODE_SUCCESS;
779 }
780 
781 uint8_t avrcp_browsing_controller_decline_incoming_connection(uint16_t avrcp_browsing_cid){
782     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
783     if (!avrcp_connection){
784         log_error("avrcp_browsing_controller_decline_incoming_connection: could not find a connection.");
785         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
786     }
787     if (!avrcp_connection->browsing_connection) return ERROR_CODE_SUCCESS;
788     if (avrcp_connection->browsing_connection->state > AVCTP_CONNECTION_W4_ERTM_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED;
789 
790     l2cap_decline_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid);
791     // free connection
792     btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
793     avrcp_connection->browsing_connection = NULL;
794     return ERROR_CODE_SUCCESS;
795 }
796 
797 uint8_t avrcp_browsing_controller_get_item_attributes_for_scope(uint16_t avrcp_browsing_cid, uint8_t * uid, uint16_t uid_counter, uint32_t attr_bitmap, avrcp_browsing_scope_t scope){
798     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
799     if (!avrcp_connection){
800         log_error("avrcp_browsing_controller_get_item_attributes: could not find a connection.");
801         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
802     }
803     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
804     if (connection->state != AVCTP_CONNECTION_OPENED){
805         log_error("avrcp_browsing_controller_get_item_attributes: connection in wrong state %d, expected %d.", connection->state, AVCTP_CONNECTION_OPENED);
806         return ERROR_CODE_COMMAND_DISALLOWED;
807     }
808 
809     connection->get_item_attributes = 1;
810     connection->scope = scope;
811     memcpy(connection->folder_uid, uid, 8);
812     connection->uid_counter = uid_counter;
813     connection->attr_bitmap = attr_bitmap;
814 
815     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
816     return ERROR_CODE_SUCCESS;
817 }
818 
819 /**
820  * @brief Retrieve a listing of the contents of a folder.
821  * @param scope    0-player list, 1-virtual file system, 2-search, 3-now playing
822  * @param start_item
823  * @param end_item
824  * @param attribute_count
825  * @param attribute_list
826  **/
827 static uint8_t avrcp_browsing_controller_get_folder_items(uint16_t avrcp_browsing_cid, avrcp_browsing_scope_t scope, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){
828     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
829     if (!avrcp_connection){
830         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
831         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
832     }
833     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
834     if (connection->state != AVCTP_CONNECTION_OPENED) {
835         log_error("avrcp_browsing_controller_get_folder_items: connection in wrong state %d, expected %d.", connection->state, AVCTP_CONNECTION_OPENED);
836         return ERROR_CODE_COMMAND_DISALLOWED;
837     }
838 
839     connection->get_folder_items = 1;
840     connection->scope = scope;
841     connection->start_item = start_item;
842     connection->end_item = end_item;
843     connection->attr_bitmap = attr_bitmap;
844 
845     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
846     return ERROR_CODE_SUCCESS;
847 }
848 
849 uint8_t avrcp_browsing_controller_get_media_players(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){
850     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, AVRCP_BROWSING_MEDIA_PLAYER_LIST, start_item, end_item, attr_bitmap);
851 }
852 
853 uint8_t avrcp_browsing_controller_browse_file_system(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){
854     // return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 1, 0, 0xFFFFFFFF, attr_bitmap);
855     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, AVRCP_BROWSING_MEDIA_PLAYER_VIRTUAL_FILESYSTEM, start_item, end_item, attr_bitmap);
856 }
857 
858 uint8_t avrcp_browsing_controller_browse_media(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){
859     // return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 2, 0, 0xFFFFFFFF, 0, NULL);
860     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, AVRCP_BROWSING_SEARCH, start_item, end_item, attr_bitmap);
861 }
862 
863 uint8_t avrcp_browsing_controller_browse_now_playing_list(uint16_t avrcp_browsing_cid, uint32_t start_item, uint32_t end_item, uint32_t attr_bitmap){
864     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, AVRCP_BROWSING_NOW_PLAYING, start_item, end_item, attr_bitmap);
865 }
866 
867 
868 uint8_t avrcp_browsing_controller_set_browsed_player(uint16_t avrcp_browsing_cid, uint16_t browsed_player_id){
869     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
870     if (!avrcp_connection){
871         log_error("avrcp_browsing_controller_change_path: could not find a connection.");
872         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
873     }
874 
875     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
876     if (connection->state != AVCTP_CONNECTION_OPENED){
877         log_error("avrcp_browsing_controller_change_path: connection in wrong state.");
878         return ERROR_CODE_COMMAND_DISALLOWED;
879     }
880 
881     connection->set_browsed_player_id = 1;
882     connection->browsed_player_id = browsed_player_id;
883     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
884     return ERROR_CODE_SUCCESS;
885 }
886 
887 /**
888  * @brief Retrieve a listing of the contents of a folder.
889  * @param direction     0-folder up, 1-folder down
890  * @param folder_uid    8 bytes long
891  **/
892 uint8_t avrcp_browsing_controller_change_path(uint16_t avrcp_browsing_cid, uint8_t direction, uint8_t * folder_uid){
893     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
894     if (!avrcp_connection){
895         log_error("avrcp_browsing_controller_change_path: could not find a connection.");
896         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
897     }
898 
899     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
900 
901     if (!connection || connection->state != AVCTP_CONNECTION_OPENED){
902         log_error("avrcp_browsing_controller_change_path: connection in wrong state.");
903         return ERROR_CODE_COMMAND_DISALLOWED;
904     }
905 
906     if (!connection->browsed_player_id){
907         log_error("avrcp_browsing_controller_change_path: no browsed player set.");
908         return ERROR_CODE_COMMAND_DISALLOWED;
909     }
910     connection->change_path = 1;
911     connection->direction = direction;
912     memset(connection->folder_uid, 0, 8);
913     if (folder_uid){
914         memcpy(connection->folder_uid, folder_uid, 8);
915     }
916 
917     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
918     return ERROR_CODE_SUCCESS;
919 }
920 
921 uint8_t avrcp_browsing_controller_go_up_one_level(uint16_t avrcp_browsing_cid){
922     return avrcp_browsing_controller_change_path(avrcp_browsing_cid, 0, NULL);
923 }
924 
925 uint8_t avrcp_browsing_controller_go_down_one_level(uint16_t avrcp_browsing_cid, uint8_t * folder_uid){
926     return avrcp_browsing_controller_change_path(avrcp_browsing_cid, 1, folder_uid);
927 }
928 
929 uint8_t avrcp_browsing_controller_search(uint16_t avrcp_browsing_cid, uint16_t search_str_len, char * search_str){
930     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
931     if (!avrcp_connection){
932         log_error("avrcp_browsing_controller_change_path: could not find a connection.");
933         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
934     }
935 
936     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
937 
938     if (!connection || connection->state != AVCTP_CONNECTION_OPENED){
939         log_error("avrcp_browsing_controller_change_path: connection in wrong state.");
940         return ERROR_CODE_COMMAND_DISALLOWED;
941     }
942 
943     if (!connection->browsed_player_id){
944         log_error("avrcp_browsing_controller_change_path: no browsed player set.");
945         return ERROR_CODE_COMMAND_DISALLOWED;
946     }
947     if (!search_str || search_str_len == 0){
948         return AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND;
949     }
950 
951     connection->search = 1;
952 
953     connection->search_str_len = btstack_min(search_str_len, sizeof(connection->search_str)-1);
954     memset(connection->search_str, 0, sizeof(connection->search_str));
955     memcpy(connection->search_str, search_str, connection->search_str_len);
956     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
957     return ERROR_CODE_SUCCESS;
958 }
959 
960 uint8_t avrcp_browsing_controller_get_total_nr_items_for_scope(uint16_t avrcp_browsing_cid, avrcp_browsing_scope_t scope){
961     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
962     if (!avrcp_connection){
963         log_error("avrcp_browsing_controller_change_path: could not find a connection.");
964         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
965     }
966 
967     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
968 
969     if (!connection || connection->state != AVCTP_CONNECTION_OPENED){
970         log_error("avrcp_browsing_controller_change_path: connection in wrong state.");
971         return ERROR_CODE_COMMAND_DISALLOWED;
972     }
973 
974     if (!connection->browsed_player_id){
975         log_error("avrcp_browsing_controller_change_path: no browsed player set.");
976         return ERROR_CODE_COMMAND_DISALLOWED;
977     }
978     connection->get_total_nr_items = 1;
979     connection->get_total_nr_items_scope = scope;
980     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
981     return ERROR_CODE_SUCCESS;
982 }
983