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
31 #define INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C
32 #include "BluetoothHCICommands.h"
34 /** Temporary Bluetooth Device Address, for HCI responses which much include the detination address */
35 static uint8_t Bluetooth_TempDeviceAddress
[6];
37 /** Bluetooth HCI processing task. This task should be called repeatedly the main Bluetooth
38 * stack task to manage the HCI processing state.
40 void Bluetooth_HCITask(void)
42 BT_HCICommand_Header_t HCICommandHeader
;
44 switch (Bluetooth_State
.CurrentHCIState
)
46 case Bluetooth_ProcessEvents
:
47 Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE
);
50 if (Pipe_IsReadWriteAllowed())
52 BT_HCIEvent_Header_t HCIEventHeader
;
54 /* Read in the event header to fetch the event code and payload length */
55 Pipe_Read_Stream_LE(&HCIEventHeader
, sizeof(HCIEventHeader
));
57 /* Create a temporary buffer for the event parameters */
58 uint8_t EventParams
[HCIEventHeader
.ParameterLength
];
60 /* Read in the event parameters into the temporary buffer */
61 Pipe_Read_Stream_LE(&EventParams
, HCIEventHeader
.ParameterLength
);
64 BT_HCI_DEBUG(1, "Event Received (0x%02X)", HCIEventHeader
.EventCode
);
66 switch (HCIEventHeader
.EventCode
)
68 case EVENT_COMMAND_COMPLETE
:
69 BT_HCI_DEBUG(1, "<< Command Complete");
71 /* Check which operation was completed in case we need to process the even parameters */
72 switch (((BT_HCIEvent_CommandComplete_t
*)&EventParams
)->Opcode
)
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
));
82 Bluetooth_State
.CurrentHCIState
= Bluetooth_State
.NextHCIState
;
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
));
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
;
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
));
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
));
101 bool IsACLConnection
= (((BT_HCIEvent_ConnectionRequest_t
*)&EventParams
)->LinkType
== 0x01);
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
;
109 BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State
.CurrentHCIState
== Bluetooth_Conn_RejectConnection
) ?
110 PSTR("REJECTED") : PSTR("ACCEPTED"));
113 case EVENT_PIN_CODE_REQUEST
:
114 BT_HCI_DEBUG(1, "<< Pin Code Request");
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
));
121 Bluetooth_State
.CurrentHCIState
= Bluetooth_Conn_SendPINCode
;
123 case EVENT_LINK_KEY_REQUEST
:
124 BT_HCI_DEBUG(1, "<< Link Key Request");
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
));
131 Bluetooth_State
.CurrentHCIState
= Bluetooth_Conn_SendLinkKeyNAK
;
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
);
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
));
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;
146 Bluetooth_ConnectionComplete();
148 case EVENT_DISCONNECTION_COMPLETE
:
149 BT_HCI_DEBUG(1, "<< Disconnection Complete");
151 /* Device disconnected, indicate connection information no longer valid */
152 Bluetooth_Connection
.IsConnected
= false;
154 Bluetooth_DisconnectionComplete();
156 Bluetooth_State
.CurrentHCIState
= Bluetooth_Init
;
165 BT_HCI_DEBUG(1, "# Init");
167 Bluetooth_State
.IsInitialized
= false;
169 /* Reset the connection information structure to destroy any previous connection state */
170 memset(&Bluetooth_Connection
, 0x00, sizeof(Bluetooth_Connection
));
172 Bluetooth_State
.CurrentHCIState
= Bluetooth_Init_Reset
;
174 case Bluetooth_Init_Reset
:
175 BT_HCI_DEBUG(1, "# Reset");
177 HCICommandHeader
= (BT_HCICommand_Header_t
)
179 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_RESET
),
183 /* Send the command to reset the bluetooth dongle controller */
184 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
186 Bluetooth_State
.NextHCIState
= Bluetooth_Init_ReadBufferSize
;
187 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
189 case Bluetooth_Init_ReadBufferSize
:
190 BT_HCI_DEBUG(1, "# Read Buffer Size");
192 HCICommandHeader
= (BT_HCICommand_Header_t
)
194 OpCode
: (OGF_CTRLR_INFORMATIONAL
| OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE
),
198 /* Send the command to read the bluetooth buffer size (mandatory before device sends any data) */
199 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
201 Bluetooth_State
.NextHCIState
= Bluetooth_Init_GetBDADDR
;
202 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
204 case Bluetooth_Init_GetBDADDR
:
205 BT_HCI_DEBUG(1, "# Get BDADDR");
207 HCICommandHeader
= (BT_HCICommand_Header_t
)
209 OpCode
: (OGF_CTRLR_INFORMATIONAL
| OCF_CTRLR_INFORMATIONAL_READBDADDR
),
213 /* Send the command to retrieve the BDADDR of the inserted bluetooth dongle */
214 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
216 Bluetooth_State
.NextHCIState
= Bluetooth_Init_SetLocalName
;
217 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
219 case Bluetooth_Init_SetLocalName
:
220 BT_HCI_DEBUG(1, "# Set Local Name");
222 HCICommandHeader
= (BT_HCICommand_Header_t
)
224 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME
),
225 ParameterLength
: 248,
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
));
231 Bluetooth_State
.NextHCIState
= Bluetooth_Init_SetDeviceClass
;
232 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
234 case Bluetooth_Init_SetDeviceClass
:
235 BT_HCI_DEBUG(1, "# Set Device Class");
237 HCICommandHeader
= (BT_HCICommand_Header_t
)
239 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE
),
243 /* Send the command to set the class of the device for other devices to see */
244 Bluetooth_SendHCICommand(&HCICommandHeader
, &Bluetooth_DeviceConfiguration
.Class
, 3);
246 Bluetooth_State
.NextHCIState
= Bluetooth_Init_WriteScanEnable
;
247 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
249 case Bluetooth_Init_WriteScanEnable
:
250 BT_HCI_DEBUG(1, "# Write Scan Enable");
252 HCICommandHeader
= (BT_HCICommand_Header_t
)
254 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE
),
258 uint8_t Interval
= BT_SCANMODE_InquiryAndPageScans
;
260 /* Send the command to set the remote device scanning mode */
261 Bluetooth_SendHCICommand(&HCICommandHeader
, &Interval
, 1);
263 Bluetooth_State
.NextHCIState
= Bluetooth_Init_FinalizeInit
;
264 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
266 case Bluetooth_Init_FinalizeInit
:
267 Bluetooth_State
.IsInitialized
= true;
269 /* Fire the user application callback to indicate that the stack is now fully initialized */
270 Bluetooth_StackInitialized();
272 Bluetooth_State
.NextHCIState
= Bluetooth_ProcessEvents
;
273 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
275 case Bluetooth_Conn_AcceptConnection
:
276 BT_HCI_DEBUG(1, "# Accept Connection");
278 HCICommandHeader
= (BT_HCICommand_Header_t
)
280 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST
),
281 ParameterLength
: sizeof(BT_HCICommand_AcceptConnectionReq_t
),
284 /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave
286 BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams
;
287 memcpy(AcceptConnectionParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
,
288 sizeof(AcceptConnectionParams
.RemoteAddress
));
289 AcceptConnectionParams
.SlaveRole
= true;
291 /* Send the command to accept the remote connection request */
292 Bluetooth_SendHCICommand(&HCICommandHeader
, &AcceptConnectionParams
, sizeof(BT_HCICommand_AcceptConnectionReq_t
));
294 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
296 case Bluetooth_Conn_RejectConnection
:
297 BT_HCI_DEBUG(1, "# Reject Connection");
299 HCICommandHeader
= (BT_HCICommand_Header_t
)
301 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST
),
302 ParameterLength
: sizeof(BT_HCICommand_RejectConnectionReq_t
),
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
;
311 /* Send the command to reject the remote connection request */
312 Bluetooth_SendHCICommand(&HCICommandHeader
, &RejectConnectionParams
, sizeof(BT_HCICommand_RejectConnectionReq_t
));
314 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
316 case Bluetooth_Conn_SendPINCode
:
317 BT_HCI_DEBUG(1, "# Send Pin Code");
319 HCICommandHeader
= (BT_HCICommand_Header_t
)
321 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY
),
322 ParameterLength
: sizeof(BT_HCICommand_PinCodeResp_t
),
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
));
332 /* Send the command to transmit the device's local PIN number for authentication */
333 Bluetooth_SendHCICommand(&HCICommandHeader
, &PINCodeRequestParams
, sizeof(BT_HCICommand_PinCodeResp_t
));
335 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
337 case Bluetooth_Conn_SendLinkKeyNAK
:
338 BT_HCI_DEBUG(1, "# Send Link Key NAK");
340 HCICommandHeader
= (BT_HCICommand_Header_t
)
342 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY
),
343 ParameterLength
: sizeof(BT_HCICommand_LinkKeyNAKResp_t
),
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
));
350 /* Send the command to transmit the link key NAK to the receiver */
351 Bluetooth_SendHCICommand(&HCICommandHeader
, &LinkKeyNAKParams
, sizeof(BT_HCICommand_LinkKeyNAKResp_t
));
353 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
358 /** Sends a Bluetooth HCI control command to the attached Bluetooth device.
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
364 * \return A value from the USB_Host_SendControlErrorCodes_t enum.
366 static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t
* const HCICommandHeader
, const void* Parameters
, const uint16_t ParameterLength
)
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
];
371 USB_ControlRequest
= (USB_Request_Header_t
)
373 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_DEVICE
),
377 .wLength
= sizeof(CommandBuffer
)
380 /* Copy over the HCI command header to the allocated buffer */
381 memcpy(CommandBuffer
, HCICommandHeader
, sizeof(BT_HCICommand_Header_t
));
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
);
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
);
390 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
391 return USB_Host_SendControlRequest(CommandBuffer
);