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