Copy and convert incomplete Bluetooth Host demo to the library.pull/1469/head
							parent
							
								
									d47df1cd3a
								
							
						
					
					
						commit
						2793c88fc6
					
				@ -0,0 +1,200 @@
 | 
				
			||||
/*
 | 
				
			||||
             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;
 | 
				
			||||
 | 
				
			||||
	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));
 | 
				
			||||
		
 | 
				
			||||
	Bluetooth_DataPacket_Header_t DataHeader;
 | 
				
			||||
	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;			
 | 
				
			||||
			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);
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,121 @@
 | 
				
			||||
/*
 | 
				
			||||
             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_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
 | 
				
			||||
@ -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
 | 
				
			||||
@ -0,0 +1,377 @@
 | 
				
			||||
/*
 | 
				
			||||
             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);
 | 
				
			||||
			Pipe_SetInfiniteINRequests();
 | 
				
			||||
			
 | 
				
			||||
			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;
 | 
				
			||||
	}
 | 
				
			||||
}
 | 
				
			||||
@ -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
 | 
				
			||||
@ -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.
 | 
				
			||||
*/
 | 
				
			||||
 | 
				
			||||
/*
 | 
				
			||||
	Bluetooth Dongle host demo application.
 | 
				
			||||
	
 | 
				
			||||
	** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
 | 
				
			||||
*/
 | 
				
			||||
 | 
				
			||||
#include "BluetoothHost.h"
 | 
				
			||||
 | 
				
			||||
Bluetooth_Device_t Bluetooth_DeviceConfiguration =
 | 
				
			||||
	{
 | 
				
			||||
		Class:   (DEVICE_CLASS_SERVICE_CAPTURING | DEVICE_CLASS_MAJOR_COMPUTER | DEVICE_CLASS_MINOR_COMPUTER_PALM),
 | 
				
			||||
		PINCode: "0000",
 | 
				
			||||
		Name:    "LUFA Bluetooth Demo"
 | 
				
			||||
	};
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
int main(void)
 | 
				
			||||
{
 | 
				
			||||
	SetupHardware();
 | 
				
			||||
	
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
 | 
				
			||||
 | 
				
			||||
	/* Startup message */
 | 
				
			||||
	puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
 | 
				
			||||
	       "Bluetooth Host Demo running.\r\n" ESC_INVERSE_OFF));
 | 
				
			||||
		   
 | 
				
			||||
	for (;;)
 | 
				
			||||
	{
 | 
				
			||||
		Bluetooth_Stack_Task();
 | 
				
			||||
		Bluetooth_Management_Task();
 | 
				
			||||
	}
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void SetupHardware(void)
 | 
				
			||||
{
 | 
				
			||||
	/* Disable watchdog if enabled by bootloader/fuses */
 | 
				
			||||
	MCUSR &= ~(1 << WDRF);
 | 
				
			||||
	wdt_disable();
 | 
				
			||||
 | 
				
			||||
	/* Disable clock division */
 | 
				
			||||
	clock_prescale_set(clock_div_1);
 | 
				
			||||
 | 
				
			||||
	/* Hardware Initialization */
 | 
				
			||||
	SerialStream_Init(9600, false);
 | 
				
			||||
	LEDs_Init();
 | 
				
			||||
	USB_Init();
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void EVENT_USB_DeviceAttached(void)
 | 
				
			||||
{
 | 
				
			||||
	puts_P(PSTR("Device Attached.\r\n"));
 | 
				
			||||
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void EVENT_USB_DeviceUnattached(void)
 | 
				
			||||
{
 | 
				
			||||
	puts_P(PSTR("\r\nDevice Unattached.\r\n"));
 | 
				
			||||
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void EVENT_USB_DeviceEnumerationComplete(void)
 | 
				
			||||
{
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_READY);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void EVENT_USB_HostError(uint8_t ErrorCode)
 | 
				
			||||
{
 | 
				
			||||
	USB_ShutDown();
 | 
				
			||||
 | 
				
			||||
	puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));
 | 
				
			||||
	printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
 | 
				
			||||
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 | 
				
			||||
	for(;;);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
 | 
				
			||||
{
 | 
				
			||||
	puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));
 | 
				
			||||
	printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
 | 
				
			||||
	printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);
 | 
				
			||||
	
 | 
				
			||||
	LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void Bluetooth_Management_Task(void)
 | 
				
			||||
{
 | 
				
			||||
	uint8_t ErrorCode;
 | 
				
			||||
 | 
				
			||||
	switch (USB_HostState)
 | 
				
			||||
	{
 | 
				
			||||
		case HOST_STATE_Addressed:
 | 
				
			||||
			puts_P(PSTR("Getting Device Data.\r\n"));
 | 
				
			||||
		
 | 
				
			||||
			/* Get and process the configuration descriptor data */
 | 
				
			||||
			if ((ErrorCode = ProcessDeviceDescriptor()) != SuccessfulDeviceRead)
 | 
				
			||||
			{
 | 
				
			||||
				if (ErrorCode == ControlErrorDuringDeviceRead)
 | 
				
			||||
				  puts_P(PSTR("Control Error (Get Device).\r\n"));
 | 
				
			||||
				else
 | 
				
			||||
				  puts_P(PSTR("Invalid Device.\r\n"));
 | 
				
			||||
 | 
				
			||||
				printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
 | 
				
			||||
				
 | 
				
			||||
				/* Indicate error via status LEDs */
 | 
				
			||||
				LEDs_SetAllLEDs(LEDS_LED1);
 | 
				
			||||
 | 
				
			||||
				/* Wait until USB device disconnected */
 | 
				
			||||
				while (USB_IsConnected);
 | 
				
			||||
				break;
 | 
				
			||||
			}
 | 
				
			||||
 | 
				
			||||
			puts_P(PSTR("Bluetooth Dongle Detected.\r\n"));
 | 
				
			||||
 | 
				
			||||
			/* Standard request to set the device configuration to configuration 1 */
 | 
				
			||||
			USB_ControlRequest = (USB_Request_Header_t)
 | 
				
			||||
				{
 | 
				
			||||
					bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
 | 
				
			||||
					bRequest:      REQ_SetConfiguration,
 | 
				
			||||
					wValue:        1,
 | 
				
			||||
					wIndex:        0,
 | 
				
			||||
					wLength:       0,
 | 
				
			||||
				};
 | 
				
			||||
				
 | 
				
			||||
			/* Select the control pipe for the request transfer */
 | 
				
			||||
			Pipe_SelectPipe(PIPE_CONTROLPIPE);
 | 
				
			||||
 | 
				
			||||
			/* Send the request, display error and wait for device detatch if request fails */
 | 
				
			||||
			if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
 | 
				
			||||
			{
 | 
				
			||||
				puts_P(PSTR("Control Error (Set Configuration).\r\n"));
 | 
				
			||||
				printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
 | 
				
			||||
 | 
				
			||||
				/* Indicate error via status LEDs */
 | 
				
			||||
				LEDs_SetAllLEDs(LEDS_LED1);
 | 
				
			||||
 | 
				
			||||
				/* Wait until USB device disconnected */
 | 
				
			||||
				while (USB_IsConnected);
 | 
				
			||||
				break;
 | 
				
			||||
			}
 | 
				
			||||
				
 | 
				
			||||
			USB_HostState = HOST_STATE_Configured;
 | 
				
			||||
			break;
 | 
				
			||||
		case HOST_STATE_Configured:
 | 
				
			||||
			puts_P(PSTR("Getting Config Data.\r\n"));
 | 
				
			||||
		
 | 
				
			||||
			/* Get and process the configuration descriptor data */
 | 
				
			||||
			if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
 | 
				
			||||
			{
 | 
				
			||||
				if (ErrorCode == ControlErrorDuringConfigRead)
 | 
				
			||||
				  puts_P(PSTR("Control Error (Get Configuration).\r\n"));
 | 
				
			||||
				else
 | 
				
			||||
				  puts_P(PSTR("Invalid Device.\r\n"));
 | 
				
			||||
 | 
				
			||||
				printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
 | 
				
			||||
				
 | 
				
			||||
				/* Indicate error via status LEDs */
 | 
				
			||||
				LEDs_SetAllLEDs(LEDS_LED1);
 | 
				
			||||
 | 
				
			||||
				/* Wait until USB device disconnected */
 | 
				
			||||
				while (USB_IsConnected);
 | 
				
			||||
				break;
 | 
				
			||||
			}
 | 
				
			||||
 | 
				
			||||
			puts_P(PSTR("Bluetooth Dongle Enumerated.\r\n"));
 | 
				
			||||
 | 
				
			||||
			USB_HostState = HOST_STATE_Ready;
 | 
				
			||||
			break;
 | 
				
			||||
		case HOST_STATE_Ready:
 | 
				
			||||
			/* Do nothing, Bluetooth stack will take care of enumeration */
 | 
				
			||||
			
 | 
				
			||||
			break;
 | 
				
			||||
	}
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,95 @@
 | 
				
			||||
/*
 | 
				
			||||
             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_HOST_H_
 | 
				
			||||
#define _BLUETOOTH_HOST_H_
 | 
				
			||||
 | 
				
			||||
	/* Includes: */
 | 
				
			||||
		#include <avr/io.h>
 | 
				
			||||
		#include <avr/wdt.h>
 | 
				
			||||
		#include <avr/pgmspace.h>
 | 
				
			||||
		#include <avr/power.h>
 | 
				
			||||
		#include <stdio.h>
 | 
				
			||||
 | 
				
			||||
		#include "BluetoothStack.h"
 | 
				
			||||
 | 
				
			||||
		#include "DeviceDescriptor.h"
 | 
				
			||||
		#include "ConfigDescriptor.h"
 | 
				
			||||
 | 
				
			||||
		#include <LUFA/Version.h>
 | 
				
			||||
		#include <LUFA/Drivers/Misc/TerminalCodes.h>
 | 
				
			||||
		#include <LUFA/Drivers/USB/USB.h>
 | 
				
			||||
		#include <LUFA/Drivers/Peripheral/SerialStream.h>
 | 
				
			||||
		#include <LUFA/Drivers/Board/LEDs.h>
 | 
				
			||||
		
 | 
				
			||||
	/* Macros: */
 | 
				
			||||
		/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
 | 
				
			||||
		#define LEDMASK_USB_NOTREADY      LEDS_LED1
 | 
				
			||||
 | 
				
			||||
		/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
 | 
				
			||||
		#define LEDMASK_USB_ENUMERATING  (LEDS_LED2 | LEDS_LED3)
 | 
				
			||||
 | 
				
			||||
		/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
 | 
				
			||||
		#define LEDMASK_USB_READY        (LEDS_LED2 | LEDS_LED4)
 | 
				
			||||
 | 
				
			||||
		/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
 | 
				
			||||
		#define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)
 | 
				
			||||
 | 
				
			||||
		#define BLUETOOTH_DATA_IN_PIPE          1
 | 
				
			||||
		#define BLUETOOTH_DATA_OUT_PIPE         2
 | 
				
			||||
		#define BLUETOOTH_EVENTS_PIPE           3
 | 
				
			||||
 | 
				
			||||
	/* Task Definitions: */
 | 
				
			||||
		void Bluetooth_Management_Task(void);
 | 
				
			||||
 | 
				
			||||
	/* Enums: */
 | 
				
			||||
		/** Enum for the possible status codes for passing to the UpdateStatus() function. */
 | 
				
			||||
		enum MouseHostViaInt_StatusCodes_t
 | 
				
			||||
		{
 | 
				
			||||
			Status_USBNotReady        = 0, /**< USB is not ready (disconnected from a USB device) */
 | 
				
			||||
			Status_USBEnumerating     = 1, /**< USB interface is enumerating */
 | 
				
			||||
			Status_USBReady           = 2, /**< USB interface is connected and ready */
 | 
				
			||||
			Status_EnumerationError   = 3, /**< Software error while enumerating the attached USB device */
 | 
				
			||||
			Status_HardwareError      = 4, /**< Hardware error while enumerating the attached USB device */
 | 
				
			||||
			Status_BluetoothConnected = 5, /**< Bluetooth stack connected to device and idle */
 | 
				
			||||
			Status_BluetoothBusy      = 6, /**< Bluetooth stack busy */ 
 | 
				
			||||
		};
 | 
				
			||||
		
 | 
				
			||||
	/* Event Handlers: */
 | 
				
			||||
		void EVENT_USB_DeviceAttached(void);
 | 
				
			||||
		void EVENT_USB_DeviceUnattached(void);
 | 
				
			||||
		void EVENT_USB_DeviceEnumerationComplete(void);
 | 
				
			||||
		void EVENT_USB_HostError(uint8_t ErrorCode);
 | 
				
			||||
		void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode);
 | 
				
			||||
 | 
				
			||||
	/* Function Prototypes: */
 | 
				
			||||
		void SetupHardware(void);
 | 
				
			||||
		
 | 
				
			||||
