2010-06-09 17:33:28 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 by Matthias Ringwald
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the Service Discovery Protocol Server
|
|
|
|
*/
|
|
|
|
|
2010-06-13 08:26:47 +00:00
|
|
|
#include "sdp.h"
|
2010-06-09 17:33:28 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-06-13 13:13:38 +00:00
|
|
|
#include <string.h>
|
2010-06-09 17:33:28 +00:00
|
|
|
|
|
|
|
#include <btstack/sdp_util.h>
|
|
|
|
#include "l2cap.h"
|
|
|
|
|
2010-06-11 21:12:14 +00:00
|
|
|
// max reserved ServiceRecordHandle
|
|
|
|
#define maxReservedServiceRecordHandle 0xffff
|
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
static void sdp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
|
|
|
|
|
2010-06-11 21:12:14 +00:00
|
|
|
// registered service records
|
|
|
|
linked_list_t sdp_service_records;
|
|
|
|
|
|
|
|
// our handles start after the reserved range
|
2010-06-13 08:26:47 +00:00
|
|
|
static uint32_t sdp_next_service_record_handle = maxReservedServiceRecordHandle + 2;
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// AttributeIDList used to remove ServiceRecordHandle
|
|
|
|
const uint8_t removeServiceRecordHandleAttributeIDList[] = { 0x36, 0x00, 0x05, 0x0A, 0x00, 0x01, 0xFF, 0xFF };
|
|
|
|
|
2010-07-25 12:28:23 +00:00
|
|
|
#define SDP_RESPONSE_BUFFER_SIZE HCI_ACL_3DH5_SIZE
|
|
|
|
static uint8_t sdp_response_buffer[SDP_RESPONSE_BUFFER_SIZE];
|
|
|
|
|
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
void sdp_init(){
|
|
|
|
// register with l2cap psm sevices
|
2010-11-22 17:33:33 +00:00
|
|
|
l2cap_register_service_internal(NULL, sdp_packet_handler, PSM_SDP, 1000);
|
2010-06-09 17:33:28 +00:00
|
|
|
}
|
|
|
|
|
2010-06-11 21:12:14 +00:00
|
|
|
uint32_t sdp_get_service_record_handle(uint8_t * record){
|
2010-06-14 19:32:01 +00:00
|
|
|
uint8_t * serviceRecordHandleAttribute = sdp_get_attribute_value_for_attribute_id(record, SDP_ServiceRecordHandle);
|
2010-06-11 21:12:14 +00:00
|
|
|
if (!serviceRecordHandleAttribute) return 0;
|
|
|
|
if (de_get_element_type(serviceRecordHandleAttribute) != DE_UINT) return 0;
|
|
|
|
if (de_get_size_type(serviceRecordHandleAttribute) != DE_SIZE_32) return 0;
|
|
|
|
return READ_NET_32(serviceRecordHandleAttribute, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
service_record_item_t * sdp_get_record_for_handle(uint32_t handle){
|
|
|
|
linked_item_t *it;
|
|
|
|
for (it = (linked_item_t *) sdp_service_records; it ; it = it->next){
|
|
|
|
service_record_item_t * item = (service_record_item_t *) it;
|
|
|
|
if (item->service_record_handle == handle){
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get next free, unregistered service record handle
|
|
|
|
uint32_t sdp_create_service_record_handle(){
|
|
|
|
uint32_t handle = 0;
|
|
|
|
do {
|
|
|
|
handle = sdp_next_service_record_handle++;
|
|
|
|
if (sdp_get_record_for_handle(handle)) handle = 0;
|
|
|
|
} while (handle == 0);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2011-05-05 20:10:23 +00:00
|
|
|
#ifdef EMBEDDED
|
|
|
|
|
|
|
|
// register service record internally - this special version doesn't copy the record, it cannot be freeed
|
|
|
|
// pre: AttributeIDs are in ascending order
|
|
|
|
// pre: ServiceRecordHandle is first attribute and valid
|
|
|
|
// pre: record
|
|
|
|
// @returns ServiceRecordHandle or 0 if registration failed
|
|
|
|
uint32_t sdp_register_service_internal(void *connection, service_record_item_t * record_item){
|
|
|
|
// get user record handle
|
|
|
|
uint32_t record_handle = record_itme->service_record_handle;
|
|
|
|
// validate service record handle is not in reserved range
|
|
|
|
if (record_handle <= maxReservedServiceRecordHandle) record_handle = 0;
|
|
|
|
// check if already registered
|
|
|
|
if (sdp_get_record_for_handle(record_handle)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// add to linked list
|
|
|
|
linked_list_add(&sdp_service_records, (linked_item_t *) record_item);
|
|
|
|
return record_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// register service record internally - the normal version creates a copy of the record
|
2010-06-11 21:12:14 +00:00
|
|
|
// pre: AttributeIDs are in ascending order => ServiceRecordHandle is first attribute if present
|
2010-06-09 17:33:28 +00:00
|
|
|
// @returns ServiceRecordHandle or 0 if registration failed
|
2010-07-19 17:53:46 +00:00
|
|
|
uint32_t sdp_register_service_internal(void *connection, uint8_t * record){
|
2010-06-11 21:12:14 +00:00
|
|
|
|
2010-06-12 20:59:22 +00:00
|
|
|
// dump for now
|
2010-11-22 17:33:33 +00:00
|
|
|
// printf("Register service record\n");
|
|
|
|
// de_dump_data_element(record);
|
2010-06-12 20:59:22 +00:00
|
|
|
|
2010-06-11 21:12:14 +00:00
|
|
|
// get user record handle
|
|
|
|
uint32_t record_handle = sdp_get_service_record_handle(record);
|
|
|
|
|
|
|
|
// validate service record handle is not in reserved range
|
|
|
|
if (record_handle <= maxReservedServiceRecordHandle) record_handle = 0;
|
|
|
|
|
|
|
|
// check if already in use
|
2010-06-13 21:21:27 +00:00
|
|
|
if (record_handle) {
|
|
|
|
if (sdp_get_record_for_handle(record_handle)) {
|
|
|
|
record_handle = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// create new handle if needed
|
|
|
|
if (!record_handle){
|
|
|
|
record_handle = sdp_create_service_record_handle();
|
|
|
|
}
|
|
|
|
|
2010-06-12 10:28:04 +00:00
|
|
|
// calculate size of new service record: DES (2 byte len)
|
2010-06-14 19:32:01 +00:00
|
|
|
// + ServiceRecordHandle attribute (UINT16 UINT32) + size of existing attributes
|
|
|
|
uint16_t recordSize = 3 + (3 + 5) + de_get_data_size(record);
|
2010-06-12 10:28:04 +00:00
|
|
|
|
2010-06-11 21:12:14 +00:00
|
|
|
// alloc memory for new service_record_item
|
2010-06-12 10:28:04 +00:00
|
|
|
service_record_item_t * newRecordItem = (service_record_item_t *) malloc(recordSize + sizeof(service_record_item_t));
|
2010-06-11 21:12:14 +00:00
|
|
|
if (!newRecordItem) return 0;
|
2010-06-13 08:38:41 +00:00
|
|
|
|
|
|
|
// link new service item to client connection
|
|
|
|
newRecordItem->connection = connection;
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// set new handle
|
|
|
|
newRecordItem->service_record_handle = record_handle;
|
|
|
|
|
|
|
|
// create updated service record
|
|
|
|
uint8_t * newRecord = (uint8_t *) &(newRecordItem->service_record);
|
|
|
|
|
|
|
|
// create DES for new record
|
|
|
|
de_create_sequence(newRecord);
|
|
|
|
|
|
|
|
// set service record handle
|
2010-06-14 19:32:01 +00:00
|
|
|
de_add_number(newRecord, DE_UINT, DE_SIZE_16, 0);
|
|
|
|
de_add_number(newRecord, DE_UINT, DE_SIZE_32, record_handle);
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// add other attributes
|
2010-06-13 09:58:11 +00:00
|
|
|
sdp_append_attributes_in_attributeIDList(record, (uint8_t *) removeServiceRecordHandleAttributeIDList, 0, recordSize, newRecord);
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// dump for now
|
2010-11-22 17:33:33 +00:00
|
|
|
// de_dump_data_element(newRecord);
|
|
|
|
// printf("reserved size %u, actual size %u\n", recordSize, de_get_len(newRecord));
|
2010-06-11 21:12:14 +00:00
|
|
|
|
|
|
|
// add to linked list
|
|
|
|
linked_list_add(&sdp_service_records, (linked_item_t *) newRecordItem);
|
|
|
|
return record_handle;
|
2010-06-09 17:33:28 +00:00
|
|
|
}
|
|
|
|
|
2011-05-05 20:10:23 +00:00
|
|
|
#endif
|
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
// unregister service record internally
|
2010-06-13 08:38:41 +00:00
|
|
|
//
|
|
|
|
// makes sure one client cannot remove service records of other clients
|
|
|
|
//
|
2010-07-19 17:53:46 +00:00
|
|
|
void sdp_unregister_service_internal(void *connection, uint32_t service_record_handle){
|
2010-06-11 21:12:14 +00:00
|
|
|
service_record_item_t * record_item = sdp_get_record_for_handle(service_record_handle);
|
2010-06-13 08:38:41 +00:00
|
|
|
if (record_item && record_item->connection == connection) {
|
2010-06-11 21:12:14 +00:00
|
|
|
linked_list_remove(&sdp_service_records, (linked_item_t *) record_item);
|
|
|
|
}
|
2010-06-09 17:33:28 +00:00
|
|
|
}
|
|
|
|
|
2010-06-13 08:38:41 +00:00
|
|
|
// remove all service record for a client connection
|
2010-07-19 17:53:46 +00:00
|
|
|
void sdp_unregister_services_for_connection(void *connection){
|
2010-06-13 08:38:41 +00:00
|
|
|
linked_item_t *it = (linked_item_t *) &sdp_service_records;
|
|
|
|
while (it->next){
|
2010-06-13 21:27:47 +00:00
|
|
|
service_record_item_t *record_item = (service_record_item_t *) it->next;
|
|
|
|
if (record_item->connection == connection){
|
2010-06-13 08:38:41 +00:00
|
|
|
it->next = it->next->next;
|
2010-06-13 21:27:47 +00:00
|
|
|
free(record_item);
|
2010-06-13 08:38:41 +00:00
|
|
|
} else {
|
|
|
|
it = it->next;
|
|
|
|
}
|
|
|
|
}
|
2010-06-13 08:26:47 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
// PDU
|
|
|
|
// PDU ID (1), Transaction ID (2), Param Length (2), Param 1, Param 2, ..
|
|
|
|
|
2010-06-12 18:41:28 +00:00
|
|
|
int sdp_create_error_response(uint16_t transaction_id, uint16_t error_code){
|
|
|
|
sdp_response_buffer[0] = SDP_ErrorResponse;
|
|
|
|
net_store_16(sdp_response_buffer, 1, transaction_id);
|
|
|
|
net_store_16(sdp_response_buffer, 3, 2);
|
|
|
|
net_store_16(sdp_response_buffer, 5, error_code); // invalid syntax
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
|
2010-06-18 20:53:42 +00:00
|
|
|
int sdp_handle_service_search_request(uint8_t * packet, uint16_t remote_mtu){
|
2010-06-12 18:21:16 +00:00
|
|
|
|
|
|
|
// get request details
|
|
|
|
uint16_t transaction_id = READ_NET_16(packet, 1);
|
|
|
|
// not used yet - uint16_t param_len = READ_NET_16(packet, 3);
|
|
|
|
uint8_t * serviceSearchPattern = &packet[5];
|
2010-06-13 09:32:29 +00:00
|
|
|
uint16_t serviceSearchPatternLen = de_get_len(serviceSearchPattern);
|
|
|
|
uint16_t maximumServiceRecordCount = READ_NET_16(packet, 5 + serviceSearchPatternLen);
|
|
|
|
uint8_t * continuationState = &packet[5+serviceSearchPatternLen+2];
|
|
|
|
|
2010-06-18 20:53:42 +00:00
|
|
|
// calc maxumumServiceRecordCount based on remote MTU
|
|
|
|
uint16_t maximumServiceRecordCount2 = (remote_mtu - (9+3))/4;
|
|
|
|
if (maximumServiceRecordCount2 < maximumServiceRecordCount) {
|
|
|
|
maximumServiceRecordCount = maximumServiceRecordCount2;
|
|
|
|
}
|
|
|
|
|
2010-06-13 09:32:29 +00:00
|
|
|
// continuation state contains index of next service record to examine
|
2010-06-13 13:13:38 +00:00
|
|
|
int continuation = 0;
|
2010-06-13 09:32:29 +00:00
|
|
|
uint16_t continuation_index = 0;
|
|
|
|
if (continuationState[0] == 2){
|
|
|
|
continuation_index = READ_NET_16(continuationState, 1);
|
|
|
|
}
|
2010-06-12 18:21:16 +00:00
|
|
|
|
|
|
|
// header
|
|
|
|
sdp_response_buffer[0] = SDP_ServiceSearchResponse;
|
|
|
|
net_store_16(sdp_response_buffer, 1, transaction_id);
|
|
|
|
|
|
|
|
// ServiceRecordHandleList at 9
|
|
|
|
uint16_t pos = 9;
|
2010-06-13 09:32:29 +00:00
|
|
|
uint16_t total_service_count = 0;
|
|
|
|
uint16_t current_service_count = 0;
|
|
|
|
uint16_t current_service_index = 0;
|
2010-06-12 18:21:16 +00:00
|
|
|
// for all service records that match
|
|
|
|
linked_item_t *it;
|
2010-06-13 09:32:29 +00:00
|
|
|
for (it = (linked_item_t *) sdp_service_records; it ; it = it->next, ++current_service_index){
|
2010-06-12 18:21:16 +00:00
|
|
|
service_record_item_t * item = (service_record_item_t *) it;
|
|
|
|
if (sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)){
|
2010-06-13 09:32:29 +00:00
|
|
|
|
|
|
|
// get total count
|
|
|
|
total_service_count++;
|
|
|
|
|
|
|
|
// add to list if index higher than last continuation index and space left
|
2010-06-13 13:13:38 +00:00
|
|
|
if (current_service_index >= continuation_index && !continuation) {
|
|
|
|
if ( current_service_count < maximumServiceRecordCount) {
|
|
|
|
net_store_32(sdp_response_buffer, pos, item->service_record_handle);
|
|
|
|
current_service_count++;
|
|
|
|
pos += 4;
|
|
|
|
} else {
|
|
|
|
// next time start with this one
|
|
|
|
continuation = 1;
|
|
|
|
continuation_index = current_service_index;
|
|
|
|
}
|
2010-06-13 09:32:29 +00:00
|
|
|
}
|
2010-06-12 18:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 13:13:38 +00:00
|
|
|
// Store continuation state
|
|
|
|
if (continuation) {
|
2010-06-13 09:32:29 +00:00
|
|
|
sdp_response_buffer[pos++] = 2;
|
|
|
|
net_store_16(sdp_response_buffer, pos, continuation_index);
|
|
|
|
pos += 2;
|
2010-06-13 13:13:38 +00:00
|
|
|
} else {
|
|
|
|
sdp_response_buffer[pos++] = 0;
|
2010-06-13 09:32:29 +00:00
|
|
|
}
|
2010-06-12 18:21:16 +00:00
|
|
|
|
2010-06-13 09:32:29 +00:00
|
|
|
// update header info
|
|
|
|
net_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload
|
|
|
|
net_store_16(sdp_response_buffer, 5, total_service_count);
|
|
|
|
net_store_16(sdp_response_buffer, 7, current_service_count);
|
2010-06-12 18:21:16 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2010-06-18 20:53:42 +00:00
|
|
|
int sdp_handle_service_attribute_request(uint8_t * packet, uint16_t remote_mtu){
|
2010-06-12 18:29:11 +00:00
|
|
|
|
|
|
|
// get request details
|
|
|
|
uint16_t transaction_id = READ_NET_16(packet, 1);
|
|
|
|
// not used yet - uint16_t param_len = READ_NET_16(packet, 3);
|
|
|
|
uint32_t serviceRecordHandle = READ_NET_32(packet, 5);
|
|
|
|
uint16_t maximumAttributeByteCount = READ_NET_16(packet, 9);
|
|
|
|
uint8_t * attributeIDList = &packet[11];
|
2010-06-13 09:58:11 +00:00
|
|
|
uint16_t attributeIDListLen = de_get_len(attributeIDList);
|
|
|
|
uint8_t * continuationState = &packet[11+attributeIDListLen];
|
|
|
|
|
2010-06-18 20:53:42 +00:00
|
|
|
// calc maximumAttributeByteCount based on remote MTU
|
|
|
|
uint16_t maximumAttributeByteCount2 = remote_mtu - (7+3);
|
|
|
|
if (maximumAttributeByteCount2 < maximumAttributeByteCount) {
|
|
|
|
maximumAttributeByteCount = maximumAttributeByteCount2;
|
|
|
|
}
|
|
|
|
|
2010-09-10 22:59:22 +00:00
|
|
|
// continuation state contains the offset into the complete response
|
|
|
|
uint16_t continuation_offset = 0;
|
2010-06-13 09:58:11 +00:00
|
|
|
if (continuationState[0] == 2){
|
2010-09-10 22:59:22 +00:00
|
|
|
continuation_offset = READ_NET_16(continuationState, 1);
|
2010-06-13 09:58:11 +00:00
|
|
|
}
|
2010-06-12 18:29:11 +00:00
|
|
|
|
2010-06-12 18:41:28 +00:00
|
|
|
// get service record
|
|
|
|
service_record_item_t * item = sdp_get_record_for_handle(serviceRecordHandle);
|
|
|
|
if (!item){
|
|
|
|
// service record handle doesn't exist
|
|
|
|
return sdp_create_error_response(transaction_id, 0x0002); /// invalid Service Record Handle
|
|
|
|
}
|
|
|
|
|
2010-06-12 18:29:11 +00:00
|
|
|
// header
|
|
|
|
sdp_response_buffer[0] = SDP_ServiceAttributeResponse;
|
|
|
|
net_store_16(sdp_response_buffer, 1, transaction_id);
|
|
|
|
|
|
|
|
// AttributeList - starts at offset 7
|
|
|
|
uint16_t pos = 7;
|
|
|
|
uint8_t *attributeList = &sdp_response_buffer[pos];
|
|
|
|
de_create_sequence(attributeList);
|
2010-09-10 22:59:22 +00:00
|
|
|
// copy ALL specified attributes
|
|
|
|
sdp_append_attributes_in_attributeIDList(item->service_record, attributeIDList, 0, SDP_RESPONSE_BUFFER_SIZE-7-3, attributeList);
|
2010-06-12 18:29:11 +00:00
|
|
|
pos += de_get_len(attributeList);
|
2010-09-10 22:59:22 +00:00
|
|
|
|
|
|
|
// handle continuation
|
|
|
|
if (continuation_offset){
|
|
|
|
memmove(&sdp_response_buffer[7], &sdp_response_buffer[7+continuation_offset], maximumAttributeByteCount);
|
|
|
|
pos -= continuation_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t attributeListByteCount = pos - 7;
|
2010-06-12 18:29:11 +00:00
|
|
|
|
2010-06-13 09:58:11 +00:00
|
|
|
// Continuation State
|
2010-09-10 22:59:22 +00:00
|
|
|
if (pos <= 7 + maximumAttributeByteCount) {
|
|
|
|
// complete
|
|
|
|
sdp_response_buffer[pos++] = 0;
|
|
|
|
} else {
|
|
|
|
pos = 7 + maximumAttributeByteCount;
|
2010-09-11 18:49:32 +00:00
|
|
|
attributeListByteCount = maximumAttributeByteCount;
|
2010-09-10 22:59:22 +00:00
|
|
|
continuation_offset += maximumAttributeByteCount;
|
2010-06-13 09:58:11 +00:00
|
|
|
sdp_response_buffer[pos++] = 2;
|
2010-09-10 22:59:22 +00:00
|
|
|
net_store_16(sdp_response_buffer, pos, (uint16_t) continuation_offset);
|
2010-06-13 09:58:11 +00:00
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update header
|
2010-06-13 10:30:43 +00:00
|
|
|
net_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload
|
2010-09-10 22:59:22 +00:00
|
|
|
net_store_16(sdp_response_buffer, 5, attributeListByteCount);
|
2010-06-12 18:29:11 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2010-06-18 20:53:42 +00:00
|
|
|
int sdp_handle_service_search_attribute_request(uint8_t * packet, uint16_t remote_mtu){
|
2010-06-11 21:46:15 +00:00
|
|
|
|
2010-06-11 22:05:38 +00:00
|
|
|
// get request details
|
|
|
|
uint16_t transaction_id = READ_NET_16(packet, 1);
|
2010-06-12 18:21:16 +00:00
|
|
|
// not used yet - uint16_t param_len = READ_NET_16(packet, 3);
|
2010-06-11 22:05:38 +00:00
|
|
|
uint8_t * serviceSearchPattern = &packet[5];
|
|
|
|
uint16_t serviceSearchPatternLen = de_get_len(serviceSearchPattern);
|
|
|
|
uint16_t maximumAttributeByteCount = READ_NET_16(packet, 5 + serviceSearchPatternLen);
|
|
|
|
uint8_t * attributeIDList = &packet[5+serviceSearchPatternLen+2];
|
2010-06-13 10:30:43 +00:00
|
|
|
uint16_t attributeIDListLen = de_get_len(attributeIDList);
|
|
|
|
uint8_t * continuationState = &packet[5+serviceSearchPatternLen+2+attributeIDListLen];
|
2010-06-18 20:53:42 +00:00
|
|
|
|
|
|
|
// calc maximumAttributeByteCount based on remote MTU
|
|
|
|
uint16_t maximumAttributeByteCount2 = remote_mtu - (7+5);
|
|
|
|
if (maximumAttributeByteCount2 < maximumAttributeByteCount) {
|
|
|
|
maximumAttributeByteCount = maximumAttributeByteCount2;
|
|
|
|
}
|
2010-06-16 19:42:10 +00:00
|
|
|
|
2010-06-13 10:30:43 +00:00
|
|
|
// continuation state contains index of next service record to examine
|
2010-09-11 18:49:32 +00:00
|
|
|
// continuation state contains the offset into this particular response
|
2010-06-13 10:30:43 +00:00
|
|
|
uint16_t continuation_service_index = 0;
|
2010-09-11 18:49:32 +00:00
|
|
|
uint16_t continuation_offset = 0;
|
2010-06-13 10:30:43 +00:00
|
|
|
int continuation = 0;
|
|
|
|
if (continuationState[0] == 4){
|
2010-09-11 18:49:32 +00:00
|
|
|
continuation_service_index = READ_NET_16(continuationState, 1);
|
|
|
|
continuation_offset = READ_NET_16(continuationState, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// AttributeLists - starts at offset 7
|
|
|
|
uint16_t pos = 7;
|
|
|
|
int complete_response = 0;
|
|
|
|
|
|
|
|
// get sum of all service attribute responses on first response and create DES header for it
|
|
|
|
// note: this fills the response buffer several times
|
|
|
|
uint16_t total_response_size = 0;
|
|
|
|
linked_item_t *it;
|
|
|
|
if (!continuation_service_index && !continuation_offset){
|
|
|
|
// for all service records that match
|
|
|
|
for (it = (linked_item_t *) sdp_service_records; it ; it = it->next){
|
|
|
|
service_record_item_t * item = (service_record_item_t *) it;
|
|
|
|
if (sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)){
|
|
|
|
uint8_t *attributeList = &sdp_response_buffer[0];
|
|
|
|
de_create_sequence(attributeList);
|
|
|
|
// copy ALL specified attributes
|
|
|
|
sdp_append_attributes_in_attributeIDList(item->service_record, attributeIDList, 0, SDP_RESPONSE_BUFFER_SIZE-7-3, attributeList);
|
|
|
|
total_response_size += de_get_len(attributeList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
de_store_descriptor_with_len(&sdp_response_buffer[pos], DE_DES, DE_SIZE_VAR_16, total_response_size);
|
|
|
|
pos += de_get_header_size(&sdp_response_buffer[pos]);
|
|
|
|
complete_response = total_response_size + 3 <= maximumAttributeByteCount; // all in one?
|
2010-06-13 10:30:43 +00:00
|
|
|
}
|
2010-06-11 21:46:15 +00:00
|
|
|
|
2010-09-11 18:49:32 +00:00
|
|
|
// also get highest index of matching record
|
|
|
|
int highest_matching_record = -1;
|
|
|
|
uint16_t current_service_index = 0;
|
|
|
|
// for all service records that match
|
|
|
|
for (it = (linked_item_t *) sdp_service_records; it ; it = it->next, ++current_service_index){
|
|
|
|
service_record_item_t * item = (service_record_item_t *) it;
|
|
|
|
if (sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)){
|
|
|
|
highest_matching_record = current_service_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-11 21:46:15 +00:00
|
|
|
// header
|
|
|
|
sdp_response_buffer[0] = SDP_ServiceSearchAttributeResponse;
|
|
|
|
net_store_16(sdp_response_buffer, 1, transaction_id);
|
|
|
|
|
2010-06-11 22:05:38 +00:00
|
|
|
// for all service records that match
|
2010-09-11 18:49:32 +00:00
|
|
|
current_service_index = 0;
|
2010-06-13 10:30:43 +00:00
|
|
|
for (it = (linked_item_t *) sdp_service_records; it ; it = it->next, ++current_service_index){
|
2010-06-11 22:05:38 +00:00
|
|
|
service_record_item_t * item = (service_record_item_t *) it;
|
2010-06-13 10:30:43 +00:00
|
|
|
if (current_service_index >= continuation_service_index ) {
|
|
|
|
if (sdp_record_matches_service_search_pattern(item->service_record, serviceSearchPattern)){
|
|
|
|
|
2010-09-11 18:49:32 +00:00
|
|
|
// create sequence and copy specified attributes
|
|
|
|
uint8_t *attributeList = &sdp_response_buffer[pos];
|
|
|
|
de_create_sequence(attributeList);
|
|
|
|
sdp_append_attributes_in_attributeIDList(item->service_record, attributeIDList, 0,SDP_RESPONSE_BUFFER_SIZE-7-3, attributeList);
|
|
|
|
uint16_t listLen = de_get_len(attributeList);
|
2010-06-15 18:25:10 +00:00
|
|
|
|
2010-09-11 18:49:32 +00:00
|
|
|
// handle continuation
|
|
|
|
if (continuation_offset){
|
|
|
|
memmove(&sdp_response_buffer[pos], &sdp_response_buffer[pos+continuation_offset], maximumAttributeByteCount);
|
|
|
|
listLen -= continuation_offset;
|
2010-06-13 10:30:43 +00:00
|
|
|
}
|
2010-09-11 18:49:32 +00:00
|
|
|
pos += listLen;
|
|
|
|
|
|
|
|
// complete response from this record?
|
|
|
|
if (pos - 7 <= maximumAttributeByteCount){
|
|
|
|
continuation_offset = 0;
|
|
|
|
current_service_index++;
|
|
|
|
if (current_service_index > highest_matching_record) {
|
|
|
|
// done
|
|
|
|
continuation = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (!complete_response){
|
|
|
|
continuation = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
continuation_offset += maximumAttributeByteCount;
|
|
|
|
if (!continuation_service_index && continuation_offset == maximumAttributeByteCount){
|
|
|
|
continuation_offset -= 3; // without initial header
|
|
|
|
}
|
|
|
|
complete_response = 0;
|
2010-06-13 10:30:43 +00:00
|
|
|
continuation = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-06-11 22:05:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-13 10:30:43 +00:00
|
|
|
// Continuation State
|
2010-09-11 18:49:32 +00:00
|
|
|
uint16_t attributeListByteCount = pos - 7;
|
|
|
|
if (continuation){
|
|
|
|
if (pos > 7 + maximumAttributeByteCount) {
|
|
|
|
pos = 7 + maximumAttributeByteCount;
|
|
|
|
attributeListByteCount = maximumAttributeByteCount;
|
|
|
|
}
|
2010-06-13 10:30:43 +00:00
|
|
|
sdp_response_buffer[pos++] = 4;
|
2010-09-11 18:49:32 +00:00
|
|
|
net_store_16(sdp_response_buffer, pos, (uint16_t) current_service_index);
|
2010-06-13 15:35:21 +00:00
|
|
|
pos += 2;
|
2010-09-11 18:49:32 +00:00
|
|
|
net_store_16(sdp_response_buffer, pos, continuation_offset);
|
2010-06-13 15:35:21 +00:00
|
|
|
pos += 2;
|
2010-06-13 10:30:43 +00:00
|
|
|
} else {
|
2010-09-11 18:49:32 +00:00
|
|
|
// complete
|
2010-06-13 10:30:43 +00:00
|
|
|
sdp_response_buffer[pos++] = 0;
|
|
|
|
}
|
2010-09-11 18:49:32 +00:00
|
|
|
|
2010-06-13 10:30:43 +00:00
|
|
|
// update header
|
|
|
|
net_store_16(sdp_response_buffer, 3, pos - 5); // size of variable payload
|
2010-09-11 18:49:32 +00:00
|
|
|
net_store_16(sdp_response_buffer, 5, attributeListByteCount); // AttributeListsByteCount
|
2010-06-11 21:46:15 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
static void sdp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
|
|
|
|
uint16_t transaction_id;
|
2010-06-09 18:41:25 +00:00
|
|
|
SDP_PDU_ID_t pdu_id;
|
2010-06-09 17:33:28 +00:00
|
|
|
uint16_t param_len;
|
2010-06-18 20:53:42 +00:00
|
|
|
uint16_t remote_mtu;
|
2010-06-11 21:37:23 +00:00
|
|
|
int pos = 5;
|
2010-06-09 17:33:28 +00:00
|
|
|
|
|
|
|
switch (packet_type) {
|
|
|
|
|
|
|
|
case L2CAP_DATA_PACKET:
|
|
|
|
pdu_id = packet[0];
|
|
|
|
transaction_id = READ_NET_16(packet, 1);
|
|
|
|
param_len = READ_NET_16(packet, 3);
|
2010-06-18 20:53:42 +00:00
|
|
|
remote_mtu = l2cap_get_remote_mtu_for_local_cid(channel);
|
2010-07-25 12:28:23 +00:00
|
|
|
// account for our buffer
|
|
|
|
if (remote_mtu > SDP_RESPONSE_BUFFER_SIZE){
|
|
|
|
remote_mtu = SDP_RESPONSE_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2010-06-22 17:00:55 +00:00
|
|
|
// printf("SDP Request: type %u, transaction id %u, len %u, mtu %u\n", pdu_id, transaction_id, param_len, remote_mtu);
|
2010-06-09 17:33:28 +00:00
|
|
|
switch (pdu_id){
|
2010-06-09 18:41:25 +00:00
|
|
|
|
|
|
|
case SDP_ServiceSearchRequest:
|
2010-06-18 20:53:42 +00:00
|
|
|
pos = sdp_handle_service_search_request(packet, remote_mtu);
|
2010-06-09 18:41:25 +00:00
|
|
|
break;
|
2010-06-12 18:21:16 +00:00
|
|
|
|
2010-06-09 18:41:25 +00:00
|
|
|
case SDP_ServiceAttributeRequest:
|
2010-06-18 20:53:42 +00:00
|
|
|
pos = sdp_handle_service_attribute_request(packet, remote_mtu);
|
2010-06-09 18:41:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDP_ServiceSearchAttributeRequest:
|
2010-06-18 20:53:42 +00:00
|
|
|
pos = sdp_handle_service_search_attribute_request(packet, remote_mtu);
|
2010-06-09 17:33:28 +00:00
|
|
|
break;
|
2010-06-09 18:41:25 +00:00
|
|
|
|
2010-06-09 17:33:28 +00:00
|
|
|
default:
|
2010-06-12 18:41:28 +00:00
|
|
|
pos = sdp_create_error_response(transaction_id, 0x0003); // invalid syntax
|
2010-06-09 17:33:28 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-06-12 19:17:12 +00:00
|
|
|
l2cap_send_internal(channel, sdp_response_buffer, pos);
|
2010-06-09 17:33:28 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HCI_EVENT_PACKET:
|
|
|
|
|
|
|
|
switch (packet[0]) {
|
|
|
|
|
|
|
|
case L2CAP_EVENT_INCOMING_CONNECTION:
|
|
|
|
// accept
|
|
|
|
l2cap_accept_connection_internal(channel);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// other event
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// other packet type
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-22 17:33:33 +00:00
|
|
|
#if 0
|
2010-06-13 13:13:38 +00:00
|
|
|
static uint8_t record[100];
|
|
|
|
static uint8_t request[100];
|
2010-09-11 18:49:32 +00:00
|
|
|
static uint8_t response[100];
|
2010-06-13 13:13:38 +00:00
|
|
|
static void dump_service_search_response(){
|
|
|
|
uint16_t nr_services = READ_NET_16(sdp_response_buffer, 7);
|
|
|
|
int i;
|
|
|
|
printf("Nr service handles: %u\n", nr_services);
|
|
|
|
for (i=0; i<nr_services;i++){
|
2010-09-11 18:49:32 +00:00
|
|
|
printf(" ServiceHandle %x\n", READ_NET_32(sdp_response_buffer, 9+i*4));
|
2010-06-13 13:13:38 +00:00
|
|
|
}
|
|
|
|
if (sdp_response_buffer[9 + nr_services * 4]){
|
2010-09-11 18:49:32 +00:00
|
|
|
printf(" Continuation index %u\n", READ_NET_16(sdp_response_buffer, 9+nr_services*4+1));
|
2010-06-13 13:13:38 +00:00
|
|
|
} else {
|
2010-09-11 18:49:32 +00:00
|
|
|
printf(" Continuation: NO\n");
|
2010-06-13 13:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-09 17:33:28 +00:00
|
|
|
|
2010-06-13 13:13:38 +00:00
|
|
|
void sdp_test(){
|
2010-06-18 20:53:42 +00:00
|
|
|
const uint16_t remote_mtu = 150;
|
2010-06-13 13:13:38 +00:00
|
|
|
|
|
|
|
// create two records with 2 attributes each
|
|
|
|
de_create_sequence(record);
|
2010-06-14 19:32:01 +00:00
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_32, 0x10001);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
|
|
|
|
de_add_number(record, DE_UUID, DE_SIZE_16, 0x0001);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList);
|
|
|
|
de_add_number(record, DE_UUID, DE_SIZE_16, 0x0001);
|
2010-06-13 13:13:38 +00:00
|
|
|
uint32_t handle_1 = sdp_register_service_internal(NULL, record);
|
|
|
|
|
2010-06-14 19:32:01 +00:00
|
|
|
de_create_sequence(record);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_ServiceRecordHandle);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_32, 0x10002);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_ServiceClassIDList);
|
|
|
|
de_add_number(record, DE_UUID, DE_SIZE_16, 0x0002);
|
|
|
|
de_add_number(record, DE_UINT, DE_SIZE_16, SDP_BrowseGroupList);
|
|
|
|
de_add_number(record, DE_UUID, DE_SIZE_16, 0x0001);
|
2010-09-11 18:49:32 +00:00
|
|
|
sdp_register_service_internal(NULL, record);
|
2010-06-13 13:13:38 +00:00
|
|
|
|
|
|
|
// sdp_handle_service_search_request
|
|
|
|
uint16_t transactionID = 1;
|
|
|
|
uint16_t nr_services = 1;
|
|
|
|
request[0] = SDP_ServiceSearchRequest;
|
|
|
|
net_store_16(request, 1, transactionID++); // transaction ID
|
|
|
|
uint8_t * serviceSearchPattern = &request[5];
|
|
|
|
de_create_sequence(serviceSearchPattern);
|
|
|
|
{
|
|
|
|
de_add_number(serviceSearchPattern, DE_UUID, DE_SIZE_16, 0x0001);
|
|
|
|
}
|
|
|
|
uint16_t serviceSearchPatternLen = de_get_len(serviceSearchPattern);
|
|
|
|
net_store_16(request, 5 + serviceSearchPatternLen, 1);
|
|
|
|
request[5 + serviceSearchPatternLen + 2] = 0;
|
2010-06-18 20:53:42 +00:00
|
|
|
sdp_handle_service_search_request(request, remote_mtu);
|
2010-06-13 13:13:38 +00:00
|
|
|
dump_service_search_response();
|
|
|
|
memcpy(request + 5 + serviceSearchPatternLen + 2, sdp_response_buffer + 9 + nr_services*4, 3);
|
2010-06-18 20:53:42 +00:00
|
|
|
sdp_handle_service_search_request(request, remote_mtu);
|
2010-06-13 13:13:38 +00:00
|
|
|
dump_service_search_response();
|
|
|
|
|
|
|
|
// sdp_handle_service_attribute_request
|
2010-06-13 13:47:50 +00:00
|
|
|
request[0] = SDP_ServiceAttributeRequest;
|
|
|
|
net_store_16(request, 1, transactionID++); // transaction ID
|
2010-06-13 15:35:21 +00:00
|
|
|
net_store_32(request, 5, handle_1); // record handle
|
2010-09-11 18:49:32 +00:00
|
|
|
net_store_16(request, 9, 10); // max bytes
|
2010-06-13 13:47:50 +00:00
|
|
|
uint8_t * attributeIDList = request + 11;
|
|
|
|
de_create_sequence(attributeIDList);
|
|
|
|
de_add_number(attributeIDList, DE_UINT, DE_SIZE_32, 0x0000ffff);
|
|
|
|
uint16_t attributeIDListLen = de_get_len(attributeIDList);
|
|
|
|
request[11+attributeIDListLen] = 0;
|
2010-09-11 18:49:32 +00:00
|
|
|
uint16_t response_pos = 0;
|
2010-06-13 15:35:21 +00:00
|
|
|
while(1) {
|
2010-06-18 20:53:42 +00:00
|
|
|
sdp_handle_service_attribute_request(request, remote_mtu);
|
2010-09-11 18:49:32 +00:00
|
|
|
|
|
|
|
uint16_t attributeListByteCount = READ_NET_16(sdp_response_buffer, 5);
|
|
|
|
memcpy( &response[response_pos], &sdp_response_buffer[7], attributeListByteCount);
|
|
|
|
response_pos += attributeListByteCount;
|
|
|
|
|
|
|
|
printf("Continuation %u, offset %u\n", sdp_response_buffer[7+attributeListByteCount], READ_NET_16(sdp_response_buffer, 7+attributeListByteCount+1));
|
|
|
|
if (sdp_response_buffer[7+attributeListByteCount] == 0) break;
|
|
|
|
memcpy(request+11+attributeIDListLen, sdp_response_buffer+7+attributeListByteCount, 3);
|
2010-06-13 15:35:21 +00:00
|
|
|
}
|
2010-09-11 18:49:32 +00:00
|
|
|
de_dump_data_element(response);
|
2010-06-13 13:13:38 +00:00
|
|
|
|
|
|
|
// sdp_handle_service_search_attribute_request
|
2010-06-13 15:35:21 +00:00
|
|
|
request[0] = SDP_ServiceSearchAttributeRequest;
|
|
|
|
net_store_16(request, 1, transactionID++); // transaction ID
|
|
|
|
de_create_sequence(serviceSearchPattern);
|
|
|
|
{
|
|
|
|
de_add_number(serviceSearchPattern, DE_UUID, DE_SIZE_16, 0x0001);
|
|
|
|
}
|
|
|
|
serviceSearchPatternLen = de_get_len(serviceSearchPattern);
|
2010-09-11 18:49:32 +00:00
|
|
|
net_store_16(request, 5 + serviceSearchPatternLen, 46); // MaximumAttributeByteCount:
|
2010-06-13 15:35:21 +00:00
|
|
|
attributeIDList = request + 5 + serviceSearchPatternLen + 2;
|
|
|
|
de_create_sequence(attributeIDList);
|
|
|
|
de_add_number(attributeIDList, DE_UINT, DE_SIZE_32, 0x0000ffff);
|
|
|
|
attributeIDListLen = de_get_len(attributeIDList);
|
|
|
|
request[5 + serviceSearchPatternLen + 2 + attributeIDListLen] = 0;
|
2010-09-11 18:49:32 +00:00
|
|
|
response_pos = 0;
|
2010-06-13 15:35:21 +00:00
|
|
|
while (1) {
|
2010-06-18 20:53:42 +00:00
|
|
|
sdp_handle_service_search_attribute_request(request, remote_mtu);
|
2010-09-11 18:49:32 +00:00
|
|
|
uint16_t attributeListByteCount = READ_NET_16(sdp_response_buffer, 5);
|
|
|
|
memcpy( &response[response_pos], &sdp_response_buffer[7], attributeListByteCount);
|
|
|
|
response_pos += attributeListByteCount;
|
|
|
|
|
|
|
|
printf("attributeListByteCount %u\n", attributeListByteCount);
|
|
|
|
printf("Continuation %u\n", sdp_response_buffer[7+attributeListByteCount]);
|
|
|
|
if (sdp_response_buffer[7+attributeListByteCount] == 0) break;
|
|
|
|
printf("Continuation {%u,%u}\n", READ_NET_16(sdp_response_buffer, 7+attributeListByteCount+1),
|
|
|
|
READ_NET_16(sdp_response_buffer, 7+attributeListByteCount+3));
|
|
|
|
memcpy(request+5 + serviceSearchPatternLen + 2 + attributeIDListLen, sdp_response_buffer+7+attributeListByteCount, 5);
|
2010-06-13 15:35:21 +00:00
|
|
|
}
|
2010-09-11 18:49:32 +00:00
|
|
|
de_dump_data_element(response);
|
|
|
|
|
2010-06-13 15:35:21 +00:00
|
|
|
/////
|
2010-06-13 13:13:38 +00:00
|
|
|
}
|
|
|
|
#endif
|