3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 #define INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C
32 #include "ServiceDiscoveryProtocol.h"
34 SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Name
, "SDP");
35 SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Description
, "BT Service Discovery");
36 SERVICE_ATTRIBUTE_8BIT_LEN(SDP_Attribute_Availability
, SDP_DATATYPE_UNSIGNED_INT
, 1, {0xFF});
37 const ServiceAttributeTable_t SDP_Attribute_Table
[] PROGMEM
=
39 {.AttributeID
= SDP_ATTRIBUTE_NAME
, .AttributeData
= &SDP_Attribute_Name
},
40 {.AttributeID
= SDP_ATTRIBUTE_DESCRIPTION
, .AttributeData
= &SDP_Attribute_Description
},
41 {.AttributeID
= SDP_ATTRIBUTE_AVAILABILITY
, .AttributeData
= &SDP_Attribute_Availability
},
42 SERVICE_ATTRIBUTE_TABLE_TERMINATOR
45 SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Name
, "RFCOMM");
46 SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Description
, "Virtual Serial");
47 SERVICE_ATTRIBUTE_8BIT_LEN(RFCOMM_Attribute_Availability
, SDP_DATATYPE_UNSIGNED_INT
, 1, {0xFF});
48 const ServiceAttributeTable_t RFCOMM_Attribute_Table
[] PROGMEM
=
50 {.AttributeID
= SDP_ATTRIBUTE_NAME
, .AttributeData
= &RFCOMM_Attribute_Name
},
51 {.AttributeID
= SDP_ATTRIBUTE_DESCRIPTION
, .AttributeData
= &RFCOMM_Attribute_Description
},
52 {.AttributeID
= SDP_ATTRIBUTE_AVAILABILITY
, .AttributeData
= &RFCOMM_Attribute_Availability
},
53 SERVICE_ATTRIBUTE_TABLE_TERMINATOR
56 const ServiceTable_t SDP_Services_Table
[] =
58 { // 128-bit UUID for the SDP service
59 .UUID
= {BASE_96BIT_UUID
, 0x01, 0x00, 0x00, 0x00},
60 .AttributeTable
= &SDP_Attribute_Table
,
62 { // 128-bit UUID for the RFCOMM service
63 .UUID
= {BASE_96BIT_UUID
, 0x03, 0x00, 0x00, 0x00},
64 .AttributeTable
= &RFCOMM_Attribute_Table
,
69 void ServiceDiscovery_ProcessPacket(void* Data
, Bluetooth_Channel_t
* Channel
)
71 SDP_PDUHeader_t
* SDPHeader
= (SDP_PDUHeader_t
*)Data
;
72 SDPHeader
->ParameterLength
= SwapEndian_16(SDPHeader
->ParameterLength
);
74 BT_SDP_DEBUG(1, "SDP Packet Received");
75 BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader
->PDU
);
76 BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader
->ParameterLength
);
78 switch (SDPHeader
->PDU
)
80 case SDP_PDU_SERVICESEARCHREQUEST
:
81 ServiceDiscovery_ProcessServiceSearch(SDPHeader
);
83 case SDP_PDU_SERVICEATTRIBUTEREQUEST
:
84 ServiceDiscovery_ProcessServiceAttribute(SDPHeader
);
86 case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST
:
87 ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader
);
92 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t
* SDPHeader
)
94 BT_SDP_DEBUG(1, "<< Service Search");
97 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t
* SDPHeader
)
99 BT_SDP_DEBUG(1, "<< Service Attribute");
102 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t
* SDPHeader
)
104 void* CurrentParameter
= ((void*)SDPHeader
+ sizeof(SDP_PDUHeader_t
));
106 BT_SDP_DEBUG(1, "<< Service Search Attribute");
108 uint8_t ElementHeaderSize
;
110 uint16_t ServicePatternLength
= ServiceDiscovery_GetDataElementSize(&CurrentParameter
, &ElementHeaderSize
);
111 BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength
);
112 while (ServicePatternLength
)
114 uint8_t UUIDLength
= ServiceDiscovery_GetDataElementSize(&CurrentParameter
, &ElementHeaderSize
);
115 uint8_t UUID
[16] = {BASE_96BIT_UUID
, 0x00, 0x00, 0x00, 0x00};
117 if (UUIDLength
<= 32)
118 memcpy(&UUID
[sizeof(UUID
) - sizeof(uint32_t)], CurrentParameter
, UUIDLength
);
120 memcpy(UUID
, CurrentParameter
, UUIDLength
);
122 CurrentParameter
+= UUIDLength
;
124 BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
126 UUID
[15], UUID
[14], UUID
[13], UUID
[12], UUID
[11], UUID
[10], UUID
[9], UUID
[8],
127 UUID
[7], UUID
[6], UUID
[5], UUID
[4], UUID
[3], UUID
[2], UUID
[1], UUID
[0]);
129 ServicePatternLength
-= (UUIDLength
+ ElementHeaderSize
);
132 uint16_t MaxAttributeSize
= ServiceDiscovery_Read16BitParameter(&CurrentParameter
);
133 BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize
);
135 uint16_t AttributeIDListLength
= ServiceDiscovery_GetDataElementSize(&CurrentParameter
, &ElementHeaderSize
);
136 BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength
);
137 while (AttributeIDListLength
)
139 uint8_t AttributeLength
= ServiceDiscovery_GetDataElementSize(&CurrentParameter
, &ElementHeaderSize
);
140 uint32_t Attribute
= 0;
142 memcpy(&Attribute
, CurrentParameter
, AttributeLength
);
143 CurrentParameter
+= AttributeLength
;
145 BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength
, Attribute
);
147 AttributeIDListLength
-= (AttributeLength
+ ElementHeaderSize
);
151 static uint32_t ServiceDiscovery_GetDataElementSize(void** DataElementHeader
, uint8_t* ElementHeaderSize
)
153 uint8_t SizeIndex
= (*((uint8_t*)*DataElementHeader
) & 0x07);
154 *DataElementHeader
+= sizeof(uint8_t);
156 *ElementHeaderSize
= 1;
158 uint32_t ElementValue
;
178 ElementValue
= *((uint8_t*)*DataElementHeader
);
179 *DataElementHeader
+= sizeof(uint8_t);
180 *ElementHeaderSize
= (1 + sizeof(uint8_t));
183 ElementValue
= *((uint16_t*)*DataElementHeader
);
184 *DataElementHeader
+= sizeof(uint16_t);
185 *ElementHeaderSize
= (1 + sizeof(uint16_t));
188 ElementValue
= *((uint32_t*)*DataElementHeader
);
189 *DataElementHeader
+= sizeof(uint32_t);
190 *ElementHeaderSize
= (1 + sizeof(uint32_t));