Make bluetooth SDP code correctly unpack the search UUIDs and parameters.
[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 void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel)
35 {
36 SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data;
37 SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength);
38
39 BT_SDP_DEBUG(1, "SDP Packet Received", NULL);
40 BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU);
41 BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength);
42
43 printf("\r\n");
44 for (uint8_t i = 0; i < SDPHeader->ParameterLength; i++)
45 printf("0x%02X ", *((uint8_t*)Data + sizeof(SDP_PDUHeader_t) + i));
46 printf("\r\n");
47
48 switch (SDPHeader->PDU)
49 {
50 case SDP_PDU_SERVICESEARCHREQUEST:
51 ServiceDiscovery_ProcessServiceSearch(SDPHeader);
52 break;
53 case SDP_PDU_SERVICEATTRIBUTEREQUEST:
54 ServiceDiscovery_ProcessServiceAttribute(SDPHeader);
55 break;
56 case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST:
57 ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader);
58 break;
59 }
60 }
61
62 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader)
63 {
64 BT_SDP_DEBUG(1, "<< Service Search", NULL);
65 }
66
67 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader)
68 {
69 BT_SDP_DEBUG(1, "<< Service Attribute", NULL);
70 }
71
72 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader)
73 {
74 void* CurrentParameter = ((void*)SDPHeader + sizeof(SDP_PDUHeader_t));
75
76 BT_SDP_DEBUG(1, "<< Service Search Attribute", NULL);
77
78 uint8_t ElementHeaderSize;
79
80 uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
81 BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength);
82 while (ServicePatternLength)
83 {
84 uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
85 uint8_t UUID[16];
86
87 memset(UUID, 0x00, sizeof(UUID));
88 memcpy(UUID, CurrentParameter, UUIDLength);
89 CurrentParameter += UUIDLength;
90
91 BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
92 UUIDLength,
93 UUID[15], UUID[14], UUID[13], UUID[12], UUID[11], UUID[10], UUID[9], UUID[8],
94 UUID[7], UUID[6], UUID[5], UUID[4], UUID[3], UUID[2], UUID[1], UUID[0]);
95
96 ServicePatternLength -= (UUIDLength + ElementHeaderSize);
97 }
98
99 uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter);
100 BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize);
101
102 uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
103 BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength);
104 while (AttributeIDListLength)
105 {
106 uint8_t AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
107 uint32_t Attribute = 0;
108
109 memcpy(&Attribute, CurrentParameter, AttributeLength);
110 CurrentParameter += AttributeLength;
111
112 BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength, Attribute);
113
114 AttributeIDListLength -= (AttributeLength + ElementHeaderSize);
115 }
116 }
117
118 static uint32_t ServiceDiscovery_GetDataElementSize(void** DataElementHeader, uint8_t* ElementHeaderSize)
119 {
120 uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07);
121 *DataElementHeader += sizeof(uint8_t);
122
123 *ElementHeaderSize = 1;
124
125 uint32_t ElementValue;
126
127 switch (SizeIndex)
128 {
129 case 0:
130 ElementValue = 1;
131 break;
132 case 1:
133 ElementValue = 2;
134 break;
135 case 2:
136 ElementValue = 4;
137 break;
138 case 3:
139 ElementValue = 8;
140 break;
141 case 4:
142 ElementValue = 16;
143 break;
144 case 5:
145 ElementValue = *((uint8_t*)*DataElementHeader);
146 *DataElementHeader += sizeof(uint8_t);
147 *ElementHeaderSize = (1 + sizeof(uint8_t));
148 break;
149 case 6:
150 ElementValue = *((uint16_t*)*DataElementHeader);
151 *DataElementHeader += sizeof(uint16_t);
152 *ElementHeaderSize = (1 + sizeof(uint16_t));
153 break;
154 default:
155 ElementValue = *((uint32_t*)*DataElementHeader);
156 *DataElementHeader += sizeof(uint32_t);
157 *ElementHeaderSize = (1 + sizeof(uint32_t));
158 break;
159 }
160
161 return ElementValue;
162 }