Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

BTHID.h 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. e-mail : [email protected]
  13. */
  14. #ifndef _bthid_h_
  15. #define _bthid_h_
  16. #include "BTD.h"
  17. #include "hidboot.h"
  18. #define KEYBOARD_PARSER_ID 0
  19. #define MOUSE_PARSER_ID 1
  20. #define NUM_PARSERS 2
  21. /** This BluetoothService class implements support for Bluetooth HID devices. */
  22. class BTHID : public BluetoothService {
  23. public:
  24. /**
  25. * Constructor for the BTHID class.
  26. * @param p Pointer to the BTD class instance.
  27. * @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.
  28. * @param pin Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
  29. */
  30. BTHID(BTD *p, bool pair = false, const char *pin = "0000");
  31. /** @name BluetoothService implementation */
  32. /** Used this to disconnect the devices. */
  33. void disconnect();
  34. /**@}*/
  35. /**
  36. * Get HIDReportParser.
  37. * @param id ID of parser.
  38. * @return Returns the corresponding HIDReportParser. Returns NULL if id is not valid.
  39. */
  40. HIDReportParser *GetReportParser(uint8_t id) {
  41. if (id >= NUM_PARSERS)
  42. return NULL;
  43. return pRptParser[id];
  44. };
  45. /**
  46. * Set HIDReportParser to be used.
  47. * @param id Id of parser.
  48. * @param prs Pointer to HIDReportParser.
  49. * @return Returns true if the HIDReportParser is set. False otherwise.
  50. */
  51. bool SetReportParser(uint8_t id, HIDReportParser *prs) {
  52. if (id >= NUM_PARSERS)
  53. return false;
  54. pRptParser[id] = prs;
  55. return true;
  56. };
  57. /**
  58. * Set HID protocol mode.
  59. * @param mode HID protocol to use. Either HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.
  60. */
  61. void setProtocolMode(uint8_t mode) {
  62. protocolMode = mode;
  63. };
  64. /**
  65. * Used to set the leds on a keyboard.
  66. * @param data See KBDLEDS in hidboot.h
  67. */
  68. void setLeds(uint8_t data);
  69. /** True if a device is connected */
  70. bool connected;
  71. /** Call this to start the paring sequence with a device */
  72. void pair(void) {
  73. if(pBtd)
  74. pBtd->pairWithHID();
  75. };
  76. protected:
  77. /** @name BluetoothService implementation */
  78. /**
  79. * Used to pass acldata to the services.
  80. * @param ACLData Incoming acldata.
  81. */
  82. void ACLData(uint8_t* ACLData);
  83. /** Used to run part of the state machine. */
  84. void Run();
  85. /** Use this to reset the service. */
  86. void Reset();
  87. /**
  88. * Called when a device is successfully initialized.
  89. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  90. * This is useful for instance if you want to set the LEDs in a specific way.
  91. */
  92. void onInit() {
  93. if(pFuncOnInit)
  94. pFuncOnInit(); // Call the user function
  95. OnInitBTHID();
  96. };
  97. /**@}*/
  98. /** @name Overridable functions */
  99. /**
  100. * Used to parse Bluetooth HID data to any class that inherits this class.
  101. * @param len The length of the incoming data.
  102. * @param buf Pointer to the data buffer.
  103. */
  104. virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
  105. return;
  106. };
  107. /** Called when a device is connected */
  108. virtual void OnInitBTHID() {
  109. return;
  110. };
  111. /** Used to reset any buffers in the class that inherits this */
  112. virtual void ResetBTHID() {
  113. return;
  114. }
  115. /**@}*/
  116. /** L2CAP source CID for HID_Control */
  117. uint8_t control_scid[2];
  118. /** L2CAP source CID for HID_Interrupt */
  119. uint8_t interrupt_scid[2];
  120. private:
  121. HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
  122. /** Set report protocol. */
  123. void setProtocol();
  124. uint8_t protocolMode;
  125. void L2CAP_task(); // L2CAP state machine
  126. bool activeConnection; // Used to indicate if it already has established a connection
  127. /* Variables used for L2CAP communication */
  128. uint8_t control_dcid[2]; // L2CAP device CID for HID_Control - Always 0x0070
  129. uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071
  130. uint8_t l2cap_state;
  131. };
  132. #endif