#endif
 | 
				
			||||
@ -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;
 | 
				
			||||
}
 | 
				
			||||
@ -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
 | 
				
			||||
@ -0,0 +1,139 @@
 | 
				
			||||
/*
 | 
				
			||||
             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 "ConfigDescriptor.h"
 | 
				
			||||
 | 
				
			||||
uint8_t ProcessConfigurationDescriptor(void)
 | 
				
			||||
{
 | 
				
			||||
	uint8_t* ConfigDescriptorData;
 | 
				
			||||
	uint16_t ConfigDescriptorSize;
 | 
				
			||||
	uint8_t  FoundEndpoints = 0;
 | 
				
			||||
	
 | 
				
			||||
	/* Get Configuration Descriptor size from the device */
 | 
				
			||||
	if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, NULL) != HOST_SENDCONTROL_Successful)
 | 
				
			||||
	  return ControlErrorDuringConfigRead;
 | 
				
			||||
	
 | 
				
			||||
	/* Ensure that the Configuration Descriptor isn't too large */
 | 
				
			||||
	if (ConfigDescriptorSize > MAX_CONFIG_DESCRIPTOR_SIZE)
 | 
				
			||||
	  return DescriptorTooLarge;
 | 
				
			||||
	  
 | 
				
			||||
	/* Allocate enough memory for the entire config descriptor */
 | 
				
			||||
	ConfigDescriptorData = alloca(ConfigDescriptorSize);
 | 
				
			||||
 | 
				
			||||
	/* Retrieve the entire configuration descriptor into the allocated buffer */
 | 
				
			||||
	USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize, ConfigDescriptorData);
 | 
				
			||||
	
 | 
				
			||||
	/* Validate returned data - ensure first entry is a configuration header descriptor */
 | 
				
			||||
	if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
 | 
				
			||||
	  return InvalidConfigDataReturned;
 | 
				
			||||
 | 
				
			||||
	/* The bluetooth USB transport addendium mandates that the data (not streaming voice) endpoints
 | 
				
			||||
	   be in the first interface descriptor (interface 0) */
 | 
				
			||||
	USB_GetNextDescriptorOfType(&ConfigDescriptorSize, &ConfigDescriptorData, DTYPE_Interface);
 | 
				
			||||
	
 | 
				
			||||
	/* Ensure that an interface was found, and the end of the descriptor was not reached */
 | 
				
			||||
	if (!(ConfigDescriptorSize))
 | 
				
			||||
	  return NoInterfaceFound;
 | 
				
			||||
 | 
				
			||||
	/* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */
 | 
				
			||||
	while (FoundEndpoints != ((1 << BLUETOOTH_DATA_IN_PIPE) | (1 << BLUETOOTH_DATA_OUT_PIPE) |
 | 
				
			||||
	                          (1 << BLUETOOTH_EVENTS_PIPE)))
 | 
				
			||||
	{
 | 
				
			||||
		/* Fetch the next endpoint from the current bluetooth interface */
 | 
				
			||||
		if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
 | 
				
			||||
		                                   NextInterfaceBluetoothDataEndpoint))
 | 
				
			||||
		{
 | 
				
			||||
			/* Descriptor not found, error out */
 | 
				
			||||
			return NoEndpointFound;
 | 
				
			||||
		}
 | 
				
			||||
		
 | 
				
			||||
		USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
 | 
				
			||||
 | 
				
			||||
		/* Check if the endpoint is a bulk or interrupt type endpoint */
 | 
				
			||||
		if ((EndpointData->Attributes & EP_TYPE_MASK) == EP_TYPE_INTERRUPT)
 | 
				
			||||
		{
 | 
				
			||||
			if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
 | 
				
			||||
			{
 | 
				
			||||
				/* Configure the events IN pipe */
 | 
				
			||||
				Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
 | 
				
			||||
								   EndpointData->EndpointAddress, EndpointData->EndpointSize,
 | 
				
			||||
								   PIPE_BANK_SINGLE);
 | 
				
			||||
 | 
				
			||||
				Pipe_SetInfiniteINRequests();
 | 
				
			||||
				Pipe_SetInterruptPeriod(EndpointData->PollingIntervalMS);
 | 
				
			||||
				
 | 
				
			||||
				/* Set the flag indicating that the events notification pipe has been found */
 | 
				
			||||
				FoundEndpoints |= (1 << BLUETOOTH_EVENTS_PIPE);	
 | 
				
			||||
			}
 | 
				
			||||
		}
 | 
				
			||||
		else
 | 
				
			||||
		{
 | 
				
			||||
			if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
 | 
				
			||||
			{
 | 
				
			||||
				/* Configure the data IN pipe */
 | 
				
			||||
				Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE, EP_TYPE_BULK, PIPE_TOKEN_IN,
 | 
				
			||||
								   EndpointData->EndpointAddress, EndpointData->EndpointSize,
 | 
				
			||||
								   PIPE_BANK_SINGLE);
 | 
				
			||||
 | 
				
			||||
				Pipe_SetInfiniteINRequests();
 | 
				
			||||
 | 
				
			||||
				/* Set the flag indicating that the data IN pipe has been found */
 | 
				
			||||
				FoundEndpoints |= (1 << BLUETOOTH_DATA_IN_PIPE);
 | 
				
			||||
			}
 | 
				
			||||
			else
 | 
				
			||||
			{
 | 
				
			||||
				/* Configure the data OUT pipe */
 | 
				
			||||
				Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE, EP_TYPE_BULK, PIPE_TOKEN_OUT,
 | 
				
			||||
								   EndpointData->EndpointAddress, EndpointData->EndpointSize,
 | 
				
			||||
								   PIPE_BANK_SINGLE);
 | 
				
			||||
 | 
				
			||||
				/* Set the flag indicating that the data OUT pipe has been found */
 | 
				
			||||
				FoundEndpoints |= (1 << BLUETOOTH_DATA_OUT_PIPE);
 | 
				
			||||
			}		
 | 
				
			||||
		}
 | 
				
			||||
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	/* Valid data found, return success */
 | 
				
			||||
	return SuccessfulConfigRead;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor)
 | 
				
			||||
{
 | 
				
			||||
	/* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */
 | 
				
			||||
 | 
				
			||||
	if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
 | 
				
			||||
	  return DESCRIPTOR_SEARCH_Found;
 | 
				
			||||
	else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
 | 
				
			||||
	  return DESCRIPTOR_SEARCH_Fail;
 | 
				
			||||
 | 
				
			||||
	return DESCRIPTOR_SEARCH_NotFound;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
@ -0,0 +1,58 @@
 | 
				
			||||
/*
 | 
				
			||||
             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 _CONFIGDESCRIPTOR_H_
 | 
				
			||||
#define _CONFIGDESCRIPTOR_H_
 | 
				
			||||
 | 
				
			||||
	/* Includes: */
 | 
				
			||||
		#include <LUFA/Drivers/USB/USB.h>                        // USB Functionality
 | 
				
			||||
		
 | 
				
			||||
		#include "BluetoothHost.h"
 | 
				
			||||
		
 | 
				
			||||
	/* Macros: */
 | 
				
			||||
		#define MAX_CONFIG_DESCRIPTOR_SIZE       512
 | 
				
			||||
 | 
				
			||||
	/* Enums: */
 | 
				
			||||
		enum BluetoothHost_GetConfigDescriptorDataCodes_t
 | 
				
			||||
		{
 | 
				
			||||
			SuccessfulConfigRead                 = 0,
 | 
				
			||||
			ControlErrorDuringConfigRead         = 1,
 | 
				
			||||
			InvalidConfigDataReturned            = 2,
 | 
				
			||||
			DescriptorTooLarge                   = 3,
 | 
				
			||||
			NoInterfaceFound                     = 4,
 | 
				
			||||
			NoEndpointFound                      = 5,
 | 
				
			||||
		};
 | 
				
			||||
	
 | 
				
			||||
	/* Function Prototypes: */
 | 
				
			||||
		uint8_t ProcessConfigurationDescriptor(void);
 | 
				
			||||
		
 | 
				
			||||
		uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor);
 | 
				
			||||
 | 
				
			||||
