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