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