#endif
 | 
				
			||||
@ -0,0 +1,66 @@
 | 
				
			||||
/*
 | 
				
			||||
             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 "DeviceDescriptor.h"
 | 
				
			||||
 | 
				
			||||
uint8_t ProcessDeviceDescriptor(void)
 | 
				
			||||
{
 | 
				
			||||
	USB_Descriptor_Device_t DeviceDescriptor;
 | 
				
			||||
 | 
				
			||||
	/* Standard request to get the device descriptor */
 | 
				
			||||
	USB_ControlRequest = (USB_Request_Header_t)
 | 
				
			||||
		{
 | 
				
			||||
			bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_DEVICE),
 | 
				
			||||
			bRequest:      REQ_GetDescriptor,
 | 
				
			||||
			wValue:        (DTYPE_Device << 8),
 | 
				
			||||
			wIndex:        0,
 | 
				
			||||
			wLength:       sizeof(USB_Descriptor_Device_t),
 | 
				
			||||
		};
 | 
				
			||||
 | 
				
			||||
	/* Select the control pipe for the request transfer */
 | 
				
			||||
	Pipe_SelectPipe(PIPE_CONTROLPIPE);
 | 
				
			||||
 | 
				
			||||
	/* Send the request to retrieve the device descriptor */
 | 
				
			||||
	if (USB_Host_SendControlRequest((void*)&DeviceDescriptor) != HOST_SENDCONTROL_Successful)
 | 
				
			||||
	  return ControlErrorDuringDeviceRead;
 | 
				
			||||
	  
 | 
				
			||||
	/* Validate returned data - ensure the returned data is a device descriptor */
 | 
				
			||||
	if (DeviceDescriptor.Header.Type != DTYPE_Device)
 | 
				
			||||
	  return InvalidDeviceDataReturned;
 | 
				
			||||
	  
 | 
				
			||||
	if ((DeviceDescriptor.Class != BLUETOOTH_DEVICE_CLASS) ||
 | 
				
			||||
	    (DeviceDescriptor.SubClass != BLUETOOTH_DEVICE_SUBCLASS) ||
 | 
				
			||||
	    (DeviceDescriptor.Protocol != BLUETOOTH_DEVICE_PROTOCOL))
 | 
				
			||||
	{
 | 
				
			||||
		return IncorrectDevice;
 | 
				
			||||
	}
 | 
				
			||||
	
 | 
				
			||||
	return SuccessfulDeviceRead;
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,56 @@
 | 
				
			||||
