Added incomplete PrinterHost demo application.

Seperated out Lib components of the incomplete BluetoothHost demo application out into a seperate Lib subfolder.

Changed F_CLOCK entries in project makefiles to alias to F_CPU by default, as this is the most common case.
This commit is contained in:
Dean Camera 2009-07-11 15:05:56 +00:00
parent b462f2d457
commit e0af6014a7
62 changed files with 1362 additions and 74 deletions

View file

@ -0,0 +1,206 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#define INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C
#include "BluetoothACLPackets.h"
void Bluetooth_ProcessACLPackets(void)
{
Bluetooth_ACL_Header_t ACLPacketHeader;
Bluetooth_DataPacket_Header_t DataHeader;
Pipe_SelectPipe(BLUETOOTH_DATA_IN_PIPE);
Pipe_SetToken(PIPE_TOKEN_IN);
Pipe_Unfreeze();
if (!(Pipe_IsReadWriteAllowed()))
{
Pipe_Freeze();
return;
}
Pipe_Read_Stream_LE(&ACLPacketHeader, sizeof(ACLPacketHeader));
Pipe_Read_Stream_LE(&DataHeader, sizeof(DataHeader));
BT_DEBUG("(ACL) Packet Received", NULL);
BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader.ConnectionHandle);
BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader.DataLength);
BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader.DestinationChannel);
BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader.PayloadLength);
if (DataHeader.DestinationChannel == BLUETOOTH_CHANNEL_SIGNALING)
{
Bluetooth_SignalCommand_Header_t SignalCommandHeader;
Pipe_Read_Stream_LE(&SignalCommandHeader, sizeof(SignalCommandHeader));
switch (SignalCommandHeader.Code)
{
case BLUETOOTH_SIGNAL_CONNECTION_REQUEST:
Bluetooth_ProcessSignalPacket_ConnectionRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
break;
case BLUETOOTH_SIGNAL_CONFIGURATION_REQUEST:
Bluetooth_ProcessSignalPacket_ConfigurationRequest(&ACLPacketHeader, &DataHeader, &SignalCommandHeader);
break;
case BLUETOOTH_SIGNAL_INFORMATION_REQUEST:
BT_DEBUG("(ACL) -- Information Request, Discarded");
Pipe_Discard_Stream(ACLPacketHeader.DataLength);
Pipe_ClearIN();
Pipe_Freeze();
break;
default:
BT_DEBUG("(ACL) >> Unknown Signalling Command 0x%02X", SignalCommandHeader.Code);
Pipe_Discard_Stream(ACLPacketHeader.DataLength);
Pipe_ClearIN();
Pipe_Freeze();
break;
}
}
else
{
uint8_t DataPayload[DataHeader.PayloadLength];
Pipe_Read_Stream_LE(&DataPayload, sizeof(DataPayload));
DataHeader.PayloadLength = 0;
BT_DEBUG("(ACL) -- Data Payload: ", NULL);
for (uint16_t B = 0; B < sizeof(DataPayload); B++)
printf("0x%02X ", DataPayload[B]);
BT_DEBUG("", NULL);
Pipe_Discard_Stream(ACLPacketHeader.DataLength);
Pipe_ClearIN();
Pipe_Freeze();
}
}
static inline void Bluetooth_ProcessSignalPacket_ConnectionRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
Bluetooth_DataPacket_Header_t* DataHeader,
Bluetooth_SignalCommand_Header_t* SignalCommandHeader)
{
Bluetooth_SignalCommand_ConnectionRequest_t ConnectionRequest;
Pipe_Read_Stream_LE(&ConnectionRequest, sizeof(ConnectionRequest));
BT_DEBUG("(ACL) >> L2CAP Connection Request", NULL);
BT_DEBUG("(ACL) -- PSM: 0x%04X", ConnectionRequest.PSM);
BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionRequest.SourceChannel);
Pipe_ClearIN();
Pipe_Freeze();
Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
Pipe_SetToken(PIPE_TOKEN_OUT);
Pipe_Unfreeze();
Bluetooth_SignalCommand_ConnectionResponse_t ConnectionResponse;
ACLPacketHeader->DataLength = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);
DataHeader->PayloadLength = sizeof(*SignalCommandHeader) + sizeof(ConnectionResponse);
DataHeader->DestinationChannel = BLUETOOTH_CHANNEL_SIGNALING;
SignalCommandHeader->Code = BLUETOOTH_SIGNAL_CONNECTION_RESPONSE;
SignalCommandHeader->Length = sizeof(ConnectionResponse);
Bluetooth_Channel_t* ChannelData = Bluetooth_InitChannelData(ConnectionRequest.SourceChannel, ConnectionRequest.PSM);
ConnectionResponse.Result = (ChannelData == NULL) ? BLUETOOTH_CONNECTION_REFUSED_RESOURCES :
BLUETOOTH_CONNECTION_SUCCESSFUL;
ConnectionResponse.DestinationChannel = ChannelData->LocalNumber;
ConnectionResponse.SourceChannel = ChannelData->RemoteNumber;
ConnectionResponse.Status = 0x00;
Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));
Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));
Pipe_Write_Stream_LE(SignalCommandHeader, sizeof(*SignalCommandHeader));
Pipe_Write_Stream_LE(&ConnectionResponse, sizeof(ConnectionResponse));
Pipe_ClearOUT();
Pipe_Freeze();
BT_DEBUG("(ACL) Packet Sent", NULL);
BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader->ConnectionHandle);
BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader->DataLength);
BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader->DestinationChannel);
BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader->PayloadLength);
BT_DEBUG("(ACL) >> L2CAP Connection Response", NULL);
BT_DEBUG("(ACL) -- Source Channel: 0x%04X", ConnectionResponse.SourceChannel);
BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConnectionResponse.DestinationChannel);
}
static inline void Bluetooth_ProcessSignalPacket_ConfigurationRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
Bluetooth_DataPacket_Header_t* DataHeader,
Bluetooth_SignalCommand_Header_t* SignalCommandHeader)
{
Bluetooth_SignalCommand_ConfigurationRequest_t ConfigurationRequest;
Pipe_Read_Stream_LE(&ConfigurationRequest, sizeof(ConfigurationRequest));
BT_DEBUG("(ACL) >> L2CAP Configuration Request", NULL);
BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", ConfigurationRequest.DestinationChannel);
Pipe_ClearIN();
Pipe_Freeze();
Pipe_SelectPipe(BLUETOOTH_DATA_OUT_PIPE);
Pipe_SetToken(PIPE_TOKEN_OUT);
Pipe_Unfreeze();
Bluetooth_SignalCommand_ConfigurationResponse_t ConfigurationResponse;
ACLPacketHeader->DataLength = sizeof(*DataHeader) + sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);
DataHeader->PayloadLength = sizeof(*SignalCommandHeader) + sizeof(ConfigurationResponse);
DataHeader->DestinationChannel = BLUETOOTH_CHANNEL_SIGNALING;
SignalCommandHeader->Code = BLUETOOTH_SIGNAL_CONFIGURATION_RESPONSE;
SignalCommandHeader->Length = sizeof(ConfigurationResponse);
Bluetooth_Channel_t* ChannelData = Bluetooth_GetChannelData(ConfigurationRequest.DestinationChannel, CHANNEL_LOOKUP_BY_DESTINATION);
if (ChannelData != NULL)
ChannelData->State = Channel_Open;
// TODO: Add channel config data to the tail of ConfigurationResponse
ConfigurationResponse.SourceChannel = ChannelData->RemoteNumber;
ConfigurationResponse.Flags = 0x00;
ConfigurationResponse.Result = (ChannelData != NULL) ? BLUETOOTH_CONFIGURATION_SUCCESSFUL : BLUETOOTH_CONFIGURATION_REJECTED;
Pipe_Write_Stream_LE(ACLPacketHeader, sizeof(*ACLPacketHeader));
Pipe_Write_Stream_LE(DataHeader, sizeof(*DataHeader));
Pipe_Write_Stream_LE(SignalCommandHeader, sizeof(*SignalCommandHeader));
Pipe_Write_Stream_LE(&ConfigurationResponse, sizeof(ConfigurationResponse));
Pipe_ClearOUT();
Pipe_Freeze();
BT_DEBUG("(ACL) Packet Sent", NULL);
BT_DEBUG("(ACL) -- Connection Handle: 0x%04X", ACLPacketHeader->ConnectionHandle);
BT_DEBUG("(ACL) -- Data Length: 0x%04X", ACLPacketHeader->DataLength);
BT_DEBUG("(ACL) -- Destination Channel: 0x%04X", DataHeader->DestinationChannel);
BT_DEBUG("(ACL) -- Payload Length: 0x%04X", DataHeader->PayloadLength);
BT_DEBUG("(ACL) >> L2CAP Configuration Response", NULL);
}

