Add LUFA-side channel open/close routines, add signalling handlers for the creation...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / BluetoothHCICommands.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_BLUETOOTHHCICOMMANDS_C
32 #include "BluetoothHCICommands.h"
33
34 static BT_HCICommand_Header_t HCICommandHeader;
35
36 uint8_t Bluetooth_HCIProcessingState;
37 static uint8_t Bluetooth_HCINextState;
38 static uint8_t Bluetooth_TempDeviceAddress[6];
39
40 void Bluetooth_HCITask(void)
41 {
42 switch (Bluetooth_HCIProcessingState)
43 {
44 case Bluetooth_ProcessEvents:
45 Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE);
46 Pipe_Unfreeze();
47
48 if (Pipe_IsReadWriteAllowed())
49 {
50 BT_HCIEvent_Header_t HCIEventHeader;
51
52 /* Read in the event header to fetch the event code and payload length */
53 Pipe_Read_Stream_LE(&HCIEventHeader, sizeof(HCIEventHeader));
54
55 /* Create a temporary buffer for the event parameters */
56 uint8_t EventParams[HCIEventHeader.ParameterLength];
57
58 /* Read in the event parameters into the temporary buffer */
59 Pipe_Read_Stream_LE(&EventParams, HCIEventHeader.ParameterLength);
60 Pipe_ClearIN();
61
62 switch (HCIEventHeader.EventCode)
63 {
64 case EVENT_COMMAND_COMPLETE:
65 Bluetooth_HCIProcessingState = Bluetooth_HCINextState;
66 break;
67 case EVENT_COMMAND_STATUS:
68 /* If the execution of a command failed, reset the stack */
69 if (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status)
70 Bluetooth_HCIProcessingState = Bluetooth_Init;
71 break;
72 case EVENT_CONNECTION_REQUEST:
73 /* Need to store the remote device's BT address in a temporary buffer for later use */
74 memcpy(Bluetooth_TempDeviceAddress,
75 &((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress,
76 sizeof(Bluetooth_TempDeviceAddress));
77
78 bool IsACLConnection = (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01);
79
80 /* Only accept the connection if it is a ACL (data) connection, a device is not already connected
81 and the user application has indicated that the connection should be allowed */
82 Bluetooth_HCIProcessingState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) ||
83 !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ?
84 Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection;
85 break;
86 case EVENT_PIN_CODE_REQUEST:
87 /* Need to store the remote device's BT address in a temporary buffer for later use */
88 memcpy(Bluetooth_TempDeviceAddress,
89 &((BT_HCIEvent_PinCodeReq_t*)&EventParams)->RemoteAddress,
90 sizeof(Bluetooth_TempDeviceAddress));
91
92 Bluetooth_HCIProcessingState = Bluetooth_Conn_SendPINCode;
93 break;
94 case EVENT_CONNECTION_COMPLETE:
95 /* Need to store the remote device's BT address in a temporary buffer for later use */
96 memcpy(Bluetooth_Connection.RemoteAddress,
97 &((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->RemoteAddress,
98 sizeof(Bluetooth_TempDeviceAddress));
99
100 /* Store the created connection handle and indicate that the connection has been established */
101 Bluetooth_Connection.ConnectionHandle = ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle;
102 Bluetooth_Connection.IsConnected = true;
103
104 Bluetooth_ConnectionComplete();
105 break;
106 case EVENT_DISCONNECTION_COMPLETE:
107 /* Device disconnected, indicate connection information no longer valid */
108 Bluetooth_Connection.IsConnected = false;
109
110 Bluetooth_DisconnectionComplete();
111
112 Bluetooth_HCIProcessingState = Bluetooth_Init;
113 break;
114 }
115 }
116
117 Pipe_Freeze();
118
119 break;
120 case Bluetooth_Init:
121 /* Reset the connection information structure to destroy any previous connection state */
122 memset(&Bluetooth_Connection, 0x00, sizeof(Bluetooth_Connection));
123
124 Bluetooth_HCIProcessingState = Bluetooth_Init_Reset;
125 break;
126 case Bluetooth_Init_Reset:
127 HCICommandHeader = (BT_HCICommand_Header_t)
128 {
129 OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_RESET},
130 ParameterLength: 0,
131 };
132
133 /* Send the command to reset the bluetooth dongle controller */
134 Bluetooth_SendHCICommand(NULL, 0);
135
136 Bluetooth_HCINextState = Bluetooth_Init_SetLocalName;
137 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
138 break;
139 case Bluetooth_Init_SetLocalName:
140 HCICommandHeader = (BT_HCICommand_Header_t)
141 {
142 OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME},
143 ParameterLength: 248,
144 };
145
146 /* Send the command to set the bluetooth dongle's name for other devices to see */
147 Bluetooth_SendHCICommand(Bluetooth_DeviceConfiguration.Name, strlen(Bluetooth_DeviceConfiguration.Name));
148
149 Bluetooth_HCINextState = Bluetooth_Init_SetDeviceClass;
150 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
151 break;
152 case Bluetooth_Init_SetDeviceClass:
153 HCICommandHeader = (BT_HCICommand_Header_t)
154 {
155 OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE},
156 ParameterLength: 3,
157 };
158
159 /* Send the command to set the class of the device for other devices to see */
160 Bluetooth_SendHCICommand(&Bluetooth_DeviceConfiguration.Class, 3);
161
162 Bluetooth_HCINextState = Bluetooth_Init_WriteScanEnable;
163 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
164 break;
165 case Bluetooth_Init_WriteScanEnable:
166 HCICommandHeader = (BT_HCICommand_Header_t)
167 {
168 OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE},
169 ParameterLength: 1,
170 };
171
172 uint8_t Interval = BT_SCANMODE_InquiryAndPageScans;
173
174 /* Send the command to set the remote device scanning mode */
175 Bluetooth_SendHCICommand(&Interval, 1);
176
177 Bluetooth_HCINextState = Bluetooth_ProcessEvents;
178 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
179 break;
180 case Bluetooth_Conn_AcceptConnection:
181 HCICommandHeader = (BT_HCICommand_Header_t)
182 {
183 OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST},
184 ParameterLength: sizeof(BT_HCICommand_AcceptConnectionReq_t),
185 };
186
187 /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave
188 connection role */
189 BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams;
190 memcpy(AcceptConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress,
191 sizeof(AcceptConnectionParams.RemoteAddress));
192 AcceptConnectionParams.SlaveRole = true;
193
194 /* Send the command to accept the remote connection request */
195 Bluetooth_SendHCICommand(&AcceptConnectionParams, sizeof(BT_HCICommand_AcceptConnectionReq_t));
196
197 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
198 break;
199 case Bluetooth_Conn_RejectConnection:
200 HCICommandHeader = (BT_HCICommand_Header_t)
201 {
202 OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST},
203 ParameterLength: sizeof(BT_HCICommand_RejectConnectionReq_t),
204 };
205
206 /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure
207 to accept the connection due to limited device resources or incorrect device address */
208 BT_HCICommand_RejectConnectionReq_t RejectConnectionParams;
209 memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(RejectConnectionParams.RemoteAddress));
210 RejectConnectionParams.Reason = Bluetooth_Connection.IsConnected ? ERROR_LIMITED_RESOURCES : ERROR_UNACCEPTABLE_BDADDR;
211
212 /* Send the command to reject the remote connection request */
213 Bluetooth_SendHCICommand(&RejectConnectionParams, sizeof(BT_HCICommand_RejectConnectionReq_t));
214
215 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
216 break;
217 case Bluetooth_Conn_SendPINCode:
218 HCICommandHeader = (BT_HCICommand_Header_t)
219 {
220 OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY},
221 ParameterLength: sizeof(BT_HCICommand_PinCodeResp_t),
222 };
223
224 /* Copy over the temporary BT device address saved from the PIN Code Request event, copy over the
225 local PIN authentication code to the response */
226 BT_HCICommand_PinCodeResp_t PINCodeRequestParams;
227 memcpy(PINCodeRequestParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(PINCodeRequestParams.RemoteAddress));
228 PINCodeRequestParams.PINCodeLength = strlen(Bluetooth_DeviceConfiguration.PINCode);
229 memcpy(PINCodeRequestParams.PINCode, Bluetooth_DeviceConfiguration.PINCode, sizeof(PINCodeRequestParams.PINCode));
230
231 /* Send the command to transmit the device's local PIN number for authentication */
232 Bluetooth_SendHCICommand(&PINCodeRequestParams, sizeof(BT_HCICommand_PinCodeResp_t));
233
234 Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
235 break;
236 }
237 }
238
239 static uint8_t Bluetooth_SendHCICommand(void* Parameters, uint16_t ParameterLength)
240 {
241 /* Need to reserve the amount of bytes given in the header for the complete payload */
242 uint8_t CommandBuffer[sizeof(HCICommandHeader) + HCICommandHeader.ParameterLength];
243
244 USB_ControlRequest = (USB_Request_Header_t)
245 {
246 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_DEVICE),
247 .bRequest = 0,
248 .wValue = 0,
249 .wIndex = 0,
250 .wLength = sizeof(CommandBuffer)
251 };
252
253 /* Copy over the HCI command header to the allocated buffer */
254 memcpy(CommandBuffer, &HCICommandHeader, sizeof(HCICommandHeader));
255
256 /* Zero out the parameter section of the response to ensure that any padding bytes do not expose private RAM contents */
257 memset(&CommandBuffer[sizeof(HCICommandHeader)], 0x00, HCICommandHeader.ParameterLength);
258
259 /* Copy over the command parameters (if any) to the command buffer - note, the number of actual source parameter bytes
260 may differ to those in the header; any difference in length is filled with 0x00 padding bytes */
261 memcpy(&CommandBuffer[sizeof(HCICommandHeader)], Parameters, ParameterLength);
262
263 Pipe_SelectPipe(PIPE_CONTROLPIPE);
264 return USB_Host_SendControlRequest(CommandBuffer);
265 }