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 static BT_HCICommand_Header_t HCICommandHeader
;
36 uint8_t Bluetooth_HCIProcessingState
;
37 static uint8_t Bluetooth_HCINextState
;
38 static uint8_t Bluetooth_TempDeviceAddress
[6];
40 void Bluetooth_HCITask(void)
42 switch (Bluetooth_HCIProcessingState
)
44 case Bluetooth_ProcessEvents
:
45 Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE
);
48 if (Pipe_IsReadWriteAllowed())
50 BT_HCIEvent_Header_t HCIEventHeader
;
52 /* Read in the event header to fetch the event code and payload length */
53 Pipe_Read_Stream_LE(&HCIEventHeader
, sizeof(HCIEventHeader
));
55 /* Create a temporary buffer for the event parameters */
56 uint8_t EventParams
[HCIEventHeader
.ParameterLength
];
58 /* Read in the event parameters into the temporary buffer */
59 Pipe_Read_Stream_LE(&EventParams
, HCIEventHeader
.ParameterLength
);
62 switch (HCIEventHeader
.EventCode
)
64 case EVENT_COMMAND_COMPLETE
:
65 Bluetooth_HCIProcessingState
= Bluetooth_HCINextState
;
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
;
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
));
78 bool IsACLConnection
= (((BT_HCIEvent_ConnectionRequest_t
*)&EventParams
)->LinkType
== 0x01);
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
;
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
));
92 Bluetooth_HCIProcessingState
= Bluetooth_Conn_SendPINCode
;
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
));
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;
104 Bluetooth_ConnectionComplete();
106 case EVENT_DISCONNECTION_COMPLETE
:
107 /* Device disconnected, indicate connection information no longer valid */
108 Bluetooth_Connection
.IsConnected
= false;
110 Bluetooth_DisconnectionComplete();
112 Bluetooth_HCIProcessingState
= Bluetooth_Init
;
121 /* Reset the connection information structure to destroy any previous connection state */
122 memset(&Bluetooth_Connection
, 0x00, sizeof(Bluetooth_Connection
));
124 Bluetooth_HCIProcessingState
= Bluetooth_Init_Reset
;
126 case Bluetooth_Init_Reset
:
127 HCICommandHeader
= (BT_HCICommand_Header_t
)
129 OpCode
: {OGF
: OGF_CTRLR_BASEBAND
, OCF
: OCF_CTRLR_BASEBAND_RESET
},
133 /* Send the command to reset the bluetooth dongle controller */
134 Bluetooth_SendHCICommand(NULL
, 0);
136 Bluetooth_HCINextState
= Bluetooth_Init_SetLocalName
;
137 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
139 case Bluetooth_Init_SetLocalName
:
140 HCICommandHeader
= (BT_HCICommand_Header_t
)
142 OpCode
: {OGF
: OGF_CTRLR_BASEBAND
, OCF
: OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME
},
143 ParameterLength
: 248,
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
));
149 Bluetooth_HCINextState
= Bluetooth_Init_SetDeviceClass
;
150 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
152 case Bluetooth_Init_SetDeviceClass
:
153 HCICommandHeader
= (BT_HCICommand_Header_t
)
155 OpCode
: {OGF
: OGF_CTRLR_BASEBAND
, OCF
: OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE
},
159 /* Send the command to set the class of the device for other devices to see */
160 Bluetooth_SendHCICommand(&Bluetooth_DeviceConfiguration
.Class
, 3);
162 Bluetooth_HCINextState
= Bluetooth_Init_WriteScanEnable
;
163 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
165 case Bluetooth_Init_WriteScanEnable
:
166 HCICommandHeader
= (BT_HCICommand_Header_t
)
168 OpCode
: {OGF
: OGF_CTRLR_BASEBAND
, OCF
: OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE
},
172 uint8_t Interval
= BT_SCANMODE_InquiryAndPageScans
;
174 /* Send the command to set the remote device scanning mode */
175 Bluetooth_SendHCICommand(&Interval
, 1);
177 Bluetooth_HCINextState
= Bluetooth_ProcessEvents
;
178 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
180 case Bluetooth_Conn_AcceptConnection
:
181 HCICommandHeader
= (BT_HCICommand_Header_t
)
183 OpCode
: {OGF
: OGF_LINK_CONTROL
, OCF
: OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST
},
184 ParameterLength
: sizeof(BT_HCICommand_AcceptConnectionReq_t
),
187 /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave
189 BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams
;
190 memcpy(AcceptConnectionParams
.RemoteAddress
, Bluetooth_TempDeviceAddress
,
191 sizeof(AcceptConnectionParams
.RemoteAddress
));
192 AcceptConnectionParams
.SlaveRole
= true;
194 /* Send the command to accept the remote connection request */
195 Bluetooth_SendHCICommand(&AcceptConnectionParams
, sizeof(BT_HCICommand_AcceptConnectionReq_t
));
197 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
199 case Bluetooth_Conn_RejectConnection
:
200 HCICommandHeader
= (BT_HCICommand_Header_t
)
202 OpCode
: {OGF
: OGF_LINK_CONTROL
, OCF
: OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST
},
203 ParameterLength
: sizeof(BT_HCICommand_RejectConnectionReq_t
),
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
;
212 /* Send the command to reject the remote connection request */
213 Bluetooth_SendHCICommand(&RejectConnectionParams
, sizeof(BT_HCICommand_RejectConnectionReq_t
));
215 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
217 case Bluetooth_Conn_SendPINCode
:
218 HCICommandHeader
= (BT_HCICommand_Header_t
)
220 OpCode
: {OGF
: OGF_LINK_CONTROL
, OCF
: OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY
},
221 ParameterLength
: sizeof(BT_HCICommand_PinCodeResp_t
),
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
));
231 /* Send the command to transmit the device's local PIN number for authentication */
232 Bluetooth_SendHCICommand(&PINCodeRequestParams
, sizeof(BT_HCICommand_PinCodeResp_t
));
234 Bluetooth_HCIProcessingState
= Bluetooth_ProcessEvents
;
239 static uint8_t Bluetooth_SendHCICommand(void* Parameters
, uint16_t ParameterLength
)
241 /* Need to reserve the amount of bytes given in the header for the complete payload */
242 uint8_t CommandBuffer
[sizeof(HCICommandHeader
) + HCICommandHeader
.ParameterLength
];
244 USB_ControlRequest
= (USB_Request_Header_t
)
246 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_DEVICE
),
250 .wLength
= sizeof(CommandBuffer
)
253 /* Copy over the HCI command header to the allocated buffer */
254 memcpy(CommandBuffer
, &HCICommandHeader
, sizeof(HCICommandHeader
));
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
);
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
);
263 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
264 return USB_Host_SendControlRequest(CommandBuffer
);