More RFCOMM layer implementation - fix FCS calculation, add in start of control channel command processing.

pull/1469/head
Dean Camera 15 years ago
parent 34e0fed4ca
commit f92229697f

@ -74,18 +74,18 @@ void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
/* Decode the RFCOMM frame type from the header */
switch (FrameHeader->Control & ~FRAME_POLL_FINAL)
{
case RFCOMM_Frame_SABM:
RFCOMM_ProcessSABM(FrameHeader, Channel);
break;
case RFCOMM_Frame_UA:
RFCOMM_ProcessUA(FrameHeader, Channel);
break;
case RFCOMM_Frame_DM:
RFCOMM_ProcessDM(FrameHeader, Channel);
break;
case RFCOMM_Frame_DISC:
RFCOMM_ProcessDISC(FrameHeader, Channel);
break;
case RFCOMM_Frame_SABM:
RFCOMM_ProcessSABM(FrameHeader, Channel);
break;
case RFCOMM_Frame_UA:
RFCOMM_ProcessUA(FrameHeader, Channel);
break;
case RFCOMM_Frame_UIH:
RFCOMM_ProcessUIH(FrameHeader, Channel);
break;
@ -95,47 +95,55 @@ void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel)
}
}
static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< SABM Received");
BT_RFCOMM_DEBUG(1, "<< DM Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI);
}
// TODO: Reset channel send/receive state here
static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
BT_RFCOMM_DEBUG(1, "<< DISC Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI);
// TODO: Close down connection
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameHeader->Address.DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
}
static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));
BT_RFCOMM_DEBUG(1, "<< UA Received");
BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);
}
BT_RFCOMM_DEBUG(1, "<< SABM Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI);
static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));
// TODO: Reset channel send/receive state
BT_RFCOMM_DEBUG(1, "<< DM Received");
BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameHeader->Address.DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
}
static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));
BT_RFCOMM_DEBUG(1, "<< DISC Received");
BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);
BT_RFCOMM_DEBUG(1, "<< UA Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI);
}
static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel)
{
const uint8_t* CurrBufferPos = ((const uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t));
BT_RFCOMM_DEBUG(1, "<< UIH Received");
BT_RFCOMM_DEBUG(2, "-- Address 0x%02X", FrameHeader->Address);
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI);
uint8_t* FrameData = (uint8_t*)FrameHeader + sizeof(RFCOMM_Header_t);
uint16_t FrameDataLen = RFCOMM_GetFrameDataLength(FrameData);
FrameData += (FrameDataLen < 128) ? 1 : 2;
BT_RFCOMM_DEBUG(2, "-- Length 0x%02X", FrameDataLen);
if (FrameHeader->Address.DLCI == RFCOMM_CONTROL_DLCI)
{
// TODO: Process control command
}
}
static void RFCOMM_SendFrame(const uint8_t DLCI, const bool CommandResponse, const uint8_t Control, const uint16_t DataLen, const uint8_t* Data,
@ -170,38 +178,38 @@ static void RFCOMM_SendFrame(const uint8_t DLCI, const bool CommandResponse, con
/* Copy over the packet data from the source buffer to the response packet buffer */
memcpy(ResponsePacket.Data, Data, DataLen);
/* Determine the length of the frame which is to be used to calculate the CRC value */
uint8_t CRCLength = sizeof(ResponsePacket.FrameHeader);
if ((Control & ~FRAME_POLL_FINAL) != RFCOMM_Frame_UIH)
CRCLength += sizeof(ResponsePacket.Size);
/* Calculate the frame checksum from the appropriate fields */
if ((Control & ~FRAME_POLL_FINAL) == RFCOMM_Frame_UIH)
ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, sizeof(ResponsePacket.FrameHeader));
else
ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, sizeof(ResponsePacket.FrameHeader) + sizeof(ResponsePacket.Size));
ResponsePacket.FCS = RFCOMM_GetFCSValue(&ResponsePacket, CRCLength);
/* Send the completed response packet to the sender */
Bluetooth_SendPacket(&ResponsePacket, sizeof(ResponsePacket), Channel);
}
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length)
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length)
{
const uint8_t* CurrPos = FrameStart;
uint8_t FCS = 0xFF;
while (Length--)
{
FCS = pgm_read_byte(&CRC8_Table[FCS ^ *CurrPos]);
CurrPos++;
}
for (uint8_t i = 0; i < Length; i++)
FCS = pgm_read_byte(&CRC8_Table[FCS ^ *(CurrPos++)]);
return (0xFF - FCS);
return ~FCS;
}
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t** BufferPos)
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos)
{
uint8_t FirstOctet = *((*BufferPos)++);
uint8_t FirstOctet = BufferPos[0];
uint8_t SecondOctet = 0;
if (!(FirstOctet & 0x01))
SecondOctet = *((*BufferPos)++);
SecondOctet = BufferPos[1];
return (((uint16_t)SecondOctet << 7) | (FirstOctet >> 1));
return (((uint16_t)SecondOctet << 7) | FirstOctet >> 1);
}

@ -54,17 +54,31 @@
#define FRAME_POLL_FINAL (1 << 4)
#define RFCOMM_CONTROL_DLCI 0
/* Enums: */
/** Enum for the types of RFCOMM frames which can be exchanged on a Bluetooth channel. */
enum RFCOMM_Frame_Types_t
{
RFCOMM_Frame_SABM = 0x2F, /**< Set Asynchronous Balance Mode Field */
RFCOMM_Frame_UA = 0x63, /**< Unnumbered Acknowledgement Field */
RFCOMM_Frame_DM = 0x0F, /**< Disconnected Mode Field */
RFCOMM_Frame_DISC = 0x43, /**< Disconnect Field */
RFCOMM_Frame_SABM = 0x2F, /**< Set Asynchronous Balance Mode Field */
RFCOMM_Frame_UA = 0x63, /**< Unnumbered Acknowledgement Field */
RFCOMM_Frame_UIH = 0xEF, /**< Unnumbered Information with Header check Field */
};
enum RFCOMM_Control_Commands_t
{
RFCOMM_Control_Test = 0x20;
RFCOMM_Control_FlowControlEnable = 0xA0;
RFCOMM_Control_FlowControlDisable = 0x60;
RFCOMM_Control_ModemStatus = 0xE0;
RFCOMM_Control_RemotePortNegotiation = 0x90;
RFCOMM_Control_RemoteLineStatus = 0x50;
RFCOMM_Control_DLCParameterNegotiation = 0x80;
RFCOMM_Control_NonSupportedCommand = 0x10;
}
/* Type Defines: */
typedef struct
{
@ -84,16 +98,16 @@
void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel);
#if defined(INCLUDE_FROM_RFCOMM_C)
static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessUA(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel);
static void RFCOMM_SendFrame(const uint8_t DLCI, const bool CommandResponse, const uint8_t Control,
const uint16_t DataLen, const uint8_t* Data, Bluetooth_Channel_t* const Channel);
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint16_t Length);
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t** BufferPos);
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length);
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos);
#endif
#endif

Loading…
Cancel
Save