Added ability to search by Channel PSM to the GetChannelData() function in the Blueto...
[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
69 void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel)
70 {
71 SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data;
72 SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength);
73
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);
77
78 switch (SDPHeader->PDU)
79 {
80 case SDP_PDU_SERVICESEARCHREQUEST:
81 ServiceDiscovery_ProcessServiceSearch(SDPHeader);
82 break;
83 case SDP_PDU_SERVICEATTRIBUTEREQUEST:
84 ServiceDiscovery_ProcessServiceAttribute(SDPHeader);
85 break;
86 case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST:
87 ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader);
88 break;
89 }
90 }
91
92 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader)
93 {
94 BT_SDP_DEBUG(1, "<< Service Search");
95 }
96
97 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader)
98 {
99 BT_SDP_DEBUG(1, "<< Service Attribute");
100 }
101
102 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader)
103 {
104 void* CurrentParameter = ((void*)SDPHeader + sizeof(SDP_PDUHeader_t));
105
106 BT_SDP_DEBUG(1, "<< Service Search Attribute");
107
108 uint8_t ElementHeaderSize;
109
110 uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
111 BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength);
112 while (ServicePatternLength)
113 {
114 uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
115 uint8_t UUID[16] = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x00};
116
117 if (UUIDLength <= 32)
118 memcpy(&UUID[sizeof(UUID) - sizeof(uint32_t)], CurrentParameter, UUIDLength);
119 else
120 memcpy(UUID, CurrentParameter, UUIDLength);
121
122 CurrentParameter += UUIDLength;
123
124 BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
125 UUIDLength,
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]);
128
129 ServicePatternLength -= (UUIDLength + ElementHeaderSize);
130 }
131
132 uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter);
133 BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize);
134
135 uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
136 BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength);
137 while (AttributeIDListLength)
138 {
139 uint8_t AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
140 uint32_t Attribute = 0;
141
142 memcpy(&Attribute, CurrentParameter, AttributeLength);
143 CurrentParameter += AttributeLength;
144
145 BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength, Attribute);
146
147 AttributeIDListLength -= (AttributeLength + ElementHeaderSize);
148 }
149 }
150
151 static uint32_t ServiceDiscovery_GetDataElementSize(void** DataElementHeader, uint8_t* ElementHeaderSize)
152 {
153 uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07);
154 *DataElementHeader += sizeof(uint8_t);
155
156 *ElementHeaderSize = 1;
157
158 uint32_t ElementValue;
159
160 switch (SizeIndex)
161 {
162 case 0:
163 ElementValue = 1;
164 break;
165 case 1:
166 ElementValue = 2;
167 break;
168 case 2:
169 ElementValue = 4;
170 break;
171 case 3:
172 ElementValue = 8;
173 break;
174 case 4:
175 ElementValue = 16;
176 break;
177 case 5:
178 ElementValue = *((uint8_t*)*DataElementHeader);
179 *DataElementHeader += sizeof(uint8_t);
180 *ElementHeaderSize = (1 + sizeof(uint8_t));
181 break;
182 case 6:
183 ElementValue = *((uint16_t*)*DataElementHeader);
184 *DataElementHeader += sizeof(uint16_t);
185 *ElementHeaderSize = (1 + sizeof(uint16_t));
186 break;
187 default:
188 ElementValue = *((uint32_t*)*DataElementHeader);
189 *DataElementHeader += sizeof(uint32_t);
190 *ElementHeaderSize = (1 + sizeof(uint32_t));
191 break;
192 }
193
194 return ElementValue;
195 }