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