1 /* 2 * Copyright (C) 2014 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 #include "btstack_config.h" 39 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "btstack_debug.h" 46 #include "hci_dump.h" 47 #include "bluetooth_sdp.h" 48 #include "btstack_event.h" 49 #include "classic/goep_client.h" 50 #include "classic/obex.h" 51 #include "classic/obex_iterator.h" 52 #include "classic/rfcomm.h" 53 #include "classic/sdp_client_rfcomm.h" 54 55 //------------------------------------------------------------------------------------------------------------ 56 // goep_client.c 57 // 58 59 typedef enum { 60 GOEP_INIT, 61 GOEP_W4_SDP, 62 GOEP_W4_CONNECTION, 63 GOEP_CONNECTED, 64 } goep_state_t; 65 66 typedef struct { 67 uint16_t cid; 68 goep_state_t state; 69 bd_addr_t bd_addr; 70 hci_con_handle_t con_handle; 71 uint8_t incoming; 72 uint8_t bearer_l2cap; 73 uint16_t bearer_port; // l2cap: psm, rfcomm: channel nr 74 uint16_t bearer_cid; 75 uint16_t bearer_mtu; 76 77 uint8_t obex_opcode; 78 uint32_t obex_connection_id; 79 int obex_connection_id_set; 80 81 btstack_packet_handler_t client_handler; 82 } goep_client_t; 83 84 static goep_client_t _goep_client; 85 static goep_client_t * goep_client = &_goep_client; 86 87 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){ 88 uint8_t event[22]; 89 int pos = 0; 90 event[pos++] = HCI_EVENT_GOEP_META; 91 pos++; // skip len 92 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 93 little_endian_store_16(event,pos,context->cid); 94 pos+=2; 95 event[pos++] = status; 96 memcpy(&event[pos], context->bd_addr, 6); 97 pos += 6; 98 little_endian_store_16(event,pos,context->con_handle); 99 pos += 2; 100 event[pos++] = context->incoming; 101 event[1] = pos - 2; 102 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 103 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 104 } 105 106 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){ 107 uint8_t event[5]; 108 int pos = 0; 109 event[pos++] = HCI_EVENT_GOEP_META; 110 pos++; // skip len 111 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 112 little_endian_store_16(event,pos,context->cid); 113 pos+=2; 114 event[1] = pos - 2; 115 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 116 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 117 } 118 119 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){ 120 uint8_t event[5]; 121 int pos = 0; 122 event[pos++] = HCI_EVENT_GOEP_META; 123 pos++; // skip len 124 event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW; 125 little_endian_store_16(event,pos,context->cid); 126 pos+=2; 127 event[1] = pos - 2; 128 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 129 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 130 } 131 132 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 133 UNUSED(channel); 134 UNUSED(size); 135 uint8_t status; 136 switch (packet_type){ 137 case HCI_EVENT_PACKET: 138 switch (hci_event_packet_get_type(packet)) { 139 case RFCOMM_EVENT_CHANNEL_OPENED: 140 status = rfcomm_event_channel_opened_get_status(packet); 141 if (status) { 142 log_info("goep_client: RFCOMM channel open failed, status %u", rfcomm_event_channel_opened_get_status(packet)); 143 goep_client->state = GOEP_INIT; 144 } else { 145 goep_client->bearer_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 146 log_info("goep_client: RFCOMM channel open succeeded. cid %u, max frame size %u", goep_client->bearer_cid, goep_client->bearer_mtu); 147 goep_client->state = GOEP_CONNECTED; 148 } 149 goep_client_emit_connected_event(goep_client, status); 150 return; 151 case RFCOMM_EVENT_CAN_SEND_NOW: 152 goep_client_emit_can_send_now_event(goep_client); 153 break; 154 case RFCOMM_CHANNEL_CLOSED: 155 goep_client->state = GOEP_INIT; 156 goep_client_emit_connection_closed_event(goep_client); 157 break; 158 default: 159 break; 160 } 161 break; 162 case RFCOMM_DATA_PACKET: 163 goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size); 164 break; 165 default: 166 break; 167 } 168 } 169 170 static void goep_client_handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 171 UNUSED(packet_type); 172 UNUSED(channel); 173 UNUSED(size); 174 175 switch (packet[0]){ 176 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 177 goep_client->bearer_port = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 178 break; 179 case SDP_EVENT_QUERY_COMPLETE: 180 if (goep_client->bearer_port == 0){ 181 log_info("Remote GOEP RFCOMM Server Channel not found"); 182 goep_client->state = GOEP_INIT; 183 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 184 break; 185 } 186 log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->bearer_port); 187 rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->bearer_port, &goep_client->bearer_cid); 188 break; 189 } 190 } 191 192 static void goep_client_packet_append(const uint8_t * data, uint16_t len){ 193 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 194 uint16_t pos = big_endian_read_16(buffer, 1); 195 memcpy(&buffer[pos], data, len); 196 pos += len; 197 big_endian_store_16(buffer, 1, pos); 198 } 199 200 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){ 201 UNUSED(goep_cid); 202 rfcomm_reserve_packet_buffer(); 203 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 204 buffer[0] = opcode; 205 big_endian_store_16(buffer, 1, 3); 206 // store opcode for parsing of response 207 goep_client->obex_opcode = opcode; 208 } 209 210 static void goep_client_packet_add_connection_id(uint16_t goep_cid){ 211 UNUSED(goep_cid); 212 // add connection_id header if set, must be first header if used 213 if (goep_client->obex_connection_id != OBEX_CONNECTION_ID_INVALID){ 214 uint8_t header[5]; 215 header[0] = OBEX_HEADER_CONNECTION_ID; 216 big_endian_store_32(header, 1, goep_client->obex_connection_id); 217 goep_client_packet_append(&header[0], sizeof(header)); 218 } 219 } 220 221 void goep_client_init(void){ 222 memset(goep_client, 0, sizeof(goep_client_t)); 223 goep_client->state = GOEP_INIT; 224 goep_client->cid = 1; 225 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 226 } 227 228 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){ 229 if (goep_client->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED; 230 goep_client->client_handler = handler; 231 goep_client->state = GOEP_W4_SDP; 232 memcpy(goep_client->bd_addr, addr, 6); 233 sdp_client_query_rfcomm_channel_and_name_for_uuid(&goep_client_handle_query_rfcomm_event, goep_client->bd_addr, uuid); 234 *out_cid = goep_client->cid; 235 return 0; 236 } 237 238 uint8_t goep_client_disconnect(uint16_t goep_cid){ 239 UNUSED(goep_cid); 240 rfcomm_disconnect(goep_client->bearer_cid); 241 return 0; 242 } 243 244 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 245 UNUSED(goep_cid); 246 goep_client->obex_connection_id = connection_id; 247 } 248 249 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 250 UNUSED(goep_cid); 251 return goep_client->obex_opcode; 252 } 253 254 void goep_client_request_can_send_now(uint16_t goep_cid){ 255 UNUSED(goep_cid); 256 rfcomm_request_can_send_now_event(goep_client->bearer_cid); 257 } 258 259 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 260 UNUSED(goep_cid); 261 goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT); 262 uint8_t fields[4]; 263 fields[0] = obex_version_number; 264 fields[1] = flags; 265 // workaround: limit OBEX packet len to RFCOMM MTU to avoid handling of fragemented packets 266 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu); 267 big_endian_store_16(fields, 2, maximum_obex_packet_length); 268 goep_client_packet_append(&fields[0], sizeof(fields)); 269 } 270 271 void goep_client_create_get_request(uint16_t goep_cid){ 272 UNUSED(goep_cid); 273 goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 274 goep_client_packet_add_connection_id(goep_cid); 275 } 276 277 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){ 278 UNUSED(goep_cid); 279 goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH); 280 uint8_t fields[2]; 281 fields[0] = flags; 282 fields[1] = 0; // reserved 283 goep_client_packet_append(&fields[0], sizeof(fields)); 284 goep_client_packet_add_connection_id(goep_cid); 285 } 286 287 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){ 288 UNUSED(goep_cid); 289 uint8_t header[3]; 290 header[0] = OBEX_HEADER_TARGET; 291 big_endian_store_16(header, 1, 1 + 2 + length); 292 goep_client_packet_append(&header[0], sizeof(header)); 293 goep_client_packet_append(target, length); 294 } 295 296 void goep_client_add_header_name(uint16_t goep_cid, const char * name){ 297 UNUSED(goep_cid); 298 int len_incl_zero = strlen(name) + 1; 299 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 300 uint16_t pos = big_endian_read_16(buffer, 1); 301 buffer[pos++] = OBEX_HEADER_NAME; 302 big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2); 303 pos += 2; 304 int i; 305 // @note name[len] == 0 306 for (i = 0 ; i < len_incl_zero ; i++){ 307 buffer[pos++] = 0; 308 buffer[pos++] = *name++; 309 } 310 big_endian_store_16(buffer, 1, pos); 311 } 312 313 void goep_client_add_header_type(uint16_t goep_cid, const char * type){ 314 UNUSED(goep_cid); 315 uint8_t header[3]; 316 header[0] = OBEX_HEADER_TYPE; 317 int len_incl_zero = strlen(type) + 1; 318 big_endian_store_16(header, 1, 1 + 2 + len_incl_zero); 319 goep_client_packet_append(&header[0], sizeof(header)); 320 goep_client_packet_append((const uint8_t*)type, len_incl_zero); 321 } 322 323 int goep_client_execute(uint16_t goep_cid){ 324 UNUSED(goep_cid); 325 uint8_t * buffer = rfcomm_get_outgoing_buffer(); 326 uint16_t pos = big_endian_read_16(buffer, 1); 327 return rfcomm_send_prepared(goep_client->bearer_cid, pos); 328 } 329