View file

@ -0,0 +1,122 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#ifndef _BLUETOOTH_ACLPACKETS_
#define _BLUETOOTH_ACLPACKETS_
/* Includes: */
#include <avr/io.h>
#include <string.h>
#include <stdbool.h>
#include <LUFA/Drivers/USB/USB.h>
#include "BluetoothStack.h"
/* Macros: */
#define BLUETOOTH_CHANNEL_SIGNALING 0x0001
#define BLUETOOTH_CHANNEL_CONNECTIONLESS 0x0002
#define BLUETOOTH_SIGNAL_CONNECTION_REQUEST 0x02
#define BLUETOOTH_SIGNAL_CONNECTION_RESPONSE 0x03
#define BLUETOOTH_SIGNAL_CONFIGURATION_REQUEST 0x04
#define BLUETOOTH_SIGNAL_CONFIGURATION_RESPONSE 0x05
#define BLUETOOTH_SIGNAL_INFORMATION_REQUEST 0x0A
#define BLUETOOTH_CONNECTION_SUCCESSFUL 0x0000
#define BLUETOOTH_CONNECTION_REFUSED_RESOURCES 0x0004
#define BLUETOOTH_CONFIGURATION_SUCCESSFUL 0x0000
#define BLUETOOTH_CONFIGURATION_REJECTED 0x0002
#define BLUETOOTH_CONFIGURATION_UNKNOWNOPTIONS 0x0003
/* Type Defines: */
typedef struct
{
uint16_t ConnectionHandle;
uint16_t DataLength;
} Bluetooth_ACL_Header_t;
typedef struct
{
uint16_t PayloadLength;
uint16_t DestinationChannel;
} Bluetooth_DataPacket_Header_t;
typedef struct
{
uint8_t Code;
uint8_t Identifier;
uint16_t Length;
} Bluetooth_SignalCommand_Header_t;
typedef struct
{
uint16_t PSM;
uint16_t SourceChannel;
} Bluetooth_SignalCommand_ConnectionRequest_t;
typedef struct
{
uint16_t DestinationChannel;
uint16_t SourceChannel;
uint16_t Result;
uint16_t Status;
} Bluetooth_SignalCommand_ConnectionResponse_t;
typedef struct
{
uint16_t DestinationChannel;
uint16_t Flags;
uint8_t Options[];
} Bluetooth_SignalCommand_ConfigurationRequest_t;
typedef struct
{
uint16_t SourceChannel;
uint16_t Flags;
uint16_t Result;
uint8_t Config;
} Bluetooth_SignalCommand_ConfigurationResponse_t;
/* Function Prototypes: */
void Bluetooth_ProcessACLPackets(void);
#if defined(INCLUDE_FROM_BLUETOOTH_ACLPACKETS_C)
static inline void Bluetooth_ProcessSignalPacket_ConnectionRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
Bluetooth_DataPacket_Header_t* DataHeader,
Bluetooth_SignalCommand_Header_t* SignalCommandHeader);
static inline void Bluetooth_ProcessSignalPacket_ConfigurationRequest(Bluetooth_ACL_Header_t* ACLPacketHeader,
Bluetooth_DataPacket_Header_t* DataHeader,
Bluetooth_SignalCommand_Header_t* SignalCommandHeader);
#endif
#endif

