Clean up RelayBoard project code.
Make AVRISP project clear the XMEGA target's reset register twice; this does not appear to take affect properly the first time under some circumstances.
 \r
 #include "BluetoothHost.h"\r
 \r
-Bluetooth_Device_t Bluetooth_DeviceConfiguration =\r
-       {\r
-               Class:   (DEVICE_CLASS_SERVICE_CAPTURING | DEVICE_CLASS_MAJOR_COMPUTER | DEVICE_CLASS_MINOR_COMPUTER_PALM),\r
-               PINCode: "0000",\r
-               Name:    "LUFA Bluetooth Demo"\r
-       };\r
-\r
 /** Main program entry point. This routine configures the hardware required by the application, then\r
  *  enters a loop to run the application tasks in sequence.\r
  */\r
 \r
        for (;;)\r
        {\r
-               Bluetooth_Stack_Task();\r
+               Bluetooth_Stack_USBTask();\r
                Bluetooth_Host_Task();\r
                USB_USBTask();\r
        }\r
                        puts_P(PSTR("Bluetooth Dongle Enumerated.\r\n"));\r
                        \r
                        /* Initialize the Bluetooth stack */\r
-                       Bluetooth_State_Init();\r
+                       Bluetooth_Stack_Init();\r
 \r
                        USB_HostState = HOST_STATE_Configured;\r
                        break;\r
 
                                                memcpy(Bluetooth_TempDeviceAddress,\r
                                                       &((Bluetooth_HCIEvent_ConnectionRequest_t*)&EventParams)->RemoteAddress,\r
                                                       sizeof(Bluetooth_TempDeviceAddress));\r
