Improve commenting of the Dataflash stub board driver file, to prevent confusion...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / RFCOMMControl.c
index 01fab78..e5bce54 100644 (file)
@@ -56,7 +56,7 @@ void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* c
                        RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);\r
                        break;\r
                case RFCOMM_Control_ModemStatus:\r
-                       RFCOMM_ProcessMSCommand(CommandHeader, CommandDataLen, CommandData, Channel);\r
+                       RFCOMM_ProcessMSCCommand(CommandHeader, CommandDataLen, CommandData, Channel);\r
                        break;\r
                case RFCOMM_Control_RemotePortNegotiation:\r
                        RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);\r
@@ -110,12 +110,12 @@ static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader
        BT_RFCOMM_DEBUG(1, "<< FCD Command");\r
 }\r
 \r
-static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t CommandDataLen,\r
-                                    const uint8_t* CommandData, Bluetooth_Channel_t* const Channel)\r
+static void RFCOMM_ProcessMSCCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t CommandDataLen,\r
+                                     const uint8_t* CommandData, Bluetooth_Channel_t* const Channel)\r
 {\r
-       const RFCOMM_MS_Parameters_t* Params = (const RFCOMM_MS_Parameters_t*)CommandData;\r
+       const RFCOMM_MSC_Parameters_t* Params = (const RFCOMM_MSC_Parameters_t*)CommandData;\r
 \r
-       BT_RFCOMM_DEBUG(1, "<< MS Command");\r
+       BT_RFCOMM_DEBUG(1, "<< MSC %s", (CommandHeader->CR) ? "Command" : "Response");\r
        BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->Channel.DLCI);\r
        \r
        /* Ignore status flags sent to the control channel */\r
@@ -128,31 +128,41 @@ static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader,
        /* If the channel does not exist, abort */\r
        if (RFCOMMChannel == NULL)\r
          return;\r
-         \r
-       /* Save the new channel signals to the channel state structure */\r
-       RFCOMMChannel->Remote.Signals = Params->Signals;\r
-       \r
-       /* If the command contains the optional break signals field, store the value */\r
-       if (CommandDataLen == sizeof(RFCOMM_MS_Parameters_t))\r
-         RFCOMMChannel->Remote.BreakSignal = Params->BreakSignal;\r
-         \r
-       struct\r
+\r
+       /* Check if the MSC packet is a command or a response */\r
+       if (CommandHeader->CR)\r
        {\r
-               RFCOMM_Command_t       CommandHeader;\r
-               uint8_t                Length;\r
-               RFCOMM_MS_Parameters_t Params;\r
-       } MSResponse;\r
-\r
-       /* Fill out the MS response data */\r
-       MSResponse.CommandHeader  = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true, .CR = false};\r
-       MSResponse.Length         = (CommandDataLen << 1) | 0x01;\r
-       memcpy(&MSResponse.Params, Params, sizeof(RFCOMM_MS_Parameters_t));\r
-       \r
-       BT_RFCOMM_DEBUG(1, ">> MS Response");\r
+               /* Save the new channel signals to the channel state structure */\r
+               RFCOMMChannel->Remote.Signals  = Params->Signals;       \r
+               RFCOMMChannel->ConfigFlags    |= RFCOMM_CONFIG_REMOTESIGNALS;\r
+               \r
+               /* If the command contains the optional break signals field, store the value */\r
+               if (CommandDataLen == sizeof(RFCOMM_MSC_Parameters_t))\r
+                 RFCOMMChannel->Remote.BreakSignal = Params->BreakSignal;\r
+                 \r
+               struct\r
+               {\r
+                       RFCOMM_Command_t        CommandHeader;\r
+                       uint8_t                 Length;\r
+                       RFCOMM_MSC_Parameters_t Params;\r
+               } MSResponse;\r
 \r
-       /* Send the PDN response to acknowledge the command */\r
-       RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH,\r
-                        (sizeof(MSResponse) - sizeof(MSResponse.Params) + CommandDataLen), &MSResponse, Channel);\r
+               /* Fill out the MS response data */\r
+               MSResponse.CommandHeader  = (RFCOMM_Command_t){.Command = RFCOMM_Control_ModemStatus, .EA = true, .CR = false};\r
+               MSResponse.Length         = (CommandDataLen << 1) | 0x01;\r
+               memcpy(&MSResponse.Params, Params, sizeof(RFCOMM_MSC_Parameters_t));\r
+\r
+               BT_RFCOMM_DEBUG(1, ">> MSC Response");\r
+\r
+               /* Send the MSC response to acknowledge the command */\r
+               RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH,\r
+                                                (sizeof(MSResponse) - sizeof(MSResponse.Params) + CommandDataLen), &MSResponse, Channel);\r
+       }\r
+       else\r
+       {\r
+               /* Indicate that the remote device has acknowledged the sent signals */\r
+               RFCOMMChannel->ConfigFlags |= RFCOMM_CONFIG_LOCALSIGNALS;\r
+       }       \r
 }\r
 \r
 static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
@@ -185,22 +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 DLCI is zero, the channel state entry is free */\r
-                       if (!(RFCOMM_Channels[i].DLCI))\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
-                               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
@@ -211,9 +207,9 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
        }\r
        \r
        /* Save the new channel configuration */\r
-       RFCOMMChannel->State    = RFCOMM_Channel_Open;\r
-       RFCOMMChannel->Priority = Params->Priority;\r
-       RFCOMMChannel->MTU      = Params->MaximumFrameSize;\r
+       RFCOMMChannel->State       = RFCOMM_Channel_Configure;\r
+       RFCOMMChannel->Priority    = Params->Priority;\r
+       RFCOMMChannel->MTU         = Params->MaximumFrameSize;\r
        \r
        struct\r
        {\r
@@ -223,13 +219,13 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
        } DPNResponse;\r
        \r
        /* Fill out the DPN response data */\r
-       DPNResponse.CommandHeader           = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true, .CR = false};\r
+       DPNResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_DLCParameterNegotiation, .EA = true, .CR = false};\r
        DPNResponse.Length                  = (sizeof(DPNResponse.Params) << 1) | 0x01;\r
        memcpy(&DPNResponse.Params, Params, sizeof(RFCOMM_DPN_Parameters_t));\r
        DPNResponse.Params.ConvergenceLayer = 0x00; // TODO: Enable credit based transaction support\r
        \r
        BT_RFCOMM_DEBUG(1, ">> DPN Response");\r
 \r
-       /* Send the PDN response to acknowledge the command */\r
+       /* Send the DPN response to acknowledge the command */\r
        RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);\r
 }\r