1 /* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
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
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
24 #define KEYBOARD_PARSER_ID 0
25 #define MOUSE_PARSER_ID 1
28 /** This BluetoothService class implements support for Bluetooth HID devices. */
29 class BTHID : public BluetoothService {
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.
37 BTHID(BTD *p, bool pair = false, const char *pin = "0000");
39 /** @name BluetoothService implementation */
40 /** Used this to disconnect the devices. */
45 * Get HIDReportParser.
46 * @param id ID of parser.
47 * @return Returns the corresponding HIDReportParser. Returns NULL if id is not valid.
49 HIDReportParser *GetReportParser(uint8_t id) {
50 if (id >= NUM_PARSERS)
52 return pRptParser[id];
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.
61 bool SetReportParser(uint8_t id, HIDReportParser *prs) {
62 if (id >= NUM_PARSERS)
69 * Set HID protocol mode.
70 * @param mode HID protocol to use. Either HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.
72 void setProtocolMode(uint8_t mode) {
77 * Used to set the leds on a keyboard.
78 * @param data See KBDLEDS in hidboot.h
80 void setLeds(uint8_t data);
82 /** True if a device is connected */
85 /** Call this to start the paring sequence with a device */
92 /** @name BluetoothService implementation */
94 * Used to pass acldata to the services.
95 * @param ACLData Incoming acldata.
97 void ACLData(uint8_t* ACLData);
98 /** Used to run part of the state machine. */
100 /** Use this to reset the service. */
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.
109 pFuncOnInit(); // Call the user function
114 /** @name Overridable functions */
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.
120 virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
123 /** Called when a device is connected */
124 virtual void OnInitBTHID() {
127 /** Used to reset any buffers in the class that inherits this */
128 virtual void ResetBTHID() {
133 /** L2CAP source CID for HID_Control */
134 uint8_t control_scid[2];
136 /** L2CAP source CID for HID_Interrupt */
137 uint8_t interrupt_scid[2];
140 HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
142 /** Set report protocol. */
144 uint8_t protocolMode;
146 void L2CAP_task(); // L2CAP state machine
148 bool activeConnection; // Used to indicate if it already has established a connection
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