xref: /btstack/src/classic/avrcp_browsing_target.c (revision cf26c8fbbf161f7e5ee9327de39264752c730a34)
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 <string.h>
42 #include <inttypes.h>
43 #include "classic/avrcp.h"
44 #include "classic/avrcp_browsing.h"
45 #include "classic/avrcp_browsing_target.h"
46 #include "classic/avrcp_target.h"
47 #include "classic/avrcp_controller.h"
48 
49 #include "bluetooth_sdp.h"
50 #include "btstack_debug.h"
51 #include "btstack_event.h"
52 
53 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
54 
55 static int avrcp_browsing_target_handle_can_send_now(avrcp_browsing_connection_t * connection){
56     int pos = 0;
57 
58     // l2cap_reserve_packet_buffer();
59     // uint8_t * packet = l2cap_get_outgoing_buffer();
60     uint8_t packet[300];
61     connection->packet_type = AVRCP_SINGLE_PACKET;
62 
63     packet[pos++] = (connection->transaction_label << 4) | (connection->packet_type << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
64     // Profile IDentifier (PID)
65     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
66     packet[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
67     (void)memcpy(packet + pos, connection->cmd_operands,
68                  connection->cmd_operands_length);
69 
70     pos += connection->cmd_operands_length;
71     connection->wait_to_send = false;
72     // return l2cap_send_prepared(connection->l2cap_browsing_cid, pos);
73     return l2cap_send(connection->l2cap_browsing_cid, packet, pos);
74 }
75 
76 
77 static uint8_t avrcp_browsing_target_response_general_reject(avrcp_browsing_connection_t * connection, avrcp_status_code_t status){
78     // AVRCP_CTYPE_RESPONSE_REJECTED
79     int pos = 0;
80     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GENERAL_REJECT;
81     // connection->cmd_operands[pos++] = 0;
82     // param length
83     big_endian_store_16(connection->cmd_operands, pos, 1);
84     pos += 2;
85     connection->cmd_operands[pos++] = status;
86     connection->cmd_operands_length = 4;
87     connection->state = AVCTP_W2_SEND_RESPONSE;
88     avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid);
89     return ERROR_CODE_SUCCESS;
90 }
91 
92 static void avrcp_browsing_target_emit_get_folder_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){
93     btstack_assert(callback != NULL);
94 
95     uint8_t event[10];
96     int pos = 0;
97     event[pos++] = HCI_EVENT_AVRCP_META;
98     event[pos++] = sizeof(event) - 2;
99     event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_FOLDER_ITEMS;
100     little_endian_store_16(event, pos, browsing_cid);
101     pos += 2;
102     event[pos++] = connection->scope;
103     big_endian_store_32(event, pos, connection->attr_bitmap);
104     pos += 4;
105     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
106 }
107 
108 static void avrcp_browsing_target_emit_get_total_num_items(btstack_packet_handler_t callback, uint16_t browsing_cid, avrcp_browsing_connection_t * connection){
109     btstack_assert(callback != NULL);
110 
111     uint8_t event[10];
112     int pos = 0;
113     event[pos++] = HCI_EVENT_AVRCP_META;
114     event[pos++] = sizeof(event) - 2;
115     event[pos++] = AVRCP_SUBEVENT_BROWSING_GET_TOTAL_NUM_ITEMS;
116     little_endian_store_16(event, pos, browsing_cid);
117     pos += 2;
118     event[pos++] = connection->scope;
119     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
120 }
121 
122 
123 static void avrcp_browsing_target_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
124     UNUSED(size);
125     avrcp_browsing_connection_t * browsing_connection;
126 
127     switch (packet_type) {
128         case L2CAP_DATA_PACKET:{
129             browsing_connection = avrcp_get_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel);
130             if (!browsing_connection) break;
131             int pos = 0;
132             uint8_t transport_header = packet[pos++];
133             // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
134             browsing_connection->transaction_label = transport_header >> 4;
135             avrcp_packet_type_t avctp_packet_type = (transport_header & 0x0F) >> 2;
136             switch (avctp_packet_type){
137                 case AVRCP_SINGLE_PACKET:
138                 case AVRCP_START_PACKET:
139                     browsing_connection->subunit_type = packet[pos++] >> 2;
140                     browsing_connection->subunit_id = 0;
141                     browsing_connection->command_opcode = packet[pos++];
142                     browsing_connection->num_packets = 1;
143                     if (avctp_packet_type == AVRCP_START_PACKET){
144                         browsing_connection->num_packets = packet[pos++];
145                     }
146                     browsing_connection->pdu_id = packet[pos++];
147                     break;
148                 default:
149                     break;
150             }
151             switch (avctp_packet_type){
152                 case AVRCP_SINGLE_PACKET:
153                 case AVRCP_END_PACKET:
154                     switch(browsing_connection->pdu_id){
155                         case AVRCP_PDU_ID_GET_FOLDER_ITEMS:
156                             browsing_connection->scope = packet[pos++];
157                             browsing_connection->start_item = big_endian_read_32(packet, pos);
158                             pos += 4;
159                             browsing_connection->end_item = big_endian_read_32(packet, pos);
160                             pos += 4;
161                             uint8_t attr_count = packet[pos++];
162 
163                             while (attr_count){
164                                 uint32_t attr_id = big_endian_read_32(packet, pos);
165                                 pos += 4;
166                                 browsing_connection->attr_bitmap |= (1 << attr_id);
167                                 attr_count--;
168                             }
169                             avrcp_browsing_target_emit_get_folder_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection);
170                             break;
171                         case AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS:{
172                             // send total num items
173                             browsing_connection->scope = packet[pos++];
174                             avrcp_browsing_target_emit_get_total_num_items(avrcp_target_context.browsing_avrcp_callback, channel, browsing_connection);
175                             break;
176                         }
177                         default:
178                             avrcp_browsing_target_response_general_reject(browsing_connection, AVRCP_STATUS_INVALID_COMMAND);
179                             log_info("not parsed pdu ID 0x%02x", browsing_connection->pdu_id);
180                             break;
181                     }
182                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
183                     break;
184                 default:
185                     break;
186             }
187             break;
188         }
189 
190         case HCI_EVENT_PACKET:
191             switch (hci_event_packet_get_type(packet)){
192                 case L2CAP_EVENT_CAN_SEND_NOW:
193                     browsing_connection = avrcp_get_browsing_connection_for_l2cap_cid_for_role(AVRCP_TARGET, channel);
194                     if (browsing_connection->state != AVCTP_W2_SEND_RESPONSE) return;
195                     browsing_connection->state = AVCTP_CONNECTION_OPENED;
196                     avrcp_browsing_target_handle_can_send_now(browsing_connection);
197                     break;
198                 default:
199                     break;
200             }
201             break;
202 
203         default:
204             break;
205     }
206 }
207 
208 void avrcp_browsing_target_init(void){
209     avrcp_target_context.browsing_packet_handler = avrcp_browsing_target_packet_handler;
210     avrcp_browsing_register_target_packet_handler(avrcp_browsing_target_packet_handler);
211 }
212 
213 void avrcp_browsing_target_deinit(void){
214     avrcp_controller_context.browsing_packet_handler = NULL;
215 }
216 
217 void avrcp_browsing_target_register_packet_handler(btstack_packet_handler_t callback){
218     btstack_assert(callback != NULL);
219     avrcp_target_context.browsing_avrcp_callback = callback;
220 }
221 
222 uint8_t avrcp_browsing_target_send_get_folder_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint8_t * attr_list, uint16_t attr_list_size){
223     avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
224     if (!avrcp_connection){
225         log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid);
226         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
227     }
228 
229     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
230     if (!connection){
231         log_info("Could not find a browsing connection.");
232         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
233     }
234     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
235 
236     // TODO: handle response to SetAddressedPlayer
237 
238     uint16_t pos = 0;
239     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_FOLDER_ITEMS;
240     big_endian_store_16(connection->cmd_operands, pos, attr_list_size);
241     pos += 2;
242 
243     connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS;
244     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
245     pos += 2;
246 
247     // TODO: fragmentation
248     if (attr_list_size >  sizeof(connection->cmd_operands)){
249         connection->attr_list = attr_list;
250         connection->attr_list_size = attr_list_size;
251         log_info(" todo: list too big, invoke fragmentation");
252         return 1;
253     }
254     (void)memcpy(&connection->cmd_operands[pos], attr_list, attr_list_size);
255     pos += attr_list_size;
256     connection->cmd_operands_length = pos;
257 
258     connection->state = AVCTP_W2_SEND_RESPONSE;
259     avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid);
260     return ERROR_CODE_SUCCESS;
261 }
262 
263 
264 uint8_t avrcp_browsing_target_send_get_total_num_items_response(uint16_t avrcp_browsing_cid, uint16_t uid_counter, uint32_t total_num_items){
265     avrcp_connection_t * avrcp_connection = avrcp_get_connection_for_browsing_cid_for_role(AVRCP_TARGET, avrcp_browsing_cid);
266     if (!avrcp_connection){
267         log_error("Could not find an AVRCP Target connection for browsing_cid 0x%02x.", avrcp_browsing_cid);
268         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
269     }
270 
271     avrcp_browsing_connection_t * connection = avrcp_connection->browsing_connection;
272     if (!connection){
273         log_info("Could not find a browsing connection.");
274         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
275     }
276     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
277 
278     if (connection->state != AVCTP_CONNECTION_OPENED) {
279         log_info("Browsing connection wrong state.");
280         return ERROR_CODE_COMMAND_DISALLOWED;
281     }
282 
283     int pos = 0;
284     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS;
285     big_endian_store_16(connection->cmd_operands, pos, 7);
286     pos += 2;
287     connection->cmd_operands[pos++] = AVRCP_STATUS_SUCCESS;
288     big_endian_store_16(connection->cmd_operands, pos, uid_counter);
289     pos += 2;
290     big_endian_store_32(connection->cmd_operands, pos, total_num_items);
291     pos += 4;
292     connection->cmd_operands_length = pos;
293 
294     connection->state = AVCTP_W2_SEND_RESPONSE;
295     avrcp_browsing_request_can_send_now(connection, connection->l2cap_browsing_cid);
296     return ERROR_CODE_SUCCESS;
297 }
298 
299