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
32 TODO: Add local to remote device connections
35 #define INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C
36 #include "BluetoothHCICommands.h"
38 /** Temporary Bluetooth Device Address, for HCI responses which much include the detination address */
39 static uint8_t Bluetooth_TempDeviceAddress
[6];
41 /** Bluetooth HCI processing task. This task should be called repeatedly the main Bluetooth
42 * stack task to manage the HCI processing state.
44 void Bluetooth_HCITask(void)
46 BT_HCICommand_Header_t HCICommandHeader
;
48 switch (Bluetooth_State
.CurrentHCIState
)
50 case Bluetooth_ProcessEvents
:
51 Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE
);
54 if (Pipe_IsReadWriteAllowed())
56 BT_HCIEvent_Header_t HCIEventHeader
;
58 /* Read in the event header to fetch the event code and payload length */
59 Pipe_Read_Stream_LE(&HCIEventHeader
, sizeof(HCIEventHeader
));
61 /* Create a temporary buffer for the event parameters */
62 uint8_t EventParams
[HCIEventHeader
.ParameterLength
];
64 /* Read in the event parameters into the temporary buffer */
65 Pipe_Read_Stream_LE(&EventParams
, HCIEventHeader
.ParameterLength
);
68 BT_HCI_DEBUG(1, "Event Received (0x%02X)", HCIEventHeader
.EventCode
);
70 switch (HCIEventHeader
.EventCode
)
72 case EVENT_COMMAND_COMPLETE
:
73 BT_HCI_DEBUG(1, "<< Command Complete");
75 /* Check which operation was completed in case we need to process the even parameters */
76 switch (((BT_HCIEvent_CommandComplete_t
*)&EventParams
)->Opcode
)
78 case (OGF_CTRLR_INFORMATIONAL
| OCF_CTRLR_INFORMATIONAL_READBDADDR
):
79 /* A READ BDADDR command completed, copy over the local device's BDADDR from the response */
80 memcpy(Bluetooth_State
.LocalBDADDR
,
81 &((BT_HCIEvent_CommandComplete_t
*)&EventParams
)->ReturnParams
[1],
82 sizeof(Bluetooth_State
.LocalBDADDR
));
86 Bluetooth_State
.CurrentHCIState
= Bluetooth_State
.NextHCIState
;
88 case EVENT_COMMAND_STATUS
:
89 BT_HCI_DEBUG(1, "<< Command Status");
90 BT_HCI_DEBUG(2, "-- Status Code: 0x%02X", (((BT_HCIEvent_CommandStatus_t
*)&EventParams
)->Status
));
92 /* If the execution of a command failed, reset the stack */
93 if (((BT_HCIEvent_CommandStatus_t
*)&EventParams
)->Status
)
94 Bluetooth_State
.CurrentHCIState
= Bluetooth_Init
;
96 case EVENT_CONNECTION_REQUEST
:
97 BT_HCI_DEBUG(1, "<< Connection Request");
98 BT_HCI_DEBUG(2, "-- Link Type: 0x%02X", (((BT_HCIEvent_ConnectionRequest_t
*)&EventParams
)->LinkType
));
100 /* Need to store the remote device's BT address in a temporary buffer for later use */
101 memcpy(Bluetooth_TempDeviceAddress
,
102 &((BT_HCIEvent_ConnectionRequest_t
*)&EventParams
)->RemoteAddress
,
103 sizeof(Bluetooth_TempDeviceAddress
));
105 bool IsACLConnection
= (((BT_HCIEvent_ConnectionRequest_t
*)&EventParams
)->LinkType
== 0x01);
107 /* Only accept the connection if it is a ACL (data) connection, a device is not already connected
108 and the user application has indicated that the connection should be allowed */
109 Bluetooth_State
.CurrentHCIState
= (Bluetooth_Connection
.IsConnected
|| !(IsACLConnection
) ||
110 !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress
))) ?
111 Bluetooth_Conn_RejectConnection
: Bluetooth_Conn_AcceptConnection
;
113 BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State
.CurrentHCIState
== Bluetooth_Conn_RejectConnection
) ?
114 PSTR("REJECTED") : PSTR("ACCEPTED"));
117 case EVENT_PIN_CODE_REQUEST
:
118 BT_HCI_DEBUG(1, "<< Pin Code Request");
120 /* Need to store the remote device's BT address in a temporary buffer for later use */
121 memcpy(Bluetooth_TempDeviceAddress
,
122 &((BT_HCIEvent_PinCodeReq_t
*)&EventParams
)->RemoteAddress
,
123 sizeof(Bluetooth_TempDeviceAddress
));
125 Bluetooth_State
.CurrentHCIState
= Bluetooth_Conn_SendPINCode
;
127 case EVENT_LINK_KEY_REQUEST
:
128 BT_HCI_DEBUG(1, "<< Link Key Request");
130 /* Need to store the remote device's BT address in a temporary buffer for later use */
131 memcpy(Bluetooth_TempDeviceAddress
,
132 &((BT_HCIEvent_LinkKeyReq_t
*)&EventParams
)->RemoteAddress
,
133 sizeof(Bluetooth_TempDeviceAddress
));
135 Bluetooth_State
.CurrentHCIState
= Bluetooth_Conn_SendLinkKeyNAK
;
137 case EVENT_CONNECTION_COMPLETE
:
138 BT_HCI_DEBUG(1, "<< Connection Complete");
139 BT_HCI_DEBUG(2, "-- Handle: 0x%04X", ((BT_HCIEvent_ConnectionComplete_t
*)&EventParams
)->ConnectionHandle
);
141 /* Need to store the remote device's BT address in a temporary buffer for later use */
142 memcpy(Bluetooth_Connection
.RemoteAddress
,
143 &((BT_HCIEvent_ConnectionComplete_t
*)&EventParams
)->RemoteAddress
,
144 sizeof(Bluetooth_TempDeviceAddress
));
146 /* Store the created connection handle and indicate that the connection has been established */
147 Bluetooth_Connection
.ConnectionHandle
= ((BT_HCIEvent_ConnectionComplete_t
*)&EventParams
)->ConnectionHandle
;
148 Bluetooth_Connection
.IsConnected
= true;
150 Bluetooth_ConnectionComplete();
152 case EVENT_DISCONNECTION_COMPLETE
:
153 BT_HCI_DEBUG(1, "<< Disconnection Complete");
155 /* Device disconnected, indicate connection information no longer valid */
156 Bluetooth_Connection
.IsConnected
= false;
158 Bluetooth_DisconnectionComplete();
160 Bluetooth_State
.CurrentHCIState
= Bluetooth_Init
;
169 BT_HCI_DEBUG(1, "# Init");
171 Bluetooth_State
.IsInitialized
= false;
173 /* Reset the connection information structure to destroy any previous connection state */
174 memset(&Bluetooth_Connection
, 0x00, sizeof(Bluetooth_Connection
));
176 Bluetooth_State
.CurrentHCIState
= Bluetooth_Init_Reset
;
178 case Bluetooth_Init_Reset
:
179 BT_HCI_DEBUG(1, "# Reset");
181 HCICommandHeader
= (BT_HCICommand_Header_t
)
183 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_RESET
),
187 /* Send the command to reset the bluetooth dongle controller */
188 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
190 Bluetooth_State
.NextHCIState
= Bluetooth_Init_ReadBufferSize
;
191 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
193 case Bluetooth_Init_ReadBufferSize
:
194 BT_HCI_DEBUG(1, "# Read Buffer Size");
196 HCICommandHeader
= (BT_HCICommand_Header_t
)
198 OpCode
: (OGF_CTRLR_INFORMATIONAL
| OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE
),
202 /* Send the command to read the bluetooth buffer size (mandatory before device sends any data) */
203 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
205 Bluetooth_State
.NextHCIState
= Bluetooth_Init_GetBDADDR
;
206 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
208 case Bluetooth_Init_GetBDADDR
:
209 BT_HCI_DEBUG(1, "# Get BDADDR");
211 HCICommandHeader
= (BT_HCICommand_Header_t
)
213 OpCode
: (OGF_CTRLR_INFORMATIONAL
| OCF_CTRLR_INFORMATIONAL_READBDADDR
),
217 /* Send the command to retrieve the BDADDR of the inserted bluetooth dongle */
218 Bluetooth_SendHCICommand(&HCICommandHeader
, NULL
, 0);
220 Bluetooth_State
.NextHCIState
= Bluetooth_Init_SetLocalName
;
221 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
223 case Bluetooth_Init_SetLocalName
:
224 BT_HCI_DEBUG(1, "# Set Local Name");
226 HCICommandHeader
= (BT_HCICommand_Header_t
)
228 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME
),
229 ParameterLength
: 248,
232 /* Send the command to set the bluetooth dongle's name for other devices to see */
233 Bluetooth_SendHCICommand(&HCICommandHeader
, Bluetooth_DeviceConfiguration
.Name
, strlen(Bluetooth_DeviceConfiguration
.Name
));
235 Bluetooth_State
.NextHCIState
= Bluetooth_Init_SetDeviceClass
;
236 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
238 case Bluetooth_Init_SetDeviceClass
:
239 BT_HCI_DEBUG(1, "# Set Device Class");
241 HCICommandHeader
= (BT_HCICommand_Header_t
)
243 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE
),
247 /* Send the command to set the class of the device for other devices to see */
248 Bluetooth_SendHCICommand(&HCICommandHeader
, &Bluetooth_DeviceConfiguration
.Class
, 3);
250 Bluetooth_State
.NextHCIState
= Bluetooth_Init_WriteScanEnable
;
251 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
253 case Bluetooth_Init_WriteScanEnable
:
254 BT_HCI_DEBUG(1, "# Write Scan Enable");
256 HCICommandHeader
= (BT_HCICommand_Header_t
)
258 OpCode
: (OGF_CTRLR_BASEBAND
| OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE
),
262 uint8_t Interval
= BT_SCANMODE_InquiryAndPageScans
;
264 /* Send the command to set the remote device scanning mode */
265 Bluetooth_SendHCICommand(&HCICommandHeader
, &Interval
, 1);
267 Bluetooth_State
.NextHCIState
= Bluetooth_Init_FinalizeInit
;
268 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
270 case Bluetooth_Init_FinalizeInit
:
271 Bluetooth_State
.IsInitialized
= true;
273 /* Fire the user application callback to indicate that the stack is now fully initialized */
274 Bluetooth_StackInitialized();
276 Bluetooth_State
.NextHCIState
= Bluetooth_ProcessEvents
;
277 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
279 case Bluetooth_Conn_AcceptConnection
:
280 BT_HCI_DEBUG(1, "# Accept Connection");
282 HCICommandHeader
= (BT_HCICommand_Header_t
)
284 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST
),
285 ParameterLength
: sizeof(BT_HCICommand_AcceptConnectionReq_t
),
288 /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave
290 BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams
;
291 memcpy(AcceptConnectionParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
,
292 sizeof(AcceptConnectionParams
.RemoteAddress
));
293 AcceptConnectionParams
.SlaveRole
= true;
295 /* Send the command to accept the remote connection request */
296 Bluetooth_SendHCICommand(&HCICommandHeader
, &AcceptConnectionParams
, sizeof(BT_HCICommand_AcceptConnectionReq_t
));
298 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
300 case Bluetooth_Conn_RejectConnection
:
301 BT_HCI_DEBUG(1, "# Reject Connection");
303 HCICommandHeader
= (BT_HCICommand_Header_t
)
305 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST
),
306 ParameterLength
: sizeof(BT_HCICommand_RejectConnectionReq_t
),
309 /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure
310 to accept the connection due to limited device resources or incorrect device address */
311 BT_HCICommand_RejectConnectionReq_t RejectConnectionParams
;
312 memcpy(RejectConnectionParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
, sizeof(RejectConnectionParams
.RemoteAddress
));
313 RejectConnectionParams
.Reason
= Bluetooth_Connection
.IsConnected ? ERROR_LIMITED_RESOURCES
: ERROR_UNACCEPTABLE_BDADDR
;
315 /* Send the command to reject the remote connection request */
316 Bluetooth_SendHCICommand(&HCICommandHeader
, &RejectConnectionParams
, sizeof(BT_HCICommand_RejectConnectionReq_t
));
318 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
320 case Bluetooth_Conn_SendPINCode
:
321 BT_HCI_DEBUG(1, "# Send Pin Code");
323 HCICommandHeader
= (BT_HCICommand_Header_t
)
325 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY
),
326 ParameterLength
: sizeof(BT_HCICommand_PinCodeResp_t
),
329 /* Copy over the temporary BT device address saved from the PIN Code Request event, copy over the
330 local PIN authentication code to the response */
331 BT_HCICommand_PinCodeResp_t PINCodeRequestParams
;
332 memcpy(PINCodeRequestParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
, sizeof(PINCodeRequestParams
.RemoteAddress
));
333 PINCodeRequestParams
.PINCodeLength
= strlen(Bluetooth_DeviceConfiguration
.PINCode
);
334 memcpy(PINCodeRequestParams
.PINCode
, Bluetooth_DeviceConfiguration
.PINCode
, sizeof(PINCodeRequestParams
.PINCode
));
336 /* Send the command to transmit the device's local PIN number for authentication */
337 Bluetooth_SendHCICommand(&HCICommandHeader
, &PINCodeRequestParams
, sizeof(BT_HCICommand_PinCodeResp_t
));
339 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
341 case Bluetooth_Conn_SendLinkKeyNAK
:
342 BT_HCI_DEBUG(1, "# Send Link Key NAK");
344 HCICommandHeader
= (BT_HCICommand_Header_t
)
346 OpCode
: (OGF_LINK_CONTROL
| OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY
),
347 ParameterLength
: sizeof(BT_HCICommand_LinkKeyNAKResp_t
),
350 /* Copy over the temporary BT device address saved from the Link Key Request event */
351 BT_HCICommand_LinkKeyNAKResp_t LinkKeyNAKParams
;
352 memcpy(LinkKeyNAKParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
, sizeof(LinkKeyNAKParams
.RemoteAddress
));
354 /* Send the command to transmit the link key NAK to the receiver */
355 Bluetooth_SendHCICommand(&HCICommandHeader
, &LinkKeyNAKParams
, sizeof(BT_HCICommand_LinkKeyNAKResp_t
));
357 Bluetooth_State
.CurrentHCIState
= Bluetooth_ProcessEvents
;
362 /** Sends a Bluetooth HCI control command to the attached Bluetooth device.
364 * \param[in] HCICommandHeader HCI command header to send to the attached device
365 * \param[in] Parameters Pointer to the source of the control parameters (if any)
366 * \param[in] ParameterLength Length of the parameters to send in bytes
368 * \return A value from the USB_Host_SendControlErrorCodes_t enum.
370 static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t
* const HCICommandHeader
, const void* Parameters
, const uint16_t ParameterLength
)
372 /* Need to reserve the amount of bytes given in the header for the complete payload */
373 uint8_t CommandBuffer
[sizeof(BT_HCICommand_Header_t
) + HCICommandHeader
->ParameterLength
];
375 USB_ControlRequest
= (USB_Request_Header_t
)
377 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_DEVICE
),
381 .wLength
= sizeof(CommandBuffer
)
384 /* Copy over the HCI command header to the allocated buffer */
385 memcpy(CommandBuffer
, HCICommandHeader
, sizeof(BT_HCICommand_Header_t
));
387 /* Zero out the parameter section of the response so that all padding bytes are known to be zero */
388 memset(&CommandBuffer
[sizeof(BT_HCICommand_Header_t
)], 0x00, HCICommandHeader
->ParameterLength
);
390 /* Copy over the command parameters (if any) to the command buffer - note, the number of actual source parameter bytes
391 may differ to those in the header; any difference in length is filled with 0x00 padding bytes */
392 memcpy(&CommandBuffer
[sizeof(BT_HCICommand_Header_t
)], Parameters
, ParameterLength
);
394 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
395 return USB_Host_SendControlRequest(CommandBuffer
);