Update file contributor copyrights for 2012.
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / RFCOMMControl.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 /** \file
32 *
33 * RFCOMM multiplexer control layer module. This module handles multiplexer
34 * channel commands to the control DLCI in the RFCOMM layer, to open, configure,
35 * test and close logical RFCOMM channels.
36 */
37
38 #define INCLUDE_FROM_RFCOMM_CONTROL_C
39 #include "RFCOMMControl.h"
40
41 void RFCOMM_ProcessControlCommand(const uint8_t* Command,
42 Bluetooth_Channel_t* const ACLChannel)
43 {
44 const RFCOMM_Command_t* CommandHeader = (const RFCOMM_Command_t*)Command;
45 const uint8_t* CommandData = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);
46 uint8_t CommandDataLen = RFCOMM_GetVariableFieldValue(&CommandData);
47
48 switch (CommandHeader->Command)
49 {
50 case RFCOMM_Control_Test:
51 RFCOMM_ProcessTestCommand(CommandHeader, CommandDataLen, CommandData, ACLChannel);
52 break;
53 case RFCOMM_Control_FlowControlEnable:
54 RFCOMM_ProcessFCECommand(CommandHeader, CommandData, ACLChannel);
55 break;
56 case RFCOMM_Control_FlowControlDisable:
57 RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, ACLChannel);
58 break;
59 case RFCOMM_Control_ModemStatus:
60 RFCOMM_ProcessMSCCommand(CommandHeader, CommandDataLen, CommandData, ACLChannel);
61 break;
62 case RFCOMM_Control_RemotePortNegotiation:
63 RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, ACLChannel);
64 break;
65 case RFCOMM_Control_RemoteLineStatus:
66 RFCOMM_ProcessRLSCommand(CommandHeader, CommandData, ACLChannel);
67 break;
68 case RFCOMM_Control_DLCParameterNegotiation:
69 RFCOMM_ProcessDPNCommand(CommandHeader, CommandData, ACLChannel);
70 break;
71 default:
72 BT_RFCOMM_DEBUG(1, "<< Unknown Command");
73 break;
74 }
75 }
76
77 static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader,
78 const uint8_t CommandDataLen,
79 const uint8_t* CommandData,
80 Bluetooth_Channel_t* const ACLChannel)
81 {
82 const uint8_t* Params = (const uint8_t*)CommandData;
83
84 BT_RFCOMM_DEBUG(1, "<< TEST Command");
85
86 struct
87 {
88 RFCOMM_Command_t CommandHeader;
89 uint8_t Length;
90 uint8_t TestData[CommandDataLen];
91 } TestResponse;
92
93 /* Fill out the Test response data */
94 TestResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_Test, .EA = true, .CR = false};
95 TestResponse.Length = (CommandDataLen << 1) | 0x01;
96 memcpy(TestResponse.TestData, Params, CommandDataLen);
97
98 BT_RFCOMM_DEBUG(1, ">> TEST Response");
99
100 /* Send the PDN response to acknowledge the command */
101 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(TestResponse), &TestResponse, ACLChannel);
102 }
103
104 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader,
105 const uint8_t* CommandData,
106 Bluetooth_Channel_t* const ACLChannel)
107 {
108 BT_RFCOMM_DEBUG(1, "<< FCE Command");
109 }
110
111 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader,
112 const uint8_t* CommandData,
113 Bluetooth_Channel_t* const ACLChannel)
114 {
115 BT_RFCOMM_DEBUG(1, "<< FCD Command");
116 }
117
118 static void RFCOMM_ProcessMSCCommand(const RFCOMM_Command_t* const CommandHeader,
119 const uint8_t CommandDataLen,
120 const uint8_t* CommandData,
121 Bluetooth_Channel_t* const ACLChannel)
122 {
123 const RFCOMM_MSC_Parameters_t* Params = (const RFCOMM_MSC_Parameters_t*)CommandData;
124
125 BT_RFCOMM_DEBUG(1, "<< MSC %s", (CommandHeader->CR) ? "Command" : "Response");
126 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->Channel.DLCI);
127
128 /* Ignore status flags sent to the control channel */
129 if (Params->Channel.DLCI == RFCOMM_CONTROL_DLCI)
130 return;
131
132 /* Retrieve existing channel configuration data, if already opened */
133 RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->Channel.DLCI);
134
135 /* If the channel does not exist, abort */
136 if (RFCOMMChannel == NULL)
137 return;
138
139 /* Check if the MSC packet is a command or a response */
140 if (CommandHeader->CR)
141 {
142 /* Save the new channel signals to the channel state structure */
143 RFCOMMChannel->Remote.Signals = Params->Signals;
144 RFCOMMChannel->ConfigFlags |= RFCOMM_CONFIG_REMOTESIGNALS;
145
146 /* If the command contains the optional break signals field, store the value */
147 if (CommandDataLen == sizeof(RFCOMM_MSC_Parameters_t))
148 RFCOMMChannel->Remote.BreakSignal = Params->BreakSignal;
149
150 /* Notify the user application that the signals have been received */
151 RFCOMM_ChannelSignalsReceived(RFCOMMChannel);
152
153 struct
154 {
155 RFCOMM_Command_t CommandHeader;
156 uint8_t Length;
157 RFCOMM_MSC_Parameters_t Params;
158 } MSResponse;
159
160 /* Fill out the MS response data */
161 MSResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true, .CR = false};
162 MSResponse.Length = (CommandDataLen << 1) | 0x01;
163 memcpy(&MSResponse.Params, Params, sizeof(RFCOMM_MSC_Parameters_t));
164
165 BT_RFCOMM_DEBUG(1, ">> MSC Response");
166
167 /* Send the MSC response to acknowledge the command */
168 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH,
169 (sizeof(MSResponse) - sizeof(MSResponse.Params) + CommandDataLen), &MSResponse, ACLChannel);
170 }
171 else
172 {
173 /* Indicate that the remote device has acknowledged the sent signals */
174 RFCOMMChannel->ConfigFlags |= RFCOMM_CONFIG_LOCALSIGNALS;
175 }
176 }
177
178 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader,
179 const uint8_t* CommandData,
180 Bluetooth_Channel_t* const ACLChannel)
181 {
182 BT_RFCOMM_DEBUG(1, "<< RPN Command");
183 }
184
185 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader,
186 const uint8_t* CommandData,
187 Bluetooth_Channel_t* const ACLChannel)
188 {
189 BT_RFCOMM_DEBUG(1, "<< RLS Command");
190 }
191
192 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader,
193 const uint8_t* CommandData,
194 Bluetooth_Channel_t* const ACLChannel)
195 {
196 const RFCOMM_DPN_Parameters_t* Params = (const RFCOMM_DPN_Parameters_t*)CommandData;
197
198 BT_RFCOMM_DEBUG(1, "<< DPN Command");
199 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->DLCI);
200
201 /* Ignore parameter negotiations to the control channel */
202 if (Params->DLCI == RFCOMM_CONTROL_DLCI)
203 return;
204
205 /* Retrieve existing channel configuration data, if already opened */
206 RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->DLCI);
207
208 /* Check if the channel has no corresponding entry - remote did not open it first */
209 if (RFCOMMChannel == NULL)
210 {
211 /* Create a new entry in the channel table for the new channel */
212 RFCOMMChannel = RFCOMM_GetFreeChannelEntry(Params->DLCI);
213
214 /* No free entry was found, discard the request */
215 if (RFCOMMChannel == NULL)
216 {
217 BT_RFCOMM_DEBUG(2, "-- No Free Channel");
218 return;
219 }
220 }
221
222 /* Save the new channel configuration */
223 RFCOMMChannel->State = RFCOMM_Channel_Configure;
224 RFCOMMChannel->Priority = Params->Priority;
225 RFCOMMChannel->MTU = Params->MaximumFrameSize;
226
227 struct
228 {
229 RFCOMM_Command_t CommandHeader;
230 uint8_t Length;
231 RFCOMM_DPN_Parameters_t Params;
232 } DPNResponse;
233
234 /* Fill out the DPN response data */
235 DPNResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true, .CR = false};
236 DPNResponse.Length = (sizeof(DPNResponse.Params) << 1) | 0x01;
237 memcpy(&DPNResponse.Params, Params, sizeof(RFCOMM_DPN_Parameters_t));
238 DPNResponse.Params.ConvergenceLayer = 0x00; // TODO: Enable credit based transaction support
239
240 BT_RFCOMM_DEBUG(1, ">> DPN Response");
241
242 /* Send the DPN response to acknowledge the command */
243 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, ACLChannel);
244 }
245