From 4b952d479aa4bafe203a64d6211512450833cf8f Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Wed, 23 Jun 2010 08:10:21 +0000
Subject: [PATCH] Split RFCOMM channel signals into Remote and Local pairs. Use
 memcpy() to copy over data from the command parameters to the RFCOMM response
 parameters.

Ensure that only the valid parameter bytes are sent back in response to MSC commands.
---
 .../Incomplete/BluetoothHost/Lib/RFCOMM.c     | 14 ++++----
 .../Incomplete/BluetoothHost/Lib/RFCOMM.h     | 13 +++++--
 .../BluetoothHost/Lib/RFCOMMControl.c         | 36 ++++++++++---------
 .../BluetoothHost/Lib/RFCOMMControl.h         | 12 +++----
 4 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
index 994a24b9a..7c39cb028 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
@@ -232,12 +232,14 @@ static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluet
 		/* If the channel's DLCI is zero, the channel state entry is free */
 		if (!(CurrRFCOMMChannel->DLCI))
 		{
-			CurrRFCOMMChannel->DLCI         = FrameAddress->DLCI;
-			CurrRFCOMMChannel->State        = RFCOMM_Channel_Open;
-			CurrRFCOMMChannel->Priority     = 7 + (CurrRFCOMMChannel->DLCI >> 3) + ((CurrRFCOMMChannel->DLCI >> 3) * 7);
-			CurrRFCOMMChannel->MTU          = 0xFFFF;
-			CurrRFCOMMChannel->Signals      = 0;
-			CurrRFCOMMChannel->BreakSignals = 0;
+			CurrRFCOMMChannel->DLCI     = FrameAddress->DLCI;
+			CurrRFCOMMChannel->State    = RFCOMM_Channel_Open;
+			CurrRFCOMMChannel->Priority = 7 + (CurrRFCOMMChannel->DLCI >> 3) + ((CurrRFCOMMChannel->DLCI >> 3) * 7);
+			CurrRFCOMMChannel->MTU      = 0xFFFF;
+			CurrRFCOMMChannel->Remote.Signals     = 0 | (1 << 0);
+			CurrRFCOMMChannel->Remote.BreakSignal = 0 | (1 << 0);
+			CurrRFCOMMChannel->Local.Signals      = RFCOMM_SIGNAL_RTC | RFCOMM_SIGNAL_RTR | RFCOMM_SIGNAL_DV | (1 << 0);
+			CurrRFCOMMChannel->Local.BreakSignal  = 0 | (1 << 0);
 		
 			BT_RFCOMM_DEBUG(1, ">> UA Sent");
 			RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
index d37c068d9..7b1f45b30 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
@@ -86,9 +86,16 @@
 			uint8_t  State;
 			uint8_t  Priority;
 			uint16_t MTU;
-			uint8_t  StatusFlags;
-			uint8_t  Signals;
-			uint8_t  BreakSignals;
+			struct
+			{
+				uint8_t  Signals;
+				uint8_t  BreakSignal;
+			} Remote;
+			struct
+			{
+				uint8_t  Signals;
+				uint8_t  BreakSignal;
+			} Local;
 		} RFCOMM_Channel_t;
 		
 	/* External Variables: */
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
index 1d1863348..01fab788f 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
@@ -88,7 +88,7 @@ static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeade
 	} TestResponse;
 
 	/* Fill out the Test response data */
-	TestResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_Test, .EA = true};
+	TestResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_Test, .EA = true, .CR = false};
 	TestResponse.Length        = (CommandDataLen << 1) | 0x01;
 	memcpy(TestResponse.TestData, Params, CommandDataLen);
 	
@@ -130,11 +130,11 @@ static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader,
 	  return;
 	  
 	/* Save the new channel signals to the channel state structure */
-	RFCOMMChannel->Signals = Params->Signals;
+	RFCOMMChannel->Remote.Signals = Params->Signals;
 	
 	/* If the command contains the optional break signals field, store the value */
 	if (CommandDataLen == sizeof(RFCOMM_MS_Parameters_t))
