Update all demos, projects and bootloaders to indent all function parameters, one...
[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 /** \file
32 *
33 * Bluetooth HCI layer management code. This module manages the overall
34 * Bluetooth stack connection state to and from other devices, processes
35 * received events from the Bluetooth controller, and issues commands to
36 * modify the controller's configuration, such as the broadcast name of the
37 * device.
38 */
39
40 /*
41 TODO: Add local to remote device connections
42 */
43
44 #define INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C
45 #include "BluetoothHCICommands.h"
46
47 /** Temporary Bluetooth Device Address, for HCI responses which much include the destination address */
48 static uint8_t Bluetooth_TempDeviceAddress[6];
49
50 /** Bluetooth HCI processing task. This task should be called repeatedly the main Bluetooth
51 * stack task to manage the HCI processing state.
52 */
53 void Bluetooth_HCITask(void)
54 {
55 BT_HCICommand_Header_t HCICommandHeader;
56
57 switch (Bluetooth_State.CurrentHCIState)
58 {
59 case Bluetooth_ProcessEvents:
60 Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE);
61 Pipe_Unfreeze();
62
63 if (Pipe_IsReadWriteAllowed())
64 {
65 BT_HCIEvent_Header_t HCIEventHeader;
66
67 /* Read in the event header to fetch the event code and payload length */
68 Pipe_Read_Stream_LE(&HCIEventHeader, sizeof(HCIEventHeader));
69
70 /* Create a temporary buffer for the event parameters */
71 uint8_t EventParams[HCIEventHeader.ParameterLength];
72
73 /* Read in the event parameters into the temporary buffer */
74 Pipe_Read_Stream_LE(&EventParams, HCIEventHeader.ParameterLength);
75 Pipe_ClearIN();
76
77 BT_HCI_DEBUG(1, "Event Received (0x%02X)", HCIEventHeader.EventCode);
78
79 switch (HCIEventHeader.EventCode)
80 {
81 case EVENT_COMMAND_COMPLETE:
82 BT_HCI_DEBUG(1, "<< Command Complete");
83
84 /* Check which operation was completed in case we need to process the even parameters */
85 switch (((BT_HCIEvent_CommandComplete_t*)&EventParams)->Opcode)
86 {
87 case (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR):
88 /* A READ BDADDR command completed, copy over the local device's BDADDR from the response */
89 memcpy(Bluetooth_State.LocalBDADDR,
90 &((BT_HCIEvent_CommandComplete_t*)&EventParams)->ReturnParams[1],
91 sizeof(Bluetooth_State.LocalBDADDR));
92 break;
93 }
94
95 Bluetooth_State.CurrentHCIState = Bluetooth_State.NextHCIState;
96 break;
97 case EVENT_COMMAND_STATUS:
98 BT_HCI_DEBUG(1, "<< Command Status");
99 BT_HCI_DEBUG(2, "-- Status Code: 0x%02X", (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status));
100
101 /* If the execution of a command failed, reset the stack */
102 if (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status)
103 Bluetooth_State.CurrentHCIState = Bluetooth_Init;
104 break;
105 case EVENT_CONNECTION_REQUEST:
106 BT_HCI_DEBUG(1, "<< Connection Request");
107 BT_HCI_DEBUG(2, "-- Link Type: 0x%02X", (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType));
108
109 /* Need to store the remote device's BT address in a temporary buffer for later use */
110 memcpy(Bluetooth_TempDeviceAddress,
111 &((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress,
112 sizeof(Bluetooth_TempDeviceAddress));
113
114 bool IsACLConnection = (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01);
115
116 /* Only accept the connection if it is a ACL (data) connection, a device is not already connected
117 and the user application has indicated that the connection should be allowed */
118 Bluetooth_State.CurrentHCIState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) ||
119 !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ?
120 Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection;
121
122 BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State.CurrentHCIState == Bluetooth_Conn_RejectConnection) ?
123 PSTR("REJECTED") : PSTR("ACCEPTED"));
124
125 break;
126 case EVENT_PIN_CODE_REQUEST:
127 BT_HCI_DEBUG(1, "<< Pin Code Request");
128
129 /* Need to store the remote device's BT address in a temporary buffer for later use */
130 memcpy(Bluetooth_TempDeviceAddress,
131 &((BT_HCIEvent_PinCodeReq_t*)&EventParams)->RemoteAddress,
132 sizeof(Bluetooth_TempDeviceAddress));
133
134 Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendPINCode;
135 break;
136 case EVENT_LINK_KEY_REQUEST:
137 BT_HCI_DEBUG(1, "<< Link Key Request");
138
139 /* Need to store the remote device's BT address in a temporary buffer for later use */
140 memcpy(Bluetooth_TempDeviceAddress,
141 &((BT_HCIEvent_LinkKeyReq_t*)&EventParams)->RemoteAddress,
142 sizeof(Bluetooth_TempDeviceAddress));
143
144 Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendLinkKeyNAK;
145 break;
146 case EVENT_CONNECTION_COMPLETE:
147 BT_HCI_DEBUG(1, "<< Connection Complete");
148 BT_HCI_DEBUG(2, "-- Handle: 0x%04X", ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle);
149
150 /* Need to store the remote device's BT address in a temporary buffer for later use */
151 memcpy(Bluetooth_Connection.RemoteAddress,
152 &((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->RemoteAddress,
153 sizeof(Bluetooth_TempDeviceAddress));
154
155 /* Store the created connection handle and indicate that the connection has been established */
156 Bluetooth_Connection.ConnectionHandle = ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle;
157 Bluetooth_Connection.IsConnected = true;
158
159 Bluetooth_ConnectionComplete();
160 break;
161 case EVENT_DISCONNECTION_COMPLETE:
162 BT_HCI_DEBUG(1, "<< Disconnection Complete");
163
164 /* Device disconnected, indicate connection information no longer valid */
165 Bluetooth_Connection.IsConnected = false;
166
167 Bluetooth_DisconnectionComplete();
168 break;
169 }
170 }
171
172 Pipe_Freeze();
173
174 break;
175 case Bluetooth_Init:
176 BT_HCI_DEBUG(1, "# Init");
177
178 Bluetooth_State.IsInitialized = false;
179
180 /* Reset the connection information structure to destroy any previous connection state */
181 memset(&Bluetooth_Connection, 0x00, sizeof(Bluetooth_Connection));
182
183 Bluetooth_State.CurrentHCIState = Bluetooth_Init_Reset;
184 break;
185 case Bluetooth_Init_Reset:
186 BT_HCI_DEBUG(1, "# Reset");
187
188 HCICommandHeader = (BT_HCICommand_Header_t)
189 {
190 OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_RESET),
191 ParameterLength: 0,
192 };
193
194 /* Send the command to reset the Bluetooth dongle controller */
195 Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0);
196
197 Bluetooth_State.NextHCIState = Bluetooth_Init_ReadBufferSize;
198 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
199 break;
200 case Bluetooth_Init_ReadBufferSize:
201 BT_HCI_DEBUG(1, "# Read Buffer Size");
202
203 HCICommandHeader = (BT_HCICommand_Header_t)
204 {
205 OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE),
206 ParameterLength: 0,
207 };
208
209 /* Send the command to read the Bluetooth buffer size (mandatory before device sends any data) */
210 Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0);
211
212 Bluetooth_State.NextHCIState = Bluetooth_Init_GetBDADDR;
213 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
214 break;
215 case Bluetooth_Init_GetBDADDR:
216 BT_HCI_DEBUG(1, "# Get BDADDR");
217
218 HCICommandHeader = (BT_HCICommand_Header_t)
219 {
220 OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR),
221 ParameterLength: 0,
222 };
223
224 /* Send the command to retrieve the BDADDR of the inserted Bluetooth dongle */
225 Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0);
226
227 Bluetooth_State.NextHCIState = Bluetooth_Init_SetLocalName;
228 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
229 break;
230 case Bluetooth_Init_SetLocalName:
231 BT_HCI_DEBUG(1, "# Set Local Name");
232
233 HCICommandHeader = (BT_HCICommand_Header_t)
234 {
235 OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME),
236 ParameterLength: 248,
237 };
238
239 /* Send the command to set the Bluetooth dongle's name for other devices to see */
240 Bluetooth_SendHCICommand(&HCICommandHeader, Bluetooth_DeviceConfiguration.Name, strlen(Bluetooth_DeviceConfiguration.Name));
241
242 Bluetooth_State.NextHCIState = Bluetooth_Init_SetDeviceClass;
243 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
244 break;
245 case Bluetooth_Init_SetDeviceClass:
246 BT_HCI_DEBUG(1, "# Set Device Class");
247
248 HCICommandHeader = (BT_HCICommand_Header_t)
249 {
250 OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE),
251 ParameterLength: 3,
252 };
253
254 /* Send the command to set the class of the device for other devices to see */
255 Bluetooth_SendHCICommand(&HCICommandHeader, &Bluetooth_DeviceConfiguration.Class, 3);
256
257 Bluetooth_State.NextHCIState = Bluetooth_Init_WriteScanEnable;
258 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
259 break;
260 case Bluetooth_Init_WriteScanEnable:
261 BT_HCI_DEBUG(1, "# Write Scan Enable");
262
263 HCICommandHeader = (BT_HCICommand_Header_t)
264 {
265 OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE),
266 ParameterLength: 1,
267 };
268
269 uint8_t Interval = BT_SCANMODE_InquiryAndPageScans;
270
271 /* Send the command to set the remote device scanning mode */
272 Bluetooth_SendHCICommand(&HCICommandHeader, &Interval, 1);
273
274 Bluetooth_State.NextHCIState = Bluetooth_Init_FinalizeInit;
275 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
276 break;
277 case Bluetooth_Init_FinalizeInit:
278 Bluetooth_State.IsInitialized = true;
279
280 /* Fire the user application callback to indicate that the stack is now fully initialized */
281 Bluetooth_StackInitialized();
282
283 Bluetooth_State.NextHCIState = Bluetooth_ProcessEvents;
284 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
285 break;
286 case Bluetooth_Conn_AcceptConnection:
287 BT_HCI_DEBUG(1, "# Accept Connection");
288
289 HCICommandHeader = (BT_HCICommand_Header_t)
290 {
291 OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST),
292 ParameterLength: sizeof(BT_HCICommand_AcceptConnectionReq_t),
293 };
294
295 /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave
296 connection role */
297 BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams;
298 memcpy(AcceptConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress,
299 sizeof(AcceptConnectionParams.RemoteAddress));
300 AcceptConnectionParams.SlaveRole = true;
301
302 /* Send the command to accept the remote connection request */
303 Bluetooth_SendHCICommand(&HCICommandHeader, &AcceptConnectionParams, sizeof(BT_HCICommand_AcceptConnectionReq_t));
304
305 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
306 break;
307 case Bluetooth_Conn_RejectConnection:
308 BT_HCI_DEBUG(1, "# Reject Connection");
309
310 HCICommandHeader = (BT_HCICommand_Header_t)
311 {
312 OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST),
313 ParameterLength: sizeof(BT_HCICommand_RejectConnectionReq_t),
314 };
315
316 /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure
317 to accept the connection due to limited device resources or incorrect device address */
318 BT_HCICommand_RejectConnectionReq_t RejectConnectionParams;
319 memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(RejectConnectionParams.RemoteAddress));
320 RejectConnectionParams.Reason = Bluetooth_Connection.IsConnected ? ERROR_LIMITED_RESOURCES : ERROR_UNACCEPTABLE_BDADDR;
321
322 /* Send the command to reject the remote connection request */
323 Bluetooth_SendHCICommand(&HCICommandHeader, &RejectConnectionParams, sizeof(BT_HCICommand_RejectConnectionReq_t));
324
325 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
326 break;
327 case Bluetooth_Conn_SendPINCode:
328 BT_HCI_DEBUG(1, "# Send Pin Code");
329
330 HCICommandHeader = (BT_HCICommand_Header_t)
331 {
332 OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY),
333 ParameterLength: sizeof(BT_HCICommand_PinCodeResp_t),
334 };
335
336 /* Copy over the temporary BT device address saved from the PIN Code Request event, copy over the
337 local PIN authentication code to the response */
338 BT_HCICommand_PinCodeResp_t PINCodeRequestParams;
339 memcpy(PINCodeRequestParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(PINCodeRequestParams.RemoteAddress));
340 PINCodeRequestParams.PINCodeLength = strlen(Bluetooth_DeviceConfiguration.PINCode);
341 memcpy(PINCodeRequestParams.PINCode, Bluetooth_DeviceConfiguration.PINCode, sizeof(PINCodeRequestParams.PINCode));
342
343 /* Send the command to transmit the device's local PIN number for authentication */
344 Bluetooth_SendHCICommand(&HCICommandHeader, &PINCodeRequestParams, sizeof(BT_HCICommand_PinCodeResp_t));
345
346 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
347 break;
348 case Bluetooth_Conn_SendLinkKeyNAK:
349 BT_HCI_DEBUG(1, "# Send Link Key NAK");
350
351 HCICommandHeader = (BT_HCICommand_Header_t)
352 {
353 OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY),
354 ParameterLength: sizeof(BT_HCICommand_LinkKeyNAKResp_t),
355 };
356
357 /* Copy over the temporary BT device address saved from the Link Key Request event */
358 BT_HCICommand_LinkKeyNAKResp_t LinkKeyNAKParams;
359 memcpy(LinkKeyNAKParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(LinkKeyNAKParams.RemoteAddress));
360
361 /* Send the command to transmit the link key NAK to the receiver */
362 Bluetooth_SendHCICommand(&HCICommandHeader, &LinkKeyNAKParams, sizeof(BT_HCICommand_LinkKeyNAKResp_t));
363
364 Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents;
365 break;
366 }
367 }
368
369 /** Sends a Bluetooth HCI control command to the attached Bluetooth device.
370 *
371 * \param[in] HCICommandHeader HCI command header to send to the attached device
372 * \param[in] Parameters Pointer to the source of the control parameters (if any)
373 * \param[in] ParameterLength Length of the parameters to send in bytes
374 *
375 * \return A value from the USB_Host_SendControlErrorCodes_t enum.
376 */
377 static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t* const HCICommandHeader,
378 const void* Parameters,
379 const uint16_t ParameterLength)
380 {
381 /* Need to reserve the amount of bytes given in the header for the complete payload */
382 uint8_t CommandBuffer[sizeof(BT_HCICommand_Header_t) + HCICommandHeader->ParameterLength];
383
384 USB_ControlRequest = (USB_Request_Header_t)
385 {
386 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_DEVICE),
387 .bRequest = 0,
388 .wValue = 0,
389 .wIndex = 0,
390 .wLength = sizeof(CommandBuffer)
391 };
392
393 /* Copy over the HCI command header to the allocated buffer */
394 memcpy(CommandBuffer, HCICommandHeader, sizeof(BT_HCICommand_Header_t));
395
396 /* Zero out the parameter section of the response so that all padding bytes are known to be zero */
397 memset(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], 0x00, HCICommandHeader->ParameterLength);
398
399 /* Copy over the command parameters (if any) to the command buffer - note, the number of actual source parameter bytes
400 may differ to those in the header; any difference in length is filled with 0x00 padding bytes */
401 memcpy(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], Parameters, ParameterLength);
402
403 Pipe_SelectPipe(PIPE_CONTROLPIPE);
404 return USB_Host_SendControlRequest(CommandBuffer);
405 }