View file

@ -0,0 +1,110 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#ifndef _BLUETOOTH_CLASS_CODES_H_
#define _BLUETOOTH_CLASS_CODES_H_
/* Macros: */
#define DEVICE_CLASS_SERVICE_POSITIONING (1UL << 16)
#define DEVICE_CLASS_SERVICE_NETWORKING (1UL << 17)
#define DEVICE_CLASS_SERVICE_RENDERING (1UL << 18)
#define DEVICE_CLASS_SERVICE_CAPTURING (1UL << 19)
#define DEVICE_CLASS_SERVICE_OBJECTTRANSFER (1UL << 20)
#define DEVICE_CLASS_SERVICE_AUDIO (1UL << 21)
#define DEVICE_CLASS_SERVICE_TELEPHONY (1UL << 22)
#define DEVICE_CLASS_SERVICE_INFORMATION (1UL << 23)
#define DEVICE_CLASS_MAJOR_MISC (0x00 << 8)
#define DEVICE_CLASS_MAJOR_COMPUTER (0x01 << 8)
#define DEVICE_CLASS_MAJOR_PHONE (0x02 << 8)
#define DEVICE_CLASS_MAJOR_LAN (0x03 << 8)
#define DEVICE_CLASS_MAJOR_AUDIOVIDEO (0x04 << 8)
#define DEVICE_CLASS_MAJOR_PERIPHERAL (0x05 << 8)
#define DEVICE_CLASS_MAJOR_IMAGING (0x06 << 8)
#define DEVICE_CLASS_MAJOR_UNCLASSIFIED (0x1F << 8)
#define DEVICE_CLASS_MINOR_COMPUTER_UNCATEGORIZED (0x00 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_DESKTOP (0x01 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_SERVER (0x02 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_LAPTOP (0x03 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_HANDHELD (0x04 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_PALM (0x05 << 2)
#define DEVICE_CLASS_MINOR_COMPUTER_WEARABLE (0x06 << 2)
#define DEVICE_CLASS_MINOR_PHONE_UNCATEGORIZED (0x00 << 2)
#define DEVICE_CLASS_MINOR_PHONE_CELLULAR (0x01 << 2)
#define DEVICE_CLASS_MINOR_PHONE_CORDLESS (0x02 << 2)
#define DEVICE_CLASS_MINOR_PHONE_SMARTPHONE (0x03 << 2)
#define DEVICE_CLASS_MINOR_PHONE_WIREDMODEM (0x04 << 2)
#define DEVICE_CLASS_MINOR_PHONE_ISDN (0x05 << 2)
#define DEVICE_CLASS_MINOR_LAN_FULLY_AVAILABLE (0x00 << 5)
#define DEVICE_CLASS_MINOR_LAN_1_TO_17_PC_UTILIZED (0x01 << 5)
#define DEVICE_CLASS_MINOR_LAN_17_TO_33_PC_UTILIZED (0x02 << 5)
#define DEVICE_CLASS_MINOR_LAN_33_TO_50_PC_UTILIZED (0x03 << 5)
#define DEVICE_CLASS_MINOR_LAN_50_TO_67_PC_UTILIZED (0x04 << 5)
#define DEVICE_CLASS_MINOR_LAN_67_TO_83_PC_UTILIZED (0x05 << 5)
#define DEVICE_CLASS_MINOR_LAN_83_TO_99_PC_UTILIZED (0x06 << 5)
#define DEVICE_CLASS_MINOR_NO_SERVICE_AVAILABLE (0x07 << 5)
#define DEVICE_CLASS_MINOR_AV_UNCATEGORIZED (0x00 << 2)
#define DEVICE_CLASS_MINOR_AV_HEADSET (0x01 << 2)
#define DEVICE_CLASS_MINOR_AV_HANDSFREE (0x02 << 2)
#define DEVICE_CLASS_MINOR_AV_MICROPHONE (0x04 << 2)
#define DEVICE_CLASS_MINOR_AV_LOUDSPEAKER (0x05 << 2)
#define DEVICE_CLASS_MINOR_AV_HEADPHONES (0x06 << 2)
#define DEVICE_CLASS_MINOR_AV_PORTABLE_AUDIO (0x07 << 2)
#define DEVICE_CLASS_MINOR_AV_CARAUDIO (0x08 << 2)
#define DEVICE_CLASS_MINOR_AV_SETTOP_BOX (0x09 << 2)
#define DEVICE_CLASS_MINOR_AV_HIFI (0x0A << 2)
#define DEVICE_CLASS_MINOR_AV_VCR (0x0B << 2)
#define DEVICE_CLASS_MINOR_AV_VIDEO_CAMERA (0x0C << 2)
#define DEVICE_CLASS_MINOR_AV_CAMCORDER (0x0D << 2)
#define DEVICE_CLASS_MINOR_AV_VIDEO_MONITOR (0x0E << 2)
#define DEVICE_CLASS_MINOR_AV_DISPLAY_AND_LOUDSPEAKER (0x0F << 2)
#define DEVICE_CLASS_MINOR_AV_VIDEO_CONFERENCING (0x10 << 2)
#define DEVICE_CLASS_MINOR_AV_GAMING_TOY (0x12 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_KEYBOARD (0x01 << 6)
#define DEVICE_CLASS_MINOR_PERIPHERAL_POINTING (0x02 << 6)
#define DEVICE_CLASS_MINOR_PERIPHERAL_COMBO (0x03 << 6)
#define DEVICE_CLASS_MINOR_PERIPHERAL_UNCATEGORIZED (0x00 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_JOYSTICK (0x01 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_GAMEPAD (0x02 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_REMOTE_CONTROL (0x03 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_SENSING_DEVICE (0x04 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_DIGITIZER (0x05 << 2)
#define DEVICE_CLASS_MINOR_PERIPHERAL_CARD_READER (0x06 << 2)
#define DEVICE_CLASS_MINOR_IMAGING_DISPLAY (1 << 4)
#define DEVICE_CLASS_MINOR_IMAGING_CAMERA (1 << 5)
#define DEVICE_CLASS_MINOR_IMAGING_SCANNER (1 << 6)
#define DEVICE_CLASS_MINOR_IMAGING_PRINTER (1 << 7)
#endif

