1124758Semax/* 2124758Semax * sd.c 3124758Semax * 4124758Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5124758Semax * All rights reserved. 6124758Semax * 7124758Semax * Redistribution and use in source and binary forms, with or without 8124758Semax * modification, are permitted provided that the following conditions 9124758Semax * are met: 10124758Semax * 1. Redistributions of source code must retain the above copyright 11124758Semax * notice, this list of conditions and the following disclaimer. 12124758Semax * 2. Redistributions in binary form must reproduce the above copyright 13124758Semax * notice, this list of conditions and the following disclaimer in the 14124758Semax * documentation and/or other materials provided with the distribution. 15124758Semax * 16124758Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17124758Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18124758Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19124758Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20124758Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21124758Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22124758Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23124758Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24124758Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25124758Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26124758Semax * SUCH DAMAGE. 27124758Semax * 28124758Semax * $Id: sd.c,v 1.4 2004/01/13 01:54:39 max Exp $ 29124758Semax * $FreeBSD: releng/10.2/usr.sbin/bluetooth/sdpd/sd.c 185320 2008-11-25 21:54:42Z emax $ 30124758Semax */ 31124758Semax 32124758Semax#include <sys/queue.h> 33124758Semax#include <bluetooth.h> 34124758Semax#include <sdp.h> 35124758Semax#include <string.h> 36124758Semax#include "profile.h" 37124758Semax#include "provider.h" 38124758Semax 39124758Semaxstatic int32_t 40124758Semaxsd_profile_create_service_class_id_list( 41124758Semax uint8_t *buf, uint8_t const * const eob, 42124758Semax uint8_t const *data, uint32_t datalen) 43124758Semax{ 44124758Semax static uint16_t service_classes[] = { 45124758Semax SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER 46124758Semax }; 47124758Semax 48124758Semax return (common_profile_create_service_class_id_list( 49124758Semax buf, eob, 50124758Semax (uint8_t const *) service_classes, 51124758Semax sizeof(service_classes))); 52124758Semax} 53124758Semax 54124758Semaxstatic int32_t 55185320Semaxsd_profile_create_bluetooth_profile_descriptor_list( 56185320Semax uint8_t *buf, uint8_t const * const eob, 57185320Semax uint8_t const *data, uint32_t datalen) 58185320Semax{ 59185320Semax static uint16_t profile_descriptor_list[] = { 60185320Semax SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER, 61185320Semax 0x0100 62185320Semax }; 63185320Semax 64185320Semax return (common_profile_create_bluetooth_profile_descriptor_list( 65185320Semax buf, eob, 66185320Semax (uint8_t const *) profile_descriptor_list, 67185320Semax sizeof(profile_descriptor_list))); 68185320Semax} 69185320Semax 70185320Semaxstatic int32_t 71124758Semaxsd_profile_create_service_id( 72124758Semax uint8_t *buf, uint8_t const * const eob, 73124758Semax uint8_t const *data, uint32_t datalen) 74124758Semax{ 75124758Semax if (buf + 3 > eob) 76124758Semax return (-1); 77124758Semax 78124758Semax /* 79124758Semax * The ServiceID is a UUID that universally and uniquely identifies 80124758Semax * the service instance described by the service record. This service 81124758Semax * attribute is particularly useful if the same service is described 82124758Semax * by service records in more than one SDP server 83124758Semax */ 84124758Semax 85124758Semax SDP_PUT8(SDP_DATA_UUID16, buf); 86124758Semax SDP_PUT16(SDP_UUID_PROTOCOL_SDP, buf); /* XXX ??? */ 87124758Semax 88124758Semax return (3); 89124758Semax} 90124758Semax 91124758Semaxstatic int32_t 92124758Semaxsd_profile_create_service_name( 93124758Semax uint8_t *buf, uint8_t const * const eob, 94124758Semax uint8_t const *data, uint32_t datalen) 95124758Semax{ 96124758Semax static char service_name[] = "Bluetooth service discovery"; 97124758Semax 98124758Semax return (common_profile_create_string8( 99124758Semax buf, eob, 100124758Semax (uint8_t const *) service_name, strlen(service_name))); 101124758Semax} 102124758Semax 103124758Semaxstatic int32_t 104124758Semaxsd_profile_create_protocol_descriptor_list( 105124758Semax uint8_t *buf, uint8_t const * const eob, 106124758Semax uint8_t const *data, uint32_t datalen) 107124758Semax{ 108185320Semax if (buf + 12 > eob) 109124758Semax return (-1); 110124758Semax 111124758Semax SDP_PUT8(SDP_DATA_SEQ8, buf); 112185320Semax SDP_PUT8(10, buf); 113124758Semax 114124758Semax SDP_PUT8(SDP_DATA_SEQ8, buf); 115185320Semax SDP_PUT8(3, buf); 116124758Semax SDP_PUT8(SDP_DATA_UUID16, buf); 117124758Semax SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf); 118124758Semax 119185320Semax SDP_PUT8(SDP_DATA_SEQ8, buf); 120185320Semax SDP_PUT8(3, buf); 121185320Semax SDP_PUT8(SDP_DATA_UUID16, buf); 122185320Semax SDP_PUT16(SDP_UUID_PROTOCOL_SDP, buf); 123124758Semax 124185320Semax return (12); 125124758Semax} 126124758Semax 127124758Semaxstatic int32_t 128124758Semaxsd_profile_create_browse_group_list( 129124758Semax uint8_t *buf, uint8_t const * const eob, 130124758Semax uint8_t const *data, uint32_t datalen) 131124758Semax{ 132124758Semax if (buf + 5 > eob) 133124758Semax return (-1); 134124758Semax 135124758Semax SDP_PUT8(SDP_DATA_SEQ8, buf); 136124758Semax SDP_PUT8(3, buf); 137124758Semax 138124758Semax /* 139124758Semax * The top-level browse group ID, called PublicBrowseRoot and 140124758Semax * representing the root of the browsing hierarchy, has the value 141124758Semax * 00001002-0000-1000-8000-00805F9B34FB (UUID16: 0x1002) from the 142124758Semax * Bluetooth Assigned Numbers document 143124758Semax */ 144124758Semax 145124758Semax SDP_PUT8(SDP_DATA_UUID16, buf); 146124758Semax SDP_PUT16(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, buf); 147124758Semax 148124758Semax return (5); 149124758Semax} 150124758Semax 151124758Semaxstatic int32_t 152124758Semaxsd_profile_create_version_number_list( 153124758Semax uint8_t *buf, uint8_t const * const eob, 154124758Semax uint8_t const *data, uint32_t datalen) 155124758Semax{ 156124758Semax if (buf + 5 > eob) 157124758Semax return (-1); 158124758Semax 159124758Semax SDP_PUT8(SDP_DATA_SEQ8, buf); 160124758Semax SDP_PUT8(3, buf); 161124758Semax 162124758Semax /* 163124758Semax * The VersionNumberList is a data element sequence in which each 164124758Semax * element of the sequence is a version number supported by the SDP 165124758Semax * server. A version number is a 16-bit unsigned integer consisting 166124758Semax * of two fields. The higher-order 8 bits contain the major version 167124758Semax * number field and the low-order 8 bits contain the minor version 168124758Semax * number field. The initial version of SDP has a major version of 169124758Semax * 1 and a minor version of 0 170124758Semax */ 171124758Semax 172124758Semax SDP_PUT8(SDP_DATA_UINT16, buf); 173124758Semax SDP_PUT16(0x0100, buf); 174124758Semax 175124758Semax return (5); 176124758Semax} 177124758Semax 178124758Semaxstatic int32_t 179124758Semaxsd_profile_create_service_database_state( 180124758Semax uint8_t *buf, uint8_t const * const eob, 181124758Semax uint8_t const *data, uint32_t datalen) 182124758Semax{ 183124758Semax uint32_t change_state = provider_get_change_state(); 184124758Semax 185124758Semax if (buf + 5 > eob) 186124758Semax return (-1); 187124758Semax 188124758Semax SDP_PUT8(SDP_DATA_UINT32, buf); 189124758Semax SDP_PUT32(change_state, buf); 190124758Semax 191124758Semax return (5); 192124758Semax} 193124758Semax 194124758Semaxstatic attr_t sd_profile_attrs[] = { 195124758Semax { SDP_ATTR_SERVICE_RECORD_HANDLE, 196124758Semax common_profile_create_service_record_handle }, 197124758Semax { SDP_ATTR_SERVICE_CLASS_ID_LIST, 198124758Semax sd_profile_create_service_class_id_list }, 199185320Semax { SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST, 200185320Semax sd_profile_create_bluetooth_profile_descriptor_list }, 201124758Semax { SDP_ATTR_SERVICE_ID, 202124758Semax sd_profile_create_service_id }, 203124758Semax { SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST, 204124758Semax common_profile_create_language_base_attribute_id_list }, 205124758Semax { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET, 206124758Semax sd_profile_create_service_name }, 207124758Semax { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET, 208124758Semax sd_profile_create_service_name }, 209124758Semax { SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_PROVIDER_NAME_OFFSET, 210124758Semax common_profile_create_service_provider_name }, 211124758Semax { SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST, 212124758Semax sd_profile_create_protocol_descriptor_list }, 213124758Semax { SDP_ATTR_BROWSE_GROUP_LIST, 214124758Semax sd_profile_create_browse_group_list }, 215124758Semax { SDP_ATTR_VERSION_NUMBER_LIST, 216124758Semax sd_profile_create_version_number_list }, 217124758Semax { SDP_ATTR_SERVICE_DATABASE_STATE, 218124758Semax sd_profile_create_service_database_state }, 219124758Semax { 0, NULL } /* end entry */ 220124758Semax}; 221124758Semax 222124758Semaxprofile_t sd_profile_descriptor = { 223124758Semax SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER, 224124758Semax 0, 225124758Semax (profile_data_valid_p) NULL, 226124758Semax (attr_t const * const) &sd_profile_attrs 227124758Semax}; 228124758Semax 229