3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
38 #define INCLUDE_FROM_RFCOMM_CONTROL_C
39 #include "RFCOMMControl.h"
41 void RFCOMM_ProcessControlCommand(const uint8_t* Command
, Bluetooth_Channel_t
* const Channel
)
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
);
47 switch (CommandHeader
->Command
)
49 case RFCOMM_Control_Test
:
50 RFCOMM_ProcessTestCommand(CommandHeader
, CommandDataLen
, CommandData
, Channel
);
52 case RFCOMM_Control_FlowControlEnable
:
53 RFCOMM_ProcessFCECommand(CommandHeader
, CommandData
, Channel
);
55 case RFCOMM_Control_FlowControlDisable
:
56 RFCOMM_ProcessFCDCommand(CommandHeader
, CommandData
, Channel
);
58 case RFCOMM_Control_ModemStatus
:
59 RFCOMM_ProcessMSCommand(CommandHeader
, CommandDataLen
, CommandData
, Channel
);
61 case RFCOMM_Control_RemotePortNegotiation
:
62 RFCOMM_ProcessRPNCommand(CommandHeader
, CommandData
, Channel
);
64 case RFCOMM_Control_RemoteLineStatus
:
65 RFCOMM_ProcessRLSCommand(CommandHeader
, CommandData
, Channel
);
67 case RFCOMM_Control_DLCParameterNegotiation
:
68 RFCOMM_ProcessDPNCommand(CommandHeader
, CommandData
, Channel
);
71 BT_RFCOMM_DEBUG(1, "<< Unknown Command");
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
)
79 const uint8_t* Params
= (const uint8_t*)CommandData
;
81 BT_RFCOMM_DEBUG(1, "<< TEST Command");
85 RFCOMM_Command_t CommandHeader
;
87 uint8_t TestData
[CommandDataLen
];
90 /* Fill out the Test response data */
91 TestResponse
.CommandHeader
= (RFCOMM_Command_t
){.Command
= RFCOMM_Control_Test
, .EA
= true, .CR
= false};
92 TestResponse
.Length
= (CommandDataLen
<< 1) | 0x01;
93 memcpy(TestResponse
.TestData
, Params
, CommandDataLen
);
95 BT_RFCOMM_DEBUG(1, ">> TEST Response");
97 /* Send the PDN response to acknowledge the command */
98 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(TestResponse
), &TestResponse
, Channel
);
101 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
,
102 Bluetooth_Channel_t
* const Channel
)
104 BT_RFCOMM_DEBUG(1, "<< FCE Command");
107 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
,
108 Bluetooth_Channel_t
* const Channel
)
110 BT_RFCOMM_DEBUG(1, "<< FCD Command");
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
)
116 const RFCOMM_MS_Parameters_t
* Params
= (const RFCOMM_MS_Parameters_t
*)CommandData
;
118 BT_RFCOMM_DEBUG(1, "<< MS Command");
119 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->Channel
.DLCI
);
121 /* Ignore status flags sent to the control channel */
122 if (Params
->Channel
.DLCI
== RFCOMM_CONTROL_DLCI
)
125 /* Retrieve existing channel configuration data, if already opened */
126 RFCOMM_Channel_t
* RFCOMMChannel
= RFCOMM_GetChannelData(Params
->Channel
.DLCI
);
128 /* If the channel does not exist, abort */
129 if (RFCOMMChannel
== NULL
)
132 /* Save the new channel signals to the channel state structure */
133 RFCOMMChannel
->Remote
.Signals
= Params
->Signals
;
135 /* If the command contains the optional break signals field, store the value */
136 if (CommandDataLen
== sizeof(RFCOMM_MS_Parameters_t
))
137 RFCOMMChannel
->Remote
.BreakSignal
= Params
->BreakSignal
;
141 RFCOMM_Command_t CommandHeader
;
143 RFCOMM_MS_Parameters_t Params
;
146 /* Fill out the MS response data */
147 MSResponse
.CommandHeader
= (RFCOMM_Command_t
){.Command
= RFCOMM_Control_ModemStatus
, .EA
= true, .CR
= false};
148 MSResponse
.Length
= (CommandDataLen
<< 1) | 0x01;
149 memcpy(&MSResponse
.Params
, Params
, sizeof(RFCOMM_MS_Parameters_t
));
151 BT_RFCOMM_DEBUG(1, ">> MS Response");
153 /* Send the PDN response to acknowledge the command */
154 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
,
155 (sizeof(MSResponse
) - sizeof(MSResponse
.Params
) + CommandDataLen
), &MSResponse
, Channel
);
158 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
,
159 Bluetooth_Channel_t
* const Channel
)
161 BT_RFCOMM_DEBUG(1, "<< RPN Command");
164 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
,
165 Bluetooth_Channel_t
* const Channel
)
167 BT_RFCOMM_DEBUG(1, "<< RLS Command");
170 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t
* const CommandHeader
, const uint8_t* CommandData
,
171 Bluetooth_Channel_t
* const Channel
)
173 const RFCOMM_DPN_Parameters_t
* Params
= (const RFCOMM_DPN_Parameters_t
*)CommandData
;
175 BT_RFCOMM_DEBUG(1, "<< DPN Command");
176 BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params
->DLCI
);
178 /* Ignore parameter negotiations to the control channel */
179 if (Params
->DLCI
== RFCOMM_CONTROL_DLCI
)
182 /* Retrieve existing channel configuration data, if already opened */
183 RFCOMM_Channel_t
* RFCOMMChannel
= RFCOMM_GetChannelData(Params
->DLCI
);
185 /* Check if the channel has no corresponding entry - remote did not open it first */
186 if (RFCOMMChannel
== NULL
)
188 /* Find a free entry in the RFCOMM channel multiplexer state array */
189 for (uint8_t i
= 0; i
< RFCOMM_MAX_OPEN_CHANNELS
; i
++)
191 /* If the channel's DLCI is zero, the channel state entry is free */
192 if (!(RFCOMM_Channels
[i
].DLCI
))
194 RFCOMMChannel
= &RFCOMM_Channels
[i
];
195 RFCOMMChannel
->DLCI
= Params
->DLCI
;
196 RFCOMMChannel
->MTU
= 0xFFFF;
197 RFCOMMChannel
->Remote
.Signals
= 0 | (1 << 0);
198 RFCOMMChannel
->Remote
.BreakSignal
= 0 | (1 << 0);
199 RFCOMMChannel
->Local
.Signals
= RFCOMM_SIGNAL_RTC
| RFCOMM_SIGNAL_RTR
| RFCOMM_SIGNAL_DV
| (1 << 0);
200 RFCOMMChannel
->Local
.BreakSignal
= 0 | (1 << 0);
205 /* No free entry was found, discard the request */
206 if (RFCOMMChannel
== NULL
)
208 BT_RFCOMM_DEBUG(2, "-- No Free Channel");
213 /* Save the new channel configuration */
214 RFCOMMChannel
->State
= RFCOMM_Channel_Open
;
215 RFCOMMChannel
->Priority
= Params
->Priority
;
216 RFCOMMChannel
->MTU
= Params
->MaximumFrameSize
;
220 RFCOMM_Command_t CommandHeader
;
222 RFCOMM_DPN_Parameters_t Params
;
225 /* Fill out the DPN response data */
226 DPNResponse
.CommandHeader
= (RFCOMM_Command_t
){.Command
= RFCOMM_Control_DLCParameterNegotiation
, .EA
= true, .CR
= false};
227 DPNResponse
.Length
= (sizeof(DPNResponse
.Params
) << 1) | 0x01;
228 memcpy(&DPNResponse
.Params
, Params
, sizeof(RFCOMM_DPN_Parameters_t
));
229 DPNResponse
.Params
.ConvergenceLayer
= 0x00; // TODO: Enable credit based transaction support
231 BT_RFCOMM_DEBUG(1, ">> DPN Response");
233 /* Send the PDN response to acknowledge the command */
234 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI
, false, RFCOMM_Frame_UIH
, sizeof(DPNResponse
), &DPNResponse
, Channel
);