From b6565404685fbac1698d256823abdbdb3d3ddaff Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sat, 26 Jun 2010 15:07:13 +0000
Subject: [PATCH] Make Bluetooth ACL channel searches skip over closed
 (invalid) channels. RFCOMM channels are considered invalid when the channel
 state is closed, not when the DLCI is zero - fix incorrect code.

---
 .../Incomplete/BluetoothHost/Lib/BluetoothStack.c    |  5 +++++
 Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c     |  9 +++++----
 Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h     |  4 ++--
 .../Incomplete/BluetoothHost/Lib/RFCOMMControl.c     | 12 ++++++------
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
index 1a667987b..07d867b4b 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/BluetoothStack.c
@@ -84,9 +84,14 @@ Bluetooth_Channel_t* Bluetooth_GetChannelData(const uint16_t SearchValue, const
 	for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
 	{
 		Bluetooth_Channel_t* ChannelData = &Bluetooth_Connection.Channels[i];
+
+		/* Closed channels should be ignored as they are not considered valid data */
+		if (ChannelData->State == BT_Channel_Closed)
+		  continue;
 	
 		bool FoundMatch = false;
 		
+		/* Search the current channel for the search key to see if it matches */
 		switch (SearchKey)
 		{
 			case CHANNEL_SEARCH_LOCALNUMBER:
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
index 7c39cb028..c7b3e8d8d 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
@@ -111,7 +111,7 @@ RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI)
 		RFCOMM_Channel_t* CurrRFCOMMChannel = &RFCOMM_Channels[i];
 	
 		/* If the current non-closed channel's DLCI matches the search DLCI, return it to the caller */
-		if ((CurrRFCOMMChannel->DLCI == DLCI) && (CurrRFCOMMChannel->State != RFCOMM_Channel_Closed))
+		if ((CurrRFCOMMChannel->State != RFCOMM_Channel_Closed) && (CurrRFCOMMChannel->DLCI == DLCI))
 		  return CurrRFCOMMChannel;
 	}
 
@@ -192,6 +192,7 @@ static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length)
 {
 	uint8_t FCS = 0xFF;
 	
+	/* Calculate new Frame CRC value via the given data bytes and the CRC table */
 	for (uint8_t i = 0; i < Length; i++)
 	  FCS = pgm_read_byte(&CRC8_Table[FCS ^ ((uint8_t*)FrameStart)[i]]);
 
@@ -213,7 +214,7 @@ static void RFCOMM_ProcessDISC(const RFCOMM_Address_t* const FrameAddress, Bluet
 	
 	/* If the requested channel is currently open, destroy it */
 	if (RFCOMMChannel != NULL)
-	  RFCOMMChannel->DLCI = 0x00;
+	  RFCOMMChannel->State = RFCOMM_Channel_Closed;
 
 	BT_RFCOMM_DEBUG(1, ">> UA Sent");
 	RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
@@ -229,8 +230,8 @@ static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluet
 	{
 		RFCOMM_Channel_t* CurrRFCOMMChannel = &RFCOMM_Channels[i];
 	
-		/* If the channel's DLCI is zero, the channel state entry is free */
-		if (!(CurrRFCOMMChannel->DLCI))
+		/* If the channel's state is closed, the channel state entry is free */
+		if (CurrRFCOMMChannel->State == RFCOMM_Channel_Closed)
 		{
 			CurrRFCOMMChannel->DLCI     = FrameAddress->DLCI;
 			CurrRFCOMMChannel->State    = RFCOMM_Channel_Open;
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
index 7b1f45b30..edd93f9d5 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
@@ -111,14 +111,14 @@
 			                               const uint16_t DataLen, const void* Data, Bluetooth_Channel_t* const Channel);
 
 		#if defined(INCLUDE_FROM_RFCOMM_C)
+			static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length);			
+
 			static void RFCOMM_ProcessDM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel);
 			static void RFCOMM_ProcessDISC(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel);
 			static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel);
 			static void RFCOMM_ProcessUA(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel);
 			static void RFCOMM_ProcessUIH(const RFCOMM_Address_t* const FrameAddress, const uint16_t FrameLength, 
                                           const uint8_t* FrameData, Bluetooth_Channel_t* const Channel);
-
-			static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length);			
 		#endif
 		
 #endif
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
index 01fab788f..5e9c86b1d 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
@@ -188,12 +188,12 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
 		/* Find a free entry in the RFCOMM channel multiplexer state array */
 		for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
 		{
-			/* If the channel's DLCI is zero, the channel state entry is free */
-			if (!(RFCOMM_Channels[i].DLCI))
+			/* If the channel's state is closed, the channel state entry is free */
+			if (RFCOMMChannel->State == RFCOMM_Channel_Closed)
 			{
-				RFCOMMChannel       = &RFCOMM_Channels[i];
-				RFCOMMChannel->DLCI = Params->DLCI;
-				RFCOMMChannel->MTU  = 0xFFFF;
+				RFCOMMChannel        = &RFCOMM_Channels[i];
+				RFCOMMChannel->DLCI  = Params->DLCI;
+				RFCOMMChannel->MTU   = 0xFFFF;
 				RFCOMMChannel->Remote.Signals     = 0 | (1 << 0);
 				RFCOMMChannel->Remote.BreakSignal = 0 | (1 << 0);
 				RFCOMMChannel->Local.Signals      = RFCOMM_SIGNAL_RTC | RFCOMM_SIGNAL_RTR | RFCOMM_SIGNAL_DV | (1 << 0);
@@ -230,6 +230,6 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
 	
 	BT_RFCOMM_DEBUG(1, ">> DPN Response");
 
-	/* Send the PDN response to acknowledge the command */
+	/* Send the DPN response to acknowledge the command */
 	RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);
 }
-- 
GitLab