2013-11-24 21:55:15 +01:00
|
|
|
/* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Kristian Lauszus, TKJ Electronics
|
|
|
|
Web : http://www.tkjelectronics.com
|
|
|
|
e-mail : kristianl@tkjelectronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _bthid_h_
|
|
|
|
#define _bthid_h_
|
|
|
|
|
|
|
|
#include "BTD.h"
|
2013-11-24 23:51:45 +01:00
|
|
|
#include "hidboot.h"
|
2013-11-24 21:55:15 +01:00
|
|
|
|
2013-11-24 23:51:45 +01:00
|
|
|
#define KEYBOARD_PARSER_ID 0
|
|
|
|
#define MOUSE_PARSER_ID 1
|
2013-12-05 07:39:28 +01:00
|
|
|
#define NUM_PARSERS 2
|
2013-11-24 23:51:45 +01:00
|
|
|
|
2014-01-19 16:58:29 +01:00
|
|
|
/** This BluetoothService class implements support for Bluetooth HID devices. */
|
2013-11-24 21:55:15 +01:00
|
|
|
class BTHID : public BluetoothService {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor for the BTHID class.
|
|
|
|
* @param p Pointer to the BTD class instance.
|
|
|
|
* @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.
|
2013-11-25 02:20:57 +01:00
|
|
|
* @param pin Write the pin to BTD#btdPin. If argument is omitted, then "0000" will be used.
|
2013-11-24 21:55:15 +01:00
|
|
|
*/
|
2013-11-25 02:20:57 +01:00
|
|
|
BTHID(BTD *p, bool pair = false, const char *pin = "0000");
|
2013-11-24 21:55:15 +01:00
|
|
|
|
|
|
|
/** @name BluetoothService implementation */
|
2013-12-30 16:05:50 +01:00
|
|
|
/** Used this to disconnect the devices. */
|
2013-11-24 21:55:15 +01:00
|
|
|
virtual void disconnect();
|
|
|
|
/**@}*/
|
|
|
|
|
2013-12-30 16:08:03 +01:00
|
|
|
/**
|
|
|
|
* Get HIDReportParser.
|
|
|
|
* @param id ID of parser.
|
|
|
|
* @return Returns the corresponding HIDReportParser. Returns NULL if id is not valid.
|
|
|
|
*/
|
2013-11-24 23:51:45 +01:00
|
|
|
HIDReportParser *GetReportParser(uint8_t id) {
|
2013-12-30 16:08:03 +01:00
|
|
|
if (id >= NUM_PARSERS)
|
|
|
|
return NULL;
|
2013-11-24 23:51:45 +01:00
|
|
|
return pRptParser[id];
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:08:03 +01:00
|
|
|
/**
|
|
|
|
* Set HIDReportParser to be used.
|
|
|
|
* @param id Id of parser.
|
|
|
|
* @param prs Pointer to HIDReportParser.
|
|
|
|
* @return Returns true if the HIDReportParser is set. False otherwise.
|
|
|
|
*/
|
2013-11-24 23:51:45 +01:00
|
|
|
bool SetReportParser(uint8_t id, HIDReportParser *prs) {
|
2013-12-30 16:08:03 +01:00
|
|
|
if (id >= NUM_PARSERS)
|
|
|
|
return false;
|
2013-11-24 23:51:45 +01:00
|
|
|
pRptParser[id] = prs;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:05:50 +01:00
|
|
|
/**
|
|
|
|
* Set HID protocol mode.
|
|
|
|
* @param mode HID protocol to use. Either HID_BOOT_PROTOCOL or HID_RPT_PROTOCOL.
|
|
|
|
*/
|
2013-11-24 23:51:45 +01:00
|
|
|
void setProtocolMode(uint8_t mode) {
|
|
|
|
protocolMode = mode;
|
|
|
|
};
|
|
|
|
|
2013-12-30 16:05:50 +01:00
|
|
|
/**
|
|
|
|
* Used to set the leds on a keyboard.
|
|
|
|
* @param data See KBDLEDS in hidboot.h
|
|
|
|
*/
|
2013-11-25 01:45:24 +01:00
|
|
|
void setLeds(uint8_t data);
|
|
|
|
|
2013-11-24 21:55:15 +01:00
|
|
|
/** True if a device is connected */
|
|
|
|
bool connected;
|
|
|
|
|
2013-12-30 16:05:50 +01:00
|
|
|
/** Call this to start the paring sequence with a device */
|
2013-11-24 21:55:15 +01:00
|
|
|
void pair(void) {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(pBtd)
|
2013-11-24 21:55:15 +01:00
|
|
|
pBtd->pairWithHID();
|
2013-11-24 23:51:45 +01:00
|
|
|
};
|
2013-11-24 21:55:15 +01:00
|
|
|
|
2014-09-02 11:02:17 +02:00
|
|
|
protected:
|
|
|
|
/** @name BluetoothService implementation */
|
2013-11-24 21:55:15 +01:00
|
|
|
/**
|
2014-09-02 11:02:17 +02:00
|
|
|
* Used to pass acldata to the services.
|
|
|
|
* @param ACLData Incoming acldata.
|
2013-11-24 21:55:15 +01:00
|
|
|
*/
|
2014-09-02 11:02:17 +02:00
|
|
|
virtual void ACLData(uint8_t* ACLData);
|
|
|
|
/** Used to run part of the state machine. */
|
|
|
|
virtual void Run();
|
|
|
|
/** Use this to reset the service. */
|
|
|
|
virtual void Reset();
|
|
|
|
/**
|
|
|
|
* Called when a device is successfully initialized.
|
|
|
|
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
|
|
|
|
* This is useful for instance if you want to set the LEDs in a specific way.
|
|
|
|
*/
|
|
|
|
virtual void onInit() {
|
|
|
|
if(pFuncOnInit)
|
|
|
|
pFuncOnInit(); // Call the user function
|
|
|
|
OnInitBTHID();
|
2013-11-24 21:55:15 +01:00
|
|
|
};
|
2014-09-02 11:02:17 +02:00
|
|
|
/**@}*/
|
2013-11-24 21:55:15 +01:00
|
|
|
|
2014-01-18 22:36:01 +01:00
|
|
|
/** @name Overridable functions */
|
2014-01-12 17:20:18 +01:00
|
|
|
/**
|
2014-01-18 22:36:01 +01:00
|
|
|
* Used to parse Bluetooth HID data to any class that inherits this class.
|
|
|
|
* @param len The length of the incoming data.
|
|
|
|
* @param buf Pointer to the data buffer.
|
2014-01-12 17:20:18 +01:00
|
|
|
*/
|
2014-02-03 23:06:27 +01:00
|
|
|
virtual void ParseBTHIDData(uint8_t len, uint8_t *buf) {
|
2014-01-18 22:36:01 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
/** Called when a device is connected */
|
|
|
|
virtual void OnInitBTHID() {
|
|
|
|
return;
|
2014-01-12 17:20:18 +01:00
|
|
|
};
|
2014-01-18 22:36:01 +01:00
|
|
|
/** Used to reset any buffers in the class that inherits this */
|
|
|
|
virtual void ResetBTHID() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/**@}*/
|
2014-01-12 17:20:18 +01:00
|
|
|
|
2014-01-22 05:13:49 +01:00
|
|
|
/** L2CAP source CID for HID_Control */
|
|
|
|
uint8_t control_scid[2];
|
|
|
|
|
|
|
|
/** L2CAP source CID for HID_Interrupt */
|
|
|
|
uint8_t interrupt_scid[2];
|
|
|
|
|
2013-11-24 21:55:15 +01:00
|
|
|
private:
|
2013-12-30 16:05:50 +01:00
|
|
|
HIDReportParser *pRptParser[NUM_PARSERS]; // Pointer to HIDReportParsers.
|
2013-11-24 23:51:45 +01:00
|
|
|
|
2013-11-24 21:55:15 +01:00
|
|
|
/** Set report protocol. */
|
|
|
|
void setProtocol();
|
2013-11-24 23:51:45 +01:00
|
|
|
uint8_t protocolMode;
|
2013-11-24 21:55:15 +01:00
|
|
|
|
|
|
|
void L2CAP_task(); // L2CAP state machine
|
|
|
|
|
2013-12-30 16:05:50 +01:00
|
|
|
bool activeConnection; // Used to indicate if it already has established a connection
|
2013-11-24 21:55:15 +01:00
|
|
|
|
2014-01-22 05:13:49 +01:00
|
|
|
/* Variables used for L2CAP communication */
|
|
|
|
uint8_t control_dcid[2]; // L2CAP device CID for HID_Control - Always 0x0070
|
|
|
|
uint8_t interrupt_dcid[2]; // L2CAP device CID for HID_Interrupt - Always 0x0071
|
2013-11-24 21:55:15 +01:00
|
|
|
uint8_t l2cap_state;
|
|
|
|
};
|
2013-12-25 11:09:57 +01:00
|
|
|
#endif
|