Commit 3125fd5f authored by Dean Camera's avatar Dean Camera
Browse files

Add RFCOMM channel configuration parsing and channel states to the incomplete BluetoothHost demo.

Add missing BT_* prefix to the Bluetooth stack's channel state enum values.
parent dcedde01
......@@ -62,11 +62,11 @@ void Bluetooth_ACLTask(void)
/* Check if we are in a channel state which requires a configuration request to be sent */
switch (ChannelData->State)
{
case Channel_Config_WaitConfig:
ChannelData->State = Channel_Config_WaitReqResp;
case BT_Channel_Config_WaitConfig:
ChannelData->State = BT_Channel_Config_WaitReqResp;
break;
case Channel_Config_WaitSendConfig:
ChannelData->State = Channel_Config_WaitResp;
case BT_Channel_Config_WaitSendConfig:
ChannelData->State = BT_Channel_Config_WaitResp;
break;
default:
MustSendConfigReq = false;
......@@ -224,7 +224,7 @@ uint8_t Bluetooth_SendPacket(void* Data, const uint16_t DataLen, Bluetooth_Chann
return BT_SENDPACKET_NotConnected;
/* If the destination channel is not the signalling channel and it is not currently fully open, abort */
if ((Channel != NULL) && (Channel->State != Channel_Open))
if ((Channel != NULL) && (Channel->State != BT_Channel_Open))
return BT_SENDPACKET_ChannelNotOpen;
/* Fill out the packet's header from the remote device connection information structure */
......@@ -274,7 +274,7 @@ Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)
/* Search through the channel information list for a free channel item */
for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
{
if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
if (Bluetooth_Connection.Channels[i].State == BT_Channel_Closed)
{
ChannelData = &Bluetooth_Connection.Channels[i];
......@@ -293,7 +293,7 @@ Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)
ChannelData->RemoteNumber = 0;
ChannelData->PSM = PSM;
ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
ChannelData->State = Channel_WaitConnectRsp;
ChannelData->State = BT_Channel_WaitConnectRsp;
struct
{
......@@ -332,11 +332,11 @@ Bluetooth_Channel_t* Bluetooth_OpenChannel(const uint16_t PSM)
void Bluetooth_CloseChannel(Bluetooth_Channel_t* const Channel)
{
/* Don't try to close a non-existing or already closed channel */
if ((Channel == NULL) || (Channel->State == Channel_Closed))
if ((Channel == NULL) || (Channel->State == BT_Channel_Closed))
return;
/* Set the channel's state to the start of the teardown process */
Channel->State = Channel_WaitDisconnect;
Channel->State = BT_Channel_WaitDisconnect;
struct
{
......@@ -386,7 +386,7 @@ static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* cons
/* Look through the channel information list for a free entry */
for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
{
if (Bluetooth_Connection.Channels[i].State == Channel_Closed)
if (Bluetooth_Connection.Channels[i].State == BT_Channel_Closed)
{
ChannelData = &Bluetooth_Connection.Channels[i];
......@@ -409,7 +409,7 @@ static inline void Bluetooth_Signal_ConnectionReq(const BT_Signal_Header_t* cons
ChannelData->RemoteNumber = ConnectionRequest.SourceChannel;
ChannelData->PSM = ConnectionRequest.PSM;
ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
ChannelData->State = Channel_Config_WaitConfig;
ChannelData->State = BT_Channel_Config_WaitConfig;
ChannelStatus = BT_CONNECTION_SUCCESSFUL;
}
......@@ -471,7 +471,7 @@ static inline void Bluetooth_Signal_ConnectionResp(const BT_Signal_Header_t* con
/* Set the channel structure's remote channel number to the channel allocated on the remote device */
ChannelData->RemoteNumber = ConnectionResponse.SourceChannel;
ChannelData->State = (ConnectionResponse.Result == BT_CONNECTION_SUCCESSFUL) ?
Channel_Config_WaitConfig : Channel_Closed;
BT_Channel_Config_WaitConfig : BT_Channel_Closed;
}
}
......@@ -544,14 +544,14 @@ static inline void Bluetooth_Signal_ConfigurationReq(const BT_Signal_Header_t* c
{
switch (ChannelData->State)
{
case Channel_Config_WaitConfig:
ChannelData->State = Channel_Config_WaitSendConfig;
case BT_Channel_Config_WaitConfig:
ChannelData->State = BT_Channel_Config_WaitSendConfig;
break;
case Channel_Config_WaitReqResp:
ChannelData->State = Channel_Config_WaitResp;
case BT_Channel_Config_WaitReqResp:
ChannelData->State = BT_Channel_Config_WaitResp;
break;
case Channel_Config_WaitReq:
ChannelData->State = Channel_Open;
case BT_Channel_Config_WaitReq:
ChannelData->State = BT_Channel_Open;
break;
}
}
......@@ -589,18 +589,18 @@ static inline void Bluetooth_Signal_ConfigurationResp(const BT_Signal_Header_t*
{
switch (ChannelData->State)
{
case Channel_Config_WaitReqResp:
ChannelData->State = Channel_Config_WaitReq;
case BT_Channel_Config_WaitReqResp:
ChannelData->State = BT_Channel_Config_WaitReq;
break;
case Channel_Config_WaitResp:
ChannelData->State = Channel_Open;
case BT_Channel_Config_WaitResp:
ChannelData->State = BT_Channel_Open;
break;
}
}
else
{
/* Configuration failed - close the channel */
ChannelData->State = Channel_Closed;
ChannelData->State = BT_Channel_Closed;
}
}
}
......@@ -644,7 +644,7 @@ static inline void Bluetooth_Signal_DisconnectionReq(const BT_Signal_Header_t* c
/* If the channel was found in the channel list, close it */
if (ChannelData != NULL)
ChannelData->State = Channel_Closed;
ChannelData->State = BT_Channel_Closed;
BT_ACL_DEBUG(1, ">> L2CAP Disconnection Response");
BT_ACL_DEBUG(2, "-- Source Channel: 0x%04X", ResponsePacket.DisconnectionResponse.SourceChannel);
......@@ -673,7 +673,7 @@ static inline void Bluetooth_Signal_DisconnectionResp(const BT_Signal_Header_t*
/* If the channel was found in the channel list, close it */
if (ChannelData != NULL)
ChannelData->State = Channel_Closed;
ChannelData->State = BT_Channel_Closed;
}
/** Internal Bluetooth stack Signal Command processing routine for an Echo Request command.
......
......@@ -66,19 +66,22 @@
/** Enum for the possible states for a Bluetooth ACL channel. */
enum BT_ChannelStates_t
{
Channel_Closed = 0, /**< Channel is closed and inactive. No data may be sent or received. */
Channel_WaitConnect = 1, /**< A connection request has been received, but a response has not been sent. */
Channel_WaitConnectRsp = 2, /**< A connection request has been sent, but a response has not been received. */
Channel_Config_WaitConfig = 3, /**< Channel has been connected, but not yet configured on either end. */
Channel_Config_WaitSendConfig = 4, /**< Channel configuration has been received and accepted, but not yet sent. */
Channel_Config_WaitReqResp = 5, /**< Channel configuration has been sent but not responded to, and a configuration
request from the remote end has not yet been received. */
Channel_Config_WaitResp = 6, /**< Channel configuration has been sent but not accepted, but a configuration request
from the remote end has been accepted. */
Channel_Config_WaitReq = 7, /**< Channel configuration has been sent and accepted, but a configuration request
from the remote end has not yet been accepted. */
Channel_Open = 8, /**< Channel is open and ready to send or receive data */
Channel_WaitDisconnect = 9, /**< A disconnection request has been sent, but not yet acknowledged. */
BT_Channel_Closed = 0, /**< Channel is closed and inactive. No data may be sent or received. */
BT_Channel_WaitConnect = 1, /**< A connection request has been received, but a response has not been sent. */
BT_Channel_WaitConnectRsp = 2, /**< A connection request has been sent, but a response has not been received. */
BT_Channel_Config_WaitConfig = 3, /**< Channel has been connected, but not yet configured on either end. */
BT_Channel_Config_WaitSendConfig = 4, /**< Channel configuration has been received and accepted, but not yet sent. */
BT_Channel_Config_WaitReqResp = 5, /**< Channel configuration has been sent but not responded to, and a configuration
* request from the remote end has not yet been received.
*/
BT_Channel_Config_WaitResp = 6, /**< Channel configuration has been sent but not accepted, but a configuration request
* from the remote end has been accepted.
*/
BT_Channel_Config_WaitReq = 7, /**< Channel configuration has been sent and accepted, but a configuration request
* from the remote end has not yet been accepted.
*/
BT_Channel_Open = 8, /**< Channel is open and ready to send or receive data */
BT_Channel_WaitDisconnect = 9, /**< A disconnection request has been sent, but not yet acknowledged. */
};
/** Enum for the possible error codes returned by the \ref Bluetooth_SendPacket() function. */
......
......@@ -70,7 +70,7 @@ void RFCOMM_Initialize(void)
{
/* Reset the RFCOMM channel structures, to invalidate any confiured RFCOMM channels */
for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
RFCOMM_Channels[i].DLCI = 0x00;
RFCOMM_Channels[i].State = RFCOMM_Channel_Closed;
}
void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
......@@ -221,7 +221,10 @@ static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluet
if (!(CurrRFCOMMChannel->DLCI))
{
CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
CurrRFCOMMChannel->Configured = false;
CurrRFCOMMChannel->State = RFCOMM_Channel_Open;
CurrRFCOMMChannel->Priority = 7 + (CurrRFCOMMChannel->DLCI >> 3) + ((CurrRFCOMMChannel->DLCI >> 3) * 7);
CurrRFCOMMChannel->UseUIFrames = false;
CurrRFCOMMChannel->RemoteMTU = 0xFFFF;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
......@@ -244,15 +247,20 @@ static void RFCOMM_ProcessUA(const RFCOMM_Address_t* const FrameAddress, Bluetoo
static void RFCOMM_ProcessUIH(const RFCOMM_Address_t* const FrameAddress, const uint16_t FrameLength,
const uint8_t* FrameData, Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< UIH Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
BT_RFCOMM_DEBUG(2, "-- Length 0x%02X", FrameLength);
if (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI)
{
RFCOMM_ProcessControlCommand(FrameData, Channel);
return;
}
// TODO: Handle regular channel data here
BT_RFCOMM_DEBUG(1, "<< UIH Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
BT_RFCOMM_DEBUG(2, "-- Length 0x%02X", FrameLength);
puts("RFCOMM Data: ");
for (uint8_t i = 0; i < FrameLength; i++)
printf("0x%02X (%c) ", FrameData[i], FrameData[i]);
printf("\r\n");
}
......@@ -69,24 +69,24 @@
RFCOMM_Frame_UIH = 0xEF, /**< Unnumbered Information with Header check Field */
};
/* Type Defines: */
typedef struct
enum RFCOMM_Channel_States_t
{
unsigned char EA : 1;
unsigned char CR : 1;
unsigned char DLCI : 6;
} RFCOMM_Address_t;
typedef struct
{
RFCOMM_Address_t Address;
uint8_t Control;
} RFCOMM_Header_t;
RFCOMM_Channel_Closed = 0,
RFCOMM_Channel_Create = 1,
RFCOMM_Channel_Creating = 2,
RFCOMM_Channel_Configure = 3,
RFCOMM_Channel_Configuring = 4,
RFCOMM_Channel_Open = 5,
};
/* Type Defines: */
typedef struct
{
uint8_t DLCI;
bool Configured;
uint8_t State;
uint8_t Priority;
bool UseUIFrames;
uint16_t RemoteMTU;
} RFCOMM_Channel_t;
/* External Variables: */
......
......@@ -42,29 +42,33 @@ void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* c
{
const RFCOMM_Command_t* CommandHeader = (const RFCOMM_Command_t*)Command;
const uint8_t* CommandData = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);
uint16_t ControlDataLen = RFCOMM_GetFrameDataLength(CommandData);
/* Adjust the command data pointer to skip over the variable size field */
CommandData += (ControlDataLen < 128) ? 1 : 2;
switch (CommandHeader->Command)
{
case RFCOMM_Control_Test:
RFCOMM_ProcessTestCommand(CommandHeader, CommandData);
RFCOMM_ProcessTestCommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_FlowControlEnable:
RFCOMM_ProcessFCECommand(CommandHeader, CommandData);
RFCOMM_ProcessFCECommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_FlowControlDisable:
RFCOMM_ProcessFCDCommand(CommandHeader, CommandData);
RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_ModemStatus:
RFCOMM_ProcessMSCommand(CommandHeader, CommandData);
RFCOMM_ProcessMSCommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_RemotePortNegotiation:
RFCOMM_ProcessRPNCommand(CommandHeader, CommandData);
RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_RemoteLineStatus:
RFCOMM_ProcessRLSCommand(CommandHeader, CommandData);
RFCOMM_ProcessRLSCommand(CommandHeader, CommandData, Channel);
break;
case RFCOMM_Control_DLCParameterNegotiation:
RFCOMM_ProcessDPNCommand(CommandHeader, CommandData);
RFCOMM_ProcessDPNCommand(CommandHeader, CommandData, Channel);
break;
default:
BT_RFCOMM_DEBUG(1, "<< Unknown Command");
......@@ -72,49 +76,101 @@ void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* c
}
}
static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< TEST Command");
}
static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< FCE Command");
}
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< FCD Command");
}
static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< MS Command");
}
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< RPN Command");
}
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< RLS Command");
}
static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel)
{
const RFCOMM_DPN_Parameters_t* Params = (const RFCOMM_DPN_Parameters_t*)CommandData;
BT_RFCOMM_DEBUG(1, "<< DPN Command");
BT_RFCOMM_DEBUG(2, "-- Config DLCI: 0x%02X", Params->DLCI);
/* Ignore parameter negotiations to the control channel */
if (Params->DLCI == RFCOMM_CONTROL_DLCI)
return;
/* Retrieve existing channel configuration data, if already opened */
RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->DLCI);
/* Skip over the first data byte (Length field) since its size and value are fixed */
RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(CommandData[2]);
/* Check if the channel has no corresponding entry - remote did not open it first */
if (RFCOMMChannel == NULL)
{
/* 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 DLCI is zero, the channel state entry is free */
if (!(RFCOMM_Channels[i].DLCI))
{
RFCOMMChannel = &RFCOMM_Channels[i];
RFCOMMChannel->DLCI = Params->DLCI;
break;
}
}
/* No free entry was found, discard the request */
if (RFCOMMChannel == NULL)
{
// TODO: Channel does not exist - create it
BT_RFCOMM_DEBUG(2, "-- No Free Channel");
return;
}
}
RFCOMMChannel->Configured = true;
/* Save the new channel configuration */
RFCOMMChannel->State = RFCOMM_Channel_Open;
RFCOMMChannel->Priority = Params->Priority;
RFCOMMChannel->UseUIFrames = (Params->FrameType != 0);
RFCOMMChannel->RemoteMTU = Params->MaximumFrameSize;
struct
{
RFCOMM_Command_t CommandHeader;
uint8_t Length;
RFCOMM_DPN_Parameters_t Params;
} DPNResponse;
/* Fill out the DPN response data */
DPNResponse.CommandHeader.Command = CommandHeader->Command;
DPNResponse.CommandHeader.EA = true;
DPNResponse.Length = (sizeof(DPNResponse.Params) << 1) | 0x01;
DPNResponse.Params = *Params;
BT_RFCOMM_DEBUG(1, ">> DPN Response");
// TODO: Read in channel data, ACK request
/* Send the PDN response to acknowledge the command */
RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);
}
......@@ -49,6 +49,9 @@
#include "BluetoothStack.h"
#include "RFCOMM.h"
/* Macros: */
#define RFCOMM_
/* Enums: */
enum RFCOMM_Control_Commands_t
{
......@@ -63,6 +66,19 @@
};
/* Type Defines: */
typedef struct
{
unsigned char EA : 1;
unsigned char CR : 1;
unsigned char DLCI : 6;
} RFCOMM_Address_t;
typedef struct
{
RFCOMM_Address_t Address;
uint8_t Control;
} RFCOMM_Header_t;
typedef struct
{
unsigned char EA : 1;
......@@ -70,17 +86,36 @@
unsigned char Command : 6;
} RFCOMM_Command_t;
typedef struct
{
uint8_t DLCI;
unsigned char FrameType : 4;
unsigned char ConvergenceLayer : 4;
uint8_t Priority;
uint8_t ACKTimerTicks;
uint16_t MaximumFrameSize;
uint8_t MaxRetransmissions;
uint8_t RecoveryWindowSize;
} RFCOMM_DPN_Parameters_t;
/* Function Prototypes: */
void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* const Channel);
#if defined(INCLUDE_FROM_RFCOMM_CONTROL_C)
static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);
static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,
Bluetooth_Channel_t* const Channel);
#endif
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment