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 // use matching SDP record with highest MAP version as there might be additional records for backwards compatibility 222 if (goep_client->mas_info.instance_id == goep_client->map_mas_instance_id){ 223 if (goep_client->mas_info.version > goep_client->map_version){ 224 // Requested MAS Instance found, accept info 225 goep_client->rfcomm_port = goep_client->mas_info.rfcomm_port; 226 goep_client->profile_supported_features = goep_client->mas_info.supported_features; 227 goep_client->map_supported_message_types = goep_client->mas_info.supported_message_types; 228 #ifdef ENABLE_GOEP_L2CAP 229 goep_client->l2cap_psm = goep_client->mas_info.l2cap_psm; 230 goep_client->map_version = goep_client->mas_info.version; 231 log_info("MAS Instance #%u found, version %u.%u: rfcomm #%u, l2cap 0x%04x ", goep_client->map_mas_instance_id, 232 goep_client->map_version, goep_client->map_version, goep_client->rfcomm_port, goep_client->l2cap_psm); 233 #else 234 log_info("MAS Instance #%u found, rfcomm #%u", goep_client->map_mas_instance_id, 235 goep_client->rfcomm_port); 236 #endif 237 } 238 } 239 } 240 } 241 static uint8_t goep_client_start_connect(goep_client_t * goep_client){ 242 #ifdef ENABLE_GOEP_L2CAP 243 if (goep_client->l2cap_psm && (goep_client->ertm_buffer != NULL)){ 244 log_info("Remote GOEP L2CAP PSM: %u", goep_client->l2cap_psm); 245 return l2cap_ertm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->l2cap_psm, 246 &goep_client->ertm_config, goep_client->ertm_buffer, 247 goep_client->ertm_buffer_size, &goep_client->bearer_cid); 248 } 249 #endif 250 log_info("Remote GOEP RFCOMM Server Channel: %u", goep_client->rfcomm_port); 251 return rfcomm_create_channel(&goep_client_packet_handler, goep_client->bd_addr, goep_client->rfcomm_port, &goep_client->bearer_cid); 252 } 253 254 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 255 goep_client_t * goep_client = goep_client_sdp_active; 256 btstack_assert(goep_client != NULL); 257 258 UNUSED(packet_type); 259 UNUSED(channel); 260 UNUSED(size); 261 262 des_iterator_t des_list_it; 263 des_iterator_t prot_it; 264 uint8_t status; 265 uint16_t record_index; 266 bool goep_server_found; 267 268 switch (hci_event_packet_get_type(packet)){ 269 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 270 271 // detect new record 272 record_index = sdp_event_query_attribute_byte_get_record_id(packet); 273 if (record_index != goep_client->record_index){ 274 goep_client->record_index = record_index; 275 goep_client_handle_sdp_query_end_of_record(goep_client); 276 memset(&goep_client->mas_info, 0, sizeof(goep_client->mas_info)); 277 } 278 279 // check if relevant attribute 280 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 281 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 282 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 283 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID: 284 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES: 285 #ifdef ENABLE_GOEP_L2CAP 286 case BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST: 287 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 288 #endif 289 break; 290 default: 291 return; 292 } 293 294 // warn if attribute too large to fit in our buffer 295 if (sdp_event_query_attribute_byte_get_attribute_length(packet) > goep_client_sdp_query_attribute_value_buffer_size) { 296 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)); 297 break; 298 } 299 300 // store single byte 301 goep_client_sdp_query_attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 302 303 // wait until value fully received 304 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break; 305 306 // process attributes 307 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 308 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 309 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)) { 310 uint8_t *des_element; 311 uint8_t *element; 312 uint32_t uuid; 313 314 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 315 316 des_element = des_iterator_get_element(&des_list_it); 317 des_iterator_init(&prot_it, des_element); 318 319 // first element is UUID 320 element = des_iterator_get_element(&prot_it); 321 if (de_get_element_type(element) != DE_UUID) continue; 322 323 uuid = de_get_uuid32(element); 324 des_iterator_next(&prot_it); 325 if (!des_iterator_has_more(&prot_it)) continue; 326 327 // second element is RFCOMM server channel or L2CAP PSM 328 element = des_iterator_get_element(&prot_it); 329 if (uuid == BLUETOOTH_PROTOCOL_RFCOMM){ 330 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) { 331 goep_client->mas_info.rfcomm_port = element[de_get_header_size(element)]; 332 } else { 333 goep_client->rfcomm_port = element[de_get_header_size(element)]; 334 } 335 } 336 } 337 break; 338 #ifdef ENABLE_GOEP_L2CAP 339 340 case BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST: 341 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)) { 342 uint8_t *des_element; 343 uint8_t *element; 344 uint32_t uuid; 345 346 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 347 348 des_element = des_iterator_get_element(&des_list_it); 349 des_iterator_init(&prot_it, des_element); 350 element = des_iterator_get_element(&prot_it); 351 352 if (de_get_element_type(element) != DE_UUID) continue; 353 354 uuid = de_get_uuid32(element); 355 des_iterator_next(&prot_it); 356 switch (uuid){ 357 case BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_PROFILE: 358 if (!des_iterator_has_more(&prot_it)) continue; 359 de_element_get_uint16(des_iterator_get_element(&prot_it), &goep_client->mas_info.version); 360 break; 361 default: 362 break; 363 } 364 } 365 break; 366 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 367 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER){ 368 de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->mas_info.l2cap_psm); 369 } else { 370 de_element_get_uint16(goep_client_sdp_query_attribute_value, &goep_client->l2cap_psm); 371 } 372 break; 373 #endif 374 // BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES == BLUETOOTH_ATTRIBUTE_MAP_SUPPORTED_FEATURES == 0x0317 375 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 376 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 377 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_32) break; 378 if (goep_client->uuid == BLUETOOTH_SERVICE_CLASS_MESSAGE_ACCESS_SERVER) { 379 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)); 380 } else { 381 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)); 382 } 383 break; 384 385 case BLUETOOTH_ATTRIBUTE_MAS_INSTANCE_ID: 386 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 387 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 388 goep_client->mas_info.instance_id = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 389 break; 390 391 case BLUETOOTH_ATTRIBUTE_SUPPORTED_MESSAGE_TYPES: 392 if (de_get_element_type(goep_client_sdp_query_attribute_value) != DE_UINT) break; 393 if (de_get_size_type(goep_client_sdp_query_attribute_value) != DE_SIZE_8) break; 394 goep_client->mas_info.supported_message_types = goep_client_sdp_query_attribute_value[de_get_header_size(goep_client_sdp_query_attribute_value)]; 395 break; 396 397 default: 398 break; 399 } 400 break; 401 402 case SDP_EVENT_QUERY_COMPLETE: 403 goep_client_sdp_active = NULL; 404 goep_client_handle_sdp_query_end_of_record(goep_client); 405 status = sdp_event_query_complete_get_status(packet); 406 if (status != ERROR_CODE_SUCCESS){ 407 log_info("GOEP client, SDP query failed 0x%02x", status); 408 goep_client_handle_connection_opened(goep_client, status, 0); 409 break; 410 } 411 goep_server_found = false; 412 if (goep_client->rfcomm_port != 0){ 413 goep_server_found = true; 414 } 415 #ifdef ENABLE_GOEP_L2CAP 416 if (goep_client->l2cap_psm != 0){ 417 goep_server_found = true; 418 } 419 #endif 420 if (goep_server_found == false){ 421 log_info("No GOEP RFCOMM or L2CAP server found"); 422 goep_client_handle_connection_opened(goep_client, SDP_SERVICE_NOT_FOUND, 0); 423 break; 424 } 425 (void) goep_client_start_connect(goep_client); 426 break; 427 428 default: 429 break; 430 } 431 } 432 433 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * goep_client){ 434 if (goep_client->l2cap_psm){ 435 return goep_packet_buffer; 436 } else { 437 return rfcomm_get_outgoing_buffer(); 438 } 439 } 440 441 static uint16_t goep_client_get_outgoing_buffer_len(goep_client_t * goep_client){ 442 if (goep_client->l2cap_psm){ 443 return sizeof(goep_packet_buffer); 444 } else { 445 return rfcomm_get_max_frame_size(goep_client->bearer_cid); 446 } 447 } 448 449 static void goep_client_packet_init(goep_client_t *goep_client, uint8_t opcode) { 450 if (goep_client->l2cap_psm != 0){ 451 } else { 452 rfcomm_reserve_packet_buffer(); 453 } 454 // store opcode for parsing of response 455 goep_client->obex_opcode = opcode; 456 } 457 458 void goep_client_init(void){ 459 } 460 461 void goep_client_deinit(void){ 462 goep_clients = NULL; 463 goep_client_cid = 0; 464 memset(goep_client_sdp_query_attribute_value, 0, sizeof(goep_client_sdp_query_attribute_value)); 465 memset(goep_packet_buffer, 0, sizeof(goep_packet_buffer)); 466 } 467 468 static void geop_client_sdp_query_start(void * context){ 469 uint16_t goep_cid = (uint16_t)(uintptr_t) context; 470 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 471 if (context != NULL){ 472 goep_client_sdp_active = goep_client; 473 sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, goep_client->bd_addr, 474 goep_client->uuid); 475 } 476 } 477 478 uint8_t 479 goep_client_connect(goep_client_t *goep_client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 480 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, 481 uint8_t instance_id, uint16_t *out_cid) { 482 btstack_assert(goep_client != NULL); 483 484 // get new goep cid, skip 0x0000 485 goep_client_cid++; 486 if (goep_client_cid == 0) { 487 goep_client_cid = 1; 488 } 489 490 // add to list 491 memset(goep_client, 0, sizeof(goep_client_t)); 492 goep_client->state = GOEP_CLIENT_W4_SDP; 493 goep_client->cid = goep_client_cid; 494 goep_client->client_handler = handler; 495 goep_client->uuid = uuid; 496 goep_client->map_mas_instance_id = instance_id; 497 goep_client->profile_supported_features = PROFILE_FEATURES_NOT_PRESENT; 498 (void)memcpy(goep_client->bd_addr, addr, 6); 499 goep_client->sdp_query_request.callback = geop_client_sdp_query_start; 500 goep_client->sdp_query_request.context = (void *)(uintptr_t) goep_client->cid; 501 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 502 #ifdef ENABLE_GOEP_L2CAP 503 if (l2cap_ertm_config != NULL){ 504 memcpy(&goep_client->ertm_config, l2cap_ertm_config, sizeof(l2cap_ertm_config_t)); 505 } 506 goep_client->ertm_buffer_size = l2cap_ertm_buffer_size; 507 goep_client->ertm_buffer = l2cap_ertm_buffer; 508 #else 509 UNUSED(l2cap_ertm_buffer); 510 UNUSED(l2cap_ertm_config); 511 UNUSED(l2cap_ertm_buffer_size); 512 #endif 513 btstack_linked_list_add(&goep_clients, (btstack_linked_item_t *) goep_client); 514 515 // request sdp query 516 sdp_client_register_query_callback(&goep_client->sdp_query_request); 517 518 *out_cid = goep_client->cid; 519 return ERROR_CODE_SUCCESS; 520 } 521 522 #ifdef ENABLE_GOEP_L2CAP 523 uint8_t goep_client_connect_l2cap(goep_client_t *goep_client, l2cap_ertm_config_t *l2cap_ertm_config, uint8_t *l2cap_ertm_buffer, 524 uint16_t l2cap_ertm_buffer_size, btstack_packet_handler_t handler, bd_addr_t addr, uint16_t l2cap_psm, 525 uint16_t *out_cid){ 526 btstack_assert(goep_client != NULL); 527 btstack_assert(l2cap_ertm_config != NULL); 528 btstack_assert(l2cap_ertm_buffer != NULL); 529 530 // get new goep cid, skip 0x0000 531 goep_client_cid++; 532 if (goep_client_cid == 0) { 533 goep_client_cid = 1; 534 } 535 536 // add to list 537 memset(goep_client, 0, sizeof(goep_client_t)); 538 goep_client->state = GOEP_CLIENT_W4_SDP; 539 goep_client->cid = goep_client_cid; 540 goep_client->client_handler = handler; 541 goep_client->profile_supported_features = PROFILE_FEATURES_NOT_PRESENT; 542 (void)memcpy(goep_client->bd_addr, addr, 6); 543 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 544 memcpy(&goep_client->ertm_config, l2cap_ertm_config, sizeof(l2cap_ertm_config_t)); 545 goep_client->ertm_buffer_size = l2cap_ertm_buffer_size; 546 goep_client->ertm_buffer = l2cap_ertm_buffer; 547 btstack_linked_list_add_tail(&goep_clients, (btstack_linked_item_t *) goep_client); 548 549 goep_client->l2cap_psm = l2cap_psm; 550 551 *out_cid = goep_client->cid; 552 553 return goep_client_start_connect(goep_client); 554 } 555 #endif 556 557 uint32_t goep_client_get_pbap_supported_features(uint16_t goep_cid){ 558 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 559 if (goep_client == NULL){ 560 return PBAP_FEATURES_NOT_PRESENT; 561 } 562 return goep_client->profile_supported_features; 563 } 564 565 uint32_t goep_client_get_map_supported_features(uint16_t goep_cid){ 566 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 567 if (goep_client == NULL){ 568 return PROFILE_FEATURES_NOT_PRESENT; 569 } 570 return goep_client->profile_supported_features; 571 } 572 573 uint8_t goep_client_get_map_mas_instance_id(uint16_t goep_cid){ 574 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 575 if (goep_client == NULL){ 576 return 0; 577 } 578 return goep_client->map_mas_instance_id; 579 } 580 581 uint8_t goep_client_get_map_supported_message_types(uint16_t goep_cid){ 582 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 583 if (goep_client == NULL){ 584 return 0; 585 } 586 return goep_client->map_supported_message_types; 587 } 588 589 590 bool goep_client_version_20_or_higher(uint16_t goep_cid){ 591 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 592 if (goep_client == NULL){ 593 return false; 594 } 595 return goep_client->l2cap_psm != 0; 596 } 597 598 void goep_client_request_can_send_now(uint16_t goep_cid){ 599 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 600 if (goep_client == NULL){ 601 return; 602 } 603 if (goep_client->l2cap_psm){ 604 l2cap_request_can_send_now_event(goep_client->bearer_cid); 605 } else { 606 rfcomm_request_can_send_now_event(goep_client->bearer_cid); 607 } 608 } 609 610 uint8_t goep_client_disconnect(uint16_t goep_cid){ 611 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 612 if (goep_client == NULL){ 613 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 614 } 615 if (goep_client->l2cap_psm){ 616 l2cap_disconnect(goep_client->bearer_cid); 617 } else { 618 rfcomm_disconnect(goep_client->bearer_cid); 619 } 620 return ERROR_CODE_SUCCESS; 621 } 622 623 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 624 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 625 if (goep_client == NULL){ 626 return; 627 } 628 goep_client->obex_connection_id = connection_id; 629 } 630 631 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 632 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 633 if (goep_client == NULL){ 634 return 0; 635 } 636 return goep_client->obex_opcode; 637 } 638 639 void goep_client_request_create_connect(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 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_CONNECT); 645 // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets 646 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, goep_client->bearer_mtu); 647 648 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 649 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 650 obex_message_builder_request_create_connect(buffer, buffer_len, obex_version_number, flags, maximum_obex_packet_length); 651 } 652 653 void goep_client_request_create_get(uint16_t goep_cid){ 654 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 655 if (goep_client == NULL){ 656 return; 657 } 658 goep_client_packet_init(goep_client, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 659 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 660 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 661 obex_message_builder_request_create_get(buffer, buffer_len, goep_client->obex_connection_id); 662 } 663 664 void goep_client_request_create_put(uint16_t goep_cid){ 665 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 666 if (goep_client == NULL){ 667 return; 668 } 669 goep_client_packet_init(goep_client, OBEX_OPCODE_PUT | OBEX_OPCODE_FINAL_BIT_MASK); 670 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 671 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 672 obex_message_builder_request_create_put(buffer, buffer_len, goep_client->obex_connection_id); 673 } 674 675 void goep_client_request_create_set_path(uint16_t goep_cid, uint8_t flags){ 676 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 677 if (goep_client == NULL){ 678 return; 679 } 680 goep_client_packet_init(goep_client, OBEX_OPCODE_SETPATH); 681 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 682 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 683 obex_message_builder_request_create_set_path(buffer, buffer_len, flags, goep_client->obex_connection_id); 684 } 685 686 void goep_client_request_create_abort(uint16_t goep_cid){ 687 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 688 if (goep_client == NULL){ 689 return; 690 } 691 goep_client_packet_init(goep_client, OBEX_OPCODE_ABORT); 692 693 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 694 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 695 obex_message_builder_request_create_abort(buffer, buffer_len, goep_client->obex_connection_id); 696 } 697 698 void goep_client_request_create_disconnect(uint16_t goep_cid){ 699 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 700 if (goep_client == NULL){ 701 return; 702 } 703 goep_client_packet_init(goep_client, OBEX_OPCODE_DISCONNECT); 704 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 705 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 706 obex_message_builder_request_create_disconnect(buffer, buffer_len, goep_client->obex_connection_id); 707 } 708 709 void goep_client_header_add_byte(uint16_t goep_cid, uint8_t header_type, uint8_t value){ 710 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 711 if (goep_client == NULL){ 712 return; 713 } 714 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 715 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 716 obex_message_builder_header_add_byte(buffer, buffer_len, header_type, value); 717 } 718 719 void goep_client_header_add_word(uint16_t goep_cid, uint8_t header_type, uint32_t value){ 720 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 721 if (goep_client == NULL){ 722 return; 723 } 724 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 725 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 726 obex_message_builder_header_add_word(buffer, buffer_len, header_type, value); 727 } 728 729 void goep_client_header_add_variable(uint16_t goep_cid, uint8_t header_type, const uint8_t * header_data, uint16_t header_data_length){ 730 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 731 if (goep_client == NULL){ 732 return; 733 } 734 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 735 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 736 obex_message_builder_header_add_variable(buffer, buffer_len, header_type, header_data, header_data_length); 737 } 738 739 void goep_client_header_add_srm_enable(uint16_t goep_cid){ 740 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 741 if (goep_client == NULL){ 742 return; 743 } 744 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 745 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 746 obex_message_builder_header_add_srm_enable(buffer, buffer_len); 747 } 748 749 void goep_client_header_add_target(uint16_t goep_cid, const uint8_t * target, uint16_t length){ 750 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 751 if (goep_client == NULL){ 752 return; 753 } 754 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 755 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 756 obex_message_builder_header_add_target(buffer, buffer_len, target, length); 757 } 758 759 void goep_client_header_add_application_parameters(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 760 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 761 if (goep_client == NULL){ 762 return; 763 } 764 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 765 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 766 obex_message_builder_header_add_application_parameters(buffer, buffer_len, data, length); 767 } 768 769 void goep_client_header_add_challenge_response(uint16_t goep_cid, const uint8_t * data, uint16_t length){ 770 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 771 if (goep_client == NULL){ 772 return; 773 } 774 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 775 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 776 obex_message_builder_header_add_challenge_response(buffer, buffer_len, data, length); 777 } 778 779 void goep_client_body_add_static(uint16_t goep_cid, const uint8_t * data, uint32_t length){ 780 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 781 if (goep_client == NULL){ 782 return; 783 } 784 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 785 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 786 obex_message_builder_body_add_static(buffer, buffer_len, data, length); 787 } 788 789 uint16_t goep_client_body_get_outgoing_buffer_len(uint16_t goep_cid) { 790 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 791 if (goep_client == NULL){ 792 return 0; 793 } 794 return goep_client_get_outgoing_buffer_len(goep_client); 795 } 796 797 void goep_client_body_fillup_static(uint16_t goep_cid, const uint8_t * data, uint32_t length, uint32_t * ret_length){ 798 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 799 if (goep_client == NULL){ 800 return; 801 } 802 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 803 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 804 obex_message_builder_body_fillup_static(buffer, buffer_len, data, length, ret_length); 805 } 806 807 void goep_client_header_add_name(uint16_t goep_cid, const char * name){ 808 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 809 if (goep_client == NULL){ 810 return; 811 } 812 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 813 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 814 obex_message_builder_header_add_name(buffer, buffer_len, name); 815 } 816 817 void goep_client_header_add_name_prefix(uint16_t goep_cid, const char * name, uint16_t name_len){ 818 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 819 if (goep_client == NULL){ 820 return; 821 } 822 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 823 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 824 obex_message_builder_header_add_name_prefix(buffer, buffer_len, name, name_len); 825 } 826 827 void goep_client_header_add_unicode_prefix(uint16_t goep_cid, uint8_t header_id, const char * name, uint16_t name_len){ 828 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 829 if (goep_client == NULL){ 830 return; 831 } 832 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 833 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 834 obex_message_builder_header_add_unicode_prefix(buffer, buffer_len, header_id, name, name_len); 835 } 836 837 void goep_client_header_add_type(uint16_t goep_cid, const char * type){ 838 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 839 if (goep_client == NULL){ 840 return; 841 } 842 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 843 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 844 obex_message_builder_header_add_type(buffer, buffer_len, type); 845 } 846 847 void goep_client_header_add_length(uint16_t goep_cid, uint32_t length){ 848 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 849 if (goep_client == NULL){ 850 return; 851 } 852 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 853 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 854 obex_message_builder_header_add_length(buffer, buffer_len, length); 855 } 856 857 uint16_t goep_client_request_get_max_body_size(uint16_t goep_cid){ 858 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 859 if (goep_client == NULL){ 860 return 0; 861 } 862 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 863 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 864 uint16_t pos = big_endian_read_16(buffer, 1); 865 return buffer_len - pos; 866 } 867 868 int goep_client_execute(uint16_t goep_cid){ 869 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 870 if (goep_client == NULL){ 871 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 872 } 873 return goep_client_execute_with_final_bit (goep_cid, true); 874 } 875 876 int goep_client_execute_with_final_bit(uint16_t goep_cid, bool final){ 877 goep_client_t * goep_client = goep_client_for_cid(goep_cid); 878 if (goep_client == NULL){ 879 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 880 } 881 uint8_t * buffer = goep_client_get_outgoing_buffer(goep_client); 882 uint16_t buffer_len = goep_client_get_outgoing_buffer_len(goep_client); 883 884 obex_message_builder_set_final_bit (buffer, buffer_len, final); 885 886 uint16_t pos = big_endian_read_16(buffer, 1); 887 if (goep_client->l2cap_psm){ 888 return l2cap_send(goep_client->bearer_cid, buffer, pos); 889 } else { 890 return rfcomm_send_prepared(goep_client->bearer_cid, pos); 891 } 892 } 893 894