From 797130bddc25d519cd765ca8d82d1e729e3d8ae0 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sat, 10 Apr 2010 05:58:37 +0000
Subject: [PATCH] Bluetooth demo can now create and maintain logical channels -
 need to determine why Windows machines refuse to connect.

---
 .../BluetoothHost/Lib/BluetoothACLPackets.c   | 72 ++++++++++++++-----
 .../BluetoothHost/Lib/BluetoothACLPackets.h   |  9 ++-
 .../BluetoothHost/Lib/BluetoothStack.c        | 37 ++++++----
 .../BluetoothHost/Lib/BluetoothStack.h        |  2 +-
 LUFA/ManPages/MainPage.txt                    |  2 +-
 5 files changed, 86 insertions(+), 36 deletions(-)

diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
index a35dcb17a..7c238e9a6 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.c
@@ -114,7 +114,7 @@ static void Bluetooth_ProcessACLPackets(void)
 	{
 		BT_Signal_Header_t SignalCommandHeader;
 		Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader));
-
+		
 		switch (SignalCommandHeader.Code)
 		{
 			case BT_SIGNAL_CONNECTION_REQUEST:
@@ -123,6 +123,9 @@ static void Bluetooth_ProcessACLPackets(void)
 			case BT_SIGNAL_CONFIGURATION_REQUEST:
 				Bluetooth_Signal_ConfigurationReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
 				break;
+			case BT_SIGNAL_CONFIGURATION_RESPONSE:
+				Bluetooth_Signal_ConfigurationResp(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
+				break;
 			case BT_SIGNAL_DISCONNECTION_REQUEST:
 				Bluetooth_Signal_DisconnectionReq(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
 				break;			
@@ -143,7 +146,7 @@ static void Bluetooth_ProcessACLPackets(void)
 	}
 	else
 	{
-		Bluetooth_PacketReceived(&DataHeader.PayloadLength, Bluetooth_GetChannelData(DataHeader.DestinationChannel, true));
+		Bluetooth_PacketReceived(&DataHeader.PayloadLength, Bluetooth_GetChannelData(DataHeader.DestinationChannel, false));
 	
 		Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE);
 		Pipe_Discard_Stream(DataHeader.PayloadLength);
@@ -165,7 +168,7 @@ uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t*
 
 	// TODO: Add packet fragmentation here after retrieving the device's signal channel MTU
 
-	ACLPacketHeader.ConnectionHandle      = Bluetooth_Connection.ConnectionHandle | (1 << 13);
+	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;
@@ -176,6 +179,7 @@ uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t*
 	Pipe_Write_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader));
 	Pipe_Write_Stream_LE(&DataHeader, sizeof(DataHeader));
 	Pipe_Write_Stream_LE(Data, DataLen);
+	Pipe_ClearOUT();
 	
 	Pipe_Freeze();
 	
@@ -189,9 +193,9 @@ uint8_t Bluetooth_SendPacket(void* Data, uint16_t DataLen, Bluetooth_Channel_t*
 	return BT_SENDPACKET_NoError;
 }
 
-static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPacketHeader,
+static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t*        ACLPacketHeader,
                                                   BT_DataPacket_Header_t* DataHeader,
-                                                  BT_Signal_Header_t* SignalCommandHeader)
+                                                  BT_Signal_Header_t*     SignalCommandHeader)
 {
 	BT_Signal_ConnectionReq_t ConnectionRequest;
 	
@@ -216,8 +220,8 @@ static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPacketHead
 	ResponsePacket.SignalCommandHeader.Identifier        = SignalCommandHeader->Identifier;
 	ResponsePacket.SignalCommandHeader.Length            = sizeof(ResponsePacket.ConnectionResponse);
 	ResponsePacket.ConnectionResponse.Result             = (ChannelData == NULL) ? BT_CONNECTION_REFUSED_RESOURCES : BT_CONNECTION_SUCCESSFUL;
-	ResponsePacket.ConnectionResponse.DestinationChannel = ChannelData->LocalNumber;
-	ResponsePacket.ConnectionResponse.SourceChannel      = ChannelData->RemoteNumber;
+	ResponsePacket.ConnectionResponse.DestinationChannel = ChannelData->RemoteNumber;
+	ResponsePacket.ConnectionResponse.SourceChannel      = ChannelData->LocalNumber;
 	ResponsePacket.ConnectionResponse.Status             = 0x00;
 	
 	Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
@@ -228,9 +232,9 @@ static inline void Bluetooth_Signal_ConnectionReq(BT_ACL_Header_t* ACLPacketHead
 	BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ResponsePacket.ConnectionResponse.DestinationChannel);
 }
 