/*
 | 
				
			||||
             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 _DEVICEDESCRIPTOR_H_
 | 
				
			||||
#define _DEVICEDESCRIPTOR_H_
 | 
				
			||||
 | 
				
			||||
	/* Includes: */
 | 
				
			||||
		#include <LUFA/Drivers/USB/USB.h>                        // USB Functionality
 | 
				
			||||
		
 | 
				
			||||
		#include "BluetoothHost.h"
 | 
				
			||||
		
 | 
				
			||||
	/* Macros: */
 | 
				
			||||
		#define BLUETOOTH_DEVICE_CLASS           0xE0
 | 
				
			||||
		#define BLUETOOTH_DEVICE_SUBCLASS        0x01
 | 
				
			||||
		#define BLUETOOTH_DEVICE_PROTOCOL        0x01
 | 
				
			||||
 | 
				
			||||
	/* Enums: */
 | 
				
			||||
		enum BluetoothHost_GetDeviceDescriptorDataCodes_t
 | 
				
			||||
		{
 | 
				
			||||
			SuccessfulDeviceRead                 = 0,
 | 
				
			||||
			ControlErrorDuringDeviceRead         = 1,
 | 
				
			||||
			InvalidDeviceDataReturned            = 2,
 | 
				
			||||
			IncorrectDevice                      = 3,
 | 
				
			||||
		};
 | 
				
			||||
 | 
				
			||||
	/* Function Prototypes: */
 | 
				
			||||
		uint8_t ProcessDeviceDescriptor(void);	
 | 
				
			||||
 | 
				
			||||
#endif
 | 
				
			||||
@ -0,0 +1,737 @@
 | 
				
			||||
# Hey Emacs, this is a -*- makefile -*-
 | 
				
			||||
#----------------------------------------------------------------------------
 | 
				
			||||
# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
 | 
				
			||||
#  >> Modified for use with the LUFA project. <<
 | 
				
			||||
#
 | 
				
			||||
# Released to the Public Domain
 | 
				
			||||
#
 | 
				
			||||
# Additional material for this makefile was written by:
 | 
				
			||||
# Peter Fleury
 | 
				
			||||
# Tim Henigan
 | 
				
			||||
# Colin O'Flynn
 | 
				
			||||
# Reiner Patommel
 | 
				
			||||
# Markus Pfaff
 | 
				
			||||
# Sander Pool
 | 
				
			||||
# Frederik Rouleau
 | 
				
			||||
# Carlos Lamas
 | 
				
			||||
# Dean Camera
 | 
				
			||||
# Opendous Inc.
 | 
				
			||||
# Denver Gingerich
 | 
				
			||||
#
 | 
				
			||||
#----------------------------------------------------------------------------
 | 
				
			||||
# On command line:
 | 
				
			||||
#
 | 
				
			||||
# make all = Make software.
 | 
				
			||||
#
 | 
				
			||||
# make clean = Clean out built project files.
 | 
				
			||||
#
 | 
				
			||||
# make coff = Convert ELF to AVR COFF.
 | 
				
			||||
#
 | 
				
			||||
# make extcoff = Convert ELF to AVR Extended COFF.
 | 
				
			||||
#
 | 
				
			||||
# make program = Download the hex file to the device, using avrdude.
 | 
				
			||||
#                Please customize the avrdude settings below first!
 | 
				
			||||
#
 | 
				
			||||
# make dfu = Download the hex file to the device, using dfu-programmer (must
 | 
				
			||||
#            have dfu-programmer installed).
 | 
				
			||||
#
 | 
				
			||||
# make flip = Download the hex file to the device, using Atmel FLIP (must
 | 
				
			||||
#             have Atmel FLIP installed).
 | 
				
			||||
#
 | 
				
			||||
# make dfu-ee = Download the eeprom file to the device, using dfu-programmer
 | 
				
			||||
#               (must have dfu-programmer installed).
 | 
				
			||||
#
 | 
				
			||||
# make flip-ee = Download the eeprom file to the device, using Atmel FLIP
 | 
				
			||||
#                (must have Atmel FLIP installed).
 | 
				
			||||
#
 | 
				
			||||
# make doxygen = Generate DoxyGen documentation for the project (must have
 | 
				
			||||
#                DoxyGen installed)
 | 
				
			||||
#
 | 
				
			||||
# make debug = Start either simulavr or avarice as specified for debugging, 
 | 
				
			||||
#              with avr-gdb or avr-insight as the front end for debugging.
 | 
				
			||||
#
 | 
				
			||||
# make filename.s = Just compile filename.c into the assembler code only.
 | 
				
			||||
#
 | 
				
			||||
# make filename.i = Create a preprocessed source file for use in submitting
 | 
				
			||||
#                   bug reports to the GCC project.
 | 
				
			||||
#
 | 
				
			||||
# To rebuild project do "make clean" then "make all".
 | 
				
			||||
#----------------------------------------------------------------------------
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# MCU name
 | 
				
			||||
