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 #define IOGEAR_GBU521_VID 0x0A5C // The IOGEAR GBU521 dongle does not presents itself correctly, so we have to check for it manually
31 #define IOGEAR_GBU521_PID 0x21E8
32 
33 /* Bluetooth dongle data taken from descriptors */
34 #define BULK_MAXPKTSIZE 64 // max size for ACL data
35 
36 // Used in control endpoint header for HCI Commands
37 #define bmREQ_HCI_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_DEVICE
38 // Used in control endpoint header for HID Commands
39 #define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
40 #define HID_REQUEST_SET_REPORT 0x09
41 
42 /* Bluetooth HCI states for hci_task() */
43 #define HCI_INIT_STATE 0
44 #define HCI_RESET_STATE 1
45 #define HCI_CLASS_STATE 2
46 #define HCI_BDADDR_STATE 3
47 #define HCI_LOCAL_VERSION_STATE 4
48 #define HCI_SET_NAME_STATE 5
49 #define HCI_CHECK_WII_SERVICE 6
50 
51 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a Wii controller
52 #define HCI_CONNECT_WII_STATE 8
53 #define HCI_CONNECTED_WII_STATE 9
54 
55 #define HCI_SCANNING_STATE 10
56 #define HCI_CONNECT_IN_STATE 11
57 #define HCI_REMOTE_NAME_STATE 12
58 #define HCI_CONNECTED_STATE 13
59 #define HCI_DISABLE_SCAN_STATE 14
60 #define HCI_DONE_STATE 15
61 #define HCI_DISCONNECT_STATE 16
62 
63 /* HCI event flags*/
64 #define HCI_FLAG_CMD_COMPLETE 0x01
65 #define HCI_FLAG_CONN_COMPLETE 0x02
66 #define HCI_FLAG_DISCONN_COMPLETE 0x04
67 #define HCI_FLAG_REMOTE_NAME_COMPLETE 0x08
68 #define HCI_FLAG_INCOMING_REQUEST 0x10
69 #define HCI_FLAG_READ_BDADDR 0x20
70 #define HCI_FLAG_READ_VERSION 0x40
71 #define HCI_FLAG_WII_FOUND 0x80
72 #define HCI_FLAG_CONNECT_EVENT 0x100
73 
74 /*Macros for HCI event flag tests */
75 #define hci_cmd_complete (hci_event_flag & HCI_FLAG_CMD_COMPLETE)
76 #define hci_connect_complete (hci_event_flag & HCI_FLAG_CONN_COMPLETE)
77 #define hci_disconnect_complete (hci_event_flag & HCI_FLAG_DISCONN_COMPLETE)
78 #define hci_remote_name_complete (hci_event_flag & HCI_FLAG_REMOTE_NAME_COMPLETE)
79 #define hci_incoming_connect_request (hci_event_flag & HCI_FLAG_INCOMING_REQUEST)
80 #define hci_read_bdaddr_complete (hci_event_flag & HCI_FLAG_READ_BDADDR)
81 #define hci_read_version_complete (hci_event_flag & HCI_FLAG_READ_VERSION)
82 #define hci_wii_found (hci_event_flag & HCI_FLAG_WII_FOUND)
83 #define hci_connect_event (hci_event_flag & HCI_FLAG_CONNECT_EVENT)
84 
85 /* HCI Events managed */
86 #define EV_INQUIRY_COMPLETE 0x01
87 #define EV_INQUIRY_RESULT 0x02
88 #define EV_CONNECT_COMPLETE 0x03
89 #define EV_INCOMING_CONNECT 0x04
90 #define EV_DISCONNECT_COMPLETE 0x05
91 #define EV_AUTHENTICATION_COMPLETE 0x06
92 #define EV_REMOTE_NAME_COMPLETE 0x07
93 #define EV_ENCRYPTION_CHANGE 0x08
94 #define EV_CHANGE_CONNECTION_LINK 0x09
95 #define EV_ROLE_CHANGED 0x12
96 #define EV_NUM_COMPLETE_PKT 0x13
97 #define EV_PIN_CODE_REQUEST 0x16
98 #define EV_LINK_KEY_REQUEST 0x17
99 #define EV_LINK_KEY_NOTIFICATION 0x18
100 #define EV_DATA_BUFFER_OVERFLOW 0x1A
101 #define EV_MAX_SLOTS_CHANGE 0x1B
102 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
103 #define EV_QOS_SETUP_COMPLETE 0x0D
104 #define EV_COMMAND_COMPLETE 0x0E
105 #define EV_COMMAND_STATUS 0x0F
106 #define EV_LOOPBACK_COMMAND 0x19
107 #define EV_PAGE_SCAN_REP_MODE 0x20
108 
109 /* L2CAP signaling commands */
110 #define L2CAP_CMD_COMMAND_REJECT 0x01
111 #define L2CAP_CMD_CONNECTION_REQUEST 0x02
112 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
113 #define L2CAP_CMD_CONFIG_REQUEST 0x04
114 #define L2CAP_CMD_CONFIG_RESPONSE 0x05
115 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
116 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
117 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
118 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
119 
120 // Used For Connection Response - Remember to Include High Byte
121 #define PENDING 0x01
122 #define SUCCESSFUL 0x00
123 
124 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
125 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
126 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
127 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
128 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
129 
130 // Used to determine if it is a Bluetooth dongle
131 #define WI_SUBCLASS_RF 0x01 // RF Controller
132 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
133 
134 #define BTD_MAX_ENDPOINTS 4
135 #define BTD_NUMSERVICES 4 // Max number of Bluetooth services - if you need more than four simply increase this number
136 
139 public:
144  virtual void ACLData(uint8_t* ACLData);
146  virtual void Run();
148  virtual void Reset();
150  virtual void disconnect();
151 };
152 
157 class BTD : public USBDeviceConfig, public UsbConfigXtracter {
158 public:
163  BTD(USB *p);
164 
173  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
178  virtual uint8_t Release();
183  virtual uint8_t Poll();
184 
189  virtual uint8_t GetAddress() {
190  return bAddress;
191  };
192 
197  virtual bool isReady() {
198  return bPollEnable;
199  };
205  virtual boolean DEVCLASSOK(uint8_t klass) { return (klass == USB_CLASS_WIRELESS_CTRL); }
206 
214  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
215  return ((vid == PS3_VID || vid == IOGEAR_GBU521_VID) && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID || pid == IOGEAR_GBU521_PID));
216  };
228  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
232  void disconnect() {
233  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++)
234  if(btService[i])
235  btService[i]->disconnect();
236  };
237 
244  for(uint8_t i = 0; i < BTD_NUMSERVICES; i++) {
245  if(!btService[i]) {
246  btService[i] = pService;
247  return i; // Return ID
248  }
249  }
250  return -1; // ErrorregisterServiceClass
251  };
252 
259  void HCI_Command(uint8_t* data, uint16_t nbytes);
261  void hci_reset();
263  void hci_read_bdaddr();
270  void hci_set_local_name(const char* name);
272  void hci_write_scan_enable();
274  void hci_write_scan_disable();
276  void hci_remote_name();
278  void hci_accept_connection();
283  void hci_disconnect(uint16_t handle);
300  void hci_inquiry();
302  void hci_inquiry_cancel();
304  void hci_connect();
318  void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
326  void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
335  void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
342  void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
349  void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
357  void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
365  void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
372  void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
383 
385  const char* btdName;
387  const char* btdPin;
388 
390  uint8_t my_bdaddr[6];
392  uint16_t hci_handle;
394  uint8_t disc_bdaddr[6];
396  uint8_t remote_name[30];
402  uint8_t hci_version;
403 
406  pairWithWii = true;
407  hci_state = HCI_CHECK_WII_SERVICE;
408  };
410  bool connectToWii;
419 
424  uint8_t readPollInterval() {
425  return pollInterval;
426  };
427 
428 protected:
430  USB *pUsb;
432  uint8_t bAddress;
435 
437  uint8_t bConfNum;
439  uint8_t bNumEP;
441  uint32_t qNextPollTime;
442 
444  static const uint8_t BTD_CONTROL_PIPE;
446  static const uint8_t BTD_EVENT_PIPE;
448  static const uint8_t BTD_DATAIN_PIPE;
450  static const uint8_t BTD_DATAOUT_PIPE;
451 
457 
458 private:
459  BluetoothService* btService[BTD_NUMSERVICES];
460 
461  bool bPollEnable;
462  uint8_t pollInterval;
463 
464  /* Variables used by high level HCI task */
465  uint8_t hci_state; //current state of bluetooth hci connection
466  uint16_t hci_counter; // counter used for bluetooth hci reset loops
467  uint8_t hci_num_reset_loops; // this value indicate how many times it should read before trying to reset
468  uint16_t hci_event_flag; // hci flags of received bluetooth events
469  uint8_t inquiry_counter;
470 
471  uint8_t hcibuf[BULK_MAXPKTSIZE]; //General purpose buffer for hci data
472  uint8_t l2capinbuf[BULK_MAXPKTSIZE]; //General purpose buffer for l2cap in data
473  uint8_t l2capoutbuf[14]; //General purpose buffer for l2cap out data
474 
475  /* State machines */
476  void HCI_event_task(); // Poll the HCI event pipe
477  void HCI_task(); // HCI state machine
478  void ACL_event_task(); // ACL input pipe
479 
480  /* Used to set the Bluetooth Address internally to the PS3 Controllers */
481  void setBdaddr(uint8_t* BDADDR);
482  void setMoveBdaddr(uint8_t* BDADDR);
483 };
484 #endif