3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  32         Bluetooth Dongle host demo application. 
  34         ** NOT CURRENTLY FUNCTIONAL - DO NOT USE ** 
  37 #include "BluetoothHost.h" 
  39 Bluetooth_Device_t Bluetooth_DeviceConfiguration 
= 
  41                 Class
:   (DEVICE_CLASS_SERVICE_CAPTURING 
| DEVICE_CLASS_MAJOR_COMPUTER 
| DEVICE_CLASS_MINOR_COMPUTER_PALM
), 
  43                 Name
:    "LUFA Bluetooth Demo" 
  51         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  53         puts_P(PSTR(ESC_FG_CYAN 
"Bluetooth Host Demo running.\r\n" ESC_FG_WHITE
)); 
  57                 Bluetooth_Stack_Task(); 
  58                 Bluetooth_Management_Task(); 
  63 void SetupHardware(void) 
  65         /* Disable watchdog if enabled by bootloader/fuses */ 
  66         MCUSR 
&= ~(1 << WDRF
); 
  69         /* Disable clock division */ 
  70         clock_prescale_set(clock_div_1
); 
  72         /* Hardware Initialization */ 
  73         SerialStream_Init(9600, false); 
  78 void EVENT_USB_Host_DeviceAttached(void) 
  80         puts_P(PSTR(ESC_FG_GREEN 
"Device Attached.\r\n" ESC_FG_WHITE
)); 
  82         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
  85 void EVENT_USB_Host_DeviceUnattached(void) 
  87         puts_P(PSTR(ESC_FG_GREEN 
"\r\nDevice Unattached.\r\n" ESC_FG_WHITE
)); 
  89         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  92 void EVENT_USB_Host_DeviceEnumerationComplete(void) 
  94         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
  97 void EVENT_USB_Host_HostError(uint8_t ErrorCode
) 
 101         printf_P(PSTR(ESC_FG_RED 
"Host Mode Error\r\n" 
 102                                  " -- Error Code %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 104         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 108 void EVENT_USB_Host_DeviceEnumerationFailed(uint8_t ErrorCode
, uint8_t SubErrorCode
) 
 110         printf_P(PSTR(ESC_FG_RED 
"Dev Enum Error\r\n" 
 111                                  " -- Error Code %d\r\n" 
 112                                  " -- Sub Error Code %d\r\n" 
 113                                  " -- In State %d\r\n" ESC_FG_WHITE
), ErrorCode
, SubErrorCode
, USB_HostState
); 
 115         LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 118 void Bluetooth_Management_Task(void) 
 122         switch (USB_HostState
) 
 124                 case HOST_STATE_Addressed
: 
 125                         puts_P(PSTR("Getting Device Data.\r\n")); 
 127                         /* Get and process the configuration descriptor data */ 
 128                         if ((ErrorCode 
= ProcessDeviceDescriptor()) != SuccessfulDeviceRead
) 
 130                                 if (ErrorCode 
== ControlErrorDuringDeviceRead
) 
 131                                   puts_P(PSTR(ESC_FG_RED 
"Control Error (Get Device).\r\n")); 
 133                                   puts_P(PSTR(ESC_FG_RED 
"Invalid Device.\r\n")); 
 135                                 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 137                                 /* Indicate error via status LEDs */ 
 138                                 LEDs_SetAllLEDs(LEDS_LED1
); 
 140                                 /* Wait until USB device disconnected */ 
 141                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 145                         puts_P(PSTR("Bluetooth Dongle Detected.\r\n")); 
 147                         /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */ 
 148                         if ((ErrorCode 
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
) 
 150                                 printf_P(PSTR(ESC_FG_RED 
"Control Error (Set Configuration).\r\n" 
 151                                                          " -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 153                                 /* Indicate error via status LEDs */ 
 154                                 LEDs_SetAllLEDs(LEDS_LED1
); 
 156                                 /* Wait until USB device disconnected */ 
 157                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 161                         puts_P(PSTR("Getting Config Data.\r\n")); 
 163                         /* Get and process the configuration descriptor data */ 
 164                         if ((ErrorCode 
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
) 
 166                                 if (ErrorCode 
== ControlErrorDuringConfigRead
) 
 167                                   puts_P(PSTR(ESC_FG_RED 
"Control Error (Get Configuration).\r\n")); 
 169                                   puts_P(PSTR(ESC_FG_RED 
"Invalid Device.\r\n")); 
 171                                 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE
), ErrorCode
); 
 173                                 /* Indicate error via status LEDs */ 
 174                                 LEDs_SetAllLEDs(LEDS_LED1
); 
 176                                 /* Wait until USB device disconnected */ 
 177                                 USB_HostState 
= HOST_STATE_WaitForDeviceRemoval
; 
 181                         puts_P(PSTR("Bluetooth Dongle Enumerated.\r\n")); 
 183                         USB_HostState 
= HOST_STATE_Configured
;