Move out RFCOMM channel structure init code to a seperate routine, to save on compile...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 2 Jul 2010 03:06:16 +0000 (03:06 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 2 Jul 2010 03:06:16 +0000 (03:06 +0000)
Demos/Host/Incomplete/BluetoothHost/BluetoothHost.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.c
Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMMControl.h

index 73b709d..2ad3ed5 100644 (file)
@@ -58,12 +58,15 @@ int main(void)
 
        for (;;)
        {
-               Bluetooth_Channel_t* RFCOMMChannel = Bluetooth_GetChannelData(CHANNEL_PSM_RFCOMM, CHANNEL_SEARCH_PSM);
-
-               /* If an RFCOMM channel is open, service the RFCOMM logical channels */
-               if (RFCOMMChannel)
-                 RFCOMM_ServiceChannels(RFCOMMChannel);
-
+               if (Bluetooth_Connection.IsConnected)
+               {
+                       Bluetooth_Channel_t* RFCOMMChannel = Bluetooth_GetChannelData(CHANNEL_PSM_RFCOMM, CHANNEL_SEARCH_PSM);
+
+                       /* If an RFCOMM channel is open, service the RFCOMM logical channels */
+                       if (RFCOMMChannel)
+                         RFCOMM_ServiceChannels(RFCOMMChannel);
+               }
+               
                Bluetooth_Stack_USBTask();
                Bluetooth_Host_Task();
                USB_USBTask();
index 271f2dc..e0446c0 100644 (file)
@@ -129,6 +129,11 @@ void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
        }
 }
 
+RFCOMM_Channel_t* RFCOMM_OpenChannel(Bluetooth_Channel_t* const BluetoothChannel)
+{
+       return NULL;
+}
+
 void RFCOMM_SendChannelSignals(const RFCOMM_Channel_t* const RFCOMMChannel, Bluetooth_Channel_t* const BluetoothChannel)
 {
        BT_RFCOMM_DEBUG(1, ">> MSC Command");
@@ -151,6 +156,33 @@ void RFCOMM_SendChannelSignals(const RFCOMM_Channel_t* const RFCOMMChannel, Blue
        RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, true, RFCOMM_Frame_UIH, sizeof(MSCommand), &MSCommand, BluetoothChannel); 
 }
 
+RFCOMM_Channel_t* RFCOMM_GetFreeChannelEntry(const uint8_t DLCI)
+{
+       /* Find a free entry in the RFCOMM channel multiplexer state array */
+       for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
+       {
+               RFCOMM_Channel_t* RFCOMMChannel = &RFCOMM_Channels[i];
+
+               /* If the channel's state is closed, the channel state entry is free */
+               if (RFCOMMChannel->State == RFCOMM_Channel_Closed)
+               {
+                       RFCOMMChannel->DLCI               = DLCI;
+                       RFCOMMChannel->State              = RFCOMM_Channel_Configure;
+                       RFCOMMChannel->Priority           = 7 + (RFCOMMChannel->DLCI & 0xF8);
+                       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);
+                       RFCOMMChannel->ConfigFlags        = 0;
+                       
+                       return RFCOMMChannel;
+               }
+       }
+       
+       return NULL;
+}
+
 RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI)
 {
        /* Search through the RFCOMM channel list, looking for the specified channel */
@@ -268,32 +300,25 @@ static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluet
        BT_RFCOMM_DEBUG(1, "<< SABM Received");
        BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
 
-       RFCOMM_Channel_t* RFCOMMChannel;
-       
-       if (FrameAddress->DLCI != RFCOMM_CONTROL_DLCI)
+       if (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI)
        {
-               /* 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 state is closed, the channel state entry is free */
-                       if (RFCOMM_Channels[i].State == RFCOMM_Channel_Closed)
-                       {
-                               RFCOMMChannel                     = &RFCOMM_Channels[i];
-                               RFCOMMChannel->DLCI               = FrameAddress->DLCI;
-                               RFCOMMChannel->State              = RFCOMM_Channel_Configure;
-                               RFCOMMChannel->Priority           = 7 + (RFCOMMChannel->DLCI & 0xF8);
-                               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);
-                               RFCOMMChannel->ConfigFlags        = 0;
-                               break;
-                       }
-               }
+               BT_RFCOMM_DEBUG(1, ">> UA Sent");
+               
+               /* Free channel found, or request was to the control channel - accept SABM by sending a UA frame */
+               RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
+
+               return;
        }
