Refactor out RFCOMM packet send code into a seperate function.
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 6 Jun 2010 14:39:51 +0000 (14:39 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 6 Jun 2010 14:39:51 +0000 (14:39 +0000)
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h

index a57f3a4..c0209d5 100644 (file)
@@ -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;
index cd5375e..dd30d55 100644 (file)
        /* 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