f7935cad0377e1e9770d7e9531df1a6cfbed5a9a
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / ServiceDiscoveryProtocol.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #define INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C
32 #include "ServiceDiscoveryProtocol.h"
33
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 =
38 {
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
43 };
44
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 =
49 {
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
54 };
55
56 const ServiceTable_t SDP_Services_Table[] =
57 {
58 { // 128-bit UUID for the SDP service
59 .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00},
60 .AttributeTable = &SDP_Attribute_Table,
61 },
62 { // 128-bit UUID for the RFCOMM service
63 .UUID = {BASE_96BIT_UUID, 0x03, 0x00, 0x00, 0x00},
64 .AttributeTable = &RFCOMM_Attribute_Table,
65 },
66 };
67
68 const uint8_t BaseUUID[] = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x00};
69
70
71 void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel)
72 {
73 SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data;
74 SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength);
75
76 BT_SDP_DEBUG(1, "SDP Packet Received");
77 BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU);
78 BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength);
79
80 switch (SDPHeader->PDU)
81 {
82 case SDP_PDU_SERVICESEARCHREQUEST:
83 ServiceDiscovery_ProcessServiceSearch(SDPHeader, Channel);
84 break;
85 case SDP_PDU_SERVICEATTRIBUTEREQUEST:
86 ServiceDiscovery_ProcessServiceAttribute(SDPHeader, Channel);
87 break;
88 case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST:
89 ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader, Channel);
90 break;
91 }
92 }
93
94 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel)
95 {
96 BT_SDP_DEBUG(1, "<< Service Search");
97 }
98
99 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel)
100 {
101 BT_SDP_DEBUG(1, "<< Service Attribute");
102 }
103
104 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel)
105 {
106 const void* CurrentParameter = ((void*)SDPHeader + sizeof(SDP_PDUHeader_t));
107
108 BT_SDP_DEBUG(1, "<< Service Search Attribute");
109
110 uint8_t ElementHeaderSize;
111
112 uint8_t UUIDList[12][16];
113 uint8_t TotalUUIDs = ServiceDiscovery_GetUUIDList(UUIDList, &CurrentParameter);
114 BT_SDP_DEBUG(2, "-- Total UUIDs: %d", TotalUUIDs);
115
116 uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter);
117 BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize);
118
119 uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
120 BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength);
121 while (AttributeIDListLength)
122 {
123 uint8_t AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
124 uint32_t Attribute = 0;
125
126 memcpy(&Attribute, CurrentParameter, AttributeLength);
127
128 BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength, Attribute);
129
130 AttributeIDListLength -= (AttributeLength + ElementHeaderSize);
131 CurrentParameter += AttributeLength;
132 }
133
134 struct
135 {
136 SDP_PDUHeader_t SDPHeader;
137 uint8_t ResponseData[100];
138 } ResponsePacket;
139
140 ResponsePacket.SDPHeader.PDU = SDP_PDU_SERVICESEARCHATTRIBUTERESPONSE;
141 ResponsePacket.SDPHeader.TransactionID = SDPHeader->TransactionID;
142
143 Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket.SDPHeader) + ResponsePacket.SDPHeader.ParameterLength),
144 Channel);
145 }
146
147 static uint8_t ServiceDiscovery_GetUUIDList(uint8_t UUIDList[12][16], const void** CurrentParameter)
148 {
149 uint8_t ElementHeaderSize;
150 uint8_t TotalUUIDs = 0;
151
152 uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(CurrentParameter, &ElementHeaderSize);
153 BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength);
154 while (ServicePatternLength)
155 {
156 uint8_t* CurrentUUID = UUIDList[TotalUUIDs++];
157
158 uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(CurrentParameter, &ElementHeaderSize);
159
160 memcpy(CurrentUUID, BaseUUID, sizeof(BaseUUID));
161
162 if (UUIDLength <= 32)
163 memcpy(&CurrentUUID[sizeof(BaseUUID) - sizeof(uint32_t)], *CurrentParameter, UUIDLength);
164 else
165 memcpy(CurrentUUID, *CurrentParameter, UUIDLength);
166
167 BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
168 UUIDLength,
169 CurrentUUID[15], CurrentUUID[14], CurrentUUID[13], CurrentUUID[12],
170 CurrentUUID[11], CurrentUUID[10], CurrentUUID[9], CurrentUUID[8],
171 CurrentUUID[7], CurrentUUID[6], CurrentUUID[5], CurrentUUID[4],
172 CurrentUUID[3], CurrentUUID[2], CurrentUUID[1], CurrentUUID[0]);
173
174 ServicePatternLength -= (UUIDLength + ElementHeaderSize);
175 *CurrentParameter += UUIDLength;
176 }
177
178 return TotalUUIDs;
179 }
180
181 static uint32_t ServiceDiscovery_GetDataElementSize(const void** DataElementHeader, uint8_t* ElementHeaderSize)
182 {
183 uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07);
184 *DataElementHeader += sizeof(uint8_t);
185
186 *ElementHeaderSize = 1;
187
188 uint32_t ElementValue;
189
190 switch (SizeIndex)
191 {
192 case 5:
193 ElementValue = *((uint8_t*)*DataElementHeader);
194 *DataElementHeader += sizeof(uint8_t);
195 *ElementHeaderSize = (1 + sizeof(uint8_t));
196 break;
197 case 6:
198 ElementValue = *((uint16_t*)*DataElementHeader);
199 *DataElementHeader += sizeof(uint16_t);
200 *ElementHeaderSize = (1 + sizeof(uint16_t));
201 break;
202 case 7:
203 ElementValue = *((uint32_t*)*DataElementHeader);
204 *DataElementHeader += sizeof(uint32_t);
205 *ElementHeaderSize = (1 + sizeof(uint32_t));
206 break;
207 default:
208 ElementValue = (1UL << SizeIndex);
209 break;
210 }
211
212 return ElementValue;
213 }