MCU = at90usb1287
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Target board (see library "Board Types" documentation, USER or blank for projects not requiring
 | 
				
			||||
# LUFA board drivers). If USER is selected, put custom board drivers in a directory called 
 | 
				
			||||
# "Board" inside the application directory.
 | 
				
			||||
BOARD  = USBKEY
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Processor frequency.
 | 
				
			||||
#     This will define a symbol, F_CPU, in all source code files equal to the 
 | 
				
			||||
#     processor frequency. You can then use this symbol in your source code to 
 | 
				
			||||
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
 | 
				
			||||
#     automatically to create a 32-bit value in your source code.
 | 
				
			||||
#     Typical values are:
 | 
				
			||||
#         F_CPU =  1000000
 | 
				
			||||
#         F_CPU =  1843200
 | 
				
			||||
#         F_CPU =  2000000
 | 
				
			||||
#         F_CPU =  3686400
 | 
				
			||||
#         F_CPU =  4000000
 | 
				
			||||
#         F_CPU =  7372800
 | 
				
			||||
#         F_CPU =  8000000
 | 
				
			||||
#         F_CPU = 11059200
 | 
				
			||||
#         F_CPU = 14745600
 | 
				
			||||
#         F_CPU = 16000000
 | 
				
			||||
#         F_CPU = 18432000
 | 
				
			||||
#         F_CPU = 20000000
 | 
				
			||||
F_CPU = 8000000
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Input clock frequency.
 | 
				
			||||
#     This will define a symbol, F_CLOCK, in all source code files equal to the 
 | 
				
			||||
#     input clock frequency (before any prescaling is performed). This value may
 | 
				
			||||
#     differ from F_CPU if prescaling is used on the latter, and is required as the
 | 
				
			||||
#     raw input clock is fed directly to the PLL sections of the AVR for high speed
 | 
				
			||||
#     clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
 | 
				
			||||
#     at the end, this will be done automatically to create a 32-bit value in your
 | 
				
			||||
#     source code.
 | 
				
			||||
#
 | 
				
			||||
#     If no clock division is performed on the input clock inside the AVR (via the
 | 
				
			||||
#     CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
 | 
				
			||||
F_CLOCK = 8000000
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Output format. (can be srec, ihex, binary)
 | 
				
			||||
FORMAT = ihex
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Target file name (without extension).
 | 
				
			||||
TARGET = BluetoothHost
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Object files directory
 | 
				
			||||
#     To put object files in current directory, use a dot (.), do NOT make
 | 
				
			||||
#     this an empty or blank macro!
 | 
				
			||||
OBJDIR = .
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Path to the LUFA library
 | 
				
			||||
LUFA_PATH = ../../../..
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# List C source files here. (C dependencies are automatically generated.)
 | 
				
			||||
SRC = $(TARGET).c                                                 \
 | 
				
			||||
	  DeviceDescriptor.c                                          \
 | 
				
			||||
	  ConfigDescriptor.c                                          \
 | 
				
			||||
	  BluetoothStack.c                                            \
 | 
				
			||||
	  BluetoothHCICommands.c                                      \
 | 
				
			||||
	  BluetoothACLPackets.c                                       \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/Peripheral/SerialStream.c         \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/Peripheral/Serial.c               \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/DevChapter9.c        \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Endpoint.c           \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Host.c               \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/HostChapter9.c       \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/LowLevel.c           \
 | 
				
			||||
 	  $(LUFA_PATH)/LUFA/Drivers/USB/LowLevel/Pipe.c               \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/HighLevel/Events.c            \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/HighLevel/USBInterrupt.c      \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/HighLevel/USBTask.c           \
 | 
				
			||||
	  $(LUFA_PATH)/LUFA/Drivers/USB/HighLevel/ConfigDescriptor.c  \
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# List C++ source files here. (C dependencies are automatically generated.)
 | 
				
			||||
CPPSRC = 
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# List Assembler source files here.
 | 
				
			||||
#     Make them always end in a capital .S.  Files ending in a lowercase .s
 | 
				
			||||
#     will not be considered source files but generated files (assembler
 | 
				
			||||
#     output from the compiler), and will be deleted upon "make clean"!
 | 
				
			||||
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
 | 
				
			||||
#     it will preserve the spelling of the filenames, and gcc itself does
 | 
				
			||||
#     care about how the name is spelled on its command-line.
 | 
				
			||||
ASRC =
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Optimization level, can be [0, 1, 2, 3, s]. 
 | 
				
			||||
#     0 = turn off optimization. s = optimize for size.
 | 
				
			||||
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
 | 
				
			||||
OPT = s
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Debugging format.
 | 
				
			||||
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
 | 
				
			||||
#     AVR Studio 4.10 requires dwarf-2.
 | 
				
			||||
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
 | 
				
			||||
DEBUG = dwarf-2
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# List any extra directories to look for include files here.
 | 
				
			||||
#     Each directory must be seperated by a space.
 | 
				
			||||
#     Use forward slashes for directory separators.
 | 
				
			||||
#     For a directory that has spaces, enclose it in quotes.
 | 
				
			||||
EXTRAINCDIRS = $(LUFA_PATH)/
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compiler flag to set the C Standard level.
 | 
				
			||||
#     c89   = "ANSI" C
 | 
				
			||||
#     gnu89 = c89 plus GCC extensions
 | 
				
			||||
#     c99   = ISO C99 standard (not yet fully implemented)
 | 
				
			||||
#     gnu99 = c99 plus GCC extensions
 | 
				
			||||
CSTANDARD = -std=gnu99
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Place -D or -U options here for C sources
 | 
				
			||||
CDEFS  = -DF_CPU=$(F_CPU)UL -DF_CLOCK=$(F_CLOCK)UL -DBOARD=BOARD_$(BOARD)
 | 
				
			||||
CDEFS += -DUSE_NONSTANDARD_DESCRIPTOR_NAMES -DUSB_HOST_ONLY -DNO_STREAM_CALLBACKS
 | 
				
			||||
CDEFS += -DUSE_STATIC_OPTIONS="(USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Place -D or -U options here for ASM sources
 | 
				
			||||
ADEFS = -DF_CPU=$(F_CPU)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Place -D or -U options here for C++ sources
 | 
				
			||||
CPPDEFS = -DF_CPU=$(F_CPU)UL
 | 
				
			||||
#CPPDEFS += -D__STDC_LIMIT_MACROS
 | 
				
			||||
#CPPDEFS += -D__STDC_CONSTANT_MACROS
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Compiler Options C ----------------
 | 
				
			||||
#  -g*:          generate debugging information
 | 
				
			||||
#  -O*:          optimization level
 | 
				
			||||
#  -f...:        tuning, see GCC manual and avr-libc documentation
 | 
				
			||||
#  -Wall...:     warning level
 | 
				
			||||
#  -Wa,...:      tell GCC to pass this to the assembler.
 | 
				
			||||
#    -adhlns...: create assembler listing
 | 
				
			||||
CFLAGS = -g$(DEBUG)
 | 
				
			||||
CFLAGS += $(CDEFS)
 | 
				
			||||
CFLAGS += -O$(OPT)
 | 
				
			||||
CFLAGS += -funsigned-char
 | 
				
			||||
CFLAGS += -funsigned-bitfields
 | 
				
			||||
CFLAGS += -ffunction-sections
 | 
				
			||||
CFLAGS += -fpack-struct
 | 
				
			||||
CFLAGS += -fshort-enums
 | 
				
			||||
CFLAGS += -finline-limit=20
 | 
				
			||||
CFLAGS += -Wall
 | 
				
			||||
CFLAGS += -Wstrict-prototypes
 | 
				
			||||
CFLAGS += -Wundef
 | 
				
			||||
#CFLAGS += -fno-unit-at-a-time
 | 
				
			||||
#CFLAGS += -Wunreachable-code
 | 
				
			||||
#CFLAGS += -Wsign-compare
 | 
				
			||||
CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
 | 
				
			||||
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
 | 
				
			||||
CFLAGS += $(CSTANDARD)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Compiler Options C++ ----------------
 | 
				
			||||
#  -g*:          generate debugging information
 | 
				
			||||
#  -O*:          optimization level
 | 
				
			||||
#  -f...:        tuning, see GCC manual and avr-libc documentation
 | 
				
			||||
#  -Wall...:     warning level
 | 
				
			||||
#  -Wa,...:      tell GCC to pass this to the assembler.
 | 
				
			||||
#    -adhlns...: create assembler listing
 | 
				
			||||
CPPFLAGS = -g$(DEBUG)
 | 
				
			||||
CPPFLAGS += $(CPPDEFS)
 | 
				
			||||
CPPFLAGS += -O$(OPT)
 | 
				
			||||
CPPFLAGS += -funsigned-char
 | 
				
			||||
CPPFLAGS += -funsigned-bitfields
 | 
				
			||||
CPPFLAGS += -fpack-struct
 | 
				
			||||
CPPFLAGS += -fshort-enums
 | 
				
			||||
CPPFLAGS += -fno-exceptions
 | 
				
			||||
CPPFLAGS += -Wall
 | 
				
			||||
CFLAGS += -Wundef
 | 
				
			||||
#CPPFLAGS += -mshort-calls
 | 
				
			||||
#CPPFLAGS += -fno-unit-at-a-time
 | 
				
			||||
#CPPFLAGS += -Wstrict-prototypes
 | 
				
			||||
#CPPFLAGS += -Wunreachable-code
 | 
				
			||||
#CPPFLAGS += -Wsign-compare
 | 
				
			||||
CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
 | 
				
			||||
CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
 | 
				
			||||
#CPPFLAGS += $(CSTANDARD)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Assembler Options ----------------
 | 
				
			||||
#  -Wa,...:   tell GCC to pass this to the assembler.
 | 
				
			||||
#  -adhlns:   create listing
 | 
				
			||||
#  -gstabs:   have the assembler create line number information; note that
 | 
				
			||||
#             for use in COFF files, additional information about filenames
 | 
				
			||||
#             and function names needs to be present in the assembler source
 | 
				
			||||
#             files -- see avr-libc docs [FIXME: not yet described there]
 | 
				
			||||
#  -listing-cont-lines: Sets the maximum number of continuation lines of hex 
 | 
				
			||||
#       dump that will be displayed for a given single line of source input.
 | 
				
			||||
ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Library Options ----------------
 | 
				
			||||
# Minimalistic printf version
 | 
				
			||||
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
 | 
				
			||||
 | 
				
			||||
# Floating point printf version (requires MATH_LIB = -lm below)
 | 
				
			||||
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
 | 
				
			||||
 | 
				
			||||
# If this is left blank, then it will use the Standard printf version.
 | 
				
			||||
PRINTF_LIB = 
 | 
				
			||||
#PRINTF_LIB = $(PRINTF_LIB_MIN)
 | 
				
			||||
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Minimalistic scanf version
 | 
				
			||||
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
 | 
				
			||||
 | 
				
			||||
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
 | 
				
			||||
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
 | 
				
			||||
 | 
				
			||||
# If this is left blank, then it will use the Standard scanf version.
 | 
				
			||||
SCANF_LIB = 
 | 
				
			||||
#SCANF_LIB = $(SCANF_LIB_MIN)
 | 
				
			||||
#SCANF_LIB = $(SCANF_LIB_FLOAT)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
MATH_LIB = -lm
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# List any extra directories to look for libraries here.
 | 
				
			||||
#     Each directory must be seperated by a space.
 | 
				
			||||
#     Use forward slashes for directory separators.
 | 
				
			||||
#     For a directory that has spaces, enclose it in quotes.
 | 
				
			||||
EXTRALIBDIRS = 
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- External Memory Options ----------------
 | 
				
			||||
 | 
				
			||||
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
 | 
				
			||||
# used for variables (.data/.bss) and heap (malloc()).
 | 
				
			||||
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
 | 
				
			||||
 | 
				
			||||
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
 | 
				
			||||
# only used for heap (malloc()).
 | 
				
			||||
#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
 | 
				
			||||
 | 
				
			||||
EXTMEMOPTS =
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Linker Options ----------------
 | 
				
			||||
#  -Wl,...:     tell GCC to pass this to linker.
 | 
				
			||||
#    -Map:      create map file
 | 
				
			||||
#    --cref:    add cross reference to  map file
 | 
				
			||||
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
 | 
				
			||||
LDFLAGS += -Wl,--relax 
 | 
				
			||||
LDFLAGS += -Wl,--gc-sections
 | 
				
			||||
LDFLAGS += $(EXTMEMOPTS)
 | 
				
			||||
LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
 | 
				
			||||
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
 | 
				
			||||
#LDFLAGS += -T linker_script.x
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Programming Options (avrdude) ----------------
 | 
				
			||||
 | 
				
			||||
# Programming hardware: alf avr910 avrisp bascom bsd 
 | 
				
			||||
# dt006 pavr picoweb pony-stk200 sp12 stk200 stk500
 | 
				
			||||
#
 | 
				
			||||
# Type: avrdude -c ?
 | 
				
			||||
# to get a full listing.
 | 
				
			||||
#
 | 
				
			||||
AVRDUDE_PROGRAMMER = jtagmkII
 | 
				
			||||
 | 
				
			||||
# com1 = serial port. Use lpt1 to connect to parallel port.
 | 
				
			||||
AVRDUDE_PORT = usb
 | 
				
			||||
 | 
				
			||||
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
 | 
				
			||||
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Uncomment the following if you want avrdude's erase cycle counter.
 | 
				
			||||
# Note that this counter needs to be initialized first using -Yn,
 | 
				
			||||
# see avrdude manual.
 | 
				
			||||
#AVRDUDE_ERASE_COUNTER = -y
 | 
				
			||||
 | 
				
			||||
# Uncomment the following if you do /not/ wish a verification to be
 | 
				
			||||
# performed after programming the device.
 | 
				
			||||
#AVRDUDE_NO_VERIFY = -V
 | 
				
			||||
 | 
				
			||||
# Increase verbosity level.  Please use this when submitting bug
 | 
				
			||||
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> 
 | 
				
			||||
# to submit bug reports.
 | 
				
			||||
#AVRDUDE_VERBOSE = -v -v
 | 
				
			||||
 | 
				
			||||
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
 | 
				
			||||
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
 | 
				
			||||
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
 | 
				
			||||
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#---------------- Debugging Options ----------------
 | 
				
			||||
 | 
				
			||||
# For simulavr only - target MCU frequency.
 | 
				
			||||
DEBUG_MFREQ = $(F_CPU)
 | 
				
			||||
 | 
				
			||||
# Set the DEBUG_UI to either gdb or insight.
 | 
				
			||||
# DEBUG_UI = gdb
 | 
				
			||||
DEBUG_UI = insight
 | 
				
			||||
 | 
				
			||||
# Set the debugging back-end to either avarice, simulavr.
 | 
				
			||||
DEBUG_BACKEND = avarice
 | 
				
			||||
#DEBUG_BACKEND = simulavr
 | 
				
			||||
 | 
				
			||||
# GDB Init Filename.
 | 
				
			||||
GDBINIT_FILE = __avr_gdbinit
 | 
				
			||||
 | 
				
			||||
# When using avarice settings for the JTAG
 | 
				
			||||
JTAG_DEV = /dev/com1
 | 
				
			||||
 | 
				
			||||
# Debugging port used to communicate between GDB / avarice / simulavr.
 | 
				
			||||
DEBUG_PORT = 4242
 | 
				
			||||
 | 
				
			||||
# Debugging host used to communicate between GDB / avarice / simulavr, normally
 | 
				
			||||
#     just set to localhost unless doing some sort of crazy debugging when 
 | 
				
			||||
#     avarice is running on a different computer.
 | 
				
			||||
DEBUG_HOST = localhost
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
#============================================================================
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Define programs and commands.
 | 
				
			||||
SHELL = sh
 | 
				
			||||
CC = avr-gcc
 | 
				
			||||
OBJCOPY = avr-objcopy
 | 
				
			||||
OBJDUMP = avr-objdump
 | 
				
			||||
SIZE = avr-size
 | 
				
			||||
AR = avr-ar rcs
 | 
				
			||||
NM = avr-nm
 | 
				
			||||
AVRDUDE = avrdude
 | 
				
			||||
REMOVE = rm -f
 | 
				
			||||
REMOVEDIR = rm -rf
 | 
				
			||||
COPY = cp
 | 
				
			||||
WINSHELL = cmd
 | 
				
			||||
 | 
				
			||||
# Define Messages
 | 
				
			||||
# English
 | 
				
			||||
MSG_ERRORS_NONE = Errors: none
 | 
				
			||||
MSG_BEGIN = -------- begin --------
 | 
				
			||||
MSG_END = --------  end  --------
 | 
				
			||||
MSG_SIZE_BEFORE = Size before: 
 | 
				
			||||
MSG_SIZE_AFTER = Size after:
 | 
				
			||||
MSG_COFF = Converting to AVR COFF:
 | 
				
			||||
MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
 | 
				
			||||
MSG_FLASH = Creating load file for Flash:
 | 
				
			||||
MSG_EEPROM = Creating load file for EEPROM:
 | 
				
			||||
MSG_EXTENDED_LISTING = Creating Extended Listing:
 | 
				
			||||
MSG_SYMBOL_TABLE = Creating Symbol Table:
 | 
				
			||||
MSG_LINKING = Linking:
 | 
				
			||||
MSG_COMPILING = Compiling C:
 | 
				
			||||
MSG_COMPILING_CPP = Compiling C++:
 | 
				
			||||
MSG_ASSEMBLING = Assembling:
 | 
				
			||||
MSG_CLEANING = Cleaning project:
 | 
				
			||||
MSG_CREATING_LIBRARY = Creating library:
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Define all object files.
 | 
				
			||||
OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o) 
 | 
				
			||||
 | 
				
			||||
