Split out the RFCOMM Control Channel command processing code into a seperate set of files for clarity.

pull/1469/head
Dean Camera 14 years ago
parent b522e35965
commit 67bc625109

@ -106,133 +106,32 @@ void RFCOMM_ProcessPacket(void* Data, 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", FrameAddress->DLCI);
}
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", FrameAddress->DLCI);
// TODO: Close down connection
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
}
static void RFCOMM_ProcessSABM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel)
RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI)
{
BT_RFCOMM_DEBUG(1, "<< SABM Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
/* Find a free entry in the RFCOMM channel multiplexer state array */
for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
{
RFCOMM_Channel_t* CurrRFCOMMChannel = &RFCOMM_Channels[i];
/* If the channel's DLCI is zero, the channel state entry is free */
if (!(CurrRFCOMMChannel->DLCI))
{
CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
CurrRFCOMMChannel->Configured = false;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
return;
}
if (CurrRFCOMMChannel->DLCI == DLCI)
return CurrRFCOMMChannel;
}
BT_RFCOMM_DEBUG(1, ">> DM Sent");
/* No free channel in the multiplexer - decline the SABM by sending a DM frame */
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_DM | FRAME_POLL_FINAL), 0, NULL, 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", FrameAddress->DLCI);
}
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
return NULL;
}
static void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* const Channel)
uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos)
{
const RFCOMM_Command_t* CommandHeader = (const RFCOMM_Command_t*)Command;
const uint8_t* CommandData = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);
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");
// TODO - Set channel state
// RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(
// RFCOMMChannel->Configured = true;
// TODO - send ACK/NAK response
break;
default:
BT_RFCOMM_DEBUG(1, "<< Unknown 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;
}
uint8_t FirstOctet = BufferPos[0];
uint8_t SecondOctet = 0;
if (!(FirstOctet & 0x01))
SecondOctet = BufferPos[1];
return (((uint16_t)SecondOctet << 7) | FirstOctet >> 1);
}
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)
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
{
@ -287,26 +186,73 @@ static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length)
return ~FCS;
}
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos)
static void RFCOMM_ProcessDM(const RFCOMM_Address_t* const FrameAddress, Bluetooth_Channel_t* const Channel)
{
uint8_t FirstOctet = BufferPos[0];
uint8_t SecondOctet = 0;
if (!(FirstOctet & 0x01))
SecondOctet = BufferPos[1];
BT_RFCOMM_DEBUG(1, "<< DM Received");
BT_RFCOMM_DEBUG(2, "-- DLCI 0x%02X", FrameAddress->DLCI);
}
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", FrameAddress->DLCI);
RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(FrameAddress->DLCI);
return (((uint16_t)SecondOctet << 7) | FirstOctet >> 1);
/* If the requested channel is currently open, destroy it */
if (RFCOMMChannel != NULL)
RFCOMMChannel->DLCI = 0x00;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
}
RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI)
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", FrameAddress->DLCI);
/* Find a free entry in the RFCOMM channel multiplexer state array */
for (uint8_t i = 0; i < RFCOMM_MAX_OPEN_CHANNELS; i++)
{
RFCOMM_Channel_t* CurrRFCOMMChannel = &RFCOMM_Channels[i];
if (CurrRFCOMMChannel->DLCI == DLCI)
return CurrRFCOMMChannel;
/* If the channel's DLCI is zero, the channel state entry is free */
if (!(CurrRFCOMMChannel->DLCI))
{
CurrRFCOMMChannel->DLCI = FrameAddress->DLCI;
CurrRFCOMMChannel->Configured = false;
BT_RFCOMM_DEBUG(1, ">> UA Sent");
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_UA | FRAME_POLL_FINAL), 0, NULL, Channel);
return;
}
}
BT_RFCOMM_DEBUG(1, ">> DM Sent");
/* No free channel in the multiplexer - decline the SABM by sending a DM frame */
RFCOMM_SendFrame(FrameAddress->DLCI, true, (RFCOMM_Frame_DM | FRAME_POLL_FINAL), 0, NULL, 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", FrameAddress->DLCI);
}
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);
return NULL;
if (FrameAddress->DLCI == RFCOMM_CONTROL_DLCI)
{
RFCOMM_ProcessControlCommand(FrameData, Channel);
return;
}
// TODO: Handle regular channel data here
}

