xref: /btstack/src/classic/avrcp_browsing_target.c (revision b99ca858cd8c5686b3847d38bf8caa6361ca629c)
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_target.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_target.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_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
53 
54 static void avrcp_browsing_target_request_can_send_now(avrcp_browsing_connection_t * connection, uint16_t l2cap_cid){
55     connection->wait_to_send = 1;
56     l2cap_request_can_send_now_event(l2cap_cid);
57 }
58 
59 static int avrcp_browsing_target_handle_can_send_now(avrcp_browsing_connection_t * connection){
60     int pos = 0;
61     // printf("avrcp_browsing_target_handle_can_send_now, cmd_operands_length %d\n", connection->cmd_operands_length);
62     // printf_hexdump(connection->cmd_operands, connection->cmd_operands_length);
63 
64     // l2cap_reserve_packet_buffer();
65     // uint8_t * packet = l2cap_get_outgoing_buffer();
66     uint8_t packet[300];
67     connection->packet_type = AVRCP_SINGLE_PACKET;
68 
69     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
70     // Profile IDentifier (PID)
71     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
72     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
73     (void)memcpy(packet + pos, connection->cmd_operands,
74                  connection->cmd_operands_length);
75 
76     pos += connection->cmd_operands_length;
77     connection->wait_to_send = 0;
78     // return l2cap_send_prepared(connection->l2cap_browsing_cid, pos);
79     return l2cap_send(connection->l2cap_browsing_cid, packet, pos);
80 }
81 
82 
83 static uint8_t avrcp_browsing_target_response_general_reject(avrcp_browsing_connection_t * connection, avrcp_status_code_t status){
84     // AVRCP_CTYPE_RESPONSE_REJECTED
85     int pos = 0;
86     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT;
87     // connection->cmd_operands[pos++] = 0;
88     // param length
89     big_endian_store_16(connection->cmd_operands, pos, 1);
90     pos += 2;
91     connection->cmd_operands[pos++] = status;
92     connection->cmd_operands_length = 4;
93     connection->state = AVCTP_W2_SEND_RESPONSE;
94     avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid);
95     return ERROR_CODE_SUCCESS;
96 }
97 
98 static void avrcp_browsing_target_emit_get_folder_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){
99     btstack_assert(callback != NULL);
100 
101     uint8_t event[10];
102     int pos = 0;
103     event[pos++] = HCI_EVENT_AVRCP_META;
104     event[pos++] = sizeof(event) - 2;
105     event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_FOLDER_ITEMS;
106     little_endian_store_16(event, pos, browsing_cid);
107     pos += 2;
108     event[pos++] = connection->scope;
109     big_endian_store_32(event, pos, connection->attr_bitmap);
110     pos += 4;
111     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
112 }
113 
114 static void avrcp_browsing_target_emit_get_total_num_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){
115     btstack_assert(callback != NULL);
116 
117     uint8_t event[10];
118     int pos = 0;
119     event[pos++] = HCI_EVENT_AVRCP_META;
120     event[pos++] = sizeof(event) - 2;
121     event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_TOTAL_NUM_ITEMS;
122     little_endian_store_16(event, pos, browsing_cid);
123     pos += 2;
124     event[pos++] = connection->scope;
125     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
126 }
127 
128 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
129     btstack_assert(callback != NULL);
130 
131     uint8_t event[12];
132     int pos = 0;
133     event[pos++] = HCI_EVENT_AVRCP_META;
134     event[pos++] = sizeof(event) - 2;
135     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
136     event[pos++] = status;
137     reverse_bd_addr(addr,&event[pos]);
138     pos += 6;
139     little_endian_store_16(event, pos, browsing_cid);
140     pos += 2;
141     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
142 }
143 
144 static void avrcp_emit_incoming_browsing_connection(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr){
145     btstack_assert(callback != NULL);
146 
147     uint8_t event[11];
148     int pos = 0;
149     event[pos++] = HCI_EVENT_AVRCP_META;
150     event[pos++] = sizeof(event) - 2;
151     event[pos++] = AVRCP_SUBEVENT_INCOMING_BROWSING_CONNECTION;
152     reverse_bd_addr(addr,&event[pos]);
153     pos += 6;
154     little_endian_store_16(event, pos, browsing_cid);
155     pos += 2;
156     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
157 }
158 
159 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){
160     btstack_assert(callback != NULL);
161 
162     uint8_t event[5];
163     int pos = 0;
164     event[pos++] = HCI_EVENT_AVRCP_META;
165     event[pos++] = sizeof(event) - 2;
166     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED;
167     little_endian_store_16(event, pos, browsing_cid);
168     pos += 2;
169     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
170 }
171 
172 static void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){
173     UNUSED(channel);
174     UNUSED(size);
175     bd_addr_t event_addr;
176     uint16_t local_cid;
177     uint8_t  status;
178     avrcp_browsing_connection_t * browsing_connection = NULL;
179     avrcp_connection_t * avrcp_connection = NULL;
180 
181     if (packet_type != HCI_EVENT_PACKET) return;
182 
183     switch (hci_event_packet_get_type(packet)) {
184         case HCI_EVENT_DISCONNECTION_COMPLETE:
185             avrcp_emit_browsing_connection_closed(context->browsing_avrcp_callback, 0);
186             break;
187         case L2CAP_EVENT_INCOMING_CONNECTION:
188             l2cap_event_incoming_connection_get_address(packet, event_addr);
189             local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
190             avrcp_connection = get_avrcp_connection_for_bd_addr_for_role(context->role, event_addr);
191             if (!avrcp_connection) {
192                 log_error("No previously created AVRCP controller connections");
193                 l2cap_decline_connection(local_cid);
194                 break;
195             }
196             browsing_connection = avrcp_browsing_create_connection(avrcp_connection);
197             browsing_connection->l2cap_browsing_cid = local_cid;
198             browsing_connection->state = AVCTP_CONNECTION_W4_ERTM_CONFIGURATION;
199             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);
200             avrcp_emit_incoming_browsing_connection(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr);
201             break;
202 
203         case L2CAP_EVENT_CHANNEL_OPENED:
204             l2cap_event_channel_opened_get_address(packet, event_addr);
205             status = l2cap_event_channel_opened_get_status(packet);
206             local_cid = l2cap_event_channel_opened_get_local_cid(packet);
207 
208             avrcp_connection = get_avrcp_connection_for_bd_addr_for_role(context->role, event_addr);
209             if (!avrcp_connection){
210                 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr));
211                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST);
212                 l2cap_disconnect(local_cid, 0); // reason isn't used
213                 break;
214             }
215 
216             browsing_connection = avrcp_connection->browsing_connection;
217             if (status != ERROR_CODE_SUCCESS){
218                 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
219                 avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status);
220                 btstack_memory_avrcp_browsing_connection_free(browsing_connection);
221                 avrcp_connection->browsing_connection = NULL;
222                 break;
223             }
224             if (browsing_connection->state != AVCTP_CONNECTION_W4_L2CAP_CONNECTED) break;
225 
226             browsing_connection->l2cap_browsing_cid = local_cid;
227 
228             log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, browsing_connection->l2cap_browsing_cid);
229             browsing_connection->state = AVCTP_CONNECTION_OPENED;
230             avrcp_emit_browsing_connection_established(context->browsing_avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS);
231             break;
232 
233         case L2CAP_EVENT_CHANNEL_CLOSED:
234             local_cid = l2cap_event_channel_closed_get_local_cid(packet);
235             avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid_for_role(context->role, local_cid);
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 
251 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
252     avrcp_browsing_connection_t * browsing_connection;
253 
254     switch (packet_type) {
255         case L2CAP_DATA_PACKET:{
256             browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel);
257             if (!browsing_connection) break;
258             int pos = 0;
259             uint8_t transport_header = packet[pos++];
260             // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
261             browsing_connection->transaction_label = transport_header >> 4;
262             avrcp_packet_type_t avctp_packet_type = (transport_header & 0x0F) >> 2;
263             switch (avctp_packet_type){
264                 case AVRCP_SINGLE_PACKET:
265                 case AVRCP_START_PACKET:
266                     browsing_connection->subunit_type = packet[pos++] >> 2;
267                     browsing_connection->subunit_id = 0;
268                     browsing_connection->command_opcode = packet[pos++];
269                     browsing_connection->num_packets = 1;
270                     if (avctp_packet_type == AVRCP_START_PACKET){
271                         browsing_connection->num_packets = packet[pos++];
272                     }
273                     browsing_connection->pdu_id = packet[pos++];
274                     break;
275                 default:
276                     break;
277             }
278             switch (avctp_packet_type){
279                 case AVRCP_SINGLE_PACKET:
280                 case AVRCP_END_PACKET:
281                     switch(browsing_connection->pdu_id){
282                         case AVRCP_PDU_ID_GET_FOLDER_ITEMS:
283                             printf("\n");
284                             browsing_connection->scope = packet[pos++];
285                             browsing_connection->start_item = big_endian_read_32(packet, pos);
286                             pos += 4;
287                             browsing_connection->end_item = big_endian_read_32(packet, pos);
288                             pos += 4;
289                             uint8_t attr_count = packet[pos++];
290 
291                             while (attr_count){
292                                 uint32_t attr_id = big_endian_read_32(packet, pos);
293                                 pos += 4;
294                                 browsing_connection->attr_bitmap |= (1 << attr_id);
295                                 attr_count--;
296                             }
297                             avrcp_browsing_target_emit_get_folder_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection);
298                             break;
299                         case AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:{
300                             // send total num items
301                             browsing_connection->scope = packet[pos++];
302                             avrcp_browsing_target_emit_get_total_num_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection);
303                             break;
304                         }
305                         default:
306                             avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND);
307                             log_info(" not parsed pdu ID 0x%02x", browsing_connection->pdu_id);
308                             break;
309                     }
310                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
311                     break;
312                 default:
313                     break;
314             }
315             break;
316         }
317 
318         case HCI_EVENT_PACKET:
319             switch (hci_event_packet_get_type(packet)){
320                 case L2CAP_EVENT_CAN_SEND_NOW:
321                     browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel);
322                     if (!browsing_connection) break;
323                     if (browsing_connection->state != AVCTP_W2_SEND_RESPONSE) return;
324                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
325                     avrcp_browsing_target_handle_can_send_now(browsing_connection);
326                     break;
327                 default:
328                     avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
329                     break;
330             }
331             break;
332 
333         default:
334             break;
335     }
336 }
337 
338 void avrcp_browsing_target_init(void){
339     avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler;
340     l2cap_register_service(&avrcp_browsing_target_packet_handler, PSM_AVCTP_BROWSING, 0xffff, gap_get_security_level());
341 }
342 
343 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){
344     btstack_assert(callback != NULL);
345     avrcp_target_context.browsing_avrcp_callback = callback;
346 }
347 
348 uint8_t avrcp_browsing_target_connect(bd_addr_t bd_addr, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config, uint16_t * avrcp_browsing_cid){
349     return avrcp_browsing_connect(bd_addr, AVRCP_TARGET, avrcp_browsing_target_packet_handler, ertm_buffer, size, ertm_config, avrcp_browsing_cid);
350 }
351 
352 uint8_t avrcp_browsing_target_disconnect(uint16_t avrcp_browsing_cid){
353     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
354     if (!avrcp_connection){
355         log_error("avrcp_browsing_target_disconnect: could not find a connection.");
356         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
357     }
358     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
359 
360     l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0);
361     return ERROR_CODE_SUCCESS;
362 }
363 
364 uint8_t avrcp_browsing_target_configure_incoming_connection(uint16_t avrcp_browsing_cid, uint8_t * ertm_buffer, uint32_t size, l2cap_ertm_config_t * ertm_config){
365     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
366     if (!avrcp_connection){
367         log_error("avrcp_browsing_decline_incoming_connection: could not find a connection.");
368         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
369     }
370     if (!avrcp_connection->browsing_connection){
371         log_error("avrcp_browsing_decline_incoming_connection: no browsing connection.");
372         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
373     }
374 
375     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_W4_ERTM_CONFIGURATION){
376         log_error("avrcp_browsing_decline_incoming_connection: browsing connection in a wrong state.");
377         return ERROR_CODE_COMMAND_DISALLOWED;
378     }
379 
380     avrcp_connection->browsing_connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
381     avrcp_connection->browsing_connection->ertm_buffer = ertm_buffer;
382     avrcp_connection->browsing_connection->ertm_buffer_size = size;
383     (void)memcpy(&avrcp_connection->browsing_connection->ertm_config,
384                  ertm_config, sizeof(l2cap_ertm_config_t));
385     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);
386     return ERROR_CODE_SUCCESS;
387 }
388 
389 uint8_t avrcp_browsing_target_decline_incoming_connection(uint16_t avrcp_browsing_cid){
390     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
391     if (!avrcp_connection){
392         log_error("avrcp_browsing_decline_incoming_connection: could not find a connection.");
393         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
394     }
395     if (!avrcp_connection->browsing_connection) return ERROR_CODE_SUCCESS;
396     if (avrcp_connection->browsing_connection->state > AVCTP_CONNECTION_W4_ERTM_CONFIGURATION) return ERROR_CODE_COMMAND_DISALLOWED;
397 
398     l2cap_decline_connection(avrcp_connection->browsing_connection->l2cap_browsing_cid);
399     // free connection
400     btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
401     avrcp_connection->browsing_connection = NULL;
402     return ERROR_CODE_SUCCESS;
403 }
404 
405 uint8_t avrcp_subevent_browsing_get_folder_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint8_t * attr_list, uint16_t attr_list_size){
406     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
407     if (!avrcp_connection){
408         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
409         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
410     }
411 
412     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
413     if (!connection){
414         log_info("avrcp_subevent_browsing_get_folder_items_response: could not find a connection.");
415         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
416     }
417     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
418 
419     if (connection->state != AVCTP_CONNECTION_OPENED) {
420         log_info("avrcp_subevent_browsing_get_folder_items_response: wrong state.");
421         return ERROR_CODE_COMMAND_DISALLOWED;
422     }
423     int pos = 0;
424 
425     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS;
426     big_endian_store_16(connection->cmd_operands, pos, attr_list_size);
427     pos += 2;
428 
429     connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS;
430     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
431     pos += 2;
432 
433     // TODO: fragmentation
434     if (attr_list_size >  sizeof(connection->cmd_operands)){
435         connection->attr_list = attr_list;
436         connection->attr_list_size = attr_list_size;
437         log_info(" todo: list too big, invoke fragmentation");
438         return 1;
439     }
440     (void)memcpy(&connection->cmd_operands[pos], attr_list, attr_list_size);
441     pos += attr_list_size;
442     connection->cmd_operands_length = pos;
443     // printf_hexdump(connection->cmd_operands, connection->cmd_operands_length);
444 
445     connection->state = AVCTP_W2_SEND_RESPONSE;
446     avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid);
447     return ERROR_CODE_SUCCESS;
448 }
449 
450 
451 uint8_t avrcp_subevent_browsing_get_total_num_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint32_t total_num_items){
452     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
453     if (!avrcp_connection){
454         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
455         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
456     }
457 
458     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
459     if (!connection){
460         log_info("avrcp_subevent_browsing_get_folder_items_response: could not find a connection.");
461         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
462     }
463     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
464 
465     if (connection->state != AVCTP_CONNECTION_OPENED) {
466         log_info("avrcp_subevent_browsing_get_folder_items_response: wrong state.");
467         return ERROR_CODE_COMMAND_DISALLOWED;
468     }
469 
470     int pos = 0;
471     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS;
472     big_endian_store_16(connection->cmd_operands, pos, 7);
473     pos += 2;
474     connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS;
475     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
476     pos += 2;
477     big_endian_store_32(connection->cmd_operands, pos, total_num_items);
478     pos += 4;
479     connection->cmd_operands_length = pos;
480 
481     connection->state = AVCTP_W2_SEND_RESPONSE;
482     avrcp_browsing_target_request_can_send_now(connection, connection->l2cap_browsing_cid);
483     return ERROR_CODE_SUCCESS;
484 }
485 
486