]> git.gir.st - tmk_keyboard.git/blob - protocol/usb_hid/USB_Host_Shield_2.0/BTD.h
Squashed 'tmk_core/' changes from 8da1898..e5f9940
[tmk_keyboard.git] / protocol / usb_hid / USB_Host_Shield_2.0 / BTD.h
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 "hid.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
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_DEVICE_SERVICE 6
47
48 #define HCI_INQUIRY_STATE 7 // These three states are only used if it should pair and connect to a device
49 #define HCI_CONNECT_DEVICE_STATE 8
50 #define HCI_CONNECTED_DEVICE_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 (1UL << 0)
62 #define HCI_FLAG_CONNECT_COMPLETE (1UL << 1)
63 #define HCI_FLAG_DISCONNECT_COMPLETE (1UL << 2)
64 #define HCI_FLAG_REMOTE_NAME_COMPLETE (1UL << 3)
65 #define HCI_FLAG_INCOMING_REQUEST (1UL << 4)
66 #define HCI_FLAG_READ_BDADDR (1UL << 5)
67 #define HCI_FLAG_READ_VERSION (1UL << 6)
68 #define HCI_FLAG_DEVICE_FOUND (1UL << 7)
69 #define HCI_FLAG_CONNECT_EVENT (1UL << 8)
70
71 /* Macros for HCI event flag tests */
72 #define hci_check_flag(flag) (hci_event_flag & (flag))
73 #define hci_set_flag(flag) (hci_event_flag |= (flag))
74 #define hci_clear_flag(flag) (hci_event_flag &= ~(flag))
75
76 /* HCI Events managed */
77 #define EV_INQUIRY_COMPLETE 0x01
78 #define EV_INQUIRY_RESULT 0x02
79 #define EV_CONNECT_COMPLETE 0x03
80 #define EV_INCOMING_CONNECT 0x04
81 #define EV_DISCONNECT_COMPLETE 0x05
82 #define EV_AUTHENTICATION_COMPLETE 0x06
83 #define EV_REMOTE_NAME_COMPLETE 0x07
84 #define EV_ENCRYPTION_CHANGE 0x08
85 #define EV_CHANGE_CONNECTION_LINK 0x09
86 #define EV_ROLE_CHANGED 0x12
87 #define EV_NUM_COMPLETE_PKT 0x13
88 #define EV_PIN_CODE_REQUEST 0x16
89 #define EV_LINK_KEY_REQUEST 0x17
90 #define EV_LINK_KEY_NOTIFICATION 0x18
91 #define EV_DATA_BUFFER_OVERFLOW 0x1A
92 #define EV_MAX_SLOTS_CHANGE 0x1B
93 #define EV_READ_REMOTE_VERSION_INFORMATION_COMPLETE 0x0C
94 #define EV_QOS_SETUP_COMPLETE 0x0D
95 #define EV_COMMAND_COMPLETE 0x0E
96 #define EV_COMMAND_STATUS 0x0F
97 #define EV_LOOPBACK_COMMAND 0x19
98 #define EV_PAGE_SCAN_REP_MODE 0x20
99
100 /* Bluetooth states for the different Bluetooth drivers */
101 #define L2CAP_WAIT 0
102 #define L2CAP_DONE 1
103
104 /* Used for HID Control channel */
105 #define L2CAP_CONTROL_CONNECT_REQUEST 2
106 #define L2CAP_CONTROL_CONFIG_REQUEST 3
107 #define L2CAP_CONTROL_SUCCESS 4
108 #define L2CAP_CONTROL_DISCONNECT 5
109
110 /* Used for HID Interrupt channel */
111 #define L2CAP_INTERRUPT_SETUP 6
112 #define L2CAP_INTERRUPT_CONNECT_REQUEST 7
113 #define L2CAP_INTERRUPT_CONFIG_REQUEST 8
114 #define L2CAP_INTERRUPT_DISCONNECT 9
115
116 /* Used for SDP channel */
117 #define L2CAP_SDP_WAIT 10
118 #define L2CAP_SDP_SUCCESS 11
119
120 /* Used for RFCOMM channel */
121 #define L2CAP_RFCOMM_WAIT 12
122 #define L2CAP_RFCOMM_SUCCESS 13
123
124 #define L2CAP_DISCONNECT_RESPONSE 14 // Used for both SDP and RFCOMM channel
125
126 /* Bluetooth states used by some drivers */
127 #define TURN_ON_LED 17
128 #define PS3_ENABLE_SIXAXIS 18
129 #define WII_CHECK_MOTION_PLUS_STATE 19
130 #define WII_CHECK_EXTENSION_STATE 20
131 #define WII_INIT_MOTION_PLUS_STATE 21
132
133 /* L2CAP event flags for HID Control channel */
134 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST (1UL << 0)
135 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS (1UL << 1)
136 #define L2CAP_FLAG_CONTROL_CONNECTED (1UL << 2)
137 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE (1UL << 3)
138
139 /* L2CAP event flags for HID Interrupt channel */
140 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST (1UL << 4)
141 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS (1UL << 5)
142 #define L2CAP_FLAG_INTERRUPT_CONNECTED (1UL << 6)
143 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE (1UL << 7)
144
145 /* L2CAP event flags for SDP channel */
146 #define L2CAP_FLAG_CONNECTION_SDP_REQUEST (1UL << 8)
147 #define L2CAP_FLAG_CONFIG_SDP_SUCCESS (1UL << 9)
148 #define L2CAP_FLAG_DISCONNECT_SDP_REQUEST (1UL << 10)
149
150 /* L2CAP event flags for RFCOMM channel */
151 #define L2CAP_FLAG_CONNECTION_RFCOMM_REQUEST (1UL << 11)
152 #define L2CAP_FLAG_CONFIG_RFCOMM_SUCCESS (1UL << 12)
153 #define L2CAP_FLAG_DISCONNECT_RFCOMM_REQUEST (1UL << 13)
154
155 #define L2CAP_FLAG_DISCONNECT_RESPONSE (1UL << 14)
156
157 /* Macros for L2CAP event flag tests */
158 #define l2cap_check_flag(flag) (l2cap_event_flag & (flag))
159 #define l2cap_set_flag(flag) (l2cap_event_flag |= (flag))
160 #define l2cap_clear_flag(flag) (l2cap_event_flag &= ~(flag))
161
162 /* L2CAP signaling commands */
163 #define L2CAP_CMD_COMMAND_REJECT 0x01
164 #define L2CAP_CMD_CONNECTION_REQUEST 0x02
165 #define L2CAP_CMD_CONNECTION_RESPONSE 0x03
166 #define L2CAP_CMD_CONFIG_REQUEST 0x04
167 #define L2CAP_CMD_CONFIG_RESPONSE 0x05
168 #define L2CAP_CMD_DISCONNECT_REQUEST 0x06
169 #define L2CAP_CMD_DISCONNECT_RESPONSE 0x07
170 #define L2CAP_CMD_INFORMATION_REQUEST 0x0A
171 #define L2CAP_CMD_INFORMATION_RESPONSE 0x0B
172
173 // Used For Connection Response - Remember to Include High Byte
174 #define PENDING 0x01
175 #define SUCCESSFUL 0x00
176
177 /* Bluetooth L2CAP PSM - see http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm */
178 #define SDP_PSM 0x01 // Service Discovery Protocol PSM Value
179 #define RFCOMM_PSM 0x03 // RFCOMM PSM Value
180 #define HID_CTRL_PSM 0x11 // HID_Control PSM Value
181 #define HID_INTR_PSM 0x13 // HID_Interrupt PSM Value
182
183 // Used to determine if it is a Bluetooth dongle
184 #define WI_SUBCLASS_RF 0x01 // RF Controller
185 #define WI_PROTOCOL_BT 0x01 // Bluetooth Programming Interface
186
187 #define BTD_MAX_ENDPOINTS 4
188 #define BTD_NUM_SERVICES 4 // Max number of Bluetooth services - if you need more than 4 simply increase this number
189
190 #define PAIR 1
191
192 class BluetoothService;
193
194 /**
195 * The Bluetooth Dongle class will take care of all the USB communication
196 * and then pass the data to the BluetoothService classes.
197 */
198 class BTD : public USBDeviceConfig, public UsbConfigXtracter {
199 public:
200 /**
201 * Constructor for the BTD class.
202 * @param p Pointer to USB class instance.
203 */
204 BTD(USB *p);
205
206 /** @name USBDeviceConfig implementation */
207 /**
208 * Address assignment and basic initialization is done here.
209 * @param parent Hub number.
210 * @param port Port number on the hub.
211 * @param lowspeed Speed of the device.
212 * @return 0 on success.
213 */
214 uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
215 /**
216 * Initialize the Bluetooth dongle.
217 * @param parent Hub number.
218 * @param port Port number on the hub.
219 * @param lowspeed Speed of the device.
220 * @return 0 on success.
221 */
222 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
223 /**
224 * Release the USB device.
225 * @return 0 on success.
226 */
227 uint8_t Release();
228 /**
229 * Poll the USB Input endpoints and run the state machines.
230 * @return 0 on success.
231 */
232 uint8_t Poll();
233
234 /**
235 * Get the device address.
236 * @return The device address.
237 */
238 virtual uint8_t GetAddress() {
239 return bAddress;
240 };
241
242 /**
243 * Used to check if the dongle has been initialized.
244 * @return True if it's ready.
245 */
246 virtual bool isReady() {
247 return bPollEnable;
248 };
249
250 /**
251 * Used by the USB core to check what this driver support.
252 * @param klass The device's USB class.
253 * @return Returns true if the device's USB class matches this driver.
254 */
255 virtual bool DEVCLASSOK(uint8_t klass) {
256 return (klass == USB_CLASS_WIRELESS_CTRL);
257 };
258
259 /**
260 * Used by the USB core to check what this driver support.
261 * Used to set the Bluetooth address into the PS3 controllers.
262 * @param vid The device's VID.
263 * @param pid The device's PID.
264 * @return Returns true if the device's VID and PID matches this driver.
265 */
266 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
267 if(vid == IOGEAR_GBU521_VID && pid == IOGEAR_GBU521_PID)
268 return true;
269 if(my_bdaddr[0] != 0x00 || my_bdaddr[1] != 0x00 || my_bdaddr[2] != 0x00 || my_bdaddr[3] != 0x00 || my_bdaddr[4] != 0x00 || my_bdaddr[5] != 0x00) { // Check if Bluetooth address is set
270 if(vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID))
271 return true;
272 }
273 return false;
274 };
275 /**@}*/
276
277 /** @name UsbConfigXtracter implementation */
278 /**
279 * UsbConfigXtracter implementation, used to extract endpoint information.
280 * @param conf Configuration value.
281 * @param iface Interface number.
282 * @param alt Alternate setting.
283 * @param proto Interface Protocol.
284 * @param ep Endpoint Descriptor.
285 */
286 void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
287 /**@}*/
288
289 /** Disconnects both the L2CAP Channel and the HCI Connection for all Bluetooth services. */
290 void disconnect();
291
292 /**
293 * Register Bluetooth dongle members/services.
294 * @param pService Pointer to BluetoothService class instance.
295 * @return The service ID on success or -1 on fail.
296 */
297 int8_t registerBluetoothService(BluetoothService *pService) {
298 for(uint8_t i = 0; i < BTD_NUM_SERVICES; i++) {
299 if(!btService[i]) {
300 btService[i] = pService;
301 return i; // Return ID
302 }
303 }
304 return -1; // Error registering BluetoothService
305 };
306
307 /** @name HCI Commands */
308 /**
309 * Used to send a HCI Command.
310 * @param data Data to send.
311 * @param nbytes Number of bytes to send.
312 */
313 void HCI_Command(uint8_t* data, uint16_t nbytes);
314 /** Reset the Bluetooth dongle. */
315 void hci_reset();
316 /** Read the Bluetooth address of the dongle. */
317 void hci_read_bdaddr();
318 /** Read the HCI Version of the Bluetooth dongle. */
319 void hci_read_local_version_information();
320 /**
321 * Set the local name of the Bluetooth dongle.
322 * @param name Desired name.
323 */
324 void hci_set_local_name(const char* name);
325 /** Enable visibility to other Bluetooth devices. */
326 void hci_write_scan_enable();
327 /** Disable visibility to other Bluetooth devices. */
328 void hci_write_scan_disable();
329 /** Read the remote devices name. */
330 void hci_remote_name();
331 /** Accept the connection with the Bluetooth device. */
332 void hci_accept_connection();
333 /**
334 * Disconnect the HCI connection.
335 * @param handle The HCI Handle for the connection.
336 */
337 void hci_disconnect(uint16_t handle);
338 /**
339 * Respond with the pin for the connection.
340 * The pin is automatically set for the Wii library,
341 * but can be customized for the SPP library.
342 */
343 void hci_pin_code_request_reply();
344 /** Respons when no pin was set. */
345 void hci_pin_code_negative_request_reply();
346 /**
347 * Command is used to reply to a Link Key Request event from the BR/EDR Controller
348 * if the Host does not have a stored Link Key for the connection.
349 */
350 void hci_link_key_request_negative_reply();
351 /** Used to try to authenticate with the remote device. */
352 void hci_authentication_request();
353 /** Start a HCI inquiry. */
354 void hci_inquiry();
355 /** Cancel a HCI inquiry. */
356 void hci_inquiry_cancel();
357 /** Connect to last device communicated with. */
358 void hci_connect();
359 /**
360 * Connect to device.
361 * @param bdaddr Bluetooth address of the device.
362 */
363 void hci_connect(uint8_t *bdaddr);
364 /** Used to a set the class of the device. */
365 void hci_write_class_of_device();
366 /**@}*/
367
368 /** @name L2CAP Commands */
369 /**
370 * Used to send L2CAP Commands.
371 * @param handle HCI Handle.
372 * @param data Data to send.
373 * @param nbytes Number of bytes to send.
374 * @param channelLow,channelHigh Low and high byte of channel to send to.
375 * If argument is omitted then the Standard L2CAP header: Channel ID (0x01) for ACL-U will be used.
376 */
377 void L2CAP_Command(uint16_t handle, uint8_t* data, uint8_t nbytes, uint8_t channelLow = 0x01, uint8_t channelHigh = 0x00);
378 /**
379 * L2CAP Connection Request.
380 * @param handle HCI handle.
381 * @param rxid Identifier.
382 * @param scid Source Channel Identifier.
383 * @param psm Protocol/Service Multiplexer - see: https://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm.
384 */
385 void l2cap_connection_request(uint16_t handle, uint8_t rxid, uint8_t* scid, uint16_t psm);
386 /**
387 * L2CAP Connection Response.
388 * @param handle HCI handle.
389 * @param rxid Identifier.
390 * @param dcid Destination Channel Identifier.
391 * @param scid Source Channel Identifier.
392 * @param result Result - First send ::PENDING and then ::SUCCESSFUL.
393 */
394 void l2cap_connection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid, uint8_t result);
395 /**
396 * L2CAP Config Request.
397 * @param handle HCI Handle.
398 * @param rxid Identifier.
399 * @param dcid Destination Channel Identifier.
400 */
401 void l2cap_config_request(uint16_t handle, uint8_t rxid, uint8_t* dcid);
402 /**
403 * L2CAP Config Response.
404 * @param handle HCI Handle.
405 * @param rxid Identifier.
406 * @param scid Source Channel Identifier.
407 */
408 void l2cap_config_response(uint16_t handle, uint8_t rxid, uint8_t* scid);
409 /**
410 * L2CAP Disconnection Request.
411 * @param handle HCI Handle.
412 * @param rxid Identifier.
413 * @param dcid Device Channel Identifier.
414 * @param scid Source Channel Identifier.
415 */
416 void l2cap_disconnection_request(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
417 /**
418 * L2CAP Disconnection Response.
419 * @param handle HCI Handle.
420 * @param rxid Identifier.
421 * @param dcid Device Channel Identifier.
422 * @param scid Source Channel Identifier.
423 */
424 void l2cap_disconnection_response(uint16_t handle, uint8_t rxid, uint8_t* dcid, uint8_t* scid);
425 /**
426 * L2CAP Information Response.
427 * @param handle HCI Handle.
428 * @param rxid Identifier.
429 * @param infoTypeLow,infoTypeHigh Infotype.
430 */
431 void l2cap_information_response(uint16_t handle, uint8_t rxid, uint8_t infoTypeLow, uint8_t infoTypeHigh);
432 /**@}*/
433
434 /** Use this to see if it is waiting for a incoming connection. */
435 bool watingForConnection;
436 /** This is used by the service to know when to store the device information. */
437 bool l2capConnectionClaimed;
438 /** This is used by the SPP library to claim the current SDP incoming request. */
439 bool sdpConnectionClaimed;
440 /** This is used by the SPP library to claim the current RFCOMM incoming request. */
441 bool rfcommConnectionClaimed;
442
443 /** The name you wish to make the dongle show up as. It is set automatically by the SPP library. */
444 const char* btdName;
445 /** The pin you wish to make the dongle use for authentication. It is set automatically by the SPP and BTHID library. */
446 const char* btdPin;
447
448 /** The bluetooth dongles Bluetooth address. */
449 uint8_t my_bdaddr[6];
450 /** HCI handle for the last connection. */
451 uint16_t hci_handle;
452 /** Last incoming devices Bluetooth address. */
453 uint8_t disc_bdaddr[6];
454 /** First 30 chars of last remote name. */
455 char remote_name[30];
456 /**
457 * The supported HCI Version read from the Bluetooth dongle.
458 * Used by the PS3BT library to check the HCI Version of the Bluetooth dongle,
459 * it should be at least 3 to work properly with the library.
460 */
461 uint8_t hci_version;
462
463 /** Call this function to pair with a Wiimote */
464 void pairWithWiimote() {
465 pairWithWii = true;
466 hci_state = HCI_CHECK_DEVICE_SERVICE;
467 };
468 /** Used to only send the ACL data to the Wiimote. */
469 bool connectToWii;
470 /** True if a Wiimote is connecting. */
471 bool incomingWii;
472 /** True when it should pair with a Wiimote. */
473 bool pairWithWii;
474 /** True if it's the new Wiimote with the Motion Plus Inside or a Wii U Pro Controller. */
475 bool motionPlusInside;
476 /** True if it's a Wii U Pro Controller. */
477 bool wiiUProController;
478
479 /** Call this function to pair with a Wiimote */
480 void pairWithHID() {
481 pairWithHIDDevice = true;
482 hci_state = HCI_CHECK_DEVICE_SERVICE;
483 };
484 /** Used to only send the ACL data to the Wiimote. */
485 bool connectToHIDDevice;
486 /** True if a Wiimote is connecting. */
487 bool incomingHIDDevice;
488 /** True when it should pair with a device like a mouse or keyboard. */
489 bool pairWithHIDDevice;
490
491 /**
492 * Read the poll interval taken from the endpoint descriptors.
493 * @return The poll interval in ms.
494 */
495 uint8_t readPollInterval() {
496 return pollInterval;
497 };
498
499 protected:
500 /** Pointer to USB class instance. */
501 USB *pUsb;
502 /** Device address. */
503 uint8_t bAddress;
504 /** Endpoint info structure. */
505 EpInfo epInfo[BTD_MAX_ENDPOINTS];
506
507 /** Configuration number. */
508 uint8_t bConfNum;
509 /** Total number of endpoints in the configuration. */
510 uint8_t bNumEP;
511 /** Next poll time based on poll interval taken from the USB descriptor. */
512 uint32_t qNextPollTime;
513
514 /** Bluetooth dongle control endpoint. */
515 static const uint8_t BTD_CONTROL_PIPE;
516 /** HCI event endpoint index. */
517 static const uint8_t BTD_EVENT_PIPE;
518 /** ACL In endpoint index. */
519 static const uint8_t BTD_DATAIN_PIPE;
520 /** ACL Out endpoint index. */
521 static const uint8_t BTD_DATAOUT_PIPE;
522
523 /**
524 * Used to print the USB Endpoint Descriptor.
525 * @param ep_ptr Pointer to USB Endpoint Descriptor.
526 */
527 void PrintEndpointDescriptor(const USB_ENDPOINT_DESCRIPTOR* ep_ptr);
528
529 private:
530 void Initialize(); // Set all variables, endpoint structs etc. to default values
531 BluetoothService *btService[BTD_NUM_SERVICES];
532
533 uint16_t PID, VID; // PID and VID of device connected
534
535 uint8_t pollInterval;
536 bool bPollEnable;
537
538 bool pairWiiUsingSync; // True if paring was done using the Wii SYNC button.
539 bool checkRemoteName; // Used to check remote device's name before connecting.
540 bool incomingPS4; // True if a PS4 controller is connecting
541 uint8_t classOfDevice[3]; // Class of device of last device
542
543 /* Variables used by high level HCI task */
544 uint8_t hci_state; // Current state of Bluetooth HCI connection
545 uint16_t hci_counter; // Counter used for Bluetooth HCI reset loops
546 uint16_t hci_num_reset_loops; // This value indicate how many times it should read before trying to reset
547 uint16_t hci_event_flag; // HCI flags of received Bluetooth events
548 uint8_t inquiry_counter;
549
550 uint8_t hcibuf[BULK_MAXPKTSIZE]; // General purpose buffer for HCI data
551 uint8_t l2capinbuf[BULK_MAXPKTSIZE]; // General purpose buffer for L2CAP in data
552 uint8_t l2capoutbuf[14]; // General purpose buffer for L2CAP out data
553
554 /* State machines */
555 void HCI_event_task(); // Poll the HCI event pipe
556 void HCI_task(); // HCI state machine
557 void ACL_event_task(); // ACL input pipe
558
559 /* Used to set the Bluetooth Address internally to the PS3 Controllers */
560 void setBdaddr(uint8_t* BDADDR);
561 void setMoveBdaddr(uint8_t* BDADDR);
562 };
563
564 /** All Bluetooth services should inherit this class. */
565 class BluetoothService {
566 public:
567 BluetoothService(BTD *p) : pBtd(p) {
568 if(pBtd)
569 pBtd->registerBluetoothService(this); // Register it as a Bluetooth service
570 };
571 /**
572 * Used to pass acldata to the Bluetooth service.
573 * @param ACLData Pointer to the incoming acldata.
574 */
575 virtual void ACLData(uint8_t* ACLData) = 0;
576 /** Used to run the different state machines in the Bluetooth service. */
577 virtual void Run() = 0;
578 /** Used to reset the Bluetooth service. */
579 virtual void Reset() = 0;
580 /** Used to disconnect both the L2CAP Channel and the HCI Connection for the Bluetooth service. */
581 virtual void disconnect() = 0;
582
583 /**
584 * Used to call your own function when the device is successfully initialized.
585 * @param funcOnInit Function to call.
586 */
587 void attachOnInit(void (*funcOnInit)(void)) {
588 pFuncOnInit = funcOnInit; // TODO: This really belong in a class of it's own as it is repeated several times
589 };
590
591 protected:
592 /**
593 * Called when a device is successfully initialized.
594 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
595 * This is useful for instance if you want to set the LEDs in a specific way.
596 */
597 virtual void onInit() = 0;
598
599 /** Used to check if the incoming L2CAP data matches the HCI Handle */
600 bool checkHciHandle(uint8_t *buf, uint16_t handle) {
601 return (buf[0] == (handle & 0xFF)) && (buf[1] == ((handle >> 8) | 0x20));
602 }
603
604 /** Pointer to function called in onInit(). */
605 void (*pFuncOnInit)(void);
606
607 /** Pointer to BTD instance. */
608 BTD *pBtd;
609
610 /** The HCI Handle for the connection. */
611 uint16_t hci_handle;
612
613 /** L2CAP flags of received Bluetooth events. */
614 uint32_t l2cap_event_flag;
615
616 /** Identifier for L2CAP commands. */
617 uint8_t identifier;
618 };
619
620 #endif
Imprint / Impressum