xref: /btstack/src/classic/avrcp_browsing_controller.c (revision ed0df7b27698bfe7828431f4bb6d91845ad5295f)
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 void avrcp_handle_l2cap_data_packet_for_browsing_connection(avrcp_browsing_connection_t * connection, uint8_t *packet, uint16_t size){
244     UNUSED(size);
245 
246     int pos = 3;
247     avrcp_pdu_id_t pdu_id = packet[pos++];
248 
249     switch(pdu_id){
250         case AVRCP_PDU_ID_GET_FOLDER_ITEMS:
251             printf("AVRCP_PDU_ID_GET_FOLDER_ITEMS response \n");
252             break;
253         default:
254             break;
255     }
256     connection->state = AVCTP_CONNECTION_OPENED;
257 }
258 
259 static int avrcp_browsing_controller_send_get_folder_items_cmd(uint16_t cid, avrcp_browsing_connection_t * connection){
260     uint8_t command[100];
261     int pos = 0;
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     big_endian_store_16(command, pos, 10+connection->attribute_count);
270     pos += 2;
271 
272     command[pos++] = connection->scope;
273     big_endian_store_32(command, pos, connection->start_item);
274     pos += 4;
275     big_endian_store_32(command, pos, connection->end_item);
276     pos += 4;
277 
278     command[pos++] = connection->attribute_count;
279     if (connection->attribute_count){
280         memcpy(command+pos, connection->attribute_list, connection->attribute_count);
281         pos += connection->attribute_count;
282     }
283     return l2cap_send(cid, command, pos);
284 }
285 
286 
287 static void avrcp_browsing_controller_handle_can_send_now(avrcp_browsing_connection_t * connection){
288     switch (connection->state){
289         case AVCTP_CONNECTION_OPENED:
290             if (connection->get_folder_item){
291                 connection->state = AVCTP_W2_RECEIVE_RESPONSE;
292                 connection->get_folder_item = 0;
293                 avrcp_browsing_controller_send_get_folder_items_cmd(connection->l2cap_browsing_cid, connection);
294                 break;
295             }
296             break;
297         default:
298             return;
299     }
300 }
301 
302 static void avrcp_browsing_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
303     avrcp_browsing_connection_t * connection;
304 
305     switch (packet_type) {
306         case L2CAP_DATA_PACKET:
307             connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
308             if (!connection) break;
309             avrcp_handle_l2cap_data_packet_for_browsing_connection(connection, packet, size);
310             break;
311         case HCI_EVENT_PACKET:
312             switch (hci_event_packet_get_type(packet)){
313                 case L2CAP_EVENT_CAN_SEND_NOW:
314                     connection = get_avrcp_browsing_connection_for_l2cap_cid(channel, &avrcp_controller_context);
315                     if (!connection) break;
316                     avrcp_browsing_controller_handle_can_send_now(connection);
317                     break;
318             default:
319                 avrcp_browser_packet_handler(packet_type, channel, packet, size, &avrcp_controller_context);
320                 break;
321         }
322         default:
323             break;
324     }
325 }
326 
327 void avrcp_browsing_controller_init(void){
328     avrcp_controller_context.browsing_packet_handler = avrcp_browsing_controller_packet_handler;
329     l2cap_register_service(&avrcp_browsing_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
330 }
331 
332 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){
333     return avrcp_browsing_connect(bd_addr, &avrcp_controller_context, ertm_buffer, size, ertm_config, avrcp_browsing_cid);
334 }
335 
336 uint8_t avrcp_browsing_controller_disconnect(uint16_t avrcp_browsing_cid){
337     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
338     if (!avrcp_connection){
339         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
340         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
341     }
342     if (avrcp_connection->browsing_connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
343 
344     l2cap_disconnect(avrcp_connection->browsing_connection->l2cap_browsing_cid, 0);
345     return ERROR_CODE_SUCCESS;
346 }
347 
348 /**
349  * @brief Retrieve a listing of the contents of a folder.
350  * @param scope    0-player list, 1-virtual file system, 2-search, 3-now playing
351  * @param start_item
352  * @param end_item
353  * @param attribute_count
354  * @param attribute_list
355  **/
356 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){
357     avrcp_connection_t * avrcp_connection = get_avrcp_connection_for_browsing_cid(avrcp_browsing_cid, &avrcp_controller_context);
358     if (!avrcp_connection){
359         log_error("avrcp_browsing_controller_disconnect: could not find a connection.");
360         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
361     }
362     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
363     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
364 
365     connection->get_folder_item = 1;
366     connection->scope = scope;
367     connection->start_item = start_item;
368     connection->end_item = end_item;
369     connection->attribute_count = attribute_count;
370     connection->attribute_list = attribute_list;
371 
372     avrcp_request_can_send_now(avrcp_connection, connection->l2cap_browsing_cid);
373     return ERROR_CODE_SUCCESS;
374 }
375 
376 uint8_t avrcp_browsing_controller_get_player_list(uint16_t avrcp_browsing_cid){
377     return avrcp_browsing_controller_get_folder_items(avrcp_browsing_cid, 0, 0, 0xFFFFFFFF, 0, NULL);
378 }
379