aboutsummaryrefslogtreecommitdiffstats
path: root/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
diff options
context:
space:
mode:
authorDean Camera <dean@fourwalledcubicle.com>2010-04-10 07:33:19 +0000
committerDean Camera <dean@fourwalledcubicle.com>2010-04-10 07:33:19 +0000
commit28f1ac81176bbcd9fd13d94e61323522a5279d27 (patch)
tree3cfd8e9fc2b594fbe4dfa676343d54a72f3f5581 /Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
parent797130bddc25d519cd765ca8d82d1e729e3d8ae0 (diff)
downloadlufa-28f1ac81176bbcd9fd13d94e61323522a5279d27.tar.gz
lufa-28f1ac81176bbcd9fd13d94e61323522a5279d27.tar.bz2
lufa-28f1ac81176bbcd9fd13d94e61323522a5279d27.zip
Add LUFA-side channel open/close routines, add signalling handlers for the creation and configuration of channels from the local device to the remote device, to add to the existing remote to local channel capabilities.
Diffstat (limited to 'Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c')
-rw-r--r--Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c150
1 files changed, 147 insertions, 3 deletions
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
index 7c238e9a6..50fba7c5d 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
@@ -120,6 +120,9 @@ static void Bluetooth_ProcessACLPackets(void)
case BT_SIGNAL_CONNECTION_REQUEST:
Bluetooth_Signal_ConnectionReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
break;
+ case BT_SIGNAL_CONNECTION_RESPONSE:
+ Bluetooth_Signal_ConnectionResp(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
+ break;
case BT_SIGNAL_CONFIGURATION_REQUEST:
Bluetooth_Signal_ConfigurationReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
break;
@@ -128,7 +131,10 @@ static void Bluetooth_ProcessACLPackets(void)
break;
case BT_SIGNAL_DISCONNECTION_REQUEST:
Bluetooth_Signal_DisconnectionReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
- break;
+ break;
+ case BT_SIGNAL_DISCONNECTION_RESPONSE:
+ Bluetooth_Signal_DisconnectionResp(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
+ break;
case BT_SIGNAL_ECHO_REQUEST:
Bluetooth_Signal_EchoReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
break;
@@ -193,6 +199,75 @@ uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t*
return BT_SENDPACKET_NoError;
}
+Bluetooth_Channel_t* Bluetooth_OpenChannel(uint16_t PSM)
+{
+ Bluetooth_Channel_t* ChannelData = NULL;
+
+ for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
+ {
+ if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
+ {
+ ChannelData = &Bluetooth_Connection.Channels[i];
+ ChannelData->LocalNumber = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);
+ break;
+ }
+ }
+
+ if (ChannelData == NULL)
+ return NULL;
+
+ ChannelData->RemoteNumber = 0;
+ ChannelData->PSM = PSM;
+ ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
+ ChannelData->State = Channel_Config_WaitConfig;
+
+ struct
+ {
+ BT_Signal_Header_t SignalCommandHeader;
+ BT_Signal_ConnectionReq_t ConnectionRequest;
+ } PacketData;
+
+ PacketData.SignalCommandHeader.Code = BT_SIGNAL_CONNECTION_REQUEST;
+ PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier;
+ PacketData.SignalCommandHeader.Length = sizeof(PacketData.ConnectionRequest);
+ PacketData.ConnectionRequest.PSM = PSM;
+ PacketData.ConnectionRequest.SourceChannel = ChannelData->LocalNumber;
+
+ Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
+
+ BT_ACL_DEBUG(1, ">> L2CAP Connection Request", NULL);
+ BT_ACL_DEBUG(2, "-- PSM 0x%04X", PacketData.ConnectionRequest.PSM);
+ BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.ConnectionRequest.SourceChannel);
+
+ return ChannelData;
+}
+
+void Bluetooth_CloseChannel(Bluetooth_Channel_t* Channel)
+{
+ if ((Channel == NULL) || (Channel->State == Channel_Closed))
+ return;
+
+ Channel->State = Channel_WaitDisconnect;
+
+ struct
+ {
+ BT_Signal_Header_t SignalCommandHeader;
+ BT_Signal_DisconnectionReq_t DisconnectionRequest;
+ } PacketData;
+
+ PacketData.SignalCommandHeader.Code = BT_SIGNAL_DISCONNECTION_REQUEST;
+ PacketData.SignalCommandHeader.Identifier = ++Bluetooth_Connection.SignallingIdentifier;
+ PacketData.SignalCommandHeader.Length = sizeof(PacketData.DisconnectionRequest);
+ PacketData.DisconnectionRequest.DestinationChannel = Channel->RemoteNumber;
+ PacketData.DisconnectionRequest.SourceChannel = Channel->LocalNumber;
+
+ Bluetooth_SendPacket(&PacketData, sizeof(PacketData), NULL);
+
+ BT_ACL_DEBUG(1, ">> L2CAP Disconnection Request", NULL);
+ BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", PacketData.DisconnectionRequest.DestinationChannel);
+ BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", PacketData.DisconnectionRequest.SourceChannel);
+}
+
static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPacketHeader,
BT_DataPacket_Header_t* DataHeader,
BT_Signal_Header_t* SignalCommandHeader)
@@ -208,8 +283,29 @@ static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPac
BT_ACL_DEBUG(2, "-- PSM: 0x%04X", ConnectionRequest.PSM);
BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConnectionRequest.SourceChannel);
- Bluetooth_Channel_t* ChannelData = Bluetooth_InitChannelData(ConnectionRequest.SourceChannel, ConnectionRequest.PSM);
+ Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionRequest.SourceChannel, true);
+ if (ChannelData == NULL)
+ {
+ for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
+ {
+ if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
+ {
+ ChannelData = &Bluetooth_Connection.Channels[i];
+ ChannelData->LocalNumber = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);
+ break;
+ }
+ }
+ }
+
+ if (ChannelData != NULL)
+ {
+ ChannelData->RemoteNumber = ConnectionRequest.SourceChannel;
+ ChannelData->PSM = ConnectionRequest.PSM;
+ ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
+ ChannelData->State = Channel_Config_WaitConfig;
+ }
+
struct
{
BT_Signal_Header_t SignalCommandHeader;
@@ -228,10 +324,37 @@ static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPac
BT_ACL_DEBUG(1, ">> L2CAP Connection Response", NULL);
BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConnectionResponse.Result);
- BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConnectionResponse.SourceChannel);
BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.ConnectionResponse.DestinationChannel);
+ BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.ConnectionResponse.SourceChannel);
+}
+
+static inline void Bluetooth_Signal_ConnectionResp(BT_ACL_Header_t* ACLPacketHeader,
+ BT_DataPacket_Header_t* DataHeader,
+ BT_Signal_Header_t* SignalCommandHeader)
+{
+ BT_Signal_ConnectionResp_t ConnectionResponse;
+
+ Pipe_Read_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
+
+ Pipe_ClearIN();
+ Pipe_Freeze();
+
+ BT_ACL_DEBUG(1, "<< L2CAP Connection Response", NULL);
+ 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);
+
+ Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConnectionResponse.DestinationChannel, false);
+
+ if (ChannelData != NULL)
+ {
+ ChannelData->RemoteNumber = ConnectionResponse.SourceChannel;
+ ChannelData->State = (ConnectionResponse.Result == BT_CONNECTION_SUCCESSFUL) ?
+ Channel_Config_WaitConfig : Channel_Closed;
+ }
}
+
static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t* ACLPacketHeader,
BT_DataPacket_Header_t* DataHeader,
BT_Signal_Header_t* SignalCommandHeader)
@@ -375,6 +498,27 @@ static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t* ACL
BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.DisconnectionResponse.DestinationChannel);
}
+static inline void Bluetooth_Signal_DisconnectionResp(BT_ACL_Header_t* ACLPacketHeader,
+ BT_DataPacket_Header_t* DataHeader,
+ BT_Signal_Header_t* SignalCommandHeader)
+{
+ BT_Signal_DisconnectionResp_t DisconnectionResponse;
+
+ Pipe_Read_Stream_LE(&DisconnectionResponse, sizeof(DisconnectionResponse));
+
+ BT_ACL_DEBUG(1, "<< L2CAP Disconnection Response", NULL);
+ BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", DisconnectionResponse.DestinationChannel);
+ BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", DisconnectionResponse.SourceChannel);
+
+ Pipe_ClearIN();
+ Pipe_Freeze();
+
+ Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionResponse.SourceChannel, true);
+
+ if (ChannelData->State == Channel_WaitDisconnect)
+ ChannelData->State = Channel_Closed;
+}
+
static inline void Bluetooth_Signal_EchoReq(BT_ACL_Header_t* ACLPacketHeader,
BT_DataPacket_Header_t* DataHeader,
BT_Signal_Header_t* SignalCommandHeader)