From 071e02c6b6b4837fa9cf0b6d4c749994e02638d7 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Sat, 8 May 2010 03:12:14 +0000 Subject: Add svn:eol-style property to source files, so that the line endings are correctly converted to the target system's native end of line style. --- .../BluetoothHost/Lib/BluetoothACLPackets.c | 1512 ++++++++++---------- .../BluetoothHost/Lib/BluetoothACLPackets.h | 352 ++--- .../BluetoothHost/Lib/BluetoothClassCodes.h | 220 +-- .../BluetoothHost/Lib/BluetoothHCICommands.c | 792 +++++----- .../BluetoothHost/Lib/BluetoothHCICommands.h | 412 +++--- .../Incomplete/BluetoothHost/Lib/BluetoothStack.c | 198 +-- .../Incomplete/BluetoothHost/Lib/BluetoothStack.h | 322 ++--- .../BluetoothHost/Lib/ServiceDiscoveryProtocol.c | 390 ++--- .../BluetoothHost/Lib/ServiceDiscoveryProtocol.h | 296 ++-- 9 files changed, 2247 insertions(+), 2247 deletions(-) (limited to 'Demos/Host/Incomplete/BluetoothHost/Lib') diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c index 7c03a64cc..160aa3ac8 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c @@ -1,756 +1,756 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -/* - TODO: Make SendPacket respect receiver's MTU - TODO: Make ReceivePacket stitch together MTU fragments (?) - TODO: Add channel opened/closed callbacks - */ - -#define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C -#include "BluetoothACLPackets.h" - -/** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth - * stack task to manage the ACL processing state. - */ -void Bluetooth_ACLTask(void) -{ - /* Process incomming ACL packets, if any */ - Bluetooth_ProcessIncommingACLPackets(); - - /* Check for any half-open channels, send configuration details to the remote device if found */ - for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) - { - Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i]; - - bool MustSendConfigReq = true; - - /* Check if we are in a channel state which requires a configuration request to be sent */ - switch (ChannelData->State) - { - case Channel_Config_WaitConfig: - ChannelData->State = Channel_Config_WaitReqResp; - break; - case Channel_Config_WaitSendConfig: - ChannelData->State = Channel_Config_WaitResp; - break; - default: - MustSendConfigReq = false; - break; - } - - /* Only send a configuration request if it the channel was in a state which required it */ - if (MustSendConfigReq) - { - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_ConfigurationReq_t ConfigurationRequest; - - struct - { - BT_Config_Option_Header_t Header; - uint16_t Value; - } Option_LocalMTU; - } PacketData; - - /* Fill out the Signal Command header in the response packet */ - PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONFIGURATION_REQUEST; - PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; - PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConfigurationRequest) + - sizeof(PacketData.Option_LocalMTU); - - /* Fill out the Configuration Request in the response packet, including local MTU information */ - PacketData.ConfigurationRequest.DestinationChannel = ChannelData->RemoteNumber; - PacketData.ConfigurationRequest.Flags = 0; - PacketData.Option_LocalMTU.Header.Type = BT_CONFIG_OPTION_MTU; - PacketData.Option_LocalMTU.Header.Length = sizeof(PacketData.Option_LocalMTU.Value); - PacketData.Option_LocalMTU.Value = ChannelData->LocalMTU; - - Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Configuration Request"); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.ConfigurationRequest.DestinationChannel); - } - } -} - -/** Incomming ACL packet processing task. This task is called by the main ACL processing task to read in and process - * any incomming ACL packets to the device, handling signal requests as they are received or passing along channel - * data to the user application. - */ -static void Bluetooth_ProcessIncommingACLPackets(void) -{ - BT_ACL_Header_t ACLPacketHeader; - BT_DataPacket_Header_t DataHeader; - - Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE); - Pipe_Unfreeze(); - - if (!(Pipe_IsReadWriteAllowed())) - { - Pipe_Freeze(); - return; - } - - /* Read in the received ACL packet headers when it has been discovered that a packet has been received */ - Pipe_Read_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader)); - Pipe_Read_Stream_LE(&DataHeader, sizeof(DataHeader)); - - BT_ACL_DEBUG(2, ""); - BT_ACL_DEBUG(2, "Packet Received"); - BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader.ConnectionHandle & 0x0FFF)); - BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader.DataLength); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader.DestinationChannel); - BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader.PayloadLength); - - /* Check the packet's destination channel - signalling channel should be processed by the stack internally */ - if (DataHeader.DestinationChannel == BT_CHANNEL_SIGNALING) - { - /* Read in the Signal Command header of the incomming packet */ - BT_Signal_Header_t SignalCommandHeader; - Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader)); - - /* Dispatch to the appropriate handler function based on the Signal message code */ - switch (SignalCommandHeader.Code) - { - case BT_SIGNAL_CONNECTION_REQUEST: - Bluetooth_Signal_ConnectionReq(&SignalCommandHeader); - break; - case BT_SIGNAL_CONNECTION_RESPONSE: - Bluetooth_Signal_ConnectionResp(&SignalCommandHeader); - break; - case BT_SIGNAL_CONFIGURATION_REQUEST: - Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader); - break; - case BT_SIGNAL_CONFIGURATION_RESPONSE: - Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader); - break; - case BT_SIGNAL_DISCONNECTION_REQUEST: - Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader); - break; - case BT_SIGNAL_DISCONNECTION_RESPONSE: - Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader); - break; - case BT_SIGNAL_ECHO_REQUEST: - Bluetooth_Signal_EchoReq(&SignalCommandHeader); - break; - case BT_SIGNAL_INFORMATION_REQUEST: - Bluetooth_Signal_InformationReq(&SignalCommandHeader); - break; - case BT_SIGNAL_COMMAND_REJECT: - BT_ACL_DEBUG(1, "<< Command Reject"); - - uint16_t RejectReason; - Pipe_Read_Stream_LE(&RejectReason, sizeof(RejectReason)); - Pipe_Discard_Stream(ACLPacketHeader.DataLength - sizeof(RejectReason)); - Pipe_ClearIN(); - Pipe_Freeze(); - - BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason); - break; - default: - BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader.Code); - - Pipe_Discard_Stream(ACLPacketHeader.DataLength); - Pipe_ClearIN(); - Pipe_Freeze(); - break; - } - } - else - { - /* Non-signalling packet received, read in the packet contents and pass to the user application */ - uint8_t PacketData[DataHeader.PayloadLength]; - Pipe_Read_Stream_LE(PacketData, DataHeader.PayloadLength); - Pipe_ClearIN(); - Pipe_Freeze(); - - Bluetooth_PacketReceived(PacketData, DataHeader.PayloadLength, - Bluetooth_GetChannelData(DataHeader.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER)); - } -} - -/** Sends a packet to the remote device on the specified channel. - * - * \param[in] Data Pointer to a buffer where the data is to be sourced from - * \param[in] DataLen Length of the data to send - * \param[in] Channel Channel information structure containing the destination channel's information, NULL to send - * to the remote device's signalling channel - * - * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum - */ -uint8_t Bluetooth_SendPacket(void* Data, const uint16_t DataLen, Bluetooth_Channel_t* const Channel) -{ - BT_ACL_Header_t ACLPacketHeader; - BT_DataPacket_Header_t DataHeader; - - /* A remote device must be connected before a packet transmission is attempted */ - if (!(Bluetooth_Connection.IsConnected)) - return BT_SENDPACKET_NotConnected; - - /* If the destination channel is not the signalling channel and it is not currently fully open, abort */ - if ((Channel != NULL) && (Channel->State != Channel_Open)) - return BT_SENDPACKET_ChannelNotOpen; - - /* Fill out the packet's header from the remote device connection information structure */ - ACLPacketHeader.ConnectionHandle = (Bluetooth_Connection.ConnectionHandle | BT_ACL_FIRST_AUTOFLUSH); - ACLPacketHeader.DataLength = sizeof(DataHeader) + DataLen; - DataHeader.PayloadLength = DataLen; - DataHeader.DestinationChannel = (Channel == NULL) ? BT_CHANNEL_SIGNALING : Channel->RemoteNumber; - - Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE); - Pipe_Unfreeze(); - - /* Write the packet contents to the pipe so that it can be sent to the remote device */ - Pipe_Write_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader)); - Pipe_Write_Stream_LE(&DataHeader, sizeof(DataHeader)); - Pipe_Write_Stream_LE(Data, DataLen); - Pipe_ClearOUT(); - - Pipe_Freeze(); - - BT_ACL_DEBUG(2, ""); - BT_ACL_DEBUG(2, "Packet Sent"); - BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader.ConnectionHandle & 0x0FFF)); - BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader.DataLength); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader.DestinationChannel); - BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader.PayloadLength); - - return BT_SENDPACKET_NoError; -} - -/** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged. - * - * \note The channel is not immediately opened when this function returns - it must undergo a two way - * connection and configuration process first as the main Bluetooth stack processing task is - * repeatedly called. The returned channel is unusable by the user application until its State - * element has progressed to the Open state. - * - * \param[in] PSM PSM of the service that the channel is to be opened for - * - * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels - */ -Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM) -{ - Bluetooth_Channel_t* ChannelData = NULL; - - /* Search through the channel information list for a free channel item */ - for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) - { - if (Bluetooth_Connection.Channels[i].State == Channel_Closed) - { - ChannelData = &Bluetooth_Connection.Channels[i]; - - /* Set the new channel structure's local channel number to a unique value within the connection orientated - channel address space */ - ChannelData->LocalNumber = (BT_CHANNELNUMBER_BASEOFFSET + i); - break; - } - } - - /* If no free channel item was found in the list, all channels are occupied - abort */ - if (ChannelData == NULL) - return NULL; - - /* Reset and fill out the allocated channel's information structure with defaults */ - ChannelData->RemoteNumber = 0; - ChannelData->PSM = PSM; - ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU; - ChannelData->State = Channel_WaitConnectRsp; - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_ConnectionReq_t ConnectionRequest; - } PacketData; - - /* Fill out the Signal Command header in the response packet */ - PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_REQUEST; - PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; - PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConnectionRequest); - - /* Fill out the Connection Request in the response packet */ - PacketData.ConnectionRequest.PSM = PSM; - PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber; - - Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Connection Request"); - BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData.ConnectionRequest.PSM); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.ConnectionRequest.SourceChannel); - - return ChannelData; -} - -/** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data - * can be exchanged. - * - * \note The channel is not immediately closed when this function returns - it must undergo an asynchronous - * disconnection process first as the main Bluetooth stack processing task is repeatedly called. The - * returned channel is unusable by the user application upon return however the channel is not completely - * closed until its State element has progressed to the Closed state. - * - * \param[in,out] Channel Channel information structure of the channel to close - */ -void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel) -{ - /* Don't try to close a non-existing or already closed channel */ - if ((Channel == NULL) || (Channel->State == Channel_Closed)) - return; - - /* Set the channel's state to the start of the teardown process */ - Channel->State = Channel_WaitDisconnect; - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_DisconnectionReq_t DisconnectionRequest; - } PacketData; - - /* Fill out the Signal Command header in the response packet */ - PacketData.SignalCommandHeader.Code = BT_SIGNAL_DISCONNECTION_REQUEST; - PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; - PacketData.SignalCommandHeader.Length = sizeof(PacketData.DisconnectionRequest); - - /* Fill out the Disconnection Request in the response packet */ - PacketData.DisconnectionRequest.DestinationChannel = Channel->RemoteNumber; - PacketData.DisconnectionRequest.SourceChannel = Channel->LocalNumber; - - Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request"); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.DisconnectionRequest.DestinationChannel); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.DisconnectionRequest.SourceChannel); -} - -/** Internal Bluetooth stack Signal Command processing routine for a Connection Request command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_ConnectionReq_t ConnectionRequest; - - Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest)); - - Pipe_ClearIN(); - Pipe_Freeze(); - - BT_ACL_DEBUG(1, "<< L2CAP Connection Request"); - BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest.PSM); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest.SourceChannel); - - /* Try to retrieve the existing channel's information structure if it exists */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionRequest.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); - - /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */ - if (ChannelData == NULL) - { - /* Look through the channel information list for a free entry */ - for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) - { - if (Bluetooth_Connection.Channels[i].State == Channel_Closed) - { - ChannelData = &Bluetooth_Connection.Channels[i]; - - /* Set the new channel structure's local channel number to a unique value within the connection orientated - channel address space */ - ChannelData->LocalNumber = (BT_CHANNELNUMBER_BASEOFFSET + i); - break; - } - } - } - - uint8_t ChannelStatus = BT_CONNECTION_REFUSED_RESOURCES; - - /* Reset the channel item contents only if a channel entry was found for it */ - if (ChannelData != NULL) - { - /* Check if the user application will allow the connection based on its PSM */ - if (Bluetooth_ChannelConnectionRequest(ConnectionRequest.PSM)) - { - ChannelData->RemoteNumber = ConnectionRequest.SourceChannel; - ChannelData->PSM = ConnectionRequest.PSM; - ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU; - ChannelData->State = Channel_Config_WaitConfig; - - ChannelStatus = BT_CONNECTION_SUCCESSFUL; - } - else - { - ChannelStatus = BT_CONNECTION_REFUSED_PSM; - } - } - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_ConnectionResp_t ConnectionResponse; - } ResponsePacket; - - /* Fill out the Signal Command header in the response packet */ - ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_RESPONSE; - ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; - ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.ConnectionResponse); - - /* Fill out the Connection Response in the response packet */ - ResponsePacket.ConnectionResponse.DestinationChannel = ChannelData->LocalNumber; - ResponsePacket.ConnectionResponse.SourceChannel = ChannelData->RemoteNumber; - ResponsePacket.ConnectionResponse.Result = ChannelStatus; - ResponsePacket.ConnectionResponse.Status = 0x00; - - Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Connection Response"); - BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConnectionResponse.Result); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.ConnectionResponse.DestinationChannel); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConnectionResponse.SourceChannel); -} - -/** Internal Bluetooth stack Signal Command processing routine for a Connection Response command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_ConnectionResp_t ConnectionResponse; - - Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse)); - - Pipe_ClearIN(); - Pipe_Freeze(); - - BT_ACL_DEBUG(1, "<< L2CAP Connection Response"); - BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse.Result); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse.SourceChannel); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse.DestinationChannel); - - /* Search for the referenced channel in the channel information list */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.SourceChannel, CHANNEL_SEARCH_LOCALNUMBER); - - /* Only progress if the referenced channel data was found */ - if (ChannelData != NULL) - { - /* Set the channel structure's remote channel number to the channel allocated on the remote device */ - ChannelData->RemoteNumber = ConnectionResponse.SourceChannel; - ChannelData->State = (ConnectionResponse.Result == BT_CONNECTION_SUCCESSFUL) ? - Channel_Config_WaitConfig : Channel_Closed; - } -} - -/** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_ConfigurationReq_t ConfigurationRequest; - - /* Allocate a buffer large enough to hold the variable number of configuration options in the request */ - uint8_t OptionsLen = (SignalCommandHeader->Length - sizeof(ConfigurationRequest)); - uint8_t Options[OptionsLen]; - - Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest)); - Pipe_Read_Stream_LE(&Options, sizeof(Options)); - - Pipe_ClearIN(); - Pipe_Freeze(); - - /* Search for the referenced channel in the channel information list */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER); - - BT_ACL_DEBUG(1, "<< L2CAP Configuration Request"); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel); - BT_ACL_DEBUG(2, "-- Remote MTU: 0x%04X", ChannelData->RemoteMTU); - BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen); - - /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */ - if (ChannelData != NULL) - { - /* Iterate through each option in the configuration request to look for ones which can be processed */ - uint8_t OptionPos = 0; - while (OptionPos < OptionsLen) - { - BT_Config_Option_Header_t* OptionHeader = (BT_Config_Option_Header_t*)&Options[OptionPos]; - void* OptionData = &Options[OptionPos + sizeof(BT_Config_Option_Header_t)]; - - BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader->Type); - BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length)); - - /* Store the remote MTU option's value if present */ - if (OptionHeader->Type == BT_CONFIG_OPTION_MTU) - ChannelData->RemoteMTU = *((uint16_t*)OptionData); - - /* Progress to the next option in the packet */ - OptionPos += (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length); - } - } - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_ConfigurationResp_t ConfigurationResponse; - } ResponsePacket; - - /* Fill out the Signal Command header in the response packet */ - ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_CONFIGURATION_RESPONSE; - ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; - ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.ConfigurationResponse); - - /* Fill out the Configuration Response in the response packet */ - ResponsePacket.ConfigurationResponse.SourceChannel = ChannelData->RemoteNumber; - ResponsePacket.ConfigurationResponse.Flags = 0x00; - ResponsePacket.ConfigurationResponse.Result = (ChannelData != NULL) ? BT_CONFIGURATION_SUCCESSFUL : BT_CONFIGURATION_REJECTED; - - Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); - - if (ChannelData != NULL) - { - switch (ChannelData->State) - { - case Channel_Config_WaitConfig: - ChannelData->State = Channel_Config_WaitSendConfig; - break; - case Channel_Config_WaitReqResp: - ChannelData->State = Channel_Config_WaitResp; - break; - case Channel_Config_WaitReq: - ChannelData->State = Channel_Open; - break; - } - } - - BT_ACL_DEBUG(1, ">> L2CAP Configuration Response"); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConfigurationResponse.SourceChannel); - BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConfigurationResponse.Result); -} - -/** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_ConfigurationResp_t ConfigurationResponse; - - Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse)); - - Pipe_ClearIN(); - Pipe_Freeze(); - - BT_ACL_DEBUG(1, "<< L2CAP Configuration Response"); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse.SourceChannel); - BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse.Result); - - /* Search for the referenced channel in the channel information list */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); - - /* Only update the channel's state if it was found in the channel list */ - if (ChannelData != NULL) - { - /* Check if the channel configuration completed successfuly */ - if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL) - { - switch (ChannelData->State) - { - case Channel_Config_WaitReqResp: - ChannelData->State = Channel_Config_WaitReq; - break; - case Channel_Config_WaitResp: - ChannelData->State = Channel_Open; - break; - } - } - else - { - /* Configuration failed - close the channel */ - ChannelData->State = Channel_Closed; - } - } -} - -/** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_DisconnectionReq_t DisconnectionRequest; - - Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest)); - - BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request"); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest.DestinationChannel); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest.SourceChannel); - - Pipe_ClearIN(); - Pipe_Freeze(); - - /* Search for the referenced channel in the channel information list */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_DisconnectionResp_t DisconnectionResponse; - } ResponsePacket; - - /* Fill out the Signal Command header in the response packet */ - ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_DISCONNECTION_RESPONSE; - ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; - ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.DisconnectionResponse); - - /* Fill out the Disconnection Response in the response packet */ - ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber; - ResponsePacket.DisconnectionResponse.SourceChannel = ChannelData->LocalNumber; - - Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); - - /* If the channel was found in the channel list, close it */ - if (ChannelData != NULL) - ChannelData->State = Channel_Closed; - - BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response"); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.DisconnectionResponse.SourceChannel); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.DisconnectionResponse.DestinationChannel); -} - -/** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_DisconnectionResp_t DisconnectionResponse; - - Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse)); - - BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response"); - BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse.DestinationChannel); - BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse.SourceChannel); - - Pipe_ClearIN(); - Pipe_Freeze(); - - /* Search for the referenced channel in the channel information list */ - Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); - - /* If the channel was found in the channel list, close it */ - if (ChannelData != NULL) - ChannelData->State = Channel_Closed; -} - -/** Internal Bluetooth stack Signal Command processing routine for an Echo Request command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_ACL_DEBUG(1, "<< L2CAP Echo Request"); - - Pipe_ClearIN(); - Pipe_Freeze(); - - struct - { - BT_Signal_Header_t SignalCommandHeader; - } ResponsePacket; - - /* Fill out the Signal Command header in the response packet */ - ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_ECHO_RESPONSE; - ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; - ResponsePacket.SignalCommandHeader.Length = 0; - - Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Echo Response"); -} - -/** Internal Bluetooth stack Signal Command processing routine for an Information Request command. - * - * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header - */ -static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader) -{ - BT_Signal_InformationReq_t InformationRequest; - - Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest)); - - BT_ACL_DEBUG(1, "<< L2CAP Information Request"); - BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest.InfoType); - - Pipe_ClearIN(); - Pipe_Freeze(); - - struct - { - BT_Signal_Header_t SignalCommandHeader; - BT_Signal_InformationResp_t InformationResponse; - - uint8_t Data[4]; - } ResponsePacket; - - uint8_t DataLen = 0; - - /* Retrieve the requested information and store it in the outgoing packet, if found */ - switch (InformationRequest.InfoType) - { - case BT_INFOREQ_MTU: - ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL; - DataLen = 2; - - *((uint16_t*)&ResponsePacket.Data) = MAXIMUM_CHANNEL_MTU; - break; - case BT_INFOREQ_EXTENDEDFEATURES: - ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL; - DataLen = 4; - - *((uint32_t*)&ResponsePacket.Data) = 0; - break; - default: - ResponsePacket.InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED; - DataLen = 0; - break; - } - - /* Fill out the Signal Command header in the response packet */ - ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_INFORMATION_RESPONSE; - ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; - ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.InformationResponse) + DataLen; - - /* Fill out the Information Response in the response packet */ - ResponsePacket.InformationResponse.InfoType = InformationRequest.InfoType; - - Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket) - sizeof(ResponsePacket.Data) + DataLen), NULL); - - BT_ACL_DEBUG(1, ">> L2CAP Information Response"); - BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.InformationResponse.Result); -} +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/* + TODO: Make SendPacket respect receiver's MTU + TODO: Make ReceivePacket stitch together MTU fragments (?) + TODO: Add channel opened/closed callbacks + */ + +#define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C +#include "BluetoothACLPackets.h" + +/** Bluetooth ACL processing task. This task should be called repeatedly the main Bluetooth + * stack task to manage the ACL processing state. + */ +void Bluetooth_ACLTask(void) +{ + /* Process incomming ACL packets, if any */ + Bluetooth_ProcessIncommingACLPackets(); + + /* Check for any half-open channels, send configuration details to the remote device if found */ + for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) + { + Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i]; + + bool MustSendConfigReq = true; + + /* Check if we are in a channel state which requires a configuration request to be sent */ + switch (ChannelData->State) + { + case Channel_Config_WaitConfig: + ChannelData->State = Channel_Config_WaitReqResp; + break; + case Channel_Config_WaitSendConfig: + ChannelData->State = Channel_Config_WaitResp; + break; + default: + MustSendConfigReq = false; + break; + } + + /* Only send a configuration request if it the channel was in a state which required it */ + if (MustSendConfigReq) + { + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_ConfigurationReq_t ConfigurationRequest; + + struct + { + BT_Config_Option_Header_t Header; + uint16_t Value; + } Option_LocalMTU; + } PacketData; + + /* Fill out the Signal Command header in the response packet */ + PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONFIGURATION_REQUEST; + PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; + PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConfigurationRequest) + + sizeof(PacketData.Option_LocalMTU); + + /* Fill out the Configuration Request in the response packet, including local MTU information */ + PacketData.ConfigurationRequest.DestinationChannel = ChannelData->RemoteNumber; + PacketData.ConfigurationRequest.Flags = 0; + PacketData.Option_LocalMTU.Header.Type = BT_CONFIG_OPTION_MTU; + PacketData.Option_LocalMTU.Header.Length = sizeof(PacketData.Option_LocalMTU.Value); + PacketData.Option_LocalMTU.Value = ChannelData->LocalMTU; + + Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Configuration Request"); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.ConfigurationRequest.DestinationChannel); + } + } +} + +/** Incomming ACL packet processing task. This task is called by the main ACL processing task to read in and process + * any incomming ACL packets to the device, handling signal requests as they are received or passing along channel + * data to the user application. + */ +static void Bluetooth_ProcessIncommingACLPackets(void) +{ + BT_ACL_Header_t ACLPacketHeader; + BT_DataPacket_Header_t DataHeader; + + Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE); + Pipe_Unfreeze(); + + if (!(Pipe_IsReadWriteAllowed())) + { + Pipe_Freeze(); + return; + } + + /* Read in the received ACL packet headers when it has been discovered that a packet has been received */ + Pipe_Read_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader)); + Pipe_Read_Stream_LE(&DataHeader, sizeof(DataHeader)); + + BT_ACL_DEBUG(2, ""); + BT_ACL_DEBUG(2, "Packet Received"); + BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader.ConnectionHandle & 0x0FFF)); + BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader.DataLength); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader.DestinationChannel); + BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader.PayloadLength); + + /* Check the packet's destination channel - signalling channel should be processed by the stack internally */ + if (DataHeader.DestinationChannel == BT_CHANNEL_SIGNALING) + { + /* Read in the Signal Command header of the incomming packet */ + BT_Signal_Header_t SignalCommandHeader; + Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader)); + + /* Dispatch to the appropriate handler function based on the Signal message code */ + switch (SignalCommandHeader.Code) + { + case BT_SIGNAL_CONNECTION_REQUEST: + Bluetooth_Signal_ConnectionReq(&SignalCommandHeader); + break; + case BT_SIGNAL_CONNECTION_RESPONSE: + Bluetooth_Signal_ConnectionResp(&SignalCommandHeader); + break; + case BT_SIGNAL_CONFIGURATION_REQUEST: + Bluetooth_Signal_ConfigurationReq(&SignalCommandHeader); + break; + case BT_SIGNAL_CONFIGURATION_RESPONSE: + Bluetooth_Signal_ConfigurationResp(&SignalCommandHeader); + break; + case BT_SIGNAL_DISCONNECTION_REQUEST: + Bluetooth_Signal_DisconnectionReq(&SignalCommandHeader); + break; + case BT_SIGNAL_DISCONNECTION_RESPONSE: + Bluetooth_Signal_DisconnectionResp(&SignalCommandHeader); + break; + case BT_SIGNAL_ECHO_REQUEST: + Bluetooth_Signal_EchoReq(&SignalCommandHeader); + break; + case BT_SIGNAL_INFORMATION_REQUEST: + Bluetooth_Signal_InformationReq(&SignalCommandHeader); + break; + case BT_SIGNAL_COMMAND_REJECT: + BT_ACL_DEBUG(1, "<< Command Reject"); + + uint16_t RejectReason; + Pipe_Read_Stream_LE(&RejectReason, sizeof(RejectReason)); + Pipe_Discard_Stream(ACLPacketHeader.DataLength - sizeof(RejectReason)); + Pipe_ClearIN(); + Pipe_Freeze(); + + BT_ACL_DEBUG(2, "-- Reason: %d", RejectReason); + break; + default: + BT_ACL_DEBUG(1, "<< Unknown Signaling Command 0x%02X", SignalCommandHeader.Code); + + Pipe_Discard_Stream(ACLPacketHeader.DataLength); + Pipe_ClearIN(); + Pipe_Freeze(); + break; + } + } + else + { + /* Non-signalling packet received, read in the packet contents and pass to the user application */ + uint8_t PacketData[DataHeader.PayloadLength]; + Pipe_Read_Stream_LE(PacketData, DataHeader.PayloadLength); + Pipe_ClearIN(); + Pipe_Freeze(); + + Bluetooth_PacketReceived(PacketData, DataHeader.PayloadLength, + Bluetooth_GetChannelData(DataHeader.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER)); + } +} + +/** Sends a packet to the remote device on the specified channel. + * + * \param[in] Data Pointer to a buffer where the data is to be sourced from + * \param[in] DataLen Length of the data to send + * \param[in] Channel Channel information structure containing the destination channel's information, NULL to send + * to the remote device's signalling channel + * + * \return A value from the \ref BT_SendPacket_ErrorCodes_t enum + */ +uint8_t Bluetooth_SendPacket(void* Data, const uint16_t DataLen, Bluetooth_Channel_t* const Channel) +{ + BT_ACL_Header_t ACLPacketHeader; + BT_DataPacket_Header_t DataHeader; + + /* A remote device must be connected before a packet transmission is attempted */ + if (!(Bluetooth_Connection.IsConnected)) + return BT_SENDPACKET_NotConnected; + + /* If the destination channel is not the signalling channel and it is not currently fully open, abort */ + if ((Channel != NULL) && (Channel->State != Channel_Open)) + return BT_SENDPACKET_ChannelNotOpen; + + /* Fill out the packet's header from the remote device connection information structure */ + ACLPacketHeader.ConnectionHandle = (Bluetooth_Connection.ConnectionHandle | BT_ACL_FIRST_AUTOFLUSH); + ACLPacketHeader.DataLength = sizeof(DataHeader) + DataLen; + DataHeader.PayloadLength = DataLen; + DataHeader.DestinationChannel = (Channel == NULL) ? BT_CHANNEL_SIGNALING : Channel->RemoteNumber; + + Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE); + Pipe_Unfreeze(); + + /* Write the packet contents to the pipe so that it can be sent to the remote device */ + Pipe_Write_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader)); + Pipe_Write_Stream_LE(&DataHeader, sizeof(DataHeader)); + Pipe_Write_Stream_LE(Data, DataLen); + Pipe_ClearOUT(); + + Pipe_Freeze(); + + BT_ACL_DEBUG(2, ""); + BT_ACL_DEBUG(2, "Packet Sent"); + BT_ACL_DEBUG(2, "-- Connection Handle: 0x%04X", (ACLPacketHeader.ConnectionHandle & 0x0FFF)); + BT_ACL_DEBUG(2, "-- Data Length: 0x%04X", ACLPacketHeader.DataLength); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DataHeader.DestinationChannel); + BT_ACL_DEBUG(2, "-- Payload Length: 0x%04X", DataHeader.PayloadLength); + + return BT_SENDPACKET_NoError; +} + +/** Opens a bluetooth channel to the currently connected remote device, so that data can be exchanged. + * + * \note The channel is not immediately opened when this function returns - it must undergo a two way + * connection and configuration process first as the main Bluetooth stack processing task is + * repeatedly called. The returned channel is unusable by the user application until its State + * element has progressed to the Open state. + * + * \param[in] PSM PSM of the service that the channel is to be opened for + * + * \return Pointer to the channel information structure of the opened channel, or NULL if no free channels + */ +Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM) +{ + Bluetooth_Channel_t* ChannelData = NULL; + + /* Search through the channel information list for a free channel item */ + for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) + { + if (Bluetooth_Connection.Channels[i].State == Channel_Closed) + { + ChannelData = &Bluetooth_Connection.Channels[i]; + + /* Set the new channel structure's local channel number to a unique value within the connection orientated + channel address space */ + ChannelData->LocalNumber = (BT_CHANNELNUMBER_BASEOFFSET + i); + break; + } + } + + /* If no free channel item was found in the list, all channels are occupied - abort */ + if (ChannelData == NULL) + return NULL; + + /* Reset and fill out the allocated channel's information structure with defaults */ + ChannelData->RemoteNumber = 0; + ChannelData->PSM = PSM; + ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU; + ChannelData->State = Channel_WaitConnectRsp; + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_ConnectionReq_t ConnectionRequest; + } PacketData; + + /* Fill out the Signal Command header in the response packet */ + PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_REQUEST; + PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; + PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConnectionRequest); + + /* Fill out the Connection Request in the response packet */ + PacketData.ConnectionRequest.PSM = PSM; + PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber; + + Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Connection Request"); + BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData.ConnectionRequest.PSM); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.ConnectionRequest.SourceChannel); + + return ChannelData; +} + +/** Closes a bluetooth channel that is open to the currently connected remote device, so that no further data + * can be exchanged. + * + * \note The channel is not immediately closed when this function returns - it must undergo an asynchronous + * disconnection process first as the main Bluetooth stack processing task is repeatedly called. The + * returned channel is unusable by the user application upon return however the channel is not completely + * closed until its State element has progressed to the Closed state. + * + * \param[in,out] Channel Channel information structure of the channel to close + */ +void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel) +{ + /* Don't try to close a non-existing or already closed channel */ + if ((Channel == NULL) || (Channel->State == Channel_Closed)) + return; + + /* Set the channel's state to the start of the teardown process */ + Channel->State = Channel_WaitDisconnect; + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_DisconnectionReq_t DisconnectionRequest; + } PacketData; + + /* Fill out the Signal Command header in the response packet */ + PacketData.SignalCommandHeader.Code = BT_SIGNAL_DISCONNECTION_REQUEST; + PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier; + PacketData.SignalCommandHeader.Length = sizeof(PacketData.DisconnectionRequest); + + /* Fill out the Disconnection Request in the response packet */ + PacketData.DisconnectionRequest.DestinationChannel = Channel->RemoteNumber; + PacketData.DisconnectionRequest.SourceChannel = Channel->LocalNumber; + + Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request"); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.DisconnectionRequest.DestinationChannel); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.DisconnectionRequest.SourceChannel); +} + +/** Internal Bluetooth stack Signal Command processing routine for a Connection Request command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_ConnectionReq_t ConnectionRequest; + + Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest)); + + Pipe_ClearIN(); + Pipe_Freeze(); + + BT_ACL_DEBUG(1, "<< L2CAP Connection Request"); + BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest.PSM); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest.SourceChannel); + + /* Try to retrieve the existing channel's information structure if it exists */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionRequest.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); + + /* If an existing channel item with the correct remote channel number was not found, find a free channel entry */ + if (ChannelData == NULL) + { + /* Look through the channel information list for a free entry */ + for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) + { + if (Bluetooth_Connection.Channels[i].State == Channel_Closed) + { + ChannelData = &Bluetooth_Connection.Channels[i]; + + /* Set the new channel structure's local channel number to a unique value within the connection orientated + channel address space */ + ChannelData->LocalNumber = (BT_CHANNELNUMBER_BASEOFFSET + i); + break; + } + } + } + + uint8_t ChannelStatus = BT_CONNECTION_REFUSED_RESOURCES; + + /* Reset the channel item contents only if a channel entry was found for it */ + if (ChannelData != NULL) + { + /* Check if the user application will allow the connection based on its PSM */ + if (Bluetooth_ChannelConnectionRequest(ConnectionRequest.PSM)) + { + ChannelData->RemoteNumber = ConnectionRequest.SourceChannel; + ChannelData->PSM = ConnectionRequest.PSM; + ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU; + ChannelData->State = Channel_Config_WaitConfig; + + ChannelStatus = BT_CONNECTION_SUCCESSFUL; + } + else + { + ChannelStatus = BT_CONNECTION_REFUSED_PSM; + } + } + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_ConnectionResp_t ConnectionResponse; + } ResponsePacket; + + /* Fill out the Signal Command header in the response packet */ + ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_RESPONSE; + ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; + ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.ConnectionResponse); + + /* Fill out the Connection Response in the response packet */ + ResponsePacket.ConnectionResponse.DestinationChannel = ChannelData->LocalNumber; + ResponsePacket.ConnectionResponse.SourceChannel = ChannelData->RemoteNumber; + ResponsePacket.ConnectionResponse.Result = ChannelStatus; + ResponsePacket.ConnectionResponse.Status = 0x00; + + Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Connection Response"); + BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConnectionResponse.Result); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.ConnectionResponse.DestinationChannel); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConnectionResponse.SourceChannel); +} + +/** Internal Bluetooth stack Signal Command processing routine for a Connection Response command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_ConnectionResp_t ConnectionResponse; + + Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse)); + + Pipe_ClearIN(); + Pipe_Freeze(); + + BT_ACL_DEBUG(1, "<< L2CAP Connection Response"); + BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConnectionResponse.Result); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionResponse.SourceChannel); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConnectionResponse.DestinationChannel); + + /* Search for the referenced channel in the channel information list */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.SourceChannel, CHANNEL_SEARCH_LOCALNUMBER); + + /* Only progress if the referenced channel data was found */ + if (ChannelData != NULL) + { + /* Set the channel structure's remote channel number to the channel allocated on the remote device */ + ChannelData->RemoteNumber = ConnectionResponse.SourceChannel; + ChannelData->State = (ConnectionResponse.Result == BT_CONNECTION_SUCCESSFUL) ? + Channel_Config_WaitConfig : Channel_Closed; + } +} + +/** Internal Bluetooth stack Signal Command processing routine for a Configuration Request command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_ConfigurationReq_t ConfigurationRequest; + + /* Allocate a buffer large enough to hold the variable number of configuration options in the request */ + uint8_t OptionsLen = (SignalCommandHeader->Length - sizeof(ConfigurationRequest)); + uint8_t Options[OptionsLen]; + + Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest)); + Pipe_Read_Stream_LE(&Options, sizeof(Options)); + + Pipe_ClearIN(); + Pipe_Freeze(); + + /* Search for the referenced channel in the channel information list */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, CHANNEL_SEARCH_LOCALNUMBER); + + BT_ACL_DEBUG(1, "<< L2CAP Configuration Request"); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel); + BT_ACL_DEBUG(2, "-- Remote MTU: 0x%04X", ChannelData->RemoteMTU); + BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", OptionsLen); + + /* Only look at the channel configuration options if a valid channel entry for the local channel number was found */ + if (ChannelData != NULL) + { + /* Iterate through each option in the configuration request to look for ones which can be processed */ + uint8_t OptionPos = 0; + while (OptionPos < OptionsLen) + { + BT_Config_Option_Header_t* OptionHeader = (BT_Config_Option_Header_t*)&Options[OptionPos]; + void* OptionData = &Options[OptionPos + sizeof(BT_Config_Option_Header_t)]; + + BT_ACL_DEBUG(2, "-- Option Type: 0x%04X", OptionHeader->Type); + BT_ACL_DEBUG(2, "-- Option Length: 0x%04X", (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length)); + + /* Store the remote MTU option's value if present */ + if (OptionHeader->Type == BT_CONFIG_OPTION_MTU) + ChannelData->RemoteMTU = *((uint16_t*)OptionData); + + /* Progress to the next option in the packet */ + OptionPos += (sizeof(BT_Config_Option_Header_t) + OptionHeader->Length); + } + } + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_ConfigurationResp_t ConfigurationResponse; + } ResponsePacket; + + /* Fill out the Signal Command header in the response packet */ + ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_CONFIGURATION_RESPONSE; + ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; + ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.ConfigurationResponse); + + /* Fill out the Configuration Response in the response packet */ + ResponsePacket.ConfigurationResponse.SourceChannel = ChannelData->RemoteNumber; + ResponsePacket.ConfigurationResponse.Flags = 0x00; + ResponsePacket.ConfigurationResponse.Result = (ChannelData != NULL) ? BT_CONFIGURATION_SUCCESSFUL : BT_CONFIGURATION_REJECTED; + + Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); + + if (ChannelData != NULL) + { + switch (ChannelData->State) + { + case Channel_Config_WaitConfig: + ChannelData->State = Channel_Config_WaitSendConfig; + break; + case Channel_Config_WaitReqResp: + ChannelData->State = Channel_Config_WaitResp; + break; + case Channel_Config_WaitReq: + ChannelData->State = Channel_Open; + break; + } + } + + BT_ACL_DEBUG(1, ">> L2CAP Configuration Response"); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConfigurationResponse.SourceChannel); + BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConfigurationResponse.Result); +} + +/** Internal Bluetooth stack Signal Command processing routine for a Configuration Response command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_ConfigurationResp_t ConfigurationResponse; + + Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse)); + + Pipe_ClearIN(); + Pipe_Freeze(); + + BT_ACL_DEBUG(1, "<< L2CAP Configuration Response"); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse.SourceChannel); + BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse.Result); + + /* Search for the referenced channel in the channel information list */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); + + /* Only update the channel's state if it was found in the channel list */ + if (ChannelData != NULL) + { + /* Check if the channel configuration completed successfuly */ + if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL) + { + switch (ChannelData->State) + { + case Channel_Config_WaitReqResp: + ChannelData->State = Channel_Config_WaitReq; + break; + case Channel_Config_WaitResp: + ChannelData->State = Channel_Open; + break; + } + } + else + { + /* Configuration failed - close the channel */ + ChannelData->State = Channel_Closed; + } + } +} + +/** Internal Bluetooth stack Signal Command processing routine for a Disconnection Request command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_DisconnectionReq_t DisconnectionRequest; + + Pipe_Read_Stream_LE(&DisconnectionRequest, sizeof(DisconnectionRequest)); + + BT_ACL_DEBUG(1, "<< L2CAP Disconnection Request"); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionRequest.DestinationChannel); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionRequest.SourceChannel); + + Pipe_ClearIN(); + Pipe_Freeze(); + + /* Search for the referenced channel in the channel information list */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_DisconnectionResp_t DisconnectionResponse; + } ResponsePacket; + + /* Fill out the Signal Command header in the response packet */ + ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_DISCONNECTION_RESPONSE; + ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; + ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.DisconnectionResponse); + + /* Fill out the Disconnection Response in the response packet */ + ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber; + ResponsePacket.DisconnectionResponse.SourceChannel = ChannelData->LocalNumber; + + Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); + + /* If the channel was found in the channel list, close it */ + if (ChannelData != NULL) + ChannelData->State = Channel_Closed; + + BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response"); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.DisconnectionResponse.SourceChannel); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.DisconnectionResponse.DestinationChannel); +} + +/** Internal Bluetooth stack Signal Command processing routine for a Disconnection Response command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_DisconnectionResp_t DisconnectionResponse; + + Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse)); + + BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response"); + BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse.DestinationChannel); + BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse.SourceChannel); + + Pipe_ClearIN(); + Pipe_Freeze(); + + /* Search for the referenced channel in the channel information list */ + Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, CHANNEL_SEARCH_REMOTENUMBER); + + /* If the channel was found in the channel list, close it */ + if (ChannelData != NULL) + ChannelData->State = Channel_Closed; +} + +/** Internal Bluetooth stack Signal Command processing routine for an Echo Request command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_ACL_DEBUG(1, "<< L2CAP Echo Request"); + + Pipe_ClearIN(); + Pipe_Freeze(); + + struct + { + BT_Signal_Header_t SignalCommandHeader; + } ResponsePacket; + + /* Fill out the Signal Command header in the response packet */ + ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_ECHO_RESPONSE; + ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; + ResponsePacket.SignalCommandHeader.Length = 0; + + Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Echo Response"); +} + +/** Internal Bluetooth stack Signal Command processing routine for an Information Request command. + * + * \param[in] SignalCommandHeader Pointer to the start of the received packet's Signal Command header + */ +static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader) +{ + BT_Signal_InformationReq_t InformationRequest; + + Pipe_Read_Stream_LE(&InformationRequest, sizeof(InformationRequest)); + + BT_ACL_DEBUG(1, "<< L2CAP Information Request"); + BT_ACL_DEBUG(2, "-- Info Type: 0x%04X", InformationRequest.InfoType); + + Pipe_ClearIN(); + Pipe_Freeze(); + + struct + { + BT_Signal_Header_t SignalCommandHeader; + BT_Signal_InformationResp_t InformationResponse; + + uint8_t Data[4]; + } ResponsePacket; + + uint8_t DataLen = 0; + + /* Retrieve the requested information and store it in the outgoing packet, if found */ + switch (InformationRequest.InfoType) + { + case BT_INFOREQ_MTU: + ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL; + DataLen = 2; + + *((uint16_t*)&ResponsePacket.Data) = MAXIMUM_CHANNEL_MTU; + break; + case BT_INFOREQ_EXTENDEDFEATURES: + ResponsePacket.InformationResponse.Result = BT_INFORMATION_SUCCESSFUL; + DataLen = 4; + + *((uint32_t*)&ResponsePacket.Data) = 0; + break; + default: + ResponsePacket.InformationResponse.Result = BT_INFORMATION_NOTSUPPORTED; + DataLen = 0; + break; + } + + /* Fill out the Signal Command header in the response packet */ + ResponsePacket.SignalCommandHeader.Code = BT_SIGNAL_INFORMATION_RESPONSE; + ResponsePacket.SignalCommandHeader.Identifier = SignalCommandHeader->Identifier; + ResponsePacket.SignalCommandHeader.Length = sizeof(ResponsePacket.InformationResponse) + DataLen; + + /* Fill out the Information Response in the response packet */ + ResponsePacket.InformationResponse.InfoType = InformationRequest.InfoType; + + Bluetooth_SendPacket(&ResponsePacket, (sizeof(ResponsePacket) - sizeof(ResponsePacket.Data) + DataLen), NULL); + + BT_ACL_DEBUG(1, ">> L2CAP Information Response"); + BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.InformationResponse.Result); +} diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h index 1a72cede3..cc0160ff7 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h @@ -1,176 +1,176 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#ifndef _BLUETOOTH_ACLPACKETS_ -#define _BLUETOOTH_ACLPACKETS_ - - /* Includes: */ - #include - #include - #include - #include - - #include - #include - - #include "BluetoothStack.h" - - /* Macros: */ - #define BT_ACL_DEBUG(l, s, ...) do { if (ACL_DEBUG_LEVEL >= l) printf_P(PSTR("(ACL) " s "\r\n"), ##__VA_ARGS__); } while (0) - #define ACL_DEBUG_LEVEL 0 - - #define BT_CHANNELNUMBER_BASEOFFSET 0x0040 - - #define BT_CHANNEL_SIGNALING 0x0001 - #define BT_CHANNEL_CONNECTIONLESS 0x0002 - - #define BT_SIGNAL_COMMAND_REJECT 0x01 - #define BT_SIGNAL_CONNECTION_REQUEST 0x02 - #define BT_SIGNAL_CONNECTION_RESPONSE 0x03 - #define BT_SIGNAL_CONFIGURATION_REQUEST 0x04 - #define BT_SIGNAL_CONFIGURATION_RESPONSE 0x05 - #define BT_SIGNAL_DISCONNECTION_REQUEST 0x06 - #define BT_SIGNAL_DISCONNECTION_RESPONSE 0x07 - #define BT_SIGNAL_ECHO_REQUEST 0x08 - #define BT_SIGNAL_ECHO_RESPONSE 0x09 - #define BT_SIGNAL_INFORMATION_REQUEST 0x0A - #define BT_SIGNAL_INFORMATION_RESPONSE 0x0B - - #define BT_INFOREQ_MTU 0x0001 - #define BT_INFOREQ_EXTENDEDFEATURES 0x0002 - - #define BT_INFORMATION_SUCCESSFUL 0x0000 - #define BT_INFORMATION_NOTSUPPORTED 0x0001 - - #define BT_CONNECTION_SUCCESSFUL 0x0000 - #define BT_CONNECTION_REFUSED_PSM 0x0002 - #define BT_CONNECTION_REFUSED_RESOURCES 0x0004 - - #define BT_CONFIGURATION_SUCCESSFUL 0x0000 - #define BT_CONFIGURATION_REJECTED 0x0002 - #define BT_CONFIGURATION_UNKNOWNOPTIONS 0x0003 - - #define BT_CONFIG_OPTION_MTU 1 - - #define BT_ACL_FIRST_AUTOFLUSH (1 << 13) - - /* Type Defines: */ - typedef struct - { - uint16_t ConnectionHandle; - uint16_t DataLength; - } BT_ACL_Header_t; - - typedef struct - { - uint16_t PayloadLength; - uint16_t DestinationChannel; - } BT_DataPacket_Header_t; - - typedef struct - { - uint8_t Code; - uint8_t Identifier; - uint16_t Length; - } BT_Signal_Header_t; - - typedef struct - { - uint16_t PSM; - uint16_t SourceChannel; - } BT_Signal_ConnectionReq_t; - - typedef struct - { - uint16_t DestinationChannel; - uint16_t SourceChannel; - uint16_t Result; - uint16_t Status; - } BT_Signal_ConnectionResp_t; - - typedef struct - { - uint16_t DestinationChannel; - uint16_t SourceChannel; - } BT_Signal_DisconnectionReq_t; - - typedef struct - { - uint16_t DestinationChannel; - uint16_t SourceChannel; - } BT_Signal_DisconnectionResp_t; - - typedef struct - { - uint16_t DestinationChannel; - uint16_t Flags; - } BT_Signal_ConfigurationReq_t; - - typedef struct - { - uint16_t SourceChannel; - uint16_t Flags; - uint16_t Result; - } BT_Signal_ConfigurationResp_t; - - typedef struct - { - uint16_t InfoType; - } BT_Signal_InformationReq_t; - - typedef struct - { - uint16_t InfoType; - uint16_t Result; - } BT_Signal_InformationResp_t; - - typedef struct - { - uint8_t Type; - uint8_t Length; - } BT_Config_Option_Header_t; - - /* Function Prototypes: */ - void Bluetooth_ACLTask(void); - - #if defined(INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C) - static void Bluetooth_ProcessIncommingACLPackets(void); - - static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader); - static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader); - #endif - -#endif +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#ifndef _BLUETOOTH_ACLPACKETS_ +#define _BLUETOOTH_ACLPACKETS_ + + /* Includes: */ + #include + #include + #include + #include + + #include + #include + + #include "BluetoothStack.h" + + /* Macros: */ + #define BT_ACL_DEBUG(l, s, ...) do { if (ACL_DEBUG_LEVEL >= l) printf_P(PSTR("(ACL) " s "\r\n"), ##__VA_ARGS__); } while (0) + #define ACL_DEBUG_LEVEL 0 + + #define BT_CHANNELNUMBER_BASEOFFSET 0x0040 + + #define BT_CHANNEL_SIGNALING 0x0001 + #define BT_CHANNEL_CONNECTIONLESS 0x0002 + + #define BT_SIGNAL_COMMAND_REJECT 0x01 + #define BT_SIGNAL_CONNECTION_REQUEST 0x02 + #define BT_SIGNAL_CONNECTION_RESPONSE 0x03 + #define BT_SIGNAL_CONFIGURATION_REQUEST 0x04 + #define BT_SIGNAL_CONFIGURATION_RESPONSE 0x05 + #define BT_SIGNAL_DISCONNECTION_REQUEST 0x06 + #define BT_SIGNAL_DISCONNECTION_RESPONSE 0x07 + #define BT_SIGNAL_ECHO_REQUEST 0x08 + #define BT_SIGNAL_ECHO_RESPONSE 0x09 + #define BT_SIGNAL_INFORMATION_REQUEST 0x0A + #define BT_SIGNAL_INFORMATION_RESPONSE 0x0B + + #define BT_INFOREQ_MTU 0x0001 + #define BT_INFOREQ_EXTENDEDFEATURES 0x0002 + + #define BT_INFORMATION_SUCCESSFUL 0x0000 + #define BT_INFORMATION_NOTSUPPORTED 0x0001 + + #define BT_CONNECTION_SUCCESSFUL 0x0000 + #define BT_CONNECTION_REFUSED_PSM 0x0002 + #define BT_CONNECTION_REFUSED_RESOURCES 0x0004 + + #define BT_CONFIGURATION_SUCCESSFUL 0x0000 + #define BT_CONFIGURATION_REJECTED 0x0002 + #define BT_CONFIGURATION_UNKNOWNOPTIONS 0x0003 + + #define BT_CONFIG_OPTION_MTU 1 + + #define BT_ACL_FIRST_AUTOFLUSH (1 << 13) + + /* Type Defines: */ + typedef struct + { + uint16_t ConnectionHandle; + uint16_t DataLength; + } BT_ACL_Header_t; + + typedef struct + { + uint16_t PayloadLength; + uint16_t DestinationChannel; + } BT_DataPacket_Header_t; + + typedef struct + { + uint8_t Code; + uint8_t Identifier; + uint16_t Length; + } BT_Signal_Header_t; + + typedef struct + { + uint16_t PSM; + uint16_t SourceChannel; + } BT_Signal_ConnectionReq_t; + + typedef struct + { + uint16_t DestinationChannel; + uint16_t SourceChannel; + uint16_t Result; + uint16_t Status; + } BT_Signal_ConnectionResp_t; + + typedef struct + { + uint16_t DestinationChannel; + uint16_t SourceChannel; + } BT_Signal_DisconnectionReq_t; + + typedef struct + { + uint16_t DestinationChannel; + uint16_t SourceChannel; + } BT_Signal_DisconnectionResp_t; + + typedef struct + { + uint16_t DestinationChannel; + uint16_t Flags; + } BT_Signal_ConfigurationReq_t; + + typedef struct + { + uint16_t SourceChannel; + uint16_t Flags; + uint16_t Result; + } BT_Signal_ConfigurationResp_t; + + typedef struct + { + uint16_t InfoType; + } BT_Signal_InformationReq_t; + + typedef struct + { + uint16_t InfoType; + uint16_t Result; + } BT_Signal_InformationResp_t; + + typedef struct + { + uint8_t Type; + uint8_t Length; + } BT_Config_Option_Header_t; + + /* Function Prototypes: */ + void Bluetooth_ACLTask(void); + + #if defined(INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C) + static void Bluetooth_ProcessIncommingACLPackets(void); + + static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_EchoReq(const BT_Signal_Header_t* const SignalCommandHeader); + static inline void Bluetooth_Signal_InformationReq(const BT_Signal_Header_t* const SignalCommandHeader); + #endif + +#endif diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothClassCodes.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothClassCodes.h index 17e53c8e8..209256fd0 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothClassCodes.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothClassCodes.h @@ -1,110 +1,110 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#ifndef _BLUETOOTH_CLASS_CODES_H_ -#define _BLUETOOTH_CLASS_CODES_H_ - - /* Macros: */ - #define DEVICE_CLASS_SERVICE_POSITIONING (1UL << 16) - #define DEVICE_CLASS_SERVICE_NETWORKING (1UL << 17) - #define DEVICE_CLASS_SERVICE_RENDERING (1UL << 18) - #define DEVICE_CLASS_SERVICE_CAPTURING (1UL << 19) - #define DEVICE_CLASS_SERVICE_OBJECTTRANSFER (1UL << 20) - #define DEVICE_CLASS_SERVICE_AUDIO (1UL << 21) - #define DEVICE_CLASS_SERVICE_TELEPHONY (1UL << 22) - #define DEVICE_CLASS_SERVICE_INFORMATION (1UL << 23) - - #define DEVICE_CLASS_MAJOR_MISC (0x00 << 8) - #define DEVICE_CLASS_MAJOR_COMPUTER (0x01 << 8) - #define DEVICE_CLASS_MAJOR_PHONE (0x02 << 8) - #define DEVICE_CLASS_MAJOR_LAN (0x03 << 8) - #define DEVICE_CLASS_MAJOR_AUDIOVIDEO (0x04 << 8) - #define DEVICE_CLASS_MAJOR_PERIPHERAL (0x05 << 8) - #define DEVICE_CLASS_MAJOR_IMAGING (0x06 << 8) - #define DEVICE_CLASS_MAJOR_UNCLASSIFIED (0x1F << 8) - - #define DEVICE_CLASS_MINOR_COMPUTER_UNCATEGORIZED (0x00 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_DESKTOP (0x01 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_SERVER (0x02 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_LAPTOP (0x03 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_HANDHELD (0x04 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_PALM (0x05 << 2) - #define DEVICE_CLASS_MINOR_COMPUTER_WEARABLE (0x06 << 2) - - #define DEVICE_CLASS_MINOR_PHONE_UNCATEGORIZED (0x00 << 2) - #define DEVICE_CLASS_MINOR_PHONE_CELLULAR (0x01 << 2) - #define DEVICE_CLASS_MINOR_PHONE_CORDLESS (0x02 << 2) - #define DEVICE_CLASS_MINOR_PHONE_SMARTPHONE (0x03 << 2) - #define DEVICE_CLASS_MINOR_PHONE_WIREDMODEM (0x04 << 2) - #define DEVICE_CLASS_MINOR_PHONE_ISDN (0x05 << 2) - - #define DEVICE_CLASS_MINOR_LAN_FULLY_AVAILABLE (0x00 << 5) - #define DEVICE_CLASS_MINOR_LAN_1_TO_17_PC_UTILIZED (0x01 << 5) - #define DEVICE_CLASS_MINOR_LAN_17_TO_33_PC_UTILIZED (0x02 << 5) - #define DEVICE_CLASS_MINOR_LAN_33_TO_50_PC_UTILIZED (0x03 << 5) - #define DEVICE_CLASS_MINOR_LAN_50_TO_67_PC_UTILIZED (0x04 << 5) - #define DEVICE_CLASS_MINOR_LAN_67_TO_83_PC_UTILIZED (0x05 << 5) - #define DEVICE_CLASS_MINOR_LAN_83_TO_99_PC_UTILIZED (0x06 << 5) - #define DEVICE_CLASS_MINOR_NO_SERVICE_AVAILABLE (0x07 << 5) - - #define DEVICE_CLASS_MINOR_AV_UNCATEGORIZED (0x00 << 2) - #define DEVICE_CLASS_MINOR_AV_HEADSET (0x01 << 2) - #define DEVICE_CLASS_MINOR_AV_HANDSFREE (0x02 << 2) - #define DEVICE_CLASS_MINOR_AV_MICROPHONE (0x04 << 2) - #define DEVICE_CLASS_MINOR_AV_LOUDSPEAKER (0x05 << 2) - #define DEVICE_CLASS_MINOR_AV_HEADPHONES (0x06 << 2) - #define DEVICE_CLASS_MINOR_AV_PORTABLE_AUDIO (0x07 << 2) - #define DEVICE_CLASS_MINOR_AV_CARAUDIO (0x08 << 2) - #define DEVICE_CLASS_MINOR_AV_SETTOP_BOX (0x09 << 2) - #define DEVICE_CLASS_MINOR_AV_HIFI (0x0A << 2) - #define DEVICE_CLASS_MINOR_AV_VCR (0x0B << 2) - #define DEVICE_CLASS_MINOR_AV_VIDEO_CAMERA (0x0C << 2) - #define DEVICE_CLASS_MINOR_AV_CAMCORDER (0x0D << 2) - #define DEVICE_CLASS_MINOR_AV_VIDEO_MONITOR (0x0E << 2) - #define DEVICE_CLASS_MINOR_AV_DISPLAY_AND_LOUDSPEAKER (0x0F << 2) - #define DEVICE_CLASS_MINOR_AV_VIDEO_CONFERENCING (0x10 << 2) - #define DEVICE_CLASS_MINOR_AV_GAMING_TOY (0x12 << 2) - - #define DEVICE_CLASS_MINOR_PERIPHERAL_KEYBOARD (0x01 << 6) - #define DEVICE_CLASS_MINOR_PERIPHERAL_POINTING (0x02 << 6) - #define DEVICE_CLASS_MINOR_PERIPHERAL_COMBO (0x03 << 6) - #define DEVICE_CLASS_MINOR_PERIPHERAL_UNCATEGORIZED (0x00 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_JOYSTICK (0x01 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_GAMEPAD (0x02 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_REMOTE_CONTROL (0x03 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_SENSING_DEVICE (0x04 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_DIGITIZER (0x05 << 2) - #define DEVICE_CLASS_MINOR_PERIPHERAL_CARD_READER (0x06 << 2) - - #define DEVICE_CLASS_MINOR_IMAGING_DISPLAY (1 << 4) - #define DEVICE_CLASS_MINOR_IMAGING_CAMERA (1 << 5) - #define DEVICE_CLASS_MINOR_IMAGING_SCANNER (1 << 6) - #define DEVICE_CLASS_MINOR_IMAGING_PRINTER (1 << 7) - -#endif +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#ifndef _BLUETOOTH_CLASS_CODES_H_ +#define _BLUETOOTH_CLASS_CODES_H_ + + /* Macros: */ + #define DEVICE_CLASS_SERVICE_POSITIONING (1UL << 16) + #define DEVICE_CLASS_SERVICE_NETWORKING (1UL << 17) + #define DEVICE_CLASS_SERVICE_RENDERING (1UL << 18) + #define DEVICE_CLASS_SERVICE_CAPTURING (1UL << 19) + #define DEVICE_CLASS_SERVICE_OBJECTTRANSFER (1UL << 20) + #define DEVICE_CLASS_SERVICE_AUDIO (1UL << 21) + #define DEVICE_CLASS_SERVICE_TELEPHONY (1UL << 22) + #define DEVICE_CLASS_SERVICE_INFORMATION (1UL << 23) + + #define DEVICE_CLASS_MAJOR_MISC (0x00 << 8) + #define DEVICE_CLASS_MAJOR_COMPUTER (0x01 << 8) + #define DEVICE_CLASS_MAJOR_PHONE (0x02 << 8) + #define DEVICE_CLASS_MAJOR_LAN (0x03 << 8) + #define DEVICE_CLASS_MAJOR_AUDIOVIDEO (0x04 << 8) + #define DEVICE_CLASS_MAJOR_PERIPHERAL (0x05 << 8) + #define DEVICE_CLASS_MAJOR_IMAGING (0x06 << 8) + #define DEVICE_CLASS_MAJOR_UNCLASSIFIED (0x1F << 8) + + #define DEVICE_CLASS_MINOR_COMPUTER_UNCATEGORIZED (0x00 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_DESKTOP (0x01 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_SERVER (0x02 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_LAPTOP (0x03 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_HANDHELD (0x04 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_PALM (0x05 << 2) + #define DEVICE_CLASS_MINOR_COMPUTER_WEARABLE (0x06 << 2) + + #define DEVICE_CLASS_MINOR_PHONE_UNCATEGORIZED (0x00 << 2) + #define DEVICE_CLASS_MINOR_PHONE_CELLULAR (0x01 << 2) + #define DEVICE_CLASS_MINOR_PHONE_CORDLESS (0x02 << 2) + #define DEVICE_CLASS_MINOR_PHONE_SMARTPHONE (0x03 << 2) + #define DEVICE_CLASS_MINOR_PHONE_WIREDMODEM (0x04 << 2) + #define DEVICE_CLASS_MINOR_PHONE_ISDN (0x05 << 2) + + #define DEVICE_CLASS_MINOR_LAN_FULLY_AVAILABLE (0x00 << 5) + #define DEVICE_CLASS_MINOR_LAN_1_TO_17_PC_UTILIZED (0x01 << 5) + #define DEVICE_CLASS_MINOR_LAN_17_TO_33_PC_UTILIZED (0x02 << 5) + #define DEVICE_CLASS_MINOR_LAN_33_TO_50_PC_UTILIZED (0x03 << 5) + #define DEVICE_CLASS_MINOR_LAN_50_TO_67_PC_UTILIZED (0x04 << 5) + #define DEVICE_CLASS_MINOR_LAN_67_TO_83_PC_UTILIZED (0x05 << 5) + #define DEVICE_CLASS_MINOR_LAN_83_TO_99_PC_UTILIZED (0x06 << 5) + #define DEVICE_CLASS_MINOR_NO_SERVICE_AVAILABLE (0x07 << 5) + + #define DEVICE_CLASS_MINOR_AV_UNCATEGORIZED (0x00 << 2) + #define DEVICE_CLASS_MINOR_AV_HEADSET (0x01 << 2) + #define DEVICE_CLASS_MINOR_AV_HANDSFREE (0x02 << 2) + #define DEVICE_CLASS_MINOR_AV_MICROPHONE (0x04 << 2) + #define DEVICE_CLASS_MINOR_AV_LOUDSPEAKER (0x05 << 2) + #define DEVICE_CLASS_MINOR_AV_HEADPHONES (0x06 << 2) + #define DEVICE_CLASS_MINOR_AV_PORTABLE_AUDIO (0x07 << 2) + #define DEVICE_CLASS_MINOR_AV_CARAUDIO (0x08 << 2) + #define DEVICE_CLASS_MINOR_AV_SETTOP_BOX (0x09 << 2) + #define DEVICE_CLASS_MINOR_AV_HIFI (0x0A << 2) + #define DEVICE_CLASS_MINOR_AV_VCR (0x0B << 2) + #define DEVICE_CLASS_MINOR_AV_VIDEO_CAMERA (0x0C << 2) + #define DEVICE_CLASS_MINOR_AV_CAMCORDER (0x0D << 2) + #define DEVICE_CLASS_MINOR_AV_VIDEO_MONITOR (0x0E << 2) + #define DEVICE_CLASS_MINOR_AV_DISPLAY_AND_LOUDSPEAKER (0x0F << 2) + #define DEVICE_CLASS_MINOR_AV_VIDEO_CONFERENCING (0x10 << 2) + #define DEVICE_CLASS_MINOR_AV_GAMING_TOY (0x12 << 2) + + #define DEVICE_CLASS_MINOR_PERIPHERAL_KEYBOARD (0x01 << 6) + #define DEVICE_CLASS_MINOR_PERIPHERAL_POINTING (0x02 << 6) + #define DEVICE_CLASS_MINOR_PERIPHERAL_COMBO (0x03 << 6) + #define DEVICE_CLASS_MINOR_PERIPHERAL_UNCATEGORIZED (0x00 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_JOYSTICK (0x01 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_GAMEPAD (0x02 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_REMOTE_CONTROL (0x03 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_SENSING_DEVICE (0x04 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_DIGITIZER (0x05 << 2) + #define DEVICE_CLASS_MINOR_PERIPHERAL_CARD_READER (0x06 << 2) + + #define DEVICE_CLASS_MINOR_IMAGING_DISPLAY (1 << 4) + #define DEVICE_CLASS_MINOR_IMAGING_CAMERA (1 << 5) + #define DEVICE_CLASS_MINOR_IMAGING_SCANNER (1 << 6) + #define DEVICE_CLASS_MINOR_IMAGING_PRINTER (1 << 7) + +#endif diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c index 740ce6766..f678ecba5 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.c @@ -1,396 +1,396 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -/* - TODO: Add local to remote device connections - */ - -#define INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C -#include "BluetoothHCICommands.h" - -/** Temporary Bluetooth Device Address, for HCI responses which much include the detination address */ -static uint8_t Bluetooth_TempDeviceAddress[6]; - -/** Bluetooth HCI processing task. This task should be called repeatedly the main Bluetooth - * stack task to manage the HCI processing state. - */ -void Bluetooth_HCITask(void) -{ - BT_HCICommand_Header_t HCICommandHeader; - - switch (Bluetooth_State.CurrentHCIState) - { - case Bluetooth_ProcessEvents: - Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE); - Pipe_Unfreeze(); - - if (Pipe_IsReadWriteAllowed()) - { - BT_HCIEvent_Header_t HCIEventHeader; - - /* Read in the event header to fetch the event code and payload length */ - Pipe_Read_Stream_LE(&HCIEventHeader, sizeof(HCIEventHeader)); - - /* Create a temporary buffer for the event parameters */ - uint8_t EventParams[HCIEventHeader.ParameterLength]; - - /* Read in the event parameters into the temporary buffer */ - Pipe_Read_Stream_LE(&EventParams, HCIEventHeader.ParameterLength); - Pipe_ClearIN(); - - BT_HCI_DEBUG(1, "Event Received (0x%02X)", HCIEventHeader.EventCode); - - switch (HCIEventHeader.EventCode) - { - case EVENT_COMMAND_COMPLETE: - BT_HCI_DEBUG(1, "<< Command Complete"); - - /* Check which operation was completed in case we need to process the even parameters */ - switch (((BT_HCIEvent_CommandComplete_t*)&EventParams)->Opcode) - { - case (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR): - /* A READ BDADDR command completed, copy over the local device's BDADDR from the response */ - memcpy(Bluetooth_State.LocalBDADDR, - &((BT_HCIEvent_CommandComplete_t*)&EventParams)->ReturnParams[1], - sizeof(Bluetooth_State.LocalBDADDR)); - break; - } - - Bluetooth_State.CurrentHCIState = Bluetooth_State.NextHCIState; - break; - case EVENT_COMMAND_STATUS: - BT_HCI_DEBUG(1, "<< Command Status"); - BT_HCI_DEBUG(2, "-- Status Code: 0x%02X", (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status)); - - /* If the execution of a command failed, reset the stack */ - if (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status) - Bluetooth_State.CurrentHCIState = Bluetooth_Init; - break; - case EVENT_CONNECTION_REQUEST: - BT_HCI_DEBUG(1, "<< Connection Request"); - BT_HCI_DEBUG(2, "-- Link Type: 0x%02X", (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType)); - - /* Need to store the remote device's BT address in a temporary buffer for later use */ - memcpy(Bluetooth_TempDeviceAddress, - &((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress, - sizeof(Bluetooth_TempDeviceAddress)); - - bool IsACLConnection = (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01); - - /* Only accept the connection if it is a ACL (data) connection, a device is not already connected - and the user application has indicated that the connection should be allowed */ - Bluetooth_State.CurrentHCIState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) || - !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ? - Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection; - - BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State.CurrentHCIState == Bluetooth_Conn_RejectConnection) ? - PSTR("REJECTED") : PSTR("ACCEPTED")); - - break; - case EVENT_PIN_CODE_REQUEST: - BT_HCI_DEBUG(1, "<< Pin Code Request"); - - /* Need to store the remote device's BT address in a temporary buffer for later use */ - memcpy(Bluetooth_TempDeviceAddress, - &((BT_HCIEvent_PinCodeReq_t*)&EventParams)->RemoteAddress, - sizeof(Bluetooth_TempDeviceAddress)); - - Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendPINCode; - break; - case EVENT_LINK_KEY_REQUEST: - BT_HCI_DEBUG(1, "<< Link Key Request"); - - /* Need to store the remote device's BT address in a temporary buffer for later use */ - memcpy(Bluetooth_TempDeviceAddress, - &((BT_HCIEvent_LinkKeyReq_t*)&EventParams)->RemoteAddress, - sizeof(Bluetooth_TempDeviceAddress)); - - Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendLinkKeyNAK; - break; - case EVENT_CONNECTION_COMPLETE: - BT_HCI_DEBUG(1, "<< Connection Complete"); - BT_HCI_DEBUG(2, "-- Handle: 0x%04X", ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle); - - /* Need to store the remote device's BT address in a temporary buffer for later use */ - memcpy(Bluetooth_Connection.RemoteAddress, - &((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->RemoteAddress, - sizeof(Bluetooth_TempDeviceAddress)); - - /* Store the created connection handle and indicate that the connection has been established */ - Bluetooth_Connection.ConnectionHandle = ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle; - Bluetooth_Connection.IsConnected = true; - - Bluetooth_ConnectionComplete(); - break; - case EVENT_DISCONNECTION_COMPLETE: - BT_HCI_DEBUG(1, "<< Disconnection Complete"); - - /* Device disconnected, indicate connection information no longer valid */ - Bluetooth_Connection.IsConnected = false; - - Bluetooth_DisconnectionComplete(); - - Bluetooth_State.CurrentHCIState = Bluetooth_Init; - break; - } - } - - Pipe_Freeze(); - - break; - case Bluetooth_Init: - BT_HCI_DEBUG(1, "# Init"); - - Bluetooth_State.IsInitialized = false; - - /* Reset the connection information structure to destroy any previous connection state */ - memset(&Bluetooth_Connection, 0x00, sizeof(Bluetooth_Connection)); - - Bluetooth_State.CurrentHCIState = Bluetooth_Init_Reset; - break; - case Bluetooth_Init_Reset: - BT_HCI_DEBUG(1, "# Reset"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_RESET), - ParameterLength: 0, - }; - - /* Send the command to reset the bluetooth dongle controller */ - Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); - - Bluetooth_State.NextHCIState = Bluetooth_Init_ReadBufferSize; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_ReadBufferSize: - BT_HCI_DEBUG(1, "# Read Buffer Size"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE), - ParameterLength: 0, - }; - - /* Send the command to read the bluetooth buffer size (mandatory before device sends any data) */ - Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); - - Bluetooth_State.NextHCIState = Bluetooth_Init_GetBDADDR; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_GetBDADDR: - BT_HCI_DEBUG(1, "# Get BDADDR"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR), - ParameterLength: 0, - }; - - /* Send the command to retrieve the BDADDR of the inserted bluetooth dongle */ - Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); - - Bluetooth_State.NextHCIState = Bluetooth_Init_SetLocalName; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_SetLocalName: - BT_HCI_DEBUG(1, "# Set Local Name"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME), - ParameterLength: 248, - }; - - /* Send the command to set the bluetooth dongle's name for other devices to see */ - Bluetooth_SendHCICommand(&HCICommandHeader, Bluetooth_DeviceConfiguration.Name, strlen(Bluetooth_DeviceConfiguration.Name)); - - Bluetooth_State.NextHCIState = Bluetooth_Init_SetDeviceClass; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_SetDeviceClass: - BT_HCI_DEBUG(1, "# Set Device Class"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE), - ParameterLength: 3, - }; - - /* Send the command to set the class of the device for other devices to see */ - Bluetooth_SendHCICommand(&HCICommandHeader, &Bluetooth_DeviceConfiguration.Class, 3); - - Bluetooth_State.NextHCIState = Bluetooth_Init_WriteScanEnable; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_WriteScanEnable: - BT_HCI_DEBUG(1, "# Write Scan Enable"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE), - ParameterLength: 1, - }; - - uint8_t Interval = BT_SCANMODE_InquiryAndPageScans; - - /* Send the command to set the remote device scanning mode */ - Bluetooth_SendHCICommand(&HCICommandHeader, &Interval, 1); - - Bluetooth_State.NextHCIState = Bluetooth_Init_FinalizeInit; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Init_FinalizeInit: - Bluetooth_State.IsInitialized = true; - - /* Fire the user application callback to indicate that the stack is now fully initialized */ - Bluetooth_StackInitialized(); - - Bluetooth_State.NextHCIState = Bluetooth_ProcessEvents; - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Conn_AcceptConnection: - BT_HCI_DEBUG(1, "# Accept Connection"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST), - ParameterLength: sizeof(BT_HCICommand_AcceptConnectionReq_t), - }; - - /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave - connection role */ - BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams; - memcpy(AcceptConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, - sizeof(AcceptConnectionParams.RemoteAddress)); - AcceptConnectionParams.SlaveRole = true; - - /* Send the command to accept the remote connection request */ - Bluetooth_SendHCICommand(&HCICommandHeader, &AcceptConnectionParams, sizeof(BT_HCICommand_AcceptConnectionReq_t)); - - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Conn_RejectConnection: - BT_HCI_DEBUG(1, "# Reject Connection"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST), - ParameterLength: sizeof(BT_HCICommand_RejectConnectionReq_t), - }; - - /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure - to accept the connection due to limited device resources or incorrect device address */ - BT_HCICommand_RejectConnectionReq_t RejectConnectionParams; - memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(RejectConnectionParams.RemoteAddress)); - RejectConnectionParams.Reason = Bluetooth_Connection.IsConnected ? ERROR_LIMITED_RESOURCES : ERROR_UNACCEPTABLE_BDADDR; - - /* Send the command to reject the remote connection request */ - Bluetooth_SendHCICommand(&HCICommandHeader, &RejectConnectionParams, sizeof(BT_HCICommand_RejectConnectionReq_t)); - - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Conn_SendPINCode: - BT_HCI_DEBUG(1, "# Send Pin Code"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY), - ParameterLength: sizeof(BT_HCICommand_PinCodeResp_t), - }; - - /* Copy over the temporary BT device address saved from the PIN Code Request event, copy over the - local PIN authentication code to the response */ - BT_HCICommand_PinCodeResp_t PINCodeRequestParams; - memcpy(PINCodeRequestParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(PINCodeRequestParams.RemoteAddress)); - PINCodeRequestParams.PINCodeLength = strlen(Bluetooth_DeviceConfiguration.PINCode); - memcpy(PINCodeRequestParams.PINCode, Bluetooth_DeviceConfiguration.PINCode, sizeof(PINCodeRequestParams.PINCode)); - - /* Send the command to transmit the device's local PIN number for authentication */ - Bluetooth_SendHCICommand(&HCICommandHeader, &PINCodeRequestParams, sizeof(BT_HCICommand_PinCodeResp_t)); - - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - case Bluetooth_Conn_SendLinkKeyNAK: - BT_HCI_DEBUG(1, "# Send Link Key NAK"); - - HCICommandHeader = (BT_HCICommand_Header_t) - { - OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY), - ParameterLength: sizeof(BT_HCICommand_LinkKeyNAKResp_t), - }; - - /* Copy over the temporary BT device address saved from the Link Key Request event */ - BT_HCICommand_LinkKeyNAKResp_t LinkKeyNAKParams; - memcpy(LinkKeyNAKParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(LinkKeyNAKParams.RemoteAddress)); - - /* Send the command to transmit the link key NAK to the receiver */ - Bluetooth_SendHCICommand(&HCICommandHeader, &LinkKeyNAKParams, sizeof(BT_HCICommand_LinkKeyNAKResp_t)); - - Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; - break; - } -} - -/** Sends a Bluetooth HCI control command to the attached Bluetooth device. - * - * \param[in] HCICommandHeader HCI command header to send to the attached device - * \param[in] Parameters Pointer to the source of the control parameters (if any) - * \param[in] ParameterLength Length of the parameters to send in bytes - * - * \return A value from the USB_Host_SendControlErrorCodes_t enum. - */ -static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t* const HCICommandHeader, const void* Parameters, const uint16_t ParameterLength) -{ - /* Need to reserve the amount of bytes given in the header for the complete payload */ - uint8_t CommandBuffer[sizeof(BT_HCICommand_Header_t) + HCICommandHeader->ParameterLength]; - - USB_ControlRequest = (USB_Request_Header_t) - { - .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_DEVICE), - .bRequest = 0, - .wValue = 0, - .wIndex = 0, - .wLength = sizeof(CommandBuffer) - }; - - /* Copy over the HCI command header to the allocated buffer */ - memcpy(CommandBuffer, HCICommandHeader, sizeof(BT_HCICommand_Header_t)); - - /* Zero out the parameter section of the response so that all padding bytes are known to be zero */ - memset(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], 0x00, HCICommandHeader->ParameterLength); - - /* Copy over the command parameters (if any) to the command buffer - note, the number of actual source parameter bytes - may differ to those in the header; any difference in length is filled with 0x00 padding bytes */ - memcpy(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], Parameters, ParameterLength); - - Pipe_SelectPipe(PIPE_CONTROLPIPE); - return USB_Host_SendControlRequest(CommandBuffer); -} +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +/* + TODO: Add local to remote device connections + */ + +#define INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C +#include "BluetoothHCICommands.h" + +/** Temporary Bluetooth Device Address, for HCI responses which much include the detination address */ +static uint8_t Bluetooth_TempDeviceAddress[6]; + +/** Bluetooth HCI processing task. This task should be called repeatedly the main Bluetooth + * stack task to manage the HCI processing state. + */ +void Bluetooth_HCITask(void) +{ + BT_HCICommand_Header_t HCICommandHeader; + + switch (Bluetooth_State.CurrentHCIState) + { + case Bluetooth_ProcessEvents: + Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE); + Pipe_Unfreeze(); + + if (Pipe_IsReadWriteAllowed()) + { + BT_HCIEvent_Header_t HCIEventHeader; + + /* Read in the event header to fetch the event code and payload length */ + Pipe_Read_Stream_LE(&HCIEventHeader, sizeof(HCIEventHeader)); + + /* Create a temporary buffer for the event parameters */ + uint8_t EventParams[HCIEventHeader.ParameterLength]; + + /* Read in the event parameters into the temporary buffer */ + Pipe_Read_Stream_LE(&EventParams, HCIEventHeader.ParameterLength); + Pipe_ClearIN(); + + BT_HCI_DEBUG(1, "Event Received (0x%02X)", HCIEventHeader.EventCode); + + switch (HCIEventHeader.EventCode) + { + case EVENT_COMMAND_COMPLETE: + BT_HCI_DEBUG(1, "<< Command Complete"); + + /* Check which operation was completed in case we need to process the even parameters */ + switch (((BT_HCIEvent_CommandComplete_t*)&EventParams)->Opcode) + { + case (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR): + /* A READ BDADDR command completed, copy over the local device's BDADDR from the response */ + memcpy(Bluetooth_State.LocalBDADDR, + &((BT_HCIEvent_CommandComplete_t*)&EventParams)->ReturnParams[1], + sizeof(Bluetooth_State.LocalBDADDR)); + break; + } + + Bluetooth_State.CurrentHCIState = Bluetooth_State.NextHCIState; + break; + case EVENT_COMMAND_STATUS: + BT_HCI_DEBUG(1, "<< Command Status"); + BT_HCI_DEBUG(2, "-- Status Code: 0x%02X", (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status)); + + /* If the execution of a command failed, reset the stack */ + if (((BT_HCIEvent_CommandStatus_t*)&EventParams)->Status) + Bluetooth_State.CurrentHCIState = Bluetooth_Init; + break; + case EVENT_CONNECTION_REQUEST: + BT_HCI_DEBUG(1, "<< Connection Request"); + BT_HCI_DEBUG(2, "-- Link Type: 0x%02X", (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType)); + + /* Need to store the remote device's BT address in a temporary buffer for later use */ + memcpy(Bluetooth_TempDeviceAddress, + &((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress, + sizeof(Bluetooth_TempDeviceAddress)); + + bool IsACLConnection = (((BT_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01); + + /* Only accept the connection if it is a ACL (data) connection, a device is not already connected + and the user application has indicated that the connection should be allowed */ + Bluetooth_State.CurrentHCIState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) || + !(Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ? + Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection; + + BT_HCI_DEBUG(2, "-- Connection %S", (Bluetooth_State.CurrentHCIState == Bluetooth_Conn_RejectConnection) ? + PSTR("REJECTED") : PSTR("ACCEPTED")); + + break; + case EVENT_PIN_CODE_REQUEST: + BT_HCI_DEBUG(1, "<< Pin Code Request"); + + /* Need to store the remote device's BT address in a temporary buffer for later use */ + memcpy(Bluetooth_TempDeviceAddress, + &((BT_HCIEvent_PinCodeReq_t*)&EventParams)->RemoteAddress, + sizeof(Bluetooth_TempDeviceAddress)); + + Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendPINCode; + break; + case EVENT_LINK_KEY_REQUEST: + BT_HCI_DEBUG(1, "<< Link Key Request"); + + /* Need to store the remote device's BT address in a temporary buffer for later use */ + memcpy(Bluetooth_TempDeviceAddress, + &((BT_HCIEvent_LinkKeyReq_t*)&EventParams)->RemoteAddress, + sizeof(Bluetooth_TempDeviceAddress)); + + Bluetooth_State.CurrentHCIState = Bluetooth_Conn_SendLinkKeyNAK; + break; + case EVENT_CONNECTION_COMPLETE: + BT_HCI_DEBUG(1, "<< Connection Complete"); + BT_HCI_DEBUG(2, "-- Handle: 0x%04X", ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle); + + /* Need to store the remote device's BT address in a temporary buffer for later use */ + memcpy(Bluetooth_Connection.RemoteAddress, + &((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->RemoteAddress, + sizeof(Bluetooth_TempDeviceAddress)); + + /* Store the created connection handle and indicate that the connection has been established */ + Bluetooth_Connection.ConnectionHandle = ((BT_HCIEvent_ConnectionComplete_t*)&EventParams)->ConnectionHandle; + Bluetooth_Connection.IsConnected = true; + + Bluetooth_ConnectionComplete(); + break; + case EVENT_DISCONNECTION_COMPLETE: + BT_HCI_DEBUG(1, "<< Disconnection Complete"); + + /* Device disconnected, indicate connection information no longer valid */ + Bluetooth_Connection.IsConnected = false; + + Bluetooth_DisconnectionComplete(); + + Bluetooth_State.CurrentHCIState = Bluetooth_Init; + break; + } + } + + Pipe_Freeze(); + + break; + case Bluetooth_Init: + BT_HCI_DEBUG(1, "# Init"); + + Bluetooth_State.IsInitialized = false; + + /* Reset the connection information structure to destroy any previous connection state */ + memset(&Bluetooth_Connection, 0x00, sizeof(Bluetooth_Connection)); + + Bluetooth_State.CurrentHCIState = Bluetooth_Init_Reset; + break; + case Bluetooth_Init_Reset: + BT_HCI_DEBUG(1, "# Reset"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_RESET), + ParameterLength: 0, + }; + + /* Send the command to reset the bluetooth dongle controller */ + Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); + + Bluetooth_State.NextHCIState = Bluetooth_Init_ReadBufferSize; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_ReadBufferSize: + BT_HCI_DEBUG(1, "# Read Buffer Size"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE), + ParameterLength: 0, + }; + + /* Send the command to read the bluetooth buffer size (mandatory before device sends any data) */ + Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); + + Bluetooth_State.NextHCIState = Bluetooth_Init_GetBDADDR; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_GetBDADDR: + BT_HCI_DEBUG(1, "# Get BDADDR"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_INFORMATIONAL | OCF_CTRLR_INFORMATIONAL_READBDADDR), + ParameterLength: 0, + }; + + /* Send the command to retrieve the BDADDR of the inserted bluetooth dongle */ + Bluetooth_SendHCICommand(&HCICommandHeader, NULL, 0); + + Bluetooth_State.NextHCIState = Bluetooth_Init_SetLocalName; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_SetLocalName: + BT_HCI_DEBUG(1, "# Set Local Name"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME), + ParameterLength: 248, + }; + + /* Send the command to set the bluetooth dongle's name for other devices to see */ + Bluetooth_SendHCICommand(&HCICommandHeader, Bluetooth_DeviceConfiguration.Name, strlen(Bluetooth_DeviceConfiguration.Name)); + + Bluetooth_State.NextHCIState = Bluetooth_Init_SetDeviceClass; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_SetDeviceClass: + BT_HCI_DEBUG(1, "# Set Device Class"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE), + ParameterLength: 3, + }; + + /* Send the command to set the class of the device for other devices to see */ + Bluetooth_SendHCICommand(&HCICommandHeader, &Bluetooth_DeviceConfiguration.Class, 3); + + Bluetooth_State.NextHCIState = Bluetooth_Init_WriteScanEnable; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_WriteScanEnable: + BT_HCI_DEBUG(1, "# Write Scan Enable"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_CTRLR_BASEBAND | OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE), + ParameterLength: 1, + }; + + uint8_t Interval = BT_SCANMODE_InquiryAndPageScans; + + /* Send the command to set the remote device scanning mode */ + Bluetooth_SendHCICommand(&HCICommandHeader, &Interval, 1); + + Bluetooth_State.NextHCIState = Bluetooth_Init_FinalizeInit; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Init_FinalizeInit: + Bluetooth_State.IsInitialized = true; + + /* Fire the user application callback to indicate that the stack is now fully initialized */ + Bluetooth_StackInitialized(); + + Bluetooth_State.NextHCIState = Bluetooth_ProcessEvents; + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Conn_AcceptConnection: + BT_HCI_DEBUG(1, "# Accept Connection"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST), + ParameterLength: sizeof(BT_HCICommand_AcceptConnectionReq_t), + }; + + /* Copy over the temporary BT device address saved from the Connection Request event, indicate slave + connection role */ + BT_HCICommand_AcceptConnectionReq_t AcceptConnectionParams; + memcpy(AcceptConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, + sizeof(AcceptConnectionParams.RemoteAddress)); + AcceptConnectionParams.SlaveRole = true; + + /* Send the command to accept the remote connection request */ + Bluetooth_SendHCICommand(&HCICommandHeader, &AcceptConnectionParams, sizeof(BT_HCICommand_AcceptConnectionReq_t)); + + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Conn_RejectConnection: + BT_HCI_DEBUG(1, "# Reject Connection"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST), + ParameterLength: sizeof(BT_HCICommand_RejectConnectionReq_t), + }; + + /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure + to accept the connection due to limited device resources or incorrect device address */ + BT_HCICommand_RejectConnectionReq_t RejectConnectionParams; + memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(RejectConnectionParams.RemoteAddress)); + RejectConnectionParams.Reason = Bluetooth_Connection.IsConnected ? ERROR_LIMITED_RESOURCES : ERROR_UNACCEPTABLE_BDADDR; + + /* Send the command to reject the remote connection request */ + Bluetooth_SendHCICommand(&HCICommandHeader, &RejectConnectionParams, sizeof(BT_HCICommand_RejectConnectionReq_t)); + + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Conn_SendPINCode: + BT_HCI_DEBUG(1, "# Send Pin Code"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY), + ParameterLength: sizeof(BT_HCICommand_PinCodeResp_t), + }; + + /* Copy over the temporary BT device address saved from the PIN Code Request event, copy over the + local PIN authentication code to the response */ + BT_HCICommand_PinCodeResp_t PINCodeRequestParams; + memcpy(PINCodeRequestParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(PINCodeRequestParams.RemoteAddress)); + PINCodeRequestParams.PINCodeLength = strlen(Bluetooth_DeviceConfiguration.PINCode); + memcpy(PINCodeRequestParams.PINCode, Bluetooth_DeviceConfiguration.PINCode, sizeof(PINCodeRequestParams.PINCode)); + + /* Send the command to transmit the device's local PIN number for authentication */ + Bluetooth_SendHCICommand(&HCICommandHeader, &PINCodeRequestParams, sizeof(BT_HCICommand_PinCodeResp_t)); + + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + case Bluetooth_Conn_SendLinkKeyNAK: + BT_HCI_DEBUG(1, "# Send Link Key NAK"); + + HCICommandHeader = (BT_HCICommand_Header_t) + { + OpCode: (OGF_LINK_CONTROL | OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY), + ParameterLength: sizeof(BT_HCICommand_LinkKeyNAKResp_t), + }; + + /* Copy over the temporary BT device address saved from the Link Key Request event */ + BT_HCICommand_LinkKeyNAKResp_t LinkKeyNAKParams; + memcpy(LinkKeyNAKParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(LinkKeyNAKParams.RemoteAddress)); + + /* Send the command to transmit the link key NAK to the receiver */ + Bluetooth_SendHCICommand(&HCICommandHeader, &LinkKeyNAKParams, sizeof(BT_HCICommand_LinkKeyNAKResp_t)); + + Bluetooth_State.CurrentHCIState = Bluetooth_ProcessEvents; + break; + } +} + +/** Sends a Bluetooth HCI control command to the attached Bluetooth device. + * + * \param[in] HCICommandHeader HCI command header to send to the attached device + * \param[in] Parameters Pointer to the source of the control parameters (if any) + * \param[in] ParameterLength Length of the parameters to send in bytes + * + * \return A value from the USB_Host_SendControlErrorCodes_t enum. + */ +static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t* const HCICommandHeader, const void* Parameters, const uint16_t ParameterLength) +{ + /* Need to reserve the amount of bytes given in the header for the complete payload */ + uint8_t CommandBuffer[sizeof(BT_HCICommand_Header_t) + HCICommandHeader->ParameterLength]; + + USB_ControlRequest = (USB_Request_Header_t) + { + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_DEVICE), + .bRequest = 0, + .wValue = 0, + .wIndex = 0, + .wLength = sizeof(CommandBuffer) + }; + + /* Copy over the HCI command header to the allocated buffer */ + memcpy(CommandBuffer, HCICommandHeader, sizeof(BT_HCICommand_Header_t)); + + /* Zero out the parameter section of the response so that all padding bytes are known to be zero */ + memset(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], 0x00, HCICommandHeader->ParameterLength); + + /* Copy over the command parameters (if any) to the command buffer - note, the number of actual source parameter bytes + may differ to those in the header; any difference in length is filled with 0x00 padding bytes */ + memcpy(&CommandBuffer[sizeof(BT_HCICommand_Header_t)], Parameters, ParameterLength); + + Pipe_SelectPipe(PIPE_CONTROLPIPE); + return USB_Host_SendControlRequest(CommandBuffer); +} diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.h index 6682619b3..5f98e34a9 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothHCICommands.h @@ -1,206 +1,206 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#ifndef _BLUETOOTH_HCICOMMANDS_H_ -#define _BLUETOOTH_HCICOMMANDS_H_ - - /* Includes: */ - #include - #include - #include - #include - - #include - #include - - #include "BluetoothStack.h" - #include "BluetoothClassCodes.h" - - /* Macros: */ - #define BT_HCI_DEBUG(l, s, ...) do { if (HCI_DEBUG_LEVEL >= l) printf_P(PSTR("(HCI) " s "\r\n"), ##__VA_ARGS__); } while (0) - #define HCI_DEBUG_LEVEL 0 - - #define OGF_LINK_CONTROL (0x01 << 10) - #define OGF_CTRLR_BASEBAND (0x03 << 10) - #define OGF_CTRLR_INFORMATIONAL (0x04 << 10) - - #define OCF_LINK_CONTROL_INQUIRY 0x0001 - #define OCF_LINK_CONTROL_INQUIRY_CANCEL 0x0002 - #define OCF_LINK_CONTROL_PERIODIC_INQUIRY 0x0003 - #define OCF_LINK_CONTROL_EXIT_PERIODIC_INQUIRY 0x0004 - #define OCF_LINK_CONTROL_CREATE_CONNECTION 0x0005 - #define OCF_LINK_CONTROL_DISCONNECT 0x0006 - #define OCF_LINK_CONTROL_CREATE_CONNECTION_CANCEL 0x0008 - #define OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST 0x0009 - #define OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST 0x000A - #define OCF_LINK_CONTROL_LINK_KEY_REQUEST_REPLY 0x000B - #define OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY 0x000C - #define OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY 0x000D - #define OCF_LINK_CONTROL_PIN_CODE_REQUEST_NEG_REPLY 0x000E - #define OCF_LINK_CONTROL_CHANGE_CONNECTION_PACKET_TYPE 0x000F - #define OCF_LINK_CONTROL_REMOTE_NAME_REQUEST 0x0019 - #define OCF_CTRLR_BASEBAND_SET_EVENT_MASK 0x0001 - #define OCF_CTRLR_BASEBAND_RESET 0x0003 - #define OCF_CTRLR_BASEBAND_WRITE_PIN_TYPE 0x000A - #define OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME 0x0013 - #define OCF_CTRLR_BASEBAND_READ_LOCAL_NAME 0x0014 - #define OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE 0x001A - #define OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE 0x0024 - #define OCF_CTRLR_BASEBAND_WRITE_SIMPLE_PAIRING_MODE 0x0056 - #define OCF_CTRLR_BASEBAND_WRITE_AUTHENTICATION_ENABLE 0x0020 - #define OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE 0x0005 - #define OCF_CTRLR_INFORMATIONAL_READBDADDR 0x0009 - - #define EVENT_COMMAND_STATUS 0x0F - #define EVENT_COMMAND_COMPLETE 0x0E - #define EVENT_CONNECTION_COMPLETE 0x03 - #define EVENT_CONNECTION_REQUEST 0x04 - #define EVENT_DISCONNECTION_COMPLETE 0x05 - #define EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 - #define EVENT_PIN_CODE_REQUEST 0x16 - #define EVENT_LINK_KEY_REQUEST 0x17 - - #define ERROR_LIMITED_RESOURCES 0x0D - #define ERROR_UNACCEPTABLE_BDADDR 0x0F - - /* Type Defines: */ - typedef struct - { - uint16_t OpCode; - uint8_t ParameterLength; - uint8_t Parameters[]; - } BT_HCICommand_Header_t; - - typedef struct - { - uint8_t EventCode; - uint8_t ParameterLength; - } BT_HCIEvent_Header_t; - - typedef struct - { - uint8_t Status; - uint8_t Packets; - uint16_t OpCode; - } BT_HCIEvent_CommandStatus_t; - - typedef struct - { - uint8_t HCIPacketsAllowable; - uint16_t Opcode; - uint8_t ReturnParams[]; - } BT_HCIEvent_CommandComplete_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - uint8_t ClassOfDevice_Service; - uint16_t ClassOfDevice_MajorMinor; - uint8_t LinkType; - } BT_HCIEvent_ConnectionRequest_t; - - typedef struct - { - uint8_t Status; - uint16_t ConnectionHandle; - uint8_t RemoteAddress[6]; - uint8_t LinkType; - uint8_t EncryptionEnabled; - } BT_HCIEvent_ConnectionComplete_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - } BT_HCIEvent_PinCodeReq_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - } BT_HCIEvent_LinkKeyReq_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - } BT_HCICommand_LinkKeyNAKResp_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - uint8_t PINCodeLength; - char PINCode[16]; - } BT_HCICommand_PinCodeResp_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - uint8_t SlaveRole; - } BT_HCICommand_AcceptConnectionReq_t; - - typedef struct - { - uint8_t RemoteAddress[6]; - uint8_t Reason; - } BT_HCICommand_RejectConnectionReq_t; - - /* Enums: */ - enum BT_ScanEnable_Modes_t - { - BT_SCANMODE_NoScansEnabled = 0, - BT_SCANMODE_InquiryScanOnly = 1, - BT_SCANMODE_PageScanOnly = 2, - BT_SCANMODE_InquiryAndPageScans = 3, - }; - - enum BT_HCIStates_t - { - Bluetooth_ProcessEvents = 0, - Bluetooth_Init = 1, - Bluetooth_Init_Reset = 2, - Bluetooth_Init_ReadBufferSize = 3, - Bluetooth_Init_GetBDADDR = 4, - Bluetooth_Init_SetLocalName = 5, - Bluetooth_Init_SetDeviceClass = 6, - Bluetooth_Init_WriteScanEnable = 7, - Bluetooth_Init_FinalizeInit = 8, - Bluetooth_Conn_AcceptConnection = 9, - Bluetooth_Conn_RejectConnection = 10, - Bluetooth_Conn_SendPINCode = 11, - Bluetooth_Conn_SendLinkKeyNAK = 12, - }; - - /* Function Prototypes: */ - void Bluetooth_HCITask(void); - - #if defined(INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C) - static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t* const HCICommandHeader, const void* Parameters, - const uint16_t ParameterLength); - #endif - -#endif +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#ifndef _BLUETOOTH_HCICOMMANDS_H_ +#define _BLUETOOTH_HCICOMMANDS_H_ + + /* Includes: */ + #include + #include + #include + #include + + #include + #include + + #include "BluetoothStack.h" + #include "BluetoothClassCodes.h" + + /* Macros: */ + #define BT_HCI_DEBUG(l, s, ...) do { if (HCI_DEBUG_LEVEL >= l) printf_P(PSTR("(HCI) " s "\r\n"), ##__VA_ARGS__); } while (0) + #define HCI_DEBUG_LEVEL 0 + + #define OGF_LINK_CONTROL (0x01 << 10) + #define OGF_CTRLR_BASEBAND (0x03 << 10) + #define OGF_CTRLR_INFORMATIONAL (0x04 << 10) + + #define OCF_LINK_CONTROL_INQUIRY 0x0001 + #define OCF_LINK_CONTROL_INQUIRY_CANCEL 0x0002 + #define OCF_LINK_CONTROL_PERIODIC_INQUIRY 0x0003 + #define OCF_LINK_CONTROL_EXIT_PERIODIC_INQUIRY 0x0004 + #define OCF_LINK_CONTROL_CREATE_CONNECTION 0x0005 + #define OCF_LINK_CONTROL_DISCONNECT 0x0006 + #define OCF_LINK_CONTROL_CREATE_CONNECTION_CANCEL 0x0008 + #define OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST 0x0009 + #define OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST 0x000A + #define OCF_LINK_CONTROL_LINK_KEY_REQUEST_REPLY 0x000B + #define OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY 0x000C + #define OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY 0x000D + #define OCF_LINK_CONTROL_PIN_CODE_REQUEST_NEG_REPLY 0x000E + #define OCF_LINK_CONTROL_CHANGE_CONNECTION_PACKET_TYPE 0x000F + #define OCF_LINK_CONTROL_REMOTE_NAME_REQUEST 0x0019 + #define OCF_CTRLR_BASEBAND_SET_EVENT_MASK 0x0001 + #define OCF_CTRLR_BASEBAND_RESET 0x0003 + #define OCF_CTRLR_BASEBAND_WRITE_PIN_TYPE 0x000A + #define OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME 0x0013 + #define OCF_CTRLR_BASEBAND_READ_LOCAL_NAME 0x0014 + #define OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE 0x001A + #define OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE 0x0024 + #define OCF_CTRLR_BASEBAND_WRITE_SIMPLE_PAIRING_MODE 0x0056 + #define OCF_CTRLR_BASEBAND_WRITE_AUTHENTICATION_ENABLE 0x0020 + #define OCF_CTRLR_INFORMATIONAL_READBUFFERSIZE 0x0005 + #define OCF_CTRLR_INFORMATIONAL_READBDADDR 0x0009 + + #define EVENT_COMMAND_STATUS 0x0F + #define EVENT_COMMAND_COMPLETE 0x0E + #define EVENT_CONNECTION_COMPLETE 0x03 + #define EVENT_CONNECTION_REQUEST 0x04 + #define EVENT_DISCONNECTION_COMPLETE 0x05 + #define EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07 + #define EVENT_PIN_CODE_REQUEST 0x16 + #define EVENT_LINK_KEY_REQUEST 0x17 + + #define ERROR_LIMITED_RESOURCES 0x0D + #define ERROR_UNACCEPTABLE_BDADDR 0x0F + + /* Type Defines: */ + typedef struct + { + uint16_t OpCode; + uint8_t ParameterLength; + uint8_t Parameters[]; + } BT_HCICommand_Header_t; + + typedef struct + { + uint8_t EventCode; + uint8_t ParameterLength; + } BT_HCIEvent_Header_t; + + typedef struct + { + uint8_t Status; + uint8_t Packets; + uint16_t OpCode; + } BT_HCIEvent_CommandStatus_t; + + typedef struct + { + uint8_t HCIPacketsAllowable; + uint16_t Opcode; + uint8_t ReturnParams[]; + } BT_HCIEvent_CommandComplete_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + uint8_t ClassOfDevice_Service; + uint16_t ClassOfDevice_MajorMinor; + uint8_t LinkType; + } BT_HCIEvent_ConnectionRequest_t; + + typedef struct + { + uint8_t Status; + uint16_t ConnectionHandle; + uint8_t RemoteAddress[6]; + uint8_t LinkType; + uint8_t EncryptionEnabled; + } BT_HCIEvent_ConnectionComplete_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + } BT_HCIEvent_PinCodeReq_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + } BT_HCIEvent_LinkKeyReq_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + } BT_HCICommand_LinkKeyNAKResp_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + uint8_t PINCodeLength; + char PINCode[16]; + } BT_HCICommand_PinCodeResp_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + uint8_t SlaveRole; + } BT_HCICommand_AcceptConnectionReq_t; + + typedef struct + { + uint8_t RemoteAddress[6]; + uint8_t Reason; + } BT_HCICommand_RejectConnectionReq_t; + + /* Enums: */ + enum BT_ScanEnable_Modes_t + { + BT_SCANMODE_NoScansEnabled = 0, + BT_SCANMODE_InquiryScanOnly = 1, + BT_SCANMODE_PageScanOnly = 2, + BT_SCANMODE_InquiryAndPageScans = 3, + }; + + enum BT_HCIStates_t + { + Bluetooth_ProcessEvents = 0, + Bluetooth_Init = 1, + Bluetooth_Init_Reset = 2, + Bluetooth_Init_ReadBufferSize = 3, + Bluetooth_Init_GetBDADDR = 4, + Bluetooth_Init_SetLocalName = 5, + Bluetooth_Init_SetDeviceClass = 6, + Bluetooth_Init_WriteScanEnable = 7, + Bluetooth_Init_FinalizeInit = 8, + Bluetooth_Conn_AcceptConnection = 9, + Bluetooth_Conn_RejectConnection = 10, + Bluetooth_Conn_SendPINCode = 11, + Bluetooth_Conn_SendLinkKeyNAK = 12, + }; + + /* Function Prototypes: */ + void Bluetooth_HCITask(void); + + #if defined(INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C) + static uint8_t Bluetooth_SendHCICommand(const BT_HCICommand_Header_t* const HCICommandHeader, const void* Parameters, + const uint16_t ParameterLength); + #endif + +#endif diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c index 6b434ba43..542a783c9 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c @@ -1,99 +1,99 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#include "BluetoothStack.h" - -/** Bluetooth device connection information structure. Once connected to a remote device, this structure tracks the - * connection state of the individual L2CAP channels. - */ -Bluetooth_Connection_t Bluetooth_Connection = { IsConnected: false }; - -/** Bluetooth device state information structure. This structure contains details on the current Bluetooth stack - * state. - */ -Bluetooth_Stack_State_t Bluetooth_State = { IsInitialized: false }; - -/** Bluetooth stack initialization function. This function must be called once to initialize the Bluetooth stack, - * ready for connection to remote devices. - * - * \note This function only begins the initialization process; the stack is initialized as the main Bluetooth stack - * management task is repeatedly called. The initialization process ends when the IsInitialized element of the - * \ref Bluetooth_State structure becomes true and the \ref Bluetooth_StackInitialized() callback fires. - */ -void Bluetooth_Stack_Init(void) -{ - /* Reset the HCI state machine - this will eventually reset the adapter and stack when the Bluetooth stack task is called */ - Bluetooth_State.CurrentHCIState = Bluetooth_Init; - Bluetooth_State.NextHCIState = Bluetooth_Init; -} - -/** Bluetooth stack management task. This task must be repeatedly called to maintain the Bluetooth stack and any connection - * to remote Bluetooth devices, including both the HCI control layer and the ACL channel layer. - */ -void Bluetooth_Stack_USBTask(void) -{ - Bluetooth_HCITask(); - Bluetooth_ACLTask(); -} - -/** Retrieves the channel information structure with the given local or remote channel number from the channel list. - * - * \param[in] SearchValue Value to search for in the channel structure list - * \param[in] SearchKey Key to search within the channel structure, a CHANNEL_SEARCH_* mask - * - * \return Pointer to the matching channel information structure in the channel table if found, NULL otherwise - */ -Bluetooth_Channel_t* Bluetooth_GetChannelData(const uint16_t SearchValue, const uint8_t SearchKey) -{ - for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) - { - Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i]; - - bool FoundMatch = false; - - switch (SearchKey) - { - case CHANNEL_SEARCH_LOCALNUMBER: - FoundMatch = (SearchValue == ChannelData->LocalNumber); - break; - case CHANNEL_SEARCH_REMOTENUMBER: - FoundMatch = (SearchValue == ChannelData->RemoteNumber); - break; - case CHANNEL_SEARCH_PSM: - FoundMatch = (SearchValue == ChannelData->PSM); - break; - } - - if (FoundMatch) - return ChannelData; - } - - return NULL; -} +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#include "BluetoothStack.h" + +/** Bluetooth device connection information structure. Once connected to a remote device, this structure tracks the + * connection state of the individual L2CAP channels. + */ +Bluetooth_Connection_t Bluetooth_Connection = { IsConnected: false }; + +/** Bluetooth device state information structure. This structure contains details on the current Bluetooth stack + * state. + */ +Bluetooth_Stack_State_t Bluetooth_State = { IsInitialized: false }; + +/** Bluetooth stack initialization function. This function must be called once to initialize the Bluetooth stack, + * ready for connection to remote devices. + * + * \note This function only begins the initialization process; the stack is initialized as the main Bluetooth stack + * management task is repeatedly called. The initialization process ends when the IsInitialized element of the + * \ref Bluetooth_State structure becomes true and the \ref Bluetooth_StackInitialized() callback fires. + */ +void Bluetooth_Stack_Init(void) +{ + /* Reset the HCI state machine - this will eventually reset the adapter and stack when the Bluetooth stack task is called */ + Bluetooth_State.CurrentHCIState = Bluetooth_Init; + Bluetooth_State.NextHCIState = Bluetooth_Init; +} + +/** Bluetooth stack management task. This task must be repeatedly called to maintain the Bluetooth stack and any connection + * to remote Bluetooth devices, including both the HCI control layer and the ACL channel layer. + */ +void Bluetooth_Stack_USBTask(void) +{ + Bluetooth_HCITask(); + Bluetooth_ACLTask(); +} + +/** Retrieves the channel information structure with the given local or remote channel number from the channel list. + * + * \param[in] SearchValue Value to search for in the channel structure list + * \param[in] SearchKey Key to search within the channel structure, a CHANNEL_SEARCH_* mask + * + * \return Pointer to the matching channel information structure in the channel table if found, NULL otherwise + */ +Bluetooth_Channel_t* Bluetooth_GetChannelData(const uint16_t SearchValue, const uint8_t SearchKey) +{ + for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++) + { + Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i]; + + bool FoundMatch = false; + + switch (SearchKey) + { + case CHANNEL_SEARCH_LOCALNUMBER: + FoundMatch = (SearchValue == ChannelData->LocalNumber); + break; + case CHANNEL_SEARCH_REMOTENUMBER: + FoundMatch = (SearchValue == ChannelData->RemoteNumber); + break; + case CHANNEL_SEARCH_PSM: + FoundMatch = (SearchValue == ChannelData->PSM); + break; + } + + if (FoundMatch) + return ChannelData; + } + + return NULL; +} diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h index aebdd96e2..ff1dec749 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h @@ -1,161 +1,161 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#ifndef _BLUETOOTH_STACK_ -#define _BLUETOOTH_STACK_ - - /* Includes: */ - #include - - /* Macros: */ - #define BLUETOOTH_DATA_IN_PIPE 1 - #define BLUETOOTH_DATA_OUT_PIPE 2 - #define BLUETOOTH_EVENTS_PIPE 3 - - #define BLUETOOTH_MAX_OPEN_CHANNELS 6 - - #define CHANNEL_PSM_SDP 0x0001 - #define CHANNEL_PSM_UDP 0x0002 - #define CHANNEL_PSM_RFCOMM 0x0003 - #define CHANNEL_PSM_TCP 0x0004 - #define CHANNEL_PSM_IP 0x0009 - #define CHANNEL_PSM_FTP 0x000A - #define CHANNEL_PSM_HTTP 0x000C - #define CHANNEL_PSM_UPNP 0x0010 - #define CHANNEL_PSM_HIDP 0x0011 - - #define CHANNEL_SEARCH_LOCALNUMBER 0 - #define CHANNEL_SEARCH_REMOTENUMBER 1 - #define CHANNEL_SEARCH_PSM 2 - - #define MAXIMUM_CHANNEL_MTU 255 - - /* Enums: */ - /** Enum for the possible states for a bluetooth ACL channel. */ - enum BT_ChannelStates_t - { - Channel_Closed = 0, /**< Channel is closed and inactive. No data may be sent or received. */ - Channel_WaitConnect = 1, /**< A connection request has been received, but a response has not been sent. */ - Channel_WaitConnectRsp = 2, /**< A connection request has been sent, but a response has not been received. */ - Channel_Config_WaitConfig = 3, /**< Channel has been connected, but not yet configured on either end. */ - Channel_Config_WaitSendConfig = 4, /**< Channel configuration has been received and accepted, but not yet sent. */ - Channel_Config_WaitReqResp = 5, /**< Channel configuration has been sent but not responded to, and a configuration - request from the remote end has not yet been received. */ - Channel_Config_WaitResp = 6, /**< Channel configuration has been sent but not accepted, but a configuration request - from the remote end has been accepted. */ - Channel_Config_WaitReq = 7, /**< Channel configuration has been sent and accepted, but a configuration request - from the remote end has not yet been accepted. */ - Channel_Open = 8, /**< Channel is open and ready to send or receive data */ - Channel_WaitDisconnect = 9, /**< A disconnection request has been sent, but not yet acknowledged. */ - }; - - /** Enum for the possible error codes returned by the \ref Bluetooth_SendPacket() function. */ - enum BT_SendPacket_ErrorCodes_t - { - BT_SENDPACKET_NoError = 0, /**< The packet was sent sucessfully. */ - BT_SENDPACKET_NotConnected = 1, /**< The bluetooth stack is not currently connected to a remote device. */ - BT_SENDPACKET_ChannelNotOpen = 2, /**< The given channel is not currently in the Open state. */ - }; - - /* Type Defines: */ - /** Type define for a Bluetooth ACL channel information structure. This structure contains all the relevent - * information on an ACL channel for data transmission and reception by the stack. - */ - typedef struct - { - uint8_t State; - uint16_t LocalNumber; - uint16_t RemoteNumber; - uint16_t PSM; - uint16_t LocalMTU; - uint16_t RemoteMTU; - } Bluetooth_Channel_t; - - /** Type define for a Bluetooth device connection information structure. This structure contains all the - * information needed to maintain a connection to a remote Bluetooth device via the Bluetooth stack. - */ - typedef struct - { - bool IsConnected; /**< Indicates if the stack is currently connected to a remote device - if this value is - * false, the remaining elements are invalid. - */ - uint16_t ConnectionHandle; /**< Connection handle to the remote device, used internally in the stack. */ - uint8_t RemoteAddress[6]; /**< Bluetooth device address of the attached remote device. */ - Bluetooth_Channel_t Channels[BLUETOOTH_MAX_OPEN_CHANNELS]; /**< Channel information structures for the connection. */ - uint8_t SignallingIdentifier; /**< Next Signalling Channel unique command sequence identifier. */ - } Bluetooth_Connection_t; - - /** Local Bluetooth device information structure, for the defining of local device characteristics for the Bluetooth stack. */ - typedef struct - { - uint32_t Class; /**< Class of the local device, a mask of DEVICE_CLASS_* masks. */ - char PINCode[16]; /**< Pin code required to send or receive in order to authenticate with a remote device. */ - char Name[]; /**< Name of the local bluetooth device, up to 248 characters. */ - } Bluetooth_Device_t; - - /** Bluetooth stack state information structure, for the containment of the Bluetooth stack state. The values in - * this structure are set by the Bluetooth stack internally, and should all be treated as read only by the user - * application. - */ - typedef struct - { - uint8_t CurrentHCIState; /**< Current HCI state machine state. */ - uint8_t NextHCIState; /**< Next HCI state machine state to progress to once the currently issued command completes. */ - bool IsInitialized; /**< Indicates if the Bluetooth stack is currently initialized and ready for connections - * to or from a remote Bluetooth device. - */ - uint8_t LocalBDADDR[6]; /**< Local bluetooth adapter's BDADDR, valid when the stack is fully initialized. */ - } Bluetooth_Stack_State_t; - - /* Includes: */ - #include "BluetoothHCICommands.h" - #include "BluetoothACLPackets.h" - - /* Function Prototypes: */ - void Bluetooth_Stack_Init(void); - void Bluetooth_Stack_USBTask(void); - - void Bluetooth_StackInitialized(void); - bool Bluetooth_ConnectionRequest(const uint8_t* RemoteAddress); - void Bluetooth_ConnectionComplete(void); - void Bluetooth_DisconnectionComplete(void); - bool Bluetooth_ChannelConnectionRequest(const uint16_t PSM); - void Bluetooth_PacketReceived(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const Channel); - Bluetooth_Channel_t* Bluetooth_GetChannelData(const uint16_t SearchValue, const uint8_t SearchKey); - Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM); - void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel); - uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const Channel); - - /* External Variables: */ - extern Bluetooth_Device_t Bluetooth_DeviceConfiguration; - extern Bluetooth_Connection_t Bluetooth_Connection; - extern Bluetooth_Stack_State_t Bluetooth_State; - -#endif +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#ifndef _BLUETOOTH_STACK_ +#define _BLUETOOTH_STACK_ + + /* Includes: */ + #include + + /* Macros: */ + #define BLUETOOTH_DATA_IN_PIPE 1 + #define BLUETOOTH_DATA_OUT_PIPE 2 + #define BLUETOOTH_EVENTS_PIPE 3 + + #define BLUETOOTH_MAX_OPEN_CHANNELS 6 + + #define CHANNEL_PSM_SDP 0x0001 + #define CHANNEL_PSM_UDP 0x0002 + #define CHANNEL_PSM_RFCOMM 0x0003 + #define CHANNEL_PSM_TCP 0x0004 + #define CHANNEL_PSM_IP 0x0009 + #define CHANNEL_PSM_FTP 0x000A + #define CHANNEL_PSM_HTTP 0x000C + #define CHANNEL_PSM_UPNP 0x0010 + #define CHANNEL_PSM_HIDP 0x0011 + + #define CHANNEL_SEARCH_LOCALNUMBER 0 + #define CHANNEL_SEARCH_REMOTENUMBER 1 + #define CHANNEL_SEARCH_PSM 2 + + #define MAXIMUM_CHANNEL_MTU 255 + + /* Enums: */ + /** Enum for the possible states for a bluetooth ACL channel. */ + enum BT_ChannelStates_t + { + Channel_Closed = 0, /**< Channel is closed and inactive. No data may be sent or received. */ + Channel_WaitConnect = 1, /**< A connection request has been received, but a response has not been sent. */ + Channel_WaitConnectRsp = 2, /**< A connection request has been sent, but a response has not been received. */ + Channel_Config_WaitConfig = 3, /**< Channel has been connected, but not yet configured on either end. */ + Channel_Config_WaitSendConfig = 4, /**< Channel configuration has been received and accepted, but not yet sent. */ + Channel_Config_WaitReqResp = 5, /**< Channel configuration has been sent but not responded to, and a configuration + request from the remote end has not yet been received. */ + Channel_Config_WaitResp = 6, /**< Channel configuration has been sent but not accepted, but a configuration request + from the remote end has been accepted. */ + Channel_Config_WaitReq = 7, /**< Channel configuration has been sent and accepted, but a configuration request + from the remote end has not yet been accepted. */ + Channel_Open = 8, /**< Channel is open and ready to send or receive data */ + Channel_WaitDisconnect = 9, /**< A disconnection request has been sent, but not yet acknowledged. */ + }; + + /** Enum for the possible error codes returned by the \ref Bluetooth_SendPacket() function. */ + enum BT_SendPacket_ErrorCodes_t + { + BT_SENDPACKET_NoError = 0, /**< The packet was sent sucessfully. */ + BT_SENDPACKET_NotConnected = 1, /**< The bluetooth stack is not currently connected to a remote device. */ + BT_SENDPACKET_ChannelNotOpen = 2, /**< The given channel is not currently in the Open state. */ + }; + + /* Type Defines: */ + /** Type define for a Bluetooth ACL channel information structure. This structure contains all the relevent + * information on an ACL channel for data transmission and reception by the stack. + */ + typedef struct + { + uint8_t State; + uint16_t LocalNumber; + uint16_t RemoteNumber; + uint16_t PSM; + uint16_t LocalMTU; + uint16_t RemoteMTU; + } Bluetooth_Channel_t; + + /** Type define for a Bluetooth device connection information structure. This structure contains all the + * information needed to maintain a connection to a remote Bluetooth device via the Bluetooth stack. + */ + typedef struct + { + bool IsConnected; /**< Indicates if the stack is currently connected to a remote device - if this value is + * false, the remaining elements are invalid. + */ + uint16_t ConnectionHandle; /**< Connection handle to the remote device, used internally in the stack. */ + uint8_t RemoteAddress[6]; /**< Bluetooth device address of the attached remote device. */ + Bluetooth_Channel_t Channels[BLUETOOTH_MAX_OPEN_CHANNELS]; /**< Channel information structures for the connection. */ + uint8_t SignallingIdentifier; /**< Next Signalling Channel unique command sequence identifier. */ + } Bluetooth_Connection_t; + + /** Local Bluetooth device information structure, for the defining of local device characteristics for the Bluetooth stack. */ + typedef struct + { + uint32_t Class; /**< Class of the local device, a mask of DEVICE_CLASS_* masks. */ + char PINCode[16]; /**< Pin code required to send or receive in order to authenticate with a remote device. */ + char Name[]; /**< Name of the local bluetooth device, up to 248 characters. */ + } Bluetooth_Device_t; + + /** Bluetooth stack state information structure, for the containment of the Bluetooth stack state. The values in + * this structure are set by the Bluetooth stack internally, and should all be treated as read only by the user + * application. + */ + typedef struct + { + uint8_t CurrentHCIState; /**< Current HCI state machine state. */ + uint8_t NextHCIState; /**< Next HCI state machine state to progress to once the currently issued command completes. */ + bool IsInitialized; /**< Indicates if the Bluetooth stack is currently initialized and ready for connections + * to or from a remote Bluetooth device. + */ + uint8_t LocalBDADDR[6]; /**< Local bluetooth adapter's BDADDR, valid when the stack is fully initialized. */ + } Bluetooth_Stack_State_t; + + /* Includes: */ + #include "BluetoothHCICommands.h" + #include "BluetoothACLPackets.h" + + /* Function Prototypes: */ + void Bluetooth_Stack_Init(void); + void Bluetooth_Stack_USBTask(void); + + void Bluetooth_StackInitialized(void); + bool Bluetooth_ConnectionRequest(const uint8_t* RemoteAddress); + void Bluetooth_ConnectionComplete(void); + void Bluetooth_DisconnectionComplete(void); + bool Bluetooth_ChannelConnectionRequest(const uint16_t PSM); + void Bluetooth_PacketReceived(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const Channel); + Bluetooth_Channel_t* Bluetooth_GetChannelData(const uint16_t SearchValue, const uint8_t SearchKey); + Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM); + void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel); + uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t* const Channel); + + /* External Variables: */ + extern Bluetooth_Device_t Bluetooth_DeviceConfiguration; + extern Bluetooth_Connection_t Bluetooth_Connection; + extern Bluetooth_Stack_State_t Bluetooth_State; + +#endif diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c index 6dd3796f4..f79bb4239 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.c @@ -1,195 +1,195 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#define INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C -#include "ServiceDiscoveryProtocol.h" - -SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Name, "SDP"); -SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Description, "BT Service Discovery"); -SERVICE_ATTRIBUTE_8BIT_LEN(SDP_Attribute_Availability, SDP_DATATYPE_UNSIGNED_INT, 1, {0xFF}); -const ServiceAttributeTable_t SDP_Attribute_Table[] PROGMEM = - { - {.AttributeID = SDP_ATTRIBUTE_NAME , .AttributeData = &SDP_Attribute_Name}, - {.AttributeID = SDP_ATTRIBUTE_DESCRIPTION , .AttributeData = &SDP_Attribute_Description}, - {.AttributeID = SDP_ATTRIBUTE_AVAILABILITY, .AttributeData = &SDP_Attribute_Availability}, - SERVICE_ATTRIBUTE_TABLE_TERMINATOR - }; - -SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Name, "RFCOMM"); -SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Description, "Virtual Serial"); -SERVICE_ATTRIBUTE_8BIT_LEN(RFCOMM_Attribute_Availability, SDP_DATATYPE_UNSIGNED_INT, 1, {0xFF}); -const ServiceAttributeTable_t RFCOMM_Attribute_Table[] PROGMEM = - { - {.AttributeID = SDP_ATTRIBUTE_NAME , .AttributeData = &RFCOMM_Attribute_Name}, - {.AttributeID = SDP_ATTRIBUTE_DESCRIPTION , .AttributeData = &RFCOMM_Attribute_Description}, - {.AttributeID = SDP_ATTRIBUTE_AVAILABILITY, .AttributeData = &RFCOMM_Attribute_Availability}, - SERVICE_ATTRIBUTE_TABLE_TERMINATOR - }; - -const ServiceTable_t SDP_Services_Table[] = - { - { // 128-bit UUID for the SDP service - .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00}, - .AttributeTable = &SDP_Attribute_Table, - }, - { // 128-bit UUID for the RFCOMM service - .UUID = {BASE_96BIT_UUID, 0x03, 0x00, 0x00, 0x00}, - .AttributeTable = &RFCOMM_Attribute_Table, - }, - }; - - -void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel) -{ - SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data; - SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength); - - BT_SDP_DEBUG(1, "SDP Packet Received"); - BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU); - BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength); - - switch (SDPHeader->PDU) - { - case SDP_PDU_SERVICESEARCHREQUEST: - ServiceDiscovery_ProcessServiceSearch(SDPHeader); - break; - case SDP_PDU_SERVICEATTRIBUTEREQUEST: - ServiceDiscovery_ProcessServiceAttribute(SDPHeader); - break; - case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST: - ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader); - break; - } -} - -static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader) -{ - BT_SDP_DEBUG(1, "<< Service Search"); -} - -static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader) -{ - BT_SDP_DEBUG(1, "<< Service Attribute"); -} - -static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader) -{ - void* CurrentParameter = ((void*)SDPHeader + sizeof(SDP_PDUHeader_t)); - - BT_SDP_DEBUG(1, "<< Service Search Attribute"); - - uint8_t ElementHeaderSize; - - uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); - BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength); - while (ServicePatternLength) - { - uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); - uint8_t UUID[16] = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x00}; - - if (UUIDLength <= 32) - memcpy(&UUID[sizeof(UUID) - sizeof(uint32_t)], CurrentParameter, UUIDLength); - else - memcpy(UUID, CurrentParameter, UUIDLength); - - CurrentParameter += UUIDLength; - - BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", - UUIDLength, - UUID[15], UUID[14], UUID[13], UUID[12], UUID[11], UUID[10], UUID[9], UUID[8], - UUID[7], UUID[6], UUID[5], UUID[4], UUID[3], UUID[2], UUID[1], UUID[0]); - - ServicePatternLength -= (UUIDLength + ElementHeaderSize); - } - - uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter); - BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize); - - uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); - BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength); - while (AttributeIDListLength) - { - uint8_t AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); - uint32_t Attribute = 0; - - memcpy(&Attribute, CurrentParameter, AttributeLength); - CurrentParameter += AttributeLength; - - BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength, Attribute); - - AttributeIDListLength -= (AttributeLength + ElementHeaderSize); - } -} - -static uint32_t ServiceDiscovery_GetDataElementSize(void** DataElementHeader, uint8_t* ElementHeaderSize) -{ - uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07); - *DataElementHeader += sizeof(uint8_t); - - *ElementHeaderSize = 1; - - uint32_t ElementValue; - - switch (SizeIndex) - { - case 0: - ElementValue = 1; - break; - case 1: - ElementValue = 2; - break; - case 2: - ElementValue = 4; - break; - case 3: - ElementValue = 8; - break; - case 4: - ElementValue = 16; - break; - case 5: - ElementValue = *((uint8_t*)*DataElementHeader); - *DataElementHeader += sizeof(uint8_t); - *ElementHeaderSize = (1 + sizeof(uint8_t)); - break; - case 6: - ElementValue = *((uint16_t*)*DataElementHeader); - *DataElementHeader += sizeof(uint16_t); - *ElementHeaderSize = (1 + sizeof(uint16_t)); - break; - default: - ElementValue = *((uint32_t*)*DataElementHeader); - *DataElementHeader += sizeof(uint32_t); - *ElementHeaderSize = (1 + sizeof(uint32_t)); - break; - } - - return ElementValue; -} +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#define INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C +#include "ServiceDiscoveryProtocol.h" + +SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Name, "SDP"); +SERVICE_ATTRIBUTE_TEXT(SDP_Attribute_Description, "BT Service Discovery"); +SERVICE_ATTRIBUTE_8BIT_LEN(SDP_Attribute_Availability, SDP_DATATYPE_UNSIGNED_INT, 1, {0xFF}); +const ServiceAttributeTable_t SDP_Attribute_Table[] PROGMEM = + { + {.AttributeID = SDP_ATTRIBUTE_NAME , .AttributeData = &SDP_Attribute_Name}, + {.AttributeID = SDP_ATTRIBUTE_DESCRIPTION , .AttributeData = &SDP_Attribute_Description}, + {.AttributeID = SDP_ATTRIBUTE_AVAILABILITY, .AttributeData = &SDP_Attribute_Availability}, + SERVICE_ATTRIBUTE_TABLE_TERMINATOR + }; + +SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Name, "RFCOMM"); +SERVICE_ATTRIBUTE_TEXT(RFCOMM_Attribute_Description, "Virtual Serial"); +SERVICE_ATTRIBUTE_8BIT_LEN(RFCOMM_Attribute_Availability, SDP_DATATYPE_UNSIGNED_INT, 1, {0xFF}); +const ServiceAttributeTable_t RFCOMM_Attribute_Table[] PROGMEM = + { + {.AttributeID = SDP_ATTRIBUTE_NAME , .AttributeData = &RFCOMM_Attribute_Name}, + {.AttributeID = SDP_ATTRIBUTE_DESCRIPTION , .AttributeData = &RFCOMM_Attribute_Description}, + {.AttributeID = SDP_ATTRIBUTE_AVAILABILITY, .AttributeData = &RFCOMM_Attribute_Availability}, + SERVICE_ATTRIBUTE_TABLE_TERMINATOR + }; + +const ServiceTable_t SDP_Services_Table[] = + { + { // 128-bit UUID for the SDP service + .UUID = {BASE_96BIT_UUID, 0x01, 0x00, 0x00, 0x00}, + .AttributeTable = &SDP_Attribute_Table, + }, + { // 128-bit UUID for the RFCOMM service + .UUID = {BASE_96BIT_UUID, 0x03, 0x00, 0x00, 0x00}, + .AttributeTable = &RFCOMM_Attribute_Table, + }, + }; + + +void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel) +{ + SDP_PDUHeader_t* SDPHeader = (SDP_PDUHeader_t*)Data; + SDPHeader->ParameterLength = SwapEndian_16(SDPHeader->ParameterLength); + + BT_SDP_DEBUG(1, "SDP Packet Received"); + BT_SDP_DEBUG(2, "-- PDU ID: 0x%02X", SDPHeader->PDU); + BT_SDP_DEBUG(2, "-- Param Length: 0x%04X", SDPHeader->ParameterLength); + + switch (SDPHeader->PDU) + { + case SDP_PDU_SERVICESEARCHREQUEST: + ServiceDiscovery_ProcessServiceSearch(SDPHeader); + break; + case SDP_PDU_SERVICEATTRIBUTEREQUEST: + ServiceDiscovery_ProcessServiceAttribute(SDPHeader); + break; + case SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST: + ServiceDiscovery_ProcessServiceSearchAttribute(SDPHeader); + break; + } +} + +static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader) +{ + BT_SDP_DEBUG(1, "<< Service Search"); +} + +static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader) +{ + BT_SDP_DEBUG(1, "<< Service Attribute"); +} + +static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader) +{ + void* CurrentParameter = ((void*)SDPHeader + sizeof(SDP_PDUHeader_t)); + + BT_SDP_DEBUG(1, "<< Service Search Attribute"); + + uint8_t ElementHeaderSize; + + uint16_t ServicePatternLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); + BT_SDP_DEBUG(2, "-- Total UUID Length: 0x%04X", ServicePatternLength); + while (ServicePatternLength) + { + uint8_t UUIDLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); + uint8_t UUID[16] = {BASE_96BIT_UUID, 0x00, 0x00, 0x00, 0x00}; + + if (UUIDLength <= 32) + memcpy(&UUID[sizeof(UUID) - sizeof(uint32_t)], CurrentParameter, UUIDLength); + else + memcpy(UUID, CurrentParameter, UUIDLength); + + CurrentParameter += UUIDLength; + + BT_SDP_DEBUG(2, "-- UUID (%d): 0x%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", + UUIDLength, + UUID[15], UUID[14], UUID[13], UUID[12], UUID[11], UUID[10], UUID[9], UUID[8], + UUID[7], UUID[6], UUID[5], UUID[4], UUID[3], UUID[2], UUID[1], UUID[0]); + + ServicePatternLength -= (UUIDLength + ElementHeaderSize); + } + + uint16_t MaxAttributeSize = ServiceDiscovery_Read16BitParameter(&CurrentParameter); + BT_SDP_DEBUG(2, "-- Max Return Attribute Bytes: 0x%04X", MaxAttributeSize); + + uint16_t AttributeIDListLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); + BT_SDP_DEBUG(2, "-- Total Attribute Length: 0x%04X", AttributeIDListLength); + while (AttributeIDListLength) + { + uint8_t AttributeLength = ServiceDiscovery_GetDataElementSize(&CurrentParameter, &ElementHeaderSize); + uint32_t Attribute = 0; + + memcpy(&Attribute, CurrentParameter, AttributeLength); + CurrentParameter += AttributeLength; + + BT_SDP_DEBUG(2, "-- Attribute(%d): 0x%08lX", AttributeLength, Attribute); + + AttributeIDListLength -= (AttributeLength + ElementHeaderSize); + } +} + +static uint32_t ServiceDiscovery_GetDataElementSize(void** DataElementHeader, uint8_t* ElementHeaderSize) +{ + uint8_t SizeIndex = (*((uint8_t*)*DataElementHeader) & 0x07); + *DataElementHeader += sizeof(uint8_t); + + *ElementHeaderSize = 1; + + uint32_t ElementValue; + + switch (SizeIndex) + { + case 0: + ElementValue = 1; + break; + case 1: + ElementValue = 2; + break; + case 2: + ElementValue = 4; + break; + case 3: + ElementValue = 8; + break; + case 4: + ElementValue = 16; + break; + case 5: + ElementValue = *((uint8_t*)*DataElementHeader); + *DataElementHeader += sizeof(uint8_t); + *ElementHeaderSize = (1 + sizeof(uint8_t)); + break; + case 6: + ElementValue = *((uint16_t*)*DataElementHeader); + *DataElementHeader += sizeof(uint16_t); + *ElementHeaderSize = (1 + sizeof(uint16_t)); + break; + default: + ElementValue = *((uint32_t*)*DataElementHeader); + *DataElementHeader += sizeof(uint32_t); + *ElementHeaderSize = (1 + sizeof(uint32_t)); + break; + } + + return ElementValue; +} diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h index fc0abae45..4dc80a8e8 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/ServiceDiscoveryProtocol.h @@ -1,148 +1,148 @@ -/* - LUFA Library - Copyright (C) Dean Camera, 2010. - - dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com -*/ - -/* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the - software without specific, written prior permission. - - The author disclaim all warranties with regard to this - software, including all implied warranties of merchantability - and fitness. In no event shall the author be liable for any - special, indirect or consequential damages or any damages - whatsoever resulting from loss of use, data or profits, whether - in an action of contract, negligence or other tortious action, - arising out of or in connection with the use or performance of - this software. -*/ - -#ifndef _SERVICEDISCOVERYPROTOCOL_H_ -#define _SERVICEDISCOVERYPROTOCOL_H_ - - /* Includes: */ - #include - #include - #include - #include - - #include - #include - - #include "BluetoothStack.h" - - /* Macros: */ - #define BT_SDP_DEBUG(l, s, ...) do { if (SDP_DEBUG_LEVEL >= l) printf_P(PSTR("(SDP) " s "\r\n"), ##__VA_ARGS__); } while (0) - #define SDP_DEBUG_LEVEL 2 - - #define SDP_PDU_ERRORRESPONSE 0x01 - #define SDP_PDU_SERVICESEARCHREQUEST 0x02 - #define SDP_PDU_SERVICESEARCHRESPONSE 0x03 - #define SDP_PDU_SERVICEATTRIBUTEREQUEST 0x04 - #define SDP_PDU_SERVICEATTRIBUTERESPONSE 0x05 - #define SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST 0x06 - #define SDP_PDU_SERVICESEARCHATTRIBUTERESPONSE 0x07 - - #define SDP_ATTRIBUTE_NAME 0x0000 - #define SDP_ATTRIBUTE_DESCRIPTION 0x0001 - #define SDP_ATTRIBUTE_PROVIDER 0x0002 - #define SDP_ATTRIBUTE_AVAILABILITY 0x0008 - - #define SDP_DATATYPE_NIL (0x00 << 3) - #define SDP_DATATYPE_UNSIGNED_INT (0x01 << 3) - #define SDP_DATATYPE_SIGNED_INT (0x02 << 3) - #define SDP_DATATYPE_UUID (0x03 << 3) - #define SDP_DATATYPE_TEXT (0x04 << 3) - #define SDP_DATATYPE_BOOLEAN (0x05 << 3) - #define SDP_DATATYPE_ELEMENT_SEQUENCE (0x06 << 3) - #define SDP_DATATYPE_ELEMENT_ALTERNATIVE (0x07 << 3) - #define SDP_DATATYPE_URL (0x08 << 3) - - #define BASE_96BIT_UUID 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00 - - #define SERVICE_ATTRIBUTE_TEXT(name, string) SERVICE_ATTRIBUTE_8BIT_LEN(name, SDP_DATATYPE_TEXT, sizeof(string), string) - #define SERVICE_ATTRIBUTE_8BIT_LEN(name, type, size, ...) const ServiceAttributeData8Bit_t name PROGMEM = \ - {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} - #define SERVICE_ATTRIBUTE_16BIT_LEN(name, type, size, ...) const ServiceAttributeData16Bit_t name PROGMEM = \ - {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} - #define SERVICE_ATTRIBUTE_32BIT_LEN(name, type, size, ...) const ServiceAttributeData32Bit_t name PROGMEM = \ - {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} - #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR {.AttributeData = NULL} - - /* Type Defines: */ - typedef struct - { - uint8_t PDU; - uint16_t TransactionID; - uint16_t ParameterLength; - } SDP_PDUHeader_t; - - typedef struct - { - uint16_t AttributeID; - const void* AttributeData; - } ServiceAttributeTable_t; - - typedef struct - { - uint8_t UUID[16]; - const void* AttributeTable; - } ServiceTable_t; - - typedef struct - { - uint8_t Header; - uint32_t Size; - uint8_t Data[]; - } ServiceAttributeData32Bit_t; - - typedef struct - { - uint8_t Header; - uint16_t Size; - uint8_t Data[]; - } ServiceAttributeData16Bit_t; - - typedef struct - { - uint8_t Header; - uint8_t Size; - uint8_t Data[]; - } ServiceAttributeData8Bit_t; - - typedef struct - { - uint8_t Header; - uint8_t Data[]; - } ServiceAttributeData_t; - - /* Function Prototypes: */ - void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel); - - #if defined(INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C) - static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader); - static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader); - static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader); - - static inline uint16_t ServiceDiscovery_Read16BitParameter(void** AttributeHeader) - { - uint16_t ParamValue = *((uint16_t*)*AttributeHeader); - *AttributeHeader += sizeof(uint16_t); - return ParamValue; - } - - static uint32_t ServiceDiscovery_GetDataElementSize(void** AttributeHeader, uint8_t* ElementHeaderSize); - #endif - -#endif +/* + LUFA Library + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +/* + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the + software without specific, written prior permission. + + The author disclaim all warranties with regard to this + software, including all implied warranties of merchantability + and fitness. In no event shall the author be liable for any + special, indirect or consequential damages or any damages + whatsoever resulting from loss of use, data or profits, whether + in an action of contract, negligence or other tortious action, + arising out of or in connection with the use or performance of + this software. +*/ + +#ifndef _SERVICEDISCOVERYPROTOCOL_H_ +#define _SERVICEDISCOVERYPROTOCOL_H_ + + /* Includes: */ + #include + #include + #include + #include + + #include + #include + + #include "BluetoothStack.h" + + /* Macros: */ + #define BT_SDP_DEBUG(l, s, ...) do { if (SDP_DEBUG_LEVEL >= l) printf_P(PSTR("(SDP) " s "\r\n"), ##__VA_ARGS__); } while (0) + #define SDP_DEBUG_LEVEL 2 + + #define SDP_PDU_ERRORRESPONSE 0x01 + #define SDP_PDU_SERVICESEARCHREQUEST 0x02 + #define SDP_PDU_SERVICESEARCHRESPONSE 0x03 + #define SDP_PDU_SERVICEATTRIBUTEREQUEST 0x04 + #define SDP_PDU_SERVICEATTRIBUTERESPONSE 0x05 + #define SDP_PDU_SERVICESEARCHATTRIBUTEREQUEST 0x06 + #define SDP_PDU_SERVICESEARCHATTRIBUTERESPONSE 0x07 + + #define SDP_ATTRIBUTE_NAME 0x0000 + #define SDP_ATTRIBUTE_DESCRIPTION 0x0001 + #define SDP_ATTRIBUTE_PROVIDER 0x0002 + #define SDP_ATTRIBUTE_AVAILABILITY 0x0008 + + #define SDP_DATATYPE_NIL (0x00 << 3) + #define SDP_DATATYPE_UNSIGNED_INT (0x01 << 3) + #define SDP_DATATYPE_SIGNED_INT (0x02 << 3) + #define SDP_DATATYPE_UUID (0x03 << 3) + #define SDP_DATATYPE_TEXT (0x04 << 3) + #define SDP_DATATYPE_BOOLEAN (0x05 << 3) + #define SDP_DATATYPE_ELEMENT_SEQUENCE (0x06 << 3) + #define SDP_DATATYPE_ELEMENT_ALTERNATIVE (0x07 << 3) + #define SDP_DATATYPE_URL (0x08 << 3) + + #define BASE_96BIT_UUID 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00 + + #define SERVICE_ATTRIBUTE_TEXT(name, string) SERVICE_ATTRIBUTE_8BIT_LEN(name, SDP_DATATYPE_TEXT, sizeof(string), string) + #define SERVICE_ATTRIBUTE_8BIT_LEN(name, type, size, ...) const ServiceAttributeData8Bit_t name PROGMEM = \ + {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} + #define SERVICE_ATTRIBUTE_16BIT_LEN(name, type, size, ...) const ServiceAttributeData16Bit_t name PROGMEM = \ + {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} + #define SERVICE_ATTRIBUTE_32BIT_LEN(name, type, size, ...) const ServiceAttributeData32Bit_t name PROGMEM = \ + {.Header = (type | 5), .Size = size, .Data = __VA_ARGS__} + #define SERVICE_ATTRIBUTE_TABLE_TERMINATOR {.AttributeData = NULL} + + /* Type Defines: */ + typedef struct + { + uint8_t PDU; + uint16_t TransactionID; + uint16_t ParameterLength; + } SDP_PDUHeader_t; + + typedef struct + { + uint16_t AttributeID; + const void* AttributeData; + } ServiceAttributeTable_t; + + typedef struct + { + uint8_t UUID[16]; + const void* AttributeTable; + } ServiceTable_t; + + typedef struct + { + uint8_t Header; + uint32_t Size; + uint8_t Data[]; + } ServiceAttributeData32Bit_t; + + typedef struct + { + uint8_t Header; + uint16_t Size; + uint8_t Data[]; + } ServiceAttributeData16Bit_t; + + typedef struct + { + uint8_t Header; + uint8_t Size; + uint8_t Data[]; + } ServiceAttributeData8Bit_t; + + typedef struct + { + uint8_t Header; + uint8_t Data[]; + } ServiceAttributeData_t; + + /* Function Prototypes: */ + void ServiceDiscovery_ProcessPacket(void* Data, Bluetooth_Channel_t* Channel); + + #if defined(INCLUDE_FROM_SERVICEDISCOVERYPROTOCOL_C) + static void ServiceDiscovery_ProcessServiceSearch(SDP_PDUHeader_t* SDPHeader); + static void ServiceDiscovery_ProcessServiceAttribute(SDP_PDUHeader_t* SDPHeader); + static void ServiceDiscovery_ProcessServiceSearchAttribute(SDP_PDUHeader_t* SDPHeader); + + static inline uint16_t ServiceDiscovery_Read16BitParameter(void** AttributeHeader) + { + uint16_t ParamValue = *((uint16_t*)*AttributeHeader); + *AttributeHeader += sizeof(uint16_t); + return ParamValue; + } + + static uint32_t ServiceDiscovery_GetDataElementSize(void** AttributeHeader, uint8_t* ElementHeaderSize); + #endif + +#endif -- cgit v1.2.3