Oops - with new changes to the way the device Configuration Descriptor is retrieved...
[pub/USBasp.git] / Demos / Host / Incomplete / BluetoothHost / BluetoothHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /*
32 Bluetooth Dongle host demo application.
33
34 ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
35 */
36
37 #include "BluetoothHost.h"
38
39 Bluetooth_Device_t Bluetooth_DeviceConfiguration =
40 {
41 Class: (DEVICE_CLASS_SERVICE_CAPTURING | DEVICE_CLASS_MAJOR_COMPUTER | DEVICE_CLASS_MINOR_COMPUTER_PALM),
42 PINCode: "0000",
43 Name: "LUFA Bluetooth Demo"
44 };
45
46
47 int main(void)
48 {
49 SetupHardware();
50
51 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
52
53 puts_P(PSTR(ESC_FG_CYAN "Bluetooth Host Demo running.\r\n" ESC_FG_WHITE));
54
55 for (;;)
56 {
57 Bluetooth_Stack_Task();
58 Bluetooth_Management_Task();
59 USB_USBTask();
60 }
61 }
62
63 void SetupHardware(void)
64 {
65 /* Disable watchdog if enabled by bootloader/fuses */
66 MCUSR &= ~(1 << WDRF);
67 wdt_disable();
68
69 /* Disable clock division */
70 clock_prescale_set(clock_div_1);
71
72 /* Hardware Initialization */
73 SerialStream_Init(9600, false);
74 LEDs_Init();
75 USB_Init();
76 }
77
78 void EVENT_USB_Host_DeviceAttached(void)
79 {
80 puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
81
82 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
83 }
84
85 void EVENT_USB_Host_DeviceUnattached(void)
86 {
87 puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));
88
89 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
90 }
91
92 void EVENT_USB_Host_DeviceEnumerationComplete(void)
93 {
94 LEDs_SetAllLEDs(LEDMASK_USB_READY);
95 }
96
97 void EVENT_USB_Host_HostError(uint8_t ErrorCode)
98 {
99 USB_ShutDown();
100
101 printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
102 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
103
104 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
105 for(;;);
106 }
107
108 void EVENT_USB_Host_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
109 {
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);
114
115 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
116 }
117
118 void Bluetooth_Management_Task(void)
119 {
120 uint8_t ErrorCode;
121
122 switch (USB_HostState)
123 {
124 case HOST_STATE_Addressed:
125 puts_P(PSTR("Getting Device Data.\r\n"));
126
127 /* Get and process the configuration descriptor data */
128 if ((ErrorCode = ProcessDeviceDescriptor()) != SuccessfulDeviceRead)
129 {
130 if (ErrorCode == ControlErrorDuringDeviceRead)
131 puts_P(PSTR(ESC_FG_RED "Control Error (Get Device).\r\n"));
132 else
133 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
134
135 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
136
137 /* Indicate error via status LEDs */
138 LEDs_SetAllLEDs(LEDS_LED1);
139
140 /* Wait until USB device disconnected */
141 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
142 break;
143 }
144
145 puts_P(PSTR("Bluetooth Dongle Detected.\r\n"));
146
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)
149 {
150 printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
151 " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
152
153 /* Indicate error via status LEDs */
154 LEDs_SetAllLEDs(LEDS_LED1);
155
156 /* Wait until USB device disconnected */
157 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
158 break;
159 }
160
161 puts_P(PSTR("Getting Config Data.\r\n"));
162
163 /* Get and process the configuration descriptor data */
164 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
165 {
166 if (ErrorCode == ControlErrorDuringConfigRead)
167 puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
168 else
169 puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
170
171 printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
172
173 /* Indicate error via status LEDs */
174 LEDs_SetAllLEDs(LEDS_LED1);
175
176 /* Wait until USB device disconnected */
177 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
178 break;
179 }
180
181 puts_P(PSTR("Bluetooth Dongle Enumerated.\r\n"));
182
183 USB_HostState = HOST_STATE_Configured;
184 break;
185 }
186 }