+ switch (HCIEventHeader.EventCode)\r
+ {\r
+ case EVENT_COMMAND_COMPLETE:\r
+ BT_HCI_DEBUG(1, "<< Command Complete");\r
+ \r
+ /* Check which operation was completed in case we need to process the even parameters */\r
+ switch (((BT_HCIEvent_CommandComplete_t*)&EventParams)->Opcode)\r
+ {\r
+ case (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR):\r
+ /* A READ BDADDR command completed, copy over the local device's BDADDR from the response */\r
+ memcpy(Bluetooth_State.LocalBDADDR,\r
+ &((BT_HCIEvent_CommandComplete_t*)&EventParams)->ReturnParams[1],\r
+ sizeof(Bluetooth_State.LocalBDADDR));\r
+ break;\r
+ }\r
+ \r
+ Bluetooth_State.CurrentHCIState = Bluetooth_State.NextHCIState;\r
+ break;\r
+ case EVENT_COMMAND_STATUS:\r
+ BT_HCI_DEBUG(1, "<< Command Status");\r
+ BT_HCI_DEBUG(2, "-- Status Code: 0x%02X", (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status));\r
+\r
+ /* If the execution of a command failed, reset the stack */\r
+ if (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status)\r
+ Bluetooth_State.CurrentHCIState = Bluetooth_Init;\r
+ break;\r
+ case EVENT_CONNECTION_REQUEST:\r
+ BT_HCI_DEBUG(1, "<< Connection Request");\r
+ BT_HCI_DEBUG(2, "-- Link Type: 0x%02X", (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType));\r
+\r
+ /* Need to store the remote device's BT address in a temporary buffer for later use */\r
+ memcpy(Bluetooth_TempDeviceAddress,\r
+ &((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress,\r
+ sizeof(Bluetooth_TempDeviceAddress));\r
+ \r
+ bool IsACLConnection = (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01);\r
+\r
+ /* Only accept the connection if it is a ACL (data) connection, a device is not already connected\r
+ and the user application has indicated that the connection should be allowed */\r
+ Bluetooth_State.CurrentHCIState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) ||\r
+ !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ?\r
+ Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection;\r
+\r
+ BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State.CurrentHCIState == Bluetooth_Conn_RejectConnection) ?\r
+ PSTR("REJECTED") : PSTR("ACCEPTED"));\r
+\r
+ break;\r
+ case EVENT_PIN_CODE_REQUEST:\r
+ BT_HCI_DEBUG(1, "<< Pin Code Request");\r
+\r
+ /* Need to store the remote device's BT address in a temporary buffer for later use */\r
+ memcpy(Bluetooth_TempDeviceAddress,\r
+ &((BT_HCIEvent_PinCodeReq_t*)&EventParams)->RemoteAddress,\r
+ sizeof(Bluetooth_TempDeviceAddress));\r
+\r
+ Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendPINCode;\r
+ break;\r
+ case EVENT_LINK_KEY_REQUEST:\r
+ BT_HCI_DEBUG(1, "<< Link Key Request");\r
+ \r
+ /* Need to store the remote device's BT address in a temporary buffer for later use */\r
+ memcpy(Bluetooth_TempDeviceAddress,\r
+ &((BT_HCIEvent_LinkKeyReq_t*)&EventParams)->RemoteAddress,\r
+ sizeof(Bluetooth_TempDeviceAddress)); \r
+ \r
+ Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendLinkKeyNAK;\r
+ break;\r
+ case EVENT_CONNECTION_COMPLETE:\r
+ BT_HCI_DEBUG(1, "<< Connection Complete");\r
+ BT_HCI_DEBUG(2, "-- Handle: 0x%04X", ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle);\r
+\r
+ /* Need to store the remote device's BT address in a temporary buffer for later use */\r
+ memcpy(Bluetooth_Connection.RemoteAddress,\r
+ &((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->RemoteAddress,\r
+ sizeof(Bluetooth_TempDeviceAddress));\r
+\r
+ /* Store the created connection handle and indicate that the connection has been established */\r
+ Bluetooth_Connection.ConnectionHandle = ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle;\r
+ Bluetooth_Connection.IsConnected = true;\r
+ \r
+ Bluetooth_ConnectionComplete(); \r
+ break;\r
+ case EVENT_DISCONNECTION_COMPLETE:\r
+ BT_HCI_DEBUG(1, "<< Disconnection Complete");\r
+\r
+ /* Device disconnected, indicate connection information no longer valid */\r
+ Bluetooth_Connection.IsConnected = false;\r
+ \r
+ Bluetooth_DisconnectionComplete();\r
+ \r
+ Bluetooth_State.CurrentHCIState = Bluetooth_Init;\r
+ break;\r
+ }\r
+ }\r