Switch over the BluetoothHost demo's Service Discovery code to pass around more gener...
[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 SDP_DATATYPE_NIL (0x00 << 3)
63 #define SDP_DATATYPE_UNSIGNED_INT (0x01 << 3)
64 #define SDP_DATATYPE_SIGNED_INT (0x02 << 3)
65 #define SDP_DATATYPE_UUID (0x03 << 3)
66 #define SDP_DATATYPE_TEXT (0x04 << 3)
67 #define SDP_DATATYPE_BOOLEAN (0x05 << 3)
68 #define SDP_DATATYPE_ELEMENT_SEQUENCE (0x06 << 3)
69 #define SDP_DATATYPE_ELEMENT_ALTERNATIVE (0x07 << 3)
70 #define SDP_DATATYPE_URL (0x08 << 3)
71
72 #define UUID_SIZE_BYTES 16
73 #define BASE_96BIT_UUID 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00
74
75 #define SERVICE_ATTRIBUTE_TEXT(name, string) SERVICE_ATTRIBUTE_LEN8(name, SDP_DATATYPE_TEXT, sizeof(string), string)
76 #define SERVICE_ATTRIBUTE_LEN8(name, type, size, ...) const ServiceAttributeData8Bit_t name PROGMEM = \
77 {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__}
78 #define SERVICE_ATTRIBUTE_LEN16(name, type, size, ...) const ServiceAttributeData16Bit_t name PROGMEM = \
79 {.Header = (type | 6), .Size = size, .Data = __VA_ARGS__}
80 #define SERVICE_ATTRIBUTE_LEN32(name, type, size, ...) const ServiceAttributeData32Bit_t name PROGMEM = \
81 {.Header = (type | 7), .Size = size, .Data = __VA_ARGS__}
82 #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR {.Data = NULL}
83
84 /* Type Defines: */
85 typedef struct
86 {
87 uint8_t PDU;
88 uint16_t TransactionID;
89 uint16_t ParameterLength;
90 } SDP_PDUHeader_t;
91
92 typedef struct
93 {
94 uint16_t AttributeID;
95 const void* Data;
96 } ServiceAttributeTable_t;
97
98 typedef struct
99 {
100 uint8_t UUID[16];
101 const void* AttributeTable;
102 } ServiceTable_t;
103
104 typedef struct
105 {
106 uint8_t Header;
107 uint32_t Size;
108 uint8_t Data[];
109 } ServiceAttributeData32Bit_t;
110
111 typedef struct
112 {
113 uint8_t Header;
114 uint16_t Size;
115 uint8_t Data[];
116 } ServiceAttributeData16Bit_t;
117
118 typedef struct
119 {
120 uint8_t Header;
121 uint8_t Size;
122 uint8_t Data[];
123 } ServiceAttributeData8Bit_t;
124
125 typedef struct
126 {
127 uint8_t Header;
128 uint8_t Data[];
129 } ServiceAttributeData_t;
130
131 /* Function Prototypes: */
132 void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel);
133
134 #if defined(INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C)
135 static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
136 static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
137 static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader, Bluetooth_Channel_t* Channel);
138
139 static inline uint16_t ServiceDiscovery_Read16BitParameter(const void** AttributeHeader)
140 {
141 uint16_t ParamValue = *((uint16_t*)*AttributeHeader);
142 *AttributeHeader += sizeof(uint16_t);
143 return ParamValue;
144 }
145
146 static uint8_t ServiceDiscovery_ProcessAttributes(uint8_t UUIDList[][UUID_SIZE_BYTES], const uint8_t TotalUUIDs,
147 uint8_t* ResponseBuffer, uint8_t MaxResponseSize,
148 const void** CurrentParameter);
149 static uint8_t ServiceDiscovery_GetAttribute(uint8_t UUIDList[][UUID_SIZE_BYTES], const uint8_t TotalUUIDs,
150 const uint32_t Attribute, uint8_t** DataBuffer, uint8_t BufferLen);
151 static uint8_t ServiceDiscovery_GetUUIDList(uint8_t UUIDList[][UUID_SIZE_BYTES], const void** CurrentParameter);
152 static uint32_t ServiceDiscovery_GetDataElementSize(const void** AttributeHeader, uint8_t* ElementHeaderSize);
153 #endif
154
155 #endif