]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/BTHID.h
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / BTHID.h
1 /* Copyright (C) 2013 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 _bthid_h_
19 #define _bthid_h_
20
21 #include "BTD.h"
22 #include "hidboot.h"
23
24 #define KEYBOARD_PARSER_ID      0
25 #define MOUSE_PARSER_ID         1
26 #define NUM_PARSERS             2
27
28 /** This BluetoothService class implements support for Bluetooth HID devices. */
29 class BTHID : public BluetoothService {
30 public:
31         /**
32          * Constructor for the BTHID class.
33          * @param  p   Pointer to the BTD class instance.
34          * @param  pair   Set this to true in order to pair with the device. If the argument is omitted then it will not pair with it. One can use ::PAIR to set it to true.
35          * @param  pin   Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
36          */
37         BTHID(BTD *p, bool pair = false, const char *pin = "0000");
38
39         /** @name BluetoothService implementation */
40         /** Used this to disconnect the devices. */
41         void disconnect();
42         /**@}*/
43
44         /**
45          * Get HIDReportParser.
46          * @param  id ID of parser.
47          * @return    Returns the corresponding HIDReportParser. Returns NULL if id is not valid.
48          */
49         HIDReportParser *GetReportParser(uint8_t id) {
50                 if (id >= NUM_PARSERS)
51                         return NULL;
52                 return pRptParser[id];
53         };
54
55         /**
56          * Set HIDReportParser to be used.
57          * @param  id  Id of parser.
58          * @param  prs Pointer to HIDReportParser.
59          * @return     Returns true if the HIDReportParser is set. False otherwise.
60          */
61         bool SetReportParser(uint8_t id, HIDReportParser *prs) {
62                 if (id >= NUM_PARSERS)
63                         return false;
64                 pRptParser[id] = prs;
65                 return true;
66         };
67
68         /**
69          * Set HID protocol mode.
70          * @param mode HID protocol to use. Either HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.
71          */
72         void setProtocolMode(uint8_t mode) {
73                 protocolMode = mode;
74         };
75
76         /**
77          * Used to set the leds on a keyboard.
78          * @param data See KBDLEDS in hidboot.h
79          */
80         void setLeds(uint8_t data);
81
82         /** True if a device is connected */
83         bool connected;
84
85         /** Call this to start the paring sequence with a device */
86         void pair(void) {
87                 if(pBtd)
88                         pBtd->pairWithHID();
89         };
90
91 protected:
92         /** @name BluetoothService implementation */
93         /**
94          * Used to pass acldata to the services.
95          * @param ACLData Incoming acldata.
96          */
97         void ACLData(uint8_t* ACLData);
98         /** Used to run part of the state machine. */
99         void Run();
100         /** Use this to reset the service. */
101         void Reset();
102         /**
103          * Called when a device is successfully initialized.
104          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
105          * This is useful for instance if you want to set the LEDs in a specific way.
106          */
107         void onInit() {
108                 if(pFuncOnInit)
109                         pFuncOnInit(); // Call the user function
110                 OnInitBTHID();
111         };
112         /**@}*/
113
114         /** @name Overridable functions */
115         /**
116          * Used to parse Bluetooth HID data to any class that inherits this class.
117          * @param len The length of the incoming data.
118          * @param buf Pointer to the data buffer.
119          */
120         virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
121                 return;
122         };
123         /** Called when a device is connected */
124         virtual void OnInitBTHID() {
125                 return;
126         };
127         /** Used to reset any buffers in the class that inherits this */
128         virtual void ResetBTHID() {
129                 return;
130         }
131         /**@}*/
132
133         /** L2CAP source CID for HID_Control */
134         uint8_t control_scid[2];
135
136         /** L2CAP source CID for HID_Interrupt */
137         uint8_t interrupt_scid[2];
138
139 private:
140         HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
141
142         /** Set report protocol. */
143         void setProtocol();
144         uint8_t protocolMode;
145
146         void L2CAP_task(); // L2CAP state machine
147
148         bool activeConnection; // Used to indicate if it already has established a connection
149
150         /* Variables used for L2CAP communication */
151         uint8_t control_dcid[2]; // L2CAP device CID for HID_Control - Always 0x0070
152         uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071
153         uint8_t l2cap_state;
154 };
155 #endif