xref: /btstack/src/classic/avrcp_target.c (revision c045af99518e2a8894afc5eb9ef7d888017cd3e5)
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_target.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 
48 static avrcp_context_t avrcp_target_context;
49 
50 void avrcp_target_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint8_t browsing, uint16_t supported_features, const char * service_name, const char * service_provider_name){
51     avrcp_create_sdp_record(0, service, service_record_handle, browsing, supported_features, service_name, service_provider_name);
52 }
53 
54 static void avrcp_target_emit_respond_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subeventID){
55     if (!callback) return;
56     uint8_t event[5];
57     int pos = 0;
58     event[pos++] = HCI_EVENT_AVRCP_META;
59     event[pos++] = sizeof(event) - 2;
60     event[pos++] = subeventID;
61     little_endian_store_16(event, pos, avrcp_cid);
62     pos += 2;
63     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
64 }
65 
66 static void avrcp_target_emit_respond_subunit_info_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t offset){
67     if (!callback) return;
68     uint8_t event[6];
69     int pos = 0;
70     event[pos++] = HCI_EVENT_AVRCP_META;
71     event[pos++] = sizeof(event) - 2;
72     event[pos++] = AVRCP_SUBEVENT_SUBUNIT_INFO_QUERY;
73     little_endian_store_16(event, pos, avrcp_cid);
74     pos += 2;
75     event[pos++] = offset;
76     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
77 }
78 
79 static void avrcp_target_emit_respond_vendor_dependent_query(btstack_packet_handler_t callback, uint16_t avrcp_cid, uint8_t subevent_id){
80     if (!callback) return;
81     uint8_t event[5];
82     int pos = 0;
83     event[pos++] = HCI_EVENT_AVRCP_META;
84     event[pos++] = sizeof(event) - 2;
85     event[pos++] = subevent_id;
86     little_endian_store_16(event, pos, avrcp_cid);
87     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
88 }
89 
90 static int avrcp_send_response(uint16_t cid, avrcp_connection_t * connection){
91     uint8_t command[30];
92     int pos = 0;
93     // transport header
94     // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier)
95     command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0;
96     // Profile IDentifier (PID)
97     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8;
98     command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF;
99 
100     // command_type
101     command[pos++] = connection->command_type;
102     // subunit_type | subunit ID
103     command[pos++] = (connection->subunit_type << 3) | connection->subunit_id;
104     // opcode
105     command[pos++] = (uint8_t)connection->command_opcode;
106     // operands
107     memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length);
108     pos += connection->cmd_operands_length;
109     connection->wait_to_send = 0;
110     return l2cap_send(cid, command, pos);
111 }
112 
113 static uint8_t avrcp_target_response_reject(avrcp_connection_t * connection, avrcp_subunit_type_t subunit_type, avrcp_subunit_id_t subunit_id, avrcp_command_opcode_t opcode, avrcp_pdu_id_t pdu_id, avrcp_status_code_t status){
114     // AVRCP_CTYPE_RESPONSE_REJECTED
115     connection->command_type = AVRCP_CTYPE_RESPONSE_REJECTED;
116     connection->subunit_type = subunit_type;
117     connection->subunit_id =   subunit_id;
118     connection->command_opcode = opcode;
119 
120     // company id is 3 bytes long
121     int pos = connection->cmd_operands_length;
122     connection->cmd_operands[pos++] = pdu_id;
123     connection->cmd_operands[pos++] = 0;
124     // param length
125     big_endian_store_16(connection->cmd_operands, pos, 1);
126     pos += 2;
127     connection->cmd_operands[pos++] = status;
128     connection->cmd_operands_length = pos;
129 
130     connection->state = AVCTP_W2_SEND_RESPONSE;
131     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
132     return ERROR_CODE_SUCCESS;
133 }
134 
135 uint8_t avrcp_target_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){
136     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
137     if (!connection){
138         log_error("avrcp_unit_info: could not find a connection.");
139         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
140     }
141     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
142 
143     uint8_t unit = 0;
144     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
145     connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique
146     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
147     connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO;
148 
149     connection->cmd_operands_length = 5;
150     connection->cmd_operands[0] = 0x07;
151     connection->cmd_operands[1] = (unit_type << 4) | unit;
152     // company id is 3 bytes long
153     big_endian_store_32(connection->cmd_operands, 2, company_id);
154 
155     connection->state = AVCTP_W2_SEND_RESPONSE;
156     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
157     return ERROR_CODE_SUCCESS;
158 }
159 
160 uint8_t avrcp_target_subunit_info(uint16_t avrcp_cid, avrcp_subunit_type_t subunit_type, uint8_t offset, uint8_t * subunit_info_data){
161     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
162     if (!connection){
163         log_error("avrcp_unit_info: could not find a connection.");
164         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
165     }
166     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
167 
168     connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO;
169     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
170     connection->subunit_type = subunit_type; //vendor unique
171     connection->subunit_id =   AVRCP_SUBUNIT_ID_IGNORE;
172 
173     uint8_t page = offset / 4;
174     uint8_t extension_code = 7;
175     connection->cmd_operands_length = 5;
176     connection->cmd_operands[0] = (page << 4) | extension_code;
177     memcpy(connection->cmd_operands+1, subunit_info_data + offset, 4);
178 
179     connection->state = AVCTP_W2_SEND_RESPONSE;
180     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
181     return ERROR_CODE_SUCCESS;
182 }
183 
184 static uint8_t avrcp_target_capability(uint16_t avrcp_cid, avrcp_capability_id_t capability_id, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
185     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
186     if (!connection){
187         log_error("avrcp_unit_info: could not find a connection.");
188         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
189     }
190     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
191 
192     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
193     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
194     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; //AVRCP_SUBUNIT_TYPE_UNIT;
195     connection->subunit_id =   AVRCP_SUBUNIT_ID;
196 
197     int pos = connection->cmd_operands_length;
198     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_CAPABILITIES;
199     connection->cmd_operands[pos++] = 0;
200     // param length
201     big_endian_store_16(connection->cmd_operands, pos, 2+size);
202     pos += 2;
203     connection->cmd_operands[pos++] = capability_id;
204     connection->cmd_operands[pos++] = capabilities_num;
205     memcpy(connection->cmd_operands+pos, capabilities, size);
206     pos += size;
207     connection->cmd_operands_length = pos;
208     connection->state = AVCTP_W2_SEND_RESPONSE;
209     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
210     return ERROR_CODE_SUCCESS;
211 }
212 
213 uint8_t avrcp_target_play_status(uint16_t avrcp_cid, uint32_t song_length_ms, uint32_t song_position_ms, avrcp_play_status_t status){
214     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
215     if (!connection){
216         log_error("avrcp_unit_info: could not find a connection.");
217         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
218     }
219     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
220 
221     connection->command_opcode = AVRCP_CMD_OPCODE_VENDOR_DEPENDENT;
222     connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE;
223     connection->subunit_type = AVRCP_SUBUNIT_TYPE_PANEL; //AVRCP_SUBUNIT_TYPE_UNIT;
224     connection->subunit_id =   AVRCP_SUBUNIT_ID;
225 
226     int pos = connection->cmd_operands_length;
227     connection->cmd_operands[pos++] = AVRCP_PDU_ID_GET_PLAY_STATUS;
228     connection->cmd_operands[pos++] = 0;
229     // param length
230     big_endian_store_16(connection->cmd_operands, pos, 2+4+4+1);
231     pos += 2;
232     big_endian_store_32(connection->cmd_operands, pos, song_length_ms);
233     pos += 4;
234     big_endian_store_32(connection->cmd_operands, pos, song_position_ms);
235     pos += 4;
236     connection->cmd_operands[pos++] = status;
237     connection->cmd_operands_length = pos;
238     connection->state = AVCTP_W2_SEND_RESPONSE;
239     avrcp_request_can_send_now(connection, connection->l2cap_signaling_cid);
240     return ERROR_CODE_SUCCESS;
241 }
242 
243 
244 uint8_t avrcp_target_supported_companies(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size ){
245     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_COMPANY, capabilities_num, capabilities, size);
246 }
247 
248 uint8_t avrcp_target_supported_events(uint16_t avrcp_cid, uint8_t capabilities_num, uint8_t * capabilities, uint8_t size){
249     return avrcp_target_capability(avrcp_cid, AVRCP_CAPABILITY_ID_EVENT, capabilities_num, capabilities, size);
250 }
251 
252 
253 static uint8_t * avrcp_get_company_id(uint8_t *packet, uint16_t size){
254     UNUSED(size);
255     return packet + 6;
256 }
257 
258 static uint8_t * avrcp_get_pdu(uint8_t *packet, uint16_t size){
259     UNUSED(size);
260     return packet + 9;
261 }
262 
263 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){
264     UNUSED(connection);
265     UNUSED(packet);
266     UNUSED(size);
267 
268     // uint8_t opcode;
269 
270     uint8_t transport_header = packet[0];
271     connection->transaction_label = transport_header >> 4;
272     // uint8_t packet_type = (transport_header & 0x0F) >> 2;
273     // uint8_t frame_type = (transport_header & 0x03) >> 1;
274     // uint8_t ipid = transport_header & 0x01;
275     // uint8_t byte_value = packet[2];
276     // uint16_t pid = (byte_value << 8) | packet[2];
277 
278     // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[3];
279     // uint8_t byte_value = packet[4];
280     avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (packet[4] >> 3);
281     avrcp_subunit_id_t   subunit_id   = (avrcp_subunit_id_t) (packet[4] & 0x07);
282     // opcode = packet[pos++];
283 
284     // printf("    Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n",
285     //     transport_header, transaction_label, packet_type, frame_type, ipid, pid);
286     // printf_hexdump(packet+pos, size-pos);
287 
288     avrcp_command_opcode_t opcode = avrcp_cmd_opcode(packet,size);
289     // uint16_t param_length = big_endian_read_16(operands, 5);
290     uint8_t * company_id = avrcp_get_company_id(packet, size);
291     uint8_t * pdu = avrcp_get_pdu(packet, size);
292 
293     uint8_t   pdu_id;
294 
295     connection->cmd_operands_length = 0;
296 
297     switch (opcode){
298         case AVRCP_CMD_OPCODE_UNIT_INFO:
299             avrcp_target_emit_respond_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_UNIT_INFO_QUERY);
300             break;
301         case AVRCP_CMD_OPCODE_SUBUNIT_INFO:{
302             uint8_t offset =  4 * (packet[6]>>4);
303             avrcp_target_emit_respond_subunit_info_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, offset);
304             break;
305         }
306         case AVRCP_CMD_OPCODE_VENDOR_DEPENDENT:
307             pdu_id = pdu[0];
308             // 1 - reserved
309             // 2-3 param length,
310             memcpy(connection->cmd_operands, company_id, 3);
311             connection->cmd_operands_length = 3;
312             switch (pdu_id){
313                 case AVRCP_PDU_ID_GET_CAPABILITIES:{
314                     avrcp_capability_id_t capability_id = (avrcp_capability_id_t) pdu[4];
315                     switch (capability_id){
316                         case AVRCP_CAPABILITY_ID_EVENT:
317                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_EVENT_IDS_QUERY);
318                             break;
319                         case AVRCP_CAPABILITY_ID_COMPANY:
320                             avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_COMPANY_IDS_QUERY);
321                             break;
322                         default:
323                             avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_PARAMETER);
324                             break;
325                     }
326                     break;
327                 }
328                 case AVRCP_PDU_ID_GET_PLAY_STATUS:
329                     avrcp_target_emit_respond_vendor_dependent_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_PLAY_STATUS_QUERY);
330                     break;
331                 default:
332                     printf("unhandled pdu id 0x%02x\n", pdu_id);
333                     avrcp_target_response_reject(connection, subunit_type, subunit_id, opcode, pdu_id, AVRCP_STATUS_INVALID_COMMAND);
334                     break;
335             }
336             break;
337         default:
338             printf("AVRCP source: opcode 0x%02x not implemented\n", avrcp_cmd_opcode(packet,size));
339             break;
340     }
341 }
342 
343 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
344     avrcp_connection_t * connection;
345     switch (packet_type) {
346         case L2CAP_DATA_PACKET:
347             connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
348             if (!connection) break;
349             avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size);
350             break;
351         case HCI_EVENT_PACKET:
352             switch (hci_event_packet_get_type(packet)){
353                 case L2CAP_EVENT_CAN_SEND_NOW:
354                     connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context);
355                     if (!connection) break;
356                     switch (connection->state){
357                         case AVCTP_W2_SEND_RESPONSE:
358                             connection->state = AVCTP_CONNECTION_OPENED;
359                             avrcp_send_response(connection->l2cap_signaling_cid, connection);
360                             break;
361                         default:
362                             return;
363                     }
364                     break;
365             default:
366                 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_target_context);
367                 break;
368         }
369         default:
370             break;
371     }
372 }
373 
374 void avrcp_target_init(void){
375     avrcp_target_context.role = AVRCP_TARGET;
376     avrcp_target_context.connections = NULL;
377     avrcp_target_context.packet_handler = avrcp_controller_packet_handler;
378     l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0);
379 }
380 
381 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){
382     if (callback == NULL){
383         log_error("avrcp_register_packet_handler called with NULL callback");
384         return;
385     }
386     avrcp_target_context.avrcp_callback = callback;
387 }
388 
389 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){
390     return avrcp_connect(bd_addr, &avrcp_target_context, avrcp_cid);
391 }
392 
393 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){
394     avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context);
395     if (!connection){
396         log_error("avrcp_get_capabilities: could not find a connection.");
397         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
398     }
399     if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED;
400     l2cap_disconnect(connection->l2cap_signaling_cid, 0);
401     return ERROR_CODE_SUCCESS;
402 }
403 
404