-                                       \r
-                                               /* Only accept the connection if it is a ACL (data) connection */\r
-                                               Bluetooth_HCIProcessingState = (Bluetooth_Connection.IsConnected ||\r
-                                                                              (((Bluetooth_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType != 0x01)) ?\r
+                                                          \r
+                                               bool IsACLConnection = (((Bluetooth_HCIEvent_ConnectionRequest_t*)&EventParams)->LinkType == 0x01);\r
+\r
+                                               /* Only accept the connection if it is a ACL (data) connection, a device is not already connected\r
+                                                  and the user application has indicated that the connection should be allowed */\r
+                                               Bluetooth_HCIProcessingState = (Bluetooth_Connection.IsConnected || !(IsACLConnection) ||\r
+                                                                                                           !(CALLBACK_Bluetooth_ConnectionRequest(Bluetooth_TempDeviceAddress))) ?\r
                                                                                                           Bluetooth_Conn_RejectConnection : Bluetooth_Conn_AcceptConnection;\r
 \r
                                                BT_HCI_DEBUG(">> Connection Request from Device %02X:%02X:%02X:%02X:%02X:%02X",\r
                        BT_HCI_DEBUG("Enter State: Bluetooth_Conn_RejectConnection", NULL);\r
 \r
                        /* Copy over the temporary BT device address saved from the Connection Request event, indicate failure\r
-                          to accept the connection due to limited device resources */\r
+                          to accept the connection due to limited device resources or incorrect device address */\r
                        Bluetooth_HCICommand_RejectConnectionRequest_t RejectConnectionParams;\r
                        memcpy(RejectConnectionParams.RemoteAddress, Bluetooth_TempDeviceAddress, sizeof(RejectConnectionParams.RemoteAddress));\r
-                       RejectConnectionParams.Reason = ERROR_LIMITED_RESOURCES;\r
+                       RejectConnectionParams.Reason = Bluetooth_Connection.IsConnected ? ERROR_LIMITED_RESOURCES : ERROR_UNACCEPTABLE_BDADDR;\r
 \r
                        /* Send the command to reject the remote connection request */\r
                        Bluetooth_SendHCICommand(&RejectConnectionParams, sizeof(Bluetooth_HCICommand_RejectConnectionRequest_t));\r
 
                #define EVENT_PIN_CODE_REQUEST                         0x16\r
                \r
                #define ERROR_LIMITED_RESOURCES                        0x0D\r
+               #define ERROR_UNACCEPTABLE_BDADDR                      0x0F\r
                \r
        /* Type Defines: */\r
                typedef struct\r
        /* Function Prototypes: */\r
                void Bluetooth_ProcessHCICommands(void);\r
                void Bluetooth_ProcessHCIEvents(void);\r
+               \r
+               bool CALLBACK_Bluetooth_ConnectionRequest(uint8_t* RemoteAddress);\r
 \r
                #if defined(INCLUDE_FROM_BLUETOOTHHCICOMMANDS_C)\r
                        static uint8_t Bluetooth_SendHCICommand(void* Parameters, uint16_t ParameterLength);\r
 
 \r
 #include "BluetoothStack.h"\r
 \r
+/** Bluetooth device connection information structure. Once connected to a remote device, this structure tracks the\r
+ *  connection state of the individual L2CAP channels.\r
+ */\r
 Bluetooth_Connection_t Bluetooth_Connection = {IsConnected: false};\r
 \r
-Bluetooth_Device_t     Bluetooth_DeviceConfiguration ATTR_WEAK =\r
+/** Bluetooth configuration structure. This structure configures the bluetooth stack's user alterable settings. */\r
+Bluetooth_Device_t Bluetooth_DeviceConfiguration =\r
        {\r
-               Class:   DEVICE_CLASS_MAJOR_MISC,\r
+               Class:   (DEVICE_CLASS_SERVICE_CAPTURING | DEVICE_CLASS_MAJOR_COMPUTER | DEVICE_CLASS_MINOR_COMPUTER_PALM),\r
                PINCode: "0000",\r
-               Name:    "LUFA BT Device"\r
+               Name:    "LUFA Bluetooth Demo"\r
        };\r
 \r
-void Bluetooth_State_Init(void)\r
+void Bluetooth_Stack_Init(void)\r
 {\r
        Bluetooth_HCIProcessingState = Bluetooth_Init;\r
 }\r
 \r
-void Bluetooth_Stack_Task(void)\r
+void Bluetooth_Stack_USBTask(void)\r
 {\r
        Bluetooth_ProcessHCICommands();\r
        Bluetooth_ProcessACLPackets();\r
 }\r
 \r
+bool CALLBACK_Bluetooth_ConnectionRequest(uint8_t* RemoteAddress)\r
+{\r
+       /* Always accept connections from remote devices */\r
+       return true;\r
+}\r
+\r
 Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource)\r
 {\r
        Bluetooth_Channel_t* CurrentChannelStructure;\r
 
                Bluetooth_Channel_t* Bluetooth_GetChannelData(uint16_t ChannelNumber, bool SearchBySource);\r
                Bluetooth_Channel_t* Bluetooth_InitChannelData(uint16_t RemoteChannelNumber, uint16_t PSM);\r
                \r
-               void Bluetooth_State_Init(void);\r
-               void Bluetooth_Stack_Task(void);\r
+               void Bluetooth_Stack_Init(void);\r
+               void Bluetooth_Stack_USBTask(void);\r
 \r
        /* External Variables: */\r
                extern Bluetooth_Device_t     Bluetooth_DeviceConfiguration;\r
 
  *  Project Homepage: http://www.fourwalledcubicle.com/LUFA.php \n\r
  *  Author's Website: http://www.fourwalledcubicle.com \n\r
  *  Development Blog: http://www.fourwalledcubicle.com/blog \n\r
+ *  Commercial Licences: http://fourwalledcubicle.com/PurchaseLUFA.php \n\r
  *\r
  *  \section Sec_ProjectHelp Assistance With LUFA\r
  *  Discussion Group: http://groups.google.com/group/myusb-support-list \n\r
 
                #include <LUFA/Drivers/USB/USB.h>\r
 \r
        /* Macros: */\r
-               #if !defined(WIN_LIBUSB_COMPAT)\r
+               #if !defined(LIBUSB_FILTERDRV_COMPAT)\r
                        /** Endpoint number of the AVRISP data OUT endpoint. */\r
                        #define AVRISP_DATA_OUT_EPNUM      2\r
 \r
 
                XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
                XPROGTarget_SendByte(0x00);\r
 \r
+               /* Do it twice to make sure it takes affect (silicon bug?) */\r
+               XPROGTarget_SendByte(PDI_CMD_STCS | PDI_RESET_REG);     \r
+               XPROGTarget_SendByte(0x00);\r
+\r
                XPROGTarget_DisableTargetPDI();\r
        }\r
        else\r
 
 /** Event handler for the library USB Unhandled Control Packet event. */\r
 void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
-    const uint8_t serial[5] = { 0, 0, 0, 0, 1 };\r
-       uint8_t data[2]         = { 0, 0 };\r
+    const uint8_t SerialNumber[5] = { 0, 0, 0, 0, 1 };\r
+       uint8_t ControlData[2]        = { 0, 0 };\r
 \r
     switch (USB_ControlRequest.bRequest)\r
        {\r
 \r
                                Endpoint_ClearSETUP();\r
 \r
-                               Endpoint_Read_Control_Stream_LE(data, sizeof(data));\r
+                               Endpoint_Read_Control_Stream_LE(ControlData, sizeof(ControlData));\r
                                Endpoint_ClearIN();\r
 \r
                                switch (USB_ControlRequest.wValue)\r
                                {\r
                                        case 0x303:\r
-                                               if (data[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1; break;\r
+                                               if (ControlData[1]) PORTC &= ~RELAY1; else PORTC |= RELAY1;\r
+                                               break;\r
                                        case 0x306:\r
-                                               if (data[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2; break;\r
+                                               if (ControlData[1]) PORTC &= ~RELAY2; else PORTC |= RELAY2;\r
+                                               break;\r
                                        case 0x309:\r
-                                               if (data[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3; break;\r
+                                               if (ControlData[1]) PORTC &= ~RELAY3; else PORTC |= RELAY3;\r
+                                               break;\r
                                        case 0x30c:\r
-                                               if (data[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4; break;\r
-                                       default:\r
+                                               if (ControlData[1]) PORTC &= ~RELAY4; else PORTC |= RELAY4;\r
                                                break;\r
                                }\r
                        }\r
                                switch (USB_ControlRequest.wValue)\r
                                {\r
                                        case 0x301:\r
-                                               Endpoint_Write_Control_Stream_LE(serial, sizeof(serial));\r
+                                               Endpoint_Write_Control_Stream_LE(SerialNumber, sizeof(SerialNumber));\r
                                                break;\r
                                        case 0x303:\r
-                                               if (PORTC & RELAY1) data[1] = 2; else data[1] = 3; break;\r
+                                               ControlData[1] = (PORTC & RELAY1) ? 2 : 3;\r
+                                               break;\r
                                        case 0x306:\r
-                                               if (PORTC & RELAY2) data[1] = 2; else data[1] = 3; break;\r
+                                               ControlData[1] = (PORTC & RELAY2) ? 2 : 3;\r
+                                               break;\r
                                        case 0x309:\r
-                                               if (PORTC & RELAY3) data[1] = 2; else data[1] = 3; break;\r
+                                               ControlData[1] = (PORTC & RELAY3) ? 2 : 3;\r
+                                               break;\r
                                        case 0x30c:\r
-                                               if (PORTC & RELAY4) data[1] = 2; else data[1] = 3; break;\r
-                                       default:\r
+                                               ControlData[1] = (PORTC & RELAY4) ? 2 : 3;\r
                                                break;\r
                                }\r
                                \r
-                               if (data[1])\r
-                                 Endpoint_Write_Control_Stream_LE(data, sizeof(data));\r
+                               if (ControlData[1])\r
+                                 Endpoint_Write_Control_Stream_LE(ControlData, sizeof(ControlData));\r
 \r
                                Endpoint_ClearOUT();\r
                        }\r