+
+       /* Find the existing channel's entry in the channel table */
+       RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(FrameAddress->DLCI);
        
-       if (RFCOMMChannel || (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI))
+       /* Existing entry not found, create a new entry for the channel */
+       if (RFCOMMChannel == NULL)
+         RFCOMMChannel = RFCOMM_GetFreeChannelEntry(FrameAddress->DLCI);
+
+       /* If space was found in the channel table for the new channel, ACK the request */
+       if (RFCOMMChannel != NULL)
        {
                BT_RFCOMM_DEBUG(1, ">> UA Sent");
                
index e7ed7ec..e557858 100644 (file)
                
                void              RFCOMM_SendChannelSignals(const RFCOMM_Channel_t* const RFCOMMChannel,
                                                            Bluetooth_Channel_t* const BluetoothChannel);
-               RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI);
 
+               RFCOMM_Channel_t* RFCOMM_GetFreeChannelEntry(const uint8_t DLCI);
+               RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI);
                uint16_t          RFCOMM_GetVariableFieldValue(const uint8_t** BufferPos);
                void              RFCOMM_SendFrame(const uint8_t DLCI, const bool CommandResponse, const uint8_t Control,
                                                       const uint16_t DataLen, const void* Data, Bluetooth_Channel_t* const Channel);
index 2495bb4..e5bce54 100644 (file)
@@ -195,23 +195,8 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
        /* Check if the channel has no corresponding entry - remote did not open it first */\r
        if (RFCOMMChannel == NULL)\r
        {\r
-               /* Find a free entry in the RFCOMM channel multiplexer state array */\r
-               for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)\r
-               {\r
-                       /* If the channel's state is closed, the channel state entry is free */\r
-                       if (RFCOMM_Channels[i].State == RFCOMM_Channel_Closed)\r
-                       {\r
-                               RFCOMMChannel                     = &RFCOMM_Channels[i];\r
-                               RFCOMMChannel->DLCI               = Params->DLCI;\r
-                               RFCOMMChannel->MTU                = 0xFFFF;\r
-                               RFCOMMChannel->Remote.Signals     = 0 | (1 << 0);\r
-                               RFCOMMChannel->Remote.BreakSignal = 0 | (1 << 0);\r
-                               RFCOMMChannel->Local.Signals      = RFCOMM_SIGNAL_RTC | RFCOMM_SIGNAL_RTR | RFCOMM_SIGNAL_DV | (1 << 0);\r
-                               RFCOMMChannel->Local.BreakSignal  = 0 | (1 << 0);\r
-                               RFCOMMChannel->ConfigFlags        = 0;\r
-                               break;\r
-                       }\r
-               }\r
+               /* Create a new entry in the channel table for the new channel */\r
+               RFCOMMChannel = RFCOMM_GetFreeChannelEntry(Params->DLCI);\r
                \r
                /* No free entry was found, discard the request */\r
                if (RFCOMMChannel == NULL)\r
index 7928d94..a5e3fbe 100644 (file)
@@ -59,6 +59,7 @@
                #define RFCOMM_CONFIG_REMOTESIGNALS    (1 << 0)\r
                #define RFCOMM_CONFIG_LOCALSIGNALS     (1 << 1)\r
                #define RFCOMM_CONFIG_LOCALSIGNALSSENT (1 << 2)\r
+               #define RFCOMM_CONFIG_ABMMODESET       (1 << 3)\r
 \r
        /* Enums: */\r
                enum RFCOMM_Control_Commands_t\r