Improve commenting of the Dataflash stub board driver file, to prevent confusion...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / Lib / RFCOMMControl.c
index 23a1876..e5bce54 100644 (file)
@@ -42,15 +42,12 @@ void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* c
 {\r
        const RFCOMM_Command_t* CommandHeader  = (const RFCOMM_Command_t*)Command;\r
        const uint8_t*          CommandData    = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);\r
-       uint16_t                ControlDataLen = RFCOMM_GetFrameDataLength(CommandData);\r
-\r
-       /* Adjust the command data pointer to skip over the variable size field */\r
-       CommandData += (ControlDataLen < 128) ? 1 : 2;\r
+       uint8_t                 CommandDataLen = RFCOMM_GetVariableFieldValue(&CommandData);\r
 \r
        switch (CommandHeader->Command)\r
        {\r
                case RFCOMM_Control_Test:\r
-                       RFCOMM_ProcessTestCommand(CommandHeader, CommandData, Channel);\r
+                       RFCOMM_ProcessTestCommand(CommandHeader, CommandDataLen, CommandData, Channel);\r
                        break;\r
                case RFCOMM_Control_FlowControlEnable:\r
                        RFCOMM_ProcessFCECommand(CommandHeader, CommandData, Channel);\r
@@ -59,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, CommandData, Channel);\r
+                       RFCOMM_ProcessMSCCommand(CommandHeader, CommandDataLen, CommandData, Channel);\r
                        break;\r
                case RFCOMM_Control_RemotePortNegotiation:\r
                        RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);\r
@@ -76,10 +73,29 @@ void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* c
        }\r
 }\r
 \r
-static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
-                                                 Bluetooth_Channel_t* const Channel)\r
+static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t CommandDataLen,\r
+                                      const uint8_t* CommandData, Bluetooth_Channel_t* const Channel)\r
 {\r
+       const uint8_t* Params = (const uint8_t*)CommandData;\r
+\r
        BT_RFCOMM_DEBUG(1, "<< TEST Command");\r
+       \r
+       struct\r
+       {\r
+               RFCOMM_Command_t CommandHeader;\r
+               uint8_t          Length;\r
+               uint8_t          TestData[CommandDataLen];\r
+       } TestResponse;\r
+\r
+       /* Fill out the Test response data */\r
+       TestResponse.CommandHeader = (RFCOMM_Command_t){.Command = RFCOMM_Control_Test, .EA = true, .CR = false};\r
+       TestResponse.Length        = (CommandDataLen << 1) | 0x01;\r
+       memcpy(TestResponse.TestData, Params, CommandDataLen);\r
+       \r
+       BT_RFCOMM_DEBUG(1, ">> TEST Response");\r
+\r
+       /* Send the PDN response to acknowledge the command */\r
+       RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(TestResponse), &TestResponse, Channel);\r
 }\r
 \r
 static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
@@ -94,10 +110,59 @@ 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* CommandData,\r
-                                               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
-       BT_RFCOMM_DEBUG(1, "<< MS Command");\r
+       const RFCOMM_MSC_Parameters_t* Params = (const RFCOMM_MSC_Parameters_t*)CommandData;\r
+\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
+       if (Params->Channel.DLCI == RFCOMM_CONTROL_DLCI)\r
+         return;\r
+       \r
+       /* Retrieve existing channel configuration data, if already opened */\r
+       RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->Channel.DLCI);  \r
+       \r
+       /* If the channel does not exist, abort */\r
+       if (RFCOMMChannel == NULL)\r
+         return;\r
+\r
+       /* Check if the MSC packet is a command or a response */\r
+       if (CommandHeader->CR)\r
+       {\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
+               /* 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
@@ -118,7 +183,7 @@ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader
        const RFCOMM_DPN_Parameters_t* Params = (const RFCOMM_DPN_Parameters_t*)CommandData;\r
 \r
        BT_RFCOMM_DEBUG(1, "<< DPN Command");\r
-       BT_RFCOMM_DEBUG(2, "-- Config DLCI: 0x%02X", Params->DLCI);\r
+       BT_RFCOMM_DEBUG(2, "-- DLCI: 0x%02X", Params->DLCI);\r
        \r
        /* Ignore parameter negotiations to the control channel */\r
        if (Params->DLCI == RFCOMM_CONTROL_DLCI)\r
@@ -130,17 +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
-                               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
@@ -151,10 +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->State       = RFCOMM_Channel_Configure;\r
        RFCOMMChannel->Priority    = Params->Priority;\r
-       RFCOMMChannel->UseUIFrames = (Params->FrameType != 0);\r
-       RFCOMMChannel->RemoteMTU   = Params->MaximumFrameSize;\r
+       RFCOMMChannel->MTU         = Params->MaximumFrameSize;\r
        \r
        struct\r
        {\r
@@ -164,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.Command = CommandHeader->Command;\r
-       DPNResponse.CommandHeader.EA      = true;\r
-       DPNResponse.Length                = (sizeof(DPNResponse.Params) << 1) | 0x01;\r
-       DPNResponse.Params                = *Params;\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