Fix MIT license language to make its intent clearer.
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / BluetoothACLPackets.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 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_BLUETOOTH_ACLPACKETS_C
32 #include "BluetoothACLPackets.h"
33
34 void Bluetooth_ProcessACLPackets(void)
35 {
36 Bluetooth_ACL_Header_t ACLPacketHeader;
37 Bluetooth_DataPacket_Header_t DataHeader;
38
39 Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE);
40 Pipe_SetToken(PIPE_TOKEN_IN);
41 Pipe_Unfreeze();
42
43 if (!(Pipe_IsReadWriteAllowed()))
44 {
45 Pipe_Freeze();
46 return;
47 }
48
49 Pipe_Read_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader));
50 Pipe_Read_Stream_LE(&DataHeader, sizeof(DataHeader));
51
52 BT_DEBUG("(ACL) Packet Received", NULL);
53 BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader.ConnectionHandle);
54 BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader.DataLength);
55 BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader.DestinationChannel);
56 BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader.PayloadLength);
57
58 if (DataHeader.DestinationChannel == BLUETOOTH_CHANNEL_SIGNALING)
59 {
60 Bluetooth_SignalCommand_Header_t SignalCommandHeader;
61 Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader));
62
63 switch (SignalCommandHeader.Code)
64 {
65 case BLUETOOTH_SIGNAL_CONNECTION_REQUEST:
66 Bluetooth_ProcessSignalPacket_ConnectionRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
67 break;
68 case BLUETOOTH_SIGNAL_CONFIGURATION_REQUEST:
69 Bluetooth_ProcessSignalPacket_ConfigurationRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
70 break;
71 case BLUETOOTH_SIGNAL_INFORMATION_REQUEST:
72 BT_DEBUG("(ACL) -- Information Request, Discarded", NULL);
73
74 Pipe_Discard_Stream(ACLPacketHeader.DataLength);
75 Pipe_ClearIN();
76 Pipe_Freeze();
77 break;
78 default:
79 BT_DEBUG("(ACL) >> Unknown Signaling Command 0x%02X", SignalCommandHeader.Code);
80
81 Pipe_Discard_Stream(ACLPacketHeader.DataLength);
82 Pipe_ClearIN();
83 Pipe_Freeze();
84 break;
85 }
86 }
87 else
88 {
89 uint8_t DataPayload[DataHeader.PayloadLength];
90 Pipe_Read_Stream_LE(&DataPayload, sizeof(DataPayload));
91 DataHeader.PayloadLength = 0;
92
93 BT_DEBUG("(ACL) -- Data Payload: ", NULL);
94 for (uint16_t B = 0; B < sizeof(DataPayload); B++)
95 printf("0x%02X ", DataPayload[B]);
96 BT_DEBUG("", NULL);
97
98 Pipe_Discard_Stream(ACLPacketHeader.DataLength);
99 Pipe_ClearIN();
100 Pipe_Freeze();
101 }
102 }
103
104 static inline void Bluetooth_ProcessSignalPacket_ConnectionRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
105 Bluetooth_DataPacket_Header_t* DataHeader,
106 Bluetooth_SignalCommand_Header_t* SignalCommandHeader)
107 {
108 Bluetooth_SignalCommand_ConnectionRequest_t ConnectionRequest;
109
110 Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest));
111
112 BT_DEBUG("(ACL) >> L2CAP Connection Request", NULL);
113 BT_DEBUG("(ACL) -- PSM: 0x%04X", ConnectionRequest.PSM);
114 BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionRequest.SourceChannel);
115
116 Pipe_ClearIN();
117 Pipe_Freeze();
118 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
119 Pipe_SetToken(PIPE_TOKEN_OUT);
120 Pipe_Unfreeze();
121
122 Bluetooth_SignalCommand_ConnectionResponse_t ConnectionResponse;
123
124 ACLPacketHeader->DataLength = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);
125 DataHeader->PayloadLength = sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);
126 DataHeader->DestinationChannel = BLUETOOTH_CHANNEL_SIGNALING;
127 SignalCommandHeader->Code = BLUETOOTH_SIGNAL_CONNECTION_RESPONSE;
128 SignalCommandHeader->Length = sizeof(ConnectionResponse);
129
130 Bluetooth_Channel_t* ChannelData = Bluetooth_InitChannelData(ConnectionRequest.SourceChannel, ConnectionRequest.PSM);
131
132 ConnectionResponse.Result = (ChannelData == NULL) ? BLUETOOTH_CONNECTION_REFUSED_RESOURCES :
133 BLUETOOTH_CONNECTION_SUCCESSFUL;
134 ConnectionResponse.DestinationChannel = ChannelData->LocalNumber;
135 ConnectionResponse.SourceChannel = ChannelData->RemoteNumber;
136 ConnectionResponse.Status = 0x00;
137
138 Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));
139 Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));
140 Pipe_Write_Stream_LE(SignalCommandHeader, sizeof(*SignalCommandHeader));
141 Pipe_Write_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
142
143 Pipe_ClearOUT();
144 Pipe_Freeze();
145
146 BT_DEBUG("(ACL) Packet Sent", NULL);
147 BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader->ConnectionHandle);
148 BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader->DataLength);
149 BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader->DestinationChannel);
150 BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader->PayloadLength);
151 BT_DEBUG("(ACL) >> L2CAP Connection Response", NULL);
152 BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionResponse.SourceChannel);
153 BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConnectionResponse.DestinationChannel);
154 }
155
156 static inline void Bluetooth_ProcessSignalPacket_ConfigurationRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
157 Bluetooth_DataPacket_Header_t* DataHeader,
158 Bluetooth_SignalCommand_Header_t* SignalCommandHeader)
159 {
160 Bluetooth_SignalCommand_ConfigurationRequest_t ConfigurationRequest;
161
162 Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));
163
164 BT_DEBUG("(ACL) >> L2CAP Configuration Request", NULL);
165 BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel);
166
167 Pipe_ClearIN();
168 Pipe_Freeze();
169 Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
170 Pipe_SetToken(PIPE_TOKEN_OUT);
171 Pipe_Unfreeze();
172
173 Bluetooth_SignalCommand_ConfigurationResponse_t ConfigurationResponse;
174
175 ACLPacketHeader->DataLength = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);
176 DataHeader->PayloadLength = sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);
177 DataHeader->DestinationChannel = BLUETOOTH_CHANNEL_SIGNALING;
178 SignalCommandHeader->Code = BLUETOOTH_SIGNAL_CONFIGURATION_RESPONSE;
179 SignalCommandHeader->Length = sizeof(ConfigurationResponse);
180
181 Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, CHANNEL_LOOKUP_BY_DESTINATION);
182
183 if (ChannelData != NULL)
184 ChannelData->State = Channel_Open;
185
186 // TODO: Add channel config data to the tail of ConfigurationResponse
187
188 ConfigurationResponse.SourceChannel = ChannelData->RemoteNumber;
189 ConfigurationResponse.Flags = 0x00;
190 ConfigurationResponse.Result = (ChannelData != NULL) ? BLUETOOTH_CONFIGURATION_SUCCESSFUL : BLUETOOTH_CONFIGURATION_REJECTED;
191
192 Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));
193 Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));
194 Pipe_Write_Stream_LE(SignalCommandHeader, sizeof(*SignalCommandHeader));
195 Pipe_Write_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
196
197 Pipe_ClearOUT();
198 Pipe_Freeze();
199
200 BT_DEBUG("(ACL) Packet Sent", NULL);
201 BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader->ConnectionHandle);
202 BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader->DataLength);
203 BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader->DestinationChannel);
204 BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader->PayloadLength);
205 BT_DEBUG("(ACL) >> L2CAP Configuration Response", NULL);
206 }