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 page){ 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++] = page; 76 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 77 } 78 79 static int avrcp_send_response(uint16_t cid, avrcp_connection_t * connection){ 80 uint8_t command[30]; 81 int pos = 0; 82 // transport header 83 // Transaction label | Packet_type | C/R | IPID (1 == invalid profile identifier) 84 command[pos++] = (connection->transaction_label << 4) | (AVRCP_SINGLE_PACKET << 2) | (AVRCP_RESPONSE_FRAME << 1) | 0; 85 // Profile IDentifier (PID) 86 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL >> 8; 87 command[pos++] = BLUETOOTH_SERVICE_CLASS_AV_REMOTE_CONTROL & 0x00FF; 88 89 // command_type 90 command[pos++] = connection->command_type; 91 // subunit_type | subunit ID 92 command[pos++] = (connection->subunit_type << 3) | connection->subunit_id; 93 // opcode 94 command[pos++] = (uint8_t)connection->command_opcode; 95 // operands 96 memcpy(command+pos, connection->cmd_operands, connection->cmd_operands_length); 97 pos += connection->cmd_operands_length; 98 99 return l2cap_send(cid, command, pos); 100 } 101 102 uint8_t avrcp_target_unit_info(uint16_t avrcp_cid, avrcp_subunit_type_t unit_type, uint32_t company_id){ 103 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context); 104 if (!connection){ 105 log_error("avrcp_unit_info: could not find a connection."); 106 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 107 } 108 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 109 110 uint8_t unit = 0; 111 connection->command_opcode = AVRCP_CMD_OPCODE_UNIT_INFO; 112 connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 113 connection->subunit_type = AVRCP_SUBUNIT_TYPE_UNIT; //vendor unique 114 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 115 116 connection->cmd_operands_length = 5; 117 connection->cmd_operands[0] = 0x07; 118 connection->cmd_operands[1] = (unit_type << 4) | unit; 119 // company id is 3 bytes long 120 little_endian_store_32(connection->cmd_operands, 2, company_id); 121 return avrcp_send_response(connection->l2cap_signaling_cid, connection); 122 } 123 124 uint8_t avrcp_target_subunit_info(uint16_t avrcp_cid, avrcp_subunit_type_t subunit_type, uint8_t offset, uint8_t * subunit_info_data){ 125 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context); 126 if (!connection){ 127 log_error("avrcp_unit_info: could not find a connection."); 128 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 129 } 130 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 131 132 connection->command_opcode = AVRCP_CMD_OPCODE_SUBUNIT_INFO; 133 connection->command_type = AVRCP_CTYPE_RESPONSE_IMPLEMENTED_STABLE; 134 connection->subunit_type = subunit_type; //vendor unique 135 connection->subunit_id = AVRCP_SUBUNIT_ID_IGNORE; 136 137 uint8_t page = offset / 4; 138 uint8_t extension_code = 7; 139 connection->cmd_operands_length = 5; 140 connection->cmd_operands[0] = (page << 4) | extension_code; 141 memcpy(connection->cmd_operands+1, subunit_info_data + offset, 4); 142 return avrcp_send_response(connection->l2cap_signaling_cid, connection); 143 } 144 145 static void avrcp_handle_l2cap_data_packet_for_signaling_connection(avrcp_connection_t * connection, uint8_t *packet, uint16_t size){ 146 UNUSED(connection); 147 UNUSED(packet); 148 UNUSED(size); 149 150 // uint8_t opcode; 151 // int pos = 3; 152 uint8_t transport_header = packet[0]; 153 connection->transaction_label = transport_header >> 4; 154 // uint8_t packet_type = (transport_header & 0x0F) >> 2; 155 // uint8_t frame_type = (transport_header & 0x03) >> 1; 156 // uint8_t ipid = transport_header & 0x01; 157 // uint8_t byte_value = packet[2]; 158 // uint16_t pid = (byte_value << 8) | packet[2]; 159 160 // avrcp_command_type_t ctype = (avrcp_command_type_t) packet[pos++]; 161 // byte_value = packet[pos++]; 162 // avrcp_subunit_type_t subunit_type = (avrcp_subunit_type_t) (byte_value >> 3); 163 // avrcp_subunit_type_t subunit_id = (avrcp_subunit_type_t) (byte_value & 0x07); 164 // opcode = packet[pos++]; 165 166 // printf(" Transport header 0x%02x (transaction_label %d, packet_type %d, frame_type %d, ipid %d), pid 0x%4x\n", 167 // transport_header, transaction_label, packet_type, frame_type, ipid, pid); 168 // printf_hexdump(packet+pos, size-pos); 169 170 // uint8_t pdu_id; 171 // uint16_t param_length; 172 uint8_t * operands = packet + 6; 173 switch (avrcp_cmd_opcode(packet,size)){ 174 case AVRCP_CMD_OPCODE_UNIT_INFO: 175 avrcp_target_emit_respond_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, AVRCP_SUBEVENT_UNIT_INFO_QUERY); 176 break; 177 case AVRCP_CMD_OPCODE_SUBUNIT_INFO: 178 // printf("operands 0x%02x\n", ); 179 // printf_hexdump(packet, size) 180 avrcp_target_emit_respond_subunit_info_query(avrcp_target_context.avrcp_callback, connection->avrcp_cid, 4 * (operands[0]>>4)); 181 break; 182 default: 183 printf("AVRCP source: opcode 0x%02x not implemented\n", avrcp_cmd_opcode(packet,size)); 184 break; 185 } 186 } 187 188 static void avrcp_target_handle_can_send_now(avrcp_connection_t * connection){ 189 switch (connection->state){ 190 default: 191 return; 192 } 193 } 194 195 static void avrcp_controller_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 196 avrcp_connection_t * connection; 197 switch (packet_type) { 198 case L2CAP_DATA_PACKET: 199 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context); 200 if (!connection) break; 201 avrcp_handle_l2cap_data_packet_for_signaling_connection(connection, packet, size); 202 break; 203 case HCI_EVENT_PACKET: 204 switch (hci_event_packet_get_type(packet)){ 205 case L2CAP_EVENT_CAN_SEND_NOW: 206 connection = get_avrcp_connection_for_l2cap_signaling_cid(channel, &avrcp_target_context); 207 if (!connection) break; 208 avrcp_target_handle_can_send_now(connection); 209 break; 210 default: 211 avrcp_packet_handler(packet_type, channel, packet, size, &avrcp_target_context); 212 break; 213 } 214 default: 215 break; 216 } 217 } 218 219 void avrcp_target_init(void){ 220 avrcp_target_context.role = AVRCP_TARGET; 221 avrcp_target_context.connections = NULL; 222 avrcp_target_context.packet_handler = avrcp_controller_packet_handler; 223 l2cap_register_service(&avrcp_controller_packet_handler, BLUETOOTH_PROTOCOL_AVCTP, 0xffff, LEVEL_0); 224 } 225 226 void avrcp_target_register_packet_handler(btstack_packet_handler_t callback){ 227 if (callback == NULL){ 228 log_error("avrcp_register_packet_handler called with NULL callback"); 229 return; 230 } 231 avrcp_target_context.avrcp_callback = callback; 232 } 233 234 uint8_t avrcp_target_connect(bd_addr_t bd_addr, uint16_t * avrcp_cid){ 235 return avrcp_connect(bd_addr, &avrcp_target_context, avrcp_cid); 236 } 237 238 uint8_t avrcp_target_disconnect(uint16_t avrcp_cid){ 239 avrcp_connection_t * connection = get_avrcp_connection_for_avrcp_cid(avrcp_cid, &avrcp_target_context); 240 if (!connection){ 241 log_error("avrcp_get_capabilities: could not find a connection."); 242 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 243 } 244 if (connection->state != AVCTP_CONNECTION_OPENED) return ERROR_CODE_COMMAND_DISALLOWED; 245 l2cap_disconnect(connection->l2cap_signaling_cid, 0); 246 return ERROR_CODE_SUCCESS; 247 } 248 249