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 #ifdef ENABLE_GOEP_L2CAP 63 #ifndef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE 64 #error "ENABLE_GOEP_L2CAP requires ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE. Please enable ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE or disable ENABLE_GOEP_L2CAP" 65 #endif 66 #endif 67 68 static uint16_t goep_client_cid; 69 static btstack_linked_list_t goep_clients; 70 71 static goep_client_t * goep_client_sdp_active; 72 static uint8_t goep_client_sdp_query_attribute_value[30]; 73 static const unsigned int goep_client_sdp_query_attribute_value_buffer_size = sizeof(goep_client_sdp_query_attribute_value); 74 static uint8_t goep_packet_buffer[150]; 75 76 static inline void goep_client_emit_connected_event(goep_client_t * goep_client, uint8_t status){ 77 uint8_t event[15]; 78 int pos = 0; 79 event[pos++] = HCI_EVENT_GOEP_META; 80 pos++; // skip len 81 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 82 little_endian_store_16(event, pos, goep_client->cid); 83 pos+=2; 84 event[pos++] = status; 85 (void)memcpy(&event[pos], goep_client->bd_addr, 6); 86 pos += 6; 87 little_endian_store_16(event, pos, goep_client->con_handle); 88 pos += 2; 89 event[pos++] = goep_client->incoming; 90 event[1] = pos - 2; 91 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 92 goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos); 93 } 94 95 static inline void goep_client_emit_connection_closed_event(goep_client_t * goep_client){ 96 uint8_t event[5]; 97 int pos = 0; 98 event[pos++] = HCI_EVENT_GOEP_META; 99 pos++; // skip len 100 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 101 little_endian_store_16(event, pos, goep_client->cid); 102 pos+=2; 103 event[1] = pos - 2; 104 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 105 goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos); 106 } 107 108 static inline void goep_client_emit_can_send_now_event(goep_client_t * goep_client){ 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_CAN_SEND_NOW; 114 little_endian_store_16(event, pos, goep_client->cid); 115 pos+=2; 116 event[1] = pos - 2; 117 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 118 goep_client->client_handler(HCI_EVENT_PACKET, goep_client->cid, &event[0], pos); 119 } 120 121 static void goep_client_handle_connection_opened(goep_client_t * goep_client, uint8_t status, uint16_t mtu){ 122 if (status) { 123 goep_client->state = GOEP_CLIENT_INIT; 124 btstack_linked_list_remove(&goep_clients, (btstack_linked_item_t *) goep_client); 125 log_info("goep_client: open failed, status %u", status); 126 } else { 127 goep_client->bearer_mtu = mtu; 128 goep_client->state = GOEP_CLIENT_CONNECTED; 129 log_info("goep_client: connection opened. cid %u, max frame size %u", goep_client->bearer_cid, goep_client->bearer_mtu); 130 } 131 goep_client_emit_connected_event(goep_client, status); 132 } 133 134 static void goep_client_handle_connection_close(goep_client_t * goep_client){ 135 goep_client->state = GOEP_CLIENT_INIT; 136 btstack_linked_list_remove(&goep_clients, (btstack_linked_item_t *) goep_client); 137 goep_client_emit_connection_closed_event(goep_client); 138 } 139 140 static goep_client_t * goep_client_for_cid(uint16_t cid){ 141 btstack_linked_list_iterator_t it; 142 btstack_linked_list_iterator_init(&it, &goep_clients); 143 while (btstack_linked_list_iterator_has_next(&it)){ 144 goep_client_t * goep_client = (goep_client_t *) btstack_linked_list_iterator_next(&it); 145 if (goep_client->cid == cid){ 146 return goep_client; 147 } 148 } 149 return NULL; 150 } 151 152 static goep_client_t * goep_client_for_bearer_cid(uint16_t bearer_cid){ 153 btstack_linked_list_iterator_t it; 154 btstack_linked_list_iterator_init(&it, &goep_clients); 155 while (btstack_linked_list_iterator_has_next(&it)){ 156 goep_client_t * goep_client = (goep_client_t *) btstack_linked_list_iterator_next(&it); 157 if (goep_client->bearer_cid == bearer_cid){ 158 return goep_client; 159 } 160 } 161 return NULL; 162 } 163 164 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 165 UNUSED(channel); 166 UNUSED(size); 167 goep_client_t * goep_client; 168 switch (packet_type){ 169 case HCI_EVENT_PACKET: 170 switch (hci_event_packet_get_type(packet)) { 171 #ifdef ENABLE_GOEP_L2CAP 172 case L2CAP_EVENT_CHANNEL_OPENED: 173 goep_client = goep_client_for_bearer_cid(l2cap_event_channel_opened_get_local_cid(packet)); 174 btstack_assert(goep_client != NULL); 175 goep_client_handle_connection_opened(goep_client, l2cap_event_channel_opened_get_status(packet), 176 btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet))); 177 break; 178 case L2CAP_EVENT_CAN_SEND_NOW: 179 goep_client = goep_client_for_bearer_cid(l2cap_event_can_send_now_get_local_cid(packet)); 180 btstack_assert(goep_client != NULL); 181 goep_client_emit_can_send_now_event(goep_client); 182 break; 183 case L2CAP_EVENT_CHANNEL_CLOSED: 184 goep_client = goep_client_for_bearer_cid(l2cap_event_channel_closed_get_local_cid(packet)); 185 btstack_assert(goep_client != NULL); 186 goep_client_handle_connection_close(goep_client); 187 break; 188 #endif 189 case RFCOMM_EVENT_CHANNEL_OPENED: 190 goep_client = goep_client_for_bearer_cid(rfcomm_event_channel_opened_get_rfcomm_cid(packet)); 191 btstack_assert(goep_client != NULL); 192 goep_client_handle_connection_opened(goep_client, 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 = goep_client_for_bearer_cid(rfcomm_event_can_send_now_get_rfcomm_cid(packet)); 196 btstack_assert(goep_client != NULL); 197 goep_client_emit_can_send_now_event(goep_client); 198 break; 199 case RFCOMM_EVENT_CHANNEL_CLOSED: 200 goep_client = goep_client_for_bearer_cid(rfcomm_event_channel_closed_get_rfcomm_cid(packet)); 201 btstack_assert(goep_client != NULL); 202 goep_client_handle_connection_close(goep_client); 203 break; 204 default: 205 break; 206 } 207 break; 208 case L2CAP_DATA_PACKET: 209 case RFCOMM_DATA_PACKET: 210 goep_client = goep_client_for_bearer_cid(channel); 211 btstack_assert(goep_client != NULL); 212 goep_client->client_handler(GOEP_DATA_PACKET, goep_client->cid, packet, size); 213 break; 214 default: 215 break; 216 } 217 } 218 219 static void goep_client_handle_sdp_query_end_of_record(goep_client_t * goep_client){ 220 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER){ 221 if (goep_client->mas_info.instance_id == goep_client->map_mas_instance_id){ 222 // Requested MAS Instance found, accept info 223 goep_client->rfcomm_port = goep_client->mas_info.rfcomm_port; 224 goep_client->profile_supported_features = goep_client->mas_info.supported_features; 225 goep_client->map_supported_message_types = goep_client->mas_info.supported_message_types; 226 #ifdef ENABLE_GOEP_L2CAP 227 goep_client->l2cap_psm = goep_client->mas_info.l2cap_psm; 228 log_info("MAS Instance #%u found, rfcomm #%u, l2cap 0x%04x", goep_client->map_mas_instance_id, 229 goep_client->rfcomm_port, goep_client->l2cap_psm); 230 #else 231 log_info("MAS Instance #%u found, rfcomm #%u", goep_client->map_mas_instance_id, 232 goep_client->rfcomm_port); 233 #endif 234 } 235 } 236 } 237 static uint8_t goep_client_start_connect(goep_client_t * goep_client){ 238 #ifdef ENABLE_GOEP_L2CAP 239 if (goep_client->l2cap_psm && (goep_client->ertm_buffer != NULL)){ 240 log_info("Remote GOEP L2CAP PSM: %u", goep_client->l2cap_psm); 241 return l2cap_ertm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->l2cap_psm, 242 &goep_client->ertm_config, goep_client->ertm_buffer, 243 goep_client->ertm_buffer_size, &goep_client->bearer_cid); 244 } 245 #endif 246 log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->rfcomm_port); 247 return rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->rfcomm_port, &goep_client->bearer_cid); 248 } 249 250 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 251 goep_client_t * goep_client = goep_client_sdp_active; 252 btstack_assert(goep_client != NULL); 253 254 UNUSED(packet_type); 255 UNUSED(channel); 256 UNUSED(size); 257 258 des_iterator_t des_list_it; 259 des_iterator_t prot_it; 260 uint8_t status; 261 uint16_t record_index; 262 bool goep_server_found; 263 264 switch (hci_event_packet_get_type(packet)){ 265 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 266 267 // detect new record 268 record_index = sdp_event_query_attribute_byte_get_record_id(packet); 269 if (record_index != goep_client->record_index){ 270 goep_client->record_index = record_index; 271 goep_client_handle_sdp_query_end_of_record(goep_client); 272 memset(&goep_client->mas_info, 0, sizeof(goep_client->mas_info)); 273 } 274 275 // check if relevant attribute 276 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 277 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 278 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 279 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID: 280 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES: 281 #ifdef ENABLE_GOEP_L2CAP 282 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 283 #endif 284 break; 285 default: 286 return; 287 } 288 289 // warn if attribute too large to fit in our buffer 290 if (sdp_event_query_attribute_byte_get_attribute_length(packet) > goep_client_sdp_query_attribute_value_buffer_size) { 291 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)); 292 break; 293 } 294 295 // store single byte 296 goep_client_sdp_query_attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 297 298 // wait until value fully received 299 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break; 300 301 // process attributes 302 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 303 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 304 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)) { 305 uint8_t *des_element; 306 uint8_t *element; 307 uint32_t uuid; 308 309 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 310 311 des_element = des_iterator_get_element(&des_list_it); 312 des_iterator_init(&prot_it, des_element); 313 314 // first element is UUID 315 element = des_iterator_get_element(&prot_it); 316 if (de_get_element_type(element) != DE_UUID) continue; 317 318 uuid = de_get_uuid32(element); 319 des_iterator_next(&prot_it); 320 if (!des_iterator_has_more(&prot_it)) continue; 321 322 // second element is RFCOMM server channel or L2CAP PSM 323 element = des_iterator_get_element(&prot_it); 324 if (uuid == BLUETOOTH_PROTOCOL_RFCOMM){ 325 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) { 326 goep_client->mas_info.rfcomm_port = element[de_get_header_size(element)]; 327 } else { 328 goep_client->rfcomm_port = element[de_get_header_size(element)]; 329 } 330 } 331 } 332 break; 333 #ifdef ENABLE_GOEP_L2CAP 334 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 335 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER){ 336 de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->mas_info.l2cap_psm); 337 } else { 338 de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->l2cap_psm); 339 } 340 break; 341 #endif 342 // BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES == BLUETOOTH_ATTRIBUTE_MAP_SUPPORTED_FEATURES == 0x0317 343 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 344 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 345 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_32) break; 346 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) { 347 goep_client->mas_info.supported_features = big_endian_read_32(goep_client_sdp_query_attribute_value, de_get_header_size(goep_client_sdp_query_attribute_value)); 348 } else { 349 goep_client->profile_supported_features = big_endian_read_32(goep_client_sdp_query_attribute_value, de_get_header_size(goep_client_sdp_query_attribute_value)); 350 } 351 break; 352 353 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID: 354 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 355 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 356 goep_client->mas_info.instance_id = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 357 break; 358 359 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES: 360 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 361 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 362 goep_client->mas_info.supported_message_types = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 363 break; 364 365 default: 366 break; 367 } 368 break; 369 370 case SDP_EVENT_QUERY_COMPLETE: 371 goep_client_sdp_active = NULL; 372 goep_client_handle_sdp_query_end_of_record(goep_client); 373 status = sdp_event_query_complete_get_status(packet); 374 if (status != ERROR_CODE_SUCCESS){ 375 log_info("GOEP client, SDP query failed 0x%02x", status); 376 goep_client_handle_connection_opened(goep_client, status, 0); 377 break; 378 } 379 goep_server_found = false; 380 if (goep_client->rfcomm_port != 0){ 381 goep_server_found = true; 382 } 383 #ifdef ENABLE_GOEP_L2CAP 384 if (goep_client->l2cap_psm != 0){ 385 goep_server_found = true; 386 } 387 #endif 388 if (goep_server_found == false){ 389 log_info("No GOEP RFCOMM or L2CAP server found"); 390 goep_client_handle_connection_opened(goep_client, SDP_SERVICE_NOT_FOUND, 0); 391 break; 392 } 393 (void) goep_client_start_connect(goep_client); 394 break; 395 396 default: 397 break; 398 } 399 } 400 401 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * goep_client){ 402 if (goep_client->l2cap_psm){ 403 return goep_packet_buffer; 404 } else { 405 return rfcomm_get_outgoing_buffer(); 406 } 407 } 408 409 static uint16_t goep_client_get_outgoing_buffer_len(goep_client_t * goep_client){ 410 if (goep_client->l2cap_psm){ 411 return sizeof(goep_packet_buffer); 412 } else { 413 return rfcomm_get_max_frame_size(goep_client->bearer_cid); 414 } 415 } 416 417 static void goep_client_packet_init(goep_client_t *goep_client, uint8_t opcode) { 418 if (goep_client->l2cap_psm != 0){ 419 } else { 420 rfcomm_reserve_packet_buffer(); 421 } 422 // store opcode for parsing of response 423 goep_client->obex_opcode = opcode; 424 } 425 426 void goep_client_init(void){ 427 } 428 429 void goep_client_deinit(void){ 430 goep_clients = NULL; 431 goep_client_cid = 0; 432 memset(goep_client_sdp_query_attribute_value, 0, sizeof(goep_client_sdp_query_attribute_value)); 433 memset(goep_packet_buffer, 0, sizeof(goep_packet_buffer)); 434 } 435 436 static void geop_client_sdp_query_start(void * context){ 437 uint16_t goep_cid = (uint16_t)(uintptr_t) context; 438 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 439 if (context != NULL){ 440 goep_client_sdp_active = goep_client; 441 sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, goep_client->bd_addr, 442 goep_client->uuid); 443 } 444 } 445 446 uint8_t 447 goep_client_connect(goep_client_t *goep_client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 448 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, 449 uint8_t instance_id, uint16_t *out_cid) { 450 btstack_assert(goep_client != NULL); 451 452 // get new goep cid, skip 0x0000 453 goep_client_cid++; 454 if (goep_client_cid == 0) { 455 goep_client_cid = 1; 456 } 457 458 // add to list 459 memset(goep_client, 0, sizeof(goep_client_t)); 460 goep_client->state = GOEP_CLIENT_W4_SDP; 461 goep_client->cid = goep_client_cid; 462 goep_client->client_handler = handler; 463 goep_client->uuid = uuid; 464 goep_client->map_mas_instance_id = instance_id; 465 goep_client->profile_supported_features = PROFILE_FEATURES_NOT_PRESENT; 466 (void)memcpy(goep_client->bd_addr, addr, 6); 467 goep_client->sdp_query_request.callback = geop_client_sdp_query_start; 468 goep_client->sdp_query_request.context = (void *)(uintptr_t) goep_client->cid; 469 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 470 #ifdef ENABLE_GOEP_L2CAP 471 if (l2cap_ertm_config != NULL){ 472 memcpy(&goep_client->ertm_config, l2cap_ertm_config, sizeof(l2cap_ertm_config_t)); 473 } 474 goep_client->ertm_buffer_size = l2cap_ertm_buffer_size; 475 goep_client->ertm_buffer = l2cap_ertm_buffer; 476 #endif 477 btstack_linked_list_add(&goep_clients, (btstack_linked_item_t *) goep_client); 478 479 // request sdp query 480 sdp_client_register_query_callback(&goep_client->sdp_query_request); 481 482 *out_cid = goep_client->cid; 483 return ERROR_CODE_SUCCESS; 484 } 485 486 #ifdef ENABLE_GOEP_L2CAP 487 uint8_t goep_client_connect_l2cap(goep_client_t *goep_client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 488 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t l2cap_psm, 489 uint16_t *out_cid){ 490 btstack_assert(goep_client != NULL); 491 btstack_assert(l2cap_ertm_config != NULL); 492 btstack_assert(l2cap_ertm_buffer != NULL); 493 494 // get new goep cid, skip 0x0000 495 goep_client_cid++; 496 if (goep_client_cid == 0) { 497 goep_client_cid = 1; 498 } 499 500 // add to list 501 memset(goep_client, 0, sizeof(goep_client_t)); 502 goep_client->state = GOEP_CLIENT_W4_SDP; 503 goep_client->cid = goep_client_cid; 504 goep_client->client_handler = handler; 505 goep_client->profile_supported_features = PROFILE_FEATURES_NOT_PRESENT; 506 (void)memcpy(goep_client->bd_addr, addr, 6); 507 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 508 memcpy(&goep_client->ertm_config, l2cap_ertm_config, sizeof(l2cap_ertm_config_t)); 509 goep_client->ertm_buffer_size = l2cap_ertm_buffer_size; 510 goep_client->ertm_buffer = l2cap_ertm_buffer; 511 btstack_linked_list_add_tail(&goep_clients, (btstack_linked_item_t *) goep_client); 512 513 goep_client->l2cap_psm = l2cap_psm; 514 515 *out_cid = goep_client->cid; 516 517 return goep_client_start_connect(goep_client); 518 } 519 #endif 520 521 uint32_t goep_client_get_pbap_supported_features(uint16_t goep_cid){ 522 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 523 if (goep_client == NULL){ 524 return PBAP_FEATURES_NOT_PRESENT; 525 } 526 return goep_client->profile_supported_features; 527 } 528 529 uint32_t goep_client_get_map_supported_features(uint16_t goep_cid){ 530 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 531 if (goep_client == NULL){ 532 return PROFILE_FEATURES_NOT_PRESENT; 533 } 534 return goep_client->profile_supported_features; 535 } 536 537 uint8_t goep_client_get_map_mas_instance_id(uint16_t goep_cid){ 538 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 539 if (goep_client == NULL){ 540 return 0; 541 } 542 return goep_client->map_mas_instance_id; 543 } 544 545 uint8_t goep_client_get_map_supported_message_types(uint16_t goep_cid){ 546 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 547 if (goep_client == NULL){ 548 return 0; 549 } 550 return goep_client->map_supported_message_types; 551 } 552 553 554 bool goep_client_version_20_or_higher(uint16_t goep_cid){ 555 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 556 if (goep_client == NULL){ 557 return false; 558 } 559 return goep_client->l2cap_psm != 0; 560 } 561 562 void goep_client_request_can_send_now(uint16_t goep_cid){ 563 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 564 if (goep_client == NULL){ 565 return; 566 } 567 if (goep_client->l2cap_psm){ 568 l2cap_request_can_send_now_event(goep_client->bearer_cid); 569 } else { 570 rfcomm_request_can_send_now_event(goep_client->bearer_cid); 571 } 572 } 573 574 uint8_t goep_client_disconnect(uint16_t goep_cid){ 575 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 576 if (goep_client == NULL){ 577 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 578 } 579 if (goep_client->l2cap_psm){ 580 l2cap_disconnect(goep_client->bearer_cid); 581 } else { 582 rfcomm_disconnect(goep_client->bearer_cid); 583 } 584 return ERROR_CODE_SUCCESS; 585 } 586 587 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 588 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 589 if (goep_client == NULL){ 590 return; 591 } 592 goep_client->obex_connection_id = connection_id; 593 } 594 595 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 596 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 597 if (goep_client == NULL){ 598 return 0; 599 } 600 return goep_client->obex_opcode; 601 } 602 603 void goep_client_request_create_connect(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 604 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 605 if (goep_client == NULL){ 606 return; 607 } 608 goep_client_packet_init(goep_client, OBEX_OPCODE_CONNECT); 609 // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets 610 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu); 611 612 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 613 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 614 obex_message_builder_request_create_connect(buffer, buffer_len, obex_version_number, flags, maximum_obex_packet_length); 615 } 616 617 void goep_client_request_create_get(uint16_t goep_cid){ 618 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 619 if (goep_client == NULL){ 620 return; 621 } 622 goep_client_packet_init(goep_client, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 623 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 624 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 625 obex_message_builder_request_create_get(buffer, buffer_len, goep_client->obex_connection_id); 626 } 627 628 void goep_client_request_create_put(uint16_t goep_cid){ 629 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 630 if (goep_client == NULL){ 631 return; 632 } 633 goep_client_packet_init(goep_client, OBEX_OPCODE_PUT | OBEX_OPCODE_FINAL_BIT_MASK); 634 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 635 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 636 obex_message_builder_request_create_put(buffer, buffer_len, goep_client->obex_connection_id); 637 } 638 639 void goep_client_request_create_set_path(uint16_t goep_cid, uint8_t flags){ 640 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 641 if (goep_client == NULL){ 642 return; 643 } 644 goep_client_packet_init(goep_client, OBEX_OPCODE_SETPATH); 645 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 646 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 647 obex_message_builder_request_create_set_path(buffer, buffer_len, flags, goep_client->obex_connection_id); 648 } 649 650 void goep_client_request_create_abort(uint16_t goep_cid){ 651 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 652 if (goep_client == NULL){ 653 return; 654 } 655 goep_client_packet_init(goep_client, OBEX_OPCODE_ABORT); 656 657 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 658 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 659 obex_message_builder_request_create_abort(buffer, buffer_len, goep_client->obex_connection_id); 660 } 661 662 void goep_client_request_create_disconnect(uint16_t goep_cid){ 663 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 664 if (goep_client == NULL){ 665 return; 666 } 667 goep_client_packet_init(goep_client, OBEX_OPCODE_DISCONNECT); 668 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 669 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 670 obex_message_builder_request_create_disconnect(buffer, buffer_len, goep_client->obex_connection_id); 671 } 672 673 void goep_client_header_add_byte(uint16_t goep_cid, uint8_t header_type, uint8_t value){ 674 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 675 if (goep_client == NULL){ 676 return; 677 } 678 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 679 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 680 obex_message_builder_header_add_byte(buffer, buffer_len, header_type, value); 681 } 682 683 void goep_client_header_add_word(uint16_t goep_cid, uint8_t header_type, uint32_t value){ 684 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 685 if (goep_client == NULL){ 686 return; 687 } 688 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 689 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 690 obex_message_builder_header_add_word(buffer, buffer_len, header_type, value); 691 } 692 693 void goep_client_header_add_variable(uint16_t goep_cid, uint8_t header_type, const uint8_t * header_data, uint16_t header_data_length){ 694 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 695 if (goep_client == NULL){ 696 return; 697 } 698 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 699 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 700 obex_message_builder_header_add_variable(buffer, buffer_len, header_type, header_data, header_data_length); 701 } 702 703 void goep_client_header_add_srm_enable(uint16_t goep_cid){ 704 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 705 if (goep_client == NULL){ 706 return; 707 } 708 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 709 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 710 obex_message_builder_header_add_srm_enable(buffer, buffer_len); 711 } 712 713 void goep_client_header_add_target(uint16_t goep_cid, const uint8_t * target, uint16_t length){ 714 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 715 if (goep_client == NULL){ 716 return; 717 } 718 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 719 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 720 obex_message_builder_header_add_target(buffer, buffer_len, target, length); 721 } 722 723 void goep_client_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 724 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 725 if (goep_client == NULL){ 726 return; 727 } 728 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 729 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 730 obex_message_builder_header_add_application_parameters(buffer, buffer_len, data, length); 731 } 732 733 void goep_client_header_add_challenge_response(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 734 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 735 if (goep_client == NULL){ 736 return; 737 } 738 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 739 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 740 obex_message_builder_header_add_challenge_response(buffer, buffer_len, data, length); 741 } 742 743 void goep_client_body_add_static(uint16_t goep_cid, const uint8_t * data, uint32_t length){ 744 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 745 if (goep_client == NULL){ 746 return; 747 } 748 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 749 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 750 obex_message_builder_body_add_static(buffer, buffer_len, data, length); 751 } 752 753 uint16_t goep_client_body_get_outgoing_buffer_len(uint16_t goep_cid) { 754 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 755 if (goep_client == NULL){ 756 return 0; 757 } 758 return goep_client_get_outgoing_buffer_len(goep_client); 759 }; 760 761 void goep_client_body_fillup_static(uint16_t goep_cid, const uint8_t * data, uint32_t length, uint32_t * ret_length){ 762 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 763 if (goep_client == NULL){ 764 return; 765 } 766 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 767 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 768 obex_message_builder_body_fillup_static(buffer, buffer_len, data, length, ret_length); 769 } 770 771 void goep_client_header_add_name(uint16_t goep_cid, const char * name){ 772 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 773 if (goep_client == NULL){ 774 return; 775 } 776 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 777 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 778 obex_message_builder_header_add_name(buffer, buffer_len, name); 779 } 780 781 void goep_client_header_add_name_prefix(uint16_t goep_cid, const char * name, uint16_t name_len){ 782 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 783 if (goep_client == NULL){ 784 return; 785 } 786 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 787 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 788 obex_message_builder_header_add_name_prefix(buffer, buffer_len, name, name_len); 789 } 790 791 void goep_client_header_add_unicode_prefix(uint16_t goep_cid, uint8_t header_id, const char * name, uint16_t name_len){ 792 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 793 if (goep_client == NULL){ 794 return; 795 } 796 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 797 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 798 obex_message_builder_header_add_unicode_prefix(buffer, buffer_len, header_id, name, name_len); 799 } 800 801 void goep_client_header_add_type(uint16_t goep_cid, const char * type){ 802 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 803 if (goep_client == NULL){ 804 return; 805 } 806 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 807 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 808 obex_message_builder_header_add_type(buffer, buffer_len, type); 809 } 810 811 void goep_client_header_add_length(uint16_t goep_cid, uint32_t length){ 812 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 813 if (goep_client == NULL){ 814 return; 815 } 816 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 817 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 818 obex_message_builder_header_add_length(buffer, buffer_len, length); 819 } 820 821 uint16_t goep_client_request_get_max_body_size(uint16_t goep_cid){ 822 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 823 if (goep_client == NULL){ 824 return 0; 825 } 826 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 827 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 828 uint16_t pos = big_endian_read_16(buffer, 1); 829 return buffer_len - pos; 830 } 831 832 int goep_client_execute(uint16_t goep_cid){ 833 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 834 if (goep_client == NULL){ 835 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 836 } 837 return goep_client_execute_with_final_bit (goep_cid, true); 838 } 839 840 int goep_client_execute_with_final_bit(uint16_t goep_cid, bool final){ 841 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 842 if (goep_client == NULL){ 843 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 844 } 845 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 846 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 847 848 obex_message_builder_set_final_bit (buffer, buffer_len, final); 849 850 uint16_t pos = big_endian_read_16(buffer, 1); 851 if (goep_client->l2cap_psm){ 852 return l2cap_send(goep_client->bearer_cid, buffer, pos); 853 } else { 854 return rfcomm_send_prepared(goep_client->bearer_cid, pos); 855 } 856 } 857 858