Add missing BT_* prefix to the Bluetooth stack's channel state enum values.
/* 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;
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 */
/* 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];
ChannelData->RemoteNumber = 0;
ChannelData->PSM = PSM;
ChannelData->LocalMTU = MAXIMUM_CHANNEL_MTU;
- ChannelData->State = Channel_WaitConnectRsp;
+ ChannelData->State = BT_Channel_WaitConnectRsp;
struct
{
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
{
/* 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];
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;
}
/* 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;
}
}
{
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;
}
}
{
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;
}
}
}
/* 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);
/* 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.
/** 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. */
{
/* 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)
/* If the channel's DLCI is zero, the channel state entry is free */
if (!(CurrRFCOMMChannel->DLCI))
{
- CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
- CurrRFCOMMChannel->Configured = false;
+ 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;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
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");
}
RFCOMM_Frame_UIH = 0xEF, /**< Unnumbered Information with Header check Field */
};
- /* Type Defines: */
- typedef struct
- {
- unsigned char EA : 1;
- unsigned char CR : 1;
- unsigned char DLCI : 6;
- } RFCOMM_Address_t;
-
- typedef struct
+ enum RFCOMM_Channel_States_t
{
- 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 DLCI;
+ uint8_t State;
+ uint8_t Priority;
+ bool UseUIFrames;
+ uint16_t RemoteMTU;
} RFCOMM_Channel_t;
/* External Variables: */
{\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
\r
switch (CommandHeader->Command)\r
{\r
case RFCOMM_Control_Test:\r
- RFCOMM_ProcessTestCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessTestCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_FlowControlEnable:\r
- RFCOMM_ProcessFCECommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessFCECommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_FlowControlDisable:\r
- RFCOMM_ProcessFCDCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessFCDCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_ModemStatus:\r
- RFCOMM_ProcessMSCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessMSCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_RemotePortNegotiation:\r
- RFCOMM_ProcessRPNCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessRPNCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_RemoteLineStatus:\r
- RFCOMM_ProcessRLSCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessRLSCommand(CommandHeader, CommandData, Channel);\r
break;\r
case RFCOMM_Control_DLCParameterNegotiation:\r
- RFCOMM_ProcessDPNCommand(CommandHeader, CommandData);\r
+ RFCOMM_ProcessDPNCommand(CommandHeader, CommandData, Channel);\r
break;\r
default:\r
BT_RFCOMM_DEBUG(1, "<< Unknown Command"); \r
}\r
}\r
\r
-static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< TEST Command");\r
}\r
\r
-static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< FCE Command");\r
}\r
\r
-static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< FCD Command");\r
}\r
\r
-static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< MS Command");\r
}\r
\r
-static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< RPN Command");\r
}\r
\r
-static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\r
BT_RFCOMM_DEBUG(1, "<< RLS Command");\r
}\r
\r
-static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)\r
+static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel)\r
{\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
\r
- /* Skip over the first data byte (Length field) since its size and value are fixed */\r
- RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(CommandData[2]);\r
+ /* Ignore parameter negotiations to the control channel */\r
+ if (Params->DLCI == RFCOMM_CONTROL_DLCI)\r
+ return;\r
\r
+ /* Retrieve existing channel configuration data, if already opened */\r
+ RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(Params->DLCI);\r
+ \r
+ /* Check if the channel has no corresponding entry - remote did not open it first */\r
if (RFCOMMChannel == NULL)\r
{\r
- // TODO: Channel does not exist - create it\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
+ \r
+ /* No free entry was found, discard the request */\r
+ if (RFCOMMChannel == NULL)\r
+ {\r
+ BT_RFCOMM_DEBUG(2, "-- No Free Channel");\r
+ return;\r
+ }\r
}\r
\r
- RFCOMMChannel->Configured = true;\r
+ /* Save the new channel configuration */\r
+ RFCOMMChannel->State = RFCOMM_Channel_Open;\r
+ RFCOMMChannel->Priority = Params->Priority;\r
+ RFCOMMChannel->UseUIFrames = (Params->FrameType != 0);\r
+ RFCOMMChannel->RemoteMTU = Params->MaximumFrameSize;\r
\r
- // TODO: Read in channel data, ACK request\r
+ struct\r
+ {\r
+ RFCOMM_Command_t CommandHeader;\r
+ uint8_t Length;\r
+ RFCOMM_DPN_Parameters_t Params;\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
+ \r
+ BT_RFCOMM_DEBUG(1, ">> DPN Response");\r
+\r
+ /* Send the PDN response to acknowledge the command */\r
+ RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(DPNResponse), &DPNResponse, Channel);\r
}\r
\r
#include "BluetoothStack.h"\r
#include "RFCOMM.h"\r
+ \r
+ /* Macros: */\r
+ #define RFCOMM_\r
\r
/* Enums: */\r
enum RFCOMM_Control_Commands_t\r
/* Type Defines: */\r
typedef struct\r
{\r
+ unsigned char EA : 1;\r
+ unsigned char CR : 1;\r
+ unsigned char DLCI : 6;\r
+ } RFCOMM_Address_t;\r
+\r
+ typedef struct\r
+ {\r
+ RFCOMM_Address_t Address;\r
+ uint8_t Control;\r
+ } RFCOMM_Header_t;\r
+\r
+ typedef struct\r
+ {\r
unsigned char EA : 1;\r
unsigned char CR : 1;\r
unsigned char Command : 6;\r
} RFCOMM_Command_t;\r
+ \r
+ typedef struct\r
+ {\r
+ uint8_t DLCI;\r
+ unsigned char FrameType : 4;\r
+ unsigned char ConvergenceLayer : 4;\r
+ uint8_t Priority;\r
+ uint8_t ACKTimerTicks;\r
+ uint16_t MaximumFrameSize;\r
+ uint8_t MaxRetransmissions;\r
+ uint8_t RecoveryWindowSize; \r
+ } RFCOMM_DPN_Parameters_t;\r
\r
/* Function Prototypes: */\r
void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* const Channel);\r
\r
#if defined(INCLUDE_FROM_RFCOMM_CONTROL_C)\r
- static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\r
- static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData);\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_ProcessFCECommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\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_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
+ Bluetooth_Channel_t* const Channel);\r
+ static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* const CommandHeader, const uint8_t* CommandData,\r
+ Bluetooth_Channel_t* const Channel);\r
#endif\r
\r
#endif\r