View file

@ -0,0 +1,376 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#include "BluetoothHCICommands.h"
static Bluetooth_HCICommand_Header_t HCICommandHeader;
static Bluetooth_HCIEvent_Header_t HCIEventHeader;
uint8_t Bluetooth_HCIProcessingState;
static uint8_t Bluetooth_TempDeviceAddress[6];
static uint8_t Bluetooth_SendHCICommand(void* Parameters, uint8_t ParamLength)
{
uint8_t CommandBuffer[sizeof(HCICommandHeader) + HCICommandHeader.ParameterLength];
USB_ControlRequest = (USB_Request_Header_t)
{
bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_DEVICE),
bRequest: 0,
wValue: 0,
wIndex: 0,
wLength: sizeof(CommandBuffer)
};
memset(CommandBuffer, 0x00, sizeof(CommandBuffer));
memcpy(CommandBuffer, &HCICommandHeader, sizeof(HCICommandHeader));
if (ParamLength)
memcpy(&CommandBuffer[sizeof(HCICommandHeader)], Parameters, ParamLength);
Pipe_SelectPipe(PIPE_CONTROLPIPE);
return USB_Host_SendControlRequest(CommandBuffer);
}
static bool Bluetooth_GetNextHCIEventHeader(void)
{
Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE);
Pipe_Unfreeze();
if (!(Pipe_IsReadWriteAllowed()))
{
Pipe_Freeze();
return false;
}
Pipe_Read_Stream_LE(&HCIEventHeader, sizeof(HCIEventHeader));
Pipe_Freeze();
return true;
}
static void Bluetooth_DiscardRemainingHCIEventParameters(void)
{
Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE);
Pipe_Unfreeze();
Pipe_Discard_Stream(HCIEventHeader.ParameterLength);
Pipe_ClearIN();
Pipe_Freeze();
}
void Bluetooth_ProcessHCICommands(void)
{
uint8_t ErrorCode;
switch (Bluetooth_HCIProcessingState)
{
case Bluetooth_Init:
Pipe_SelectPipe(BLUETOOTH_EVENTS_PIPE);
memset(&Bluetooth_Connection, 0x00, sizeof(Bluetooth_Connection));
Bluetooth_HCIProcessingState = Bluetooth_Init_Reset;
break;
case Bluetooth_Init_Reset:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_RESET},
ParameterLength: 0,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_Reset", NULL);
ErrorCode = Bluetooth_SendHCICommand(NULL, 0);
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_Init_ReadBufferSize;
break;
case Bluetooth_Init_ReadBufferSize:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_INFORMATIONAL, OCF: OGF_CTRLR_INFORMATIONAL_READBUFFERSIZE},
ParameterLength: 0,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_ReadBufferSize", NULL);
ErrorCode = Bluetooth_SendHCICommand(NULL, 0);
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_Init_SetEventMask;
break;
case Bluetooth_Init_SetEventMask:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_SET_EVENT_MASK},
ParameterLength: 8,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_SetEventMask", NULL);
uint8_t EventMask[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
ErrorCode = Bluetooth_SendHCICommand(&EventMask, 8);
BT_DEBUG("(HCI) -- Event mask: 0x%02X%02X%02X%02X%02X%02X%02X%02X", EventMask[7], EventMask[6], EventMask[5], EventMask[4],
EventMask[3], EventMask[2], EventMask[1], EventMask[0]);
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_Init_SetLocalName;
break;
case Bluetooth_Init_SetLocalName:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME},
ParameterLength: 248,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_SetLocalName", NULL);
BT_DEBUG("(HCI) -- Name: %s", Bluetooth_DeviceConfiguration.Name);
ErrorCode = Bluetooth_SendHCICommand(Bluetooth_DeviceConfiguration.Name, strlen(Bluetooth_DeviceConfiguration.Name));
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_Init_SetDeviceClass;
break;
case Bluetooth_Init_SetDeviceClass:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE},
ParameterLength: 3,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_SetDeviceClass", NULL);
ErrorCode = Bluetooth_SendHCICommand(&Bluetooth_DeviceConfiguration.Class, 3);
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_Init_WriteScanEnable;
break;
case Bluetooth_Init_WriteScanEnable:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_CTRLR_BASEBAND, OCF: OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE},
ParameterLength: 1,
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Init_WriteScanEnable", NULL);
uint8_t Interval = InquiryAndPageScans;
ErrorCode = Bluetooth_SendHCICommand(&Interval, 1);
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_PrepareToProcessEvents;
break;
case Bluetooth_PrepareToProcessEvents:
BT_DEBUG("(HCI) Enter State: Bluetooth_ProcessEvents", NULL);
Bluetooth_HCIProcessingState = Bluetooth_ProcessEvents;
break;
case Bluetooth_ProcessEvents:
if (Bluetooth_GetNextHCIEventHeader())
{
BT_DEBUG("(HCI) Event Code: 0x%02X", HCIEventHeader.EventCode);
if (HCIEventHeader.EventCode == EVENT_COMMAND_STATUS)
{
Bluetooth_HCIEvent_CommandStatus_Header_t CommandStatusHeader;
Pipe_Read_Stream_LE(&CommandStatusHeader, sizeof(CommandStatusHeader));
HCIEventHeader.ParameterLength -= sizeof(CommandStatusHeader);
BT_DEBUG("(HCI) >> Command status: 0x%02X", CommandStatusHeader.CommandStatus);
if (CommandStatusHeader.CommandStatus)
Bluetooth_HCIProcessingState = Bluetooth_Init;
}
else if (HCIEventHeader.EventCode == EVENT_CONNECTION_REQUEST)
{
Bluetooth_HCIEvent_ConnectionRequest_Header_t ConnectionRequestParams;
Pipe_Read_Stream_LE(&ConnectionRequestParams, sizeof(ConnectionRequestParams));
HCIEventHeader.ParameterLength -= sizeof(ConnectionRequestParams);
BT_DEBUG("(HCI) >> Connection Request from device %02X:%02X:%02X:%02X:%02X:%02X",
ConnectionRequestParams.RemoteAddress[5], ConnectionRequestParams.RemoteAddress[4],
ConnectionRequestParams.RemoteAddress[3], ConnectionRequestParams.RemoteAddress[2],
ConnectionRequestParams.RemoteAddress[1], ConnectionRequestParams.RemoteAddress[0]);
BT_DEBUG("(HCI) -- Device Class: 0x%02X%04X", ConnectionRequestParams.ClassOfDevice_Service,
ConnectionRequestParams.ClassOfDevice_MajorMinor);
BT_DEBUG("(HCI) -- Link Type: 0x%02x", ConnectionRequestParams.LinkType);
memcpy(Bluetooth_TempDeviceAddress, ConnectionRequestParams.RemoteAddress,
sizeof(Bluetooth_TempDeviceAddress));
Bluetooth_HCIProcessingState = (Bluetooth_Connection.IsConnected) ? Bluetooth_Conn_RejectConnection :
Bluetooth_Conn_AcceptConnection;
}
else if (HCIEventHeader.EventCode == EVENT_DISCONNECTION_COMPLETE)
{
BT_DEBUG("(HCI) >> Disconnection from device complete.", NULL);
Bluetooth_HCIProcessingState = Bluetooth_Init;
}
else if (HCIEventHeader.EventCode == EVENT_CONNECTION_COMPLETE)
{
Bluetooth_HCIEvent_ConnectionComplete_Header_t ConnectionCompleteParams;
Pipe_Read_Stream_LE(&ConnectionCompleteParams, sizeof(ConnectionCompleteParams));
HCIEventHeader.ParameterLength -= sizeof(ConnectionCompleteParams);
BT_DEBUG("(HCI) >> Connection to device complete.", NULL);
BT_DEBUG("(HCI) -- Status: %d", ConnectionCompleteParams.Status);
BT_DEBUG("(HCI) -- Handle: %d", ConnectionCompleteParams.ConnectionHandle);
if (ConnectionCompleteParams.Status == 0x00)
{
memcpy(Bluetooth_Connection.DeviceAddress, ConnectionCompleteParams.RemoteAddress,
sizeof(Bluetooth_Connection.DeviceAddress));
Bluetooth_Connection.ConnectionHandle = ConnectionCompleteParams.ConnectionHandle;
Bluetooth_Connection.IsConnected = true;
}
}
else if (HCIEventHeader.EventCode == EVENT_PIN_CODE_REQUEST)
{
Pipe_Read_Stream_LE(&Bluetooth_TempDeviceAddress, sizeof(Bluetooth_TempDeviceAddress));
HCIEventHeader.ParameterLength -= sizeof(Bluetooth_TempDeviceAddress);
BT_DEBUG("(HCI) >> PIN code Request from device %02X:%02X:%02X:%02X:%02X:%02X",
Bluetooth_TempDeviceAddress[5], Bluetooth_TempDeviceAddress[4], Bluetooth_TempDeviceAddress[3],
Bluetooth_TempDeviceAddress[2], Bluetooth_TempDeviceAddress[1], Bluetooth_TempDeviceAddress[0]);
Bluetooth_HCIProcessingState = Bluetooth_Conn_SendPINCode;
}
BT_DEBUG("(HCI) -- Unread Event Param Length: %d", HCIEventHeader.ParameterLength);
Bluetooth_DiscardRemainingHCIEventParameters();
}
break;
case Bluetooth_Conn_AcceptConnection:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST},
ParameterLength: sizeof(Bluetooth_HCICommand_AcceptConnectionRequest_Params_t),
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Conn_AcceptConnection", NULL);
Bluetooth_HCICommand_AcceptConnectionRequest_Params_t AcceptConnectionParams;
memcpy(AcceptConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress,
sizeof(Bluetooth_TempDeviceAddress));
AcceptConnectionParams.SlaveRole = true;
Bluetooth_SendHCICommand(&AcceptConnectionParams, sizeof(AcceptConnectionParams));
Bluetooth_HCIProcessingState = Bluetooth_PrepareToProcessEvents;
break;
case Bluetooth_Conn_RejectConnection:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST},
ParameterLength: sizeof(Bluetooth_HCICommand_RejectConnectionRequest_Params_t),
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Conn_RejectConnection", NULL);
Bluetooth_HCICommand_RejectConnectionRequest_Params_t RejectConnectionParams;
memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress,
sizeof(Bluetooth_TempDeviceAddress));
RejectConnectionParams.Reason = ERROR_LIMITED_RESOURCES;
Bluetooth_SendHCICommand(&AcceptConnectionParams, sizeof(AcceptConnectionParams));
Bluetooth_HCIProcessingState = Bluetooth_PrepareToProcessEvents;
break;
case Bluetooth_Conn_SendPINCode:
HCICommandHeader = (Bluetooth_HCICommand_Header_t)
{
OpCode: {OGF: OGF_LINK_CONTROL, OCF: OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY},
ParameterLength: sizeof(Bluetooth_HCICommand_PinCodeResponse_Params_t),
};
BT_DEBUG("(HCI) Enter State: Bluetooth_Conn_SendPINCode", NULL);
BT_DEBUG("(HCI) -- PIN: %s", Bluetooth_DeviceConfiguration.PINCode);
Bluetooth_HCICommand_PinCodeResponse_Params_t PINCodeRequestParams;
memcpy(PINCodeRequestParams.RemoteAddress, Bluetooth_TempDeviceAddress,
sizeof(Bluetooth_TempDeviceAddress));
PINCodeRequestParams.PINCodeLength = strlen(Bluetooth_DeviceConfiguration.PINCode);
memcpy(PINCodeRequestParams.PINCode, Bluetooth_DeviceConfiguration.PINCode,
sizeof(Bluetooth_DeviceConfiguration.PINCode));
Bluetooth_SendHCICommand(&PINCodeRequestParams, sizeof(PINCodeRequestParams));
do
{
while (!(Bluetooth_GetNextHCIEventHeader()));
Bluetooth_DiscardRemainingHCIEventParameters();
} while (HCIEventHeader.EventCode != EVENT_COMMAND_COMPLETE);
Bluetooth_HCIProcessingState = Bluetooth_PrepareToProcessEvents;
break;
}
}

