Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BTHID.h Source File

BTHID.h

00001 /* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
00002 
00003  This software may be distributed and modified under the terms of the GNU
00004  General Public License version 2 (GPL2) as published by the Free Software
00005  Foundation and appearing in the file GPL2.TXT included in the packaging of
00006  this file. Please note that GPL2 Section 2[b] requires that all works based
00007  on this software must also be made publicly available under the terms of
00008  the GPL2 ("Copyleft").
00009 
00010  Contact information
00011  -------------------
00012 
00013  Kristian Lauszus, TKJ Electronics
00014  Web      :  http://www.tkjelectronics.com
00015  e-mail   :  kristianl@tkjelectronics.com
00016  */
00017 
00018 #ifndef _bthid_h_
00019 #define _bthid_h_
00020 
00021 #include "BTD.h"
00022 #include "hidboot.h"
00023 
00024 #define KEYBOARD_PARSER_ID      0
00025 #define MOUSE_PARSER_ID         1
00026 #define NUM_PARSERS             2
00027 
00028 /** This BluetoothService class implements support for Bluetooth HID devices. */
00029 class BTHID : public BluetoothService {
00030 public:
00031         /**
00032          * Constructor for the BTHID class.
00033          * @param  p   Pointer to the BTD class instance.
00034          * @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.
00035          * @param  pin   Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
00036          */
00037         BTHID(BTD *p, bool pair = false, const char *pin = "0000");
00038 
00039         /** @name BluetoothService implementation */
00040         /** Used this to disconnect the devices. */
00041         void disconnect();
00042         /**@}*/
00043 
00044         /**
00045          * Get HIDReportParser.
00046          * @param  id ID of parser.
00047          * @return    Returns the corresponding HIDReportParser. Returns NULL if id is not valid.
00048          */
00049         HIDReportParser *GetReportParser(uint8_t id) {
00050                 if (id >= NUM_PARSERS)
00051                         return NULL;
00052                 return pRptParser[id];
00053         };
00054 
00055         /**
00056          * Set HIDReportParser to be used.
00057          * @param  id  Id of parser.
00058          * @param  prs Pointer to HIDReportParser.
00059          * @return     Returns true if the HIDReportParser is set. False otherwise.
00060          */
00061         bool SetReportParser(uint8_t id, HIDReportParser *prs) {
00062                 if (id >= NUM_PARSERS)
00063                         return false;
00064                 pRptParser[id] = prs;
00065                 return true;
00066         };
00067 
00068         /**
00069          * Set HID protocol mode.
00070          * @param mode HID protocol to use. Either USB_HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.
00071          */
00072         void setProtocolMode(uint8_t mode) {
00073                 protocolMode = mode;
00074         };
00075 
00076         /**@{*/
00077         /**
00078          * Used to set the leds on a keyboard.
00079          * @param data See ::KBDLEDS in hidboot.h
00080          */
00081         void setLeds(struct KBDLEDS data) {
00082                 setLeds(*((uint8_t*)&data));
00083         };
00084         void setLeds(uint8_t data);
00085         /**@}*/
00086 
00087         /** True if a device is connected */
00088         bool connected;
00089 
00090         /** Call this to start the pairing sequence with a device */
00091         void pair(void) {
00092                 if(pBtd)
00093                         pBtd->pairWithHID();
00094         };
00095 
00096 protected:
00097         /** @name BluetoothService implementation */
00098         /**
00099          * Used to pass acldata to the services.
00100          * @param ACLData Incoming acldata.
00101          */
00102         void ACLData(uint8_t* ACLData);
00103         /** Used to run part of the state machine. */
00104         void Run();
00105         /** Use this to reset the service. */
00106         void Reset();
00107         /**
00108          * Called when a device is successfully initialized.
00109          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00110          * This is useful for instance if you want to set the LEDs in a specific way.
00111          */
00112         void onInit() {
00113                 if(pFuncOnInit)
00114                         pFuncOnInit(); // Call the user function
00115                 OnInitBTHID();
00116         };
00117         /**@}*/
00118 
00119         /** @name Overridable functions */
00120         /**
00121          * Used to parse Bluetooth HID data to any class that inherits this class.
00122          * @param len The length of the incoming data.
00123          * @param buf Pointer to the data buffer.
00124          */
00125         virtual void ParseBTHIDData(uint8_t len __attribute__((unused)), uint8_t *buf __attribute__((unused))) {
00126                 return;
00127         };
00128         /** Called when a device is connected */
00129         virtual void OnInitBTHID() {
00130                 return;
00131         };
00132         /** Used to reset any buffers in the class that inherits this */
00133         virtual void ResetBTHID() {
00134                 return;
00135         }
00136         /**@}*/
00137 
00138         /** L2CAP source CID for HID_Control */
00139         uint8_t control_scid[2];
00140 
00141         /** L2CAP source CID for HID_Interrupt */
00142         uint8_t interrupt_scid[2];
00143 
00144 private:
00145         HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
00146 
00147         /** Set report protocol. */
00148         void setProtocol();
00149         uint8_t protocolMode;
00150 
00151         void L2CAP_task(); // L2CAP state machine
00152 
00153         bool activeConnection; // Used to indicate if it already has established a connection
00154 
00155         /* Variables used for L2CAP communication */
00156         uint8_t control_dcid[2]; // L2CAP device CID for HID_Control - Always 0x0070
00157         uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071
00158         uint8_t l2cap_state;
00159 };
00160 #endif