ServiceDiscoveryProtocol.c 5.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
             LUFA Library
     Copyright (C) Dean Camera, 2010.
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this 
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in 
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting 
  documentation, and that the name of the author not be used in 
  advertising or publicity pertaining to distribution of the 
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

31
#define  INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C
32
33
#include "ServiceDiscoveryProtocol.h"

34
void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel)
35
36
37
38
{
	SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data;
	SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength);

39
	BT_SDP_DEBUG(1, "SDP Packet Received", NULL);
40
41
	BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU);
	BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength);
42
	
43
44
45
46
47
	printf("\r\n");
	for (uint8_t i = 0; i < SDPHeader->ParameterLength; i++)
	  printf("0x%02X ", *((uint8_t*)Data + sizeof(SDP_PDUHeader_t) + i));
	printf("\r\n");

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	switch (SDPHeader->PDU)
	{
		case SDP_PDU_SERVICESEARCHREQUEST:
			ServiceDiscovery_ProcessServiceSearch(SDPHeader);
			break;		
		case SDP_PDU_SERVICEATTRIBUTEREQUEST:
			ServiceDiscovery_ProcessServiceAttribute(SDPHeader);
			break;
		case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST:
			ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader);
			break;
	}
}

static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader)
{
	BT_SDP_DEBUG(1, "<< Service Search", NULL);
}

static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader)
{
	BT_SDP_DEBUG(1, "<< Service Attribute", NULL);
}

static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader)
{
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
81
	uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
	BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength);
82
83
	while (ServicePatternLength)
	{
84
		uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
85
86
87
		uint8_t UUID[16];
		
		memset(UUID, 0x00, sizeof(UUID));
88
89
		memcpy(UUID, CurrentParameter, UUIDLength);
		CurrentParameter += UUIDLength;
90
		
91
92
93
94
		BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
						UUIDLength,
		                UUID[15], UUID[14], UUID[13], UUID[12], UUID[11], UUID[10], UUID[9], UUID[8],
						UUID[7],  UUID[6],  UUID[5],  UUID[4],  UUID[3],  UUID[2],  UUID[1], UUID[0]);
95
	
96
		ServicePatternLength -= (UUIDLength + ElementHeaderSize);
97
98
	}
	
99
100
	uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter);
	BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize);
101

102
103
	uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
	BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength);
104
105
	while (AttributeIDListLength)
	{
106
107
		uint8_t  AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize);
		uint32_t Attribute = 0;
108
		
109
110
111
112
		memcpy(&Attribute, CurrentParameter, AttributeLength);
		CurrentParameter += AttributeLength;
		
		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
121
122
123
124
125
126
	uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07);
	*DataElementHeader += sizeof(uint8_t);
	
	*ElementHeaderSize = 1;
	
	uint32_t ElementValue;
	
127
128
129
	switch (SizeIndex)
	{
		case 0:
130
131
			ElementValue = 1;
			break;
132
		case 1:
133
134
			ElementValue = 2;
			break;
135
		case 2:
136
137
			ElementValue = 4;
			break;
138
		case 3:
139
140
			ElementValue = 8;
			break;
141
		case 4:
142
143
			ElementValue = 16;
			break;
144
		case 5:
145
146
147
148
			ElementValue = *((uint8_t*)*DataElementHeader);
			*DataElementHeader += sizeof(uint8_t);
			*ElementHeaderSize  = (1 + sizeof(uint8_t));
			break;
149
		case 6:
150
151
152
153
			ElementValue = *((uint16_t*)*DataElementHeader);
			*DataElementHeader += sizeof(uint16_t);
			*ElementHeaderSize  = (1 + sizeof(uint16_t));
			break;
154
		default:
155
156
157
158
			ElementValue = *((uint32_t*)*DataElementHeader);
			*DataElementHeader += sizeof(uint32_t);
			*ElementHeaderSize  = (1 + sizeof(uint32_t));
			break;
159
	}
160
161
	
	return ElementValue;
162
}