View file

@ -0,0 +1,190 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#ifndef _BLUETOOTH_HCICOMMANDS_H_
#define _BLUETOOTH_HCICOMMANDS_H_
/* Includes: */
#include <avr/io.h>
#include <string.h>
#include <stdbool.h>
#include <LUFA/Drivers/USB/USB.h>
#include "BluetoothStack.h"
#include "BluetoothClassCodes.h"
/* Macros: */
#define OGF_LINK_CONTROL 0x01
#define OGF_CTRLR_BASEBAND 0x03
#define OGF_CTRLR_INFORMATIONAL 0x04
#define OCF_LINK_CONTROL_INQUIRY 0x0001
#define OCF_LINK_CONTROL_INQUIRY_CANCEL 0x0002
#define OCF_LINK_CONTROL_PERIODIC_INQUIRY 0x0003
#define OCF_LINK_CONTROL_EXIT_PERIODIC_INQUIRY 0x0004
#define OCF_LINK_CONTROL_CREATE_CONNECTION 0x0005
#define OCF_LINK_CONTROL_DISCONNECT 0x0006
#define OCF_LINK_CONTROL_CREATE_CONNECTION_CANCEL 0x0008
#define OCF_LINK_CONTROL_ACCEPT_CONNECTION_REQUEST 0x0009
#define OCF_LINK_CONTROL_REJECT_CONNECTION_REQUEST 0x000A
#define OCF_LINK_CONTROL_LINK_KEY_REQUEST_REPLY 0x000B
#define OCF_LINK_CONTROL_LINK_KEY_REQUEST_NEG_REPLY 0x000C
#define OCF_LINK_CONTROL_PIN_CODE_REQUEST_REPLY 0x000D
#define OCF_LINK_CONTROL_PIN_CODE_REQUEST_NEG_REPLY 0x000E
#define OCF_LINK_CONTROL_CHANGE_CONNECTION_PACKET_TYPE 0x000F
#define OCF_LINK_CONTROL_REMOTE_NAME_REQUEST 0x0019
#define OCF_CTRLR_BASEBAND_SET_EVENT_MASK 0x0001
#define OCF_CTRLR_BASEBAND_RESET 0x0003
#define OCF_CTRLR_BASEBAND_WRITE_PIN_TYPE 0x000A
#define OCF_CTRLR_BASEBAND_WRITE_LOCAL_NAME 0x0013
#define OCF_CTRLR_BASEBAND_READ_LOCAL_NAME 0x0014
#define OCF_CTRLR_BASEBAND_WRITE_SCAN_ENABLE 0x001A
#define OCF_CTRLR_BASEBAND_WRITE_CLASS_OF_DEVICE 0x0024
#define OCF_CTRLR_BASEBAND_WRITE_SIMPLE_PAIRING_MODE 0x0056
#define OCF_CTRLR_BASEBAND_WRITE_AUTHENTICATION_ENABLE 0x0020
#define OGF_CTRLR_INFORMATIONAL_READBUFFERSIZE 0x0005
#define EVENT_COMMAND_STATUS 0x0F
#define EVENT_COMMAND_COMPLETE 0x0E
#define EVENT_CONNECTION_COMPLETE 0x03
#define EVENT_CONNECTION_REQUEST 0x04
#define EVENT_DISCONNECTION_COMPLETE 0x05
#define EVENT_REMOTE_NAME_REQUEST_COMPLETE 0x07
#define EVENT_PIN_CODE_REQUEST 0x16
#define ERROR_LIMITED_RESOURCES 0x0D
/* Type Defines: */
typedef struct
{
struct
{
int OCF : 10;
int OGF : 6;
} OpCode;
uint8_t ParameterLength;
uint8_t Parameters[];
} Bluetooth_HCICommand_Header_t;
typedef struct
{
uint8_t EventCode;
uint8_t ParameterLength;
} Bluetooth_HCIEvent_Header_t;
typedef struct
{
uint8_t CommandStatus;
uint8_t CommandPackets;
struct
{
int OCF : 10;
int OGF : 6;
} OpCode;
} Bluetooth_HCIEvent_CommandStatus_Header_t;
typedef struct
{
uint8_t RemoteAddress[6];
uint8_t ClassOfDevice_Service;
uint16_t ClassOfDevice_MajorMinor;
uint8_t LinkType;
} Bluetooth_HCIEvent_ConnectionRequest_Header_t;
typedef struct
{
uint8_t Status;
uint16_t ConnectionHandle;
uint8_t RemoteAddress[6];
uint8_t LinkType;
uint8_t EncryptionEnabled;
} Bluetooth_HCIEvent_ConnectionComplete_Header_t;
typedef struct
{
uint8_t RemoteAddress[6];
uint8_t SlaveRole;
} Bluetooth_HCICommand_AcceptConnectionRequest_Params_t;
typedef struct
{
uint8_t RemoteAddress[6];
uint8_t Reason;
} Bluetooth_HCICommand_RejectConnectionRequest_Params_t;
typedef struct
{
uint8_t RemoteAddress[6];
uint8_t PINCodeLength;
char PINCode[16];
} Bluetooth_HCICommand_PinCodeResponse_Params_t;
/* Enums: */
enum Bluetooth_ScanEnable_Modes_t
{
NoScansEnabled = 0,
InquiryScanOnly = 1,
PageScanOnly = 2,
InquiryAndPageScans = 3,
};
enum BluetoothStack_States_t
{
Bluetooth_Init = 0,
Bluetooth_Init_Reset = 1,
Bluetooth_Init_ReadBufferSize = 2,
Bluetooth_Init_SetEventMask = 3,
Bluetooth_Init_SetLocalName = 4,
Bluetooth_Init_SetDeviceClass = 5,
Bluetooth_Init_WriteScanEnable = 6,
Bluetooth_PrepareToProcessEvents = 7,
Bluetooth_ProcessEvents = 8,
Bluetooth_Conn_AcceptConnection = 9,
Bluetooth_Conn_RejectConnection = 10,
Bluetooth_Conn_SendPINCode = 11,
};
/* External Variables: */
extern uint8_t Bluetooth_HCIProcessingState;
/* Function Prototypes: */
void Bluetooth_ProcessHCICommands(void);
#if defined(INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C)
static uint8_t Bluetooth_SendHCICommand(void* Parameters, uint8_t ParamLength);
static bool Bluetooth_GetNextHCIEventHeader(void);
static void Bluetooth_DiscardRemainingHCIEventParameters(void);
static void Bluetooth_ProcessHCICommands(void);
#endif
#endif

