From 361e1ec1d83d5b6d97890f2a27b9c9ae24e561ee Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sun, 6 Jun 2010 14:39:51 +0000
Subject: [PATCH] Refactor out RFCOMM packet send code into a seperate
 function.

---
 .../Incomplete/BluetoothHost/Lib/RFCOMM.c     | 56 ++++++++++++-------
 .../Incomplete/BluetoothHost/Lib/RFCOMM.h     | 10 +---
 2 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
index a57f3a44d..c0209d56f 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
@@ -102,29 +102,9 @@ static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetoo
 	
 	// TODO: Reset channel send/receive state here
 	
-	struct
-	{
-		RFCOMM_Header_t FrameHeader;
-		uint8_t         FrameLength;
-		uint8_t         FCS;
-	} ResponsePacket;
-	
-	/* Copy over the same frame header as the sent packet to copy the logical RFCOMM channel address */
-	ResponsePacket.FrameHeader.Address              = FrameHeader->Address;
-	
-	/* Set the frame type to an Unnumbered Acknowledgement to acknowledge the SABM request */
-	ResponsePacket.FrameHeader.Control = RFCOMM_Frame_UA;
-	
-	/* Set the length to 0 (LSB indicates end of 8-bit length field) */
-	ResponsePacket.FrameLength = 0x01;
-	
-	/* Calculate the frame checksum from all fields except the FCS field itself */
-	ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, sizeof(ResponsePacket) - sizeof(ResponsePacket.FCS));
-	
 	BT_RFCOMM_DEBUG(1, ">> UA Sent");
 
-	/* Send the completed response packet to the sender */
-	Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), Channel);
+	RFCOMM_SendFrame(FrameHeader->Address, RFCOMM_Frame_UA, 0, NULL, Channel);
 }
 
 static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
@@ -159,6 +139,40 @@ static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetoot
 	BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);
 }
 
+static void RFCOMM_SendFrame(const uint8_t Address, const uint8_t Type, const uint16_t DataLen, const uint8_t* Data,
+                             Bluetooth_Channel_t* const Channel)
+{
+	struct
+	{
+		RFCOMM_Header_t FrameHeader;
+		uint8_t         Size[1 + (DataLen >= 128)];
+		uint8_t         Data[DataLen];
+		uint8_t         FCS;
+	} ResponsePacket;
+	
+	/* Set the frame header values to the specified address and frame type */
+	ResponsePacket.FrameHeader.Address = Address;
+	ResponsePacket.FrameHeader.Control = Type;
+	
+	/* Set the lower 7 bits of the packet length */
+	ResponsePacket.Size[0] = (DataLen << 1);
+	
+	/* Terminate the size field if size is 7 bits or lower, otherwise set the upper 8 bits of the length */
+	if (DataLen < 128)
+	  ResponsePacket.Size[0] |= 0x01;
+	else
+	  ResponsePacket.Size[1]  = (DataLen >> 7);
+	
+	/* Copy over the packet data from the source buffer to the response packet buffer */
+	memcpy(ResponsePacket.Data, Data, DataLen);
+	
+	/* Calculate the frame checksum from all fields except the FCS field itself */
+	ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, sizeof(ResponsePacket) - sizeof(ResponsePacket.FCS));
+	
+	/* Send the completed response packet to the sender */
+	Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), Channel);
+}
+
 static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length)
 {
 	const uint8_t* CurrPos = FrameStart;
diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
index cd5375e91..dd30d5535 100644
--- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
+++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
@@ -68,13 +68,7 @@
 	/* Type Defines: */
 		typedef struct
 		{
-			struct
-			{
-				unsigned char LogicalChannel   : 6;
-				unsigned char PollResponse     : 1;
-				unsigned char LastAddressOctet : 1;
-			} Address;
-			
+			uint8_t Address;			
 			uint8_t Control;
 		} RFCOMM_Header_t;
 
@@ -89,6 +83,8 @@
 			static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
 			static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
 
+			static void RFCOMM_SendFrame(const uint8_t Address, const uint8_t Type, const uint16_t DataLen,
+			                             const uint8_t* Data, Bluetooth_Channel_t* const Channel);
 			static uint8_t  RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length);
 			static uint16_t RFCOMM_GetFrameDataLength(const uint8_t** BufferPos);
 		#endif
-- 
GitLab