ccbfed41e5e942b713b63e7b29cfe6153521c0a3
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / ServiceDiscoveryProtocol.h
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 #ifndef _SERVICEDISCOVERYPROTOCOL_H_
32 #define _SERVICEDISCOVERYPROTOCOL_H_
33
34 /* Includes: */
35 #include <avr/io.h>
36 #include <string.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39
40 #include <LUFA/Common/Common.h>
41 #include <LUFA/Drivers/Peripheral/SerialStream.h>
42
43 #include "BluetoothStack.h"
44
45 /* Macros: */
46 #define BT_SDP_DEBUG(l, s, ...) do { if (SDP_DEBUG_LEVEL >= l) printf_P(PSTR("(SDP) " s "\r\n"), ##__VA_ARGS__); } while (0)
47 #define SDP_DEBUG_LEVEL 2
48
49 #define SDP_PDU_ERRORRESPONSE 0x01
50 #define SDP_PDU_SERVICESEARCHREQUEST 0x02
51 #define SDP_PDU_SERVICESEARCHRESPONSE 0x03
52 #define SDP_PDU_SERVICEATTRIBUTEREQUEST 0x04
53 #define SDP_PDU_SERVICEATTRIBUTERESPONSE 0x05
54 #define SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST 0x06
55 #define SDP_PDU_SERVICESEARCHATTRIBUTERESPONSE 0x07
56
57 #define SDP_ATTRIBUTE_NAME 0x0000
58 #define SDP_ATTRIBUTE_DESCRIPTION 0x0001
59 #define SDP_ATTRIBUTE_PROVIDER 0x0002
60 #define SDP_ATTRIBUTE_AVAILABILITY 0x0008
61
62 #define UUID_SIZE_BYTES 16
63 #define BASE_96BIT_UUID 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00
64
65 /** Defines a service attribute as a string of characters.
66 *
67 * \param name Name of the attribute (used to identify the attribute variable only)
68 * \param string String of characters to associate with the attribute
69 */
70 #define SERVICE_ATTRIBUTE_TEXT(name, string) SERVICE_ATTRIBUTE_LEN8(name, SDP_DATATYPE_String, sizeof(string), string)
71
72 /** Defines a service attribute with a contents that can fit into an 8-bit integer.
73 *
74 * \param name Name of the attribute (used to identify the attribute variable only)
75 * \param type Type of attribute contents, a value from the \ref ServiceDiscovery_DataTypes_t enum
76 * \param size Size of the data, in bytes
77 * \param ... Data to associate with the attribute
78 */
79 #define SERVICE_ATTRIBUTE_LEN8(name, type, size, ...) const ServiceAttributeData8Bit_t name PROGMEM = \
80 {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__}
81
82 /** Defines a service attribute with a contents that can fit into an 16-bit integer.
83 *
84 * \param name Name of the attribute (used to identify the attribute variable only)
85 * \param type Type of attribute contents, a value from the \ref ServiceDiscovery_DataTypes_t enum
86 * \param size Size of the data, in bytes
87 * \param ... Data to associate with the attribute
88 */
89 #define SERVICE_ATTRIBUTE_LEN16(name, type, size, ...) const ServiceAttributeData16Bit_t name PROGMEM = \
90 {.Header = (type | 6), .Size = size, .Data = __VA_ARGS__}
91
92 /** Defines a service attribute with a contents that can fit into an 32-bit integer.
93 *
94 * \param name Name of the attribute (used to identify the attribute variable only)
95 * \param type Type of attribute contents, a value from the \ref ServiceDiscovery_DataTypes_t enum
96 * \param size Size of the data, in bytes
97 * \param ... Data to associate with the attribute
98 */
99 #define SERVICE_ATTRIBUTE_LEN32(name, type, size, ...) const ServiceAttributeData32Bit_t name PROGMEM = \
100 {.Header = (type | 7), .Size = size, .Data = __VA_ARGS__}
101
102 /** Terminator for a service attribute table of type \ref ServiceAttributeTable_t. */
103 #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR {.Data = NULL}
104
105 /* Enums: */
106 enum ServiceDiscovery_DataTypes_t
107 {
108 SDP_DATATYPE_Nill = (0 << 3),
109 SDP_DATATYPE_UnsignedInt = (1 << 3),
110 SDP_DATATYPE_SignedInt = (2 << 3),
111 SDP_DATATYPE_UUID = (3 << 3),
112 SDP_DATATYPE_String = (4 << 3),
113 SDP_DATATYPE_Boolean = (5 << 3),
114 SDP_DATATYPE_Sequence = (6 << 3),
115 SDP_DATATYPE_Alternative = (7 << 3),
116 SDP_DATATYPE_URL = (8 << 3),
117 };
118
119 /* Type Defines: */
120 typedef struct
121 {
122 uint8_t PDU;
123 uint16_t TransactionID;
124 uint16_t ParameterLength;
125 } SDP_PDUHeader_t;
126
127 typedef struct
128 {
129 uint16_t AttributeID;
130 const void* Data;
131 } ServiceAttributeTable_t;
132
133 typedef struct
134 {
135 uint8_t UUID[16];
136 const void* AttributeTable;
137 } ServiceTable_t;
138
139 typedef struct
140 {
141 uint8_t Header;
142 uint32_t Size;
143 uint8_t Data[];
144 } ServiceAttributeData32Bit_t;
145
146 typedef struct
147 {
148 uint8_t Header;
149 uint16_t Size;
150 uint8_t Data[];
151 } ServiceAttributeData16Bit_t;
152
153 typedef struct
154 {
155 uint8_t Header;
156 uint8_t Size;
157 uint8_t Data[];
158 } ServiceAttributeData8Bit_t;
159
160 typedef struct
161 {
162 uint8_t Header;
163 uint8_t Data[];
164 } ServiceAttributeData_t;
165
166 /* Inline Functions: */
167 static inline uint16_t* ServiceDiscovery_AddDataElementHeader(uint8_t** BufferPos, const uint8_t Type)
168 {
169 **BufferPos = (6 | Type);
170 *BufferPos += 1;
171
172 uint16_t* SizePos = (uint16_t*)*BufferPos;
173 *SizePos = 0;
174
175 **BufferPos += 2;
176
177 return SizePos;
178 }
179
180 static inline uint16_t ServiceDiscovery_Read16BitParameter(const void** AttributeHeader)
181 {
182 uint16_t ParamValue = *((uint16_t*)*AttributeHeader);
183 *AttributeHeader += sizeof(uint16_t);
184 return ParamValue;
185 }
186
187 /* Function Prototypes: */
188 void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel);
189
190 #if defined(INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C)
191 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
192 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
193 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
194
195 static uint8_t ServiceDiscovery_ProcessAttributes(uint8_t UUIDList[][UUID_SIZE_BYTES], const uint8_t TotalUUIDs,
196 uint8_t* ResponseBuffer, uint8_t MaxResponseSize,
197 const void** CurrentParameter);
198 static uint8_t ServiceDiscovery_GetAttribute(uint8_t UUIDList[][UUID_SIZE_BYTES], const uint8_t TotalUUIDs,
199 const uint32_t Attribute, uint8_t** DataBuffer, uint8_t BufferLen);
200 static uint8_t ServiceDiscovery_GetUUIDList(uint8_t UUIDList[][UUID_SIZE_BYTES], const void** CurrentParameter);
201 static uint32_t ServiceDiscovery_GetDataElementSize(const void** AttributeHeader, uint8_t* ElementHeaderSize);
202 #endif
203
204 #endif