View file

@ -0,0 +1,88 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#include "BluetoothStack.h"
Bluetooth_Connection_t Bluetooth_Connection = {IsConnected: false};
Bluetooth_Device_t Bluetooth_DeviceConfiguration ATTR_WEAK =
{
Class: DEVICE_CLASS_MAJOR_MISC,
PINCode: "0000",
Name: "LUFA BT Device"
};
void Bluetooth_Stack_Task(void)
{
if (!(USB_IsConnected) || (USB_HostState != HOST_STATE_Ready))
Bluetooth_HCIProcessingState = Bluetooth_Init;
Bluetooth_ProcessHCICommands();
Bluetooth_ProcessACLPackets();
}
Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource)
{
Bluetooth_Channel_t* CurrentChannelStructure;
for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
{
CurrentChannelStructure = &Bluetooth_Connection.Channels[i];
uint16_t CurrentChannelNumber = ((SearchBySource) ? CurrentChannelStructure->RemoteNumber : CurrentChannelStructure->LocalNumber);
if (CurrentChannelNumber == ChannelNumber)
return CurrentChannelStructure;
}
return NULL;
}
Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM)
{
Bluetooth_Channel_t* CurrentChannelStructure;
for (uint8_t i = 0; i < BLUETOOTH_MAX_OPEN_CHANNELS; i++)
{
CurrentChannelStructure = &Bluetooth_Connection.Channels[i];
if (CurrentChannelStructure->State == Channel_Closed)
{
CurrentChannelStructure->RemoteNumber = RemoteChannelNumber;
CurrentChannelStructure->LocalNumber = (BLUETOOTH_CHANNELNUMBER_BASEOFFSET + i);
CurrentChannelStructure->PSM = PSM;
CurrentChannelStructure->State = Channel_Config;
return CurrentChannelStructure;
}
}
return NULL;
}

