Add TEST RFCOMM command handler. Remove the RFCOMM channel UseUIFrame element, as...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / RFCOMMControl.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 /** \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, Bluetooth_Channel_t* const Channel)
42 {
43 const RFCOMM_Command_t* CommandHeader = (const RFCOMM_Command_t*)Command;
44 const uint8_t* CommandData = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);
45 uint8_t CommandDataLen = RFCOMM_GetVariableFieldValue(&CommandData);
46
47 switch (CommandHeader->Command)
48 {
49 case RFCOMM_Control_Test:
50 RFCOMM_ProcessTestCommand(CommandHeader, CommandDataLen, CommandData, Channel);
51 break;
52 case RFCOMM_Control_FlowControlEnable:
53 RFCOMM_ProcessFCECommand(CommandHeader, CommandData, Channel);
54 break;
55 case RFCOMM_Control_FlowControlDisable:
56 RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);
57 break;
58 case RFCOMM_Control_ModemStatus:
59 RFCOMM_ProcessMSCommand(CommandHeader, CommandDataLen, CommandData, Channel);
60 break;
61 case RFCOMM_Control_RemotePortNegotiation:
62 RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);
63 break;
64 case RFCOMM_Control_RemoteLineStatus:
65 RFCOMM_ProcessRLSCommand(CommandHeader, CommandData, Channel);
66 break;
67 case RFCOMM_Control_DLCParameterNegotiation:
68 RFCOMM_ProcessDPNCommand(CommandHeader, CommandData, Channel);
69 break;
70 default:
71 BT_RFCOMM_DEBUG(1, "<< Unknown Command");
72 break;
73 }
74 }
75
76 static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t CommandDataLen,
77 const uint8_t* CommandData, Bluetooth_Channel_t* const Channel)
78 {
79 const uint8_t* Params = (const uint8_t*)CommandData;
80
81 BT_RFCOMM_DEBUG(1, "<< TEST Command");
82
83 struct
84 {
85 RFCOMM_Command_t CommandHeader;
86 uint8_t Length;
87 uint8_t TestData[CommandDataLen];
88 } TestResponse;
89
90 /* Fill out the Test response data */
91 TestResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_Test, .EA = true};
92 TestResponse.Length = (CommandDataLen << 1) | 0x01;
93 memcpy(TestResponse.TestData, Params, CommandDataLen);
94
95 BT_RFCOMM_DEBUG(1, ">> TEST Response");
96
97 /* Send the PDN response to acknowledge the command */
98 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(TestResponse), &TestResponse, Channel);
99 }
100
101 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
102 Bluetooth_Channel_t* const Channel)
103 {
104 BT_RFCOMM_DEBUG(1, "<< FCE Command");
105 }
106
107 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
108 Bluetooth_Channel_t* const Channel)
109 {
110 BT_RFCOMM_DEBUG(1, "<< FCD Command");
111 }
112
113 static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t CommandDataLen,
114 const uint8_t* CommandData, Bluetooth_Channel_t* const Channel)
115 {
116 const RFCOMM_MS_Parameters_t* Params = (const RFCOMM_MS_Parameters_t*)CommandData;
117
118 BT_RFCOMM_DEBUG(1, "<< MS Command");
119 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->Channel.DLCI);
120
121 /* Ignore status flags sent to the control channel */
122 if (Params->Channel.DLCI == RFCOMM_CONTROL_DLCI)
123 return;
124
125 /* Retrieve existing channel configuration data, if already opened */
126 RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->Channel.DLCI);
127
128 /* If the channel does not exist, abort */
129 if (RFCOMMChannel == NULL)
130 return;
131
132 /* Save the new channel signals to the channel state structure */
133 RFCOMMChannel->Signals = Params->Signals;
134
135 /* If the command contains the optional break signals field, store the value */
136 if (CommandDataLen == sizeof(RFCOMM_MS_Parameters_t))
137 RFCOMMChannel->BreakSignals = Params->BreakSignals;
138
139 struct
140 {
141 RFCOMM_Command_t CommandHeader;
142 uint8_t Length;
143 RFCOMM_MS_Parameters_t Params;
144 } MSResponse;
145
146 /* Fill out the MS response data */
147 MSResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true};
148 MSResponse.Length = (CommandDataLen << 1) | 0x01;
149 MSResponse.Params = *Params;
150
151 BT_RFCOMM_DEBUG(1, ">> MS Response");
152
153 /* Send the PDN response to acknowledge the command */
154 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(MSResponse), &MSResponse, Channel);
155 }
156
157 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
158 Bluetooth_Channel_t* const Channel)
159 {
160 BT_RFCOMM_DEBUG(1, "<< RPN Command");
161 }
162
163 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
164 Bluetooth_Channel_t* const Channel)
165 {
166 BT_RFCOMM_DEBUG(1, "<< RLS Command");
167 }
168
169 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
170 Bluetooth_Channel_t* const Channel)
171 {
172 const RFCOMM_DPN_Parameters_t* Params = (const RFCOMM_DPN_Parameters_t*)CommandData;
173
174 BT_RFCOMM_DEBUG(1, "<< DPN Command");
175 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->DLCI);
176
177 /* Ignore parameter negotiations to the control channel */
178 if (Params->DLCI == RFCOMM_CONTROL_DLCI)
179 return;
180
181 /* Retrieve existing channel configuration data, if already opened */
182 RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->DLCI);
183
184 /* Check if the channel has no corresponding entry - remote did not open it first */
185 if (RFCOMMChannel == NULL)
186 {
187 /* Find a free entry in the RFCOMM channel multiplexer state array */
188 for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
189 {
190 /* If the channel's DLCI is zero, the channel state entry is free */
191 if (!(RFCOMM_Channels[i].DLCI))
192 {
193 RFCOMMChannel = &RFCOMM_Channels[i];
194 RFCOMMChannel->DLCI = Params->DLCI;
195 RFCOMMChannel->Signals = 0;
196 RFCOMMChannel->BreakSignals = 0;
197 break;
198 }
199 }
200
201 /* No free entry was found, discard the request */
202 if (RFCOMMChannel == NULL)
203 {
204 BT_RFCOMM_DEBUG(2, "-- No Free Channel");
205 return;
206 }
207 }
208
209 /* Save the new channel configuration */
210 RFCOMMChannel->State = RFCOMM_Channel_Open;
211 RFCOMMChannel->Priority = Params->Priority;
212 RFCOMMChannel->MTU = Params->MaximumFrameSize;
213
214 struct
215 {
216 RFCOMM_Command_t CommandHeader;
217 uint8_t Length;
218 RFCOMM_DPN_Parameters_t Params;
219 } DPNResponse;
220
221 /* Fill out the DPN response data */
222 DPNResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true};
223 DPNResponse.Length = (sizeof(DPNResponse.Params) << 1) | 0x01;
224 DPNResponse.Params = *Params;
225 DPNResponse.Params.ConvergenceLayer = 0x00; // TODO: Enable credit based transaction support
226
227 BT_RFCOMM_DEBUG(1, ">> DPN Response");
228
229 /* Send the PDN response to acknowledge the command */
230 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);
231 }