# Define all listing files.
 | 
				
			||||
LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst) 
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compiler flags to generate dependency files.
 | 
				
			||||
GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Combine all necessary flags and optional flags.
 | 
				
			||||
# Add target processor to flags.
 | 
				
			||||
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
 | 
				
			||||
ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
 | 
				
			||||
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Default target.
 | 
				
			||||
all: begin gccversion sizebefore build checkhooks checklibmode checkboard sizeafter end
 | 
				
			||||
 | 
				
			||||
# Change the build target to build a HEX file or a library.
 | 
				
			||||
build: elf hex eep lss sym
 | 
				
			||||
#build: lib
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
elf: $(TARGET).elf
 | 
				
			||||
hex: $(TARGET).hex
 | 
				
			||||
eep: $(TARGET).eep
 | 
				
			||||
lss: $(TARGET).lss
 | 
				
			||||
sym: $(TARGET).sym
 | 
				
			||||
LIBNAME=lib$(TARGET).a
 | 
				
			||||
lib: $(LIBNAME)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Eye candy.
 | 
				
			||||
# AVR Studio 3.x does not check make's exit code but relies on
 | 
				
			||||
# the following magic strings to be generated by the compile job.
 | 
				
			||||
begin:
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_BEGIN)
 | 
				
			||||
 | 
				
			||||