-	  RFCOMMChannel->BreakSignals = Params->BreakSignals;
+	  RFCOMMChannel->Remote.BreakSignal = Params->BreakSignal;
 	  
 	struct
 	{
@@ -144,14 +144,15 @@ static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader,
 	} MSResponse;
 
 	/* Fill out the MS response data */
-	MSResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true};
-	MSResponse.Length        = (CommandDataLen << 1) | 0x01;
-	MSResponse.Params        = *Params;
+	MSResponse.CommandHeader  = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true, .CR = false};
+	MSResponse.Length         = (CommandDataLen << 1) | 0x01;
+	memcpy(&MSResponse.Params, Params, sizeof(RFCOMM_MS_Parameters_t));
 	
 	BT_RFCOMM_DEBUG(1, ">> MS Response");
 
 	/* Send the PDN response to acknowledge the command */
-	RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(MSResponse), &MSResponse, Channel);
+	RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH,
+	                 (sizeof(MSResponse) - sizeof(MSResponse.Params) + CommandDataLen), &MSResponse, Channel);
 }
 
 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
@@ -190,10 +191,13 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
 			/* If the channel's DLCI is zero, the channel state entry is free */
 			if (!(RFCOMM_Channels[i].DLCI))
 			{
-				RFCOMMChannel               = &RFCOMM_Channels[i];
-				RFCOMMChannel->DLCI         = Params->DLCI;
-				RFCOMMChannel->Signals      = 0;
-				RFCOMMChannel->BreakSignals = 0;
+				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);
+				RFCOMMChannel->Local.BreakSignal  = 0 | (1 << 0);
 				break;
 			}
 		}
@@ -207,9 +211,9 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
 	}
 	
 	/* Save the new channel configuration */
-	RFCOMMChannel->State       = RFCOMM_Channel_Open;
-	RFCOMMChannel->Priority    = Params->Priority;
-	RFCOMMChannel->MTU         = Params->MaximumFrameSize;
+	RFCOMMChannel->State    = RFCOMM_Channel_Open;
+	RFCOMMChannel->Priority = Params->Priority;
+	RFCOMMChannel->MTU      = Params->MaximumFrameSize;
 	
 	struct
 	{
@@ -219,9 +223,9 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
 	} DPNResponse;
 	
 	/* Fill out the DPN response data */
-	DPNResponse.CommandHeader           = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true};
+	DPNResponse.CommandHeader           = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true, .CR = false};
 	DPNResponse.Length                  = (sizeof(DPNResponse.Params) << 1) | 0x01;
-	DPNResponse.Params                  = *Params;
+	memcpy(&DPNResponse.Params, Params, sizeof(RFCOMM_DPN_Parameters_t));
 	DPNResponse.Params.ConvergenceLayer = 0x00; // TODO: Enable credit based transaction support
 	
 	BT_RFCOMM_DEBUG(1, ">> DPN Response");
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.h b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.h
index b992c27cb..adad4332f 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.h
@@ -50,11 +50,11 @@
 		#include "RFCOMM.h"
 		
 	/* Macros: */
-		#define RFCOMM_STATUSFLAG_FC     (1 << 1)
-		#define RFCOMM_STATUSFLAG_RTC    (1 << 2)
-		#define RFCOMM_STATUSFLAG_RTR    (1 << 3)
-		#define RFCOMM_STATUSFLAG_IC     (1 << 6)
-		#define RFCOMM_STATUSFLAG_DV     (1 << 7)
+		#define RFCOMM_SIGNAL_FC     (1 << 1)
+		#define RFCOMM_SIGNAL_RTC    (1 << 2)
+		#define RFCOMM_SIGNAL_RTR    (1 << 3)
+		#define RFCOMM_SIGNAL_IC     (1 << 6)
+		#define RFCOMM_SIGNAL_DV     (1 << 7)
 
 	/* Enums: */
 		enum RFCOMM_Control_Commands_t
@@ -106,7 +106,7 @@
 		{
 			RFCOMM_Address_t Channel;
 			uint8_t          Signals;
-			uint8_t          BreakSignals;
+			uint8_t          BreakSignal;
 		} RFCOMM_MS_Parameters_t;
 
 	/* Function Prototypes: */
-- 
GitLab