xref: /btstack/src/classic/avrcp_browsing_controller.c (revision 954cc391b7ff237c6d9460ea14cf8b96b1fbd1e4)
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 
45 #include "btstack.h"
46 #include "classic/avrcp.h"
47 #include "classic/avrcp_browsing_controller.h"
48 
49 void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context);
50 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
51 
52 static avrcp_connection_t * get_avrcp_connection_for_browsing_cid(uint16_t browsing_cid, avrcp_context_t * context){
53     btstack_linked_list_iterator_t it;
54     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
55     while (btstack_linked_list_iterator_has_next(&it)){
56         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
57         if (connection->avrcp_browsing_cid != browsing_cid) continue;
58         return connection;
59     }
60     return NULL;
61 }
62 
63 static avrcp_connection_t * get_avrcp_connection_for_browsing_l2cap_cid(uint16_t browsing_l2cap_cid, avrcp_context_t * context){
64     btstack_linked_list_iterator_t it;
65     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
66     while (btstack_linked_list_iterator_has_next(&it)){
67         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
68         if (connection->browsing_connection &&  connection->browsing_connection->l2cap_browsing_cid != browsing_l2cap_cid) continue;
69         return connection;
70     }
71     return NULL;
72 }
73 
74 static avrcp_browsing_connection_t * get_avrcp_browsing_connection_for_l2cap_cid(uint16_t l2cap_cid, avrcp_context_t * context){
75     btstack_linked_list_iterator_t it;
76     btstack_linked_list_iterator_init(&it, (btstack_linked_list_t *)  &context->connections);
77     while (btstack_linked_list_iterator_has_next(&it)){
78         avrcp_connection_t * connection = (avrcp_connection_t *)btstack_linked_list_iterator_next(&it);
79         if (connection->browsing_connection && connection->browsing_connection->l2cap_browsing_cid != l2cap_cid) continue;
80         return connection->browsing_connection;
81     }
82     return NULL;
83 }
84 
85 static void avrcp_emit_browsing_connection_established(btstack_packet_handler_t callback, uint16_t browsing_cid, bd_addr_t addr, uint8_t status){
86     if (!callback) return;
87     uint8_t event[12];
88     int pos = 0;
89     event[pos++] = HCI_EVENT_AVRCP_META;
90     event[pos++] = sizeof(event) - 2;
91     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_ESTABLISHED;
92     event[pos++] = status;
93     reverse_bd_addr(addr,&event[pos]);
94     pos += 6;
95     little_endian_store_16(event, pos, browsing_cid);
96     pos += 2;
97     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
98 }
99 
100 static void avrcp_emit_browsing_connection_closed(btstack_packet_handler_t callback, uint16_t browsing_cid){
101     if (!callback) return;
102     uint8_t event[5];
103     int pos = 0;
104     event[pos++] = HCI_EVENT_AVRCP_META;
105     event[pos++] = sizeof(event) - 2;
106     event[pos++] = AVRCP_SUBEVENT_BROWSING_CONNECTION_RELEASED;
107     little_endian_store_16(event, pos, browsing_cid);
108     pos += 2;
109     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
110 }
111 
112 static avrcp_browsing_connection_t * avrcp_browsing_create_connection(avrcp_connection_t * avrcp_connection){
113     avrcp_browsing_connection_t * connection = btstack_memory_avrcp_browsing_connection_get();
114     memset(connection, 0, sizeof(avrcp_browsing_connection_t));
115     connection->state = AVCTP_CONNECTION_IDLE;
116     connection->transaction_label = 0xFF;
117     avrcp_connection->avrcp_browsing_cid = avrcp_get_next_cid();
118     avrcp_connection->browsing_connection = connection;
119     return connection;
120 }
121 
122 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){
123     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_bd_addr(remote_addr, context);
124 
125     if (!avrcp_connection){
126         log_error("avrcp: there is no previously established AVRCP controller connection.");
127         return ERROR_CODE_COMMAND_DISALLOWED;
128     }
129 
130     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
131     if (connection){
132         printf(" avrcp_browsing_connect connection exists\n");
133         return ERROR_CODE_SUCCESS;
134     }
135 
136     connection = avrcp_browsing_create_connection(avrcp_connection);
137     if (!connection){
138         printf("avrcp: could not allocate connection struct.");
139         return BTSTACK_MEMORY_ALLOC_FAILED;
140     }
141 
142     if (!browsing_cid) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
143 
144     *browsing_cid = avrcp_connection->avrcp_browsing_cid;
145     connection->ertm_buffer = ertm_buffer;
146     connection->ertm_buffer_size = size;
147     avrcp_connection->browsing_connection = connection;
148 
149     memcpy(&connection->ertm_config, ertm_config, sizeof(l2cap_ertm_config_t));
150 
151     return l2cap_create_ertm_channel(avrcp_browsing_controller_packet_handler, remote_addr, avrcp_connection->browsing_l2cap_psm,
152                     &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size, NULL);
153 
154 }
155 
156 void avrcp_browser_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, avrcp_context_t * context){
157     UNUSED(channel);
158     UNUSED(size);
159     bd_addr_t event_addr;
160     uint16_t local_cid;
161     uint8_t  status;
162     avrcp_browsing_connection_t * connection = NULL;
163     avrcp_connection_t * avrcp_connection = NULL;
164 
165     if (packet_type != HCI_EVENT_PACKET) return;
166 
167     switch (hci_event_packet_get_type(packet)) {
168         case HCI_EVENT_DISCONNECTION_COMPLETE:
169             avrcp_emit_browsing_connection_closed(context->avrcp_callback, 0);
170             break;
171         case L2CAP_EVENT_INCOMING_CONNECTION:
172             l2cap_event_incoming_connection_get_address(packet, event_addr);
173             local_cid = l2cap_event_incoming_connection_get_local_cid(packet);
174             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
175             if (!avrcp_connection) {
176                 log_error("No previously created AVRCP controller connections");
177                 l2cap_decline_connection(local_cid);
178                 break;
179             }
180             connection = avrcp_browsing_create_connection(avrcp_connection);
181             avrcp_connection->browsing_connection = connection;
182             connection->l2cap_browsing_cid = local_cid;
183             connection->state = AVCTP_CONNECTION_W4_L2CAP_CONNECTED;
184             log_info("L2CAP_EVENT_INCOMING_CONNECTION browsing_cid 0x%02x, l2cap_signaling_cid 0x%02x", avrcp_connection->avrcp_browsing_cid, connection->l2cap_browsing_cid);
185             // l2cap_accept_connection(local_cid);
186             printf("L2CAP Accepting incoming connection request in ERTM\n");
187             l2cap_accept_ertm_connection(local_cid, &connection->ertm_config, connection->ertm_buffer, connection->ertm_buffer_size);
188             break;
189 
190         case L2CAP_EVENT_CHANNEL_OPENED:
191             l2cap_event_channel_opened_get_address(packet, event_addr);
192             status = l2cap_event_channel_opened_get_status(packet);
193             local_cid = l2cap_event_channel_opened_get_local_cid(packet);
194 
195             avrcp_connection = get_avrcp_connection_for_bd_addr(event_addr, context);
196             if (!avrcp_connection){
197                 log_error("Failed to find AVRCP connection for bd_addr %s", bd_addr_to_str(event_addr));
198                 avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, L2CAP_LOCAL_CID_DOES_NOT_EXIST);
199                 l2cap_disconnect(local_cid, 0); // reason isn't used
200                 break;
201             }
202 
203             connection = avrcp_connection->browsing_connection;
204             if (!connection){
205                 log_error("Failed to alloc AVRCP connection structure");
206                 avrcp_emit_browsing_connection_established(context->avrcp_callback, local_cid, event_addr, BTSTACK_MEMORY_ALLOC_FAILED);
207                 l2cap_disconnect(local_cid, 0); // reason isn't used
208                 break;
209             }
210 
211             if (status != ERROR_CODE_SUCCESS){
212                 log_info("L2CAP connection to connection %s failed. status code 0x%02x", bd_addr_to_str(event_addr), status);
213                 avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, status);
214                 btstack_memory_avrcp_browsing_connection_free(connection);
215                 avrcp_connection->browsing_connection = NULL;
216                 break;
217             }
218             connection->l2cap_browsing_cid = local_cid;
219 
220             log_info("L2CAP_EVENT_CHANNEL_OPENED browsing cid 0x%02x, l2cap cid 0x%02x", avrcp_connection->avrcp_browsing_cid, connection->l2cap_browsing_cid);
221             connection->state = AVCTP_CONNECTION_OPENED;
222             avrcp_emit_browsing_connection_established(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid, event_addr, ERROR_CODE_SUCCESS);
223             break;
224 
225         case L2CAP_EVENT_CHANNEL_CLOSED:
226             // data: event (8), len(8), channel (16)
227             local_cid = l2cap_event_channel_closed_get_local_cid(packet);
228             avrcp_connection = get_avrcp_connection_for_browsing_l2cap_cid(local_cid, context);
229 
230             if (avrcp_connection && avrcp_connection->browsing_connection){
231                 avrcp_emit_browsing_connection_closed(context->avrcp_callback, avrcp_connection->avrcp_browsing_cid);
232                 // free connection
233                 btstack_memory_avrcp_browsing_connection_free(avrcp_connection->browsing_connection);
234                 avrcp_connection->browsing_connection = NULL;
235                 break;
236             }
237             break;
238         default:
239             break;
240     }
241 }
242 
243 static int avrcp_browsing_controller_send_get_folder_items_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
244     uint8_t command[100];
245     int pos = 0;
246     // transport header
247     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
248     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_COMMAND_FRAME << 1) | 0;
249     // Profile IDentifier (PID)
250     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
251     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
252     command[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS;
253     big_endian_store_16(command, pos, 10+connection->attribute_count);
254     pos += 2;
255 
256     command[pos++] = connection->scope;
257     big_endian_store_32(command, pos, connection->start_item);
258     pos += 4;
259     big_endian_store_32(command, pos, connection->end_item);
260     pos += 4;
261 
262     command[pos++] = connection->attribute_count;
263     if (connection->attribute_count){
264         memcpy(command+pos, connection->attribute_list, connection->attribute_count);
265         pos += connection->attribute_count;
266     }
267     return l2cap_send(cid, command, pos);
268 }
269 
270 
271 static void avrcp_browsing_controller_handle_can_send_now(avrcp_browsing_connection_t * connection){
272     switch (connection->state){
273         case AVCTP_CONNECTION_OPENED:
274             if (connection->get_folder_item){
275                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
276                 connection->get_folder_item = 0;
277                 avrcp_browsing_controller_send_get_folder_items_cmd(connection->l2cap_browsing_cid, connection);
278                 break;
279             }
280             break;
281         default:
282             return;
283     }
284 }
285 
286 static void avrcp_browsing_controller_emit_done(btstack_packet_handler_t callback, uint16_t browsing_cid, uint8_t browsing_status, uint8_t bluetooth_status){
287     if (!callback) return;
288     uint8_t event[6];
289     int pos = 0;
290     event[pos++] = HCI_EVENT_AVRCP_META;
291     event[pos++] = sizeof(event) - 2;
292     event[pos++] = AVRCP_SUBEVENT_BROWSING_MEDIA_ITEM_DONE;
293     little_endian_store_16(event, pos, browsing_cid);
294     pos += 2;
295     event[pos++] = browsing_status;
296     event[pos++] = bluetooth_status;
297     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
298 }
299 
300 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
301     avrcp_browsing_connection_t * browsing_connection;
302 
303     switch (packet_type) {
304         case L2CAP_DATA_PACKET:{
305             browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
306             if (!browsing_connection) break;
307             browsing_connection->state = AVCTP_CONNECTION_OPENED;
308 
309             int pos = 3;
310             if (size < pos + 4){
311                 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND, ERROR_CODE_SUCCESS);
312                 break;
313             }
314 
315             avrcp_pdu_id_t pdu_id = packet[pos++];
316             uint16_t length = big_endian_read_16(packet, pos);
317             pos += 2;
318             uint8_t browsing_status = packet[pos++];
319 
320             if (browsing_status != AVRCP_BROWSING_ERROR_CODE_SUCCESS){
321                 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, browsing_status, ERROR_CODE_SUCCESS);
322                 break;
323             }
324             if (size + pos < length){
325                 avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, AVRCP_BROWSING_ERROR_CODE_INVALID_COMMAND, ERROR_CODE_SUCCESS);
326                 break;
327             }
328 
329             // uint16_t uid_counter = big_endian_read_16(packet, pos);
330             pos += 2;
331             uint16_t num_items = big_endian_read_16(packet, pos);
332             pos += 2;
333             int i;
334             switch(pdu_id){
335                 case AVRCP_PDU_ID_GET_FOLDER_ITEMS:
336                     for (i = 0; i < num_items; i++){
337                         uint16_t browsable_item_length = big_endian_read_16(packet, pos+1);
338                         (*avrcp_controller_context.avrcp_callback)(AVRCP_BROWSING_DATA_PACKET, channel, packet+pos, browsable_item_length);
339                         pos += 3 + browsable_item_length;
340                     }
341                     avrcp_browsing_controller_emit_done(avrcp_controller_context.avrcp_callback, channel, browsing_status, ERROR_CODE_SUCCESS);
342                     break;
343                 default:
344                     break;
345             }
346             break;
347         }
348         case HCI_EVENT_PACKET:
349             switch (hci_event_packet_get_type(packet)){
350                 case L2CAP_EVENT_CAN_SEND_NOW:
351                     browsing_connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
352                     if (!browsing_connection) break;
353                     avrcp_browsing_controller_handle_can_send_now(browsing_connection);
354                     break;
355             default:
356                 avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context);
357                 break;
358         }
359         default:
360             break;
361     }
362 }
363 
364 void avrcp_browsing_controller_init(void){
365     avrcp_controller_context.browsing_packet_handler = avrcp_browsing_controller_packet_handler;
366     l2cap_register_service(&avrcp_browsing_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
367 }
368 
369 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){
370     return avrcp_browsing_connect(bd_addr, &avrcp_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid);
371 }
372 
373 uint8_t avrcp_browsing_controller_disconnect(uint16_t avrcp_browsing_cid){
374     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
375     if (!avrcp_connection){
376         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
377         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
378     }
379     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
380 
381     l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0);
382     return ERROR_CODE_SUCCESS;
383 }
384 
385 /**
386  * @brief Retrieve a listing of the contents of a folder.
387  * @param scope    0-player list, 1-virtual file system, 2-search, 3-now playing
388  * @param start_item
389  * @param end_item
390  * @param attribute_count
391  * @param attribute_list
392  **/
393 uint8_t avrcp_browsing_controller_get_folder_items(uint16_t avrcp_browsing_cid, uint8_t scope, uint32_t start_item, uint32_t end_item, uint8_t attribute_count, uint8_t * attribute_list){
394     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
395     if (!avrcp_connection){
396         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
397         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
398     }
399     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
400     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
401 
402     connection->get_folder_item = 1;
403     connection->scope = scope;
404     connection->start_item = start_item;
405     connection->end_item = end_item;
406     connection->attribute_count = attribute_count;
407     connection->attribute_list = attribute_list;
408 
409     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
410     return ERROR_CODE_SUCCESS;
411 }
412 
413 uint8_t avrcp_browsing_controller_get_player_list(uint16_t avrcp_browsing_cid){
414     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 0x01, 0, 0xFFFFFFFF, 0, NULL);
415 }
416