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__ "sdp_server.c" 39 40 /* 41 * Implementation of the Service Discovery Protocol Server 42 */ 43 44 #include <string.h> 45 46 #include "bluetooth.h" 47 #include "bluetooth_psm.h" 48 #include "bluetooth_sdp.h" 49 #include "btstack_debug.h" 50 #include "btstack_event.h" 51 #include "btstack_memory.h" 52 #include "classic/core.h" 53 #include "classic/sdp_server.h" 54 #include "classic/sdp_util.h" 55 #include "hci.h" 56 #include "hci_dump.h" 57 #include "l2cap.h" 58 59 // max number of incoming l2cap connections that can be queued instead of getting rejected 60 #ifndef SDP_WAITING_LIST_MAX_COUNT 61 #define SDP_WAITING_LIST_MAX_COUNT 8 62 #endif 63 64 // max reserved ServiceRecordHandle 65 #define MAX_RESERVED_SERVICE_RECORD_HANDLE 0xffff 66 67 // max SDP response matches L2CAP PDU -- allow to use smaller buffer 68 #ifndef SDP_RESPONSE_BUFFER_SIZE 69 #define SDP_RESPONSE_BUFFER_SIZE (HCI_ACL_PAYLOAD_SIZE-L2CAP_HEADER_SIZE) 70 #endif 71 72 static void sdp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 73 74 // registered service records 75 static btstack_linked_list_t sdp_server_service_records; 76 77 // our handles start after the reserved range 78 static uint32_t sdp_server_next_service_record_handle; 79 80 static uint8_t sdp_response_buffer[SDP_RESPONSE_BUFFER_SIZE]; 81 82 static uint16_t sdp_server_l2cap_cid; 83 static uint16_t sdp_server_response_size; 84 static uint16_t sdp_server_l2cap_waiting_list_cids[SDP_WAITING_LIST_MAX_COUNT]; 85 static int sdp_server_l2cap_waiting_list_count; 86 87 #ifdef ENABLE_TESTING_SUPPORT 88 static bool sdp_server_testing_single_record_reponse = false; 89 #endif 90 91 void sdp_init(void){ 92 sdp_server_next_service_record_handle = ((uint32_t) MAX_RESERVED_SERVICE_RECORD_HANDLE) + 2; 93 // register with l2cap psm sevices - max MTU 94 l2cap_register_service(sdp_packet_handler, BLUETOOTH_PSM_SDP, 0xffff, LEVEL_0); 95 } 96 97 void sdp_deinit(void){ 98 sdp_server_service_records = NULL; 99 sdp_server_l2cap_cid = 0; 100 sdp_server_response_size = 0; 101 sdp_server_l2cap_waiting_list_count = 0; 102 } 103 104 uint32_t sdp_get_service_record_handle(const uint8_t * record){ 105 // TODO: make sdp_get_attribute_value_for_attribute_id accept const data to remove cast 106 uint8_t * serviceRecordHandleAttribute = sdp_get_attribute_value_for_attribute_id((uint8_t *)record, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 107 if (!serviceRecordHandleAttribute) return 0; 108 if (de_get_element_type(serviceRecordHandleAttribute) != DE_UINT) return 0; 109 if (de_get_size_type(serviceRecordHandleAttribute) != DE_SIZE_32) return 0; 110 return big_endian_read_32(serviceRecordHandleAttribute, 1); 111 } 112 113 static service_record_item_t * sdp_get_record_item_for_handle(uint32_t handle){ 114 btstack_linked_item_t *it; 115 for (it = (btstack_linked_item_t *) sdp_server_service_records; it ; it = it->next){ 116 service_record_item_t * item = (service_record_item_t *) it; 117 if (item->service_record_handle == handle){ 118 return item; 119 } 120 } 121 return NULL; 122 } 123 124 uint8_t * sdp_get_record_for_handle(uint32_t handle){ 125 service_record_item_t * record_item = sdp_get_record_item_for_handle(handle); 126 if (!record_item) return NULL; 127 return record_item->service_record; 128 } 129 130 // get next free, unregistered service record handle 131 uint32_t sdp_create_service_record_handle(void){ 132 uint32_t handle = 0; 133 do { 134 handle = sdp_server_next_service_record_handle++; 135 if (sdp_get_record_item_for_handle(handle)) handle = 0; 136 } while (handle == 0); 137 return handle; 138 } 139 140 /** 141 * @brief Register Service Record with database using ServiceRecordHandle stored in record 142 * @pre AttributeIDs are in ascending order 143 * @pre ServiceRecordHandle is first attribute and valid 144 * @param record is not copied! 145 * @result status 146 */ 147 uint8_t sdp_register_service(const uint8_t * record){ 148 149 // validate service record handle. it must: exist, be in valid range, not have been already used 150 uint32_t record_handle = sdp_get_service_record_handle(record); 151 if (!record_handle) return SDP_HANDLE_INVALID; 152 if (record_handle <= MAX_RESERVED_SERVICE_RECORD_HANDLE) return SDP_HANDLE_INVALID; 153 if (sdp_get_record_item_for_handle(record_handle)) return SDP_HANDLE_ALREADY_REGISTERED; 154 155 // alloc memory for new service_record_item 156 service_record_item_t * newRecordItem = btstack_memory_service_record_item_get(); 157 if (!newRecordItem) return BTSTACK_MEMORY_ALLOC_FAILED; 158 159 // set handle and record 160 newRecordItem->service_record_handle = record_handle; 161 newRecordItem->service_record = (uint8_t*) record; 162 163 // add to linked list 164 btstack_linked_list_add(&sdp_server_service_records, (btstack_linked_item_t *) newRecordItem); 165 166 return 0; 167 } 168 169 // 170 // unregister service record 171 // 172 void sdp_unregister_service(uint32_t service_record_handle){ 173 service_record_item_t * record_item = sdp_get_record_item_for_handle(service_record_handle); 174 if (!record_item) return; 175 btstack_linked_list_remove(&sdp_server_service_records, (btstack_linked_item_t *) record_item); 176 btstack_memory_service_record_item_free(record_item); 177 } 178 179 // PDU 180 // PDU ID (1), Transaction ID (2), Param Length (2), Param 1, Param 2, .. 181 182 static int sdp_create_error_response(uint16_t transaction_id, uint16_t error_code){ 183 sdp_response_buffer[0] = SDP_ErrorResponse; 184 big_endian_store_16(sdp_response_buffer, 1, transaction_id); 185 big_endian_store_16(sdp_response_buffer, 3, 2); 186 big_endian_store_16(sdp_response_buffer, 5, error_code); // invalid syntax 187 return 7; 188 } 189 190 int sdp_handle_service_search_request(uint8_t * packet, uint16_t remote_mtu){ 191 192 // get request details 193 uint16_t transaction_id = big_endian_read_16(packet, 1); 194 uint16_t param_len = big_endian_read_16(packet, 3); 195 uint8_t * serviceSearchPattern = &packet[5]; 196 uint16_t serviceSearchPatternLen = de_get_len_safe(serviceSearchPattern, param_len); 197 // assert service search pattern is contained 198 if (!serviceSearchPatternLen) return 0; 199 param_len -= serviceSearchPatternLen; 200 // assert max record count is contained 201 if (param_len < 2) return 0; 202 uint16_t maximumServiceRecordCount = big_endian_read_16(packet, 5 + serviceSearchPatternLen); 203 param_len -= 2; 204 // assert continuation state len is contained in param_len 205 if (param_len < 1) return 0; 206 uint8_t * continuationState = &packet[5+serviceSearchPatternLen+2]; 207 // assert continuation state is contained in param_len 208 if ((1 + continuationState[0]) > param_len) return 0; 209 210 // calc maximumServiceRecordCount based on remote MTU 211 uint16_t maxNrServiceRecordsPerResponse = (remote_mtu - (9+3))/4; 212 #ifdef ENABLE_TESTING_SUPPORT 213 if (sdp_server_testing_single_record_reponse){ 214 maxNrServiceRecordsPerResponse = 1; 215 } 216 #endif 217 218 // continuation state contains index of next service record to examine 219 int continuation = 0; 220 uint16_t continuation_index = 0; 221 if (continuationState[0] == 2){ 222 continuation_index = big_endian_read_16(continuationState, 1); 223 } 224 225 // get and limit total count 226 btstack_linked_item_t *it; 227 uint16_t total_service_count = 0; 228 for (it = (btstack_linked_item_t *) sdp_server_service_records; it ; it = it->next){ 229 service_record_item_t * item = (service_record_item_t *) it; 230 if (!sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)) continue; 231 total_service_count++; 232 } 233 if (total_service_count > maximumServiceRecordCount){ 234 total_service_count = maximumServiceRecordCount; 235 } 236 237 // ServiceRecordHandleList at 9 238 uint16_t pos = 9; 239 uint16_t current_service_count = 0; 240 uint16_t current_service_index = 0; 241 uint16_t matching_service_count = 0; 242 for (it = (btstack_linked_item_t *) sdp_server_service_records; it ; it = it->next, ++current_service_index){ 243 service_record_item_t * item = (service_record_item_t *) it; 244 245 if (!sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)) continue; 246 matching_service_count++; 247 248 if (current_service_index < continuation_index) continue; 249 250 big_endian_store_32(sdp_response_buffer, pos, item->service_record_handle); 251 pos += 4; 252 current_service_count++; 253 254 if (matching_service_count >= total_service_count) break; 255 256 if (current_service_count >= maxNrServiceRecordsPerResponse){ 257 continuation = 1; 258 continuation_index = current_service_index + 1; 259 break; 260 } 261 } 262 263 // Store continuation state 264 if (continuation) { 265 sdp_response_buffer[pos++] = 2; 266 big_endian_store_16(sdp_response_buffer, pos, continuation_index); 267 pos += 2; 268 } else { 269 sdp_response_buffer[pos++] = 0; 270 } 271 272 // header 273 sdp_response_buffer[0] = SDP_ServiceSearchResponse; 274 big_endian_store_16(sdp_response_buffer, 1, transaction_id); 275 big_endian_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload 276 big_endian_store_16(sdp_response_buffer, 5, total_service_count); 277 big_endian_store_16(sdp_response_buffer, 7, current_service_count); 278 279 return pos; 280 } 281 282 int sdp_handle_service_attribute_request(uint8_t * packet, uint16_t remote_mtu){ 283 284 // get request details 285 uint16_t transaction_id = big_endian_read_16(packet, 1); 286 uint16_t param_len = big_endian_read_16(packet, 3); 287 // assert serviceRecordHandle and maximumAttributeByteCount are in param_len 288 if (param_len < 6) return 0; 289 uint32_t serviceRecordHandle = big_endian_read_32(packet, 5); 290 uint16_t maximumAttributeByteCount = big_endian_read_16(packet, 9); 291 param_len -= 6; 292 uint8_t * attributeIDList = &packet[11]; 293 uint16_t attributeIDListLen = de_get_len_safe(attributeIDList, param_len); 294 if (!sdp_attribute_list_valid(attributeIDList) == false){ 295 return sdp_create_error_response(transaction_id, 0x0003); /// invalid request syntax 296 } 297 // assert attributeIDList are in param_len 298 if (!attributeIDListLen) return 0; 299 param_len -= attributeIDListLen; 300 // assert continuation state len is contained in param_len 301 if (param_len < 1) return 0; 302 uint8_t * continuationState = &packet[11+attributeIDListLen]; 303 // assert continuation state is contained in param_len 304 if ((1 + continuationState[0]) > param_len) return 0; 305 306 // calc maximumAttributeByteCount based on remote MTU 307 uint16_t maximumAttributeByteCount2 = remote_mtu - (7+3); 308 if (maximumAttributeByteCount2 < maximumAttributeByteCount) { 309 maximumAttributeByteCount = maximumAttributeByteCount2; 310 } 311 312 // continuation state contains the offset into the complete response 313 uint16_t continuation_offset = 0; 314 if (continuationState[0] == 2){ 315 continuation_offset = big_endian_read_16(continuationState, 1); 316 } 317 318 // get service record 319 service_record_item_t * item = sdp_get_record_item_for_handle(serviceRecordHandle); 320 if (!item){ 321 // service record handle doesn't exist 322 return sdp_create_error_response(transaction_id, 0x0002); /// invalid Service Record Handle 323 } 324 325 326 // AttributeList - starts at offset 7 327 uint16_t pos = 7; 328 329 if (continuation_offset == 0){ 330 331 // get size of this record 332 uint16_t filtered_attributes_size = sdp_get_filtered_size(item->service_record, attributeIDList); 333 334 // store DES 335 de_store_descriptor_with_len(&sdp_response_buffer[pos], DE_DES, DE_SIZE_VAR_16, filtered_attributes_size); 336 maximumAttributeByteCount -= 3; 337 pos += 3; 338 } 339 340 // copy maximumAttributeByteCount from record 341 uint16_t bytes_used; 342 int complete = sdp_filter_attributes_in_attributeIDList(item->service_record, attributeIDList, continuation_offset, maximumAttributeByteCount, &bytes_used, &sdp_response_buffer[pos]); 343 pos += bytes_used; 344 345 uint16_t attributeListByteCount = pos - 7; 346 347 if (complete) { 348 sdp_response_buffer[pos++] = 0; 349 } else { 350 continuation_offset += bytes_used; 351 sdp_response_buffer[pos++] = 2; 352 big_endian_store_16(sdp_response_buffer, pos, continuation_offset); 353 pos += 2; 354 } 355 356 // header 357 sdp_response_buffer[0] = SDP_ServiceAttributeResponse; 358 big_endian_store_16(sdp_response_buffer, 1, transaction_id); 359 big_endian_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload 360 big_endian_store_16(sdp_response_buffer, 5, attributeListByteCount); 361 362 return pos; 363 } 364 365 static uint16_t sdp_get_size_for_service_search_attribute_response(uint8_t * serviceSearchPattern, uint8_t * attributeIDList){ 366 uint16_t total_response_size = 0; 367 btstack_linked_item_t *it; 368 for (it = (btstack_linked_item_t *) sdp_server_service_records; it ; it = it->next){ 369 service_record_item_t * item = (service_record_item_t *) it; 370 371 if (!sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)) continue; 372 373 // for all service records that match 374 total_response_size += 3 + sdp_get_filtered_size(item->service_record, attributeIDList); 375 } 376 return total_response_size; 377 } 378 379 int sdp_handle_service_search_attribute_request(uint8_t * packet, uint16_t remote_mtu){ 380 381 // SDP header before attribute sevice list: 7 382 // Continuation, worst case: 5 383 384 // get request details 385 uint16_t transaction_id = big_endian_read_16(packet, 1); 386 uint16_t param_len = big_endian_read_16(packet, 3); 387 uint8_t * serviceSearchPattern = &packet[5]; 388 uint16_t serviceSearchPatternLen = de_get_len_safe(serviceSearchPattern, param_len); 389 // assert serviceSearchPattern header is contained in param_len 390 if (!serviceSearchPatternLen) return 0; 391 param_len -= serviceSearchPatternLen; 392 // assert maximumAttributeByteCount contained in param_len 393 if (param_len < 2) return 0; 394 uint16_t maximumAttributeByteCount = big_endian_read_16(packet, 5 + serviceSearchPatternLen); 395 param_len -= 2; 396 uint8_t * attributeIDList = &packet[5+serviceSearchPatternLen+2]; 397 uint16_t attributeIDListLen = de_get_len_safe(attributeIDList, param_len); 398 if (!sdp_attribute_list_valid(attributeIDList) == false){ 399 return sdp_create_error_response(transaction_id, 0x0003); /// invalid request syntax 400 } 401 // assert attributeIDList is contained in param_len 402 if (!attributeIDListLen) return 0; 403 // assert continuation state len is contained in param_len 404 if (param_len < 1) return 0; 405 uint8_t * continuationState = &packet[5+serviceSearchPatternLen+2+attributeIDListLen]; 406 // assert continuation state is contained in param_len 407 if ((1 + continuationState[0]) > param_len) return 0; 408 409 // calc maximumAttributeByteCount based on remote MTU, SDP header and reserved Continuation block 410 uint16_t maximumAttributeByteCount2 = remote_mtu - 12; 411 if (maximumAttributeByteCount2 < maximumAttributeByteCount) { 412 maximumAttributeByteCount = maximumAttributeByteCount2; 413 } 414 415 // continuation state contains: index of next service record to examine 416 // continuation state contains: byte offset into this service record 417 uint16_t continuation_service_index = 0; 418 uint16_t continuation_offset = 0; 419 if (continuationState[0] == 4){ 420 continuation_service_index = big_endian_read_16(continuationState, 1); 421 continuation_offset = big_endian_read_16(continuationState, 3); 422 } 423 424 // log_info("--> sdp_handle_service_search_attribute_request, cont %u/%u, max %u", continuation_service_index, continuation_offset, maximumAttributeByteCount); 425 426 // AttributeLists - starts at offset 7 427 uint16_t pos = 7; 428 429 // add DES with total size for first request 430 if ((continuation_service_index == 0) && (continuation_offset == 0)){ 431 uint16_t total_response_size = sdp_get_size_for_service_search_attribute_response(serviceSearchPattern, attributeIDList); 432 de_store_descriptor_with_len(&sdp_response_buffer[pos], DE_DES, DE_SIZE_VAR_16, total_response_size); 433 // log_info("total response size %u", total_response_size); 434 pos += 3; 435 maximumAttributeByteCount -= 3; 436 } 437 438 // create attribute list 439 int first_answer = 1; 440 int continuation = 0; 441 uint16_t current_service_index = 0; 442 btstack_linked_item_t *it = (btstack_linked_item_t *) sdp_server_service_records; 443 for ( ; it ; it = it->next, ++current_service_index){ 444 service_record_item_t * item = (service_record_item_t *) it; 445 446 if (current_service_index < continuation_service_index ) continue; 447 if (!sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)) continue; 448 449 if (continuation_offset == 0){ 450 451 // get size of this record 452 uint16_t filtered_attributes_size = sdp_get_filtered_size(item->service_record, attributeIDList); 453 454 // stop if complete record doesn't fits into response but we already have a partial response 455 if (((filtered_attributes_size + 3) > maximumAttributeByteCount) && !first_answer) { 456 continuation = 1; 457 break; 458 } 459 460 // store DES 461 de_store_descriptor_with_len(&sdp_response_buffer[pos], DE_DES, DE_SIZE_VAR_16, filtered_attributes_size); 462 pos += 3; 463 maximumAttributeByteCount -= 3; 464 } 465 466 first_answer = 0; 467 468 // copy maximumAttributeByteCount from record 469 uint16_t bytes_used; 470 int complete = sdp_filter_attributes_in_attributeIDList(item->service_record, attributeIDList, continuation_offset, maximumAttributeByteCount, &bytes_used, &sdp_response_buffer[pos]); 471 pos += bytes_used; 472 maximumAttributeByteCount -= bytes_used; 473 474 if (complete) { 475 continuation_offset = 0; 476 continue; 477 } 478 479 continuation = 1; 480 continuation_offset += bytes_used; 481 break; 482 } 483 484 uint16_t attributeListsByteCount = pos - 7; 485 486 // Continuation State 487 if (continuation){ 488 sdp_response_buffer[pos++] = 4; 489 big_endian_store_16(sdp_response_buffer, pos, (uint16_t) current_service_index); 490 pos += 2; 491 big_endian_store_16(sdp_response_buffer, pos, continuation_offset); 492 pos += 2; 493 } else { 494 // complete 495 sdp_response_buffer[pos++] = 0; 496 } 497 498 // create SDP header 499 sdp_response_buffer[0] = SDP_ServiceSearchAttributeResponse; 500 big_endian_store_16(sdp_response_buffer, 1, transaction_id); 501 big_endian_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload 502 big_endian_store_16(sdp_response_buffer, 5, attributeListsByteCount); 503 504 return pos; 505 } 506 507 static void sdp_respond(void){ 508 if (!sdp_server_response_size ) return; 509 if (!sdp_server_l2cap_cid) return; 510 511 // update state before sending packet (avoid getting called when new l2cap credit gets emitted) 512 uint16_t size = sdp_server_response_size; 513 sdp_server_response_size = 0; 514 l2cap_send(sdp_server_l2cap_cid, sdp_response_buffer, size); 515 } 516 517 // @pre space in list 518 static void sdp_waiting_list_add(uint16_t cid){ 519 sdp_server_l2cap_waiting_list_cids[sdp_server_l2cap_waiting_list_count++] = cid; 520 } 521 522 // @pre at least one item in list 523 static uint16_t sdp_waiting_list_get(void){ 524 uint16_t cid = sdp_server_l2cap_waiting_list_cids[0]; 525 sdp_server_l2cap_waiting_list_count--; 526 if (sdp_server_l2cap_waiting_list_count){ 527 memmove(&sdp_server_l2cap_waiting_list_cids[0], &sdp_server_l2cap_waiting_list_cids[1], sdp_server_l2cap_waiting_list_count * sizeof(uint16_t)); 528 } 529 return cid; 530 } 531 532 // we assume that we don't get two requests in a row 533 static void sdp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 534 uint16_t transaction_id; 535 sdp_pdu_id_t pdu_id; 536 uint16_t remote_mtu; 537 uint16_t param_len; 538 539 switch (packet_type) { 540 541 case L2CAP_DATA_PACKET: 542 pdu_id = (sdp_pdu_id_t) packet[0]; 543 transaction_id = big_endian_read_16(packet, 1); 544 param_len = big_endian_read_16(packet, 3); 545 remote_mtu = l2cap_get_remote_mtu_for_local_cid(channel); 546 // account for our buffer 547 if (remote_mtu > SDP_RESPONSE_BUFFER_SIZE){ 548 remote_mtu = SDP_RESPONSE_BUFFER_SIZE; 549 } 550 // validate parm_len against packet size 551 if ((param_len + 5) > size) { 552 // just clear pdu_id 553 pdu_id = SDP_ErrorResponse; 554 } 555 556 // log_info("SDP Request: type %u, transaction id %u, len %u, mtu %u", pdu_id, transaction_id, param_len, remote_mtu); 557 switch (pdu_id){ 558 559 case SDP_ServiceSearchRequest: 560 sdp_server_response_size = sdp_handle_service_search_request(packet, remote_mtu); 561 break; 562 563 case SDP_ServiceAttributeRequest: 564 sdp_server_response_size = sdp_handle_service_attribute_request(packet, remote_mtu); 565 break; 566 567 case SDP_ServiceSearchAttributeRequest: 568 sdp_server_response_size = sdp_handle_service_search_attribute_request(packet, remote_mtu); 569 break; 570 571 default: 572 sdp_server_response_size = sdp_create_error_response(transaction_id, 0x0004); // invalid PDU size 573 break; 574 } 575 if (!sdp_server_response_size) break; 576 l2cap_request_can_send_now_event(sdp_server_l2cap_cid); 577 break; 578 579 case HCI_EVENT_PACKET: 580 581 switch (hci_event_packet_get_type(packet)) { 582 583 case L2CAP_EVENT_INCOMING_CONNECTION: 584 if (sdp_server_l2cap_cid) { 585 // try to queue up 586 if (sdp_server_l2cap_waiting_list_count < SDP_WAITING_LIST_MAX_COUNT){ 587 sdp_waiting_list_add(channel); 588 log_info("busy, queing incoming cid 0x%04x, now %u waiting", channel, sdp_server_l2cap_waiting_list_count); 589 break; 590 } 591 592 // CONNECTION REJECTED DUE TO LIMITED RESOURCES 593 l2cap_decline_connection(channel); 594 break; 595 } 596 // accept 597 sdp_server_l2cap_cid = channel; 598 sdp_server_response_size = 0; 599 l2cap_accept_connection(sdp_server_l2cap_cid); 600 break; 601 602 case L2CAP_EVENT_CHANNEL_OPENED: 603 if (packet[2]) { 604 // open failed -> reset 605 sdp_server_l2cap_cid = 0; 606 } 607 break; 608 609 case L2CAP_EVENT_CAN_SEND_NOW: 610 sdp_respond(); 611 break; 612 613 case L2CAP_EVENT_CHANNEL_CLOSED: 614 if (channel == sdp_server_l2cap_cid){ 615 // reset 616 sdp_server_l2cap_cid = 0; 617 618 // other request queued? 619 if (!sdp_server_l2cap_waiting_list_count) break; 620 621 // get first item 622 sdp_server_l2cap_cid = sdp_waiting_list_get(); 623 624 log_info("disconnect, accept queued cid 0x%04x, now %u waiting", sdp_server_l2cap_cid, sdp_server_l2cap_waiting_list_count); 625 626 // accept connection 627 sdp_server_response_size = 0; 628 l2cap_accept_connection(sdp_server_l2cap_cid); 629 } 630 break; 631 632 default: 633 // other event 634 break; 635 } 636 break; 637 638 default: 639 // other packet type 640 break; 641 } 642 } 643 644 #ifdef ENABLE_TESTING_SUPPORT 645 void sdp_server_set_single_record_response(bool enable){ 646 sdp_server_testing_single_record_reponse = enable; 647 } 648 #endif