{
const RFCOMM_Header_t* FrameHeader = (const RFCOMM_Header_t*)Data;
const uint8_t* FrameData = (const uint8_t*)Data + sizeof(RFCOMM_Header_t);
- uint16_t FrameDataLen = RFCOMM_GetFrameDataLength(FrameData);
-
- /* Adjust the frame data pointer to skip over the variable size field */
- FrameData += (FrameDataLen < 128) ? 1 : 2;
+ uint16_t FrameDataLen = RFCOMM_GetVariableFieldValue(&FrameData);
/* Decode the RFCOMM frame type from the header */
switch (FrameHeader->Control & ~FRAME_POLL_FINAL)
RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI)
{
+ /* Search through the RFCOMM channel list, looking for the specified channel */
for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
{
RFCOMM_Channel_t* CurrRFCOMMChannel = &RFCOMM_Channels[i];
- if (CurrRFCOMMChannel->DLCI == DLCI)
+ /* If the current non-closed channel's DLCI matches the search DLCI, return it to the caller */
+ if ((CurrRFCOMMChannel->DLCI == DLCI) && (CurrRFCOMMChannel->State != RFCOMM_Channel_Closed))
return CurrRFCOMMChannel;
}
-
+
+ /* Channel not found in the channel state table, return failure */
return NULL;
}
-uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos)
+uint16_t RFCOMM_GetVariableFieldValue(const uint8_t** BufferPos)
{
- uint8_t FirstOctet = BufferPos[0];
+ uint8_t FirstOctet;
uint8_t SecondOctet = 0;
- if (!(FirstOctet & 0x01))
- SecondOctet = BufferPos[1];
+ FirstOctet = **BufferPos;
+ (*BufferPos)++;
+ /* If the field size is more than a single byte, fetch the next byte in the variable length field */
+ if (!(FirstOctet & 0x01))
+ {
+ SecondOctet = **BufferPos;
+ (*BufferPos)++;
+
+ /* Discard any remaining bytes in the variable length field that won't fit in the return value */
+ while (!(**BufferPos & 0x01))
+ (*BufferPos)++;
+ }
+
+ /* Bitshift the bytes that comprise the variable length field so that they form a single integer */
return (((uint16_t)SecondOctet << 7) | FirstOctet >> 1);
}
/* If the channel's DLCI is zero, the channel state entry is free */
if (!(CurrRFCOMMChannel->DLCI))
{
- CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
- CurrRFCOMMChannel->State = RFCOMM_Channel_Open;
- CurrRFCOMMChannel->Priority = 7 + (CurrRFCOMMChannel->DLCI >> 3) + ((CurrRFCOMMChannel->DLCI >> 3) * 7);
- CurrRFCOMMChannel->UseUIFrames = false;
- CurrRFCOMMChannel->RemoteMTU = 0xFFFF;
+ CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
+ CurrRFCOMMChannel->State = RFCOMM_Channel_Open;
+ CurrRFCOMMChannel->Priority = 7 + (CurrRFCOMMChannel->DLCI >> 3) + ((CurrRFCOMMChannel->DLCI >> 3) * 7);
+ CurrRFCOMMChannel->UseUIFrames = false;
+ CurrRFCOMMChannel->MTU = 0xFFFF;
+ CurrRFCOMMChannel->Signals = 0;
+ CurrRFCOMMChannel->BreakSignals = 0;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
{\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
RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_ModemStatus:\r
- RFCOMM_ProcessMSCommand(CommandHeader, CommandData, Channel);\r
+ RFCOMM_ProcessMSCommand(CommandHeader, CommandDataLen, CommandData, Channel);\r
break;\r
case RFCOMM_Control_RemotePortNegotiation:\r
RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);\r
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_ProcessMSCommand(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
+\r
BT_RFCOMM_DEBUG(1, "<< MS Command");\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
+ /* Save the new channel signals to the channel state structure */\r
+ RFCOMMChannel->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->BreakSignals = Params->BreakSignals;\r
+ \r
+ struct\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};\r
+ MSResponse.Length = (CommandDataLen << 1) | 0x01;\r
+ MSResponse.Params = *Params;\r
+ \r
+ BT_RFCOMM_DEBUG(1, ">> MS Response");\r
+\r
+ /* Send the PDN response to acknowledge the command */\r
+ RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(MSResponse), &MSResponse, Channel);\r
}\r
\r
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
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
/* 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 = &RFCOMM_Channels[i];\r
+ RFCOMMChannel->DLCI = Params->DLCI;\r
+ RFCOMMChannel->Signals = 0;\r
+ RFCOMMChannel->BreakSignals = 0;\r
break;\r
}\r
}\r
RFCOMMChannel->State = RFCOMM_Channel_Open;\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
} 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};\r
+ DPNResponse.Length = (sizeof(DPNResponse.Params) << 1) | 0x01;\r
+ DPNResponse.Params = *Params;\r
+ DPNResponse.Params.ConvergenceLayer = 0x00; // TODO: Enable credit based transaction support\r
\r
BT_RFCOMM_DEBUG(1, ">> DPN Response");\r
\r
#include "RFCOMM.h"\r
\r
/* Macros: */\r
- #define RFCOMM_\r
+ #define RFCOMM_STATUSFLAG_FC (1 << 1)\r
+ #define RFCOMM_STATUSFLAG_RTC (1 << 2)\r
+ #define RFCOMM_STATUSFLAG_RTR (1 << 3)\r
+ #define RFCOMM_STATUSFLAG_IC (1 << 6)\r
+ #define RFCOMM_STATUSFLAG_DV (1 << 7)\r
\r
/* Enums: */\r
enum RFCOMM_Control_Commands_t\r
uint8_t MaxRetransmissions;\r
uint8_t RecoveryWindowSize; \r
} RFCOMM_DPN_Parameters_t;\r
+ \r
+ typedef struct\r
+ {\r
+ RFCOMM_Address_t Channel;\r
+ uint8_t Signals;\r
+ uint8_t BreakSignals;\r
+ } RFCOMM_MS_Parameters_t;\r
\r
/* Function Prototypes: */\r
void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* const Channel);\r
Bluetooth_Channel_t* const Channel);\r
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
Bluetooth_Channel_t* const Channel);\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_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_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
Bluetooth_Channel_t* const Channel);\r
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r