USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BTD.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #ifndef _btd_h_
19 #define _btd_h_
20 
21 #include "Usb.h"
22 #include "confdescparser.h"
23 
24 //PID and VID of the Sony PS3 devices
25 #define PS3_VID 0x054C // Sony Corporation
26 #define PS3_PID 0x0268 // PS3 Controller DualShock 3
27 #define PS3NAVIGATION_PID 0x042F // Navigation controller
28 #define PS3MOVE_PID 0x03D5 // Motion controller
29 
30 /* Bluetooth dongle data taken from descriptors */
31 #define BULK_MAXPKTSIZE 64 // max size for ACL data
32 
33 // Used in control endpoint header for HCI Commands
34 #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
35 // Used in control endpoint header for HID Commands
36 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
37 #define HID_REQUEST_SET_REPORT 0x09
38 
39 /* Bluetooth HCI states for hci_task() */
40 #define HCI_INIT_STATE 0
41 #define HCI_RESET_STATE 1
42 #define HCI_CLASS_STATE 2
43 #define HCI_BDADDR_STATE 3
44 #define HCI_LOCAL_VERSION_STATE 4
45 #define HCI_SET_NAME_STATE 5
46 #define HCI_CHECK_WII_SERVICE 6
47 
48 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a Wii controller
49 #define HCI_CONNECT_WII_STATE 8
50 #define HCI_CONNECTED_WII_STATE 9
51 
52 #define HCI_SCANNING_STATE 10
53 #define HCI_CONNECT_IN_STATE 11
54 #define HCI_REMOTE_NAME_STATE 12
55 #define HCI_CONNECTED_STATE 13
56 #define HCI_DISABLE_SCAN_STATE 14
57 #define HCI_DONE_STATE 15
58 #define HCI_DISCONNECT_STATE 16
59 
60 /* HCI event flags*/
61 #define HCI_FLAG_CMD_COMPLETE 0x01
62 #define HCI_FLAG_CONN_COMPLETE 0x02
63 #define HCI_FLAG_DISCONN_COMPLETE 0x04
64 #define HCI_FLAG_REMOTE_NAME_COMPLETE 0x08
65 #define HCI_FLAG_INCOMING_REQUEST 0x10
66 #define HCI_FLAG_READ_BDADDR 0x20
67 #define HCI_FLAG_READ_VERSION 0x40
68 #define HCI_FLAG_WII_FOUND 0x80
69 #define HCI_FLAG_CONNECT_EVENT 0x100
70 
71 /*Macros for HCI event flag tests */
72 #define hci_cmd_complete (hci_event_flag & HCI_FLAG_CMD_COMPLETE)
73 #define hci_connect_complete (hci_event_flag & HCI_FLAG_CONN_COMPLETE)
74 #define hci_disconnect_complete (hci_event_flag & HCI_FLAG_DISCONN_COMPLETE)
75 #define hci_remote_name_complete (hci_event_flag & HCI_FLAG_REMOTE_NAME_COMPLETE)
76 #define hci_incoming_connect_request (hci_event_flag & HCI_FLAG_INCOMING_REQUEST)
77 #define hci_read_bdaddr_complete (hci_event_flag & HCI_FLAG_READ_BDADDR)
78 #define hci_read_version_complete (hci_event_flag & HCI_FLAG_READ_VERSION)
79 #define hci_wii_found (hci_event_flag & HCI_FLAG_WII_FOUND)
80 #define hci_connect_event (hci_event_flag & HCI_FLAG_CONNECT_EVENT)
81 
82 /* HCI Events managed */
83 #define EV_INQUIRY_COMPLETE 0x01
84 #define EV_INQUIRY_RESULT 0x02
85 #define EV_CONNECT_COMPLETE 0x03
86 #define EV_INCOMING_CONNECT 0x04
87 #define EV_DISCONNECT_COMPLETE 0x05
88 #define EV_AUTHENTICATION_COMPLETE 0x06
89 #define EV_REMOTE_NAME_COMPLETE 0x07
90 #define EV_ENCRYPTION_CHANGE 0x08
91 #define EV_CHANGE_CONNECTION_LINK 0x09
92 #define EV_ROLE_CHANGED 0x12
93 #define EV_NUM_COMPLETE_PKT 0x13
94 #define EV_PIN_CODE_REQUEST 0x16
95 #define EV_LINK_KEY_REQUEST 0x17
96 #define EV_LINK_KEY_NOTIFICATION 0x18
97 #define EV_DATA_BUFFER_OVERFLOW 0x1A
98 #define EV_MAX_SLOTS_CHANGE 0x1B
99 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
100 #define EV_QOS_SETUP_COMPLETE 0x0D
101 #define EV_COMMAND_COMPLETE 0x0E
102 #define EV_COMMAND_STATUS 0x0F
103 #define EV_LOOPBACK_COMMAND 0x19
104 #define EV_PAGE_SCAN_REP_MODE 0x20
105 
106 /* L2CAP signaling commands */
107 #define L2CAP_CMD_COMMAND_REJECT 0x01
108 #define L2CAP_CMD_CONNECTION_REQUEST 0x02
109 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
110 #define L2CAP_CMD_CONFIG_REQUEST 0x04
111 #define L2CAP_CMD_CONFIG_RESPONSE 0x05
112 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
113 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
114 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
115 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
116 
117 // Used For Connection Response - Remember to Include High Byte
118 #define PENDING 0x01
119 #define SUCCESSFUL 0x00
120 
121 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
122 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
123 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
124 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
125 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
126 
127 // Used to determine if it is a Bluetooth dongle
128 #define WI_SUBCLASS_RF 0x01 // RF Controller
129 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
130 
131 #define BTD_MAX_ENDPOINTS 4
132 #define BTD_NUMSERVICES 4 // Max number of Bluetooth services - if you need more than four simply increase this number
133 
136 public:
141  virtual void ACLData(uint8_t* ACLData);
143  virtual void Run();
145  virtual void Reset();
147  virtual void disconnect();
148 };
149 
154 class BTD : public USBDeviceConfig, public UsbConfigXtracter {
155 public:
160  BTD(USB *p);
161 
170  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
175  virtual uint8_t Release();
180  virtual uint8_t Poll();
181 
186  virtual uint8_t GetAddress() {
187  return bAddress;
188  };
189 
194  virtual bool isReady() {
195  return bPollEnable;
196  };
208  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
212  void disconnect() {
213  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++)
214  if(btService[i])
215  btService[i]->disconnect();
216  };
217 
224  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++) {
225  if(!btService[i]) {
226  btService[i] = pService;
227  return i; // Return ID
228  }
229  }
230  return -1; // ErrorregisterServiceClass
231  };
232 
239  void HCI_Command(uint8_t* data, uint16_t nbytes);
241  void hci_reset();
243  void hci_read_bdaddr();
250  void hci_set_local_name(const char* name);
252  void hci_write_scan_enable();
254  void hci_write_scan_disable();
256  void hci_remote_name();
258  void hci_accept_connection();
263  void hci_disconnect(uint16_t handle);
280  void hci_inquiry();
282  void hci_inquiry_cancel();
284  void hci_connect();
298  void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
306  void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
315  void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
322  void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
329  void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
337  void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
345  void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
352  void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
363 
365  const char* btdName;
367  const char* btdPin;
368 
370  uint8_t my_bdaddr[6];
372  uint16_t hci_handle;
374  uint8_t disc_bdaddr[6];
376  uint8_t remote_name[30];
382  uint8_t hci_version;
383 
386  pairWithWii = true;
387  hci_state = HCI_CHECK_WII_SERVICE;
388  };
390  bool connectToWii;
399 
404  uint8_t readPollInterval() {
405  return pollInterval;
406  };
407 
408 protected:
410  USB *pUsb;
412  uint8_t bAddress;
415 
417  uint8_t bConfNum;
419  uint8_t bNumEP;
421  uint32_t qNextPollTime;
422 
424  static const uint8_t BTD_CONTROL_PIPE;
426  static const uint8_t BTD_EVENT_PIPE;
428  static const uint8_t BTD_DATAIN_PIPE;
430  static const uint8_t BTD_DATAOUT_PIPE;
431 
437 
438 private:
439  BluetoothService* btService[BTD_NUMSERVICES];
440 
441  bool bPollEnable;
442  uint8_t pollInterval;
443 
444  /* Variables used by high level HCI task */
445  uint8_t hci_state; //current state of bluetooth hci connection
446  uint16_t hci_counter; // counter used for bluetooth hci reset loops
447  uint8_t hci_num_reset_loops; // this value indicate how many times it should read before trying to reset
448  uint16_t hci_event_flag; // hci flags of received bluetooth events
449  uint8_t inquiry_counter;
450 
451  uint8_t hcibuf[BULK_MAXPKTSIZE]; //General purpose buffer for hci data
452  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; //General purpose buffer for l2cap in data
453  uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; //General purpose buffer for l2cap out data
454 
455  /* State machines */
456  void HCI_event_task(); // Poll the HCI event pipe
457  void HCI_task(); // HCI state machine
458  void ACL_event_task(); // ACL input pipe
459 
460  /* Used to set the Bluetooth Address internally to the PS3 Controllers */
461  void setBdaddr(uint8_t* BDADDR);
462  void setMoveBdaddr(uint8_t* BDADDR);
463 };
464 #endif