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