diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c index 5a7edc2a8f..46cc182eca 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.c @@ -69,85 +69,134 @@ void RFCOMM_Initialize(void) void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel) { - const RFCOMM_Header_t* FrameHeader = (const RFCOMM_Header_t*)Data; + 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); + + FrameData += (FrameDataLen < 128) ? 1 : 2; /* Decode the RFCOMM frame type from the header */ switch (FrameHeader->Control & ~FRAME_POLL_FINAL) { case RFCOMM_Frame_DM: - RFCOMM_ProcessDM(FrameHeader, Channel); + RFCOMM_ProcessDM(&FrameHeader->Address, Channel); break; case RFCOMM_Frame_DISC: - RFCOMM_ProcessDISC(FrameHeader, Channel); + RFCOMM_ProcessDISC(&FrameHeader->Address, Channel); break; case RFCOMM_Frame_SABM: - RFCOMM_ProcessSABM(FrameHeader, Channel); + RFCOMM_ProcessSABM(&FrameHeader->Address, Channel); break; case RFCOMM_Frame_UA: - RFCOMM_ProcessUA(FrameHeader, Channel); + RFCOMM_ProcessUA(&FrameHeader->Address, Channel); break; case RFCOMM_Frame_UIH: - RFCOMM_ProcessUIH(FrameHeader, Channel); + RFCOMM_ProcessUIH(&FrameHeader->Address, FrameDataLen, FrameData, Channel); break; default: - BT_RFCOMM_DEBUG(1, "<< Unknown Frame Type"); + BT_RFCOMM_DEBUG(1, "<< Unknown Frame Received"); break; } } -static void RFCOMM_ProcessDM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel) +static void RFCOMM_ProcessDM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel) { BT_RFCOMM_DEBUG(1, "<< DM Received"); - BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI); + BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI); } -static void RFCOMM_ProcessDISC(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel) +static void RFCOMM_ProcessDISC(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel) { BT_RFCOMM_DEBUG(1, "<< DISC Received"); - BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI); + BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->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); + RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel); } -static void RFCOMM_ProcessSABM(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const Channel) +static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel) { BT_RFCOMM_DEBUG(1, "<< SABM Received"); - BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI); + BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI); // TODO: Reset channel send/receive state BT_RFCOMM_DEBUG(1, ">> UA Sent"); - RFCOMM_SendFrame(FrameHeader->Address.DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel); + RFCOMM_SendFrame(FrameAddress->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_ProcessUA(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel) { BT_RFCOMM_DEBUG(1, "<< UA Received"); - BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameHeader->Address.DLCI); + BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI); } -static void RFCOMM_ProcessUIH(const RFCOMM_Header_t* const FrameHeader, Bluetooth_Channel_t* const 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", 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); + BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI); + BT_RFCOMM_DEBUG(2, "-- Length 0x%02X", FrameLength); - if (FrameHeader->Address.DLCI == RFCOMM_CONTROL_DLCI) + if (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI) { - // TODO: Process control command + RFCOMM_ProcessControlCommand((const RFCOMM_Command_t*)FrameData, Channel); + return; + } + + // TODO: Handle regular channel data here +} + +static void RFCOMM_ProcessControlCommand(const RFCOMM_Command_t* CommandHeader, Bluetooth_Channel_t* const Channel) +{ + switch (CommandHeader->Command) + { + case RFCOMM_Control_Test: + BT_RFCOMM_DEBUG(1, "<< TEST Command"); + break; + case RFCOMM_Control_FlowControlEnable: + BT_RFCOMM_DEBUG(1, "<< FCE Command"); + break; + case RFCOMM_Control_FlowControlDisable: + BT_RFCOMM_DEBUG(1, "<< FCD Command"); + break; + case RFCOMM_Control_ModemStatus: + BT_RFCOMM_DEBUG(1, "<< MS Command"); + break; + case RFCOMM_Control_RemotePortNegotiation: + BT_RFCOMM_DEBUG(1, "<< RPN Command"); + break; + case RFCOMM_Control_RemoteLineStatus: + BT_RFCOMM_DEBUG(1, "<< RLS Command"); + break; + case RFCOMM_Control_DLCParameterNegotiation: + BT_RFCOMM_DEBUG(1, "<< DPN Command"); + + struct + { + RFCOMM_Command_t Header; + RFCOMM_Command_t Command; + } Response = + { + .Header = (RFCOMM_Command_t) + { + .Command = RFCOMM_Control_NonSupportedCommand, + .CR = true, + .EA = true, + }, + + .Command = *CommandHeader, + }; + + RFCOMM_SendFrame(RFCOMM_CONTROL_DLCI, false, RFCOMM_Frame_UIH, sizeof(RFCOMM_Command_t), &Response, Channel); + break; } } -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 void RFCOMM_SendFrame(const uint8_t DLCI, const bool CommandResponse, const uint8_t Control, const uint16_t DataLen, + const void* Data, Bluetooth_Channel_t* const Channel) { struct { diff --git a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h index 3f7887fede..8d44c18156 100644 --- a/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h +++ b/Demos/Host/Incomplete/BluetoothHost/Lib/RFCOMM.h @@ -69,15 +69,15 @@ 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; - } + RFCOMM_Control_Test = (0x20 >> 2), + RFCOMM_Control_FlowControlEnable = (0xA0 >> 2), + RFCOMM_Control_FlowControlDisable = (0x60 >> 2), + RFCOMM_Control_ModemStatus = (0xE0 >> 2), + RFCOMM_Control_RemotePortNegotiation = (0x90 >> 2), + RFCOMM_Control_RemoteLineStatus = (0x50 >> 2), + RFCOMM_Control_DLCParameterNegotiation = (0x80 >> 2), + RFCOMM_Control_NonSupportedCommand = (0x10 >> 2), + }; /* Type Defines: */ typedef struct @@ -92,20 +92,30 @@ RFCOMM_Address_t Address; uint8_t Control; } RFCOMM_Header_t; + + typedef struct + { + unsigned char EA : 1; + unsigned char CR : 1; + unsigned char Command : 6; + } RFCOMM_Command_t; /* Function Prototypes: */ void RFCOMM_Initialize(void); void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel); #if defined(INCLUDE_FROM_RFCOMM_C) - 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_ProcessDM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel); + static void RFCOMM_ProcessDISC(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel); + static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel); + static void RFCOMM_ProcessUA(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel); + static void RFCOMM_ProcessUIH(const RFCOMM_Address_t* const FrameAddress, const uint16_t FrameLength, + const uint8_t* FrameData, Bluetooth_Channel_t* const Channel); + static void RFCOMM_ProcessControlCommand(const RFCOMM_Command_t* CommandHeader, 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); + const uint16_t DataLen, const void* Data, Bluetooth_Channel_t* const Channel); static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length); static uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos); #endif