USB Host Shield 2.0
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 "hidboot.h"
23 
24 #define KEYBOARD_PARSER_ID 0
25 #define MOUSE_PARSER_ID 1
26 #define NUM_PARSERS 2
27 
29 class BTHID : public BluetoothService {
30 public:
37  BTHID(BTD *p, bool pair = false, const char *pin = "0000");
38 
41  void disconnect();
50  if (id >= NUM_PARSERS)
51  return NULL;
52  return pRptParser[id];
53  };
54 
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 
72  void setProtocolMode(uint8_t mode) {
73  protocolMode = mode;
74  };
75 
81  void setLeds(struct KBDLEDS data) {
82  setLeds(*((uint8_t*)&data));
83  };
84  void setLeds(uint8_t data);
88  bool connected;
89 
91  void pair(void) {
92  if(pBtd)
93  pBtd->pairWithHID();
94  };
95 
101  uint32_t getLastMessageTime() {
102  return lastBtDataInputIntMillis;
103  };
104 
105 protected:
111  void ACLData(uint8_t* ACLData);
113  void Run();
115  void Reset();
121  void onInit() {
122  if(pFuncOnInit)
123  pFuncOnInit(); // Call the user function
124  OnInitBTHID();
125  };
134  virtual void ParseBTHIDData(uint8_t len __attribute__((unused)), uint8_t *buf __attribute__((unused))) {
135  return;
136  };
141  virtual void ParseBTHIDControlData(uint8_t len __attribute__((unused)), uint8_t *buf __attribute__((unused))) {
142  return;
143  }
145  virtual void OnInitBTHID() {
146  return;
147  };
149  virtual void ResetBTHID() {
150  return;
151  }
155  uint8_t control_scid[2];
156 
158  uint8_t interrupt_scid[2];
159 
161  uint8_t sdp_scid[2]; // L2CAP source CID for SDP
162 
163 private:
164  HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
165 
166  uint8_t l2capoutbuf[BULK_MAXPKTSIZE]; // General purpose buffer for l2cap out data
167  void SDP_Command(uint8_t* data, uint8_t nbytes);
168  void serviceNotSupported(uint8_t transactionIDHigh, uint8_t transactionIDLow);
169 
171  void setProtocol();
172  uint8_t protocolMode;
173 
174  void SDP_task();
175  void L2CAP_task(); // L2CAP state machine
176 
177  bool activeConnection; // Used to indicate if it already has established a connection
178  bool SDPConnected;
179 
180  /* Variables used for L2CAP communication */
181  uint8_t control_dcid[2]; // L2CAP device CID for HID_Control - Always 0x0070
182  uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071
183  uint8_t sdp_dcid[2];
184  uint8_t l2cap_state;
185 
186  uint32_t lastBtDataInputIntMillis; // Variable used to store the millis value of the last Bluetooth DATA input report received on the interrupt channel
187 };
188 #endif
#define BULK_MAXPKTSIZE
Definition: BTD.h:37
#define NUM_PARSERS
Definition: BTHID.h:26
Definition: BTD.h:222
void pairWithHID()
Definition: BTD.h:514
Definition: BTHID.h:29
uint8_t sdp_scid[2]
Definition: BTHID.h:161
uint32_t getLastMessageTime()
Definition: BTHID.h:101
bool SetReportParser(uint8_t id, HIDReportParser *prs)
Definition: BTHID.h:61
uint8_t interrupt_scid[2]
Definition: BTHID.h:158
virtual void ParseBTHIDData(uint8_t len, uint8_t *buf)
Definition: BTHID.h:134
BTHID(BTD *p, bool pair=false, const char *pin="0000")
Definition: BTHID.cpp:23
virtual void OnInitBTHID()
Definition: BTHID.h:145
void ACLData(uint8_t *ACLData)
Definition: BTHID.cpp:63
void Run()
Definition: BTHID.cpp:560
void onInit()
Definition: BTHID.h:121
bool connected
Definition: BTHID.h:88
virtual void ResetBTHID()
Definition: BTHID.h:149
uint8_t control_scid[2]
Definition: BTHID.h:155
virtual void ParseBTHIDControlData(uint8_t len, uint8_t *buf)
Definition: BTHID.h:141
void setLeds(struct KBDLEDS data)
Definition: BTHID.h:81
HIDReportParser * GetReportParser(uint8_t id)
Definition: BTHID.h:49
void disconnect()
Definition: BTHID.cpp:53
uint8_t l2cap_sdp_state
Definition: BTHID.h:160
void Reset()
Definition: BTHID.cpp:43
void pair(void)
Definition: BTHID.h:91
void setProtocolMode(uint8_t mode)
Definition: BTHID.h:72
void(* pFuncOnInit)(void)
Definition: BTD.h:644
BTD * pBtd
Definition: BTD.h:647