end:
 | 
				
			||||
	@echo $(MSG_END)
 | 
				
			||||
	@echo
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Display size of file.
 | 
				
			||||
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
 | 
				
			||||
ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
 | 
				
			||||
MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
 | 
				
			||||
FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
 | 
				
			||||
 | 
				
			||||
sizebefore:
 | 
				
			||||
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
 | 
				
			||||
	2>/dev/null; echo; fi
 | 
				
			||||
 | 
				
			||||
sizeafter:
 | 
				
			||||
	@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
 | 
				
			||||
	2>/dev/null; echo; fi
 | 
				
			||||
 | 
				
			||||
checkhooks: build
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo ------- Unhooked LUFA Events -------
 | 
				
			||||
	@$(shell) (grep -s '^EVENT_.*LUFA/.*\\.o' $(TARGET).map | \
 | 
				
			||||
	           cut -d' ' -f1 | cut -d'_' -f2- | grep ".*") || \
 | 
				
			||||
			   echo "(None)"
 | 
				
			||||
	@echo ------------------------------------
 | 
				
			||||
 | 
				
			||||
checklibmode:
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo ----------- Library Mode -----------
 | 
				
			||||
	@$(shell) ($(CC) $(ALL_CFLAGS) -E -dM - < /dev/null \
 | 
				
			||||
	          | grep 'USB_\(DEVICE\|HOST\)_ONLY' | cut -d' ' -f2 | grep ".*") \
 | 
				
			||||
	          || echo "No specific mode (both device and host mode allowable)."
 | 
				
			||||
	@echo ------------------------------------
 | 
				
			||||
 | 
				
			||||
checkboard:
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo ---------- Selected Board ----------
 | 
				
			||||
	@echo Selected board model is $(BOARD).
 | 
				
			||||
	@echo ------------------------------------
 | 
				
			||||
	
 | 
				
			||||
# Display compiler version information.
 | 
				
			||||
gccversion : 
 | 
				
			||||
	@$(CC) --version
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Program the device.  
 | 
				
			||||
program: $(TARGET).hex $(TARGET).eep
 | 
				
			||||
	$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
 | 
				
			||||
 | 
				
			||||
flip: $(TARGET).hex
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation erase f
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation loadbuffer $(TARGET).hex program
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation start reset 0
 | 
				
			||||
 | 
				
			||||
dfu: $(TARGET).hex
 | 
				
			||||
	dfu-programmer $(MCU) erase
 | 
				
			||||
	dfu-programmer $(MCU) flash --debug 1 $(TARGET).hex
 | 
				
			||||
	dfu-programmer $(MCU) reset
 | 
				
			||||
 | 
				
			||||
flip-ee: $(TARGET).hex $(TARGET).eep
 | 
				
			||||
	copy $(TARGET).eep $(TARGET)eep.hex
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation memory EEPROM erase
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation memory EEPROM loadbuffer $(TARGET)eep.hex program
 | 
				
			||||
	batchisp -hardware usb -device $(MCU) -operation start reset 0
 | 
				
			||||
 | 
				
			||||
dfu-ee: $(TARGET).hex $(TARGET).eep
 | 
				
			||||
	dfu-programmer $(MCU) flash-eeprom --debug 1 --suppress-bootloader-mem $(TARGET).eep
 | 
				
			||||
	dfu-programmer $(MCU) reset
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Generate avr-gdb config/init file which does the following:
 | 
				
			||||
#     define the reset signal, load the target file, connect to target, and set 
 | 
				
			||||
#     a breakpoint at main().
 | 
				
			||||
gdb-config: 
 | 
				
			||||
	@$(REMOVE) $(GDBINIT_FILE)
 | 
				
			||||
	@echo define reset >> $(GDBINIT_FILE)
 | 
				
			||||
	@echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
 | 
				
			||||
	@echo end >> $(GDBINIT_FILE)
 | 
				
			||||
	@echo file $(TARGET).elf >> $(GDBINIT_FILE)
 | 
				
			||||
	@echo target remote $(DEBUG_HOST):$(DEBUG_PORT)  >> $(GDBINIT_FILE)
 | 
				
			||||
ifeq ($(DEBUG_BACKEND),simulavr)
 | 
				
			||||
	@echo load  >> $(GDBINIT_FILE)
 | 
				
			||||
endif
 | 
				
			||||
	@echo break main >> $(GDBINIT_FILE)
 | 
				
			||||
 | 
				
			||||
debug: gdb-config $(TARGET).elf
 | 
				
			||||
