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.h" 56 #include "classic/sdp_util.h" 57 #include "l2cap.h" 58 59 //------------------------------------------------------------------------------------------------------------ 60 // goep_client.c 61 // 62 63 typedef enum { 64 GOEP_INIT, 65 GOEP_W4_SDP, 66 GOEP_W4_CONNECTION, 67 GOEP_CONNECTED, 68 } goep_state_t; 69 70 typedef struct { 71 uint16_t cid; 72 goep_state_t state; 73 bd_addr_t bd_addr; 74 hci_con_handle_t con_handle; 75 uint8_t incoming; 76 uint8_t rfcomm_port; 77 uint16_t l2cap_psm; 78 uint16_t bearer_cid; 79 uint16_t bearer_mtu; 80 uint32_t pbap_supported_features; 81 82 uint8_t obex_opcode; 83 uint32_t obex_connection_id; 84 int obex_connection_id_set; 85 86 btstack_packet_handler_t client_handler; 87 } goep_client_t; 88 89 static goep_client_t _goep_client; 90 static goep_client_t * goep_client = &_goep_client; 91 92 static uint8_t attribute_value[30]; 93 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 94 95 static uint8_t goep_packet_buffer[100]; 96 97 #ifdef ENABLE_GOEP_L2CAP 98 static uint8_t ertm_buffer[1000]; 99 static l2cap_ertm_config_t ertm_config = { 100 1, // ertm mandatory 101 2, // max transmit, some tests require > 1 102 2000, 103 12000, 104 144, // l2cap ertm mtu 105 4, 106 4, 107 }; 108 #endif 109 110 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){ 111 uint8_t event[15]; 112 int pos = 0; 113 event[pos++] = HCI_EVENT_GOEP_META; 114 pos++; // skip len 115 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 116 little_endian_store_16(event,pos,context->cid); 117 pos+=2; 118 event[pos++] = status; 119 memcpy(&event[pos], context->bd_addr, 6); 120 pos += 6; 121 little_endian_store_16(event,pos,context->con_handle); 122 pos += 2; 123 event[pos++] = context->incoming; 124 event[1] = pos - 2; 125 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 126 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 127 } 128 129 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){ 130 uint8_t event[5]; 131 int pos = 0; 132 event[pos++] = HCI_EVENT_GOEP_META; 133 pos++; // skip len 134 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 135 little_endian_store_16(event,pos,context->cid); 136 pos+=2; 137 event[1] = pos - 2; 138 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 139 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 140 } 141 142 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){ 143 uint8_t event[5]; 144 int pos = 0; 145 event[pos++] = HCI_EVENT_GOEP_META; 146 pos++; // skip len 147 event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW; 148 little_endian_store_16(event,pos,context->cid); 149 pos+=2; 150 event[1] = pos - 2; 151 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 152 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 153 } 154 155 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){ 156 if (status) { 157 context->state = GOEP_INIT; 158 log_info("goep_client: open failed, status %u", status); 159 } else { 160 context->bearer_mtu = mtu; 161 context->state = GOEP_CONNECTED; 162 log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu); 163 } 164 goep_client_emit_connected_event(context, status); 165 } 166 167 static void goep_client_handle_connection_close(goep_client_t * context){ 168 context->state = GOEP_INIT; 169 goep_client_emit_connection_closed_event(context); 170 } 171 172 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 173 UNUSED(channel); 174 UNUSED(size); 175 goep_client_t * context = goep_client; 176 switch (packet_type){ 177 case HCI_EVENT_PACKET: 178 switch (hci_event_packet_get_type(packet)) { 179 #ifdef ENABLE_GOEP_L2CAP 180 case L2CAP_EVENT_CHANNEL_OPENED: 181 goep_client_handle_connection_opened(context, l2cap_event_channel_opened_get_status(packet), 182 btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet))); 183 return; 184 case L2CAP_EVENT_CAN_SEND_NOW: 185 goep_client_emit_can_send_now_event(context); 186 break; 187 case L2CAP_EVENT_CHANNEL_CLOSED: 188 goep_client_handle_connection_close(context); 189 break; 190 #endif 191 case RFCOMM_EVENT_CHANNEL_OPENED: 192 goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet)); 193 return; 194 case RFCOMM_EVENT_CAN_SEND_NOW: 195 goep_client_emit_can_send_now_event(context); 196 break; 197 case RFCOMM_EVENT_CHANNEL_CLOSED: 198 goep_client_handle_connection_close(context); 199 break; 200 default: 201 break; 202 } 203 break; 204 case L2CAP_DATA_PACKET: 205 case RFCOMM_DATA_PACKET: 206 context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size); 207 break; 208 default: 209 break; 210 } 211 } 212 213 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 214 goep_client_t * context = goep_client; 215 216 UNUSED(packet_type); 217 UNUSED(channel); 218 UNUSED(size); 219 220 des_iterator_t des_list_it; 221 des_iterator_t prot_it; 222 uint8_t status; 223 224 225 switch (hci_event_packet_get_type(packet)){ 226 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 227 228 // check if relevant attribute 229 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 230 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 231 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 232 #ifdef ENABLE_GOEP_L2CAP 233 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 234 #endif 235 break; 236 default: 237 return; 238 } 239 240 // warn if attribute too large to fit in our buffer 241 if (sdp_event_query_attribute_byte_get_attribute_length(packet) > attribute_value_buffer_size) { 242 log_error("SDP attribute value size exceeded for attribute %x: available %d, required %d", sdp_event_query_attribute_byte_get_attribute_id(packet), attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 243 break; 244 } 245 246 // store single byte 247 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 248 249 // wait until value fully received 250 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break; 251 252 // process attributes 253 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 254 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 255 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 256 uint8_t *des_element; 257 uint8_t *element; 258 uint32_t uuid; 259 260 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 261 262 des_element = des_iterator_get_element(&des_list_it); 263 des_iterator_init(&prot_it, des_element); 264 element = des_iterator_get_element(&prot_it); 265 266 if (de_get_element_type(element) != DE_UUID) continue; 267 268 uuid = de_get_uuid32(element); 269 switch (uuid){ 270 case BLUETOOTH_PROTOCOL_RFCOMM: 271 if (!des_iterator_has_more(&prot_it)) continue; 272 des_iterator_next(&prot_it); 273 element = des_iterator_get_element(&prot_it); 274 context->rfcomm_port = element[de_get_header_size(element)]; 275 break; 276 default: 277 break; 278 } 279 } 280 break; 281 #ifdef ENABLE_GOEP_L2CAP 282 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 283 de_element_get_uint16(attribute_value, &context->l2cap_psm); 284 break; 285 #endif 286 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 287 if (de_get_element_type(attribute_value) != DE_UINT) break; 288 if (de_get_size_type(attribute_value) != DE_SIZE_32) break; 289 context->pbap_supported_features = big_endian_read_32(attribute_value, de_get_header_size(attribute_value)); 290 log_info("pbap_supported_features 0x%x", context->pbap_supported_features); 291 break; 292 default: 293 break; 294 } 295 break; 296 297 case SDP_EVENT_QUERY_COMPLETE: 298 status = sdp_event_query_complete_get_status(packet); 299 if (status != ERROR_CODE_SUCCESS){ 300 log_info("GOEP client, SDP query failed 0x%02x", status); 301 context->state = GOEP_INIT; 302 goep_client_emit_connected_event(goep_client, status); 303 break; 304 } 305 if (context->rfcomm_port == 0 && context->l2cap_psm == 0){ 306 log_info("No GOEP RFCOMM or L2CAP server found"); 307 context->state = GOEP_INIT; 308 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 309 break; 310 } 311 #ifdef ENABLE_GOEP_L2CAP 312 if (context->l2cap_psm){ 313 log_info("Remote GOEP L2CAP PSM: %u", context->l2cap_psm); 314 l2cap_create_ertm_channel(&goep_client_packet_handler, context->bd_addr, context->l2cap_psm, 315 &ertm_config, ertm_buffer, sizeof(ertm_buffer), &context->bearer_cid); 316 return; 317 } 318 #endif 319 log_info("Remote GOEP RFCOMM Server Channel: %u", context->rfcomm_port); 320 rfcomm_create_channel(&goep_client_packet_handler, context->bd_addr, context->rfcomm_port, &context->bearer_cid); 321 } 322 } 323 324 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){ 325 if (context->l2cap_psm){ 326 return goep_packet_buffer; 327 } else { 328 return rfcomm_get_outgoing_buffer(); 329 } 330 } 331 332 static void goep_client_packet_append(const uint8_t * data, uint16_t len){ 333 goep_client_t * context = goep_client; 334 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 335 uint16_t pos = big_endian_read_16(buffer, 1); 336 memcpy(&buffer[pos], data, len); 337 pos += len; 338 big_endian_store_16(buffer, 1, pos); 339 } 340 341 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){ 342 UNUSED(goep_cid); 343 goep_client_t * context = goep_client; 344 if (context->l2cap_psm){ 345 } else { 346 rfcomm_reserve_packet_buffer(); 347 } 348 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 349 buffer[0] = opcode; 350 big_endian_store_16(buffer, 1, 3); 351 // store opcode for parsing of response 352 context->obex_opcode = opcode; 353 } 354 355 static void goep_client_packet_add_connection_id(uint16_t goep_cid){ 356 UNUSED(goep_cid); 357 goep_client_t * context = goep_client; 358 // add connection_id header if set, must be first header if used 359 if (context->obex_connection_id != OBEX_CONNECTION_ID_INVALID){ 360 uint8_t header[5]; 361 header[0] = OBEX_HEADER_CONNECTION_ID; 362 big_endian_store_32(header, 1, context->obex_connection_id); 363 goep_client_packet_append(&header[0], sizeof(header)); 364 } 365 } 366 367 void goep_client_init(void){ 368 memset(goep_client, 0, sizeof(goep_client_t)); 369 goep_client->state = GOEP_INIT; 370 goep_client->cid = 1; 371 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 372 } 373 374 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){ 375 goep_client_t * context = goep_client; 376 if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED; 377 context->client_handler = handler; 378 context->state = GOEP_W4_SDP; 379 context->l2cap_psm = 0; 380 context->rfcomm_port = 0; 381 // Backwards compatibility: If the PbapSupportedFeatures attribute is not present 0x00000003 382 // shall be assumed for a remote PSE. 383 context->pbap_supported_features = 0x03; 384 memcpy(context->bd_addr, addr, 6); 385 sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, context->bd_addr, uuid); 386 *out_cid = context->cid; 387 return 0; 388 } 389 390 uint32_t goep_client_get_pbap_supported_features(uint16_t goep_cid){ 391 UNUSED(goep_cid); 392 goep_client_t * context = goep_client; 393 return context->pbap_supported_features; 394 } 395 396 uint8_t goep_client_disconnect(uint16_t goep_cid){ 397 UNUSED(goep_cid); 398 goep_client_t * context = goep_client; 399 rfcomm_disconnect(context->bearer_cid); 400 return 0; 401 } 402 403 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 404 UNUSED(goep_cid); 405 goep_client_t * context = goep_client; 406 context->obex_connection_id = connection_id; 407 } 408 409 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 410 UNUSED(goep_cid); 411 goep_client_t * context = goep_client; 412 return context->obex_opcode; 413 } 414 415 void goep_client_request_can_send_now(uint16_t goep_cid){ 416 UNUSED(goep_cid); 417 goep_client_t * context = goep_client; 418 if (context->l2cap_psm){ 419 l2cap_request_can_send_now_event(context->bearer_cid); 420 } else { 421 rfcomm_request_can_send_now_event(context->bearer_cid); 422 } 423 } 424 425 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 426 UNUSED(goep_cid); 427 goep_client_t * context = goep_client; 428 goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT); 429 uint8_t fields[4]; 430 fields[0] = obex_version_number; 431 fields[1] = flags; 432 // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets 433 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu); 434 big_endian_store_16(fields, 2, maximum_obex_packet_length); 435 goep_client_packet_append(&fields[0], sizeof(fields)); 436 } 437 438 void goep_client_create_disconnect_request(uint16_t goep_cid){ 439 UNUSED(goep_cid); 440 goep_client_packet_init(goep_cid, OBEX_OPCODE_DISCONNECT); 441 goep_client_packet_add_connection_id(goep_cid); 442 } 443 444 void goep_client_create_get_request(uint16_t goep_cid){ 445 UNUSED(goep_cid); 446 goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 447 goep_client_packet_add_connection_id(goep_cid); 448 } 449 450 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){ 451 UNUSED(goep_cid); 452 goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH); 453 uint8_t fields[2]; 454 fields[0] = flags; 455 fields[1] = 0; // reserved 456 goep_client_packet_append(&fields[0], sizeof(fields)); 457 goep_client_packet_add_connection_id(goep_cid); 458 } 459 460 static void goep_client_add_header(uint16_t goep_cid, uint8_t header_type, uint16_t header_length, const uint8_t * header_data){ 461 UNUSED(goep_cid); 462 uint8_t header[3]; 463 header[0] = header_type; 464 big_endian_store_16(header, 1, 1 + 2 + header_length); 465 goep_client_packet_append(&header[0], sizeof(header)); 466 goep_client_packet_append(header_data, header_length); 467 } 468 469 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){ 470 goep_client_add_header(goep_cid, OBEX_HEADER_TARGET, length, target); 471 } 472 473 void goep_client_add_header_application_parameters(uint16_t goep_cid, uint16_t length, const uint8_t * data){ 474 goep_client_add_header(goep_cid, OBEX_HEADER_APPLICATION_PARAMETERS, length, data); 475 } 476 477 void goep_client_add_header_challenge_response(uint16_t goep_cid, uint16_t length, const uint8_t * data){ 478 goep_client_add_header(goep_cid, OBEX_HEADER_AUTHENTICATION_RESPONSE, length, data); 479 } 480 481 void goep_client_add_header_name(uint16_t goep_cid, const char * name){ 482 UNUSED(goep_cid); 483 goep_client_t * context = goep_client; 484 int len_incl_zero = strlen(name) + 1; 485 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 486 uint16_t pos = big_endian_read_16(buffer, 1); 487 buffer[pos++] = OBEX_HEADER_NAME; 488 big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2); 489 pos += 2; 490 int i; 491 // @note name[len] == 0 492 for (i = 0 ; i < len_incl_zero ; i++){ 493 buffer[pos++] = 0; 494 buffer[pos++] = *name++; 495 } 496 big_endian_store_16(buffer, 1, pos); 497 } 498 499 void goep_client_add_header_type(uint16_t goep_cid, const char * type){ 500 UNUSED(goep_cid); 501 uint8_t header[3]; 502 header[0] = OBEX_HEADER_TYPE; 503 int len_incl_zero = strlen(type) + 1; 504 big_endian_store_16(header, 1, 1 + 2 + len_incl_zero); 505 goep_client_packet_append(&header[0], sizeof(header)); 506 goep_client_packet_append((const uint8_t*)type, len_incl_zero); 507 } 508 509 int goep_client_execute(uint16_t goep_cid){ 510 UNUSED(goep_cid); 511 goep_client_t * context = goep_client; 512 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 513 uint16_t pos = big_endian_read_16(buffer, 1); 514 if (context->l2cap_psm){ 515 // return l2cap_send_prepared(context->bearer_cid, pos); 516 return l2cap_send(context->bearer_cid, buffer, pos); 517 } else { 518 return rfcomm_send_prepared(context->bearer_cid, pos); 519 } 520 } 521