1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "goep_client.c" 39 40 #include "btstack_config.h" 41 42 #include <stdint.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 47 #include "btstack_debug.h" 48 #include "hci_dump.h" 49 #include "bluetooth_sdp.h" 50 #include "btstack_event.h" 51 #include "classic/goep_client.h" 52 #include "classic/obex.h" 53 #include "classic/obex_iterator.h" 54 #include "classic/rfcomm.h" 55 #include "classic/sdp_client.h" 56 #include "classic/sdp_util.h" 57 #include "l2cap.h" 58 59 //------------------------------------------------------------------------------------------------------------ 60 // goep_client.c 61 // 62 63 typedef enum { 64 GOEP_INIT, 65 GOEP_W4_SDP, 66 GOEP_W4_CONNECTION, 67 GOEP_CONNECTED, 68 } goep_state_t; 69 70 typedef struct { 71 uint16_t cid; 72 goep_state_t state; 73 bd_addr_t bd_addr; 74 hci_con_handle_t con_handle; 75 uint8_t incoming; 76 uint8_t rfcomm_port; 77 uint16_t l2cap_psm; 78 uint16_t bearer_cid; 79 uint16_t bearer_mtu; 80 uint32_t pbap_supported_features; 81 82 uint8_t obex_opcode; 83 uint32_t obex_connection_id; 84 int obex_connection_id_set; 85 86 btstack_packet_handler_t client_handler; 87 } goep_client_t; 88 89 static goep_client_t _goep_client; 90 static goep_client_t * goep_client = &_goep_client; 91 92 static uint8_t attribute_value[30]; 93 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 94 95 static uint8_t ertm_buffer[1000]; 96 static l2cap_ertm_config_t ertm_config = { 97 1, // ertm mandatory 98 2, // max transmit, some tests require > 1 99 2000, 100 12000, 101 144, // l2cap ertm mtu 102 4, 103 4, 104 }; 105 106 static inline void goep_client_emit_connected_event(goep_client_t * context, uint8_t status){ 107 uint8_t event[15]; 108 int pos = 0; 109 event[pos++] = HCI_EVENT_GOEP_META; 110 pos++; // skip len 111 event[pos++] = GOEP_SUBEVENT_CONNECTION_OPENED; 112 little_endian_store_16(event,pos,context->cid); 113 pos+=2; 114 event[pos++] = status; 115 memcpy(&event[pos], context->bd_addr, 6); 116 pos += 6; 117 little_endian_store_16(event,pos,context->con_handle); 118 pos += 2; 119 event[pos++] = context->incoming; 120 event[1] = pos - 2; 121 if (pos != sizeof(event)) log_error("goep_client_emit_connected_event size %u", pos); 122 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 123 } 124 125 static inline void goep_client_emit_connection_closed_event(goep_client_t * context){ 126 uint8_t event[5]; 127 int pos = 0; 128 event[pos++] = HCI_EVENT_GOEP_META; 129 pos++; // skip len 130 event[pos++] = GOEP_SUBEVENT_CONNECTION_CLOSED; 131 little_endian_store_16(event,pos,context->cid); 132 pos+=2; 133 event[1] = pos - 2; 134 if (pos != sizeof(event)) log_error("goep_client_emit_connection_closed_event size %u", pos); 135 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 136 } 137 138 static inline void goep_client_emit_can_send_now_event(goep_client_t * context){ 139 uint8_t event[5]; 140 int pos = 0; 141 event[pos++] = HCI_EVENT_GOEP_META; 142 pos++; // skip len 143 event[pos++] = GOEP_SUBEVENT_CAN_SEND_NOW; 144 little_endian_store_16(event,pos,context->cid); 145 pos+=2; 146 event[1] = pos - 2; 147 if (pos != sizeof(event)) log_error("goep_client_emit_can_send_now_event size %u", pos); 148 context->client_handler(HCI_EVENT_PACKET, context->cid, &event[0], pos); 149 } 150 151 static void goep_client_handle_connection_opened(goep_client_t * context, uint8_t status, uint16_t mtu){ 152 if (status) { 153 context->state = GOEP_INIT; 154 log_info("goep_client: open failed, status %u", status); 155 } else { 156 context->bearer_mtu = mtu; 157 context->state = GOEP_CONNECTED; 158 log_info("goep_client: connection opened. cid %u, max frame size %u", context->bearer_cid, context->bearer_mtu); 159 } 160 goep_client_emit_connected_event(context, status); 161 } 162 163 static void goep_client_handle_connection_close(goep_client_t * context){ 164 context->state = GOEP_INIT; 165 goep_client_emit_connection_closed_event(context); 166 } 167 168 static void goep_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 169 UNUSED(channel); 170 UNUSED(size); 171 goep_client_t * context = goep_client; 172 switch (packet_type){ 173 case HCI_EVENT_PACKET: 174 switch (hci_event_packet_get_type(packet)) { 175 case L2CAP_EVENT_CHANNEL_OPENED: 176 goep_client_handle_connection_opened(context, l2cap_event_channel_opened_get_status(packet), 177 btstack_min(l2cap_event_channel_opened_get_remote_mtu(packet), l2cap_event_channel_opened_get_local_mtu(packet))); 178 return; 179 case L2CAP_EVENT_CAN_SEND_NOW: 180 goep_client_emit_can_send_now_event(context); 181 break; 182 case L2CAP_EVENT_CHANNEL_CLOSED: 183 goep_client_handle_connection_close(context); 184 break; 185 case RFCOMM_EVENT_CHANNEL_OPENED: 186 goep_client_handle_connection_opened(context, rfcomm_event_channel_opened_get_status(packet), rfcomm_event_channel_opened_get_max_frame_size(packet)); 187 return; 188 case RFCOMM_EVENT_CAN_SEND_NOW: 189 goep_client_emit_can_send_now_event(context); 190 break; 191 case RFCOMM_EVENT_CHANNEL_CLOSED: 192 goep_client_handle_connection_close(context); 193 break; 194 default: 195 break; 196 } 197 break; 198 case L2CAP_DATA_PACKET: 199 case RFCOMM_DATA_PACKET: 200 context->client_handler(GOEP_DATA_PACKET, context->cid, packet, size); 201 break; 202 default: 203 break; 204 } 205 } 206 207 static void goep_client_handle_sdp_query_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 208 goep_client_t * context = goep_client; 209 210 UNUSED(packet_type); 211 UNUSED(channel); 212 UNUSED(size); 213 214 des_iterator_t des_list_it; 215 des_iterator_t prot_it; 216 uint8_t status; 217 218 219 switch (hci_event_packet_get_type(packet)){ 220 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 221 222 // check if relevant attribute 223 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 224 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 225 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 226 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 227 break; 228 default: 229 return; 230 } 231 232 // warn if attribute too large to fit in our buffer 233 if (sdp_event_query_attribute_byte_get_attribute_length(packet) > attribute_value_buffer_size) { 234 log_error("SDP attribute value size exceeded for attribute %x: available %d, required %d", sdp_event_query_attribute_byte_get_attribute_id(packet), attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 235 break; 236 } 237 238 // store single byte 239 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 240 241 // wait until value fully received 242 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) != sdp_event_query_attribute_byte_get_attribute_length(packet)) break; 243 244 // process attributes 245 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 246 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: 247 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 248 uint8_t *des_element; 249 uint8_t *element; 250 uint32_t uuid; 251 252 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 253 254 des_element = des_iterator_get_element(&des_list_it); 255 des_iterator_init(&prot_it, des_element); 256 element = des_iterator_get_element(&prot_it); 257 258 if (de_get_element_type(element) != DE_UUID) continue; 259 260 uuid = de_get_uuid32(element); 261 switch (uuid){ 262 case BLUETOOTH_PROTOCOL_RFCOMM: 263 if (!des_iterator_has_more(&prot_it)) continue; 264 des_iterator_next(&prot_it); 265 element = des_iterator_get_element(&prot_it); 266 context->rfcomm_port = element[de_get_header_size(element)]; 267 break; 268 default: 269 break; 270 } 271 } 272 break; 273 case BLUETOOTH_ATTRIBUTE_GOEP_L2CAP_PSM: 274 de_element_get_uint16(attribute_value, &context->l2cap_psm); 275 break; 276 case BLUETOOTH_ATTRIBUTE_PBAP_SUPPORTED_FEATURES: 277 if (de_get_element_type(attribute_value) != DE_UINT) break; 278 if (de_get_size_type(attribute_value) != DE_SIZE_32) break; 279 context->pbap_supported_features = big_endian_read_32(attribute_value, de_get_header_size(attribute_value)); 280 log_info("pbap_supported_features 0x%x", context->pbap_supported_features); 281 break; 282 default: 283 break; 284 } 285 break; 286 287 case SDP_EVENT_QUERY_COMPLETE: 288 status = sdp_event_query_complete_get_status(packet); 289 if (status != ERROR_CODE_SUCCESS){ 290 log_info("GOEP client, SDP query failed 0x%02x", status); 291 context->state = GOEP_INIT; 292 goep_client_emit_connected_event(goep_client, status); 293 break; 294 } 295 if (context->rfcomm_port == 0 && context->l2cap_psm == 0){ 296 log_info("No GOEP RFCOMM or L2CAP server found"); 297 context->state = GOEP_INIT; 298 goep_client_emit_connected_event(goep_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE); 299 break; 300 } 301 if (context->l2cap_psm){ 302 log_info("Remote GOEP L2CAP PSM: %u", context->l2cap_psm); 303 l2cap_create_ertm_channel(&goep_client_packet_handler, context->bd_addr, context->l2cap_psm, 304 &ertm_config, ertm_buffer, sizeof(ertm_buffer), &context->bearer_cid); 305 } else { 306 log_info("Remote GOEP RFCOMM Server Channel: %u", context->rfcomm_port); 307 rfcomm_create_channel(&goep_client_packet_handler, context->bd_addr, context->rfcomm_port, &context->bearer_cid); 308 } 309 break; 310 } 311 } 312 313 static uint8_t * goep_client_get_outgoing_buffer(goep_client_t * context){ 314 if (context->l2cap_psm){ 315 return l2cap_get_outgoing_buffer(); 316 } else { 317 return rfcomm_get_outgoing_buffer(); 318 } 319 } 320 321 static void goep_client_packet_append(const uint8_t * data, uint16_t len){ 322 goep_client_t * context = goep_client; 323 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 324 uint16_t pos = big_endian_read_16(buffer, 1); 325 memcpy(&buffer[pos], data, len); 326 pos += len; 327 big_endian_store_16(buffer, 1, pos); 328 } 329 330 static void goep_client_packet_init(uint16_t goep_cid, uint8_t opcode){ 331 UNUSED(goep_cid); 332 goep_client_t * context = goep_client; 333 if (context->l2cap_psm){ 334 l2cap_reserve_packet_buffer(); 335 } else { 336 rfcomm_reserve_packet_buffer(); 337 } 338 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 339 buffer[0] = opcode; 340 big_endian_store_16(buffer, 1, 3); 341 // store opcode for parsing of response 342 context->obex_opcode = opcode; 343 } 344 345 static void goep_client_packet_add_connection_id(uint16_t goep_cid){ 346 UNUSED(goep_cid); 347 goep_client_t * context = goep_client; 348 // add connection_id header if set, must be first header if used 349 if (context->obex_connection_id != OBEX_CONNECTION_ID_INVALID){ 350 uint8_t header[5]; 351 header[0] = OBEX_HEADER_CONNECTION_ID; 352 big_endian_store_32(header, 1, context->obex_connection_id); 353 goep_client_packet_append(&header[0], sizeof(header)); 354 } 355 } 356 357 void goep_client_init(void){ 358 memset(goep_client, 0, sizeof(goep_client_t)); 359 goep_client->state = GOEP_INIT; 360 goep_client->cid = 1; 361 goep_client->obex_connection_id = OBEX_CONNECTION_ID_INVALID; 362 } 363 364 uint8_t goep_client_create_connection(btstack_packet_handler_t handler, bd_addr_t addr, uint16_t uuid, uint16_t * out_cid){ 365 goep_client_t * context = goep_client; 366 if (context->state != GOEP_INIT) return BTSTACK_MEMORY_ALLOC_FAILED; 367 context->client_handler = handler; 368 context->state = GOEP_W4_SDP; 369 context->l2cap_psm = 0; 370 context->rfcomm_port = 0; 371 // Backwards compatibility: If the PbapSupportedFeatures attribute is not present 0x00000003 372 // shall be assumed for a remote PSE. 373 context->pbap_supported_features = 0x03; 374 memcpy(context->bd_addr, addr, 6); 375 sdp_client_query_uuid16(&goep_client_handle_sdp_query_event, context->bd_addr, uuid); 376 *out_cid = context->cid; 377 return 0; 378 } 379 380 uint8_t goep_client_disconnect(uint16_t goep_cid){ 381 UNUSED(goep_cid); 382 goep_client_t * context = goep_client; 383 rfcomm_disconnect(context->bearer_cid); 384 return 0; 385 } 386 387 void goep_client_set_connection_id(uint16_t goep_cid, uint32_t connection_id){ 388 UNUSED(goep_cid); 389 goep_client_t * context = goep_client; 390 context->obex_connection_id = connection_id; 391 } 392 393 uint8_t goep_client_get_request_opcode(uint16_t goep_cid){ 394 UNUSED(goep_cid); 395 goep_client_t * context = goep_client; 396 return context->obex_opcode; 397 } 398 399 void goep_client_request_can_send_now(uint16_t goep_cid){ 400 UNUSED(goep_cid); 401 goep_client_t * context = goep_client; 402 if (context->l2cap_psm){ 403 l2cap_request_can_send_now_event(context->bearer_cid); 404 } else { 405 rfcomm_request_can_send_now_event(context->bearer_cid); 406 } 407 } 408 409 void goep_client_create_connect_request(uint16_t goep_cid, uint8_t obex_version_number, uint8_t flags, uint16_t maximum_obex_packet_length){ 410 UNUSED(goep_cid); 411 goep_client_t * context = goep_client; 412 goep_client_packet_init(goep_cid, OBEX_OPCODE_CONNECT); 413 uint8_t fields[4]; 414 fields[0] = obex_version_number; 415 fields[1] = flags; 416 // workaround: limit OBEX packet len to L2CAP/RFCOMM MTU to avoid handling of fragemented packets 417 maximum_obex_packet_length = btstack_min(maximum_obex_packet_length, context->bearer_mtu); 418 big_endian_store_16(fields, 2, maximum_obex_packet_length); 419 goep_client_packet_append(&fields[0], sizeof(fields)); 420 } 421 422 void goep_client_create_get_request(uint16_t goep_cid){ 423 UNUSED(goep_cid); 424 goep_client_packet_init(goep_cid, OBEX_OPCODE_GET | OBEX_OPCODE_FINAL_BIT_MASK); 425 goep_client_packet_add_connection_id(goep_cid); 426 } 427 428 void goep_client_create_set_path_request(uint16_t goep_cid, uint8_t flags){ 429 UNUSED(goep_cid); 430 goep_client_packet_init(goep_cid, OBEX_OPCODE_SETPATH); 431 uint8_t fields[2]; 432 fields[0] = flags; 433 fields[1] = 0; // reserved 434 goep_client_packet_append(&fields[0], sizeof(fields)); 435 goep_client_packet_add_connection_id(goep_cid); 436 } 437 438 static void goep_client_add_header(uint16_t goep_cid, uint8_t header_type, uint16_t header_length, const uint8_t * header_data){ 439 UNUSED(goep_cid); 440 uint8_t header[3]; 441 header[0] = header_type; 442 big_endian_store_16(header, 1, 1 + 2 + header_length); 443 goep_client_packet_append(&header[0], sizeof(header)); 444 goep_client_packet_append(header_data, header_length); 445 } 446 447 void goep_client_add_header_target(uint16_t goep_cid, uint16_t length, const uint8_t * target){ 448 goep_client_add_header(goep_cid, OBEX_HEADER_TARGET, length, target); 449 } 450 451 void goep_client_add_header_application_parameters(uint16_t goep_cid, uint16_t length, const uint8_t * data){ 452 goep_client_add_header(goep_cid, OBEX_HEADER_APPLICATION_PARAMETERS, length, data); 453 } 454 455 void goep_client_add_header_name(uint16_t goep_cid, const char * name){ 456 UNUSED(goep_cid); 457 goep_client_t * context = goep_client; 458 int len_incl_zero = strlen(name) + 1; 459 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 460 uint16_t pos = big_endian_read_16(buffer, 1); 461 buffer[pos++] = OBEX_HEADER_NAME; 462 big_endian_store_16(buffer, pos, 1 + 2 + len_incl_zero*2); 463 pos += 2; 464 int i; 465 // @note name[len] == 0 466 for (i = 0 ; i < len_incl_zero ; i++){ 467 buffer[pos++] = 0; 468 buffer[pos++] = *name++; 469 } 470 big_endian_store_16(buffer, 1, pos); 471 } 472 473 void goep_client_add_header_type(uint16_t goep_cid, const char * type){ 474 UNUSED(goep_cid); 475 uint8_t header[3]; 476 header[0] = OBEX_HEADER_TYPE; 477 int len_incl_zero = strlen(type) + 1; 478 big_endian_store_16(header, 1, 1 + 2 + len_incl_zero); 479 goep_client_packet_append(&header[0], sizeof(header)); 480 goep_client_packet_append((const uint8_t*)type, len_incl_zero); 481 } 482 483 int goep_client_execute(uint16_t goep_cid){ 484 UNUSED(goep_cid); 485 goep_client_t * context = goep_client; 486 uint8_t * buffer = goep_client_get_outgoing_buffer(context); 487 uint16_t pos = big_endian_read_16(buffer, 1); 488 printf_hexdump(buffer, pos); 489 if (context->l2cap_psm){ 490 return l2cap_send_prepared(context->bearer_cid, pos); 491 } else { 492 return rfcomm_send_prepared(context->bearer_cid, pos); 493 } 494 } 495