USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
BTHID.h
Go to the documentation of this file.
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 "controllerEnums.h"
23 #include "hidboot.h"
24 
25 /* Bluetooth L2CAP states for L2CAP_task() */
26 #define L2CAP_WAIT 0
27 
28 // These states are used if the device is the host
29 #define L2CAP_CONTROL_SUCCESS 1
30 #define L2CAP_INTERRUPT_SETUP 2
31 
32 // These states are used if the Arduino is the host
33 #define L2CAP_CONTROL_CONNECT_REQUEST 3
34 #define L2CAP_CONTROL_CONFIG_REQUEST 4
35 #define L2CAP_INTERRUPT_CONNECT_REQUEST 5
36 
37 #define L2CAP_INTERRUPT_CONFIG_REQUEST 6
38 #define L2CAP_DONE 7
39 
40 #define L2CAP_INTERRUPT_DISCONNECT 8
41 #define L2CAP_CONTROL_DISCONNECT 9
42 
43 /* L2CAP event flags */
44 #define L2CAP_FLAG_CONTROL_CONNECTED 0x01
45 #define L2CAP_FLAG_INTERRUPT_CONNECTED 0x02
46 #define L2CAP_FLAG_CONFIG_CONTROL_SUCCESS 0x04
47 #define L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS 0x08
48 #define L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE 0x10
49 #define L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE 0x20
50 #define L2CAP_FLAG_CONNECTION_CONTROL_REQUEST 0x40
51 #define L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST 0x80
52 
53 /* Macros for L2CAP event flag tests */
54 #define l2cap_connected_control_flag (l2cap_event_flag & L2CAP_FLAG_CONTROL_CONNECTED)
55 #define l2cap_connected_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_INTERRUPT_CONNECTED)
56 #define l2cap_config_success_control_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_CONTROL_SUCCESS)
57 #define l2cap_config_success_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONFIG_INTERRUPT_SUCCESS)
58 #define l2cap_disconnect_response_control_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_CONTROL_RESPONSE)
59 #define l2cap_disconnect_response_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_DISCONNECT_INTERRUPT_RESPONSE)
60 #define l2cap_connection_request_control_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_CONTROL_REQUEST)
61 #define l2cap_connection_request_interrupt_flag (l2cap_event_flag & L2CAP_FLAG_CONNECTION_INTERRUPT_REQUEST)
62 
63 #define KEYBOARD_PARSER_ID 0
64 #define MOUSE_PARSER_ID 1
65 #define epMUL 2
66 
68 class BTHID : public BluetoothService {
69 public:
76  BTHID(BTD *p, bool pair = false, const char *pin = "1234");
77 
83  virtual void ACLData(uint8_t* ACLData);
85  virtual void Run();
87  virtual void Reset();
89  virtual void disconnect();
93  return pRptParser[id];
94  };
95 
96  bool SetReportParser(uint8_t id, HIDReportParser *prs) {
97  pRptParser[id] = prs;
98  return true;
99  };
100 
101  void setProtocolMode(uint8_t mode) {
102  protocolMode = mode;
103  };
104 
106  bool connected;
107 
109  void pair(void) {
110  if (pBtd)
111  pBtd->pairWithHID();
112  };
113 
118  void attachOnInit(void (*funcOnInit)(void)) {
119  pFuncOnInit = funcOnInit;
120  };
121 
122 private:
123  BTD *pBtd; // Pointer to BTD instance
124 
125  HIDReportParser *pRptParser[epMUL];
126 
128  void setProtocol();
129  uint8_t protocolMode;
130 
136  void onInit() {
137  if (pFuncOnInit)
138  pFuncOnInit(); // Call the user function
139  }
140  void (*pFuncOnInit)(void); // Pointer to function called in onInit()
141 
142  void L2CAP_task(); // L2CAP state machine
143 
144  /* Variables filled from HCI event management */
145  uint16_t hci_handle;
146  bool activeConnection; // Used to indicate if it's already has established a connection
147 
148  /* Variables used by high level L2CAP task */
149  uint8_t l2cap_state;
150  uint8_t l2cap_event_flag; // l2cap flags of received Bluetooth events
151 
152  uint8_t ButtonState, OldButtonState, ButtonClickState;
153  int16_t xAxis, yAxis, scroll;
154 
155  /* L2CAP Channels */
156  uint8_t control_scid[2]; // L2CAP source CID for HID_Control
157  uint8_t control_dcid[2]; // 0x0070
158  uint8_t interrupt_scid[2]; // L2CAP source CID for HID_Interrupt
159  uint8_t interrupt_dcid[2]; // 0x0071
160  uint8_t identifier; // Identifier for connection
161 };
162 #endif
virtual void ACLData(uint8_t *ACLData)
Definition: BTHID.cpp:61
Definition: BTD.h:158
bool connected
Definition: BTHID.h:103
virtual void Run()
Definition: BTHID.cpp:347
virtual void Reset()
Definition: BTHID.cpp:47
void attachOnInit(void(*funcOnInit)(void))
Definition: BTHID.h:118
void pairWithHID()
Definition: BTD.h:441
virtual void disconnect()
Definition: BTHID.cpp:54
HIDReportParser * GetReportParser(uint8_t id)
Definition: BTHID.h:92
#define epMUL
Definition: BTHID.h:65
BTHID(BTD *p, bool pair=false, const char *pin="1234")
Definition: BTHID.cpp:23
Definition: BTHID.h:68
void setProtocolMode(uint8_t mode)
Definition: BTHID.h:101
void pair(void)
Definition: BTHID.h:109
bool SetReportParser(uint8_t id, HIDReportParser *prs)
Definition: BTHID.h:96