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