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 BLUEKITCHEN 24 * GMBH 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 <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_message_builder.h" 51 #include "classic/obex.h" 52 #include "classic/obex_iterator.h" 53 #include "classic/rfcomm.h" 54 #include "classic/sdp_client.h" 55 #include "classic/sdp_util.h" 56 #include "l2cap.h" 57 58 //------------------------------------------------------------------------------------------------------------ 59 // goep_client.c 60 // 61 62 // #define ENABLE_GOEP_L2CAP 63 64 #ifdef ENABLE_GOEP_L2CAP 65 #ifndef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 66 #error "ENABLE_GOEP_L2CAP requires ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE. Please enable ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE or disable ENABLE_GOEP_L2CAP" 67 #endif 68 #endif 69 70 typedef enum { 71 GOEP_INIT, 72 GOEP_W4_SDP, 73 GOEP_W4_CONNECTION, 74 GOEP_CONNECTED, 75 } goep_state_t; 76 77 typedef struct { 78 uint16_t cid; 79 goep_state_t state; 80 bd_addr_t bd_addr; 81 hci_con_handle_t con_handle; 82 uint8_t incoming; 83 uint8_t rfcomm_port; 84 uint16_t l2cap_psm; 85 uint16_t bearer_cid; 86 uint16_t bearer_mtu; 87 88 // cached higher layer information PBAP + MAP 89 uint32_t profile_supported_features; 90 uint8_t map_mas_instance_id; 91 uint8_t map_supported_message_types; 92 93 uint8_t obex_opcode; 94 uint32_t obex_connection_id; 95 int obex_connection_id_set; 96 97 btstack_packet_handler_t client_handler; 98 } goep_client_t; 99 100 static goep_client_t goep_client_singleton; 101 static goep_client_t * goep_client = &goep_client_singleton; 102 103 static uint8_t goep_client_sdp_query_attribute_value[30]; 104 static const unsigned int goep_client_sdp_query_attribute_value_buffer_size = sizeof(goep_client_sdp_query_attribute_value); 105 106 static uint8_t goep_packet_buffer[150]; 107 108 #ifdef ENABLE_GOEP_L2CAP 109 static uint8_t ertm_buffer[1000]; 110 static l2cap_ertm_config_t ertm_config = { 111 1, // ertm mandatory 112 2, // max transmit, some tests require > 1 113 2000, 114 12000, 115 512, // l2cap ertm mtu 116 2, 117 2, 118 1, // 16-bit FCS 119 }; 120 #endif 121 122 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){ 123 uint8_t event[15]; 124 int pos = 0; 125 event[pos++] = HCI_EVENT_GOEP_META; 126 pos++; // skip len 127 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 128 little_endian_store_16(event,pos,context->cid); 129 pos+=2; 130 event[pos++] = status; 131 (void)memcpy(&event[pos], context->bd_addr, 6); 132 pos += 6; 133 little_endian_store_16(event,pos,context->con_handle); 134 pos += 2; 135 event[pos++] = context->incoming; 136 event[1] = pos - 2; 137 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 138 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 139 } 140 141 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){ 142 uint8_t event[5]; 143 int pos = 0; 144 event[pos++] = HCI_EVENT_GOEP_META; 145 pos++; // skip len 146 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 147 little_endian_store_16(event,pos,context->cid); 148 pos+=2; 149 event[1] = pos - 2; 150 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 151 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 152 } 153 154 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){ 155 uint8_t event[5]; 156 int pos = 0; 157 event[pos++] = HCI_EVENT_GOEP_META; 158 pos++; // skip len 159 event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW; 160 little_endian_store_16(event,pos,context->cid); 161 pos+=2; 162 event[1] = pos - 2; 163 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 164 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 165 } 166 167 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){ 168 if (status) { 169 context->state = GOEP_INIT; 170 log_info("goep_client: open failed, status %u", status); 171 } else { 172 context->bearer_mtu = mtu; 173 context->state = GOEP_CONNECTED; 174 log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu); 175 } 176 goep_client_emit_connected_event(context, status); 177 } 178 179 static void goep_client_handle_connection_close(goep_client_t * context){ 180 context->state = GOEP_INIT; 181 goep_client_emit_connection_closed_event(context); 182 } 183 184 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 185 UNUSED(channel); 186 UNUSED(size); 187 goep_client_t * context = goep_client; 188 switch (packet_type){ 189 case HCI_EVENT_PACKET: 190 switch (hci_event_packet_get_type(packet)) { 191 #ifdef ENABLE_GOEP_L2CAP 192 case L2CAP_EVENT_CHANNEL_OPENED: 193 goep_client_handle_connection_opened(context, l2cap_event_channel_opened_get_status(packet), 194 btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet))); 195 return; 196 case L2CAP_EVENT_CAN_SEND_NOW: 197 goep_client_emit_can_send_now_event(context); 198 break; 199 case L2CAP_EVENT_CHANNEL_CLOSED: 200 goep_client_handle_connection_close(context); 201 break; 202 #endif 203 case RFCOMM_EVENT_CHANNEL_OPENED: 204 goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet)); 205 return; 206 case RFCOMM_EVENT_CAN_SEND_NOW: 207 goep_client_emit_can_send_now_event(context); 208 break; 209 case RFCOMM_EVENT_CHANNEL_CLOSED: 210 goep_client_handle_connection_close(context); 211 break; 212 default: 213 break; 214 } 215 break; 216 case L2CAP_DATA_PACKET: 217 case RFCOMM_DATA_PACKET: 218 context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size); 219 break; 220 default: 221 break; 222 } 223 } 224 225 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 226 goep_client_t * context = goep_client; 227 228 UNUSED(packet_type); 229 UNUSED(channel); 230 UNUSED(size); 231 232 des_iterator_t des_list_it; 233 des_iterator_t prot_it; 234 uint8_t status; 235 236 237 switch (hci_event_packet_get_type(packet)){ 238 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 239 240 // check if relevant attribute 241 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 242 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 243 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 244 #ifdef ENABLE_GOEP_L2CAP 245 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 246 #endif 247 break; 248 default: 249 return; 250 } 251 252 // warn if attribute too large to fit in our buffer 253 if (sdp_event_query_attribute_byte_get_attribute_length(packet) > goep_client_sdp_query_attribute_value_buffer_size) { 254 log_error("SDP attribute value size exceeded for attribute %x: available %d, required %d", sdp_event_query_attribute_byte_get_attribute_id(packet), goep_client_sdp_query_attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 255 break; 256 } 257 258 // store single byte 259 goep_client_sdp_query_attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 260 261 // wait until value fully received 262 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break; 263 264 // process attributes 265 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 266 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 267 for (des_iterator_init(&des_list_it, goep_client_sdp_query_attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 268 uint8_t *des_element; 269 uint8_t *element; 270 uint32_t uuid; 271 #ifdef ENABLE_GOEP_L2CAP 272 uint16_t l2cap_psm; 273 #endif 274 275 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 276 277 des_element = des_iterator_get_element(&des_list_it); 278 des_iterator_init(&prot_it, des_element); 279 280 // first element is UUID 281 element = des_iterator_get_element(&prot_it); 282 if (de_get_element_type(element) != DE_UUID) continue; 283 284 uuid = de_get_uuid32(element); 285 des_iterator_next(&prot_it); 286 if (!des_iterator_has_more(&prot_it)) continue; 287 288 // second element is RFCOMM server channel or L2CAP PSM 289 element = des_iterator_get_element(&prot_it); 290 switch (uuid){ 291 #ifdef ENABLE_GOEP_L2CAP 292 case BLUETOOTH_PROTOCOL_L2CAP: 293 if (de_element_get_uint16(element, &l2cap_psm)){ 294 context->l2cap_psm = l2cap_psm; 295 } 296 break; 297 #endif 298 case BLUETOOTH_PROTOCOL_RFCOMM: 299 context->rfcomm_port = element[de_get_header_size(element)]; 300 break; 301 default: 302 break; 303 } 304 } 305 break; 306 #ifdef ENABLE_GOEP_L2CAP 307 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 308 de_element_get_uint16(goep_client_sdp_query_attribute_value, &context->l2cap_psm); 309 break; 310 #endif 311 // BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES == BLUETOOTH_ATTRIBUTE_MAP_SUPPORTED_FEATURES == 0x0317 312 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 313 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 314 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_32) break; 315 context->profile_supported_features = big_endian_read_32(goep_client_sdp_query_attribute_value, de_get_header_size(goep_client_sdp_query_attribute_value)); 316 log_info("PBAP/MAP supported_features 0x%x", (unsigned int) context->profile_supported_features); 317 break; 318 319 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID: 320 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 321 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 322 context->map_mas_instance_id = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 323 log_info("MAS Instance ID 0x%x", context->map_mas_instance_id); 324 break; 325 326 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES: 327 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 328 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 329 context->map_supported_message_types = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 330 log_info("Supported Message Types 0x%x", context->map_mas_instance_id); 331 break; 332 333 default: 334 break; 335 } 336 break; 337 338 case SDP_EVENT_QUERY_COMPLETE: 339 status = sdp_event_query_complete_get_status(packet); 340 if (status != ERROR_CODE_SUCCESS){ 341 log_info("GOEP client, SDP query failed 0x%02x", status); 342 context->state = GOEP_INIT; 343 goep_client_emit_connected_event(goep_client, status); 344 break; 345 } 346 if ((context->rfcomm_port == 0) && (context->l2cap_psm == 0)){ 347 log_info("No GOEP RFCOMM or L2CAP server found"); 348 context->state = GOEP_INIT; 349 goep_client_emit_connected_event(goep_client, SDP_SERVICE_NOT_FOUND); 350 break; 351 } 352 #ifdef ENABLE_GOEP_L2CAP 353 if (context->l2cap_psm){ 354 log_info("Remote GOEP L2CAP PSM: %u", context->l2cap_psm); 355 l2cap_ertm_create_channel(&goep_client_packet_handler, context->bd_addr, context->l2cap_psm, 356 &ertm_config, ertm_buffer, sizeof(ertm_buffer), &context->bearer_cid); 357 return; 358 } 359 #endif 360 log_info("Remote GOEP RFCOMM Server Channel: %u", context->rfcomm_port); 361 rfcomm_create_channel(&goep_client_packet_handler, context->bd_addr, context->rfcomm_port, &context->bearer_cid); 362 break; 363 364 default: 365 break; 366 } 367 } 368 369 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){ 370 if (context->l2cap_psm){ 371 return goep_packet_buffer; 372 } else { 373 return rfcomm_get_outgoing_buffer(); 374 } 375 } 376 377 static uint16_t goep_client_get_outgoing_buffer_len(goep_client_t * context){ 378 if (context->l2cap_psm){ 379 return sizeof(goep_packet_buffer); 380 } else { 381 return rfcomm_get_max_frame_size(context->bearer_cid); 382 } 383 } 384 385 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){ 386 UNUSED(goep_cid); 387 goep_client_t * context = goep_client; 388 if (context->l2cap_psm){ 389 } else { 390 rfcomm_reserve_packet_buffer(); 391 } 392 // store opcode for parsing of response 393 context->obex_opcode = opcode; 394 } 395 396 void goep_client_init(void){ 397 memset(goep_client, 0, sizeof(goep_client_t)); 398 goep_client->state = GOEP_INIT; 399 goep_client->cid = 1; 400 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 401 } 402 403 void goep_client_deinit(void){ 404 memset(goep_client, 0, sizeof(goep_client_t)); 405 memset(goep_client_sdp_query_attribute_value, 0, sizeof(goep_client_sdp_query_attribute_value)); 406 memset(goep_packet_buffer, 0, sizeof(goep_packet_buffer)); 407 } 408 409 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){ 410 goep_client_t * context = goep_client; 411 if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED; 412 context->client_handler = handler; 413 context->state = GOEP_W4_SDP; 414 context->l2cap_psm = 0; 415 context->rfcomm_port = 0; 416 context->profile_supported_features = PBAP_FEATURES_NOT_PRESENT; 417 (void)memcpy(context->bd_addr, addr, 6); 418 sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, context->bd_addr, uuid); 419 *out_cid = context->cid; 420 return ERROR_CODE_SUCCESS; 421 } 422 423 uint32_t goep_client_get_pbap_supported_features(uint16_t goep_cid){ 424 UNUSED(goep_cid); 425 goep_client_t * context = goep_client; 426 return context->profile_supported_features; 427 } 428 429 uint32_t goep_client_get_map_supported_features(uint16_t goep_cid){ 430 UNUSED(goep_cid); 431 goep_client_t * context = goep_client; 432 return context->profile_supported_features; 433 } 434 435 uint8_t goep_client_get_map_mas_instance_id(uint16_t goep_cid){ 436 UNUSED(goep_cid); 437 goep_client_t * context = goep_client; 438 return context->map_mas_instance_id; 439 } 440 441 uint8_t goep_client_get_map_suported_message_types(uint16_t goep_cid){ 442 UNUSED(goep_cid); 443 goep_client_t * context = goep_client; 444 return context->map_supported_message_types; 445 } 446 447 448 bool goep_client_version_20_or_higher(uint16_t goep_cid){ 449 UNUSED(goep_cid); 450 goep_client_t * context = goep_client; 451 return context->l2cap_psm != 0; 452 } 453 454 void goep_client_request_can_send_now(uint16_t goep_cid){ 455 UNUSED(goep_cid); 456 goep_client_t * context = goep_client; 457 if (context->l2cap_psm){ 458 l2cap_request_can_send_now_event(context->bearer_cid); 459 } else { 460 rfcomm_request_can_send_now_event(context->bearer_cid); 461 } 462 } 463 464 uint8_t goep_client_disconnect(uint16_t goep_cid){ 465 UNUSED(goep_cid); 466 goep_client_t * context = goep_client; 467 if (context->l2cap_psm){ 468 l2cap_disconnect(context->bearer_cid); 469 } else { 470 rfcomm_disconnect(context->bearer_cid); 471 } 472 return ERROR_CODE_SUCCESS; 473 } 474 475 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 476 UNUSED(goep_cid); 477 goep_client_t * context = goep_client; 478 context->obex_connection_id = connection_id; 479 } 480 481 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 482 UNUSED(goep_cid); 483 goep_client_t * context = goep_client; 484 return context->obex_opcode; 485 } 486 487 void goep_client_request_create_connect(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 488 UNUSED(goep_cid); 489 goep_client_t * context = goep_client; 490 goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT); 491 492 // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets 493 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu); 494 495 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 496 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 497 obex_message_builder_request_create_connect(buffer, buffer_len, obex_version_number, flags, maximum_obex_packet_length); 498 } 499 500 void goep_client_request_create_get(uint16_t goep_cid){ 501 UNUSED(goep_cid); 502 goep_client_t * context = goep_client; 503 goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 504 505 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 506 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 507 obex_message_builder_request_create_get(buffer, buffer_len, context->obex_connection_id); 508 } 509 510 void goep_client_request_create_put(uint16_t goep_cid){ 511 UNUSED(goep_cid); 512 goep_client_t * context = goep_client; 513 goep_client_packet_init(goep_cid, OBEX_OPCODE_PUT | OBEX_OPCODE_FINAL_BIT_MASK); 514 515 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 516 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 517 obex_message_builder_request_create_put(buffer, buffer_len, context->obex_connection_id); 518 } 519 520 void goep_client_request_create_set_path(uint16_t goep_cid, uint8_t flags){ 521 UNUSED(goep_cid); 522 goep_client_t * context = goep_client; 523 goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH); 524 525 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 526 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 527 obex_message_builder_request_create_set_path(buffer, buffer_len, flags, context->obex_connection_id); 528 } 529 530 void goep_client_request_create_abort(uint16_t goep_cid){ 531 UNUSED(goep_cid); 532 goep_client_t * context = goep_client; 533 goep_client_packet_init(goep_cid, OBEX_OPCODE_ABORT); 534 535 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 536 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 537 obex_message_builder_request_create_abort(buffer, buffer_len, context->obex_connection_id); 538 } 539 540 void goep_client_request_create_disconnect(uint16_t goep_cid){ 541 UNUSED(goep_cid); 542 goep_client_t * context = goep_client; 543 goep_client_packet_init(goep_cid, OBEX_OPCODE_DISCONNECT); 544 545 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 546 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 547 obex_message_builder_request_create_disconnect(buffer, buffer_len, context->obex_connection_id); 548 } 549 550 void goep_client_header_add_byte(uint16_t goep_cid, uint8_t header_type, uint8_t value){ 551 UNUSED(goep_cid); 552 goep_client_t * context = goep_client; 553 554 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 555 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 556 obex_message_builder_header_add_byte(buffer, buffer_len, header_type, value); 557 } 558 559 void goep_client_header_add_word(uint16_t goep_cid, uint8_t header_type, uint32_t value){ 560 UNUSED(goep_cid); 561 goep_client_t * context = goep_client; 562 563 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 564 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 565 obex_message_builder_header_add_word(buffer, buffer_len, header_type, value); 566 } 567 568 void goep_client_header_add_variable(uint16_t goep_cid, uint8_t header_type, const uint8_t * header_data, uint16_t header_data_length){ 569 UNUSED(goep_cid); 570 goep_client_t * context = goep_client; 571 572 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 573 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 574 obex_message_builder_header_add_variable(buffer, buffer_len, header_type, header_data, header_data_length); 575 } 576 577 void goep_client_header_add_srm_enable(uint16_t goep_cid){ 578 UNUSED(goep_cid); 579 goep_client_t * context = goep_client; 580 581 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 582 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 583 obex_message_builder_header_add_srm_enable(buffer, buffer_len); 584 } 585 586 void goep_client_header_add_target(uint16_t goep_cid, const uint8_t * target, uint16_t length){ 587 UNUSED(goep_cid); 588 goep_client_t * context = goep_client; 589 590 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 591 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 592 obex_message_builder_header_add_target(buffer, buffer_len, target, length); 593 } 594 595 void goep_client_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 596 UNUSED(goep_cid); 597 goep_client_t * context = goep_client; 598 599 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 600 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 601 obex_message_builder_header_add_application_parameters(buffer, buffer_len, data, length); 602 } 603 604 void goep_client_header_add_challenge_response(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 605 UNUSED(goep_cid); 606 goep_client_t * context = goep_client; 607 608 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 609 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 610 obex_message_builder_header_add_challenge_response(buffer, buffer_len, data, length); 611 } 612 613 void goep_client_body_add_static(uint16_t goep_cid, const uint8_t * data, uint32_t length){ 614 UNUSED(goep_cid); 615 goep_client_t * context = goep_client; 616 617 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 618 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 619 obex_message_builder_body_add_static(buffer, buffer_len, data, length); 620 } 621 622 uint16_t goep_client_body_get_outgoing_buffer_len(uint16_t goep_cid) { 623 UNUSED(goep_cid); 624 goep_client_t * context = goep_client; 625 626 return goep_client_get_outgoing_buffer_len(context); 627 }; 628 629 void goep_client_body_fillup_static(uint16_t goep_cid, const uint8_t * data, uint32_t length, uint32_t * ret_length){ 630 UNUSED(goep_cid); 631 goep_client_t * context = goep_client; 632 633 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 634 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 635 obex_message_builder_body_fillup_static(buffer, buffer_len, data, length, ret_length); 636 } 637 638 void goep_client_header_add_name(uint16_t goep_cid, const char * name){ 639 UNUSED(goep_cid); 640 goep_client_t * context = goep_client; 641 642 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 643 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 644 obex_message_builder_header_add_name(buffer, buffer_len, name); 645 } 646 647 void goep_client_header_add_name_prefix(uint16_t goep_cid, const char * name, uint16_t name_len){ 648 UNUSED(goep_cid); 649 goep_client_t * context = goep_client; 650 651 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 652 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 653 obex_message_builder_header_add_name_prefix(buffer, buffer_len, name, name_len); 654 } 655 656 void goep_client_header_add_type(uint16_t goep_cid, const char * type){ 657 UNUSED(goep_cid); 658 goep_client_t * context = goep_client; 659 660 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 661 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 662 obex_message_builder_header_add_type(buffer, buffer_len, type); 663 } 664 665 void goep_client_header_add_length(uint16_t goep_cid, uint32_t length){ 666 UNUSED(goep_cid); 667 goep_client_t * context = goep_client; 668 669 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 670 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 671 obex_message_builder_header_add_length(buffer, buffer_len, length); 672 } 673 674 uint16_t goep_client_request_get_max_body_size(uint16_t goep_cid){ 675 UNUSED(goep_cid); 676 goep_client_t * context = goep_client; 677 678 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 679 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 680 uint16_t pos = big_endian_read_16(buffer, 1); 681 return buffer_len - pos; 682 } 683 684 int goep_client_execute(uint16_t goep_cid){ 685 return goep_client_execute_with_final_bit (goep_cid, true); 686 } 687 688 int goep_client_execute_with_final_bit(uint16_t goep_cid, bool final){ 689 UNUSED(goep_cid); 690 goep_client_t * context = goep_client; 691 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 692 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(context); 693 694 obex_message_builder_set_final_bit (buffer, buffer_len, final); 695 696 uint16_t pos = big_endian_read_16(buffer, 1); 697 if (context->l2cap_psm){ 698 return l2cap_send(context->bearer_cid, buffer, pos); 699 } else { 700 return rfcomm_send_prepared(context->bearer_cid, pos); 701 } 702 } 703 704