@ -47,6 +47,7 @@
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include "BluetoothStack.h"
#include "RFCOMMControl.h"
/* Macros: */
#define BT_RFCOMM_DEBUG(l, s, ...) do { if (RFCOMM_DEBUG_LEVEL >= l) printf_P(PSTR("(RFCOMM) " s "\r\n"), ##__VA_ARGS__); } while (0)
@ -55,9 +56,8 @@
#define FRAME_POLL_FINAL (1 << 4)
#define RFCOMM_CONTROL_DLCI 0
#define RFCOMM_MAX_OPEN_CHANNELS 5
/* Enums: */
/** Enum for the types of RFCOMM frames which can be exchanged on a Bluetooth channel. */
enum RFCOMM_Frame_Types_t
@ -69,18 +69,6 @@
RFCOMM_Frame_UIH = 0xEF, /**< Unnumbered Information with Header check Field */
};
enum RFCOMM_Control_Commands_t
{
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
{
@ -95,23 +83,24 @@
uint8_t Control;
} RFCOMM_Header_t;
typedef struct
{
unsigned char EA : 1;
unsigned char CR : 1;
unsigned char Command : 6;
} RFCOMM_Command_t;
typedef struct
{
uint8_t DLCI;
bool Configured;
} RFCOMM_Channel_t;
/* External Variables: */
extern RFCOMM_Channel_t RFCOMM_Channels[RFCOMM_MAX_OPEN_CHANNELS];
/* Function Prototypes: */
void RFCOMM_Initialize(void);
void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel);
void RFCOMM_Initialize(void);
void RFCOMM_ProcessPacket(void* Data, Bluetooth_Channel_t* const Channel);
RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI);
uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos);
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);
#if defined(INCLUDE_FROM_RFCOMM_C)
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);
@ -120,15 +109,7 @@
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 uint8_t* Command, 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);
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length);
static uint16_t RFCOMM_GetFrameDataLength(const uint8_t* const BufferPos);
RFCOMM_Channel_t* RFCOMM_GetChannelData(const uint8_t DLCI);
static uint8_t RFCOMM_GetFCSValue(const void* FrameStart, uint8_t Length);
#endif
#endif

@ -0,0 +1,120 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* RFCOMM multiplexer control layer module. This module handles multiplexer
* channel commands to the control DLCI in the RFCOMM layer, to open, configure,
* test and close logical RFCOMM channels.
*/
#define INCLUDE_FROM_RFCOMM_CONTROL_C
#include "RFCOMMControl.h"
void RFCOMM_ProcessControlCommand(const uint8_t* Command, Bluetooth_Channel_t* const Channel)
{
const RFCOMM_Command_t* CommandHeader = (const RFCOMM_Command_t*)Command;
const uint8_t* CommandData = (const uint8_t*)Command + sizeof(RFCOMM_Command_t);
switch (CommandHeader->Command)
{
case RFCOMM_Control_Test:
RFCOMM_ProcessTestCommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_FlowControlEnable:
RFCOMM_ProcessFCECommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_FlowControlDisable:
RFCOMM_ProcessFCDCommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_ModemStatus:
RFCOMM_ProcessMSCommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_RemotePortNegotiation:
RFCOMM_ProcessRPNCommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_RemoteLineStatus:
RFCOMM_ProcessRLSCommand(CommandHeader, CommandData);
break;
case RFCOMM_Control_DLCParameterNegotiation:
RFCOMM_ProcessDPNCommand(CommandHeader, CommandData);
break;
default:
BT_RFCOMM_DEBUG(1, "<< Unknown Command");
break;
}
}
static void RFCOMM_ProcessTestCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< TEST Command");
}
static void RFCOMM_ProcessFCECommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< FCE Command");
}
static void RFCOMM_ProcessFCDCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< FCD Command");
}
static void RFCOMM_ProcessMSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< MS Command");
}
static void RFCOMM_ProcessRPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< RPN Command");
}
static void RFCOMM_ProcessRLSCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< RLS Command");
}
static void RFCOMM_ProcessDPNCommand(const RFCOMM_Command_t* CommandHeader, const uint8_t* CommandData)
{
BT_RFCOMM_DEBUG(1, "<< DPN Command");
/* Skip over the first data byte (Length field) since its size and value are fixed */
RFCOMM_Channel_t* RFCOMMChannel = RFCOMM_GetChannelData(CommandData[2]);
if (RFCOMMChannel == NULL)
{
// TODO: Channel does not exist - create it
}
RFCOMMChannel->Configured = true;
// TODO: Read in channel data, ACK request
}

@ -0,0 +1,86 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2010.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for RFCOMMControl.c.
*/
#ifndef _RFCOMM_CONTROL_H_
#define _RFCOMM_CONTROL_H_
/* Includes: */
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <LUFA/Common/Common.h>
#include <LUFA/Drivers/Peripheral/SerialStream.h>
#include "BluetoothStack.h"
#include "RFCOMM.h"
/* Enums: */
enum RFCOMM_Control_Commands_t
{
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
{
unsigned char EA : 1;
unsigned char CR : 1;
unsigned char Command : 6;
} RFCOMM_Command_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);
#endif
#endif

@ -138,6 +138,7 @@ SRC = $(TARGET).c \
Lib/SDP.c \
Lib/SDPServices.c \
Lib/RFCOMM.c \
Lib/RFCOMMControl.c \
$(LUFA_PATH)/LUFA/Drivers/Peripheral/SerialStream.c \
$(LUFA_PATH)/LUFA/Drivers/Peripheral/Serial.c \
$(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c \

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save