23a18765fb3d6aaf65286f6e7fca0e58b1c19a1c
[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 uint16_t ControlDataLen = RFCOMM_GetFrameDataLength(CommandData);
46
47 /* Adjust the command data pointer to skip over the variable size field */
48 CommandData += (ControlDataLen < 128) ? 1 : 2;
49
50 switch (CommandHeader->Command)
51 {
52 case RFCOMM_Control_Test:
53 RFCOMM_ProcessTestCommand(CommandHeader, CommandData, Channel);
54 break;
55 case RFCOMM_Control_FlowControlEnable:
56 RFCOMM_ProcessFCECommand(CommandHeader, CommandData, Channel);
57 break;
58 case RFCOMM_Control_FlowControlDisable:
59 RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);
60 break;
61 case RFCOMM_Control_ModemStatus:
62 RFCOMM_ProcessMSCommand(CommandHeader, CommandData, Channel);
63 break;
64 case RFCOMM_Control_RemotePortNegotiation:
65 RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);
66 break;
67 case RFCOMM_Control_RemoteLineStatus:
68 RFCOMM_ProcessRLSCommand(CommandHeader, CommandData, Channel);
69 break;
70 case RFCOMM_Control_DLCParameterNegotiation:
71 RFCOMM_ProcessDPNCommand(CommandHeader, CommandData, Channel);
72 break;
73 default:
74 BT_RFCOMM_DEBUG(1, "<< Unknown Command");
75 break;
76 }
77 }
78
79 static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
80 Bluetooth_Channel_t* const Channel)
81 {
82 BT_RFCOMM_DEBUG(1, "<< TEST Command");
83 }
84
85 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
86 Bluetooth_Channel_t* const Channel)
87 {
88 BT_RFCOMM_DEBUG(1, "<< FCE Command");
89 }
90
91 static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
92 Bluetooth_Channel_t* const Channel)
93 {
94 BT_RFCOMM_DEBUG(1, "<< FCD Command");
95 }
96
97 static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
98 Bluetooth_Channel_t* const Channel)
99 {
100 BT_RFCOMM_DEBUG(1, "<< MS Command");
101 }
102
103 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
104 Bluetooth_Channel_t* const Channel)
105 {
106 BT_RFCOMM_DEBUG(1, "<< RPN Command");
107 }
108
109 static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
110 Bluetooth_Channel_t* const Channel)
111 {
112 BT_RFCOMM_DEBUG(1, "<< RLS Command");
113 }
114
115 static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
116 Bluetooth_Channel_t* const Channel)
117 {
118 const RFCOMM_DPN_Parameters_t* Params = (const RFCOMM_DPN_Parameters_t*)CommandData;
119
120 BT_RFCOMM_DEBUG(1, "<< DPN Command");
121 BT_RFCOMM_DEBUG(2, "-- Config DLCI: 0x%02X", Params->DLCI);
122
123 /* Ignore parameter negotiations to the control channel */
124 if (Params->DLCI == RFCOMM_CONTROL_DLCI)
125 return;
126
127 /* Retrieve existing channel configuration data, if already opened */
128 RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->DLCI);
129
130 /* Check if the channel has no corresponding entry - remote did not open it first */
131 if (RFCOMMChannel == NULL)
132 {
133 /* Find a free entry in the RFCOMM channel multiplexer state array */
134 for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
135 {
136 /* If the channel's DLCI is zero, the channel state entry is free */
137 if (!(RFCOMM_Channels[i].DLCI))
138 {
139 RFCOMMChannel = &RFCOMM_Channels[i];
140 RFCOMMChannel->DLCI = Params->DLCI;
141 break;
142 }
143 }
144
145 /* No free entry was found, discard the request */
146 if (RFCOMMChannel == NULL)
147 {
148 BT_RFCOMM_DEBUG(2, "-- No Free Channel");
149 return;
150 }
151 }
152
153 /* Save the new channel configuration */
154 RFCOMMChannel->State = RFCOMM_Channel_Open;
155 RFCOMMChannel->Priority = Params->Priority;
156 RFCOMMChannel->UseUIFrames = (Params->FrameType != 0);
157 RFCOMMChannel->RemoteMTU = Params->MaximumFrameSize;
158
159 struct
160 {
161 RFCOMM_Command_t CommandHeader;
162 uint8_t Length;
163 RFCOMM_DPN_Parameters_t Params;
164 } DPNResponse;
165
166 /* Fill out the DPN response data */
167 DPNResponse.CommandHeader.Command = CommandHeader->Command;
168 DPNResponse.CommandHeader.EA = true;
169 DPNResponse.Length = (sizeof(DPNResponse.Params) << 1) | 0x01;
170 DPNResponse.Params = *Params;
171
172 BT_RFCOMM_DEBUG(1, ">> DPN Response");
173
174 /* Send the PDN response to acknowledge the command */
175 RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);
176 }