View file

@ -0,0 +1,96 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2009.
dean [at] fourwalledcubicle [dot] com
www.fourwalledcubicle.com
*/
/*
Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, 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.
*/
#ifndef _BLUETOOTH_STACK_
#define _BLUETOOTH_STACK_
/* Includes: */
#include <LUFA/Drivers/USB/USB.h>
#include "BluetoothHost.h"
#include "BluetoothHCICommands.h"
#include "BluetoothACLPackets.h"
/* Macros: */
#define BLUETOOTH_MAX_OPEN_CHANNELS 2
#define BLUETOOTH_CHANNELNUMBER_BASEOFFSET 0x0040
#define CHANNEL_LOOKUP_BY_SOURCE true
#define CHANNEL_LOOKUP_BY_DESTINATION false
#define BT_DEBUG(s, ...) printf_P(PSTR(s "\r\n"), __VA_ARGS__)
/* Enums: */
enum Bluetooth_Channel_State_t
{
Channel_Closed = 0,
Channel_WaitConnect = 1,
Channel_WaitConnectRsp = 2,
Channel_Config = 3,
Channel_Open = 4,
Channel_WaitDisconnect = 5,
};
/* Type Defines: */
typedef struct
{
uint8_t State;
uint16_t LocalNumber;
uint16_t RemoteNumber;
uint16_t PSM;
uint16_t MTU;
} Bluetooth_Channel_t;
typedef struct
{
bool IsConnected;
uint16_t ConnectionHandle;
uint8_t DeviceAddress[6];
Bluetooth_Channel_t Channels[BLUETOOTH_MAX_OPEN_CHANNELS];
} Bluetooth_Connection_t;
typedef struct
{
uint32_t Class;
char PINCode[16];
char Name[];
} Bluetooth_Device_t;
/* Function Prototypes: */
Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource);
Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM);
void Bluetooth_Stack_Task(void);
/* External Variables: */
extern Bluetooth_Device_t Bluetooth_DeviceConfiguration;
extern Bluetooth_Connection_t Bluetooth_Connection;
#endif