-static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t* ACLPacketHeader,
+static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t*        ACLPacketHeader,
                                                      BT_DataPacket_Header_t* DataHeader,
-                                                     BT_Signal_Header_t* SignalCommandHeader)
+                                                     BT_Signal_Header_t*     SignalCommandHeader)
 {
 	BT_Signal_ConfigurationReq_t ConfigurationRequest;
 	uint8_t OptionsLen;
@@ -259,7 +263,7 @@ static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t* ACLPacketH
 
 	BT_ACL_DEBUG(1, "<< L2CAP Configuration Request", NULL);
 	BT_ACL_DEBUG(2, "-- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel);
-	BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", ConfigurationRequest.DestinationChannel);
+	BT_ACL_DEBUG(2, "-- Options Len: 0x%04X", (DataHeader->PayloadLength - sizeof(*SignalCommandHeader)));
 	BT_ACL_DEBUG(2, "-- Remote MTU: 0x%04X", ChannelData->RemoteMTU);
 	
 	struct
@@ -298,9 +302,43 @@ static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t* ACLPacketH
 	BT_ACL_DEBUG(2, "-- Result: 0x%02X", ResponsePacket.ConfigurationResponse.Result);
 }
 
-static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t* ACLPacketHeader,
+static inline void Bluetooth_Signal_ConfigurationResp(BT_ACL_Header_t*        ACLPacketHeader,
+                                                      BT_DataPacket_Header_t* DataHeader,
+                                                      BT_Signal_Header_t*     SignalCommandHeader)
+{
+	BT_Signal_ConfigurationResp_t ConfigurationResponse;
+
+	Pipe_Read_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
+
+	Pipe_ClearIN();
+	Pipe_Freeze();
+	
+	BT_ACL_DEBUG(1, "<< L2CAP Configuration Response", NULL);
+	BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ConfigurationResponse.SourceChannel);
+	BT_ACL_DEBUG(2, "-- Result: 0x%02X", ConfigurationResponse.Result);
+
+	if (ConfigurationResponse.Result == BT_CONFIGURATION_SUCCESSFUL)
+	{
+		Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationResponse.SourceChannel, true);
+		
+		if (ChannelData != NULL)
+		{
+			switch (ChannelData->State)
+			{
+				case Channel_Config_WaitReqResp:
+					ChannelData->State = Channel_Config_WaitReq;
+					break;
+				case Channel_Config_WaitResp:
+					ChannelData->State = Channel_Open;
+					break;
+			}
+		}		
+	}
+}
+
+static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t*        ACLPacketHeader,
                                                      BT_DataPacket_Header_t* DataHeader,
-                                                     BT_Signal_Header_t* SignalCommandHeader)
+                                                     BT_Signal_Header_t*     SignalCommandHeader)
 {
 	BT_Signal_DisconnectionReq_t DisconnectionRequest;
 	
@@ -313,7 +351,7 @@ static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t* ACLPacketH
 	Pipe_ClearIN();
 	Pipe_Freeze();
 	
-	Bluetooth_Channel_t* ChannelData      = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, true);
+	Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(DisconnectionRequest.SourceChannel, true);
 
 	struct
 	{
@@ -324,8 +362,8 @@ static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t* ACLPacketH
 	ResponsePacket.SignalCommandHeader.Code                 = BT_SIGNAL_DISCONNECTION_RESPONSE;
 	ResponsePacket.SignalCommandHeader.Identifier           = SignalCommandHeader->Identifier;
 	ResponsePacket.SignalCommandHeader.Length               = sizeof(ResponsePacket.DisconnectionResponse);
-	ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->LocalNumber;
-	ResponsePacket.DisconnectionResponse.SourceChannel      = ChannelData->RemoteNumber;
+	ResponsePacket.DisconnectionResponse.DestinationChannel = ChannelData->RemoteNumber;
+	ResponsePacket.DisconnectionResponse.SourceChannel      = ChannelData->LocalNumber;
 
 	Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), NULL);
 
@@ -360,9 +398,9 @@ static inline void Bluetooth_Signal_EchoReq(BT_ACL_Header_t* ACLPacketHeader,
 	BT_ACL_DEBUG(1, ">> L2CAP Echo Response", NULL);
 }
 