ifeq ($(DEBUG_BACKEND), avarice)
 | 
				
			||||
	@echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
 | 
				
			||||
	@$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
 | 
				
			||||
	$(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
 | 
				
			||||
	@$(WINSHELL) /c pause
 | 
				
			||||
 | 
				
			||||
else
 | 
				
			||||
	@$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
 | 
				
			||||
	$(DEBUG_MFREQ) --port $(DEBUG_PORT)
 | 
				
			||||
endif
 | 
				
			||||
	@$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
 | 
				
			||||
COFFCONVERT = $(OBJCOPY) --debugging
 | 
				
			||||
COFFCONVERT += --change-section-address .data-0x800000
 | 
				
			||||
COFFCONVERT += --change-section-address .bss-0x800000
 | 
				
			||||
COFFCONVERT += --change-section-address .noinit-0x800000
 | 
				
			||||
COFFCONVERT += --change-section-address .eeprom-0x810000
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
coff: $(TARGET).elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_COFF) $(TARGET).cof
 | 
				
			||||
	$(COFFCONVERT) -O coff-avr $< $(TARGET).cof
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
extcoff: $(TARGET).elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_EXTENDED_COFF) $(TARGET).cof
 | 
				
			||||
	$(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Create final output files (.hex, .eep) from ELF output file.
 | 
				
			||||
%.hex: %.elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_FLASH) $@
 | 
				
			||||
	$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
 | 
				
			||||
 | 
				
			||||
%.eep: %.elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_EEPROM) $@
 | 
				
			||||
	-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
 | 
				
			||||
	--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
 | 
				
			||||
 | 
				
			||||
# Create extended listing file from ELF output file.
 | 
				
			||||
%.lss: %.elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_EXTENDED_LISTING) $@
 | 
				
			||||
	$(OBJDUMP) -h -z -S $< > $@
 | 
				
			||||
 | 
				
			||||
# Create a symbol table from ELF output file.
 | 
				
			||||
%.sym: %.elf
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_SYMBOL_TABLE) $@
 | 
				
			||||
	$(NM) -n $< > $@
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Create library from object files.
 | 
				
			||||
.SECONDARY : $(TARGET).a
 | 
				
			||||
.PRECIOUS : $(OBJ)
 | 
				
			||||
%.a: $(OBJ)
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_CREATING_LIBRARY) $@
 | 
				
			||||
	$(AR) $@ $(OBJ)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Link: create ELF output file from object files.
 | 
				
			||||
.SECONDARY : $(TARGET).elf
 | 
				
			||||
.PRECIOUS : $(OBJ)
 | 
				
			||||
%.elf: $(OBJ)
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_LINKING) $@
 | 
				
			||||
	$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compile: create object files from C source files.
 | 
				
			||||
$(OBJDIR)/%.o : %.c
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_COMPILING) $<
 | 
				
			||||
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compile: create object files from C++ source files.
 | 
				
			||||
$(OBJDIR)/%.o : %.cpp
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_COMPILING_CPP) $<
 | 
				
			||||
	$(CC) -c $(ALL_CPPFLAGS) $< -o $@ 
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compile: create assembler files from C source files.
 | 
				
			||||
%.s : %.c
 | 
				
			||||
	$(CC) -S $(ALL_CFLAGS) $< -o $@
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Compile: create assembler files from C++ source files.
 | 
				
			||||
%.s : %.cpp
 | 
				
			||||
	$(CC) -S $(ALL_CPPFLAGS) $< -o $@
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Assemble: create object files from assembler source files.
 | 
				
			||||
$(OBJDIR)/%.o : %.S
 | 
				
			||||
	@echo
 | 
				
			||||
	@echo $(MSG_ASSEMBLING) $<
 | 
				
			||||
	$(CC) -c $(ALL_ASFLAGS) $< -o $@
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Create preprocessed source for use in sending a bug report.
 | 
				
			||||
%.i : %.c
 | 
				
			||||
	$(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ 
 | 
				
			||||
	
 | 
				
			||||
 | 
				
			||||
# Target: clean project.
 | 
				
			||||
clean: begin clean_list clean_binary end
 | 
				
			||||
 | 
				
			||||
clean_binary:
 | 
				
			||||
	$(REMOVE) $(TARGET).hex
 | 
				
			||||
	
 | 
				
			||||
clean_list:
 | 
				
			||||
	@echo $(MSG_CLEANING)
 | 
				
			||||
	$(REMOVE) $(TARGET).eep
 | 
				
			||||
	$(REMOVE) $(TARGET)eep.hex
 | 
				
			||||
	$(REMOVE) $(TARGET).cof
 | 
				
			||||
	$(REMOVE) $(TARGET).elf
 | 
				
			||||
	$(REMOVE) $(TARGET).map
 | 
				
			||||
	$(REMOVE) $(TARGET).sym
 | 
				
			||||
	$(REMOVE) $(TARGET).lss
 | 
				
			||||
	$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
 | 
				
			||||
	$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
 | 
				
			||||
	$(REMOVE) $(SRC:.c=.s)
 | 
				
			||||
	$(REMOVE) $(SRC:.c=.d)
 | 
				
			||||
	$(REMOVE) $(SRC:.c=.i)
 | 
				
			||||
	$(REMOVEDIR) .dep
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
doxygen:
 | 
				
			||||
	@echo Generating Project Documentation...
 | 
				
			||||
	@doxygen Doxygen.conf
 | 
				
			||||
	@echo Documentation Generation Complete.
 | 
				
			||||
 | 
				
			||||
clean_doxygen:
 | 
				
			||||
	rm -rf Documentation
 | 
				
			||||
 | 
				
			||||
# Create object files directory
 | 
				
			||||
$(shell mkdir $(OBJDIR) 2>/dev/null)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Include the dependency files.
 | 
				
			||||
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
 | 
				
			||||
 | 
				
			||||
 | 
				
			||||
# Listing of phony targets.
 | 
				
			||||
.PHONY : all checkhooks checklibmode checkboard   \
 | 
				
			||||
begin finish end sizebefore sizeafter gccversion  \
 | 
				
			||||
build elf hex eep lss sym coff extcoff clean      \
 | 
				
			||||
clean_list clean_binary program debug gdb-config  \
 | 
				
			||||
doxygen dfu flip flip-ee dfu-ee
 | 
				
			||||
@ -0,0 +1,18 @@
 | 
				
			||||
#
 | 
				
			||||
#             LUFA Library
 | 
				
			||||
#     Copyright (C) Dean Camera, 2009.
 | 
				
			||||
#              
 | 
				
			||||
#  dean [at] fourwalledcubicle [dot] com
 | 
				
			||||
#      www.fourwalledcubicle.com
 | 
				
			||||
#
 | 
				
			||||
 | 
				
			||||
# Makefile to build all the LUFA Class Driver and Low Level Demos. Call with
 | 
				
			||||
# "make all" to rebuild all demos of both types.
 | 
				
			||||
 | 
				
			||||
# Projects are pre-cleaned before each one is built, to ensure any
 | 
				
			||||
# custom LUFA library build options are reflected in the compiled
 | 
				
			||||
# code.
 | 
				
			||||
 | 
				
			||||
%:
 | 
				
			||||
	make -C ClassDriver/ $@
 | 
				
			||||
	make -C LowLevel/ $@
 | 
				
			||||
											
												
													File diff suppressed because one or more lines are too long
												
											
										
									
								
					Loading…
					
					
				
		Reference in new issue