-static inline void Bluetooth_Signal_InformationReq(BT_ACL_Header_t* ACLPacketHeader,
+static inline void Bluetooth_Signal_InformationReq(BT_ACL_Header_t*        ACLPacketHeader,
                                                    BT_DataPacket_Header_t* DataHeader,
-                                                   BT_Signal_Header_t* SignalCommandHeader)
+                                                   BT_Signal_Header_t*     SignalCommandHeader)
 {
 	BT_Signal_InformationReq_t InformationRequest;
 
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h
index d9d07cc70..1edbfa603 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothACLPackets.h
@@ -73,6 +73,8 @@
 		
 		#define BT_CONFIG_OPTION_MTU              1
 		
+		#define BT_ACL_FIRST_AUTOFLUSH            (1 << 13)
+		
 	/* Type Defines: */
 		typedef struct
 		{
@@ -145,8 +147,8 @@
 		
 		typedef struct
 		{
-			uint8_t  Type;
-			uint16_t Length;
+			uint8_t Type;
+			uint8_t Length;
 		} BT_Config_Option_Header_t;
 
 	/* Function Prototypes: */
@@ -164,6 +166,9 @@
 			static inline void Bluetooth_Signal_ConfigurationReq(BT_ACL_Header_t* ACLPacketHeader,
                                                                  BT_DataPacket_Header_t* DataHeader,
                                                                  BT_Signal_Header_t* SignalCommandHeader);
+			static inline void Bluetooth_Signal_ConfigurationResp(BT_ACL_Header_t* ACLPacketHeader,
+                                                                  BT_DataPacket_Header_t* DataHeader,
+                                                                  BT_Signal_Header_t* SignalCommandHeader);
 			static inline void Bluetooth_Signal_DisconnectionReq(BT_ACL_Header_t* ACLPacketHeader,
                                                                  BT_DataPacket_Header_t* DataHeader,
                                                                  BT_Signal_Header_t* SignalCommandHeader);
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
index 7989063e8..11b6a616b 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
@@ -54,13 +54,13 @@ void Bluetooth_Stack_USBTask(void)
 	Bluetooth_ACLTask();
 }
 
-Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource)
+Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchByRemoteChannel)
 {
 	for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
 	{
 		Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];
 	
-		uint16_t CurrentChannelNumber = (SearchBySource) ? ChannelData->RemoteNumber : ChannelData->LocalNumber;
+		uint16_t CurrentChannelNumber = (SearchByRemoteChannel) ? ChannelData->RemoteNumber : ChannelData->LocalNumber;
 	
 		if (CurrentChannelNumber == ChannelNumber)
 		  return ChannelData;
@@ -71,21 +71,28 @@ Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool Searc
 
 Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM)
 {
-	for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
+	Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(RemoteChannelNumber, false);
+
+	if (ChannelData == NULL)
 	{
-		Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];
-	
-		if (ChannelData->State == Channel_Closed)
+		for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
 		{
-			ChannelData->RemoteNumber = RemoteChannelNumber;
-			ChannelData->LocalNumber  = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);
-			ChannelData->PSM          = PSM;
-			ChannelData->LocalMTU     = MAXIMUM_CHANNEL_MTU;
-			ChannelData->State        = Channel_Config_WaitConfig;
-			
-			return ChannelData;
-		}		
+			if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
+			{
+				ChannelData = &Bluetooth_Connection.Channels[i];				
+				ChannelData->LocalNumber = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);
+				break;
+			}
+		}
 	}
 
-	return NULL;
+	if (ChannelData != NULL)
+	{
+		ChannelData->RemoteNumber = RemoteChannelNumber;
+		ChannelData->PSM          = PSM;
+		ChannelData->LocalMTU     = MAXIMUM_CHANNEL_MTU;
+		ChannelData->State        = Channel_Config_WaitConfig;
+	}
+
+	return ChannelData;
 }
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h
index c20cd123e..b768bc2d2 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.h
@@ -103,7 +103,7 @@
 		#include "BluetoothACLPackets.h"		
 		
 	/* Function Prototypes: */
-		Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource);
+		Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchByRemoteChannel);
 		Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM);
 		
 		void Bluetooth_Stack_Init(void);
diff --git a/LUFA/ManPages/MainPage.txt b/LUFA/ManPages/MainPage.txt
index d7baa0d65..6bf772728 100644
--- a/LUFA/ManPages/MainPage.txt
+++ b/LUFA/ManPages/MainPage.txt
@@ -8,7 +8,7 @@
  *  \mainpage
  *
  *  \image html LUFA.png
- *  <div align="center"><small><i>Logo design by <b>Ryo Yamauchi</b></i></small></div>
+ *  <div align="center"><small><i>Logo design by <b>Ryo</b> - http://ryophotography.wordpress.com</i></small></div>
  *  \n
  *
  *  <b>LUFA is donationware. For author and donation information, see \ref Page_Donating.</b>
-- 
GitLab